mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
refactor: Replace dataType by binary flag
This commit is contained in:
committed by
Joachim Van Herwegen
parent
385e1a4cdf
commit
c5c5d13570
@@ -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: [],
|
||||
|
||||
@@ -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<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.');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<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),
|
||||
};
|
||||
|
||||
@@ -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: [],
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<ResourceIdentifier> {
|
||||
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<void> {
|
||||
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,
|
||||
|
||||
@@ -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<Representation> {
|
||||
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,
|
||||
};
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
@@ -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: [],
|
||||
|
||||
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user