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

@ -27,15 +27,18 @@ module.exports = {
'@typescript-eslint/no-empty-interface': 'off', '@typescript-eslint/no-empty-interface': 'off',
'@typescript-eslint/no-unnecessary-condition': 'off', // problems with optional parameters '@typescript-eslint/no-unnecessary-condition': 'off', // problems with optional parameters
'@typescript-eslint/space-before-function-paren': [ 'error', 'never' ], '@typescript-eslint/space-before-function-paren': [ 'error', 'never' ],
'@typescript-eslint/unified-signatures': 'off',
'class-methods-use-this': 'off', // conflicts with functions from interfaces that sometimes don't require `this` 'class-methods-use-this': 'off', // conflicts with functions from interfaces that sometimes don't require `this`
'comma-dangle': ['error', 'always-multiline'], 'comma-dangle': ['error', 'always-multiline'],
'dot-location': ['error', 'property'], 'dot-location': ['error', 'property'],
'lines-between-class-members': ['error', 'always', { exceptAfterSingleLine: true }], 'lines-between-class-members': ['error', 'always', { exceptAfterSingleLine: true }],
'max-len': ['error', { code: 120, ignoreUrls: true }], 'max-len': ['error', { code: 120, ignoreUrls: true }],
'no-param-reassign': 'off', // necessary in constructor overloading
'no-underscore-dangle': 'off', // conflicts with external libraries 'no-underscore-dangle': 'off', // conflicts with external libraries
'padding-line-between-statements': 'off', 'padding-line-between-statements': 'off',
'tsdoc/syntax': 'error',
'prefer-named-capture-group': 'off', 'prefer-named-capture-group': 'off',
'tsdoc/syntax': 'error',
'unicorn/no-fn-reference-in-iterator': 'off', // this prevents some functional programming paradigms
// Import // Import
'sort-imports': 'off', // Disabled in favor of eslint-plugin-import 'sort-imports': 'off', // Disabled in favor of eslint-plugin-import

View File

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

View File

@ -1,7 +1,6 @@
import { HttpResponse } from '../../server/HttpResponse'; import { HttpResponse } from '../../server/HttpResponse';
import { HttpError } from '../../util/errors/HttpError'; import { HttpError } from '../../util/errors/HttpError';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError'; import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import { CONTENT_TYPE } from '../../util/MetadataTypes';
import { ResponseDescription } from '../operations/ResponseDescription'; import { ResponseDescription } from '../operations/ResponseDescription';
import { ResponseWriter } from './ResponseWriter'; import { ResponseWriter } from './ResponseWriter';
@ -30,7 +29,7 @@ export class BasicResponseWriter extends ResponseWriter {
} else { } else {
input.response.setHeader('location', input.result.identifier.path); input.response.setHeader('location', input.result.identifier.path);
if (input.result.body) { 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.response.setHeader('content-type', contentType);
input.result.body.data.pipe(input.response); input.result.body.data.pipe(input.response);
} }

View File

@ -1,6 +1,6 @@
import { HttpRequest } from '../../server/HttpRequest'; import { HttpRequest } from '../../server/HttpRequest';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError'; 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 { Representation } from '../representation/Representation';
import { RepresentationMetadata } from '../representation/RepresentationMetadata'; import { RepresentationMetadata } from '../representation/RepresentationMetadata';
import { BodyParser } from './BodyParser'; import { BodyParser } from './BodyParser';
@ -40,8 +40,7 @@ export class RawBodyParser extends BodyParser {
private parseMetadata(input: HttpRequest): RepresentationMetadata { private parseMetadata(input: HttpRequest): RepresentationMetadata {
const contentType = /^[^;]*/u.exec(input.headers['content-type']!)![0]; const contentType = /^[^;]*/u.exec(input.headers['content-type']!)![0];
const metadata: RepresentationMetadata = new RepresentationMetadata(); const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: contentType });
metadata.set(CONTENT_TYPE, contentType);
const { link, slug } = input.headers; const { link, slug } = input.headers;
@ -49,7 +48,7 @@ export class RawBodyParser extends BodyParser {
if (Array.isArray(slug)) { if (Array.isArray(slug)) {
throw new UnsupportedHttpError('At most 1 slug header is allowed.'); 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 // 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) ?? []; const [ , rel ] = /^ *; *rel="(.*)"$/u.exec(rest) ?? [];
return { url, rel }; return { url, rel };
}); });
parsedLinks.forEach((entry): void => { for (const entry of parsedLinks) {
if (entry.rel === 'type') { if (entry.rel === 'type') {
metadata.set(TYPE, entry.url); metadata.set(RDF_TYPE, entry.url);
break;
} }
}); }
} }
return metadata; return metadata;

View File

