refactor: Streamline RepresentationMetadata interface

This commit is contained in:
Joachim Van Herwegen
2020-09-08 09:43:30 +02:00
parent 76319ba360
commit 8d3979372b
36 changed files with 416 additions and 230 deletions

View File

@@ -4,7 +4,7 @@ import { RepresentationMetadata } from '../ldp/representation/RepresentationMeta
import { ExpressHttpServer } from '../server/ExpressHttpServer';
import { ResourceStore } from '../storage/ResourceStore';
import { TEXT_TURTLE } from '../util/ContentTypes';
import { CONTENT_TYPE } from '../util/MetadataTypes';
import { MA_CONTENT_TYPE } from '../util/MetadataTypes';
/**
* Invokes all logic to setup a server.
@@ -50,11 +50,10 @@ export class Setup {
acl:mode acl:Control;
acl:accessTo <${this.base}>;
acl:default <${this.base}>.`;
const aclId = await this.aclManager.getAcl({ path: this.base });
const metadata = new RepresentationMetadata(aclId.path);
metadata.set(CONTENT_TYPE, TEXT_TURTLE);
const baseAclId = await this.aclManager.getAcl({ path: this.base });
const metadata = new RepresentationMetadata(baseAclId.path, { [MA_CONTENT_TYPE]: TEXT_TURTLE });
await this.store.setRepresentation(
aclId,
baseAclId,
{
binary: true,
data: streamifyArray([ acl ]),

View File

@@ -1,7 +1,6 @@
import { HttpResponse } from '../../server/HttpResponse';
import { HttpError } from '../../util/errors/HttpError';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import { CONTENT_TYPE } from '../../util/MetadataTypes';
import { ResponseDescription } from '../operations/ResponseDescription';
import { ResponseWriter } from './ResponseWriter';
@@ -30,7 +29,7 @@ export class BasicResponseWriter extends ResponseWriter {
} else {
input.response.setHeader('location', input.result.identifier.path);
if (input.result.body) {
const contentType = input.result.body.metadata.get(CONTENT_TYPE)?.value ?? 'text/plain';
const contentType = input.result.body.metadata.contentType ?? 'text/plain';
input.response.setHeader('content-type', contentType);
input.result.body.data.pipe(input.response);
}

View File

@@ -1,6 +1,6 @@
import { HttpRequest } from '../../server/HttpRequest';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import { CONTENT_TYPE, SLUG, TYPE } from '../../util/MetadataTypes';
import { HTTP_SLUG, RDF_TYPE, MA_CONTENT_TYPE } from '../../util/MetadataTypes';
import { Representation } from '../representation/Representation';
import { RepresentationMetadata } from '../representation/RepresentationMetadata';
import { BodyParser } from './BodyParser';
@@ -40,8 +40,7 @@ export class RawBodyParser extends BodyParser {
private parseMetadata(input: HttpRequest): RepresentationMetadata {
const contentType = /^[^;]*/u.exec(input.headers['content-type']!)![0];
const metadata: RepresentationMetadata = new RepresentationMetadata();
metadata.set(CONTENT_TYPE, contentType);
const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: contentType });
const { link, slug } = input.headers;
@@ -49,7 +48,7 @@ export class RawBodyParser extends BodyParser {
if (Array.isArray(slug)) {
throw new UnsupportedHttpError('At most 1 slug header is allowed.');
}
metadata.set(SLUG, slug);
metadata.set(HTTP_SLUG, slug);
}
// There are similarities here to Accept header parsing so that library should become more generic probably
@@ -60,11 +59,12 @@ export class RawBodyParser extends BodyParser {
const [ , rel ] = /^ *; *rel="(.*)"$/u.exec(rest) ?? [];
return { url, rel };
});
parsedLinks.forEach((entry): void => {
for (const entry of parsedLinks) {
if (entry.rel === 'type') {
metadata.set(TYPE, entry.url);
metadata.set(RDF_TYPE, entry.url);
break;
}
});
}
}
return metadata;

View File

