Skip to content

Commit

Permalink
feat: add simple body parser
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimvh committed Jun 17, 2020
1 parent 70af469 commit d4f70d9
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .eslintignore
Expand Up @@ -7,3 +7,5 @@ coverage
**/*.js
**/*.d.ts
**/*.js.map

!external-types/*.d.ts
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -16,6 +16,7 @@ module.exports = {
'@typescript-eslint/space-before-function-paren': [ 'error', 'never' ],
'class-methods-use-this': 'off',
'comma-dangle': ['error', 'always-multiline'],
'dot-location': ['error', 'property'],
'lines-between-class-members': ['error', 'always', { exceptAfterSingleLine: true }],
'padding-line-between-statements': 'off',
'tsdoc/syntax': 'error',
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -9,3 +9,4 @@ coverage
!.eslintrc.js
!test/eslintrc.js
!jest.config.js
!external-types/*.d.ts
6 changes: 6 additions & 0 deletions external-types/arrayifyStream.d.ts
@@ -0,0 +1,6 @@
declare module 'arrayify-stream' {
import { Readable } from 'stream';

function arrayifyStream(input: Readable): Promise<any[]>;
export = arrayifyStream;
}
6 changes: 6 additions & 0 deletions external-types/streamifyArray.d.ts
@@ -0,0 +1,6 @@
declare module 'streamify-array' {
import { Readable } from 'stream';

function streamifyArray(input: any[]): Readable;
export = streamifyArray;
}
1 change: 1 addition & 0 deletions jest.config.js
Expand Up @@ -13,6 +13,7 @@ module.exports = {
"js"
],
"testEnvironment": "node",
"setupFilesAfterEnv": ["jest-rdf"],
"collectCoverage": true,
"coveragePathIgnorePatterns": [
"/node_modules/"
Expand Down
71 changes: 71 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Expand Up @@ -22,18 +22,23 @@
],
"dependencies": {
"@rdfjs/data-model": "^1.1.2",
"@types/n3": "^1.1.6",
"@types/node": "^14.0.1",
"@types/rdf-js": "^3.0.0"
"@types/rdf-js": "^3.0.0",
"n3": "^1.3.7"
},
"devDependencies": {
"@types/jest": "^25.2.1",
"@typescript-eslint/eslint-plugin": "^2.33.0",
"@typescript-eslint/parser": "^2.33.0",
"arrayify-stream": "^1.0.0",
"eslint": "^7.0.0",
"eslint-config-es": "^3.19.61",
"eslint-plugin-tsdoc": "^0.2.4",
"husky": "^4.2.5",
"jest": "^26.0.1",
"jest-rdf": "^1.5.0",
"streamify-array": "^1.0.1",
"ts-jest": "^26.0.0",
"typescript": "^3.9.2"
}
Expand Down
52 changes: 52 additions & 0 deletions src/ldp/http/SimpleBodyParser.ts
@@ -0,0 +1,52 @@
import { BodyParser } from './BodyParser';
import { HttpRequest } from '../../server/HttpRequest';
import { Quad } from 'rdf-js';
import { QuadRepresentation } from '../representation/QuadRepresentation';
import { RepresentationMetadata } from '../representation/RepresentationMetadata';
import { StreamParser } from 'n3';
import { TypedReadable } from '../../util/TypedReadable';
import { UnsupportedMediaTypeHttpError } from '../../util/errors/UnsupportedMediaTypeHttpError';
import 'jest-rdf';

export class SimpleBodyParser extends BodyParser {
private static readonly contentTypes = [
'application/n-quads',
'application/trig',
'application/n-triples',
'text/turtle',
'text/n3',
];

public async canHandle(input: HttpRequest): Promise<void> {
const contentType = input.headers['content-type'];

if (contentType && !SimpleBodyParser.contentTypes.some((type): boolean => contentType.includes(type))) {
throw new UnsupportedMediaTypeHttpError('This parser only supports RDF data.');
}
}

public async handle(input: HttpRequest): Promise<QuadRepresentation> {
const contentType = input.headers['content-type'];

if (!contentType) {
return undefined;
}

const specificType = contentType.split(';')[0];

const metadata: RepresentationMetadata = {
raw: [],
profiles: [],
contentType: specificType,
};

// StreamParser is a Readable but typings are incorrect at time of writing
const quads: TypedReadable<Quad> = input.pipe(new StreamParser()) as unknown as TypedReadable<Quad>;

return {
dataType: 'quad',
data: quads,
metadata,
};
}
}
7 changes: 7 additions & 0 deletions src/util/errors/UnsupportedMediaTypeHttpError.ts
@@ -0,0 +1,7 @@
import { HttpError } from './HttpError';

export class UnsupportedMediaTypeHttpError extends HttpError {
public constructor(message?: string) {
super(415, 'UnsupportedHttpError', message);
}
}
58 changes: 58 additions & 0 deletions test/unit/ldp/http/SimpleBodyParser.test.ts
@@ -0,0 +1,58 @@
import arrayifyStream from 'arrayify-stream';
import { HttpRequest } from '../../../../src/server/HttpRequest';
import { SimpleBodyParser } from '../../../../src/ldp/http/SimpleBodyParser';
import streamifyArray from 'streamify-array';
import { StreamParser } from 'n3';
import { UnsupportedMediaTypeHttpError } from '../../../../src/util/errors/UnsupportedMediaTypeHttpError';
import { namedNode, triple } from '@rdfjs/data-model';

const contentTypes = [
'application/n-quads',
'application/trig',
'application/n-triples',
'text/turtle',
'text/n3',
];

describe('A SimpleBodyparser', (): void => {
const bodyParser = new SimpleBodyParser();

it('rejects input with unsupported content type.', async(): Promise<void> => {
await expect(bodyParser.canHandle({ headers: { 'content-type': 'application/rdf+xml' }} as HttpRequest))
.rejects.toThrow(new UnsupportedMediaTypeHttpError('This parser only supports RDF data.'));
});

it('accepts input with no content type.', async(): Promise<void> => {
await expect(bodyParser.canHandle({ headers: { }} as HttpRequest)).resolves.toBeUndefined();
});

it('accepts turtle and similar content types.', async(): Promise<void> => {
for (const type of contentTypes) {
await expect(bodyParser.canHandle({ headers: { 'content-type': type }} as HttpRequest)).resolves.toBeUndefined();
}
});

it('returns empty output if there was no content-type.', async(): Promise<void> => {
await expect(bodyParser.handle({ headers: { }} as HttpRequest)).resolves.toBeUndefined();
});

it('returns a stream of quads if there was data.', async(): Promise<void> => {
const input = streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]) as HttpRequest;
input.headers = { 'content-type': 'text/turtle' };
const result = await bodyParser.handle(input);
expect(result).toEqual({
data: expect.any(StreamParser),
dataType: 'quad',
metadata: {
contentType: 'text/turtle',
profiles: [],
raw: [],
},
});
await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray([ triple(
namedNode('http://test.com/s'),
namedNode('http://test.com/p'),
namedNode('http://test.com/o'),
) ]);
});
});
2 changes: 2 additions & 0 deletions tsconfig.json
Expand Up @@ -5,6 +5,7 @@
"newLine": "lf",
"alwaysStrict": true,
"declaration": true,
"esModuleInterop": true,
"inlineSources": true,
"noImplicitAny": true,
"noImplicitThis": true,
Expand All @@ -14,6 +15,7 @@
"stripInternal": true
},
"include": [
"external-types/**/*.ts",
"src/**/*.ts",
"test/**/*.ts"
],
Expand Down

0 comments on commit d4f70d9

Please sign in to comment.