@ -3,7 +3,7 @@ import { translate } from 'sparqlalgebrajs';
import { HttpRequest } from '../../server/HttpRequest'; import { HttpRequest } from '../../server/HttpRequest';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError'; import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import { UnsupportedMediaTypeHttpError } from '../../util/errors/UnsupportedMediaTypeHttpError'; 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 { readableToString } from '../../util/Util';
import { RepresentationMetadata } from '../representation/RepresentationMetadata'; import { RepresentationMetadata } from '../representation/RepresentationMetadata';
import { BodyParser } from './BodyParser'; import { BodyParser } from './BodyParser';
@ -35,8 +35,7 @@ export class SparqlUpdateBodyParser extends BodyParser {
const sparql = await readableToString(toAlgebraStream); const sparql = await readableToString(toAlgebraStream);
const algebra = translate(sparql, { quads: true }); const algebra = translate(sparql, { quads: true });
const metadata = new RepresentationMetadata(); const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: 'application/sparql-update' });
metadata.add(CONTENT_TYPE, 'application/sparql-update');
// Prevent body from being requested again // Prevent body from being requested again
return { 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 { Store } from 'n3';
import type { BlankNode, Literal, NamedNode, Quad, Term } from 'rdf-js'; 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. * Stores the metadata triples and provides methods for easy access.
* Most functions return the metadata object to allow for chaining.
*/ */
export class RepresentationMetadata { export class RepresentationMetadata {
private store: Store; private store: Store;
@ -13,21 +17,59 @@ export class RepresentationMetadata {
* @param identifier - Identifier of the resource relevant to this metadata. * @param identifier - Identifier of the resource relevant to this metadata.
* A blank node will be generated if none is provided. * A blank node will be generated if none is provided.
* Strings will be converted to named nodes. @ignored * 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[]) { public constructor(identifier?: NamedNode | BlankNode | string, overrides?: { [pred: string]: MetadataOverrideValue});
this.store = new Store(quads);
if (identifier) { /**
if (typeof identifier === 'string') { * @param metadata - Starts as a copy of the input metadata.
this.id = namedNode(identifier); * @param overrides - Key/value map of extra values that need to be added to the metadata.
} else { * Will override values that were set by the input metadata.
this.id = identifier; */
} 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 { } else {
overrides = input;
this.id = this.store.createBlankNode(); 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) { public set identifier(id: NamedNode | BlankNode) {
const quads = this.quads().map((quad): Quad => { if (!id.equals(this.id)) {
if (quad.subject.equals(this.id)) { // Convert all instances of the old identifier to the new identifier in the stored quads
return createQuad(id, quad.predicate, quad.object, quad.graph); const quads = this.quads().map((quad): Quad => {
} if (quad.subject.equals(this.id)) {
if (quad.object.equals(this.id)) { return createQuad(id, quad.predicate, quad.object, quad.graph);
return createQuad(quad.subject, quad.predicate, id, quad.graph); }
} if (quad.object.equals(this.id)) {
return quad; return createQuad(quad.subject, quad.predicate, id, quad.graph);
}); }
this.store = new Store(quads); return quad;
this.id = id; });
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. * @param quads - Quads to add to the metadata.
*/ */
public addQuads(quads: Quad[]): void { public addQuads(quads: Quad[]): this {
this.store.addQuads(quads); this.store.addQuads(quads);
return this;
} }
/** /**
* @param quads - Quads to remove from the metadata. * @param quads - Quads to remove from the metadata.
*/ */
public removeQuads(quads: Quad[]): void { public removeQuads(quads: Quad[]): this {
this.store.removeQuads(quads); this.store.removeQuads(quads);
return this;
} }
/** /**
@ -78,8 +136,9 @@ export class RepresentationMetadata {
* @param predicate - Predicate linking identifier to value. * @param predicate - Predicate linking identifier to value.
* @param object - Value to add. * @param object - Value to add.
*/ */
public add(predicate: NamedNode, object: NamedNode | Literal | string): void { public add(predicate: NamedNode | string, object: NamedNode | Literal | string): this {
this.store.addQuad(this.id, predicate, typeof object === 'string' ? literal(object) : object); 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 predicate - Predicate linking identifier to value.
* @param object - Value to remove. * @param object - Value to remove.
*/ */
public remove(predicate: NamedNode, object: NamedNode | Literal | string): void { public remove(predicate: NamedNode | string, object: NamedNode | Literal | string): this {
this.store.removeQuad(this.id, predicate, typeof object === 'string' ? literal(object) : object); this.store.removeQuad(this.id, getPredicateTerm(predicate), getObjectTerm(object));
return this;
} }
/** /**
* Removes all values linked through the given predicate. * Removes all values linked through the given predicate.
* @param predicate - Predicate to remove. * @param predicate - Predicate to remove.
*/ */
public removeAll(predicate: NamedNode): void { public removeAll(predicate: NamedNode | string): this {
this.removeQuads(this.store.getQuads(this.id, predicate, null, null)); 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 * @returns The corresponding value. Undefined if there is no match
*/ */
public get(predicate: NamedNode): Term | undefined { public get(predicate: NamedNode | string): Term | undefined {
const quads = this.store.getQuads(this.id, predicate, null, null); const terms = this.getAll(predicate);
if (quads.length === 0) { if (terms.length === 0) {
return; return;
} }
if (quads.length > 1) { if (terms.length > 1) {
throw new Error(`Multiple results for ${predicate.value}`); 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. * 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 predicate - Predicate linking to the value.
* @param object - Value to set. * @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.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 { UnsupportedMediaTypeHttpError } from '../util/errors/UnsupportedMediaTypeHttpError';
import { InteractionController } from '../util/InteractionController'; import { InteractionController } from '../util/InteractionController';
import { MetadataController } from '../util/MetadataController'; 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 { ensureTrailingSlash } from '../util/Util';
import { ExtensionBasedMapper } from './ExtensionBasedMapper'; import { ExtensionBasedMapper } from './ExtensionBasedMapper';
import { ResourceStore } from './ResourceStore'; 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. // 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 path = this.resourceMapper.getRelativePath(container);
const slug = representation.metadata.get(SLUG)?.value; const slug = representation.metadata.get(HTTP_SLUG)?.value;
const type = representation.metadata.get(TYPE)?.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. // 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); const newIdentifier = this.interactionController.generateIdentifier(isContainer, slug);
let metadata; let metadata;
// eslint-disable-next-line no-param-reassign // eslint-disable-next-line no-param-reassign
@ -154,14 +162,14 @@ export class FileResourceStore implements ResourceStore {
// eslint-disable-next-line no-param-reassign // eslint-disable-next-line no-param-reassign
representation.metadata.identifier = DataFactory.namedNode(identifier.path); representation.metadata.identifier = DataFactory.namedNode(identifier.path);
const raw = representation.metadata.quads(); const raw = representation.metadata.quads();
const type = representation.metadata.get(TYPE)?.value; const types = representation.metadata.getAll(RDF_TYPE);
let metadata: Readable | undefined; let metadata: Readable | undefined;
if (raw.length > 0) { 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. // 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); const newIdentifier = this.interactionController.generateIdentifier(isContainer, documentName);
return isContainer ? return isContainer ?
await this.setDirectoryRepresentation(containerPath, newIdentifier, metadata) : await this.setDirectoryRepresentation(containerPath, newIdentifier, metadata) :
@ -222,10 +230,10 @@ export class FileResourceStore implements ResourceStore {
} catch (_) { } catch (_) {
// Metadata file doesn't exist so lets keep `rawMetaData` an empty array. // Metadata file doesn't exist so lets keep `rawMetaData` an empty array.
} }
const metadata = new RepresentationMetadata(this.resourceMapper.mapFilePathToUrl(path), rawMetadata); const metadata = new RepresentationMetadata(this.resourceMapper.mapFilePathToUrl(path)).addQuads(rawMetadata)
metadata.set(LAST_CHANGED, stats.mtime.toISOString()); .set(HTTP_LAST_CHANGED, DataFactory.literal(stats.mtime.toISOString(), XSD_DATE_TIME))
metadata.set(BYTE_SIZE, DataFactory.literal(stats.size)); .set(HTTP_BYTE_SIZE, DataFactory.literal(stats.size, XSD_INTEGER));
metadata.set(CONTENT_TYPE, contentType); metadata.contentType = contentType;
return { metadata, data: readStream, binary: true }; 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. // Metadata file doesn't exist so lets keep `rawMetaData` an empty array.
} }
const metadata = new RepresentationMetadata(containerURI, rawMetadata); const metadata = new RepresentationMetadata(containerURI).addQuads(rawMetadata)
metadata.set(LAST_CHANGED, stats.mtime.toISOString()); .set(HTTP_LAST_CHANGED, DataFactory.literal(stats.mtime.toISOString(), XSD_DATE_TIME))
metadata.set(CONTENT_TYPE, INTERNAL_QUADS); .set(MA_CONTENT_TYPE, INTERNAL_QUADS);
return { return {
binary: false, binary: false,

View File

@ -6,7 +6,7 @@ import { RepresentationMetadata } from '../ldp/representation/RepresentationMeta
import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier'; import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
import { TEXT_TURTLE } from '../util/ContentTypes'; import { TEXT_TURTLE } from '../util/ContentTypes';
import { NotFoundHttpError } from '../util/errors/NotFoundHttpError'; 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 { ensureTrailingSlash } from '../util/Util';
import { ResourceStore } from './ResourceStore'; import { ResourceStore } from './ResourceStore';
@ -26,8 +26,7 @@ export class InMemoryResourceStore implements ResourceStore {
public constructor(base: string) { public constructor(base: string) {
this.base = ensureTrailingSlash(base); this.base = ensureTrailingSlash(base);
const metadata = new RepresentationMetadata(); const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: TEXT_TURTLE });
metadata.add(CONTENT_TYPE, TEXT_TURTLE);
this.store = { this.store = {
// Default root entry (what you get when the identifier is equal to the base) // 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 { Representation } from '../ldp/representation/Representation';
import { RepresentationPreferences } from '../ldp/representation/RepresentationPreferences'; import { RepresentationPreferences } from '../ldp/representation/RepresentationPreferences';
import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier'; import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
import { CONTENT_TYPE } from '../util/MetadataTypes';
import { matchingMediaType } from '../util/Util'; import { matchingMediaType } from '../util/Util';
import { Conditions } from './Conditions'; import { Conditions } from './Conditions';
import { RepresentationConverter } from './conversion/RepresentationConverter'; import { RepresentationConverter } from './conversion/RepresentationConverter';
@ -38,12 +37,12 @@ export class RepresentationConvertingStore<T extends ResourceStore = ResourceSto
if (!preferences.type) { if (!preferences.type) {
return true; return true;
} }
const contentType = representation.metadata.get(CONTENT_TYPE); const { contentType } = representation.metadata;
return Boolean( return Boolean(
contentType && contentType &&
preferences.type.some((type): boolean => preferences.type.some((type): boolean =>
type.weight > 0 && 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 { Representation } from '../../ldp/representation/Representation';
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata'; import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
import { RepresentationPreferences } from '../../ldp/representation/RepresentationPreferences'; 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 { matchingMediaType } from '../../util/Util';
import { RepresentationConverterArgs } from './RepresentationConverter'; import { RepresentationConverterArgs } from './RepresentationConverter';
import { TypedRepresentationConverter } from './TypedRepresentationConverter'; import { TypedRepresentationConverter } from './TypedRepresentationConverter';
@ -53,8 +53,7 @@ export class ChainedConverter extends TypedRepresentationConverter {
const idx = this.converters.length - 1; const idx = this.converters.length - 1;
const lastChain = await this.getMatchingType(this.converters[idx - 1], this.converters[idx]); const lastChain = await this.getMatchingType(this.converters[idx - 1], this.converters[idx]);
const oldMeta = input.representation.metadata; const oldMeta = input.representation.metadata;
const metadata = new RepresentationMetadata(oldMeta.identifier, oldMeta.quads()); const metadata = new RepresentationMetadata(oldMeta, { [MA_CONTENT_TYPE]: lastChain });
metadata.set(CONTENT_TYPE, lastChain);
const representation: Representation = { ...input.representation, metadata }; const representation: Representation = { ...input.representation, metadata };
await this.last.canHandle({ ...input, representation }); await this.last.canHandle({ ...input, representation });
} }

View File

@ -1,7 +1,6 @@
import { RepresentationPreference } from '../../ldp/representation/RepresentationPreference'; import { RepresentationPreference } from '../../ldp/representation/RepresentationPreference';
import { RepresentationPreferences } from '../../ldp/representation/RepresentationPreferences'; import { RepresentationPreferences } from '../../ldp/representation/RepresentationPreferences';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError'; import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import { CONTENT_TYPE } from '../../util/MetadataTypes';
import { matchingMediaType } from '../../util/Util'; import { matchingMediaType } from '../../util/Util';
import { RepresentationConverterArgs } from './RepresentationConverter'; import { RepresentationConverterArgs } from './RepresentationConverter';
@ -35,11 +34,11 @@ RepresentationPreference[] => {
*/ */
export const checkRequest = (request: RepresentationConverterArgs, supportedIn: string[], supportedOut: string[]): export const checkRequest = (request: RepresentationConverterArgs, supportedIn: string[], supportedOut: string[]):
void => { void => {
const inType = request.representation.metadata.get(CONTENT_TYPE); const inType = request.representation.metadata.contentType;
if (!inType) { if (!inType) {
throw new UnsupportedHttpError('Input type required for conversion.'); 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}.`); throw new UnsupportedHttpError(`Can only convert from ${supportedIn} to ${supportedOut}.`);
} }
if (matchingTypes(request.preferences, supportedOut).length <= 0) { 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 { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
import { RepresentationPreferences } from '../../ldp/representation/RepresentationPreferences'; import { RepresentationPreferences } from '../../ldp/representation/RepresentationPreferences';
import { INTERNAL_QUADS } from '../../util/ContentTypes'; 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 { checkRequest, matchingTypes } from './ConversionUtil';
import { RepresentationConverterArgs } from './RepresentationConverter'; import { RepresentationConverterArgs } from './RepresentationConverter';
import { TypedRepresentationConverter } from './TypedRepresentationConverter'; import { TypedRepresentationConverter } from './TypedRepresentationConverter';
@ -31,8 +31,7 @@ export class QuadToRdfConverter extends TypedRepresentationConverter {
private async quadsToRdf(quads: Representation, preferences: RepresentationPreferences): Promise<Representation> { private async quadsToRdf(quads: Representation, preferences: RepresentationPreferences): Promise<Representation> {
const contentType = matchingTypes(preferences, await rdfSerializer.getContentTypes())[0].value; const contentType = matchingTypes(preferences, await rdfSerializer.getContentTypes())[0].value;
const metadata = new RepresentationMetadata(quads.metadata.identifier, quads.metadata.quads()); const metadata = new RepresentationMetadata(quads.metadata, { [MA_CONTENT_TYPE]: contentType });
metadata.set(CONTENT_TYPE, contentType);
return { return {
binary: true, binary: true,
data: rdfSerializer.serialize(quads.data, { contentType }) as Readable, 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 { Representation } from '../../ldp/representation/Representation';
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata'; import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
import { INTERNAL_QUADS, TEXT_TURTLE } from '../../util/ContentTypes'; 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 { checkRequest } from './ConversionUtil';
import { RepresentationConverter, RepresentationConverterArgs } from './RepresentationConverter'; import { RepresentationConverter, RepresentationConverterArgs } from './RepresentationConverter';
@ -19,8 +19,7 @@ export class QuadToTurtleConverter extends RepresentationConverter {
} }
private quadsToTurtle(quads: Representation): Representation { private quadsToTurtle(quads: Representation): Representation {
const metadata = new RepresentationMetadata(quads.metadata.identifier, quads.metadata.quads()); const metadata = new RepresentationMetadata(quads.metadata, { [MA_CONTENT_TYPE]: TEXT_TURTLE });
metadata.set(CONTENT_TYPE, TEXT_TURTLE);
return { return {
binary: true, binary: true,
data: quads.data.pipe(new StreamWriter({ format: TEXT_TURTLE })), 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 { Representation } from '../../ldp/representation/Representation';
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata'; import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
import { INTERNAL_QUADS } from '../../util/ContentTypes'; 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 { pipeStreamsAndErrors } from '../../util/Util';
import { checkRequest } from './ConversionUtil'; import { checkRequest } from './ConversionUtil';
import { RepresentationConverterArgs } from './RepresentationConverter'; import { RepresentationConverterArgs } from './RepresentationConverter';
@ -30,10 +30,9 @@ export class RdfToQuadConverter extends TypedRepresentationConverter {
} }
private rdfToQuads(representation: Representation, baseIRI: string): Representation { private rdfToQuads(representation: Representation, baseIRI: string): Representation {
const metadata = new RepresentationMetadata(representation.metadata.identifier, representation.metadata.quads()); const metadata = new RepresentationMetadata(representation.metadata, { [MA_CONTENT_TYPE]: INTERNAL_QUADS });
metadata.set(CONTENT_TYPE, INTERNAL_QUADS);
const rawQuads = rdfParser.parse(representation.data, { const rawQuads = rdfParser.parse(representation.data, {
contentType: representation.metadata.get(CONTENT_TYPE)!.value, contentType: representation.metadata.contentType!,
baseIRI, baseIRI,
}); });

View File

@ -4,7 +4,7 @@ import { Representation } from '../../ldp/representation/Representation';
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata'; import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
import { TEXT_TURTLE, INTERNAL_QUADS } from '../../util/ContentTypes'; import { TEXT_TURTLE, INTERNAL_QUADS } from '../../util/ContentTypes';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError'; import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import { CONTENT_TYPE } from '../../util/MetadataTypes'; import { MA_CONTENT_TYPE } from '../../util/MetadataTypes';
import { checkRequest } from './ConversionUtil'; import { checkRequest } from './ConversionUtil';
import { RepresentationConverter, RepresentationConverterArgs } from './RepresentationConverter'; import { RepresentationConverter, RepresentationConverterArgs } from './RepresentationConverter';
@ -21,8 +21,7 @@ export class TurtleToQuadConverter extends RepresentationConverter {
} }
private turtleToQuads(turtle: Representation, baseIRI: string): Representation { private turtleToQuads(turtle: Representation, baseIRI: string): Representation {
const metadata = new RepresentationMetadata(turtle.metadata.identifier, turtle.metadata.quads()); const metadata = new RepresentationMetadata(turtle.metadata, { [MA_CONTENT_TYPE]: INTERNAL_QUADS });
metadata.set(CONTENT_TYPE, INTERNAL_QUADS);
// Catch parsing errors and emit correct error // Catch parsing errors and emit correct error
// Node 10 requires both writableObjectMode and readableObjectMode // 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 { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier';
import { INTERNAL_QUADS } from '../../util/ContentTypes'; import { INTERNAL_QUADS } from '../../util/ContentTypes';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError'; import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import { CONTENT_TYPE } from '../../util/MetadataTypes'; import { MA_CONTENT_TYPE } from '../../util/MetadataTypes';
import { ResourceLocker } from '../ResourceLocker'; import { ResourceLocker } from '../ResourceLocker';
import { ResourceStore } from '../ResourceStore'; import { ResourceStore } from '../ResourceStore';
import { PatchHandler } from './PatchHandler'; import { PatchHandler } from './PatchHandler';
@ -67,8 +67,7 @@ export class SparqlUpdatePatchHandler extends PatchHandler {
}); });
store.removeQuads(deletes); store.removeQuads(deletes);
store.addQuads(inserts); store.addQuads(inserts);
const metadata = new RepresentationMetadata(input.identifier.path); const metadata = new RepresentationMetadata(input.identifier.path, { [MA_CONTENT_TYPE]: INTERNAL_QUADS });
metadata.set(CONTENT_TYPE, INTERNAL_QUADS);
const representation: Representation = { const representation: Representation = {
binary: false, binary: false,
data: store.match() as Readable, data: store.match() as Readable,

View File

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

View File

@ -1,9 +1,11 @@
import { namedNode } from '@rdfjs/data-model'; export const RDF_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';
import { NamedNode } from 'rdf-js';
export const TYPE: NamedNode = namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'); export const ACL_RESOURCE = 'urn:solid:acl:resource';
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 MA_CONTENT_TYPE = 'http://www.w3.org/ns/ma-ont#format';
export const LAST_CHANGED: NamedNode = namedNode('http://example.com/lastChanged'); export const HTTP_SLUG = 'urn:solid:http:slug';
export const BYTE_SIZE: NamedNode = namedNode('http://example.com/byteSize'); export const HTTP_LAST_CHANGED = 'urn:solid:http:lastChanged';
export const ACL_RESOURCE: NamedNode = namedNode('http://example.com/acl'); 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';

View File

@ -4,7 +4,7 @@ import { RepresentationMetadata } from '../../src/ldp/representation/Representat
import { ChainedConverter } from '../../src/storage/conversion/ChainedConverter'; import { ChainedConverter } from '../../src/storage/conversion/ChainedConverter';
import { QuadToRdfConverter } from '../../src/storage/conversion/QuadToRdfConverter'; import { QuadToRdfConverter } from '../../src/storage/conversion/QuadToRdfConverter';
import { RdfToQuadConverter } from '../../src/storage/conversion/RdfToQuadConverter'; import { RdfToQuadConverter } from '../../src/storage/conversion/RdfToQuadConverter';
import { CONTENT_TYPE } from '../../src/util/MetadataTypes'; import { MA_CONTENT_TYPE } from '../../src/util/MetadataTypes';
import { readableToString } from '../../src/util/Util'; import { readableToString } from '../../src/util/Util';
describe('A ChainedConverter', (): void => { describe('A ChainedConverter', (): void => {
@ -15,8 +15,7 @@ describe('A ChainedConverter', (): void => {
const converter = new ChainedConverter(converters); const converter = new ChainedConverter(converters);
it('can convert from JSON-LD to turtle.', async(): Promise<void> => { it('can convert from JSON-LD to turtle.', async(): Promise<void> => {
const metadata = new RepresentationMetadata(); const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: 'application/ld+json' });
metadata.set(CONTENT_TYPE, 'application/ld+json');
const representation: Representation = { const representation: Representation = {
binary: true, binary: true,
data: streamifyArray([ '{"@id": "http://test.com/s", "http://test.com/p": { "@id": "http://test.com/o" }}' ]), data: streamifyArray([ '{"@id": "http://test.com/s", "http://test.com/p": { "@id": "http://test.com/o" }}' ]),
@ -30,12 +29,11 @@ describe('A ChainedConverter', (): void => {
}); });
await expect(readableToString(result.data)).resolves.toEqual('<http://test.com/s> <http://test.com/p> <http://test.com/o>.\n'); await expect(readableToString(result.data)).resolves.toEqual('<http://test.com/s> <http://test.com/p> <http://test.com/o>.\n');
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/turtle'); expect(result.metadata.contentType).toEqual('text/turtle');
}); });
it('can convert from turtle to JSON-LD.', async(): Promise<void> => { it('can convert from turtle to JSON-LD.', async(): Promise<void> => {
const metadata = new RepresentationMetadata(); const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: 'text/turtle' });
metadata.set(CONTENT_TYPE, 'text/turtle');
const representation: Representation = { const representation: Representation = {
binary: true, binary: true,
data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]), data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]),
@ -51,6 +49,6 @@ describe('A ChainedConverter', (): void => {
expect(JSON.parse(await readableToString(result.data))).toEqual( expect(JSON.parse(await readableToString(result.data))).toEqual(
[{ '@id': 'http://test.com/s', 'http://test.com/p': [{ '@id': 'http://test.com/o' }]}], [{ '@id': 'http://test.com/s', 'http://test.com/p': [{ '@id': 'http://test.com/o' }]}],
); );
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('application/ld+json'); expect(result.metadata.contentType).toEqual('application/ld+json');
}); });
}); });

View File

@ -7,7 +7,6 @@ import { BasicTargetExtractor } from '../../src/ldp/http/BasicTargetExtractor';
import { RawBodyParser } from '../../src/ldp/http/RawBodyParser'; import { RawBodyParser } from '../../src/ldp/http/RawBodyParser';
import { RepresentationMetadata } from '../../src/ldp/representation/RepresentationMetadata'; import { RepresentationMetadata } from '../../src/ldp/representation/RepresentationMetadata';
import { HttpRequest } from '../../src/server/HttpRequest'; import { HttpRequest } from '../../src/server/HttpRequest';
import { CONTENT_TYPE } from '../../src/util/MetadataTypes';
describe('A BasicRequestParser with simple input parsers', (): void => { describe('A BasicRequestParser with simple input parsers', (): void => {
const targetExtractor = new BasicTargetExtractor(); const targetExtractor = new BasicTargetExtractor();
@ -41,7 +40,7 @@ describe('A BasicRequestParser with simple input parsers', (): void => {
metadata: expect.any(RepresentationMetadata), metadata: expect.any(RepresentationMetadata),
}, },
}); });
expect(result.body?.metadata.get(CONTENT_TYPE)?.value).toEqual('text/turtle'); expect(result.body?.metadata.contentType).toEqual('text/turtle');
await expect(arrayifyStream(result.body!.data)).resolves.toEqual( await expect(arrayifyStream(result.body!.data)).resolves.toEqual(
[ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ], [ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ],

View File

@ -5,7 +5,7 @@ import { BasicResponseWriter } from '../../../../src/ldp/http/BasicResponseWrite
import { ResponseDescription } from '../../../../src/ldp/operations/ResponseDescription'; import { ResponseDescription } from '../../../../src/ldp/operations/ResponseDescription';
import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata'; import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError'; import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes'; import { MA_CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
describe('A BasicResponseWriter', (): void => { describe('A BasicResponseWriter', (): void => {
const writer = new BasicResponseWriter(); const writer = new BasicResponseWriter();
@ -48,8 +48,7 @@ describe('A BasicResponseWriter', (): void => {
}); });
it('responds with a content-type if the metadata has it.', async(done): Promise<void> => { it('responds with a content-type if the metadata has it.', async(done): Promise<void> => {
const metadata = new RepresentationMetadata(); const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: 'text/turtle' });
metadata.add(CONTENT_TYPE, 'text/turtle');
const body = { const body = {
binary: true, binary: true,
data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]), data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]),

View File

@ -5,7 +5,7 @@ import { RepresentationMetadata } from '../../../../src/ldp/representation/Repre
import { HttpRequest } from '../../../../src/server/HttpRequest'; import { HttpRequest } from '../../../../src/server/HttpRequest';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError'; import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
import 'jest-rdf'; import 'jest-rdf';
import { CONTENT_TYPE, SLUG, TYPE } from '../../../../src/util/MetadataTypes'; import { HTTP_SLUG, RDF_TYPE } from '../../../../src/util/MetadataTypes';
describe('A RawBodyparser', (): void => { describe('A RawBodyparser', (): void => {
const bodyParser = new RawBodyParser(); const bodyParser = new RawBodyParser();
@ -43,7 +43,7 @@ describe('A RawBodyparser', (): void => {
data: input, data: input,
metadata: expect.any(RepresentationMetadata), metadata: expect.any(RepresentationMetadata),
}); });
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/turtle'); expect(result.metadata.contentType).toEqual('text/turtle');
await expect(arrayifyStream(result.data)).resolves.toEqual( await expect(arrayifyStream(result.data)).resolves.toEqual(
[ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ], [ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ],
); );
@ -53,8 +53,8 @@ describe('A RawBodyparser', (): void => {
const input = {} as HttpRequest; const input = {} as HttpRequest;
input.headers = { 'transfer-encoding': 'chunked', 'content-type': 'text/turtle', slug: 'slugText' }; input.headers = { 'transfer-encoding': 'chunked', 'content-type': 'text/turtle', slug: 'slugText' };
const result = (await bodyParser.handle(input))!; const result = (await bodyParser.handle(input))!;
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/turtle'); expect(result.metadata.contentType).toEqual('text/turtle');
expect(result.metadata.get(SLUG)?.value).toEqual('slugText'); expect(result.metadata.get(HTTP_SLUG)?.value).toEqual('slugText');
}); });
it('errors if there are multiple slugs.', async(): Promise<void> => { it('errors if there are multiple slugs.', async(): Promise<void> => {
@ -71,8 +71,8 @@ describe('A RawBodyparser', (): void => {
'content-type': 'text/turtle', 'content-type': 'text/turtle',
link: '<http://www.w3.org/ns/ldp#Container>; rel="type"' }; link: '<http://www.w3.org/ns/ldp#Container>; rel="type"' };
const result = (await bodyParser.handle(input))!; const result = (await bodyParser.handle(input))!;
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/turtle'); expect(result.metadata.contentType).toEqual('text/turtle');
expect(result.metadata.get(TYPE)?.value).toEqual('http://www.w3.org/ns/ldp#Container'); expect(result.metadata.get(RDF_TYPE)?.value).toEqual('http://www.w3.org/ns/ldp#Container');
}); });
it('ignores unknown link headers.', async(): Promise<void> => { it('ignores unknown link headers.', async(): Promise<void> => {
@ -82,6 +82,6 @@ describe('A RawBodyparser', (): void => {
link: [ '<unrelatedLink>', 'badLink' ]}; link: [ '<unrelatedLink>', 'badLink' ]};
const result = (await bodyParser.handle(input))!; const result = (await bodyParser.handle(input))!;
expect(result.metadata.quads()).toHaveLength(1); expect(result.metadata.quads()).toHaveLength(1);
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/turtle'); expect(result.metadata.contentType).toEqual('text/turtle');
}); });
}); });

View File

@ -6,7 +6,6 @@ import { SparqlUpdateBodyParser } from '../../../../src/ldp/http/SparqlUpdateBod
import { HttpRequest } from '../../../../src/server/HttpRequest'; import { HttpRequest } from '../../../../src/server/HttpRequest';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError'; import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
import { UnsupportedMediaTypeHttpError } from '../../../../src/util/errors/UnsupportedMediaTypeHttpError'; import { UnsupportedMediaTypeHttpError } from '../../../../src/util/errors/UnsupportedMediaTypeHttpError';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
describe('A SparqlUpdateBodyParser', (): void => { describe('A SparqlUpdateBodyParser', (): void => {
const bodyParser = new SparqlUpdateBodyParser(); const bodyParser = new SparqlUpdateBodyParser();
@ -35,7 +34,7 @@ describe('A SparqlUpdateBodyParser', (): void => {
namedNode('http://test.com/o'), namedNode('http://test.com/o'),
) ]); ) ]);
expect(result.binary).toBe(true); expect(result.binary).toBe(true);
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('application/sparql-update'); expect(result.metadata.contentType).toEqual('application/sparql-update');
// Workaround for Node 10 not exposing objectMode // Workaround for Node 10 not exposing objectMode
expect((await arrayifyStream(result.data)).join('')).toEqual( expect((await arrayifyStream(result.data)).join('')).toEqual(

View File

@ -1,6 +1,7 @@
import { literal, namedNode, quad } from '@rdfjs/data-model'; import { literal, namedNode, quad } from '@rdfjs/data-model';
import { Literal, NamedNode, Quad } from 'rdf-js'; import type { Literal, NamedNode, Quad } from 'rdf-js';
import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata'; import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata';
import { MA_CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
describe('A RepresentationMetadata', (): void => { describe('A RepresentationMetadata', (): void => {
let metadata: RepresentationMetadata; let metadata: RepresentationMetadata;
@ -30,15 +31,47 @@ describe('A RepresentationMetadata', (): void => {
expect(metadata.identifier).toEqualRdfTerm(namedNode('identifier')); expect(metadata.identifier).toEqualRdfTerm(namedNode('identifier'));
}); });
it('stores input quads.', async(): Promise<void> => { it('copies an other metadata object.', async(): Promise<void> => {
metadata = new RepresentationMetadata(identifier, inputQuads); const other = new RepresentationMetadata('otherId', { 'test:pred': 'objVal' });
expect(metadata.quads()).toBeRdfIsomorphic(inputQuads); metadata = new RepresentationMetadata(other);
expect(metadata.identifier).toEqualRdfTerm(namedNode('otherId'));
expect(metadata.quads()).toBeRdfIsomorphic([
quad(namedNode('otherId'), namedNode('test:pred'), literal('objVal')) ]);
});
it('takes overrides for specific predicates.', async(): Promise<void> => {
metadata = new RepresentationMetadata({ predVal: 'objVal' });
expect(metadata.get('predVal')).toEqualRdfTerm(literal('objVal'));
metadata = new RepresentationMetadata({ predVal: literal('objVal') });
expect(metadata.get('predVal')).toEqualRdfTerm(literal('objVal'));
metadata = new RepresentationMetadata({ predVal: [ 'objVal1', literal('objVal2') ], predVal2: 'objVal3' });
expect(metadata.getAll('predVal')).toEqualRdfTermArray([ literal('objVal1'), literal('objVal2') ]);
expect(metadata.get('predVal2')).toEqualRdfTerm(literal('objVal3'));
});
it('can combine overrides with an identifier.', async(): Promise<void> => {
metadata = new RepresentationMetadata(identifier, { predVal: 'objVal' });
expect(metadata.quads()).toBeRdfIsomorphic([
quad(identifier, namedNode('predVal'), literal('objVal')) ]);
});
it('can combine overrides with other metadata.', async(): Promise<void> => {
const other = new RepresentationMetadata('otherId', { 'test:pred': 'objVal' });
metadata = new RepresentationMetadata(other, { 'test:pred': 'objVal2' });
expect(metadata.quads()).toBeRdfIsomorphic([
quad(namedNode('otherId'), namedNode('test:pred'), literal('objVal2')) ]);
}); });
}); });
describe('instantiated', (): void => { describe('instantiated', (): void => {
beforeEach(async(): Promise<void> => { beforeEach(async(): Promise<void> => {
metadata = new RepresentationMetadata(identifier, inputQuads); metadata = new RepresentationMetadata(identifier).addQuads(inputQuads);
});
it('can get all quads.', async(): Promise<void> => {
expect(metadata.quads()).toBeRdfIsomorphic(inputQuads);
}); });
it('can change the stored identifier.', async(): Promise<void> => { it('can change the stored identifier.', async(): Promise<void> => {
@ -57,6 +90,28 @@ describe('A RepresentationMetadata', (): void => {
expect(metadata.quads()).toBeRdfIsomorphic(newQuads); expect(metadata.quads()).toBeRdfIsomorphic(newQuads);
}); });
it('can copy metadata.', async(): Promise<void> => {
const other = new RepresentationMetadata(identifier, { 'test:pred': 'objVal' });
metadata.setMetadata(other);
expect(metadata.identifier).toEqual(other.identifier);
expect(metadata.quads()).toBeRdfIsomorphic(inputQuads.concat([
quad(identifier, namedNode('test:pred'), literal('objVal')) ]));
});
it('updates its identifier when copying metadata.', async(): Promise<void> => {
const other = new RepresentationMetadata('otherId', { 'test:pred': 'objVal' });
metadata.setMetadata(other);
// `setMetadata` should have the same result as the following
const expectedMetadata = new RepresentationMetadata(identifier).addQuads(inputQuads);
expectedMetadata.identifier = namedNode('otherId');
expectedMetadata.add('test:pred', 'objVal');
expect(metadata.identifier).toEqual(other.identifier);
expect(metadata.quads()).toBeRdfIsomorphic(expectedMetadata.quads());
});
it('can add quads.', async(): Promise<void> => { it('can add quads.', async(): Promise<void> => {
const newQuads: Quad[] = [ const newQuads: Quad[] = [
quad(namedNode('random'), namedNode('new'), namedNode('triple')), quad(namedNode('random'), namedNode('new'), namedNode('triple')),
@ -100,6 +155,10 @@ describe('A RepresentationMetadata', (): void => {
expect(metadata.quads()).toBeRdfIsomorphic(updatedNodes); expect(metadata.quads()).toBeRdfIsomorphic(updatedNodes);
}); });
it('can get all values for a predicate.', async(): Promise<void> => {
expect(metadata.getAll(namedNode('has'))).toEqualRdfTermArray([ literal('data'), literal('moreData') ]);
});
it('can get the single value for a predicate.', async(): Promise<void> => { it('can get the single value for a predicate.', async(): Promise<void> => {
expect(metadata.get(namedNode('hasOne'))).toEqualRdfTerm(literal('otherData')); expect(metadata.get(namedNode('hasOne'))).toEqualRdfTerm(literal('otherData'));
}); });
@ -110,11 +169,27 @@ describe('A RepresentationMetadata', (): void => {
it('errors if there are multiple values when getting a value.', async(): Promise<void> => { it('errors if there are multiple values when getting a value.', async(): Promise<void> => {
expect((): any => metadata.get(namedNode('has'))).toThrow(Error); expect((): any => metadata.get(namedNode('has'))).toThrow(Error);
expect((): any => metadata.get('has')).toThrow(Error);
}); });
it('can set the value of predicate.', async(): Promise<void> => { it('can set the value of a predicate.', async(): Promise<void> => {
metadata.set(namedNode('has'), literal('singleValue')); metadata.set(namedNode('has'), literal('singleValue'));
expect(metadata.get(namedNode('has'))).toEqualRdfTerm(literal('singleValue')); expect(metadata.get(namedNode('has'))).toEqualRdfTerm(literal('singleValue'));
}); });
it('has a shorthand for content-type.', async(): Promise<void> => {
expect(metadata.contentType).toBeUndefined();
metadata.contentType = 'a/b';
expect(metadata.get(MA_CONTENT_TYPE)).toEqualRdfTerm(literal('a/b'));
expect(metadata.contentType).toEqual('a/b');
metadata.contentType = undefined;
expect(metadata.contentType).toBeUndefined();
});
it('errors if a shorthand has multiple values.', async(): Promise<void> => {
metadata.add(MA_CONTENT_TYPE, 'a/b');
metadata.add(MA_CONTENT_TYPE, 'c/d');
expect((): any => metadata.contentType).toThrow();
});
}); });
}); });

View File

@ -17,7 +17,7 @@ import { UnsupportedMediaTypeHttpError } from '../../../src/util/errors/Unsuppor
import { InteractionController } from '../../../src/util/InteractionController'; import { InteractionController } from '../../../src/util/InteractionController';
import { LINK_TYPE_LDP_BC, LINK_TYPE_LDPR } from '../../../src/util/LinkTypes'; import { LINK_TYPE_LDP_BC, LINK_TYPE_LDPR } from '../../../src/util/LinkTypes';
import { MetadataController } from '../../../src/util/MetadataController'; import { MetadataController } from '../../../src/util/MetadataController';
import { BYTE_SIZE, CONTENT_TYPE, LAST_CHANGED, SLUG, TYPE } from '../../../src/util/MetadataTypes'; import { HTTP_BYTE_SIZE, HTTP_LAST_CHANGED, HTTP_SLUG, RDF_TYPE } from '../../../src/util/MetadataTypes';
import { LDP, RDF, STAT, TERMS, XML } from '../../../src/util/Prefixes'; import { LDP, RDF, STAT, TERMS, XML } from '../../../src/util/Prefixes';
const { join: joinPath } = posix; const { join: joinPath } = posix;
@ -133,8 +133,8 @@ describe('A FileResourceStore', (): void => {
(fs.createReadStream as jest.Mock).mockImplementationOnce((): any => new Error('Metadata file does not exist.')); (fs.createReadStream as jest.Mock).mockImplementationOnce((): any => new Error('Metadata file does not exist.'));
// Write container (POST) // Write container (POST)
representation.metadata.add(TYPE, LINK_TYPE_LDP_BC); representation.metadata.add(RDF_TYPE, LINK_TYPE_LDP_BC);
representation.metadata.add(SLUG, 'myContainer/'); representation.metadata.add(HTTP_SLUG, 'myContainer/');
const identifier = await store.addResource({ path: base }, representation); const identifier = await store.addResource({ path: base }, representation);
expect(fsPromises.mkdir as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'myContainer/'), { recursive: true }); expect(fsPromises.mkdir as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'myContainer/'), { recursive: true });
expect(identifier.path).toBe(`${base}myContainer/`); expect(identifier.path).toBe(`${base}myContainer/`);
@ -146,8 +146,8 @@ describe('A FileResourceStore', (): void => {
data: expect.any(Readable), data: expect.any(Readable),
metadata: expect.any(RepresentationMetadata), metadata: expect.any(RepresentationMetadata),
}); });
expect(result.metadata.get(LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString()); expect(result.metadata.get(HTTP_LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString());
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS); expect(result.metadata.contentType).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(result.data)).resolves.toBeDefined(); await expect(arrayifyStream(result.data)).resolves.toBeDefined();
}); });
@ -156,8 +156,8 @@ describe('A FileResourceStore', (): void => {
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats); (fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
// Tests // Tests
representation.metadata.add(TYPE, LINK_TYPE_LDP_BC); representation.metadata.add(RDF_TYPE, LINK_TYPE_LDP_BC);
representation.metadata.add(SLUG, 'myContainer/'); representation.metadata.add(HTTP_SLUG, 'myContainer/');
await expect(store.addResource({ path: `${base}foo` }, representation)).rejects.toThrow(MethodNotAllowedHttpError); await expect(store.addResource({ path: `${base}foo` }, representation)).rejects.toThrow(MethodNotAllowedHttpError);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo')); expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo'));
}); });
@ -173,19 +173,19 @@ describe('A FileResourceStore', (): void => {
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats); (fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
// Tests // Tests
representation.metadata.add(TYPE, LINK_TYPE_LDP_BC); representation.metadata.add(RDF_TYPE, LINK_TYPE_LDP_BC);
representation.metadata.add(SLUG, 'myContainer/'); representation.metadata.add(HTTP_SLUG, 'myContainer/');
await expect(store.addResource({ path: `${base}doesnotexist` }, representation)) await expect(store.addResource({ path: `${base}doesnotexist` }, representation))
.rejects.toThrow(MethodNotAllowedHttpError); .rejects.toThrow(MethodNotAllowedHttpError);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'doesnotexist')); expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'doesnotexist'));
representation.metadata.set(TYPE, LINK_TYPE_LDPR); representation.metadata.set(RDF_TYPE, LINK_TYPE_LDPR);
representation.metadata.set(SLUG, 'file.txt'); representation.metadata.set(HTTP_SLUG, 'file.txt');
await expect(store.addResource({ path: `${base}doesnotexist` }, representation)) await expect(store.addResource({ path: `${base}doesnotexist` }, representation))
.rejects.toThrow(MethodNotAllowedHttpError); .rejects.toThrow(MethodNotAllowedHttpError);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'doesnotexist')); expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'doesnotexist'));
representation.metadata.removeAll(TYPE); representation.metadata.removeAll(RDF_TYPE);
await expect(store.addResource({ path: `${base}existingresource` }, representation)) await expect(store.addResource({ path: `${base}existingresource` }, representation))
.rejects.toThrow(MethodNotAllowedHttpError); .rejects.toThrow(MethodNotAllowedHttpError);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'existingresource')); expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'existingresource'));
@ -217,9 +217,9 @@ describe('A FileResourceStore', (): void => {
data: expect.any(Readable), data: expect.any(Readable),
metadata: expect.any(RepresentationMetadata), metadata: expect.any(RepresentationMetadata),
}); });
expect(result.metadata.get(LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString()); expect(result.metadata.get(HTTP_LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString());
expect(result.metadata.get(BYTE_SIZE)?.value).toEqual(`${stats.size}`); expect(result.metadata.get(HTTP_BYTE_SIZE)?.value).toEqual(`${stats.size}`);
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/plain'); expect(result.metadata.contentType).toEqual('text/plain');
await expect(arrayifyStream(result.data)).resolves.toEqual([ rawData ]); await expect(arrayifyStream(result.data)).resolves.toEqual([ rawData ]);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'file.txt')); expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'file.txt'));
expect(fs.createReadStream as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'file.txt')); expect(fs.createReadStream as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'file.txt'));
@ -254,8 +254,8 @@ describe('A FileResourceStore', (): void => {
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats); (fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
// Tests // Tests
representation.metadata.add(TYPE, LINK_TYPE_LDPR); representation.metadata.add(RDF_TYPE, LINK_TYPE_LDPR);
representation.metadata.add(SLUG, 'file.txt'); representation.metadata.add(HTTP_SLUG, 'file.txt');
const identifier = await store.addResource({ path: `${base}doesnotexistyet/` }, representation); const identifier = await store.addResource({ path: `${base}doesnotexistyet/` }, representation);
expect(identifier.path).toBe(`${base}doesnotexistyet/file.txt`); expect(identifier.path).toBe(`${base}doesnotexistyet/file.txt`);
expect(fsPromises.mkdir as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'doesnotexistyet/'), expect(fsPromises.mkdir as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'doesnotexistyet/'),
@ -280,7 +280,7 @@ describe('A FileResourceStore', (): void => {
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats); (fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
// Tests // Tests
representation.metadata.add(TYPE, LINK_TYPE_LDPR); representation.metadata.add(RDF_TYPE, LINK_TYPE_LDPR);
representation.data = readableMock; representation.data = readableMock;
await store.addResource({ path: `${base}foo/` }, representation); await store.addResource({ path: `${base}foo/` }, representation);
expect(fsPromises.mkdir as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo/'), { recursive: true }); expect(fsPromises.mkdir as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo/'), { recursive: true });
@ -368,8 +368,8 @@ describe('A FileResourceStore', (): void => {
data: expect.any(Readable), data: expect.any(Readable),
metadata: expect.any(RepresentationMetadata), metadata: expect.any(RepresentationMetadata),
}); });
expect(result.metadata.get(LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString()); expect(result.metadata.get(HTTP_LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString());
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS); expect(result.metadata.contentType).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray(quads); await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray(quads);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo/')); expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo/'));
expect(fsPromises.readdir as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo/')); expect(fsPromises.readdir as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo/'));
@ -387,7 +387,7 @@ describe('A FileResourceStore', (): void => {
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats); (fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
// Tests // Tests
representation.metadata.add(TYPE, LINK_TYPE_LDPR); representation.metadata.add(RDF_TYPE, LINK_TYPE_LDPR);
await store.setRepresentation({ path: `${base}alreadyexists.txt` }, representation); await store.setRepresentation({ path: `${base}alreadyexists.txt` }, representation);
expect(fs.createWriteStream as jest.Mock).toBeCalledTimes(2); expect(fs.createWriteStream as jest.Mock).toBeCalledTimes(2);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'alreadyexists.txt')); expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'alreadyexists.txt'));
@ -403,7 +403,7 @@ describe('A FileResourceStore', (): void => {
await expect(store.setRepresentation({ path: `${base}alreadyexists` }, representation)).rejects await expect(store.setRepresentation({ path: `${base}alreadyexists` }, representation)).rejects
.toThrow(ConflictHttpError); .toThrow(ConflictHttpError);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'alreadyexists')); expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'alreadyexists'));
representation.metadata.add(TYPE, LINK_TYPE_LDP_BC); representation.metadata.add(RDF_TYPE, LINK_TYPE_LDP_BC);
await expect(store.setRepresentation({ path: `${base}alreadyexists/` }, representation)).rejects await expect(store.setRepresentation({ path: `${base}alreadyexists/` }, representation)).rejects
.toThrow(ConflictHttpError); .toThrow(ConflictHttpError);
expect(fsPromises.access as jest.Mock).toBeCalledTimes(1); expect(fsPromises.access as jest.Mock).toBeCalledTimes(1);
@ -417,7 +417,7 @@ describe('A FileResourceStore', (): void => {
(fsPromises.mkdir as jest.Mock).mockReturnValueOnce(true); (fsPromises.mkdir as jest.Mock).mockReturnValueOnce(true);
// Tests // Tests
representation.metadata.add(TYPE, LINK_TYPE_LDP_BC); representation.metadata.add(RDF_TYPE, LINK_TYPE_LDP_BC);
await store.setRepresentation({ path: `${base}foo/` }, representation); await store.setRepresentation({ path: `${base}foo/` }, representation);
expect(fsPromises.mkdir as jest.Mock).toBeCalledTimes(1); expect(fsPromises.mkdir as jest.Mock).toBeCalledTimes(1);
expect(fsPromises.access as jest.Mock).toBeCalledTimes(1); expect(fsPromises.access as jest.Mock).toBeCalledTimes(1);
@ -435,8 +435,8 @@ describe('A FileResourceStore', (): void => {
(fsPromises.unlink as jest.Mock).mockReturnValueOnce(true); (fsPromises.unlink as jest.Mock).mockReturnValueOnce(true);
// Tests // Tests
representation.metadata.add(TYPE, LINK_TYPE_LDPR); representation.metadata.add(RDF_TYPE, LINK_TYPE_LDPR);
representation.metadata.add(SLUG, 'file.txt'); representation.metadata.add(HTTP_SLUG, 'file.txt');
await expect(store.addResource({ path: base }, representation)).rejects.toThrow(Error); await expect(store.addResource({ path: base }, representation)).rejects.toThrow(Error);
expect(fs.createWriteStream as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'file.txt.metadata')); expect(fs.createWriteStream as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'file.txt.metadata'));
expect(fs.createWriteStream as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'file.txt')); expect(fs.createWriteStream as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'file.txt'));
@ -452,8 +452,8 @@ describe('A FileResourceStore', (): void => {
(fsPromises.rmdir as jest.Mock).mockReturnValueOnce(true); (fsPromises.rmdir as jest.Mock).mockReturnValueOnce(true);
// Tests // Tests
representation.metadata.add(TYPE, LINK_TYPE_LDP_BC); representation.metadata.add(RDF_TYPE, LINK_TYPE_LDP_BC);
representation.metadata.add(SLUG, 'foo/'); representation.metadata.add(HTTP_SLUG, 'foo/');
await expect(store.addResource({ path: base }, representation)).rejects.toThrow(Error); await expect(store.addResource({ path: base }, representation)).rejects.toThrow(Error);
expect(fsPromises.rmdir as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo/')); expect(fsPromises.rmdir as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo/'));
}); });
@ -464,7 +464,7 @@ describe('A FileResourceStore', (): void => {
(fsPromises.mkdir as jest.Mock).mockReturnValueOnce(true); (fsPromises.mkdir as jest.Mock).mockReturnValueOnce(true);
// Tests // Tests
representation.metadata.add(SLUG, 'myContainer/'); representation.metadata.add(HTTP_SLUG, 'myContainer/');
const identifier = await store.addResource({ path: base }, representation); const identifier = await store.addResource({ path: base }, representation);
expect(identifier.path).toBe(`${base}myContainer/`); expect(identifier.path).toBe(`${base}myContainer/`);
expect(fsPromises.mkdir as jest.Mock).toBeCalledTimes(1); expect(fsPromises.mkdir as jest.Mock).toBeCalledTimes(1);
@ -484,9 +484,9 @@ describe('A FileResourceStore', (): void => {
data: expect.any(Readable), data: expect.any(Readable),
metadata: expect.any(RepresentationMetadata), metadata: expect.any(RepresentationMetadata),
}); });
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('application/octet-stream'); expect(result.metadata.contentType).toEqual('application/octet-stream');
expect(result.metadata.get(LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString()); expect(result.metadata.get(HTTP_LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString());
expect(result.metadata.get(BYTE_SIZE)?.value).toEqual(`${stats.size}`); expect(result.metadata.get(HTTP_BYTE_SIZE)?.value).toEqual(`${stats.size}`);
}); });
it('errors when performing a PUT on the root path.', async(): Promise<void> => { it('errors when performing a PUT on the root path.', async(): Promise<void> => {
@ -519,8 +519,8 @@ describe('A FileResourceStore', (): void => {
(fsPromises.mkdir as jest.Mock).mockReturnValue(true); (fsPromises.mkdir as jest.Mock).mockReturnValue(true);
// Tests // Tests
representation.metadata.add(TYPE, LINK_TYPE_LDP_BC); representation.metadata.add(RDF_TYPE, LINK_TYPE_LDP_BC);
representation.metadata.add(SLUG, 'bar'); representation.metadata.add(HTTP_SLUG, 'bar');
const identifier = await store.addResource({ path: `${base}foo` }, representation); const identifier = await store.addResource({ path: `${base}foo` }, representation);
expect(identifier.path).toBe(`${base}foo/bar/`); expect(identifier.path).toBe(`${base}foo/bar/`);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo')); expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo'));
@ -552,8 +552,8 @@ describe('A FileResourceStore', (): void => {
data: expect.any(Readable), data: expect.any(Readable),
metadata: expect.any(RepresentationMetadata), metadata: expect.any(RepresentationMetadata),
}); });
expect(result.metadata.get(LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString()); expect(result.metadata.get(HTTP_LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString());
expect(result.metadata.get(BYTE_SIZE)?.value).toEqual(`${stats.size}`); expect(result.metadata.get(HTTP_BYTE_SIZE)?.value).toEqual(`${stats.size}`);
await expect(arrayifyStream(result.data)).resolves.toEqual([ rawData ]); await expect(arrayifyStream(result.data)).resolves.toEqual([ rawData ]);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, name)); expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, name));
expect(fs.createReadStream as jest.Mock).toBeCalledWith(joinPath(rootFilepath, name)); expect(fs.createReadStream as jest.Mock).toBeCalledWith(joinPath(rootFilepath, name));

View File

@ -2,15 +2,14 @@ import { RepresentationMetadata } from '../../../src/ldp/representation/Represen
import { RepresentationConverter } from '../../../src/storage/conversion/RepresentationConverter'; import { RepresentationConverter } from '../../../src/storage/conversion/RepresentationConverter';
import { RepresentationConvertingStore } from '../../../src/storage/RepresentationConvertingStore'; import { RepresentationConvertingStore } from '../../../src/storage/RepresentationConvertingStore';
import { ResourceStore } from '../../../src/storage/ResourceStore'; import { ResourceStore } from '../../../src/storage/ResourceStore';
import { CONTENT_TYPE } from '../../../src/util/MetadataTypes'; import { MA_CONTENT_TYPE } from '../../../src/util/MetadataTypes';
describe('A RepresentationConvertingStore', (): void => { describe('A RepresentationConvertingStore', (): void => {
let store: RepresentationConvertingStore; let store: RepresentationConvertingStore;
let source: ResourceStore; let source: ResourceStore;
let handleSafeFn: jest.Mock<Promise<void>, []>; let handleSafeFn: jest.Mock<Promise<void>, []>;
let converter: RepresentationConverter; let converter: RepresentationConverter;
const metadata = new RepresentationMetadata(); const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: 'text/turtle' });
metadata.add(CONTENT_TYPE, 'text/turtle');
beforeEach(async(): Promise<void> => { beforeEach(async(): Promise<void> => {
source = { source = {
@ -31,7 +30,7 @@ describe('A RepresentationConvertingStore', (): void => {
data: 'data', data: 'data',
metadata: expect.any(RepresentationMetadata), metadata: expect.any(RepresentationMetadata),
}); });
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/turtle'); expect(result.metadata.contentType).toEqual('text/turtle');
expect(source.getRepresentation).toHaveBeenCalledTimes(1); expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(source.getRepresentation).toHaveBeenLastCalledWith( expect(source.getRepresentation).toHaveBeenLastCalledWith(
{ path: 'path' }, { type: [{ value: 'text/*', weight: 0 }, { value: 'text/turtle', weight: 1 }]}, undefined, { path: 'path' }, { type: [{ value: 'text/*', weight: 0 }, { value: 'text/turtle', weight: 1 }]}, undefined,
@ -45,7 +44,7 @@ describe('A RepresentationConvertingStore', (): void => {
data: 'data', data: 'data',
metadata: expect.any(RepresentationMetadata), metadata: expect.any(RepresentationMetadata),
}); });
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/turtle'); expect(result.metadata.contentType).toEqual('text/turtle');
expect(source.getRepresentation).toHaveBeenCalledTimes(1); expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(source.getRepresentation).toHaveBeenLastCalledWith( expect(source.getRepresentation).toHaveBeenLastCalledWith(
{ path: 'path' }, {}, undefined, { path: 'path' }, {}, undefined,

View File

@ -5,7 +5,7 @@ import { ChainedConverter } from '../../../../src/storage/conversion/ChainedConv
import { checkRequest } from '../../../../src/storage/conversion/ConversionUtil'; import { checkRequest } from '../../../../src/storage/conversion/ConversionUtil';
import { RepresentationConverterArgs } from '../../../../src/storage/conversion/RepresentationConverter'; import { RepresentationConverterArgs } from '../../../../src/storage/conversion/RepresentationConverter';
import { TypedRepresentationConverter } from '../../../../src/storage/conversion/TypedRepresentationConverter'; import { TypedRepresentationConverter } from '../../../../src/storage/conversion/TypedRepresentationConverter';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes'; import { MA_CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
class DummyConverter extends TypedRepresentationConverter { class DummyConverter extends TypedRepresentationConverter {
private readonly inTypes: { [contentType: string]: number }; private readonly inTypes: { [contentType: string]: number };
@ -30,9 +30,8 @@ class DummyConverter extends TypedRepresentationConverter {
} }
public async handle(input: RepresentationConverterArgs): Promise<Representation> { public async handle(input: RepresentationConverterArgs): Promise<Representation> {
const oldMeta = input.representation.metadata; const metadata = new RepresentationMetadata(input.representation.metadata,
const metadata = new RepresentationMetadata(oldMeta.identifier, oldMeta.quads()); { [MA_CONTENT_TYPE]: input.preferences.type![0].value });
metadata.set(CONTENT_TYPE, input.preferences.type![0].value);
return { ...input.representation, metadata }; return { ...input.representation, metadata };
} }
} }
@ -52,8 +51,7 @@ describe('A ChainedConverter', (): void => {
]; ];
converter = new ChainedConverter(converters); converter = new ChainedConverter(converters);
const metadata = new RepresentationMetadata(); const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: 'text/turtle' });
metadata.set(CONTENT_TYPE, 'text/turtle');
representation = { metadata } as Representation; representation = { metadata } as Representation;
preferences = { type: [{ value: 'internal/quads', weight: 1 }]}; preferences = { type: [{ value: 'internal/quads', weight: 1 }]};
args = { representation, preferences, identifier: { path: 'path' }}; args = { representation, preferences, identifier: { path: 'path' }};
@ -79,7 +77,7 @@ describe('A ChainedConverter', (): void => {
}); });
it('errors if the start of the chain does not support the representation type.', async(): Promise<void> => { it('errors if the start of the chain does not support the representation type.', async(): Promise<void> => {
representation.metadata.set(CONTENT_TYPE, 'bad/type'); representation.metadata.contentType = 'bad/type';
await expect(converter.canHandle(args)).rejects.toThrow(); await expect(converter.canHandle(args)).rejects.toThrow();
}); });
@ -94,7 +92,7 @@ describe('A ChainedConverter', (): void => {
jest.spyOn(converters[2], 'handle'); jest.spyOn(converters[2], 'handle');
const result = await converter.handle(args); const result = await converter.handle(args);
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('internal/quads'); expect(result.metadata.contentType).toEqual('internal/quads');
expect((converters[0] as any).handle).toHaveBeenCalledTimes(1); expect((converters[0] as any).handle).toHaveBeenCalledTimes(1);
expect((converters[1] as any).handle).toHaveBeenCalledTimes(1); expect((converters[1] as any).handle).toHaveBeenCalledTimes(1);
expect((converters[2] as any).handle).toHaveBeenCalledTimes(1); expect((converters[2] as any).handle).toHaveBeenCalledTimes(1);

View File

@ -3,7 +3,6 @@ import { RepresentationMetadata } from '../../../../src/ldp/representation/Repre
import { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences'; import { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences';
import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier'; import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
import { checkRequest, matchingTypes } from '../../../../src/storage/conversion/ConversionUtil'; import { checkRequest, matchingTypes } from '../../../../src/storage/conversion/ConversionUtil';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
describe('A ConversionUtil', (): void => { describe('A ConversionUtil', (): void => {
const identifier: ResourceIdentifier = { path: 'path' }; const identifier: ResourceIdentifier = { path: 'path' };
@ -23,21 +22,21 @@ describe('A ConversionUtil', (): void => {
}); });
it('requires a matching input type.', async(): Promise<void> => { it('requires a matching input type.', async(): Promise<void> => {
metadata.add(CONTENT_TYPE, 'a/x'); metadata.contentType = 'a/x';
const preferences: RepresentationPreferences = { type: [{ value: 'b/x', weight: 1 }]}; const preferences: RepresentationPreferences = { type: [{ value: 'b/x', weight: 1 }]};
expect((): any => checkRequest({ identifier, representation, preferences }, [ 'c/x' ], [ '*/*' ])) expect((): any => checkRequest({ identifier, representation, preferences }, [ 'c/x' ], [ '*/*' ]))
.toThrow('Can only convert from c/x to */*.'); .toThrow('Can only convert from c/x to */*.');
}); });
it('requires a matching output type.', async(): Promise<void> => { it('requires a matching output type.', async(): Promise<void> => {
metadata.add(CONTENT_TYPE, 'a/x'); metadata.contentType = 'a/x';
const preferences: RepresentationPreferences = { type: [{ value: 'b/x', weight: 1 }]}; const preferences: RepresentationPreferences = { type: [{ value: 'b/x', weight: 1 }]};
expect((): any => checkRequest({ identifier, representation, preferences }, [ '*/*' ], [ 'c/x' ])) expect((): any => checkRequest({ identifier, representation, preferences }, [ '*/*' ], [ 'c/x' ]))
.toThrow('Can only convert from */* to c/x.'); .toThrow('Can only convert from */* to c/x.');
}); });
it('succeeds with a valid input and output type.', async(): Promise<void> => { it('succeeds with a valid input and output type.', async(): Promise<void> => {
metadata.add(CONTENT_TYPE, 'a/x'); metadata.contentType = 'a/x';
const preferences: RepresentationPreferences = { type: [{ value: 'b/x', weight: 1 }]}; const preferences: RepresentationPreferences = { type: [{ value: 'b/x', weight: 1 }]};
expect(checkRequest({ identifier, representation, preferences }, [ '*/*' ], [ '*/*' ])) expect(checkRequest({ identifier, representation, preferences }, [ '*/*' ], [ '*/*' ]))
.toBeUndefined(); .toBeUndefined();

View File

@ -8,13 +8,12 @@ import { RepresentationPreferences } from '../../../../src/ldp/representation/Re
import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier'; import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
import { QuadToRdfConverter } from '../../../../src/storage/conversion/QuadToRdfConverter'; import { QuadToRdfConverter } from '../../../../src/storage/conversion/QuadToRdfConverter';
import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes'; import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes'; import { MA_CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
describe('A QuadToRdfConverter', (): void => { describe('A QuadToRdfConverter', (): void => {
const converter = new QuadToRdfConverter(); const converter = new QuadToRdfConverter();
const identifier: ResourceIdentifier = { path: 'path' }; const identifier: ResourceIdentifier = { path: 'path' };
const metadata = new RepresentationMetadata(); const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: INTERNAL_QUADS });
metadata.set(CONTENT_TYPE, INTERNAL_QUADS);
it('supports parsing quads.', async(): Promise<void> => { it('supports parsing quads.', async(): Promise<void> => {
await expect(converter.getInputTypes()).resolves.toEqual({ [INTERNAL_QUADS]: 1 }); await expect(converter.getInputTypes()).resolves.toEqual({ [INTERNAL_QUADS]: 1 });
@ -51,7 +50,7 @@ describe('A QuadToRdfConverter', (): void => {
binary: true, binary: true,
metadata: expect.any(RepresentationMetadata), metadata: expect.any(RepresentationMetadata),
}); });
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/turtle'); expect(result.metadata.contentType).toEqual('text/turtle');
await expect(stringifyStream(result.data)).resolves.toEqual( await expect(stringifyStream(result.data)).resolves.toEqual(
`<http://test.com/s> <http://test.com/p> <http://test.com/o>. `<http://test.com/s> <http://test.com/p> <http://test.com/o>.
`, `,
@ -59,7 +58,7 @@ describe('A QuadToRdfConverter', (): void => {
}); });
it('converts quads to JSON-LD.', async(): Promise<void> => { it('converts quads to JSON-LD.', async(): Promise<void> => {
metadata.set(CONTENT_TYPE, INTERNAL_QUADS); metadata.contentType = INTERNAL_QUADS;
const representation = { const representation = {
data: streamifyArray([ triple( data: streamifyArray([ triple(
namedNode('http://test.com/s'), namedNode('http://test.com/s'),
@ -74,7 +73,7 @@ describe('A QuadToRdfConverter', (): void => {
binary: true, binary: true,
metadata: expect.any(RepresentationMetadata), metadata: expect.any(RepresentationMetadata),
}); });
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('application/ld+json'); expect(result.metadata.contentType).toEqual('application/ld+json');
await expect(stringifyStream(result.data)).resolves.toEqual( await expect(stringifyStream(result.data)).resolves.toEqual(
`[ `[
{ {

View File

@ -7,13 +7,12 @@ import { RepresentationPreferences } from '../../../../src/ldp/representation/Re
import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier'; import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
import { QuadToTurtleConverter } from '../../../../src/storage/conversion/QuadToTurtleConverter'; import { QuadToTurtleConverter } from '../../../../src/storage/conversion/QuadToTurtleConverter';
import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes'; import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes'; import { MA_CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
describe('A QuadToTurtleConverter', (): void => { describe('A QuadToTurtleConverter', (): void => {
const converter = new QuadToTurtleConverter(); const converter = new QuadToTurtleConverter();
const identifier: ResourceIdentifier = { path: 'path' }; const identifier: ResourceIdentifier = { path: 'path' };
const metadata = new RepresentationMetadata(); const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: INTERNAL_QUADS });
metadata.add(CONTENT_TYPE, INTERNAL_QUADS);
it('can handle quad to turtle conversions.', async(): Promise<void> => { it('can handle quad to turtle conversions.', async(): Promise<void> => {
const representation = { metadata } as Representation; const representation = { metadata } as Representation;
@ -36,7 +35,7 @@ describe('A QuadToTurtleConverter', (): void => {
binary: true, binary: true,
metadata: expect.any(RepresentationMetadata), metadata: expect.any(RepresentationMetadata),
}); });
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/turtle'); expect(result.metadata.contentType).toEqual('text/turtle');
await expect(arrayifyStream(result.data)).resolves.toContain( await expect(arrayifyStream(result.data)).resolves.toContain(
'<http://test.com/s> <http://test.com/p> <http://test.com/o>', '<http://test.com/s> <http://test.com/p> <http://test.com/o>',
); );

View File

@ -10,7 +10,7 @@ import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceI
import { RdfToQuadConverter } from '../../../../src/storage/conversion/RdfToQuadConverter'; import { RdfToQuadConverter } from '../../../../src/storage/conversion/RdfToQuadConverter';
import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes'; import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError'; import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes'; import { MA_CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
describe('A RdfToQuadConverter.test.ts', (): void => { describe('A RdfToQuadConverter.test.ts', (): void => {
const converter = new RdfToQuadConverter(); const converter = new RdfToQuadConverter();
@ -25,24 +25,21 @@ describe('A RdfToQuadConverter.test.ts', (): void => {
}); });
it('can handle turtle to quad conversions.', async(): Promise<void> => { it('can handle turtle to quad conversions.', async(): Promise<void> => {
const metadata = new RepresentationMetadata(); const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: 'text/turtle' });
metadata.set(CONTENT_TYPE, 'text/turtle');
const representation = { metadata } as Representation; const representation = { metadata } as Representation;
const preferences: RepresentationPreferences = { type: [{ value: INTERNAL_QUADS, weight: 1 }]}; const preferences: RepresentationPreferences = { type: [{ value: INTERNAL_QUADS, weight: 1 }]};
await expect(converter.canHandle({ identifier, representation, preferences })).resolves.toBeUndefined(); await expect(converter.canHandle({ identifier, representation, preferences })).resolves.toBeUndefined();
}); });
it('can handle JSON-LD to quad conversions.', async(): Promise<void> => { it('can handle JSON-LD to quad conversions.', async(): Promise<void> => {
const metadata = new RepresentationMetadata(); const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: 'application/ld+json' });
metadata.set(CONTENT_TYPE, 'application/ld+json');
const representation = { metadata } as Representation; const representation = { metadata } as Representation;
const preferences: RepresentationPreferences = { type: [{ value: INTERNAL_QUADS, weight: 1 }]}; const preferences: RepresentationPreferences = { type: [{ value: INTERNAL_QUADS, weight: 1 }]};
await expect(converter.canHandle({ identifier, representation, preferences })).resolves.toBeUndefined(); await expect(converter.canHandle({ identifier, representation, preferences })).resolves.toBeUndefined();
}); });
it('converts turtle to quads.', async(): Promise<void> => { it('converts turtle to quads.', async(): Promise<void> => {
const metadata = new RepresentationMetadata(); const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: 'text/turtle' });
metadata.set(CONTENT_TYPE, 'text/turtle');
const representation = { const representation = {
data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]), data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]),
metadata, metadata,
@ -54,7 +51,7 @@ describe('A RdfToQuadConverter.test.ts', (): void => {
data: expect.any(Readable), data: expect.any(Readable),
metadata: expect.any(RepresentationMetadata), metadata: expect.any(RepresentationMetadata),
}); });
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS); expect(result.metadata.contentType).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray([ triple( await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray([ triple(
namedNode('http://test.com/s'), namedNode('http://test.com/s'),
namedNode('http://test.com/p'), namedNode('http://test.com/p'),
@ -63,8 +60,7 @@ describe('A RdfToQuadConverter.test.ts', (): void => {
}); });
it('converts JSON-LD to quads.', async(): Promise<void> => { it('converts JSON-LD to quads.', async(): Promise<void> => {
const metadata = new RepresentationMetadata(); const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: 'application/ld+json' });
metadata.set(CONTENT_TYPE, 'application/ld+json');
const representation = { const representation = {
data: streamifyArray([ '{"@id": "http://test.com/s", "http://test.com/p": { "@id": "http://test.com/o" }}' ]), data: streamifyArray([ '{"@id": "http://test.com/s", "http://test.com/p": { "@id": "http://test.com/o" }}' ]),
metadata, metadata,
@ -76,7 +72,7 @@ describe('A RdfToQuadConverter.test.ts', (): void => {
data: expect.any(Readable), data: expect.any(Readable),
metadata: expect.any(RepresentationMetadata), metadata: expect.any(RepresentationMetadata),
}); });
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS); expect(result.metadata.contentType).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray([ triple( await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray([ triple(
namedNode('http://test.com/s'), namedNode('http://test.com/s'),
namedNode('http://test.com/p'), namedNode('http://test.com/p'),
@ -85,8 +81,7 @@ describe('A RdfToQuadConverter.test.ts', (): void => {
}); });
it('throws an UnsupportedHttpError on invalid triple data.', async(): Promise<void> => { it('throws an UnsupportedHttpError on invalid triple data.', async(): Promise<void> => {
const metadata = new RepresentationMetadata(); const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: 'text/turtle' });
metadata.set(CONTENT_TYPE, 'text/turtle');
const representation = { const representation = {
data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.co' ]), data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.co' ]),
metadata, metadata,
@ -98,7 +93,7 @@ describe('A RdfToQuadConverter.test.ts', (): void => {
data: expect.any(Readable), data: expect.any(Readable),
metadata: expect.any(RepresentationMetadata), metadata: expect.any(RepresentationMetadata),
}); });
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS); expect(result.metadata.contentType).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(result.data)).rejects.toThrow(UnsupportedHttpError); await expect(arrayifyStream(result.data)).rejects.toThrow(UnsupportedHttpError);
}); });
}); });

View File

@ -9,13 +9,12 @@ import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceI
import { TurtleToQuadConverter } from '../../../../src/storage/conversion/TurtleToQuadConverter'; import { TurtleToQuadConverter } from '../../../../src/storage/conversion/TurtleToQuadConverter';
import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes'; import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError'; import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes'; import { MA_CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
describe('A TurtleToQuadConverter', (): void => { describe('A TurtleToQuadConverter', (): void => {
const converter = new TurtleToQuadConverter(); const converter = new TurtleToQuadConverter();
const identifier: ResourceIdentifier = { path: 'path' }; const identifier: ResourceIdentifier = { path: 'path' };
const metadata = new RepresentationMetadata(); const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: 'text/turtle' });
metadata.add(CONTENT_TYPE, 'text/turtle');
it('can handle turtle to quad conversions.', async(): Promise<void> => { it('can handle turtle to quad conversions.', async(): Promise<void> => {
const representation = { metadata } as Representation; const representation = { metadata } as Representation;
@ -35,7 +34,7 @@ describe('A TurtleToQuadConverter', (): void => {
data: expect.any(Readable), data: expect.any(Readable),
metadata: expect.any(RepresentationMetadata), metadata: expect.any(RepresentationMetadata),
}); });
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS); expect(result.metadata.contentType).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray([ triple( await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray([ triple(
namedNode('http://test.com/s'), namedNode('http://test.com/s'),
namedNode('http://test.com/p'), namedNode('http://test.com/p'),
@ -55,7 +54,7 @@ describe('A TurtleToQuadConverter', (): void => {
data: expect.any(Readable), data: expect.any(Readable),
metadata: expect.any(RepresentationMetadata), metadata: expect.any(RepresentationMetadata),
}); });
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS); expect(result.metadata.contentType).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(result.data)).rejects.toThrow(UnsupportedHttpError); await expect(arrayifyStream(result.data)).rejects.toThrow(UnsupportedHttpError);
}); });
}); });

View File

@ -11,7 +11,6 @@ import { ResourceLocker } from '../../../../src/storage/ResourceLocker';
import { ResourceStore } from '../../../../src/storage/ResourceStore'; import { ResourceStore } from '../../../../src/storage/ResourceStore';
import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes'; import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError'; import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
describe('A SparqlUpdatePatchHandler', (): void => { describe('A SparqlUpdatePatchHandler', (): void => {
let handler: SparqlUpdatePatchHandler; let handler: SparqlUpdatePatchHandler;
@ -77,7 +76,7 @@ describe('A SparqlUpdatePatchHandler', (): void => {
binary: false, binary: false,
metadata: expect.any(RepresentationMetadata), metadata: expect.any(RepresentationMetadata),
})); }));
expect(setParams[1].metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS); expect(setParams[1].metadata.contentType).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(setParams[1].data)).resolves.toBeRdfIsomorphic(quads); await expect(arrayifyStream(setParams[1].data)).resolves.toBeRdfIsomorphic(quads);
}; };

View File

@ -5,10 +5,11 @@ import { join } from 'path';
import * as url from 'url'; import * as url from 'url';
import { createResponse, MockResponse } from 'node-mocks-http'; import { createResponse, MockResponse } from 'node-mocks-http';
import streamifyArray from 'streamify-array'; import streamifyArray from 'streamify-array';
import { ResourceStore } from '../../index'; import { RepresentationMetadata, ResourceStore } from '../../index';
import { PermissionSet } from '../../src/ldp/permissions/PermissionSet'; import { PermissionSet } from '../../src/ldp/permissions/PermissionSet';
import { HttpHandler } from '../../src/server/HttpHandler'; import { HttpHandler } from '../../src/server/HttpHandler';
import { HttpRequest } from '../../src/server/HttpRequest'; import { HttpRequest } from '../../src/server/HttpRequest';
import { MA_CONTENT_TYPE } from '../../src/util/MetadataTypes';
import { call } from './Util'; import { call } from './Util';
export class AclTestHelper { export class AclTestHelper {
@ -49,11 +50,7 @@ export class AclTestHelper {
const representation = { const representation = {
binary: true, binary: true,
data: streamifyArray(acl), data: streamifyArray(acl),
metadata: { metadata: new RepresentationMetadata({ [MA_CONTENT_TYPE]: 'text/turtle' }),
raw: [],
profiles: [],
contentType: 'text/turtle',
},
}; };
return this.store.setRepresentation( return this.store.setRepresentation(

View File

@ -6,4 +6,4 @@
"syntaxKind": "modifier" "syntaxKind": "modifier"
} }
] ]
} }