mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Set up server using express
This commit is contained in:
parent
c53ab5ed9b
commit
a9dc59bf78
65
bin/server.ts
Normal file
65
bin/server.ts
Normal file
@ -0,0 +1,65 @@
|
||||
import yargs from 'yargs';
|
||||
import {
|
||||
AcceptPreferenceParser,
|
||||
AuthenticatedLdpHandler,
|
||||
CompositeAsyncHandler,
|
||||
ExpressHttpServer,
|
||||
Operation,
|
||||
ResponseDescription,
|
||||
SimpleAuthorizer,
|
||||
SimpleBodyParser,
|
||||
SimpleCredentialsExtractor,
|
||||
SimpleDeleteOperationHandler,
|
||||
SimpleGetOperationHandler,
|
||||
SimplePermissionsExtractor,
|
||||
SimplePostOperationHandler,
|
||||
SimpleRequestParser,
|
||||
SimpleResourceStore,
|
||||
SimpleResponseWriter,
|
||||
SimpleTargetExtractor,
|
||||
} from '..';
|
||||
|
||||
const { argv } = yargs
|
||||
.usage('node ./bin/server.js [args]')
|
||||
.options({
|
||||
port: { type: 'number', alias: 'p', default: 3000 },
|
||||
})
|
||||
.help();
|
||||
|
||||
const { port } = argv;
|
||||
|
||||
// This is instead of the dependency injection that still needs to be added
|
||||
const requestParser = new SimpleRequestParser({
|
||||
targetExtractor: new SimpleTargetExtractor(),
|
||||
preferenceParser: new AcceptPreferenceParser(),
|
||||
bodyParser: new SimpleBodyParser(),
|
||||
});
|
||||
|
||||
const credentialsExtractor = new SimpleCredentialsExtractor();
|
||||
const permissionsExtractor = new SimplePermissionsExtractor();
|
||||
const authorizer = new SimpleAuthorizer();
|
||||
|
||||
// Will have to see how to best handle this
|
||||
const store = new SimpleResourceStore(`http://localhost:${port}/`);
|
||||
const operationHandler = new CompositeAsyncHandler<Operation, ResponseDescription>([
|
||||
new SimpleGetOperationHandler(store),
|
||||
new SimplePostOperationHandler(store),
|
||||
new SimpleDeleteOperationHandler(store),
|
||||
]);
|
||||
|
||||
const responseWriter = new SimpleResponseWriter();
|
||||
|
||||
const httpHandler = new AuthenticatedLdpHandler({
|
||||
requestParser,
|
||||
credentialsExtractor,
|
||||
permissionsExtractor,
|
||||
authorizer,
|
||||
operationHandler,
|
||||
responseWriter,
|
||||
});
|
||||
|
||||
const httpServer = new ExpressHttpServer(httpHandler);
|
||||
|
||||
httpServer.listen(port);
|
||||
|
||||
process.stdout.write(`Running at http://localhost:${port}/\n`);
|
76
index.ts
Normal file
76
index.ts
Normal file
@ -0,0 +1,76 @@
|
||||
// Authentication
|
||||
export * from './src/authentication/Credentials';
|
||||
export * from './src/authentication/CredentialsExtractor';
|
||||
export * from './src/authentication/SimpleCredentialsExtractor';
|
||||
|
||||
// Authorization
|
||||
export * from './src/authorization/Authorizer';
|
||||
export * from './src/authorization/SimpleAuthorizer';
|
||||
|
||||
// LDP/HTTP
|
||||
export * from './src/ldp/http/AcceptPreferenceParser';
|
||||
export * from './src/ldp/http/BodyParser';
|
||||
export * from './src/ldp/http/Patch';
|
||||
export * from './src/ldp/http/PreferenceParser';
|
||||
export * from './src/ldp/http/RequestParser';
|
||||
export * from './src/ldp/http/ResponseWriter';
|
||||
export * from './src/ldp/http/SimpleBodyParser';
|
||||
export * from './src/ldp/http/SimpleRequestParser';
|
||||
export * from './src/ldp/http/SimpleResponseWriter';
|
||||
export * from './src/ldp/http/SimpleTargetExtractor';
|
||||
export * from './src/ldp/http/TargetExtractor';
|
||||
|
||||
// LDP/Operations
|
||||
export * from './src/ldp/operations/Operation';
|
||||
export * from './src/ldp/operations/OperationHandler';
|
||||
export * from './src/ldp/operations/ResponseDescription';
|
||||
export * from './src/ldp/operations/SimpleDeleteOperationHandler';
|
||||
export * from './src/ldp/operations/SimpleGetOperationHandler';
|
||||
export * from './src/ldp/operations/SimplePostOperationHandler';
|
||||
|
||||
// LDP/Permissions
|
||||
export * from './src/ldp/permissions/PermissionSet';
|
||||
export * from './src/ldp/permissions/PermissionsExtractor';
|
||||
export * from './src/ldp/permissions/SimplePermissionsExtractor';
|
||||
|
||||
// LDP/Representation
|
||||
export * from './src/ldp/representation/BinaryRepresentation';
|
||||
export * from './src/ldp/representation/NamedRepresentation';
|
||||
export * from './src/ldp/representation/QuadRepresentation';
|
||||
export * from './src/ldp/representation/Representation';
|
||||
export * from './src/ldp/representation/RepresentationMetadata';
|
||||
export * from './src/ldp/representation/RepresentationPreference';
|
||||
export * from './src/ldp/representation/RepresentationPreferences';
|
||||
export * from './src/ldp/representation/ResourceIdentifier';
|
||||
|
||||
// LDP
|
||||
export * from './src/ldp/AuthenticatedLdpHandler';
|
||||
|
||||
// Server
|
||||
export * from './src/server/ExpressHttpServer';
|
||||
export * from './src/server/HttpHandler';
|
||||
export * from './src/server/HttpRequest';
|
||||
export * from './src/server/HttpResponse';
|
||||
|
||||
// Storage
|
||||
export * from './src/storage/AtomicResourceStore';
|
||||
export * from './src/storage/Conditions';
|
||||
export * from './src/storage/Lock';
|
||||
export * from './src/storage/RepresentationConverter';
|
||||
export * from './src/storage/ResourceLocker';
|
||||
export * from './src/storage/ResourceMapper';
|
||||
export * from './src/storage/ResourceStore';
|
||||
export * from './src/storage/SimpleResourceStore';
|
||||
|
||||
// Util/Errors
|
||||
export * from './src/util/errors/HttpError';
|
||||
export * from './src/util/errors/NotFoundHttpError';
|
||||
export * from './src/util/errors/UnsupportedHttpError';
|
||||
export * from './src/util/errors/UnsupportedMediaTypeHttpError';
|
||||
|
||||
// Util
|
||||
export * from './src/util/AcceptParser';
|
||||
export * from './src/util/AsyncHandler';
|
||||
export * from './src/util/CompositeAsyncHandler';
|
||||
export * from './src/util/TypedReadable';
|
||||
export * from './src/util/Util';
|
1265
package-lock.json
generated
1265
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
19
package.json
19
package.json
@ -4,11 +4,14 @@
|
||||
"version": "0.0.1",
|
||||
"main": "index.js",
|
||||
"license": "MIT",
|
||||
"bin": "bin/server.js",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"coveralls": "jest --coverage && cat ./coverage/lcov.info | coveralls",
|
||||
"lint": "eslint . --ext .ts",
|
||||
"test": "jest"
|
||||
"start": "node ./bin/server.js -p 3000",
|
||||
"test": "jest",
|
||||
"watch": "nodemon --watch \"src/**/*.js\" --watch \"bin/**/*.js\" --exec npm start"
|
||||
},
|
||||
"husky": {
|
||||
"hooks": {
|
||||
@ -18,21 +21,29 @@
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.d.ts",
|
||||
"bin/*.js",
|
||||
"bin/*.d.ts",
|
||||
"src/**/*.js",
|
||||
"src/**/*.d.ts"
|
||||
],
|
||||
"dependencies": {
|
||||
"@rdfjs/data-model": "^1.1.2",
|
||||
"@types/cors": "^2.8.6",
|
||||
"@types/express": "^4.17.6",
|
||||
"@types/n3": "^1.4.0",
|
||||
"@types/node": "^14.0.1",
|
||||
"@types/rdf-js": "^3.0.0",
|
||||
"n3": "^1.4.0"
|
||||
"@types/yargs": "^15.0.5",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.17.1",
|
||||
"n3": "^1.4.0",
|
||||
"yargs": "^15.4.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/arrayify-stream": "^1.0.0",
|
||||
"@types/express": "^4.17.6",
|
||||
"@types/jest": "^25.2.1",
|
||||
"@types/streamify-array": "^1.0.0",
|
||||
"@types/supertest": "^2.0.10",
|
||||
"@typescript-eslint/eslint-plugin": "^2.33.0",
|
||||
"@typescript-eslint/parser": "^2.33.0",
|
||||
"arrayify-stream": "^1.0.0",
|
||||
@ -44,7 +55,9 @@
|
||||
"jest": "^26.0.1",
|
||||
"jest-rdf": "^1.5.0",
|
||||
"node-mocks-http": "^1.8.1",
|
||||
"nodemon": "^2.0.4",
|
||||
"streamify-array": "^1.0.1",
|
||||
"supertest": "^4.0.2",
|
||||
"ts-jest": "^26.0.0",
|
||||
"typescript": "^3.9.2"
|
||||
}
|
||||
|
28
src/server/ExpressHttpServer.ts
Normal file
28
src/server/ExpressHttpServer.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import cors from 'cors';
|
||||
import express from 'express';
|
||||
import { HttpHandler } from './HttpHandler';
|
||||
import { Server } from 'http';
|
||||
|
||||
export class ExpressHttpServer {
|
||||
private readonly handler: HttpHandler;
|
||||
|
||||
public constructor(handler: HttpHandler) {
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
public listen(port?: number): Server {
|
||||
const app = express();
|
||||
|
||||
app.use(cors({
|
||||
// Based on https://github.com/solid/solid-spec/blob/master/recommendations-server.md#cors---cross-origin-resource-sharing
|
||||
// By default origin is always '*', this forces it to be the origin header if there is one
|
||||
origin: (origin, callback): void => callback(null, (origin || '*') as any),
|
||||
methods: [ 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE' ],
|
||||
}));
|
||||
|
||||
app.use(async(request, response): Promise<void> => {
|
||||
await this.handler.handleSafe({ request, response });
|
||||
});
|
||||
return app.listen(port);
|
||||
}
|
||||
}
|
77
test/unit/server/ExpressHttpServer.test.ts
Normal file
77
test/unit/server/ExpressHttpServer.test.ts
Normal file
@ -0,0 +1,77 @@
|
||||
import { ExpressHttpServer } from '../../../src/server/ExpressHttpServer';
|
||||
import { HttpHandler } from '../../../src/server/HttpHandler';
|
||||
import { HttpRequest } from '../../../src/server/HttpRequest';
|
||||
import { HttpResponse } from '../../../src/server/HttpResponse';
|
||||
import request from 'supertest';
|
||||
import { Server } from 'http';
|
||||
|
||||
const handle = async(input: { request: HttpRequest; response: HttpResponse }): Promise<void> => {
|
||||
input.response.writeHead(200);
|
||||
input.response.end();
|
||||
};
|
||||
|
||||
class SimpleHttpHandler extends HttpHandler {
|
||||
public async canHandle(): Promise<void> {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public async handle(input: { request: HttpRequest; response: HttpResponse }): Promise<void> {
|
||||
return handle(input);
|
||||
}
|
||||
}
|
||||
|
||||
describe('ExpressHttpServer', (): void => {
|
||||
let server: Server;
|
||||
let canHandleJest: jest.Mock<Promise<void>, []>;
|
||||
let handleJest: jest.Mock<Promise<void>, [any]>;
|
||||
beforeEach(async(): Promise<void> => {
|
||||
const handler = new SimpleHttpHandler();
|
||||
canHandleJest = jest.fn(async(): Promise<void> => undefined);
|
||||
handleJest = jest.fn(async(input): Promise<void> => handle(input));
|
||||
|
||||
handler.canHandle = canHandleJest;
|
||||
handler.handle = handleJest;
|
||||
|
||||
const expressServer = new ExpressHttpServer(handler);
|
||||
server = expressServer.listen();
|
||||
});
|
||||
|
||||
afterEach(async(): Promise<void> => {
|
||||
// Close server
|
||||
server.close();
|
||||
});
|
||||
|
||||
it('returns CORS headers for an OPTIONS request.', async(): Promise<void> => {
|
||||
const res = await request(server)
|
||||
.options('/')
|
||||
.set('Access-Control-Request-Headers', 'content-type')
|
||||
.set('Access-Control-Request-Method', 'POST')
|
||||
.set('Host', 'test.com')
|
||||
.expect(204);
|
||||
expect(res.header).toEqual(expect.objectContaining({
|
||||
'access-control-allow-origin': '*',
|
||||
'access-control-allow-headers': 'content-type',
|
||||
}));
|
||||
const corsMethods = res.header['access-control-allow-methods'].split(',').map((method: string): string => method.trim());
|
||||
const allowedMethods = [ 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE' ];
|
||||
expect(corsMethods).toEqual(expect.arrayContaining(allowedMethods));
|
||||
expect(corsMethods.length).toBe(allowedMethods.length);
|
||||
});
|
||||
|
||||
it('specifies CORS origin header if an origin was supplied.', async(): Promise<void> => {
|
||||
const res = await request(server).get('/').set('origin', 'test.com').expect(200);
|
||||
expect(res.header).toEqual(expect.objectContaining({ 'access-control-allow-origin': 'test.com' }));
|
||||
});
|
||||
|
||||
it('sends incoming requests to the handler.', async(): Promise<void> => {
|
||||
await request(server).get('/').set('Host', 'test.com').expect(200);
|
||||
expect(canHandleJest).toHaveBeenCalledTimes(1);
|
||||
expect(handleJest).toHaveBeenCalledTimes(1);
|
||||
expect(handleJest).toHaveBeenLastCalledWith({
|
||||
request: expect.objectContaining({
|
||||
headers: expect.objectContaining({ host: 'test.com' }),
|
||||
}),
|
||||
response: expect.objectContaining({}),
|
||||
});
|
||||
});
|
||||
});
|
@ -15,6 +15,8 @@
|
||||
"stripInternal": true
|
||||
},
|
||||
"include": [
|
||||
"index.ts",
|
||||
"bin/**/*.ts",
|
||||
"src/**/*.ts",
|
||||
"test/**/*.ts"
|
||||
],
|
||||
|
Loading…
x
Reference in New Issue
Block a user