feat: Specifiy constants in separate file

This commit is contained in:
Joachim Van Herwegen 2020-07-24 16:17:22 +02:00
parent aaba113563
commit 14db5fed91
11 changed files with 43 additions and 31 deletions

View File

@ -1,4 +1,5 @@
import { BodyParser } from './BodyParser'; import { BodyParser } from './BodyParser';
import { DATA_TYPE_QUAD } from '../../util/ContentTypes';
import { HttpRequest } from '../../server/HttpRequest'; import { HttpRequest } from '../../server/HttpRequest';
import { PassThrough } from 'stream'; import { PassThrough } from 'stream';
import { QuadRepresentation } from '../representation/QuadRepresentation'; import { QuadRepresentation } from '../representation/QuadRepresentation';
@ -53,7 +54,7 @@ export class SimpleBodyParser extends BodyParser {
data.on('error', (error): boolean => errorStream.emit('error', new UnsupportedHttpError(error.message))); data.on('error', (error): boolean => errorStream.emit('error', new UnsupportedHttpError(error.message)));
return { return {
dataType: 'quad', dataType: DATA_TYPE_QUAD,
data: errorStream, data: errorStream,
metadata, metadata,
}; };

View File

@ -1,3 +1,4 @@
import { DATA_TYPE_BINARY } from '../../util/ContentTypes';
import { HttpError } from '../../util/errors/HttpError'; import { HttpError } from '../../util/errors/HttpError';
import { HttpResponse } from '../../server/HttpResponse'; import { HttpResponse } from '../../server/HttpResponse';
import { ResponseDescription } from '../operations/ResponseDescription'; import { ResponseDescription } from '../operations/ResponseDescription';
@ -11,8 +12,8 @@ export class SimpleResponseWriter extends ResponseWriter {
public async canHandle(input: { response: HttpResponse; result: ResponseDescription | Error }): Promise<void> { public async canHandle(input: { response: HttpResponse; result: ResponseDescription | Error }): Promise<void> {
if (!(input.result instanceof Error)) { if (!(input.result instanceof Error)) {
const dataType = input.result.body?.dataType; const dataType = input.result.body?.dataType;
if (dataType && dataType !== 'binary' && dataType !== 'string') { if (dataType && dataType !== DATA_TYPE_BINARY) {
throw new UnsupportedHttpError('Only string or binary results are supported.'); throw new UnsupportedHttpError('Only binary results are supported.');
} }
} }
} }

View File

@ -11,6 +11,7 @@ import { ResourceStore } from './ResourceStore';
import streamifyArray from 'streamify-array'; import streamifyArray from 'streamify-array';
import { StreamWriter } from 'n3'; import { StreamWriter } from 'n3';
import { UnsupportedMediaTypeHttpError } from '../util/errors/UnsupportedMediaTypeHttpError'; import { UnsupportedMediaTypeHttpError } from '../util/errors/UnsupportedMediaTypeHttpError';
import { CONTENT_TYPE_QUADS, DATA_TYPE_BINARY, DATA_TYPE_QUAD } from '../util/ContentTypes';
/** /**
* Resource store storing its data as Quads in an in-memory map. * Resource store storing its data as Quads in an in-memory map.
@ -113,7 +114,7 @@ export class SimpleResourceStore implements ResourceStore {
* @returns Promise of array of Quads pulled from the stream. * @returns Promise of array of Quads pulled from the stream.
*/ */
private async parseRepresentation(representation: Representation): Promise<Quad[]> { private async parseRepresentation(representation: Representation): Promise<Quad[]> {
if (representation.dataType !== 'quad') { if (representation.dataType !== DATA_TYPE_QUAD) {
throw new UnsupportedMediaTypeHttpError('SimpleResourceStore only supports quad representations.'); throw new UnsupportedMediaTypeHttpError('SimpleResourceStore only supports quad representations.');
} }
return arrayifyStream(representation.data); return arrayifyStream(representation.data);
@ -134,7 +135,7 @@ export class SimpleResourceStore implements ResourceStore {
*/ */
private generateRepresentation(data: Quad[], preferences: RepresentationPreferences): Representation { private generateRepresentation(data: Quad[], preferences: RepresentationPreferences): Representation {
// Always return turtle unless explicitly asked for quads // Always return turtle unless explicitly asked for quads
if (preferences.type?.some((preference): boolean => preference.value.includes('internal/quads'))) { if (preferences.type?.some((preference): boolean => preference.value.includes(CONTENT_TYPE_QUADS))) {
return this.generateQuadRepresentation(data); return this.generateQuadRepresentation(data);
} }
return this.generateBinaryRepresentation(data); return this.generateBinaryRepresentation(data);
@ -148,7 +149,7 @@ export class SimpleResourceStore implements ResourceStore {
*/ */
private generateBinaryRepresentation(data: Quad[]): BinaryRepresentation { private generateBinaryRepresentation(data: Quad[]): BinaryRepresentation {
return { return {
dataType: 'binary', dataType: DATA_TYPE_BINARY,
data: streamifyArray([ ...data ]).pipe(new StreamWriter({ format: 'text/turtle' })), data: streamifyArray([ ...data ]).pipe(new StreamWriter({ format: 'text/turtle' })),
metadata: { raw: [], profiles: [], contentType: 'text/turtle' }, metadata: { raw: [], profiles: [], contentType: 'text/turtle' },
}; };
@ -162,7 +163,7 @@ export class SimpleResourceStore implements ResourceStore {
*/ */
private generateQuadRepresentation(data: Quad[]): QuadRepresentation { private generateQuadRepresentation(data: Quad[]): QuadRepresentation {
return { return {
dataType: 'quad', dataType: DATA_TYPE_QUAD,
data: streamifyArray([ ...data ]), data: streamifyArray([ ...data ]),
metadata: { raw: [], profiles: []}, metadata: { raw: [], profiles: []},
}; };

View File

@ -11,6 +11,7 @@ import { someTerms } from 'rdf-terms';
import { SparqlUpdatePatch } from '../../ldp/http/SparqlUpdatePatch'; import { SparqlUpdatePatch } from '../../ldp/http/SparqlUpdatePatch';
import { Store } from 'n3'; import { Store } from 'n3';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError'; import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import { CONTENT_TYPE_QUADS, DATA_TYPE_QUAD } from '../../util/ContentTypes';
/** /**
* PatchHandler that supports specific types of SPARQL updates. * PatchHandler that supports specific types of SPARQL updates.
@ -55,7 +56,7 @@ export class SimpleSparqlUpdatePatchHandler extends PatchHandler {
const lock = await this.locker.acquire(input.identifier); const lock = await this.locker.acquire(input.identifier);
const quads = await this.source.getRepresentation(input.identifier, const quads = await this.source.getRepresentation(input.identifier,
{ type: [{ value: 'internal/quads', weight: 1 }]}); { type: [{ value: CONTENT_TYPE_QUADS, weight: 1 }]});
const store = new Store<BaseQuad>(); const store = new Store<BaseQuad>();
const importEmitter = store.import(quads.data); const importEmitter = store.import(quads.data);
await new Promise((resolve, reject): void => { await new Promise((resolve, reject): void => {
@ -66,11 +67,11 @@ export class SimpleSparqlUpdatePatchHandler extends PatchHandler {
store.addQuads(inserts); store.addQuads(inserts);
const representation: Representation = { const representation: Representation = {
data: store.match() as Readable, data: store.match() as Readable,
dataType: 'quad', dataType: DATA_TYPE_QUAD,
metadata: { metadata: {
raw: [], raw: [],
profiles: [], profiles: [],
contentType: 'internal/quads', contentType: CONTENT_TYPE_QUADS,
}, },
}; };
await this.source.setRepresentation(input.identifier, representation); await this.source.setRepresentation(input.identifier, representation);

4
src/util/ContentTypes.ts Normal file
View File

@ -0,0 +1,4 @@
export const DATA_TYPE_BINARY = 'binary';
export const DATA_TYPE_QUAD = 'quad';
export const CONTENT_TYPE_QUADS = 'internal/quads';

View File

@ -1,5 +1,6 @@
import { AcceptPreferenceParser } from '../../src/ldp/http/AcceptPreferenceParser'; import { AcceptPreferenceParser } from '../../src/ldp/http/AcceptPreferenceParser';
import arrayifyStream from 'arrayify-stream'; import arrayifyStream from 'arrayify-stream';
import { DATA_TYPE_QUAD } from '../../src/util/ContentTypes';
import { HttpRequest } from '../../src/server/HttpRequest'; import { HttpRequest } from '../../src/server/HttpRequest';
import { Readable } from 'stream'; import { Readable } from 'stream';
import { SimpleBodyParser } from '../../src/ldp/http/SimpleBodyParser'; import { SimpleBodyParser } from '../../src/ldp/http/SimpleBodyParser';
@ -35,7 +36,7 @@ describe('A SimpleRequestParser with simple input parsers', (): void => {
}, },
body: { body: {
data: expect.any(Readable), data: expect.any(Readable),
dataType: 'quad', dataType: DATA_TYPE_QUAD,
metadata: { metadata: {
contentType: 'text/turtle', contentType: 'text/turtle',
profiles: [], profiles: [],

View File

@ -1,4 +1,5 @@
import arrayifyStream from 'arrayify-stream'; import arrayifyStream from 'arrayify-stream';
import { DATA_TYPE_QUAD } from '../../../../src/util/ContentTypes';
import { HttpRequest } from '../../../../src/server/HttpRequest'; import { HttpRequest } from '../../../../src/server/HttpRequest';
import { Readable } from 'stream'; import { Readable } from 'stream';
import { SimpleBodyParser } from '../../../../src/ldp/http/SimpleBodyParser'; import { SimpleBodyParser } from '../../../../src/ldp/http/SimpleBodyParser';
@ -44,7 +45,7 @@ describe('A SimpleBodyparser', (): void => {
const result = (await bodyParser.handle(input))!; const result = (await bodyParser.handle(input))!;
expect(result).toEqual({ expect(result).toEqual({
data: expect.any(Readable), data: expect.any(Readable),
dataType: 'quad', dataType: DATA_TYPE_QUAD,
metadata: { metadata: {
contentType: 'text/turtle', contentType: 'text/turtle',
profiles: [], profiles: [],

View File

@ -5,6 +5,7 @@ import { SimpleResponseWriter } from '../../../../src/ldp/http/SimpleResponseWri
import streamifyArray from 'streamify-array'; import streamifyArray from 'streamify-array';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError'; import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
import { createResponse, MockResponse } from 'node-mocks-http'; import { createResponse, MockResponse } from 'node-mocks-http';
import { DATA_TYPE_BINARY, DATA_TYPE_QUAD } from '../../../../src/util/ContentTypes';
describe('A SimpleResponseWriter', (): void => { describe('A SimpleResponseWriter', (): void => {
const writer = new SimpleResponseWriter(); const writer = new SimpleResponseWriter();
@ -15,11 +16,9 @@ describe('A SimpleResponseWriter', (): void => {
}); });
it('requires the description body to be a string or binary stream if present.', async(): Promise<void> => { it('requires the description body to be a string or binary stream if present.', async(): Promise<void> => {
await expect(writer.canHandle({ response, result: { body: { dataType: 'quad' }} as ResponseDescription })) await expect(writer.canHandle({ response, result: { body: { dataType: DATA_TYPE_QUAD }} as ResponseDescription }))
.rejects.toThrow(UnsupportedHttpError); .rejects.toThrow(UnsupportedHttpError);
await expect(writer.canHandle({ response, result: { body: { dataType: 'string' }} as ResponseDescription })) await expect(writer.canHandle({ response, result: { body: { dataType: DATA_TYPE_BINARY }} as ResponseDescription }))
.resolves.toBeUndefined();
await expect(writer.canHandle({ response, result: { body: { dataType: 'binary' }} as ResponseDescription }))
.resolves.toBeUndefined(); .resolves.toBeUndefined();
}); });
@ -33,7 +32,7 @@ describe('A SimpleResponseWriter', (): void => {
it('responds with a body if the description has a body.', async(done): Promise<void> => { it('responds with a body if the description has a body.', async(done): Promise<void> => {
const body = { const body = {
data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]), data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]),
dataType: 'binary', dataType: DATA_TYPE_BINARY,
metadata: { metadata: {
raw: [] as Quad[], raw: [] as Quad[],
profiles: [] as string[], profiles: [] as string[],
@ -54,7 +53,7 @@ describe('A SimpleResponseWriter', (): void => {
it('responds with a content-type if the metadata has it.', async(done): Promise<void> => { it('responds with a content-type if the metadata has it.', async(done): Promise<void> => {
const body = { const body = {
data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]), data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]),
dataType: 'binary', dataType: DATA_TYPE_BINARY,
metadata: { metadata: {
raw: [] as Quad[], raw: [] as Quad[],
profiles: [] as string[], profiles: [] as string[],

View File

@ -1,3 +1,4 @@
import { DATA_TYPE_QUAD } from '../../../../src/util/ContentTypes';
import { Operation } from '../../../../src/ldp/operations/Operation'; import { Operation } from '../../../../src/ldp/operations/Operation';
import { Representation } from '../../../../src/ldp/representation/Representation'; import { Representation } from '../../../../src/ldp/representation/Representation';
import { ResourceStore } from '../../../../src/storage/ResourceStore'; import { ResourceStore } from '../../../../src/storage/ResourceStore';
@ -6,7 +7,7 @@ import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHtt
describe('A SimpleGetOperationHandler', (): void => { describe('A SimpleGetOperationHandler', (): void => {
const store = { const store = {
getRepresentation: async(): Promise<Representation> => ({ dataType: 'quad' } as Representation), getRepresentation: async(): Promise<Representation> => ({ dataType: DATA_TYPE_QUAD } as Representation),
} as unknown as ResourceStore; } as unknown as ResourceStore;
const handler = new SimpleGetOperationHandler(store); const handler = new SimpleGetOperationHandler(store);
@ -17,7 +18,7 @@ describe('A SimpleGetOperationHandler', (): void => {
it('returns the representation from the store with the input identifier.', async(): Promise<void> => { it('returns the representation from the store with the input identifier.', async(): Promise<void> => {
await expect(handler.handle({ target: { path: 'url' }} as Operation)).resolves.toEqual( await expect(handler.handle({ target: { path: 'url' }} as Operation)).resolves.toEqual(
{ identifier: { path: 'url' }, body: { dataType: 'quad' }}, { identifier: { path: 'url' }, body: { dataType: DATA_TYPE_QUAD }},
); );
}); });
}); });

View File

@ -6,6 +6,7 @@ import { RepresentationMetadata } from '../../../src/ldp/representation/Represen
import { SimpleResourceStore } from '../../../src/storage/SimpleResourceStore'; import { SimpleResourceStore } from '../../../src/storage/SimpleResourceStore';
import streamifyArray from 'streamify-array'; import streamifyArray from 'streamify-array';
import { UnsupportedMediaTypeHttpError } from '../../../src/util/errors/UnsupportedMediaTypeHttpError'; import { UnsupportedMediaTypeHttpError } from '../../../src/util/errors/UnsupportedMediaTypeHttpError';
import { CONTENT_TYPE_QUADS, DATA_TYPE_BINARY, DATA_TYPE_QUAD } from '../../../src/util/ContentTypes';
import { namedNode, triple } from '@rdfjs/data-model'; import { namedNode, triple } from '@rdfjs/data-model';
const base = 'http://test.com/'; const base = 'http://test.com/';
@ -24,7 +25,7 @@ describe('A SimpleResourceStore', (): void => {
representation = { representation = {
data: streamifyArray([ quad ]), data: streamifyArray([ quad ]),
dataType: 'quad', dataType: DATA_TYPE_QUAD,
metadata: {} as RepresentationMetadata, metadata: {} as RepresentationMetadata,
}; };
}); });
@ -43,16 +44,16 @@ describe('A SimpleResourceStore', (): void => {
}); });
it('errors for wrong input data types.', async(): Promise<void> => { it('errors for wrong input data types.', async(): Promise<void> => {
(representation as any).dataType = 'binary'; (representation as any).dataType = DATA_TYPE_BINARY;
await expect(store.addResource({ path: base }, representation)).rejects.toThrow(UnsupportedMediaTypeHttpError); await expect(store.addResource({ path: base }, representation)).rejects.toThrow(UnsupportedMediaTypeHttpError);
}); });
it('can write and read data.', async(): Promise<void> => { it('can write and read data.', async(): Promise<void> => {
const identifier = await store.addResource({ path: base }, representation); const identifier = await store.addResource({ path: base }, representation);
expect(identifier.path.startsWith(base)).toBeTruthy(); expect(identifier.path.startsWith(base)).toBeTruthy();
const result = await store.getRepresentation(identifier, { type: [{ value: 'internal/quads', weight: 1 }]}); const result = await store.getRepresentation(identifier, { type: [{ value: CONTENT_TYPE_QUADS, weight: 1 }]});
expect(result).toEqual({ expect(result).toEqual({
dataType: 'quad', dataType: DATA_TYPE_QUAD,
data: expect.any(Readable), data: expect.any(Readable),
metadata: { metadata: {
profiles: [], profiles: [],
@ -74,7 +75,7 @@ describe('A SimpleResourceStore', (): void => {
expect(identifier.path.startsWith(base)).toBeTruthy(); expect(identifier.path.startsWith(base)).toBeTruthy();
const result = await store.getRepresentation(identifier, { type: [{ value: 'text/turtle', weight: 1 }]}); const result = await store.getRepresentation(identifier, { type: [{ value: 'text/turtle', weight: 1 }]});
expect(result).toEqual({ expect(result).toEqual({
dataType: 'binary', dataType: DATA_TYPE_BINARY,
data: expect.any(Readable), data: expect.any(Readable),
metadata: { metadata: {
profiles: [], profiles: [],
@ -92,7 +93,7 @@ describe('A SimpleResourceStore', (): void => {
expect(identifier.path.startsWith(base)).toBeTruthy(); expect(identifier.path.startsWith(base)).toBeTruthy();
const result = await store.getRepresentation(identifier, { }); const result = await store.getRepresentation(identifier, { });
expect(result).toEqual({ expect(result).toEqual({
dataType: 'binary', dataType: DATA_TYPE_BINARY,
data: expect.any(Readable), data: expect.any(Readable),
metadata: { metadata: {
profiles: [], profiles: [],
@ -107,9 +108,9 @@ describe('A SimpleResourceStore', (): void => {
it('can set data.', async(): Promise<void> => { it('can set data.', async(): Promise<void> => {
await store.setRepresentation({ path: base }, representation); await store.setRepresentation({ path: base }, representation);
const result = await store.getRepresentation({ path: base }, { type: [{ value: 'internal/quads', weight: 1 }]}); const result = await store.getRepresentation({ path: base }, { type: [{ value: CONTENT_TYPE_QUADS, weight: 1 }]});
expect(result).toEqual({ expect(result).toEqual({
dataType: 'quad', dataType: DATA_TYPE_QUAD,
data: expect.any(Readable), data: expect.any(Readable),
metadata: { metadata: {
profiles: [], profiles: [],

View File

@ -8,6 +8,7 @@ import { SparqlUpdatePatch } from '../../../../src/ldp/http/SparqlUpdatePatch';
import streamifyArray from 'streamify-array'; import streamifyArray from 'streamify-array';
import { translate } from 'sparqlalgebrajs'; import { translate } from 'sparqlalgebrajs';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError'; import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
import { CONTENT_TYPE_QUADS, DATA_TYPE_QUAD } from '../../../../src/util/ContentTypes';
import { namedNode, quad } from '@rdfjs/data-model'; import { namedNode, quad } from '@rdfjs/data-model';
describe('A SimpleSparqlUpdatePatchHandler', (): void => { describe('A SimpleSparqlUpdatePatchHandler', (): void => {
@ -64,15 +65,15 @@ describe('A SimpleSparqlUpdatePatchHandler', (): void => {
const basicChecks = async(quads: Quad[]): Promise<void> => { const basicChecks = async(quads: Quad[]): Promise<void> => {
expect(source.getRepresentation).toHaveBeenCalledTimes(1); expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(source.getRepresentation).toHaveBeenLastCalledWith( expect(source.getRepresentation).toHaveBeenLastCalledWith(
{ path: 'path' }, { type: [{ value: 'internal/quads', weight: 1 }]}, { path: 'path' }, { type: [{ value: CONTENT_TYPE_QUADS, weight: 1 }]},
); );
expect(source.setRepresentation).toHaveBeenCalledTimes(1); expect(source.setRepresentation).toHaveBeenCalledTimes(1);
expect(order).toEqual([ 'acquire', 'getRepresentation', 'setRepresentation', 'release' ]); expect(order).toEqual([ 'acquire', 'getRepresentation', 'setRepresentation', 'release' ]);
const setParams = (source.setRepresentation as jest.Mock).mock.calls[0]; const setParams = (source.setRepresentation as jest.Mock).mock.calls[0];
expect(setParams[0]).toEqual({ path: 'path' }); expect(setParams[0]).toEqual({ path: 'path' });
expect(setParams[1]).toEqual(expect.objectContaining({ expect(setParams[1]).toEqual(expect.objectContaining({
dataType: 'quad', dataType: DATA_TYPE_QUAD,
metadata: { raw: [], profiles: [], contentType: 'internal/quads' }, metadata: { raw: [], profiles: [], contentType: CONTENT_TYPE_QUADS },
})); }));
await expect(arrayifyStream(setParams[1].data)).resolves.toBeRdfIsomorphic(quads); await expect(arrayifyStream(setParams[1].data)).resolves.toBeRdfIsomorphic(quads);
}; };