Skip to content

Commit

Permalink
Merge 4c3ed81 into 4965b47
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenVerborgh committed Sep 2, 2020
2 parents 4965b47 + 4c3ed81 commit c04dd09
Show file tree
Hide file tree
Showing 35 changed files with 127 additions and 158 deletions.
2 changes: 0 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
4 changes: 2 additions & 2 deletions src/authorization/WebAclAuthorizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Representation } from '../ldp/representation/Representation';
import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
import { ContainerManager } from '../storage/ContainerManager';
import { ResourceStore } from '../storage/ResourceStore';
import { CONTENT_TYPE_QUADS } from '../util/ContentTypes';
import { INTERNAL_QUADS } from '../util/ContentTypes';
import { ForbiddenHttpError } from '../util/errors/ForbiddenHttpError';
import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
import { UnauthorizedHttpError } from '../util/errors/UnauthorizedHttpError';
Expand Down Expand Up @@ -111,7 +111,7 @@ export class WebAclAuthorizer extends Authorizer {
private async getAclRecursive(id: ResourceIdentifier, recurse?: boolean): Promise<Store> {
try {
const acl = await this.aclManager.getAcl(id);
const data = await this.resourceStore.getRepresentation(acl, { type: [{ value: CONTENT_TYPE_QUADS, weight: 1 }]});
const data = await this.resourceStore.getRepresentation(acl, { type: [{ value: INTERNAL_QUADS, weight: 1 }]});
return this.filterData(data, recurse ? ACL.default : ACL.accessTo, id.path);
} catch (error) {
if (!(error instanceof NotFoundHttpError)) {
Expand Down
6 changes: 3 additions & 3 deletions src/init/Setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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 { TEXT_TURTLE } from '../util/ContentTypes';
import { RuntimeConfig, RuntimeConfigData } from './RuntimeConfig';

/**
Expand Down Expand Up @@ -52,12 +52,12 @@ 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: [],
profiles: [],
contentType: 'text/turtle',
contentType: TEXT_TURTLE,
},
},
);
Expand Down
4 changes: 1 addition & 3 deletions src/ldp/http/BasicResponseWriter.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -12,8 +11,7 @@ import { ResponseWriter } from './ResponseWriter';
export class BasicResponseWriter extends ResponseWriter {
public async canHandle(input: { response: HttpResponse; result: ResponseDescription | Error }): Promise<void> {
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.');
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/ldp/http/RawBodyParser.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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<BinaryRepresentation | undefined> {
public async handle(input: HttpRequest): Promise<Representation | undefined> {
if (!input.headers['content-type']) {
return;
}

return {
dataType: DATA_TYPE_BINARY,
binary: true,
data: input,
metadata: this.parseMetadata(input),
};
Expand Down
3 changes: 1 addition & 2 deletions src/ldp/http/SparqlUpdateBodyParser.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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: [],
Expand Down
10 changes: 0 additions & 10 deletions src/ldp/representation/BinaryRepresentation.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/ldp/representation/QuadRepresentation.ts

This file was deleted.

5 changes: 3 additions & 2 deletions src/ldp/representation/Representation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
12 changes: 6 additions & 6 deletions src/storage/FileResourceStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { INTERNAL_QUADS } from '../util/ContentTypes';
import { ConflictHttpError } from '../util/errors/ConflictHttpError';
import { MethodNotAllowedHttpError } from '../util/errors/MethodNotAllowedHttpError';
import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
Expand Down Expand Up @@ -59,7 +59,7 @@ export class FileResourceStore implements ResourceStore {
* @returns The newly generated identifier.
*/
public async addResource(container: ResourceIdentifier, representation: Representation): Promise<ResourceIdentifier> {
if (representation.dataType !== DATA_TYPE_BINARY) {
if (!representation.binary) {
throw new UnsupportedMediaTypeHttpError('FileResourceStore only supports binary representations.');
}

Expand Down Expand Up @@ -149,7 +149,7 @@ export class FileResourceStore implements ResourceStore {
* @param representation - New Representation.
*/
public async setRepresentation(identifier: ResourceIdentifier, representation: Representation): Promise<void> {
if (representation.dataType !== DATA_TYPE_BINARY) {
if (!representation.binary) {
throw new UnsupportedMediaTypeHttpError('FileResourceStore only supports binary representations.');
}

Expand Down Expand Up @@ -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 };
}

/**
Expand Down Expand Up @@ -295,12 +295,12 @@ export class FileResourceStore implements ResourceStore {
}

return {
dataType: DATA_TYPE_QUAD,
binary: false,
data: streamifyArray(quads),
metadata: {
raw: rawMetadata,
dateTime: stats.mtime,
contentType: CONTENT_TYPE_QUADS,
contentType: INTERNAL_QUADS,
},
};
}
Expand Down
10 changes: 5 additions & 5 deletions src/storage/InMemoryResourceStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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 { TEXT_TURTLE } from '../util/ContentTypes';
import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
import { ensureTrailingSlash } from '../util/Util';
import { ResourceStore } from './ResourceStore';
Expand All @@ -27,9 +27,9 @@ 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' },
metadata: { raw: [], profiles: [], contentType: TEXT_TURTLE },
},
};
}
Expand Down Expand Up @@ -132,7 +132,7 @@ export class InMemoryResourceStore implements ResourceStore {
private async copyRepresentation(source: Representation): Promise<Representation> {
const arr = await arrayifyStream(source.data);
return {
dataType: source.dataType,
binary: source.binary,
data: streamifyArray([ ...arr ]),
metadata: source.metadata,
};
Expand All @@ -152,7 +152,7 @@ export class InMemoryResourceStore implements ResourceStore {
source.data = streamifyArray([ ...arr ]);

return {
dataType: source.dataType,
binary: source.binary,
data: streamifyArray([ ...arr ]),
metadata: source.metadata,
};
Expand Down
8 changes: 4 additions & 4 deletions src/storage/conversion/QuadToRdfConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { INTERNAL_QUADS } from '../../util/ContentTypes';
import { checkRequest, matchingTypes } from './ConversionUtil';
import { RepresentationConverterArgs } from './RepresentationConverter';
import { TypedRepresentationConverter } from './TypedRepresentationConverter';
Expand All @@ -13,15 +13,15 @@ import { TypedRepresentationConverter } from './TypedRepresentationConverter';
*/
export class QuadToRdfConverter extends TypedRepresentationConverter {
public async getInputTypes(): Promise<{ [contentType: string]: number }> {
return { [CONTENT_TYPE_QUADS]: 1 };
return { [INTERNAL_QUADS]: 1 };
}

public async getOutputTypes(): Promise<{ [contentType: string]: number }> {
return rdfSerializer.getContentTypesPrioritized();
}

public async canHandle(input: RepresentationConverterArgs): Promise<void> {
checkRequest(input, [ CONTENT_TYPE_QUADS ], await rdfSerializer.getContentTypes());
checkRequest(input, [ INTERNAL_QUADS ], await rdfSerializer.getContentTypes());
}

public async handle(input: RepresentationConverterArgs): Promise<Representation> {
Expand All @@ -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,
};
Expand Down
10 changes: 5 additions & 5 deletions src/storage/conversion/QuadToTurtleConverter.ts
Original file line number Diff line number Diff line change
@@ -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 { INTERNAL_QUADS, TEXT_TURTLE } from '../../util/ContentTypes';
import { checkRequest } from './ConversionUtil';
import { RepresentationConverter, RepresentationConverterArgs } from './RepresentationConverter';

Expand All @@ -10,18 +10,18 @@ import { RepresentationConverter, RepresentationConverterArgs } from './Represen
*/
export class QuadToTurtleConverter extends RepresentationConverter {
public async canHandle(input: RepresentationConverterArgs): Promise<void> {
checkRequest(input, [ CONTENT_TYPE_QUADS ], [ 'text/turtle' ]);
checkRequest(input, [ INTERNAL_QUADS ], [ TEXT_TURTLE ]);
}

public async handle(input: RepresentationConverterArgs): Promise<Representation> {
return this.quadsToTurtle(input.representation);
}

private quadsToTurtle(quads: Representation): Representation {
const metadata: RepresentationMetadata = { ...quads.metadata, contentType: 'text/turtle' };
const metadata: RepresentationMetadata = { ...quads.metadata, contentType: TEXT_TURTLE };
return {
dataType: DATA_TYPE_BINARY,
data: quads.data.pipe(new StreamWriter({ format: 'text/turtle' })),
binary: true,
data: quads.data.pipe(new StreamWriter({ format: TEXT_TURTLE })),
metadata,
};
}
Expand Down
10 changes: 5 additions & 5 deletions src/storage/conversion/RdfToQuadConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { INTERNAL_QUADS } from '../../util/ContentTypes';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import { checkRequest } from './ConversionUtil';
import { RepresentationConverterArgs } from './RepresentationConverter';
Expand All @@ -17,19 +17,19 @@ export class RdfToQuadConverter extends TypedRepresentationConverter {
}

public async getOutputTypes(): Promise<{ [contentType: string]: number }> {
return { [CONTENT_TYPE_QUADS]: 1 };
return { [INTERNAL_QUADS]: 1 };
}

public async canHandle(input: RepresentationConverterArgs): Promise<void> {
checkRequest(input, await rdfParser.getContentTypes(), [ CONTENT_TYPE_QUADS ]);
checkRequest(input, await rdfParser.getContentTypes(), [ INTERNAL_QUADS ]);
}

public async handle(input: RepresentationConverterArgs): Promise<Representation> {
return this.rdfToQuads(input.representation, input.identifier.path);
}

private rdfToQuads(representation: Representation, baseIRI: string): Representation {
const metadata: RepresentationMetadata = { ...representation.metadata, contentType: CONTENT_TYPE_QUADS };
const metadata: RepresentationMetadata = { ...representation.metadata, contentType: INTERNAL_QUADS };

// Catch parsing errors and emit correct error
// Node 10 requires both writableObjectMode and readableObjectMode
Expand All @@ -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,
};
Expand Down
10 changes: 5 additions & 5 deletions src/storage/conversion/TurtleToQuadConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { TEXT_TURTLE, INTERNAL_QUADS } from '../../util/ContentTypes';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import { checkRequest } from './ConversionUtil';
import { RepresentationConverter, RepresentationConverterArgs } from './RepresentationConverter';
Expand All @@ -12,25 +12,25 @@ import { RepresentationConverter, RepresentationConverterArgs } from './Represen
*/
export class TurtleToQuadConverter extends RepresentationConverter {
public async canHandle(input: RepresentationConverterArgs): Promise<void> {
checkRequest(input, [ 'text/turtle' ], [ CONTENT_TYPE_QUADS ]);
checkRequest(input, [ TEXT_TURTLE ], [ INTERNAL_QUADS ]);
}

public async handle(input: RepresentationConverterArgs): Promise<Representation> {
return this.turtleToQuads(input.representation, input.identifier.path);
}

private turtleToQuads(turtle: Representation, baseIRI: string): Representation {
const metadata: RepresentationMetadata = { ...turtle.metadata, contentType: CONTENT_TYPE_QUADS };
const metadata: RepresentationMetadata = { ...turtle.metadata, contentType: INTERNAL_QUADS };

// Catch parsing errors and emit correct error
// Node 10 requires both writableObjectMode and readableObjectMode
const errorStream = new PassThrough({ writableObjectMode: true, readableObjectMode: true });
const data = turtle.data.pipe(new StreamParser({ format: 'text/turtle', baseIRI }));
const data = turtle.data.pipe(new StreamParser({ format: TEXT_TURTLE, baseIRI }));
data.pipe(errorStream);
data.on('error', (error): boolean => errorStream.emit('error', new UnsupportedHttpError(error.message)));

return {
dataType: DATA_TYPE_QUAD,
binary: false,
data: errorStream,
metadata,
};
Expand Down
Loading

0 comments on commit c04dd09

Please sign in to comment.