mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Convert data from ResourceStore based on preferences
This commit is contained in:
42
src/storage/PassthroughStore.ts
Normal file
42
src/storage/PassthroughStore.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { Conditions } from './Conditions';
|
||||
import { Patch } from '../ldp/http/Patch';
|
||||
import { Representation } from '../ldp/representation/Representation';
|
||||
import { RepresentationPreferences } from '../ldp/representation/RepresentationPreferences';
|
||||
import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
|
||||
import { ResourceStore } from './ResourceStore';
|
||||
|
||||
/**
|
||||
* Store that calls the corresponding functions of the source Store.
|
||||
* Can be extended by stores that do not want to override all functions
|
||||
* by implementing a decorator pattern.
|
||||
*/
|
||||
export class PassthroughStore implements ResourceStore {
|
||||
protected readonly source: ResourceStore;
|
||||
|
||||
public constructor(source: ResourceStore) {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
public async addResource(container: ResourceIdentifier, representation: Representation,
|
||||
conditions?: Conditions): Promise<ResourceIdentifier> {
|
||||
return this.source.addResource(container, representation, conditions);
|
||||
}
|
||||
|
||||
public async deleteResource(identifier: ResourceIdentifier, conditions?: Conditions): Promise<void> {
|
||||
return this.source.deleteResource(identifier, conditions);
|
||||
}
|
||||
|
||||
public async getRepresentation(identifier: ResourceIdentifier, preferences: RepresentationPreferences,
|
||||
conditions?: Conditions): Promise<Representation> {
|
||||
return this.source.getRepresentation(identifier, preferences, conditions);
|
||||
}
|
||||
|
||||
public async modifyResource(identifier: ResourceIdentifier, patch: Patch, conditions?: Conditions): Promise<void> {
|
||||
return this.source.modifyResource(identifier, patch, conditions);
|
||||
}
|
||||
|
||||
public async setRepresentation(identifier: ResourceIdentifier, representation: Representation,
|
||||
conditions?: Conditions): Promise<void> {
|
||||
return this.source.setRepresentation(identifier, representation, conditions);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
import { Conditions } from './Conditions';
|
||||
import { PassthroughStore } from './PassthroughStore';
|
||||
import { Patch } from '../ldp/http/Patch';
|
||||
import { PatchHandler } from './patch/PatchHandler';
|
||||
import { Representation } from '../ldp/representation/Representation';
|
||||
import { RepresentationPreferences } from '../ldp/representation/RepresentationPreferences';
|
||||
import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
|
||||
import { ResourceStore } from './ResourceStore';
|
||||
|
||||
@@ -11,34 +10,14 @@ import { ResourceStore } from './ResourceStore';
|
||||
* If the original store supports the {@link Patch}, behaviour will be identical,
|
||||
* otherwise one of the {@link PatchHandler}s supporting the given Patch will be called instead.
|
||||
*/
|
||||
export class PatchingStore implements ResourceStore {
|
||||
private readonly source: ResourceStore;
|
||||
export class PatchingStore extends PassthroughStore {
|
||||
private readonly patcher: PatchHandler;
|
||||
|
||||
public constructor(source: ResourceStore, patcher: PatchHandler) {
|
||||
this.source = source;
|
||||
super(source);
|
||||
this.patcher = patcher;
|
||||
}
|
||||
|
||||
public async addResource(container: ResourceIdentifier, representation: Representation,
|
||||
conditions?: Conditions): Promise<ResourceIdentifier> {
|
||||
return this.source.addResource(container, representation, conditions);
|
||||
}
|
||||
|
||||
public async deleteResource(identifier: ResourceIdentifier, conditions?: Conditions): Promise<void> {
|
||||
return this.source.deleteResource(identifier, conditions);
|
||||
}
|
||||
|
||||
public async getRepresentation(identifier: ResourceIdentifier, preferences: RepresentationPreferences,
|
||||
conditions?: Conditions): Promise<Representation> {
|
||||
return this.source.getRepresentation(identifier, preferences, conditions);
|
||||
}
|
||||
|
||||
public async setRepresentation(identifier: ResourceIdentifier, representation: Representation,
|
||||
conditions?: Conditions): Promise<void> {
|
||||
return this.source.setRepresentation(identifier, representation, conditions);
|
||||
}
|
||||
|
||||
public async modifyResource(identifier: ResourceIdentifier, patch: Patch, conditions?: Conditions): Promise<void> {
|
||||
try {
|
||||
return await this.source.modifyResource(identifier, patch, conditions);
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
import { Representation } from '../ldp/representation/Representation';
|
||||
import { RepresentationPreferences } from '../ldp/representation/RepresentationPreferences';
|
||||
|
||||
/**
|
||||
* Allows converting from one resource representation to another.
|
||||
*/
|
||||
export interface RepresentationConverter {
|
||||
/**
|
||||
* Checks if the converter supports converting the given resource based on the given preferences.
|
||||
* @param representation - The input representation.
|
||||
* @param preferences - The requested representation preferences.
|
||||
*
|
||||
* @returns A promise resolving to a boolean representing whether this conversion can be done.
|
||||
*/
|
||||
supports: (representation: Representation, preferences: RepresentationPreferences) => Promise<boolean>;
|
||||
/**
|
||||
* Converts the given representation.
|
||||
* @param representation - The input representation to convert.
|
||||
* @param preferences - The requested representation preferences.
|
||||
*
|
||||
* @returns A promise resolving to the requested representation.
|
||||
*/
|
||||
convert: (representation: Representation, preferences: RepresentationPreferences) => Promise<Representation>;
|
||||
}
|
||||
47
src/storage/RepresentationConvertingStore.ts
Normal file
47
src/storage/RepresentationConvertingStore.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { Conditions } from './Conditions';
|
||||
import { matchingMediaType } from '../util/Util';
|
||||
import { PassthroughStore } from './PassthroughStore';
|
||||
import { Representation } from '../ldp/representation/Representation';
|
||||
import { RepresentationConverter } from './conversion/RepresentationConverter';
|
||||
import { RepresentationPreferences } from '../ldp/representation/RepresentationPreferences';
|
||||
import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
|
||||
import { ResourceStore } from './ResourceStore';
|
||||
|
||||
/**
|
||||
* Store that overrides the `getRepresentation` function.
|
||||
* Tries to convert the {@link Representation} it got from the source store
|
||||
* so it matches one of the given type preferences.
|
||||
*
|
||||
* In the future this class should take the preferences of the request into account.
|
||||
* Even if there is a match with the output from the store,
|
||||
* if there is a low weight for that type conversions might still be preferred.
|
||||
*/
|
||||
export class RepresentationConvertingStore extends PassthroughStore {
|
||||
private readonly converter: RepresentationConverter;
|
||||
|
||||
public constructor(source: ResourceStore, converter: RepresentationConverter) {
|
||||
super(source);
|
||||
this.converter = converter;
|
||||
}
|
||||
|
||||
public async getRepresentation(identifier: ResourceIdentifier, preferences: RepresentationPreferences,
|
||||
conditions?: Conditions): Promise<Representation> {
|
||||
const representation = await super.getRepresentation(identifier, preferences, conditions);
|
||||
if (this.matchesPreferences(representation, preferences)) {
|
||||
return representation;
|
||||
}
|
||||
return this.converter.handleSafe({ identifier, representation, preferences });
|
||||
}
|
||||
|
||||
private matchesPreferences(representation: Representation, preferences: RepresentationPreferences): boolean {
|
||||
if (!preferences.type) {
|
||||
return true;
|
||||
}
|
||||
return Boolean(
|
||||
representation.metadata.contentType &&
|
||||
preferences.type.some((type): boolean =>
|
||||
type.weight > 0 &&
|
||||
matchingMediaType(type.value, representation.metadata.contentType!)),
|
||||
);
|
||||
}
|
||||
}
|
||||
47
src/storage/conversion/ConversionUtil.ts
Normal file
47
src/storage/conversion/ConversionUtil.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { matchingMediaType } from '../../util/Util';
|
||||
import { RepresentationConverterArgs } from './RepresentationConverter';
|
||||
import { RepresentationPreference } from '../../ldp/representation/RepresentationPreference';
|
||||
import { RepresentationPreferences } from '../../ldp/representation/RepresentationPreferences';
|
||||
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
|
||||
|
||||
/**
|
||||
* Filters out the media types from the preferred types that correspond to one of the supported types.
|
||||
* @param preferences - Preferences for output type.
|
||||
* @param supported - Types supported by the parser.
|
||||
*
|
||||
* @throws UnsupportedHttpError
|
||||
* If the type preferences are undefined.
|
||||
*
|
||||
* @returns The filtered list of preferences.
|
||||
*/
|
||||
export const matchingTypes = (preferences: RepresentationPreferences, supported: string[]):
|
||||
RepresentationPreference[] => {
|
||||
if (!Array.isArray(preferences.type)) {
|
||||
throw new UnsupportedHttpError('Output type required for conversion.');
|
||||
}
|
||||
return preferences.type.filter(({ value, weight }): boolean => weight > 0 &&
|
||||
supported.some((type): boolean => matchingMediaType(value, type)));
|
||||
};
|
||||
|
||||
/**
|
||||
* Runs some standard checks on the input request:
|
||||
* - Checks if there is a content type for the input.
|
||||
* - Checks if the input type is supported by the parser.
|
||||
* - Checks if the parser can produce one of the preferred output types.
|
||||
* @param request - Incoming arguments.
|
||||
* @param supportedIn - Media types that can be parsed by the converter.
|
||||
* @param supportedOut - Media types that can be produced by the converter.
|
||||
*/
|
||||
export const checkRequest = (request: RepresentationConverterArgs, supportedIn: string[], supportedOut: string[]):
|
||||
void => {
|
||||
const inType = request.representation.metadata.contentType;
|
||||
if (!inType) {
|
||||
throw new UnsupportedHttpError('Input type required for conversion.');
|
||||
}
|
||||
if (!supportedIn.some((type): boolean => matchingMediaType(inType, type))) {
|
||||
throw new UnsupportedHttpError(`Can only convert from ${supportedIn} to ${supportedOut}.`);
|
||||
}
|
||||
if (matchingTypes(request.preferences, supportedOut).length <= 0) {
|
||||
throw new UnsupportedHttpError(`Can only convert from ${supportedIn} to ${supportedOut}.`);
|
||||
}
|
||||
};
|
||||
28
src/storage/conversion/QuadToTurtleConverter.ts
Normal file
28
src/storage/conversion/QuadToTurtleConverter.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { checkRequest } from './ConversionUtil';
|
||||
import { Representation } from '../../ldp/representation/Representation';
|
||||
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
|
||||
import { StreamWriter } from 'n3';
|
||||
import { CONTENT_TYPE_QUADS, DATA_TYPE_BINARY } from '../../util/ContentTypes';
|
||||
import { RepresentationConverter, RepresentationConverterArgs } from './RepresentationConverter';
|
||||
|
||||
/**
|
||||
* Converts `internal/quads` to `text/turtle`.
|
||||
*/
|
||||
export class QuadToTurtleConverter extends RepresentationConverter {
|
||||
public async canHandle(input: RepresentationConverterArgs): Promise<void> {
|
||||
checkRequest(input, [ CONTENT_TYPE_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' };
|
||||
return {
|
||||
dataType: DATA_TYPE_BINARY,
|
||||
data: quads.data.pipe(new StreamWriter({ format: 'text/turtle' })),
|
||||
metadata,
|
||||
};
|
||||
}
|
||||
}
|
||||
24
src/storage/conversion/RepresentationConverter.ts
Normal file
24
src/storage/conversion/RepresentationConverter.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { AsyncHandler } from '../../util/AsyncHandler';
|
||||
import { Representation } from '../../ldp/representation/Representation';
|
||||
import { RepresentationPreferences } from '../../ldp/representation/RepresentationPreferences';
|
||||
import { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier';
|
||||
|
||||
export interface RepresentationConverterArgs {
|
||||
/**
|
||||
* Identifier of the resource. Can be used as base IRI.
|
||||
*/
|
||||
identifier: ResourceIdentifier;
|
||||
/**
|
||||
* Representation to convert.
|
||||
*/
|
||||
representation: Representation;
|
||||
/**
|
||||
* Preferences indicating what is requested.
|
||||
*/
|
||||
preferences: RepresentationPreferences;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a {@link Representation} from one media type to another, based on the given preferences.
|
||||
*/
|
||||
export abstract class RepresentationConverter extends AsyncHandler<RepresentationConverterArgs, Representation> {}
|
||||
38
src/storage/conversion/TurtleToQuadConverter.ts
Normal file
38
src/storage/conversion/TurtleToQuadConverter.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { checkRequest } from './ConversionUtil';
|
||||
import { PassThrough } from 'stream';
|
||||
import { Representation } from '../../ldp/representation/Representation';
|
||||
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
|
||||
import { StreamParser } from 'n3';
|
||||
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
|
||||
import { CONTENT_TYPE_QUADS, DATA_TYPE_QUAD } from '../../util/ContentTypes';
|
||||
import { RepresentationConverter, RepresentationConverterArgs } from './RepresentationConverter';
|
||||
|
||||
/**
|
||||
* Converts `text/turtle` to `internal/quads`.
|
||||
*/
|
||||
export class TurtleToQuadConverter extends RepresentationConverter {
|
||||
public async canHandle(input: RepresentationConverterArgs): Promise<void> {
|
||||
checkRequest(input, [ 'text/turtle' ], [ CONTENT_TYPE_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 };
|
||||
|
||||
// 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 }));
|
||||
data.pipe(errorStream);
|
||||
data.on('error', (error): boolean => errorStream.emit('error', new UnsupportedHttpError(error.message)));
|
||||
|
||||
return {
|
||||
dataType: DATA_TYPE_QUAD,
|
||||
data: errorStream,
|
||||
metadata,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -12,4 +12,33 @@ import { Readable } from 'stream';
|
||||
*/
|
||||
export const ensureTrailingSlash = (path: string): string => path.replace(/\/*$/u, '/');
|
||||
|
||||
/**
|
||||
* Joins all strings of a stream.
|
||||
* @param stream - Stream of strings.
|
||||
*
|
||||
* @returns The joined string.
|
||||
*/
|
||||
export const readableToString = async(stream: Readable): Promise<string> => (await arrayifyStream(stream)).join('');
|
||||
|
||||
/**
|
||||
* Checks if the given two media types/ranges match each other.
|
||||
* Takes wildcards into account.
|
||||
* @param mediaA - Media type to match.
|
||||
* @param mediaB - Media type to match.
|
||||
*
|
||||
* @returns True if the media type patterns can match each other.
|
||||
*/
|
||||
export const matchingMediaType = (mediaA: string, mediaB: string): boolean => {
|
||||
const [ typeA, subTypeA ] = mediaA.split('/');
|
||||
const [ typeB, subTypeB ] = mediaB.split('/');
|
||||
if (typeA === '*' || typeB === '*') {
|
||||
return true;
|
||||
}
|
||||
if (typeA !== typeB) {
|
||||
return false;
|
||||
}
|
||||
if (subTypeA === '*' || subTypeB === '*') {
|
||||
return true;
|
||||
}
|
||||
return subTypeA === subTypeB;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user