@@ -3,7 +3,7 @@ import { translate } from 'sparqlalgebrajs';
import { HttpRequest } from '../../server/HttpRequest';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import { UnsupportedMediaTypeHttpError } from '../../util/errors/UnsupportedMediaTypeHttpError';
import { CONTENT_TYPE } from '../../util/MetadataTypes';
import { MA_CONTENT_TYPE } from '../../util/MetadataTypes';
import { readableToString } from '../../util/Util';
import { RepresentationMetadata } from '../representation/RepresentationMetadata';
import { BodyParser } from './BodyParser';
@@ -35,8 +35,7 @@ export class SparqlUpdateBodyParser extends BodyParser {
const sparql = await readableToString(toAlgebraStream);
const algebra = translate(sparql, { quads: true });
const metadata = new RepresentationMetadata();
metadata.add(CONTENT_TYPE, 'application/sparql-update');
const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: 'application/sparql-update' });
// Prevent body from being requested again
return {

View File

@@ -0,0 +1,41 @@
import { literal, namedNode } from '@rdfjs/data-model';
import type { Literal, NamedNode, Term } from 'rdf-js';
import { MA_CONTENT_TYPE } from '../../util/MetadataTypes';
// Shorthands for commonly used predicates
const shorthands: { [id: string]: NamedNode } = {
contentType: namedNode(MA_CONTENT_TYPE),
};
// Caches named node conversions
const termMap: { [id: string]: NamedNode } = {};
/**
* @param input - Checks if this is a {@link Term}.
*/
export const isTerm = (input?: any): input is Term => input?.termType;
/**
* Converts the incoming predicate to a named node.
* In case of string, first checks if it is a shorthand, if not a new named node gets made.
* @param predicate - Predicate to potentially transform.
*/
export const getPredicateTerm = (predicate: NamedNode | string): NamedNode => {
if (typeof predicate === 'string') {
if (shorthands[predicate]) {
return shorthands[predicate];
}
if (!termMap[predicate]) {
termMap[predicate] = namedNode(predicate);
}
return termMap[predicate];
}
return predicate;
};
/**
* Converts an object to a literal when needed.
* @param object - Object to potentially transform.
*/
export const getObjectTerm = (object: NamedNode | Literal | string): NamedNode | Literal =>
typeof object === 'string' ? literal(object) : object;

View File

@@ -1,9 +1,13 @@
import { quad as createQuad, literal, namedNode } from '@rdfjs/data-model';
import { quad as createQuad, namedNode } from '@rdfjs/data-model';
import { Store } from 'n3';
import type { BlankNode, Literal, NamedNode, Quad, Term } from 'rdf-js';
import { getObjectTerm, getPredicateTerm, isTerm } from './MetadataUtil';
export type MetadataOverrideValue = NamedNode | Literal | string | (NamedNode | Literal | string)[];
/**
* Stores the metadata triples and provides methods for easy access.
* Most functions return the metadata object to allow for chaining.
*/
export class RepresentationMetadata {
private store: Store;
@@ -13,21 +17,59 @@ export class RepresentationMetadata {
* @param identifier - Identifier of the resource relevant to this metadata.
* A blank node will be generated if none is provided.
* Strings will be converted to named nodes. @ignored
* @param quads - Quads to fill the metadata with. @ignored
* @param overrides - Key/value map of extra values that need to be added to the metadata. @ignored
*
* `@ignored` tags are necessary for Components-Generator.js
* `@ignored` tag is necessary for Components-Generator.js
*/
public constructor(identifier?: NamedNode | BlankNode | string, quads?: Quad[]) {
this.store = new Store(quads);
if (identifier) {
if (typeof identifier === 'string') {
this.id = namedNode(identifier);
} else {
this.id = identifier;
}
public constructor(identifier?: NamedNode | BlankNode | string, overrides?: { [pred: string]: MetadataOverrideValue});
/**
* @param metadata - Starts as a copy of the input metadata.
* @param overrides - Key/value map of extra values that need to be added to the metadata.
* Will override values that were set by the input metadata.
*/
public constructor(metadata?: RepresentationMetadata, overrides?: { [pred: string]: MetadataOverrideValue});
/**
* @param overrides - Key/value map of extra values that need to be added to the metadata.
*/
public constructor(overrides?: { [pred: string]: MetadataOverrideValue});
public constructor(
input?: NamedNode | BlankNode | string | RepresentationMetadata | { [pred: string]: MetadataOverrideValue},
overrides?: { [pred: string]: MetadataOverrideValue},
) {
this.store = new Store();
if (typeof input === 'string') {
this.id = namedNode(input);
} else if (isTerm(input)) {
this.id = input;
} else if (input instanceof RepresentationMetadata) {
this.id = input.identifier;
this.addQuads(input.quads());
} else {
overrides = input;
this.id = this.store.createBlankNode();
}
if (overrides) {
this.setOverrides(overrides);
}
}
private setOverrides(overrides: { [pred: string]: MetadataOverrideValue}): void {
for (const predicate of Object.keys(overrides)) {
const namedPredicate = getPredicateTerm(predicate);
this.removeAll(namedPredicate);
let objects = overrides[predicate];
if (!Array.isArray(objects)) {
objects = [ objects ];
}
for (const object of objects.map(getObjectTerm)) {
this.store.addQuad(this.id, namedPredicate, object);
}
}
}
/**
@@ -46,31 +88,47 @@ export class RepresentationMetadata {
}
public set identifier(id: NamedNode | BlankNode) {
const quads = this.quads().map((quad): Quad => {
if (quad.subject.equals(this.id)) {
return createQuad(id, quad.predicate, quad.object, quad.graph);
}
if (quad.object.equals(this.id)) {
return createQuad(quad.subject, quad.predicate, id, quad.graph);
}
return quad;
});
this.store = new Store(quads);
this.id = id;
if (!id.equals(this.id)) {
// Convert all instances of the old identifier to the new identifier in the stored quads
const quads = this.quads().map((quad): Quad => {
if (quad.subject.equals(this.id)) {
return createQuad(id, quad.predicate, quad.object, quad.graph);
}
if (quad.object.equals(this.id)) {
return createQuad(quad.subject, quad.predicate, id, quad.graph);
}
return quad;
});
this.store = new Store(quads);
this.id = id;
}
}
/**
* Helper function to import all entries from the given metadata.
* If the new metadata has a different identifier the internal one will be updated.
* @param metadata - Metadata to import.
*/
public setMetadata(metadata: RepresentationMetadata): this {
this.identifier = metadata.identifier;
this.addQuads(metadata.quads());
return this;
}
/**
* @param quads - Quads to add to the metadata.
*/
public addQuads(quads: Quad[]): void {
public addQuads(quads: Quad[]): this {
this.store.addQuads(quads);
return this;
}
/**
* @param quads - Quads to remove from the metadata.
*/
public removeQuads(quads: Quad[]): void {
public removeQuads(quads: Quad[]): this {
this.store.removeQuads(quads);
return this;
}
/**
@@ -78,8 +136,9 @@ export class RepresentationMetadata {
* @param predicate - Predicate linking identifier to value.
* @param object - Value to add.
*/
public add(predicate: NamedNode, object: NamedNode | Literal | string): void {
this.store.addQuad(this.id, predicate, typeof object === 'string' ? literal(object) : object);
public add(predicate: NamedNode | string, object: NamedNode | Literal | string): this {
this.store.addQuad(this.id, getPredicateTerm(predicate), getObjectTerm(object));
return this;
}
/**
@@ -87,16 +146,29 @@ export class RepresentationMetadata {
* @param predicate - Predicate linking identifier to value.
* @param object - Value to remove.
*/
public remove(predicate: NamedNode, object: NamedNode | Literal | string): void {
this.store.removeQuad(this.id, predicate, typeof object === 'string' ? literal(object) : object);
public remove(predicate: NamedNode | string, object: NamedNode | Literal | string): this {
this.store.removeQuad(this.id, getPredicateTerm(predicate), getObjectTerm(object));
return this;
}
/**
* Removes all values linked through the given predicate.
* @param predicate - Predicate to remove.
*/
public removeAll(predicate: NamedNode): void {
this.removeQuads(this.store.getQuads(this.id, predicate, null, null));
public removeAll(predicate: NamedNode | string): this {
this.removeQuads(this.store.getQuads(this.id, getPredicateTerm(predicate), null, null));
return this;
}
/**
* Finds all object values matching the given predicate.
* @param predicate - Predicate to get the values for.
*
* @returns An array with all matches.
*/
public getAll(predicate: NamedNode | string): Term[] {
return this.store.getQuads(this.id, getPredicateTerm(predicate), null, null)
.map((quad): Term => quad.object);
}
/**
@@ -107,24 +179,41 @@ export class RepresentationMetadata {
*
* @returns The corresponding value. Undefined if there is no match
*/
public get(predicate: NamedNode): Term | undefined {
const quads = this.store.getQuads(this.id, predicate, null, null);
if (quads.length === 0) {
public get(predicate: NamedNode | string): Term | undefined {
const terms = this.getAll(predicate);
if (terms.length === 0) {
return;
}
if (quads.length > 1) {
throw new Error(`Multiple results for ${predicate.value}`);
if (terms.length > 1) {
throw new Error(`Multiple results for ${typeof predicate === 'string' ? predicate : predicate.value}`);
}
return quads[0].object;
return terms[0];
}
/**
* Sets the value for the given predicate, removing all other instances.
* In case the object is undefined this is identical to `removeAll(predicate)`.
* @param predicate - Predicate linking to the value.
* @param object - Value to set.
*/
public set(predicate: NamedNode, object: NamedNode | Literal | string): void {
public set(predicate: NamedNode | string, object?: NamedNode | Literal | string): this {
this.removeAll(predicate);
this.add(predicate, object);
if (object) {
this.add(predicate, object);
}
return this;
}
// Syntactic sugar for common predicates
/**
* Shorthand for the CONTENT_TYPE predicate.
*/
public get contentType(): string | undefined {
return this.get(getPredicateTerm('contentType'))?.value;
}
public set contentType(input) {
this.set(getPredicateTerm('contentType'), input);
}
}

View File

@@ -14,7 +14,15 @@ import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
import { UnsupportedMediaTypeHttpError } from '../util/errors/UnsupportedMediaTypeHttpError';
import { InteractionController } from '../util/InteractionController';
import { MetadataController } from '../util/MetadataController';
import { BYTE_SIZE, CONTENT_TYPE, LAST_CHANGED, SLUG, TYPE } from '../util/MetadataTypes';
import {
HTTP_BYTE_SIZE,
HTTP_LAST_CHANGED,
HTTP_SLUG,
MA_CONTENT_TYPE,
RDF_TYPE,
XSD_DATE_TIME,
XSD_INTEGER,
} from '../util/MetadataTypes';
import { ensureTrailingSlash } from '../util/Util';
import { ExtensionBasedMapper } from './ExtensionBasedMapper';
import { ResourceStore } from './ResourceStore';
@@ -57,11 +65,11 @@ export class FileResourceStore implements ResourceStore {
// Get the path from the request URI, all metadata triples if any, and the Slug and Link header values.
const path = this.resourceMapper.getRelativePath(container);
const slug = representation.metadata.get(SLUG)?.value;
const type = representation.metadata.get(TYPE)?.value;
const slug = representation.metadata.get(HTTP_SLUG)?.value;
const types = representation.metadata.getAll(RDF_TYPE);
// Create a new container or resource in the parent container with a specific name based on the incoming headers.
const isContainer = this.interactionController.isContainer(slug, type);
const isContainer = this.interactionController.isContainer(slug, types);
const newIdentifier = this.interactionController.generateIdentifier(isContainer, slug);
let metadata;
// eslint-disable-next-line no-param-reassign
@@ -154,14 +162,14 @@ export class FileResourceStore implements ResourceStore {
// eslint-disable-next-line no-param-reassign
representation.metadata.identifier = DataFactory.namedNode(identifier.path);
const raw = representation.metadata.quads();
const type = representation.metadata.get(TYPE)?.value;
const types = representation.metadata.getAll(RDF_TYPE);
let metadata: Readable | undefined;
if (raw.length > 0) {
metadata = streamifyArray(raw);
metadata = this.metadataController.serializeQuads(raw);
}
// Create a new container or resource in the parent container with a specific name based on the incoming headers.
const isContainer = this.interactionController.isContainer(documentName, type);
const isContainer = this.interactionController.isContainer(documentName, types);
const newIdentifier = this.interactionController.generateIdentifier(isContainer, documentName);
return isContainer ?
await this.setDirectoryRepresentation(containerPath, newIdentifier, metadata) :
@@ -222,10 +230,10 @@ export class FileResourceStore implements ResourceStore {
} catch (_) {
// Metadata file doesn't exist so lets keep `rawMetaData` an empty array.
}
const metadata = new RepresentationMetadata(this.resourceMapper.mapFilePathToUrl(path), rawMetadata);
metadata.set(LAST_CHANGED, stats.mtime.toISOString());
metadata.set(BYTE_SIZE, DataFactory.literal(stats.size));
metadata.set(CONTENT_TYPE, contentType);
const metadata = new RepresentationMetadata(this.resourceMapper.mapFilePathToUrl(path)).addQuads(rawMetadata)
.set(HTTP_LAST_CHANGED, DataFactory.literal(stats.mtime.toISOString(), XSD_DATE_TIME))
.set(HTTP_BYTE_SIZE, DataFactory.literal(stats.size, XSD_INTEGER));
metadata.contentType = contentType;
return { metadata, data: readStream, binary: true };
}
@@ -256,9 +264,9 @@ export class FileResourceStore implements ResourceStore {
// Metadata file doesn't exist so lets keep `rawMetaData` an empty array.
}
const metadata = new RepresentationMetadata(containerURI, rawMetadata);
metadata.set(LAST_CHANGED, stats.mtime.toISOString());
metadata.set(CONTENT_TYPE, INTERNAL_QUADS);
const metadata = new RepresentationMetadata(containerURI).addQuads(rawMetadata)
.set(HTTP_LAST_CHANGED, DataFactory.literal(stats.mtime.toISOString(), XSD_DATE_TIME))
.set(MA_CONTENT_TYPE, INTERNAL_QUADS);
return {
binary: false,

View File

@@ -6,7 +6,7 @@ import { RepresentationMetadata } from '../ldp/representation/RepresentationMeta
import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
import { TEXT_TURTLE } from '../util/ContentTypes';
import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
import { CONTENT_TYPE } from '../util/MetadataTypes';
import { MA_CONTENT_TYPE } from '../util/MetadataTypes';
import { ensureTrailingSlash } from '../util/Util';
import { ResourceStore } from './ResourceStore';
@@ -26,8 +26,7 @@ export class InMemoryResourceStore implements ResourceStore {
public constructor(base: string) {
this.base = ensureTrailingSlash(base);
const metadata = new RepresentationMetadata();
metadata.add(CONTENT_TYPE, TEXT_TURTLE);
const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: TEXT_TURTLE });
this.store = {
// Default root entry (what you get when the identifier is equal to the base)
'': {

View File

@@ -1,7 +1,6 @@
import { Representation } from '../ldp/representation/Representation';
import { RepresentationPreferences } from '../ldp/representation/RepresentationPreferences';
import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
import { CONTENT_TYPE } from '../util/MetadataTypes';
import { matchingMediaType } from '../util/Util';
import { Conditions } from './Conditions';
import { RepresentationConverter } from './conversion/RepresentationConverter';
@@ -38,12 +37,12 @@ export class RepresentationConvertingStore<T extends ResourceStore = ResourceSto
if (!preferences.type) {
return true;
}
const contentType = representation.metadata.get(CONTENT_TYPE);
const { contentType } = representation.metadata;
return Boolean(
contentType &&
preferences.type.some((type): boolean =>
type.weight > 0 &&
matchingMediaType(type.value, contentType.value)),
matchingMediaType(type.value, contentType)),
);
}
}

View File

@@ -1,7 +1,7 @@
import { Representation } from '../../ldp/representation/Representation';
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
import { RepresentationPreferences } from '../../ldp/representation/RepresentationPreferences';
import { CONTENT_TYPE } from '../../util/MetadataTypes';
import { MA_CONTENT_TYPE } from '../../util/MetadataTypes';
import { matchingMediaType } from '../../util/Util';
import { RepresentationConverterArgs } from './RepresentationConverter';
import { TypedRepresentationConverter } from './TypedRepresentationConverter';
@@ -53,8 +53,7 @@ export class ChainedConverter extends TypedRepresentationConverter {
const idx = this.converters.length - 1;
const lastChain = await this.getMatchingType(this.converters[idx - 1], this.converters[idx]);
const oldMeta = input.representation.metadata;
const metadata = new RepresentationMetadata(oldMeta.identifier, oldMeta.quads());
metadata.set(CONTENT_TYPE, lastChain);
const metadata = new RepresentationMetadata(oldMeta, { [MA_CONTENT_TYPE]: lastChain });
const representation: Representation = { ...input.representation, metadata };
await this.last.canHandle({ ...input, representation });
}

View File

@@ -1,7 +1,6 @@
import { RepresentationPreference } from '../../ldp/representation/RepresentationPreference';
import { RepresentationPreferences } from '../../ldp/representation/RepresentationPreferences';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import { CONTENT_TYPE } from '../../util/MetadataTypes';
import { matchingMediaType } from '../../util/Util';
import { RepresentationConverterArgs } from './RepresentationConverter';
@@ -35,11 +34,11 @@ RepresentationPreference[] => {
*/
export const checkRequest = (request: RepresentationConverterArgs, supportedIn: string[], supportedOut: string[]):
void => {
const inType = request.representation.metadata.get(CONTENT_TYPE);
const inType = request.representation.metadata.contentType;
if (!inType) {
throw new UnsupportedHttpError('Input type required for conversion.');
}
if (!supportedIn.some((type): boolean => matchingMediaType(inType.value, type))) {
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) {

View File

@@ -4,7 +4,7 @@ import { Representation } from '../../ldp/representation/Representation';
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
import { RepresentationPreferences } from '../../ldp/representation/RepresentationPreferences';
import { INTERNAL_QUADS } from '../../util/ContentTypes';
import { CONTENT_TYPE } from '../../util/MetadataTypes';
import { MA_CONTENT_TYPE } from '../../util/MetadataTypes';
import { checkRequest, matchingTypes } from './ConversionUtil';
import { RepresentationConverterArgs } from './RepresentationConverter';
import { TypedRepresentationConverter } from './TypedRepresentationConverter';
@@ -31,8 +31,7 @@ export class QuadToRdfConverter extends TypedRepresentationConverter {
private async quadsToRdf(quads: Representation, preferences: RepresentationPreferences): Promise<Representation> {
const contentType = matchingTypes(preferences, await rdfSerializer.getContentTypes())[0].value;
const metadata = new RepresentationMetadata(quads.metadata.identifier, quads.metadata.quads());
metadata.set(CONTENT_TYPE, contentType);
const metadata = new RepresentationMetadata(quads.metadata, { [MA_CONTENT_TYPE]: contentType });
return {
binary: true,
data: rdfSerializer.serialize(quads.data, { contentType }) as Readable,

View File

@@ -2,7 +2,7 @@ import { StreamWriter } from 'n3';
import { Representation } from '../../ldp/representation/Representation';
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
import { INTERNAL_QUADS, TEXT_TURTLE } from '../../util/ContentTypes';
import { CONTENT_TYPE } from '../../util/MetadataTypes';
import { MA_CONTENT_TYPE } from '../../util/MetadataTypes';
import { checkRequest } from './ConversionUtil';
import { RepresentationConverter, RepresentationConverterArgs } from './RepresentationConverter';
@@ -19,8 +19,7 @@ export class QuadToTurtleConverter extends RepresentationConverter {
}
private quadsToTurtle(quads: Representation): Representation {
const metadata = new RepresentationMetadata(quads.metadata.identifier, quads.metadata.quads());
metadata.set(CONTENT_TYPE, TEXT_TURTLE);
const metadata = new RepresentationMetadata(quads.metadata, { [MA_CONTENT_TYPE]: TEXT_TURTLE });
return {
binary: true,
data: quads.data.pipe(new StreamWriter({ format: TEXT_TURTLE })),

View File

@@ -3,7 +3,7 @@ import rdfParser from 'rdf-parse';
import { Representation } from '../../ldp/representation/Representation';
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
import { INTERNAL_QUADS } from '../../util/ContentTypes';
import { CONTENT_TYPE } from '../../util/MetadataTypes';
import { MA_CONTENT_TYPE } from '../../util/MetadataTypes';
import { pipeStreamsAndErrors } from '../../util/Util';
import { checkRequest } from './ConversionUtil';
import { RepresentationConverterArgs } from './RepresentationConverter';
@@ -30,10 +30,9 @@ export class RdfToQuadConverter extends TypedRepresentationConverter {
}
private rdfToQuads(representation: Representation, baseIRI: string): Representation {
const metadata = new RepresentationMetadata(representation.metadata.identifier, representation.metadata.quads());
metadata.set(CONTENT_TYPE, INTERNAL_QUADS);
const metadata = new RepresentationMetadata(representation.metadata, { [MA_CONTENT_TYPE]: INTERNAL_QUADS });
const rawQuads = rdfParser.parse(representation.data, {
contentType: representation.metadata.get(CONTENT_TYPE)!.value,
contentType: representation.metadata.contentType!,
baseIRI,
});

View File

@@ -4,7 +4,7 @@ import { Representation } from '../../ldp/representation/Representation';
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
import { TEXT_TURTLE, INTERNAL_QUADS } from '../../util/ContentTypes';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import { CONTENT_TYPE } from '../../util/MetadataTypes';
import { MA_CONTENT_TYPE } from '../../util/MetadataTypes';
import { checkRequest } from './ConversionUtil';
import { RepresentationConverter, RepresentationConverterArgs } from './RepresentationConverter';
@@ -21,8 +21,7 @@ export class TurtleToQuadConverter extends RepresentationConverter {
}
private turtleToQuads(turtle: Representation, baseIRI: string): Representation {
const metadata = new RepresentationMetadata(turtle.metadata.identifier, turtle.metadata.quads());
metadata.set(CONTENT_TYPE, INTERNAL_QUADS);
const metadata = new RepresentationMetadata(turtle.metadata, { [MA_CONTENT_TYPE]: INTERNAL_QUADS });
// Catch parsing errors and emit correct error
// Node 10 requires both writableObjectMode and readableObjectMode

View File

@@ -10,7 +10,7 @@ import { RepresentationMetadata } from '../../ldp/representation/RepresentationM
import { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier';
import { INTERNAL_QUADS } from '../../util/ContentTypes';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import { CONTENT_TYPE } from '../../util/MetadataTypes';
import { MA_CONTENT_TYPE } from '../../util/MetadataTypes';
import { ResourceLocker } from '../ResourceLocker';
import { ResourceStore } from '../ResourceStore';
import { PatchHandler } from './PatchHandler';
@@ -67,8 +67,7 @@ export class SparqlUpdatePatchHandler extends PatchHandler {
});
store.removeQuads(deletes);
store.addQuads(inserts);
const metadata = new RepresentationMetadata(input.identifier.path);
metadata.set(CONTENT_TYPE, INTERNAL_QUADS);
const metadata = new RepresentationMetadata(input.identifier.path, { [MA_CONTENT_TYPE]: INTERNAL_QUADS });
const representation: Representation = {
binary: false,
data: store.match() as Readable,

View File

@@ -1,3 +1,4 @@
import type { Term } from 'rdf-js';
import { v4 as uuid } from 'uuid';
import { LINK_TYPE_LDP_BC, LINK_TYPE_LDPC } from './LinkTypes';
import { trimTrailingSlashes } from './Util';
@@ -5,20 +6,20 @@ import { trimTrailingSlashes } from './Util';
export class InteractionController {
/**
* Check whether a new container or a resource should be created based on the given parameters.
* @param slug - Incoming slug header.
* @param link - Incoming link header.
* @param slug - Incoming slug metadata.
* @param types - Incoming type metadata.
*/
public isContainer(slug?: string, link?: string): boolean {
if (!slug || !slug.endsWith('/')) {
return Boolean(link === LINK_TYPE_LDPC) || Boolean(link === LINK_TYPE_LDP_BC);
public isContainer(slug?: string, types?: Term[]): boolean {
if (types && types.length > 0) {
return types.some((type): boolean => type.value === LINK_TYPE_LDPC || type.value === LINK_TYPE_LDP_BC);
}
return !link || link === LINK_TYPE_LDPC || link === LINK_TYPE_LDP_BC;
return Boolean(slug?.endsWith('/'));
}
/**
* Get the identifier path the new resource should have.
* @param isContainer - Whether or not the resource is a container.
* @param slug - Incoming slug header.
* @param slug - Incoming slug metadata.
*/
public generateIdentifier(isContainer: boolean, slug?: string): string {
if (!slug) {

View File

@@ -1,9 +1,11 @@
import { namedNode } from '@rdfjs/data-model';
import { NamedNode } from 'rdf-js';
export const RDF_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';
export const TYPE: NamedNode = namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type');
export const CONTENT_TYPE: NamedNode = namedNode('http://www.w3.org/ns/ma-ont#format');
export const SLUG: NamedNode = namedNode('http://example.com/slug');
export const LAST_CHANGED: NamedNode = namedNode('http://example.com/lastChanged');
export const BYTE_SIZE: NamedNode = namedNode('http://example.com/byteSize');
export const ACL_RESOURCE: NamedNode = namedNode('http://example.com/acl');
export const ACL_RESOURCE = 'urn:solid:acl:resource';
export const MA_CONTENT_TYPE = 'http://www.w3.org/ns/ma-ont#format';
export const HTTP_SLUG = 'urn:solid:http:slug';
export const HTTP_LAST_CHANGED = 'urn:solid:http:lastChanged';
export const HTTP_BYTE_SIZE = 'urn:solid:http:byteSize';
export const XSD_DATE_TIME = 'http://www.w3.org/2001/XMLSchema#dateTime';
export const XSD_INTEGER = 'http://www.w3.org/2001/XMLSchema#integer';