mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: add simple request parser
This commit is contained in:
parent
09eb665c12
commit
cf258d0317
@ -7,5 +7,3 @@ coverage
|
|||||||
**/*.js
|
**/*.js
|
||||||
**/*.d.ts
|
**/*.d.ts
|
||||||
**/*.js.map
|
**/*.js.map
|
||||||
|
|
||||||
!external-types/*.d.ts
|
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -9,4 +9,3 @@ coverage
|
|||||||
!.eslintrc.js
|
!.eslintrc.js
|
||||||
!test/eslintrc.js
|
!test/eslintrc.js
|
||||||
!jest.config.js
|
!jest.config.js
|
||||||
!external-types/*.d.ts
|
|
||||||
|
6
external-types/arrayifyStream.d.ts
vendored
6
external-types/arrayifyStream.d.ts
vendored
@ -1,6 +0,0 @@
|
|||||||
declare module 'arrayify-stream' {
|
|
||||||
import { Readable } from 'stream';
|
|
||||||
|
|
||||||
function arrayifyStream(input: Readable): Promise<any[]>;
|
|
||||||
export = arrayifyStream;
|
|
||||||
}
|
|
6
external-types/streamifyArray.d.ts
vendored
6
external-types/streamifyArray.d.ts
vendored
@ -1,6 +0,0 @@
|
|||||||
declare module 'streamify-array' {
|
|
||||||
import { Readable } from 'stream';
|
|
||||||
|
|
||||||
function streamifyArray(input: any[]): Readable;
|
|
||||||
export = streamifyArray;
|
|
||||||
}
|
|
18
package-lock.json
generated
18
package-lock.json
generated
@ -699,6 +699,15 @@
|
|||||||
"@sinonjs/commons": "^1.7.0"
|
"@sinonjs/commons": "^1.7.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"@types/arrayify-stream": {
|
||||||
|
"version": "1.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/arrayify-stream/-/arrayify-stream-1.0.0.tgz",
|
||||||
|
"integrity": "sha512-rTx+OXoOJxoscixxecOqaEABaN6Qh/BNImDBnytLVrDI+glba4LJ7RS9JGRcc7auLtdFcM4s8+fdN6pb/K24OA==",
|
||||||
|
"dev": true,
|
||||||
|
"requires": {
|
||||||
|
"@types/node": "*"
|
||||||
|
}
|
||||||
|
},
|
||||||
"@types/babel__core": {
|
"@types/babel__core": {
|
||||||
"version": "7.1.7",
|
"version": "7.1.7",
|
||||||
"resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.1.7.tgz",
|
"resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.1.7.tgz",
|
||||||
@ -908,6 +917,15 @@
|
|||||||
"integrity": "sha512-l42BggppR6zLmpfU6fq9HEa2oGPEI8yrSPL3GITjfRInppYFahObbIQOQK3UGxEnyQpltZLaPe75046NOZQikw==",
|
"integrity": "sha512-l42BggppR6zLmpfU6fq9HEa2oGPEI8yrSPL3GITjfRInppYFahObbIQOQK3UGxEnyQpltZLaPe75046NOZQikw==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
"@types/streamify-array": {
|
||||||
|
"version": "1.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/streamify-array/-/streamify-array-1.0.0.tgz",
|
||||||
|
"integrity": "sha512-qBRnXKNEF8ejRM7TODp3bXIFnHjDfrUM3cTpCU8hnkrI5FHH708wGTo4jc/2VnyNDd73sNYtt3un2pT+9E1y1A==",
|
||||||
|
"dev": true,
|
||||||
|
"requires": {
|
||||||
|
"@types/node": "*"
|
||||||
|
}
|
||||||
|
},
|
||||||
"@types/yargs": {
|
"@types/yargs": {
|
||||||
"version": "15.0.4",
|
"version": "15.0.4",
|
||||||
"resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-15.0.4.tgz",
|
"resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-15.0.4.tgz",
|
||||||
|
@ -28,7 +28,9 @@
|
|||||||
"n3": "^1.3.7"
|
"n3": "^1.3.7"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/arrayify-stream": "^1.0.0",
|
||||||
"@types/jest": "^25.2.1",
|
"@types/jest": "^25.2.1",
|
||||||
|
"@types/streamify-array": "^1.0.0",
|
||||||
"@typescript-eslint/eslint-plugin": "^2.33.0",
|
"@typescript-eslint/eslint-plugin": "^2.33.0",
|
||||||
"@typescript-eslint/parser": "^2.33.0",
|
"@typescript-eslint/parser": "^2.33.0",
|
||||||
"arrayify-stream": "^1.0.0",
|
"arrayify-stream": "^1.0.0",
|
||||||
|
@ -6,7 +6,6 @@ import { RepresentationMetadata } from '../representation/RepresentationMetadata
|
|||||||
import { StreamParser } from 'n3';
|
import { StreamParser } from 'n3';
|
||||||
import { TypedReadable } from '../../util/TypedReadable';
|
import { TypedReadable } from '../../util/TypedReadable';
|
||||||
import { UnsupportedMediaTypeHttpError } from '../../util/errors/UnsupportedMediaTypeHttpError';
|
import { UnsupportedMediaTypeHttpError } from '../../util/errors/UnsupportedMediaTypeHttpError';
|
||||||
import 'jest-rdf';
|
|
||||||
|
|
||||||
export class SimpleBodyParser extends BodyParser {
|
export class SimpleBodyParser extends BodyParser {
|
||||||
private static readonly contentTypes = [
|
private static readonly contentTypes = [
|
||||||
@ -32,12 +31,12 @@ export class SimpleBodyParser extends BodyParser {
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const specificType = contentType.split(';')[0];
|
const mediaType = contentType.split(';')[0];
|
||||||
|
|
||||||
const metadata: RepresentationMetadata = {
|
const metadata: RepresentationMetadata = {
|
||||||
raw: [],
|
raw: [],
|
||||||
profiles: [],
|
profiles: [],
|
||||||
contentType: specificType,
|
contentType: mediaType,
|
||||||
};
|
};
|
||||||
|
|
||||||
// StreamParser is a Readable but typings are incorrect at time of writing
|
// StreamParser is a Readable but typings are incorrect at time of writing
|
||||||
|
@ -20,7 +20,7 @@ export class SimplePreferenceParser extends PreferenceParser {
|
|||||||
// Datetime can have commas so requires separate rules
|
// Datetime can have commas so requires separate rules
|
||||||
let datetime;
|
let datetime;
|
||||||
if (input.headers['accept-datetime']) {
|
if (input.headers['accept-datetime']) {
|
||||||
datetime = [{ value: input.headers['accept-datetime'] as string }];
|
datetime = [{ value: input.headers['accept-datetime'] as string, weight: 1 }];
|
||||||
}
|
}
|
||||||
|
|
||||||
return { type, charset, datetime, language };
|
return { type, charset, datetime, language };
|
||||||
@ -34,7 +34,7 @@ export class SimplePreferenceParser extends PreferenceParser {
|
|||||||
return header.split(',').map((preference): RepresentationPreference => {
|
return header.split(',').map((preference): RepresentationPreference => {
|
||||||
const parts = preference.split(';');
|
const parts = preference.split(';');
|
||||||
if (parts.length === 1) {
|
if (parts.length === 1) {
|
||||||
return { value: parts[0].trim() };
|
return { value: parts[0].trim(), weight: 1 };
|
||||||
}
|
}
|
||||||
return { value: parts[0].trim(), weight: parseFloat(parts[1].trim().slice('q='.length)) };
|
return { value: parts[0].trim(), weight: parseFloat(parts[1].trim().slice('q='.length)) };
|
||||||
});
|
});
|
||||||
|
43
src/ldp/http/SimpleRequestParser.ts
Normal file
43
src/ldp/http/SimpleRequestParser.ts
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import { BodyParser } from './BodyParser';
|
||||||
|
import { HttpRequest } from '../../server/HttpRequest';
|
||||||
|
import { Operation } from '../operations/Operation';
|
||||||
|
import { PreferenceParser } from './PreferenceParser';
|
||||||
|
import { RequestParser } from './RequestParser';
|
||||||
|
import { TargetExtractor } from './TargetExtractor';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Input parsers required for a {@link SimpleRequestParser}.
|
||||||
|
*/
|
||||||
|
export interface SimpleRequestParserArgs {
|
||||||
|
targetExtractor: TargetExtractor;
|
||||||
|
preferenceParser: PreferenceParser;
|
||||||
|
bodyParser: BodyParser;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class SimpleRequestParser extends RequestParser {
|
||||||
|
private readonly targetExtractor: TargetExtractor;
|
||||||
|
private readonly preferenceParser: PreferenceParser;
|
||||||
|
private readonly bodyParser: BodyParser;
|
||||||
|
|
||||||
|
public constructor(args: SimpleRequestParserArgs) {
|
||||||
|
super();
|
||||||
|
Object.assign(this, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async canHandle(input: HttpRequest): Promise<void> {
|
||||||
|
if (!input.url) {
|
||||||
|
throw new Error('Missing URL.');
|
||||||
|
}
|
||||||
|
if (!input.method) {
|
||||||
|
throw new Error('Missing method.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async handle(input: HttpRequest): Promise<Operation> {
|
||||||
|
const target = await this.targetExtractor.handleSafe(input);
|
||||||
|
const preferences = await this.preferenceParser.handleSafe(input);
|
||||||
|
const body = await this.bodyParser.handleSafe(input);
|
||||||
|
|
||||||
|
return { method: input.method, target, preferences, body };
|
||||||
|
}
|
||||||
|
}
|
@ -9,5 +9,5 @@ export interface RepresentationPreference {
|
|||||||
/**
|
/**
|
||||||
* How important this preference is in a value going from 0 to 1.
|
* How important this preference is in a value going from 0 to 1.
|
||||||
*/
|
*/
|
||||||
weight?: number;
|
weight: number;
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
*/
|
*/
|
||||||
export interface ResourceIdentifier {
|
export interface ResourceIdentifier {
|
||||||
/**
|
/**
|
||||||
* Path to the relevant resource. Usually this would be an URL.
|
* Path to the relevant resource.
|
||||||
*/
|
*/
|
||||||
path: string;
|
path: string;
|
||||||
}
|
}
|
||||||
|
52
test/integration/RequestParser.test.ts
Normal file
52
test/integration/RequestParser.test.ts
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
import arrayifyStream from 'arrayify-stream';
|
||||||
|
import { HttpRequest } from '../../src/server/HttpRequest';
|
||||||
|
import { SimpleBodyParser } from '../../src/ldp/http/SimpleBodyParser';
|
||||||
|
import { SimplePreferenceParser } from '../../src/ldp/http/SimplePreferenceParser';
|
||||||
|
import { SimpleRequestParser } from '../../src/ldp/http/SimpleRequestParser';
|
||||||
|
import { SimpleTargetExtractor } from '../../src/ldp/http/SimpleTargetExtractor';
|
||||||
|
import streamifyArray from 'streamify-array';
|
||||||
|
import { StreamParser } from 'n3';
|
||||||
|
import { namedNode, triple } from '@rdfjs/data-model';
|
||||||
|
|
||||||
|
describe('A SimpleRequestParser with simple input parsers', (): void => {
|
||||||
|
const targetExtractor = new SimpleTargetExtractor();
|
||||||
|
const bodyParser = new SimpleBodyParser();
|
||||||
|
const preferenceParser = new SimplePreferenceParser();
|
||||||
|
const requestParser = new SimpleRequestParser({ targetExtractor, bodyParser, preferenceParser });
|
||||||
|
|
||||||
|
it('can parse an incoming request.', async(): Promise<void> => {
|
||||||
|
const request = streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]) as HttpRequest;
|
||||||
|
request.method = 'POST';
|
||||||
|
request.url = 'http://test.com/';
|
||||||
|
request.headers = {
|
||||||
|
accept: 'text/turtle; q=0.8',
|
||||||
|
'accept-language': 'en-gb, en;q=0.5',
|
||||||
|
'content-type': 'text/turtle',
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = await requestParser.handle(request);
|
||||||
|
expect(result).toEqual({
|
||||||
|
method: 'POST',
|
||||||
|
target: { path: 'http://test.com/' },
|
||||||
|
preferences: {
|
||||||
|
type: [{ value: 'text/turtle', weight: 0.8 }],
|
||||||
|
language: [{ value: 'en-gb', weight: 1 }, { value: 'en', weight: 0.5 }],
|
||||||
|
},
|
||||||
|
body: {
|
||||||
|
data: expect.any(StreamParser),
|
||||||
|
dataType: 'quad',
|
||||||
|
metadata: {
|
||||||
|
contentType: 'text/turtle',
|
||||||
|
profiles: [],
|
||||||
|
raw: [],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(arrayifyStream(result.body.data)).resolves.toEqualRdfQuadArray([ triple(
|
||||||
|
namedNode('http://test.com/s'),
|
||||||
|
namedNode('http://test.com/p'),
|
||||||
|
namedNode('http://test.com/o'),
|
||||||
|
) ]);
|
||||||
|
});
|
||||||
|
});
|
@ -5,6 +5,7 @@ import streamifyArray from 'streamify-array';
|
|||||||
import { StreamParser } from 'n3';
|
import { StreamParser } from 'n3';
|
||||||
import { UnsupportedMediaTypeHttpError } from '../../../../src/util/errors/UnsupportedMediaTypeHttpError';
|
import { UnsupportedMediaTypeHttpError } from '../../../../src/util/errors/UnsupportedMediaTypeHttpError';
|
||||||
import { namedNode, triple } from '@rdfjs/data-model';
|
import { namedNode, triple } from '@rdfjs/data-model';
|
||||||
|
import 'jest-rdf';
|
||||||
|
|
||||||
const contentTypes = [
|
const contentTypes = [
|
||||||
'application/n-quads',
|
'application/n-quads',
|
||||||
|
@ -14,21 +14,21 @@ describe('A SimplePreferenceParser', (): void => {
|
|||||||
|
|
||||||
it('parses accept headers.', async(): Promise<void> => {
|
it('parses accept headers.', async(): Promise<void> => {
|
||||||
await expect(preferenceParser.handle({ headers: { accept: 'audio/*; q=0.2, audio/basic' }} as HttpRequest))
|
await expect(preferenceParser.handle({ headers: { accept: 'audio/*; q=0.2, audio/basic' }} as HttpRequest))
|
||||||
.resolves.toEqual({ type: [{ value: 'audio/*', weight: 0.2 }, { value: 'audio/basic' }]});
|
.resolves.toEqual({ type: [{ value: 'audio/*', weight: 0.2 }, { value: 'audio/basic', weight: 1 }]});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('parses accept-charset headers.', async(): Promise<void> => {
|
it('parses accept-charset headers.', async(): Promise<void> => {
|
||||||
await expect(preferenceParser.handle({ headers: { 'accept-charset': 'iso-8859-5, unicode-1-1;q=0.8' }} as unknown as HttpRequest))
|
await expect(preferenceParser.handle({ headers: { 'accept-charset': 'iso-8859-5, unicode-1-1;q=0.8' }} as unknown as HttpRequest))
|
||||||
.resolves.toEqual({ charset: [{ value: 'iso-8859-5' }, { value: 'unicode-1-1', weight: 0.8 }]});
|
.resolves.toEqual({ charset: [{ value: 'iso-8859-5', weight: 1 }, { value: 'unicode-1-1', weight: 0.8 }]});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('parses accept-datetime headers.', async(): Promise<void> => {
|
it('parses accept-datetime headers.', async(): Promise<void> => {
|
||||||
await expect(preferenceParser.handle({ headers: { 'accept-datetime': 'Tue, 20 Mar 2001 20:35:00 GMT' }} as unknown as HttpRequest))
|
await expect(preferenceParser.handle({ headers: { 'accept-datetime': 'Tue, 20 Mar 2001 20:35:00 GMT' }} as unknown as HttpRequest))
|
||||||
.resolves.toEqual({ datetime: [{ value: 'Tue, 20 Mar 2001 20:35:00 GMT' }]});
|
.resolves.toEqual({ datetime: [{ value: 'Tue, 20 Mar 2001 20:35:00 GMT', weight: 1 }]});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('parses accept-language headers.', async(): Promise<void> => {
|
it('parses accept-language headers.', async(): Promise<void> => {
|
||||||
await expect(preferenceParser.handle({ headers: { 'accept-language': 'da, en-gb;q=0.8, en;q=0.7' }} as HttpRequest))
|
await expect(preferenceParser.handle({ headers: { 'accept-language': 'da, en-gb;q=0.8, en;q=0.7' }} as HttpRequest))
|
||||||
.resolves.toEqual({ language: [{ value: 'da' }, { value: 'en-gb', weight: 0.8 }, { value: 'en', weight: 0.7 }]});
|
.resolves.toEqual({ language: [{ value: 'da', weight: 1 }, { value: 'en-gb', weight: 0.8 }, { value: 'en', weight: 0.7 }]});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
40
test/unit/ldp/http/SimpleRequestParser.test.ts
Normal file
40
test/unit/ldp/http/SimpleRequestParser.test.ts
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
import { BodyParser } from '../../../../src/ldp/http/BodyParser';
|
||||||
|
import { PreferenceParser } from '../../../../src/ldp/http/PreferenceParser';
|
||||||
|
import { SimpleRequestParser } from '../../../../src/ldp/http/SimpleRequestParser';
|
||||||
|
import { StaticAsyncHandler } from '../../../util/StaticAsyncHandler';
|
||||||
|
import { TargetExtractor } from '../../../../src/ldp/http/TargetExtractor';
|
||||||
|
|
||||||
|
describe('A SimpleRequestParser', (): void => {
|
||||||
|
let targetExtractor: TargetExtractor;
|
||||||
|
let bodyParser: BodyParser;
|
||||||
|
let preferenceParser: PreferenceParser;
|
||||||
|
let requestParser: SimpleRequestParser;
|
||||||
|
|
||||||
|
beforeEach(async(): Promise<void> => {
|
||||||
|
targetExtractor = new StaticAsyncHandler(true, 'target' as any);
|
||||||
|
bodyParser = new StaticAsyncHandler(true, 'body' as any);
|
||||||
|
preferenceParser = new StaticAsyncHandler(true, 'preference' as any);
|
||||||
|
requestParser = new SimpleRequestParser({ targetExtractor, bodyParser, preferenceParser });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can handle input with both a URL and a method.', async(): Promise<void> => {
|
||||||
|
await expect(requestParser.canHandle({ url: 'url', method: 'GET' } as any)).resolves.toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects input with no URL.', async(): Promise<void> => {
|
||||||
|
await expect(requestParser.canHandle({ method: 'GET' } as any)).rejects.toThrow('Missing URL.');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects input with no method.', async(): Promise<void> => {
|
||||||
|
await expect(requestParser.canHandle({ url: 'url' } as any)).rejects.toThrow('Missing method.');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns the output of all input parsers after calling handle.', async(): Promise<void> => {
|
||||||
|
await expect(requestParser.handle({ url: 'url', method: 'GET' } as any)).resolves.toEqual({
|
||||||
|
method: 'GET',
|
||||||
|
target: 'target',
|
||||||
|
preferences: 'preference',
|
||||||
|
body: 'body',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -15,7 +15,6 @@
|
|||||||
"stripInternal": true
|
"stripInternal": true
|
||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
"external-types/**/*.ts",
|
|
||||||
"src/**/*.ts",
|
"src/**/*.ts",
|
||||||
"test/**/*.ts"
|
"test/**/*.ts"
|
||||||
],
|
],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user