From c5c5d13570f1cc760c48c7e7bf4abed83abceeb3 Mon Sep 17 00:00:00 2001 From: Ruben Verborgh Date: Wed, 2 Sep 2020 23:02:03 +0200 Subject: [PATCH] refactor: Replace dataType by binary flag --- index.ts | 2 -- src/init/Setup.ts | 3 +-- src/ldp/http/BasicResponseWriter.ts | 4 +--- src/ldp/http/RawBodyParser.ts | 7 +++---- src/ldp/http/SparqlUpdateBodyParser.ts | 3 +-- src/ldp/representation/BinaryRepresentation.ts | 10 ---------- src/ldp/representation/QuadRepresentation.ts | 10 ---------- src/ldp/representation/Representation.ts | 5 +++-- src/storage/FileResourceStore.ts | 10 +++++----- src/storage/InMemoryResourceStore.ts | 7 +++---- src/storage/conversion/QuadToRdfConverter.ts | 4 ++-- .../conversion/QuadToTurtleConverter.ts | 4 ++-- src/storage/conversion/RdfToQuadConverter.ts | 4 ++-- .../conversion/TurtleToQuadConverter.ts | 4 ++-- src/storage/patch/SparqlUpdatePatchHandler.ts | 4 ++-- src/util/ContentTypes.ts | 4 +--- test/integration/Authorization.test.ts | 3 +-- .../RepresentationConverter.test.ts | 5 ++--- test/integration/RequestParser.test.ts | 3 +-- test/unit/ldp/http/BasicResponseWriter.test.ts | 9 ++++----- test/unit/ldp/http/RawBodyParser.test.ts | 3 +-- .../ldp/http/SparqlUpdateBodyParser.test.ts | 3 +-- .../ldp/operations/GetOperationHandler.test.ts | 5 ++--- .../operations/PatchOperationHandler.test.ts | 4 ++-- .../operations/PostOperationHandler.test.ts | 6 +++--- .../ldp/operations/PutOperationHandler.test.ts | 4 ++-- test/unit/storage/FileResourceStore.test.ts | 18 +++++++++--------- .../unit/storage/InMemoryResourceStore.test.ts | 11 +++++------ .../conversion/QuadToRdfConverter.test.ts | 6 +++--- .../conversion/QuadToTurtleConverter.test.ts | 4 ++-- .../conversion/RdfToQuadConverter.test.ts | 8 ++++---- .../conversion/TurtleToQuadConverter.test.ts | 6 +++--- .../patch/SparqlUpdatePatchHandler.test.ts | 4 ++-- 33 files changed, 75 insertions(+), 112 deletions(-) delete mode 100644 src/ldp/representation/BinaryRepresentation.ts delete mode 100644 src/ldp/representation/QuadRepresentation.ts diff --git a/index.ts b/index.ts index 99c5d0d45..4e74f2cd5 100644 --- a/index.ts +++ b/index.ts @@ -47,8 +47,6 @@ export * from './src/ldp/permissions/BasePermissionsExtractor'; export * from './src/ldp/permissions/SparqlPatchPermissionsExtractor'; // LDP/Representation -export * from './src/ldp/representation/BinaryRepresentation'; -export * from './src/ldp/representation/QuadRepresentation'; export * from './src/ldp/representation/Representation'; export * from './src/ldp/representation/RepresentationMetadata'; export * from './src/ldp/representation/RepresentationPreference'; diff --git a/src/init/Setup.ts b/src/init/Setup.ts index fe2af358b..1c26e2cf8 100644 --- a/src/init/Setup.ts +++ b/src/init/Setup.ts @@ -2,7 +2,6 @@ import streamifyArray from 'streamify-array'; import { AclManager } from '../authorization/AclManager'; import { ExpressHttpServer } from '../server/ExpressHttpServer'; import { ResourceStore } from '../storage/ResourceStore'; -import { DATA_TYPE_BINARY } from '../util/ContentTypes'; import { RuntimeConfig, RuntimeConfigData } from './RuntimeConfig'; /** @@ -52,7 +51,7 @@ export class Setup { await this.store.setRepresentation( await this.aclManager.getAcl({ path: this.runtimeConfig.base }), { - dataType: DATA_TYPE_BINARY, + binary: true, data: streamifyArray([ acl ]), metadata: { raw: [], diff --git a/src/ldp/http/BasicResponseWriter.ts b/src/ldp/http/BasicResponseWriter.ts index e8e5c5739..855cd6240 100644 --- a/src/ldp/http/BasicResponseWriter.ts +++ b/src/ldp/http/BasicResponseWriter.ts @@ -1,5 +1,4 @@ import { HttpResponse } from '../../server/HttpResponse'; -import { DATA_TYPE_BINARY } from '../../util/ContentTypes'; import { HttpError } from '../../util/errors/HttpError'; import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError'; import { ResponseDescription } from '../operations/ResponseDescription'; @@ -12,8 +11,7 @@ import { ResponseWriter } from './ResponseWriter'; export class BasicResponseWriter extends ResponseWriter { public async canHandle(input: { response: HttpResponse; result: ResponseDescription | Error }): Promise { if (!(input.result instanceof Error)) { - const dataType = input.result.body?.dataType; - if (dataType && dataType !== DATA_TYPE_BINARY) { + if (input.result.body && !input.result.body.binary) { throw new UnsupportedHttpError('Only binary results are supported.'); } } diff --git a/src/ldp/http/RawBodyParser.ts b/src/ldp/http/RawBodyParser.ts index 76f9132c4..bab6f44d5 100644 --- a/src/ldp/http/RawBodyParser.ts +++ b/src/ldp/http/RawBodyParser.ts @@ -1,7 +1,6 @@ import { HttpRequest } from '../../server/HttpRequest'; -import { DATA_TYPE_BINARY } from '../../util/ContentTypes'; import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError'; -import { BinaryRepresentation } from '../representation/BinaryRepresentation'; +import { Representation } from '../representation/Representation'; import { RepresentationMetadata } from '../representation/RepresentationMetadata'; import { BodyParser } from './BodyParser'; @@ -17,13 +16,13 @@ export class RawBodyParser extends BodyParser { // Note that the only reason this is a union is in case the body is empty. // If this check gets moved away from the BodyParsers this union could be removed - public async handle(input: HttpRequest): Promise { + public async handle(input: HttpRequest): Promise { if (!input.headers['content-type']) { return; } return { - dataType: DATA_TYPE_BINARY, + binary: true, data: input, metadata: this.parseMetadata(input), }; diff --git a/src/ldp/http/SparqlUpdateBodyParser.ts b/src/ldp/http/SparqlUpdateBodyParser.ts index c2f915fde..3689703c9 100644 --- a/src/ldp/http/SparqlUpdateBodyParser.ts +++ b/src/ldp/http/SparqlUpdateBodyParser.ts @@ -1,7 +1,6 @@ import { PassThrough } from 'stream'; import { translate } from 'sparqlalgebrajs'; import { HttpRequest } from '../../server/HttpRequest'; -import { DATA_TYPE_BINARY } from '../../util/ContentTypes'; import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError'; import { UnsupportedMediaTypeHttpError } from '../../util/errors/UnsupportedMediaTypeHttpError'; import { readableToString } from '../../util/Util'; @@ -37,7 +36,7 @@ export class SparqlUpdateBodyParser extends BodyParser { // Prevent body from being requested again return { algebra, - dataType: DATA_TYPE_BINARY, + binary: true, data: dataCopy, metadata: { raw: [], diff --git a/src/ldp/representation/BinaryRepresentation.ts b/src/ldp/representation/BinaryRepresentation.ts deleted file mode 100644 index 33a791611..000000000 --- a/src/ldp/representation/BinaryRepresentation.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Readable } from 'stream'; -import { Representation } from './Representation'; - -/** - * A representation containing binary data. - */ -export interface BinaryRepresentation extends Representation { - dataType: 'binary'; - data: Readable; -} diff --git a/src/ldp/representation/QuadRepresentation.ts b/src/ldp/representation/QuadRepresentation.ts deleted file mode 100644 index 629a2bd1c..000000000 --- a/src/ldp/representation/QuadRepresentation.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Readable } from 'stream'; -import { Representation } from './Representation'; - -/** - * A representation containing quads as data. - */ -export interface QuadRepresentation extends Representation { - dataType: 'quad'; - data: Readable; -} diff --git a/src/ldp/representation/Representation.ts b/src/ldp/representation/Representation.ts index eff8295aa..69c181c16 100644 --- a/src/ldp/representation/Representation.ts +++ b/src/ldp/representation/Representation.ts @@ -14,7 +14,8 @@ export interface Representation { */ data: Readable; /** - * The data type of the contents in the data stream. + * Whether the data stream consists of binary/string chunks + * (as opposed to complex objects). */ - dataType: string; + binary: boolean; } diff --git a/src/storage/FileResourceStore.ts b/src/storage/FileResourceStore.ts index 531f13b27..cb428d57a 100644 --- a/src/storage/FileResourceStore.ts +++ b/src/storage/FileResourceStore.ts @@ -9,7 +9,7 @@ import { RuntimeConfig } from '../init/RuntimeConfig'; import { Representation } from '../ldp/representation/Representation'; import { RepresentationMetadata } from '../ldp/representation/RepresentationMetadata'; import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier'; -import { CONTENT_TYPE_QUADS, DATA_TYPE_BINARY, DATA_TYPE_QUAD } from '../util/ContentTypes'; +import { CONTENT_TYPE_QUADS } from '../util/ContentTypes'; import { ConflictHttpError } from '../util/errors/ConflictHttpError'; import { MethodNotAllowedHttpError } from '../util/errors/MethodNotAllowedHttpError'; import { NotFoundHttpError } from '../util/errors/NotFoundHttpError'; @@ -59,7 +59,7 @@ export class FileResourceStore implements ResourceStore { * @returns The newly generated identifier. */ public async addResource(container: ResourceIdentifier, representation: Representation): Promise { - if (representation.dataType !== DATA_TYPE_BINARY) { + if (!representation.binary) { throw new UnsupportedMediaTypeHttpError('FileResourceStore only supports binary representations.'); } @@ -149,7 +149,7 @@ export class FileResourceStore implements ResourceStore { * @param representation - New Representation. */ public async setRepresentation(identifier: ResourceIdentifier, representation: Representation): Promise { - if (representation.dataType !== DATA_TYPE_BINARY) { + if (!representation.binary) { throw new UnsupportedMediaTypeHttpError('FileResourceStore only supports binary representations.'); } @@ -264,7 +264,7 @@ export class FileResourceStore implements ResourceStore { if (contentType) { metadata.contentType = contentType; } - return { metadata, data: readStream, dataType: DATA_TYPE_BINARY }; + return { metadata, data: readStream, binary: true }; } /** @@ -295,7 +295,7 @@ export class FileResourceStore implements ResourceStore { } return { - dataType: DATA_TYPE_QUAD, + binary: false, data: streamifyArray(quads), metadata: { raw: rawMetadata, diff --git a/src/storage/InMemoryResourceStore.ts b/src/storage/InMemoryResourceStore.ts index 6934757f1..df11bf951 100644 --- a/src/storage/InMemoryResourceStore.ts +++ b/src/storage/InMemoryResourceStore.ts @@ -3,7 +3,6 @@ import streamifyArray from 'streamify-array'; import { RuntimeConfig } from '../init/RuntimeConfig'; import { Representation } from '../ldp/representation/Representation'; import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier'; -import { DATA_TYPE_BINARY } from '../util/ContentTypes'; import { NotFoundHttpError } from '../util/errors/NotFoundHttpError'; import { ensureTrailingSlash } from '../util/Util'; import { ResourceStore } from './ResourceStore'; @@ -27,7 +26,7 @@ export class InMemoryResourceStore implements ResourceStore { this.store = { // Default root entry (what you get when the identifier is equal to the base) '': { - dataType: DATA_TYPE_BINARY, + binary: true, data: streamifyArray([]), metadata: { raw: [], profiles: [], contentType: 'text/turtle' }, }, @@ -132,7 +131,7 @@ export class InMemoryResourceStore implements ResourceStore { private async copyRepresentation(source: Representation): Promise { const arr = await arrayifyStream(source.data); return { - dataType: source.dataType, + binary: source.binary, data: streamifyArray([ ...arr ]), metadata: source.metadata, }; @@ -152,7 +151,7 @@ export class InMemoryResourceStore implements ResourceStore { source.data = streamifyArray([ ...arr ]); return { - dataType: source.dataType, + binary: source.binary, data: streamifyArray([ ...arr ]), metadata: source.metadata, }; diff --git a/src/storage/conversion/QuadToRdfConverter.ts b/src/storage/conversion/QuadToRdfConverter.ts index bf53442d3..c36340d53 100644 --- a/src/storage/conversion/QuadToRdfConverter.ts +++ b/src/storage/conversion/QuadToRdfConverter.ts @@ -3,7 +3,7 @@ import rdfSerializer from 'rdf-serialize'; import { Representation } from '../../ldp/representation/Representation'; import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata'; import { RepresentationPreferences } from '../../ldp/representation/RepresentationPreferences'; -import { CONTENT_TYPE_QUADS, DATA_TYPE_BINARY } from '../../util/ContentTypes'; +import { CONTENT_TYPE_QUADS } from '../../util/ContentTypes'; import { checkRequest, matchingTypes } from './ConversionUtil'; import { RepresentationConverterArgs } from './RepresentationConverter'; import { TypedRepresentationConverter } from './TypedRepresentationConverter'; @@ -32,7 +32,7 @@ export class QuadToRdfConverter extends TypedRepresentationConverter { const contentType = matchingTypes(preferences, await rdfSerializer.getContentTypes())[0].value; const metadata: RepresentationMetadata = { ...quads.metadata, contentType }; return { - dataType: DATA_TYPE_BINARY, + binary: true, data: rdfSerializer.serialize(quads.data, { contentType }) as Readable, metadata, }; diff --git a/src/storage/conversion/QuadToTurtleConverter.ts b/src/storage/conversion/QuadToTurtleConverter.ts index 4187bad4a..37310cdbe 100644 --- a/src/storage/conversion/QuadToTurtleConverter.ts +++ b/src/storage/conversion/QuadToTurtleConverter.ts @@ -1,7 +1,7 @@ import { StreamWriter } from 'n3'; import { Representation } from '../../ldp/representation/Representation'; import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata'; -import { CONTENT_TYPE_QUADS, DATA_TYPE_BINARY } from '../../util/ContentTypes'; +import { CONTENT_TYPE_QUADS } from '../../util/ContentTypes'; import { checkRequest } from './ConversionUtil'; import { RepresentationConverter, RepresentationConverterArgs } from './RepresentationConverter'; @@ -20,7 +20,7 @@ export class QuadToTurtleConverter extends RepresentationConverter { private quadsToTurtle(quads: Representation): Representation { const metadata: RepresentationMetadata = { ...quads.metadata, contentType: 'text/turtle' }; return { - dataType: DATA_TYPE_BINARY, + binary: true, data: quads.data.pipe(new StreamWriter({ format: 'text/turtle' })), metadata, }; diff --git a/src/storage/conversion/RdfToQuadConverter.ts b/src/storage/conversion/RdfToQuadConverter.ts index 2c8542efa..33e4a478a 100644 --- a/src/storage/conversion/RdfToQuadConverter.ts +++ b/src/storage/conversion/RdfToQuadConverter.ts @@ -2,7 +2,7 @@ import { PassThrough } from 'stream'; import rdfParser from 'rdf-parse'; import { Representation } from '../../ldp/representation/Representation'; import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata'; -import { CONTENT_TYPE_QUADS, DATA_TYPE_QUAD } from '../../util/ContentTypes'; +import { CONTENT_TYPE_QUADS } from '../../util/ContentTypes'; import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError'; import { checkRequest } from './ConversionUtil'; import { RepresentationConverterArgs } from './RepresentationConverter'; @@ -42,7 +42,7 @@ export class RdfToQuadConverter extends TypedRepresentationConverter { data.on('error', (error): boolean => errorStream.emit('error', new UnsupportedHttpError(error.message))); return { - dataType: DATA_TYPE_QUAD, + binary: false, data: errorStream, metadata, }; diff --git a/src/storage/conversion/TurtleToQuadConverter.ts b/src/storage/conversion/TurtleToQuadConverter.ts index d97b00fe0..4b721ef88 100644 --- a/src/storage/conversion/TurtleToQuadConverter.ts +++ b/src/storage/conversion/TurtleToQuadConverter.ts @@ -2,7 +2,7 @@ import { PassThrough } from 'stream'; import { StreamParser } from 'n3'; import { Representation } from '../../ldp/representation/Representation'; import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata'; -import { CONTENT_TYPE_QUADS, DATA_TYPE_QUAD } from '../../util/ContentTypes'; +import { CONTENT_TYPE_QUADS } from '../../util/ContentTypes'; import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError'; import { checkRequest } from './ConversionUtil'; import { RepresentationConverter, RepresentationConverterArgs } from './RepresentationConverter'; @@ -30,7 +30,7 @@ export class TurtleToQuadConverter extends RepresentationConverter { data.on('error', (error): boolean => errorStream.emit('error', new UnsupportedHttpError(error.message))); return { - dataType: DATA_TYPE_QUAD, + binary: false, data: errorStream, metadata, }; diff --git a/src/storage/patch/SparqlUpdatePatchHandler.ts b/src/storage/patch/SparqlUpdatePatchHandler.ts index e66629b01..47af1d762 100644 --- a/src/storage/patch/SparqlUpdatePatchHandler.ts +++ b/src/storage/patch/SparqlUpdatePatchHandler.ts @@ -7,7 +7,7 @@ import { Algebra } from 'sparqlalgebrajs'; import { SparqlUpdatePatch } from '../../ldp/http/SparqlUpdatePatch'; import { Representation } from '../../ldp/representation/Representation'; import { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier'; -import { CONTENT_TYPE_QUADS, DATA_TYPE_QUAD } from '../../util/ContentTypes'; +import { CONTENT_TYPE_QUADS } from '../../util/ContentTypes'; import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError'; import { ResourceLocker } from '../ResourceLocker'; import { ResourceStore } from '../ResourceStore'; @@ -66,8 +66,8 @@ export class SparqlUpdatePatchHandler extends PatchHandler { store.removeQuads(deletes); store.addQuads(inserts); const representation: Representation = { + binary: false, data: store.match() as Readable, - dataType: DATA_TYPE_QUAD, metadata: { raw: [], profiles: [], diff --git a/src/util/ContentTypes.ts b/src/util/ContentTypes.ts index a757e88b8..375496382 100644 --- a/src/util/ContentTypes.ts +++ b/src/util/ContentTypes.ts @@ -1,4 +1,2 @@ -export const DATA_TYPE_BINARY = 'binary'; -export const DATA_TYPE_QUAD = 'quad'; - +// Internal (non-exposed) content types export const CONTENT_TYPE_QUADS = 'internal/quads'; diff --git a/test/integration/Authorization.test.ts b/test/integration/Authorization.test.ts index 8c6aff81b..3c65ae6ff 100644 --- a/test/integration/Authorization.test.ts +++ b/test/integration/Authorization.test.ts @@ -26,7 +26,6 @@ import { RepresentationConvertingStore } from '../../src/storage/RepresentationC import { ResourceStore } from '../../src/storage/ResourceStore'; import { UrlContainerManager } from '../../src/storage/UrlContainerManager'; import { CompositeAsyncHandler } from '../../src/util/CompositeAsyncHandler'; -import { DATA_TYPE_BINARY } from '../../src/util/ContentTypes'; import { call } from '../util/Util'; const setAcl = async(store: ResourceStore, id: string, permissions: PermissionSet, control: boolean, @@ -61,8 +60,8 @@ const setAcl = async(store: ResourceStore, id: string, permissions: PermissionSe acl.push('.'); const representation = { + binary: true, data: streamifyArray(acl), - dataType: DATA_TYPE_BINARY, metadata: { raw: [], profiles: [], diff --git a/test/integration/RepresentationConverter.test.ts b/test/integration/RepresentationConverter.test.ts index db0a5102b..ea664bf8c 100644 --- a/test/integration/RepresentationConverter.test.ts +++ b/test/integration/RepresentationConverter.test.ts @@ -3,7 +3,6 @@ import { Representation } from '../../src/ldp/representation/Representation'; import { ChainedConverter } from '../../src/storage/conversion/ChainedConverter'; import { QuadToRdfConverter } from '../../src/storage/conversion/QuadToRdfConverter'; import { RdfToQuadConverter } from '../../src/storage/conversion/RdfToQuadConverter'; -import { DATA_TYPE_BINARY } from '../../src/util/ContentTypes'; import { readableToString } from '../../src/util/Util'; describe('A ChainedConverter', (): void => { @@ -15,7 +14,7 @@ describe('A ChainedConverter', (): void => { it('can convert from JSON-LD to turtle.', async(): Promise => { const representation: Representation = { - dataType: DATA_TYPE_BINARY, + binary: true, data: streamifyArray([ '{"@id": "http://test.com/s", "http://test.com/p": { "@id": "http://test.com/o" }}' ]), metadata: { raw: [], contentType: 'application/ld+json' }, }; @@ -32,7 +31,7 @@ describe('A ChainedConverter', (): void => { it('can convert from turtle to JSON-LD.', async(): Promise => { const representation: Representation = { - dataType: DATA_TYPE_BINARY, + binary: true, data: streamifyArray([ ' .' ]), metadata: { raw: [], contentType: 'text/turtle' }, }; diff --git a/test/integration/RequestParser.test.ts b/test/integration/RequestParser.test.ts index abcf4c40c..aa39023ad 100644 --- a/test/integration/RequestParser.test.ts +++ b/test/integration/RequestParser.test.ts @@ -6,7 +6,6 @@ import { BasicRequestParser } from '../../src/ldp/http/BasicRequestParser'; import { BasicTargetExtractor } from '../../src/ldp/http/BasicTargetExtractor'; import { RawBodyParser } from '../../src/ldp/http/RawBodyParser'; import { HttpRequest } from '../../src/server/HttpRequest'; -import { DATA_TYPE_BINARY } from '../../src/util/ContentTypes'; describe('A SimpleRequestParser with simple input parsers', (): void => { const targetExtractor = new BasicTargetExtractor(); @@ -35,7 +34,7 @@ describe('A SimpleRequestParser with simple input parsers', (): void => { }, body: { data: expect.any(Readable), - dataType: DATA_TYPE_BINARY, + binary: true, metadata: { contentType: 'text/turtle', raw: [], diff --git a/test/unit/ldp/http/BasicResponseWriter.test.ts b/test/unit/ldp/http/BasicResponseWriter.test.ts index e6c63f79c..eb92b66d5 100644 --- a/test/unit/ldp/http/BasicResponseWriter.test.ts +++ b/test/unit/ldp/http/BasicResponseWriter.test.ts @@ -4,7 +4,6 @@ import { Quad } from 'rdf-js'; import streamifyArray from 'streamify-array'; import { BasicResponseWriter } from '../../../../src/ldp/http/BasicResponseWriter'; import { ResponseDescription } from '../../../../src/ldp/operations/ResponseDescription'; -import { DATA_TYPE_BINARY, DATA_TYPE_QUAD } from '../../../../src/util/ContentTypes'; import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError'; describe('A BasicResponseWriter', (): void => { @@ -16,9 +15,9 @@ describe('A BasicResponseWriter', (): void => { }); it('requires the description body to be a string or binary stream if present.', async(): Promise => { - await expect(writer.canHandle({ response, result: { body: { dataType: DATA_TYPE_QUAD }} as ResponseDescription })) + await expect(writer.canHandle({ response, result: { body: { binary: false }} as ResponseDescription })) .rejects.toThrow(UnsupportedHttpError); - await expect(writer.canHandle({ response, result: { body: { dataType: DATA_TYPE_BINARY }} as ResponseDescription })) + await expect(writer.canHandle({ response, result: { body: { binary: true }} as ResponseDescription })) .resolves.toBeUndefined(); }); @@ -31,8 +30,8 @@ describe('A BasicResponseWriter', (): void => { it('responds with a body if the description has a body.', async(done): Promise => { const body = { + binary: true, data: streamifyArray([ ' .' ]), - dataType: DATA_TYPE_BINARY, metadata: { raw: [] as Quad[], profiles: [] as string[], @@ -52,8 +51,8 @@ describe('A BasicResponseWriter', (): void => { it('responds with a content-type if the metadata has it.', async(done): Promise => { const body = { + binary: true, data: streamifyArray([ ' .' ]), - dataType: DATA_TYPE_BINARY, metadata: { raw: [] as Quad[], profiles: [] as string[], diff --git a/test/unit/ldp/http/RawBodyParser.test.ts b/test/unit/ldp/http/RawBodyParser.test.ts index 0dbaf96de..15230f927 100644 --- a/test/unit/ldp/http/RawBodyParser.test.ts +++ b/test/unit/ldp/http/RawBodyParser.test.ts @@ -2,7 +2,6 @@ import arrayifyStream from 'arrayify-stream'; import streamifyArray from 'streamify-array'; import { RawBodyParser } from '../../../../src/ldp/http/RawBodyParser'; import { HttpRequest } from '../../../../src/server/HttpRequest'; -import { DATA_TYPE_BINARY } from '../../../../src/util/ContentTypes'; import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError'; import 'jest-rdf'; @@ -22,8 +21,8 @@ describe('A RawBodyparser', (): void => { input.headers = { 'content-type': 'text/turtle' }; const result = (await bodyParser.handle(input))!; expect(result).toEqual({ + binary: true, data: input, - dataType: DATA_TYPE_BINARY, metadata: { contentType: 'text/turtle', raw: [], diff --git a/test/unit/ldp/http/SparqlUpdateBodyParser.test.ts b/test/unit/ldp/http/SparqlUpdateBodyParser.test.ts index b4aac5534..ffee298f1 100644 --- a/test/unit/ldp/http/SparqlUpdateBodyParser.test.ts +++ b/test/unit/ldp/http/SparqlUpdateBodyParser.test.ts @@ -4,7 +4,6 @@ import { Algebra } from 'sparqlalgebrajs'; import streamifyArray from 'streamify-array'; import { SparqlUpdateBodyParser } from '../../../../src/ldp/http/SparqlUpdateBodyParser'; import { HttpRequest } from '../../../../src/server/HttpRequest'; -import { DATA_TYPE_BINARY } from '../../../../src/util/ContentTypes'; import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError'; import { UnsupportedMediaTypeHttpError } from '../../../../src/util/errors/UnsupportedMediaTypeHttpError'; @@ -34,7 +33,7 @@ describe('A SparqlUpdateBodyParser', (): void => { namedNode('http://test.com/p'), namedNode('http://test.com/o'), ) ]); - expect(result.dataType).toBe(DATA_TYPE_BINARY); + expect(result.binary).toBe(true); expect(result.metadata).toEqual({ raw: [], profiles: [], diff --git a/test/unit/ldp/operations/GetOperationHandler.test.ts b/test/unit/ldp/operations/GetOperationHandler.test.ts index 3262b8fef..7c61af141 100644 --- a/test/unit/ldp/operations/GetOperationHandler.test.ts +++ b/test/unit/ldp/operations/GetOperationHandler.test.ts @@ -2,12 +2,11 @@ import { GetOperationHandler } from '../../../../src/ldp/operations/GetOperation import { Operation } from '../../../../src/ldp/operations/Operation'; import { Representation } from '../../../../src/ldp/representation/Representation'; import { ResourceStore } from '../../../../src/storage/ResourceStore'; -import { DATA_TYPE_QUAD } from '../../../../src/util/ContentTypes'; import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError'; describe('A GetOperationHandler', (): void => { const store = { - getRepresentation: async(): Promise => ({ dataType: DATA_TYPE_QUAD } as Representation), + getRepresentation: async(): Promise => ({ binary: false } as Representation), } as unknown as ResourceStore; const handler = new GetOperationHandler(store); @@ -18,7 +17,7 @@ describe('A GetOperationHandler', (): void => { it('returns the representation from the store with the input identifier.', async(): Promise => { await expect(handler.handle({ target: { path: 'url' }} as Operation)).resolves.toEqual( - { identifier: { path: 'url' }, body: { dataType: DATA_TYPE_QUAD }}, + { identifier: { path: 'url' }, body: { binary: false }}, ); }); }); diff --git a/test/unit/ldp/operations/PatchOperationHandler.test.ts b/test/unit/ldp/operations/PatchOperationHandler.test.ts index abc75d19d..f28a87688 100644 --- a/test/unit/ldp/operations/PatchOperationHandler.test.ts +++ b/test/unit/ldp/operations/PatchOperationHandler.test.ts @@ -16,9 +16,9 @@ describe('A PatchOperationHandler', (): void => { }); it('deletes the resource from the store and returns its identifier.', async(): Promise => { - await expect(handler.handle({ target: { path: 'url' }, body: { dataType: 'patch' }} as Operation)) + await expect(handler.handle({ target: { path: 'url' }, body: { binary: false }} as Operation)) .resolves.toEqual({ identifier: { path: 'url' }}); expect(store.modifyResource).toHaveBeenCalledTimes(1); - expect(store.modifyResource).toHaveBeenLastCalledWith({ path: 'url' }, { dataType: 'patch' }); + expect(store.modifyResource).toHaveBeenLastCalledWith({ path: 'url' }, { binary: false }); }); }); diff --git a/test/unit/ldp/operations/PostOperationHandler.test.ts b/test/unit/ldp/operations/PostOperationHandler.test.ts index 07b03dfc3..58b0912b0 100644 --- a/test/unit/ldp/operations/PostOperationHandler.test.ts +++ b/test/unit/ldp/operations/PostOperationHandler.test.ts @@ -11,15 +11,15 @@ describe('A PostOperationHandler', (): void => { const handler = new PostOperationHandler(store); it('only supports POST operations with a body.', async(): Promise => { - await expect(handler.canHandle({ method: 'POST', body: { dataType: 'test' }} as Operation)) + await expect(handler.canHandle({ method: 'POST', body: { }} as Operation)) .resolves.toBeUndefined(); - await expect(handler.canHandle({ method: 'GET', body: { dataType: 'test' }} as Operation)) + await expect(handler.canHandle({ method: 'GET', body: { }} as Operation)) .rejects.toThrow(UnsupportedHttpError); await expect(handler.canHandle({ method: 'POST' } as Operation)).rejects.toThrow(UnsupportedHttpError); }); it('adds the given representation to the store and returns the new identifier.', async(): Promise => { - await expect(handler.handle({ method: 'POST', body: { dataType: 'test' }} as Operation)) + await expect(handler.handle({ method: 'POST', body: { }} as Operation)) .resolves.toEqual({ identifier: { path: 'newPath' }}); }); }); diff --git a/test/unit/ldp/operations/PutOperationHandler.test.ts b/test/unit/ldp/operations/PutOperationHandler.test.ts index 6f9abea01..b594e93cf 100644 --- a/test/unit/ldp/operations/PutOperationHandler.test.ts +++ b/test/unit/ldp/operations/PutOperationHandler.test.ts @@ -17,10 +17,10 @@ describe('A PutOperationHandler', (): void => { }); it('sets the representation in the store and returns its identifier.', async(): Promise => { - await expect(handler.handle({ target: { path: 'url' }, body: { dataType: 'test' }} as Operation)) + await expect(handler.handle({ target: { path: 'url' }, body: {}} as Operation)) .resolves.toEqual({ identifier: { path: 'url' }}); expect(store.setRepresentation).toHaveBeenCalledTimes(1); - expect(store.setRepresentation).toHaveBeenLastCalledWith({ path: 'url' }, { dataType: 'test' }); + expect(store.setRepresentation).toHaveBeenLastCalledWith({ path: 'url' }, {}); }); it('errors when there is no body.', async(): Promise => { diff --git a/test/unit/storage/FileResourceStore.test.ts b/test/unit/storage/FileResourceStore.test.ts index 41adab8e8..bd3f517af 100644 --- a/test/unit/storage/FileResourceStore.test.ts +++ b/test/unit/storage/FileResourceStore.test.ts @@ -6,10 +6,10 @@ import arrayifyStream from 'arrayify-stream'; import { DataFactory } from 'n3'; import streamifyArray from 'streamify-array'; import { RuntimeConfig } from '../../../src/init/RuntimeConfig'; -import { BinaryRepresentation } from '../../../src/ldp/representation/BinaryRepresentation'; +import { Representation } from '../../../src/ldp/representation/Representation'; import { RepresentationMetadata } from '../../../src/ldp/representation/RepresentationMetadata'; import { FileResourceStore } from '../../../src/storage/FileResourceStore'; -import { CONTENT_TYPE_QUADS, DATA_TYPE_BINARY, DATA_TYPE_QUAD } from '../../../src/util/ContentTypes'; +import { CONTENT_TYPE_QUADS } from '../../../src/util/ContentTypes'; import { ConflictHttpError } from '../../../src/util/errors/ConflictHttpError'; import { MethodNotAllowedHttpError } from '../../../src/util/errors/MethodNotAllowedHttpError'; import { NotFoundHttpError } from '../../../src/util/errors/NotFoundHttpError'; @@ -33,7 +33,7 @@ fsPromises.access = jest.fn(); describe('A FileResourceStore', (): void => { let store: FileResourceStore; - let representation: BinaryRepresentation; + let representation: Representation; let readableMock: Readable; let stats: Stats; let writeStream: WriteStream; @@ -56,8 +56,8 @@ describe('A FileResourceStore', (): void => { ); representation = { + binary: true, data: streamifyArray([ rawData ]), - dataType: DATA_TYPE_BINARY, metadata: { raw: [], linkRel: { type: new Set() }} as RepresentationMetadata, }; @@ -116,7 +116,7 @@ describe('A FileResourceStore', (): void => { }); it('errors for wrong input data types.', async(): Promise => { - (representation as any).dataType = DATA_TYPE_QUAD; + (representation as any).binary = false; await expect(store.addResource({ path: base }, representation)).rejects.toThrow(UnsupportedMediaTypeHttpError); await expect(store.setRepresentation({ path: `${base}foo` }, representation)).rejects .toThrow(UnsupportedMediaTypeHttpError); @@ -142,7 +142,7 @@ describe('A FileResourceStore', (): void => { // Read container const result = await store.getRepresentation(identifier); expect(result).toEqual({ - dataType: DATA_TYPE_QUAD, + binary: false, data: expect.any(Readable), metadata: { raw: [], @@ -212,7 +212,7 @@ describe('A FileResourceStore', (): void => { expect(fs.createWriteStream as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'file.txt')); const result = await store.getRepresentation({ path: `${base}file.txt` }); expect(result).toEqual({ - dataType: DATA_TYPE_BINARY, + binary: true, data: expect.any(Readable), metadata: { raw: [], @@ -365,7 +365,7 @@ describe('A FileResourceStore', (): void => { ]; const result = await store.getRepresentation({ path: `${base}foo/` }); expect(result).toEqual({ - dataType: DATA_TYPE_QUAD, + binary: false, data: expect.any(Readable), metadata: { raw: [], @@ -492,7 +492,7 @@ describe('A FileResourceStore', (): void => { const result = await store.getRepresentation({ path: `${base}.htaccess` }); expect(result).toEqual({ - dataType: DATA_TYPE_BINARY, + binary: true, data: expect.any(Readable), metadata: { raw: [], diff --git a/test/unit/storage/InMemoryResourceStore.test.ts b/test/unit/storage/InMemoryResourceStore.test.ts index f76b29d15..8e8d26e30 100644 --- a/test/unit/storage/InMemoryResourceStore.test.ts +++ b/test/unit/storage/InMemoryResourceStore.test.ts @@ -2,25 +2,24 @@ import { Readable } from 'stream'; import arrayifyStream from 'arrayify-stream'; import streamifyArray from 'streamify-array'; import { RuntimeConfig } from '../../../src/init/RuntimeConfig'; -import { BinaryRepresentation } from '../../../src/ldp/representation/BinaryRepresentation'; +import { Representation } from '../../../src/ldp/representation/Representation'; import { RepresentationMetadata } from '../../../src/ldp/representation/RepresentationMetadata'; import { InMemoryResourceStore } from '../../../src/storage/InMemoryResourceStore'; -import { DATA_TYPE_BINARY } from '../../../src/util/ContentTypes'; import { NotFoundHttpError } from '../../../src/util/errors/NotFoundHttpError'; const base = 'http://test.com/'; describe('A InMemoryResourceStore', (): void => { let store: InMemoryResourceStore; - let representation: BinaryRepresentation; + let representation: Representation; const dataString = ' .'; beforeEach(async(): Promise => { store = new InMemoryResourceStore(new RuntimeConfig({ base })); representation = { + binary: true, data: streamifyArray([ dataString ]), - dataType: DATA_TYPE_BINARY, metadata: {} as RepresentationMetadata, }; }); @@ -43,7 +42,7 @@ describe('A InMemoryResourceStore', (): void => { expect(identifier.path.startsWith(base)).toBeTruthy(); const result = await store.getRepresentation(identifier); expect(result).toEqual({ - dataType: representation.dataType, + binary: true, data: expect.any(Readable), metadata: representation.metadata, }); @@ -61,7 +60,7 @@ describe('A InMemoryResourceStore', (): void => { await store.setRepresentation({ path: base }, representation); const result = await store.getRepresentation({ path: base }); expect(result).toEqual({ - dataType: representation.dataType, + binary: true, data: expect.any(Readable), metadata: representation.metadata, }); diff --git a/test/unit/storage/conversion/QuadToRdfConverter.test.ts b/test/unit/storage/conversion/QuadToRdfConverter.test.ts index 2d44f9bc6..f6a8c8c39 100644 --- a/test/unit/storage/conversion/QuadToRdfConverter.test.ts +++ b/test/unit/storage/conversion/QuadToRdfConverter.test.ts @@ -6,7 +6,7 @@ import { Representation } from '../../../../src/ldp/representation/Representatio import { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences'; import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier'; import { QuadToRdfConverter } from '../../../../src/storage/conversion/QuadToRdfConverter'; -import { CONTENT_TYPE_QUADS, DATA_TYPE_BINARY } from '../../../../src/util/ContentTypes'; +import { CONTENT_TYPE_QUADS } from '../../../../src/util/ContentTypes'; describe('A QuadToRdfConverter', (): void => { const converter = new QuadToRdfConverter(); @@ -44,7 +44,7 @@ describe('A QuadToRdfConverter', (): void => { const preferences: RepresentationPreferences = { type: [{ value: 'text/turtle', weight: 1 }]}; const result = await converter.handle({ identifier, representation, preferences }); expect(result).toMatchObject({ - dataType: DATA_TYPE_BINARY, + binary: true, metadata: { contentType: 'text/turtle', }, @@ -67,7 +67,7 @@ describe('A QuadToRdfConverter', (): void => { const preferences: RepresentationPreferences = { type: [{ value: 'application/ld+json', weight: 1 }]}; const result = await converter.handle({ identifier, representation, preferences }); expect(result).toMatchObject({ - dataType: DATA_TYPE_BINARY, + binary: true, metadata: { contentType: 'application/ld+json', }, diff --git a/test/unit/storage/conversion/QuadToTurtleConverter.test.ts b/test/unit/storage/conversion/QuadToTurtleConverter.test.ts index 3d4da12a6..a12bb794a 100644 --- a/test/unit/storage/conversion/QuadToTurtleConverter.test.ts +++ b/test/unit/storage/conversion/QuadToTurtleConverter.test.ts @@ -5,7 +5,7 @@ import { Representation } from '../../../../src/ldp/representation/Representatio import { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences'; import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier'; import { QuadToTurtleConverter } from '../../../../src/storage/conversion/QuadToTurtleConverter'; -import { CONTENT_TYPE_QUADS, DATA_TYPE_BINARY } from '../../../../src/util/ContentTypes'; +import { CONTENT_TYPE_QUADS } from '../../../../src/util/ContentTypes'; describe('A QuadToTurtleConverter', (): void => { const converter = new QuadToTurtleConverter(); @@ -29,7 +29,7 @@ describe('A QuadToTurtleConverter', (): void => { const preferences: RepresentationPreferences = { type: [{ value: 'text/turtle', weight: 1 }]}; const result = await converter.handle({ identifier, representation, preferences }); expect(result).toMatchObject({ - dataType: DATA_TYPE_BINARY, + binary: true, metadata: { contentType: 'text/turtle', }, diff --git a/test/unit/storage/conversion/RdfToQuadConverter.test.ts b/test/unit/storage/conversion/RdfToQuadConverter.test.ts index 18ae8ed39..2029dce88 100644 --- a/test/unit/storage/conversion/RdfToQuadConverter.test.ts +++ b/test/unit/storage/conversion/RdfToQuadConverter.test.ts @@ -7,7 +7,7 @@ import { Representation } from '../../../../src/ldp/representation/Representatio import { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences'; import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier'; import { RdfToQuadConverter } from '../../../../src/storage/conversion/RdfToQuadConverter'; -import { CONTENT_TYPE_QUADS, DATA_TYPE_QUAD } from '../../../../src/util/ContentTypes'; +import { CONTENT_TYPE_QUADS } from '../../../../src/util/ContentTypes'; import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError'; describe('A RdfToQuadConverter.test.ts', (): void => { @@ -42,8 +42,8 @@ describe('A RdfToQuadConverter.test.ts', (): void => { const preferences: RepresentationPreferences = { type: [{ value: CONTENT_TYPE_QUADS, weight: 1 }]}; const result = await converter.handle({ identifier, representation, preferences }); expect(result).toEqual({ + binary: false, data: expect.any(Readable), - dataType: DATA_TYPE_QUAD, metadata: { contentType: CONTENT_TYPE_QUADS, }, @@ -63,8 +63,8 @@ describe('A RdfToQuadConverter.test.ts', (): void => { const preferences: RepresentationPreferences = { type: [{ value: CONTENT_TYPE_QUADS, weight: 1 }]}; const result = await converter.handle({ identifier, representation, preferences }); expect(result).toEqual({ + binary: false, data: expect.any(Readable), - dataType: DATA_TYPE_QUAD, metadata: { contentType: CONTENT_TYPE_QUADS, }, @@ -84,8 +84,8 @@ describe('A RdfToQuadConverter.test.ts', (): void => { const preferences: RepresentationPreferences = { type: [{ value: CONTENT_TYPE_QUADS, weight: 1 }]}; const result = await converter.handle({ identifier, representation, preferences }); expect(result).toEqual({ + binary: false, data: expect.any(Readable), - dataType: DATA_TYPE_QUAD, metadata: { contentType: CONTENT_TYPE_QUADS, }, diff --git a/test/unit/storage/conversion/TurtleToQuadConverter.test.ts b/test/unit/storage/conversion/TurtleToQuadConverter.test.ts index 82c26b288..8521f0e5f 100644 --- a/test/unit/storage/conversion/TurtleToQuadConverter.test.ts +++ b/test/unit/storage/conversion/TurtleToQuadConverter.test.ts @@ -6,7 +6,7 @@ import { Representation } from '../../../../src/ldp/representation/Representatio import { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences'; import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier'; import { TurtleToQuadConverter } from '../../../../src/storage/conversion/TurtleToQuadConverter'; -import { CONTENT_TYPE_QUADS, DATA_TYPE_QUAD } from '../../../../src/util/ContentTypes'; +import { CONTENT_TYPE_QUADS } from '../../../../src/util/ContentTypes'; import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError'; describe('A TurtleToQuadConverter', (): void => { @@ -27,8 +27,8 @@ describe('A TurtleToQuadConverter', (): void => { const preferences: RepresentationPreferences = { type: [{ value: CONTENT_TYPE_QUADS, weight: 1 }]}; const result = await converter.handle({ identifier, representation, preferences }); expect(result).toEqual({ + binary: false, data: expect.any(Readable), - dataType: DATA_TYPE_QUAD, metadata: { contentType: CONTENT_TYPE_QUADS, }, @@ -48,8 +48,8 @@ describe('A TurtleToQuadConverter', (): void => { const preferences: RepresentationPreferences = { type: [{ value: CONTENT_TYPE_QUADS, weight: 1 }]}; const result = await converter.handle({ identifier, representation, preferences }); expect(result).toEqual({ + binary: false, data: expect.any(Readable), - dataType: DATA_TYPE_QUAD, metadata: { contentType: CONTENT_TYPE_QUADS, }, diff --git a/test/unit/storage/patch/SparqlUpdatePatchHandler.test.ts b/test/unit/storage/patch/SparqlUpdatePatchHandler.test.ts index 6a069c3f2..d7d8450ab 100644 --- a/test/unit/storage/patch/SparqlUpdatePatchHandler.test.ts +++ b/test/unit/storage/patch/SparqlUpdatePatchHandler.test.ts @@ -8,7 +8,7 @@ import { Lock } from '../../../../src/storage/Lock'; import { SparqlUpdatePatchHandler } from '../../../../src/storage/patch/SparqlUpdatePatchHandler'; import { ResourceLocker } from '../../../../src/storage/ResourceLocker'; import { ResourceStore } from '../../../../src/storage/ResourceStore'; -import { CONTENT_TYPE_QUADS, DATA_TYPE_QUAD } from '../../../../src/util/ContentTypes'; +import { CONTENT_TYPE_QUADS } from '../../../../src/util/ContentTypes'; import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError'; describe('A SparqlUpdatePatchHandler', (): void => { @@ -72,7 +72,7 @@ describe('A SparqlUpdatePatchHandler', (): void => { const setParams = (source.setRepresentation as jest.Mock).mock.calls[0]; expect(setParams[0]).toEqual({ path: 'path' }); expect(setParams[1]).toEqual(expect.objectContaining({ - dataType: DATA_TYPE_QUAD, + binary: false, metadata: { raw: [], profiles: [], contentType: CONTENT_TYPE_QUADS }, })); await expect(arrayifyStream(setParams[1].data)).resolves.toBeRdfIsomorphic(quads);