feat: Update RepresentationMetadata to store triples

This commit is contained in:
Joachim Van Herwegen
2020-09-07 09:28:40 +02:00
parent 1dd140ab61
commit 76319ba360
37 changed files with 575 additions and 247 deletions

37
package-lock.json generated
View File

@@ -1108,17 +1108,23 @@
"dev": true
},
"@microsoft/tsdoc-config": {
"version": "0.13.5",
"resolved": "https://registry.npmjs.org/@microsoft/tsdoc-config/-/tsdoc-config-0.13.5.tgz",
"integrity": "sha512-KlnIdTRnPSsU9Coz9wzDAkT8JCLopP3ec1sgsgo7trwE6QLMKRpM4hZi2uzVX897SW49Q4f439auGBcQLnZQfA==",
"version": "0.13.6",
"resolved": "https://registry.npmjs.org/@microsoft/tsdoc-config/-/tsdoc-config-0.13.6.tgz",
"integrity": "sha512-VJjV35PnrNISoX2WMemZjnCIdOUPTRpCz6pu8inISotLd3SgoDSJygGaE7+lOYdCtDl+4c8PWJdZivxxXgOnLw==",
"dev": true,
"requires": {
"@microsoft/tsdoc": "0.12.20",
"@microsoft/tsdoc": "0.12.21",
"ajv": "~6.12.3",
"jju": "~1.4.0",
"resolve": "~1.12.0"
},
"dependencies": {
"@microsoft/tsdoc": {
"version": "0.12.21",
"resolved": "https://registry.npmjs.org/@microsoft/tsdoc/-/tsdoc-0.12.21.tgz",
"integrity": "sha512-j+9OJ0A0buZZaUn6NxeHUVpoa05tY2PgVs7kXJhJQiKRB0G1zQqbJxer3T7jWtzpqQWP89OBDluyIeyTsMk8Sg==",
"dev": true
},
"resolve": {
"version": "1.12.3",
"resolved": "https://registry.npmjs.org/resolve/-/resolve-1.12.3.tgz",
@@ -3551,6 +3557,29 @@
"requires": {
"@microsoft/tsdoc": "0.12.20",
"@microsoft/tsdoc-config": "0.13.5"
},
"dependencies": {
"@microsoft/tsdoc-config": {
"version": "0.13.5",
"resolved": "https://registry.npmjs.org/@microsoft/tsdoc-config/-/tsdoc-config-0.13.5.tgz",
"integrity": "sha512-KlnIdTRnPSsU9Coz9wzDAkT8JCLopP3ec1sgsgo7trwE6QLMKRpM4hZi2uzVX897SW49Q4f439auGBcQLnZQfA==",
"dev": true,
"requires": {
"@microsoft/tsdoc": "0.12.20",
"ajv": "~6.12.3",
"jju": "~1.4.0",
"resolve": "~1.12.0"
}
},
"resolve": {
"version": "1.12.3",
"resolved": "https://registry.npmjs.org/resolve/-/resolve-1.12.3.tgz",
"integrity": "sha512-hF6+hAPlxjqHWrw4p1rF3Wztbgxd4AjA5VlUzY5zcTb4J8D3JK4/1RjU48pHz2PJWzGVsLB1VWZkvJzhK2CCOA==",
"dev": true,
"requires": {
"path-parse": "^1.0.6"
}
}
}
},
"eslint-plugin-unicorn": {

View File

@@ -90,6 +90,7 @@
"yargs": "^15.4.1"
},
"devDependencies": {
"@microsoft/tsdoc-config": "^0.13.6",
"@types/jest": "^26.0.0",
"@types/rimraf": "^3.0.0",
"@types/supertest": "^2.0.10",

View File

@@ -1,8 +1,10 @@
import streamifyArray from 'streamify-array';
import { AclManager } from '../authorization/AclManager';
import { RepresentationMetadata } from '../ldp/representation/RepresentationMetadata';
import { ExpressHttpServer } from '../server/ExpressHttpServer';
import { ResourceStore } from '../storage/ResourceStore';
import { TEXT_TURTLE } from '../util/ContentTypes';
import { CONTENT_TYPE } from '../util/MetadataTypes';
/**
* Invokes all logic to setup a server.
@@ -48,16 +50,15 @@ export class Setup {
acl:mode acl:Control;
acl:accessTo <${this.base}>;
acl:default <${this.base}>.`;
const aclId = await this.aclManager.getAcl({ path: this.base });
const metadata = new RepresentationMetadata(aclId.path);
metadata.set(CONTENT_TYPE, TEXT_TURTLE);
await this.store.setRepresentation(
await this.aclManager.getAcl({ path: this.base }),
aclId,
{
binary: true,
data: streamifyArray([ acl ]),
metadata: {
raw: [],
profiles: [],
contentType: TEXT_TURTLE,
},
metadata,
},
);
};

View File

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

View File

@@ -1,5 +1,6 @@
import { HttpRequest } from '../../server/HttpRequest';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import { CONTENT_TYPE, SLUG, TYPE } from '../../util/MetadataTypes';
import { Representation } from '../representation/Representation';
import { RepresentationMetadata } from '../representation/RepresentationMetadata';
import { BodyParser } from './BodyParser';
@@ -39,10 +40,8 @@ export class RawBodyParser extends BodyParser {
private parseMetadata(input: HttpRequest): RepresentationMetadata {
const contentType = /^[^;]*/u.exec(input.headers['content-type']!)![0];
const metadata: RepresentationMetadata = {
raw: [],
contentType,
};
const metadata: RepresentationMetadata = new RepresentationMetadata();
metadata.set(CONTENT_TYPE, contentType);
const { link, slug } = input.headers;
@@ -50,12 +49,11 @@ export class RawBodyParser extends BodyParser {
if (Array.isArray(slug)) {
throw new UnsupportedHttpError('At most 1 slug header is allowed.');
}
metadata.slug = slug;
metadata.set(SLUG, slug);
}
// There are similarities here to Accept header parsing so that library should become more generic probably
if (link) {
metadata.linkRel = {};
const linkArray = Array.isArray(link) ? link : [ link ];
const parsedLinks = linkArray.map((entry): { url: string; rel: string } => {
const [ , url, rest ] = /^<([^>]*)>(.*)$/u.exec(entry) ?? [];
@@ -63,11 +61,8 @@ export class RawBodyParser extends BodyParser {
return { url, rel };
});
parsedLinks.forEach((entry): void => {
if (entry.rel) {
if (!metadata.linkRel![entry.rel]) {
metadata.linkRel![entry.rel] = new Set();
}
metadata.linkRel![entry.rel].add(entry.url);
if (entry.rel === 'type') {
metadata.set(TYPE, entry.url);
}
});
}

View File

@@ -3,7 +3,9 @@ import { translate } from 'sparqlalgebrajs';
import { HttpRequest } from '../../server/HttpRequest';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import { UnsupportedMediaTypeHttpError } from '../../util/errors/UnsupportedMediaTypeHttpError';
import { CONTENT_TYPE } from '../../util/MetadataTypes';
import { readableToString } from '../../util/Util';
import { RepresentationMetadata } from '../representation/RepresentationMetadata';
import { BodyParser } from './BodyParser';
import { SparqlUpdatePatch } from './SparqlUpdatePatch';
@@ -33,16 +35,15 @@ export class SparqlUpdateBodyParser extends BodyParser {
const sparql = await readableToString(toAlgebraStream);
const algebra = translate(sparql, { quads: true });
const metadata = new RepresentationMetadata();
metadata.add(CONTENT_TYPE, 'application/sparql-update');
// Prevent body from being requested again
return {
algebra,
binary: true,
data: dataCopy,
metadata: {
raw: [],
profiles: [],
contentType: 'application/sparql-update',
},
metadata,
};
} catch (error) {
throw new UnsupportedHttpError(error);

View File

@@ -1,47 +1,130 @@
/**
* Contains metadata relevant to a representation.
*/
import type { Quad } from 'rdf-js';
import { quad as createQuad, literal, namedNode } from '@rdfjs/data-model';
import { Store } from 'n3';
import type { BlankNode, Literal, NamedNode, Quad, Term } from 'rdf-js';
/**
* Metadata corresponding to a {@link Representation}.
* Stores the metadata triples and provides methods for easy access.
*/
export interface RepresentationMetadata {
export class RepresentationMetadata {
private store: Store;
private id: NamedNode | BlankNode;
/**
* All metadata triples of the resource.
* @param identifier - Identifier of the resource relevant to this metadata.
* A blank node will be generated if none is provided.
* Strings will be converted to named nodes. @ignored
* @param quads - Quads to fill the metadata with. @ignored
*
* `@ignored` tags are necessary for Components-Generator.js
*/
raw: Quad[];
public constructor(identifier?: NamedNode | BlankNode | string, quads?: Quad[]) {
this.store = new Store(quads);
if (identifier) {
if (typeof identifier === 'string') {
this.id = namedNode(identifier);
} else {
this.id = identifier;
}
} else {
this.id = this.store.createBlankNode();
}
}
/**
* Optional metadata profiles.
* @returns All metadata quads.
*/
profiles?: string[];
public quads(): Quad[] {
return this.store.getQuads(null, null, null, null);
}
/**
* Optional size of the representation.
* Identifier of the resource this metadata is relevant to.
* Will update all relevant triples if this value gets changed.
*/
byteSize?: number;
public get identifier(): NamedNode | BlankNode {
return this.id;
}
public set identifier(id: NamedNode | BlankNode) {
const quads = this.quads().map((quad): Quad => {
if (quad.subject.equals(this.id)) {
return createQuad(id, quad.predicate, quad.object, quad.graph);
}
if (quad.object.equals(this.id)) {
return createQuad(quad.subject, quad.predicate, id, quad.graph);
}
return quad;
});
this.store = new Store(quads);
this.id = id;
}
/**
* Optional content type of the representation.
* @param quads - Quads to add to the metadata.
*/
contentType?: string;
public addQuads(quads: Quad[]): void {
this.store.addQuads(quads);
}
/**
* Optional encoding of the representation.
* @param quads - Quads to remove from the metadata.
*/
encoding?: string;
public removeQuads(quads: Quad[]): void {
this.store.removeQuads(quads);
}
/**
* Optional language of the representation.
* Adds a value linked to the identifier. Strings get converted to literals.
* @param predicate - Predicate linking identifier to value.
* @param object - Value to add.
*/
language?: string;
public add(predicate: NamedNode, object: NamedNode | Literal | string): void {
this.store.addQuad(this.id, predicate, typeof object === 'string' ? literal(object) : object);
}
/**
* Optional timestamp of the representation.
* Removes the given value from the metadata. Strings get converted to literals.
* @param predicate - Predicate linking identifier to value.
* @param object - Value to remove.
*/
dateTime?: Date;
public remove(predicate: NamedNode, object: NamedNode | Literal | string): void {
this.store.removeQuad(this.id, predicate, typeof object === 'string' ? literal(object) : object);
}
/**
* Optional link relationships of the representation.
* Removes all values linked through the given predicate.
* @param predicate - Predicate to remove.
*/
linkRel?: { [id: string]: Set<string> };
public removeAll(predicate: NamedNode): void {
this.removeQuads(this.store.getQuads(this.id, predicate, null, null));
}
/**
* Optional slug of the representation.
* Used to suggest the URI for the resource created.
* @param predicate - Predicate to get the value for.
*
* @throws Error
* If there are multiple matching values.
*
* @returns The corresponding value. Undefined if there is no match
*/
slug?: string;
public get(predicate: NamedNode): Term | undefined {
const quads = this.store.getQuads(this.id, predicate, null, null);
if (quads.length === 0) {
return;
}
if (quads.length > 1) {
throw new Error(`Multiple results for ${predicate.value}`);
}
return quads[0].object;
}
/**
* Sets the value for the given predicate, removing all other instances.
* @param predicate - Predicate linking to the value.
* @param object - Value to set.
*/
public set(predicate: NamedNode, object: NamedNode | Literal | string): void {
this.removeAll(predicate);
this.add(predicate, object);
}
}

View File

@@ -1,6 +1,7 @@
import { createReadStream, createWriteStream, promises as fsPromises, Stats } from 'fs';
import { posix } from 'path';
import { Readable } from 'stream';
import { DataFactory } from 'n3';
import type { Quad } from 'rdf-js';
import streamifyArray from 'streamify-array';
import { Representation } from '../ldp/representation/Representation';
@@ -13,6 +14,7 @@ import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
import { UnsupportedMediaTypeHttpError } from '../util/errors/UnsupportedMediaTypeHttpError';
import { InteractionController } from '../util/InteractionController';
import { MetadataController } from '../util/MetadataController';
import { BYTE_SIZE, CONTENT_TYPE, LAST_CHANGED, SLUG, TYPE } from '../util/MetadataTypes';
import { ensureTrailingSlash } from '../util/Util';
import { ExtensionBasedMapper } from './ExtensionBasedMapper';
import { ResourceStore } from './ResourceStore';
@@ -55,16 +57,19 @@ export class FileResourceStore implements ResourceStore {
// Get the path from the request URI, all metadata triples if any, and the Slug and Link header values.
const path = this.resourceMapper.getRelativePath(container);
const { slug, raw } = representation.metadata;
const linkTypes = representation.metadata.linkRel?.type;
const slug = representation.metadata.get(SLUG)?.value;
const type = representation.metadata.get(TYPE)?.value;
// 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 newIdentifier = this.interactionController.generateIdentifier(isContainer, slug);
let metadata;
// eslint-disable-next-line no-param-reassign
representation.metadata.identifier = DataFactory.namedNode(newIdentifier);
const raw = representation.metadata.quads();
if (raw.length > 0) {
metadata = this.metadataController.serializeQuads(raw);
}
// Create a new container or resource in the parent container with a specific name based on the incoming headers.
const isContainer = this.interactionController.isContainer(slug, linkTypes);
const newIdentifier = this.interactionController.generateIdentifier(isContainer, slug);
return isContainer ?
this.createContainer(path, newIdentifier, path.endsWith('/'), metadata) :
this.createFile(path, newIdentifier, representation.data, path.endsWith('/'), metadata);
@@ -146,15 +151,17 @@ export class FileResourceStore implements ResourceStore {
// Break up the request URI in the different parts `containerPath` and `documentName` as we know their semantics
// from addResource to call the InteractionController in the same way.
const { containerPath, documentName } = this.resourceMapper.exctractDocumentName(identifier);
const { raw } = representation.metadata;
const linkTypes = representation.metadata.linkRel?.type;
// eslint-disable-next-line no-param-reassign
representation.metadata.identifier = DataFactory.namedNode(identifier.path);
const raw = representation.metadata.quads();
const type = representation.metadata.get(TYPE)?.value;
let metadata: Readable | undefined;
if (raw.length > 0) {
metadata = streamifyArray(raw);
}
// Create a new container or resource in the parent container with a specific name based on the incoming headers.
const isContainer = this.interactionController.isContainer(documentName, linkTypes);
const isContainer = this.interactionController.isContainer(documentName, type);
const newIdentifier = this.interactionController.generateIdentifier(isContainer, documentName);
return isContainer ?
await this.setDirectoryRepresentation(containerPath, newIdentifier, metadata) :
@@ -215,13 +222,10 @@ export class FileResourceStore implements ResourceStore {
} catch (_) {
// Metadata file doesn't exist so lets keep `rawMetaData` an empty array.
}
const metadata: RepresentationMetadata = {
raw: rawMetadata,
dateTime: stats.mtime,
byteSize: stats.size,
contentType,
};
const metadata = new RepresentationMetadata(this.resourceMapper.mapFilePathToUrl(path), rawMetadata);
metadata.set(LAST_CHANGED, stats.mtime.toISOString());
metadata.set(BYTE_SIZE, DataFactory.literal(stats.size));
metadata.set(CONTENT_TYPE, contentType);
return { metadata, data: readStream, binary: true };
}
@@ -252,14 +256,14 @@ export class FileResourceStore implements ResourceStore {
// Metadata file doesn't exist so lets keep `rawMetaData` an empty array.
}
const metadata = new RepresentationMetadata(containerURI, rawMetadata);
metadata.set(LAST_CHANGED, stats.mtime.toISOString());
metadata.set(CONTENT_TYPE, INTERNAL_QUADS);
return {
binary: false,
data: streamifyArray(quads),
metadata: {
raw: rawMetadata,
dateTime: stats.mtime,
contentType: INTERNAL_QUADS,
},
metadata,
};
}

View File

@@ -2,9 +2,11 @@ import { PassThrough } from 'stream';
import arrayifyStream from 'arrayify-stream';
import streamifyArray from 'streamify-array';
import { Representation } from '../ldp/representation/Representation';
import { RepresentationMetadata } from '../ldp/representation/RepresentationMetadata';
import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
import { TEXT_TURTLE } from '../util/ContentTypes';
import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
import { CONTENT_TYPE } from '../util/MetadataTypes';
import { ensureTrailingSlash } from '../util/Util';
import { ResourceStore } from './ResourceStore';
@@ -24,12 +26,14 @@ export class InMemoryResourceStore implements ResourceStore {
public constructor(base: string) {
this.base = ensureTrailingSlash(base);
const metadata = new RepresentationMetadata();
metadata.add(CONTENT_TYPE, TEXT_TURTLE);
this.store = {
// Default root entry (what you get when the identifier is equal to the base)
'': {
binary: true,
data: streamifyArray([]),
metadata: { raw: [], profiles: [], contentType: TEXT_TURTLE },
metadata,
},
};
}

View File

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

View File

@@ -1,5 +1,7 @@
import { Representation } from '../../ldp/representation/Representation';
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
import { RepresentationPreferences } from '../../ldp/representation/RepresentationPreferences';
import { CONTENT_TYPE } from '../../util/MetadataTypes';
import { matchingMediaType } from '../../util/Util';
import { RepresentationConverterArgs } from './RepresentationConverter';
import { TypedRepresentationConverter } from './TypedRepresentationConverter';
@@ -50,8 +52,10 @@ export class ChainedConverter extends TypedRepresentationConverter {
// Check if the last converter can produce the output
const idx = this.converters.length - 1;
const lastChain = await this.getMatchingType(this.converters[idx - 1], this.converters[idx]);
const representation: Representation = { ...input.representation };
representation.metadata = { ...input.representation.metadata, contentType: lastChain };
const oldMeta = input.representation.metadata;
const metadata = new RepresentationMetadata(oldMeta.identifier, oldMeta.quads());
metadata.set(CONTENT_TYPE, lastChain);
const representation: Representation = { ...input.representation, metadata };
await this.last.canHandle({ ...input, representation });
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -6,9 +6,11 @@ import { someTerms } from 'rdf-terms';
import { Algebra } from 'sparqlalgebrajs';
import { SparqlUpdatePatch } from '../../ldp/http/SparqlUpdatePatch';
import { Representation } from '../../ldp/representation/Representation';
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
import { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier';
import { INTERNAL_QUADS } from '../../util/ContentTypes';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import { CONTENT_TYPE } from '../../util/MetadataTypes';
import { ResourceLocker } from '../ResourceLocker';
import { ResourceStore } from '../ResourceStore';
import { PatchHandler } from './PatchHandler';
@@ -65,14 +67,12 @@ export class SparqlUpdatePatchHandler extends PatchHandler {
});
store.removeQuads(deletes);
store.addQuads(inserts);
const metadata = new RepresentationMetadata(input.identifier.path);
metadata.set(CONTENT_TYPE, INTERNAL_QUADS);
const representation: Representation = {
binary: false,
data: store.match() as Readable,
metadata: {
raw: [],
profiles: [],
contentType: INTERNAL_QUADS,
},
metadata,
};
await this.source.setRepresentation(input.identifier, representation);

View File

@@ -8,11 +8,11 @@ export class InteractionController {
* @param slug - Incoming slug header.
* @param link - Incoming link header.
*/
public isContainer(slug?: string, link?: Set<string>): boolean {
public isContainer(slug?: string, link?: string): boolean {
if (!slug || !slug.endsWith('/')) {
return Boolean(link?.has(LINK_TYPE_LDPC)) || Boolean(link?.has(LINK_TYPE_LDP_BC));
return Boolean(link === LINK_TYPE_LDPC) || Boolean(link === LINK_TYPE_LDP_BC);
}
return !link || link.has(LINK_TYPE_LDPC) || link.has(LINK_TYPE_LDP_BC);
return !link || link === LINK_TYPE_LDPC || link === LINK_TYPE_LDP_BC;
}
/**

View File

@@ -4,7 +4,7 @@ import arrayifyStream from 'arrayify-stream';
import { DataFactory, StreamParser, StreamWriter } from 'n3';
import type { NamedNode, Quad } from 'rdf-js';
import streamifyArray from 'streamify-array';
import { TEXT_TURTLE } from '../util/ContentTypes';
import { TEXT_TURTLE } from './ContentTypes';
import { LDP, RDF, STAT, TERMS, XML } from './Prefixes';
import { pipeStreamsAndErrors } from './Util';

View File

@@ -0,0 +1,9 @@
import { namedNode } from '@rdfjs/data-model';
import { NamedNode } from 'rdf-js';
export const TYPE: NamedNode = namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type');
export const CONTENT_TYPE: NamedNode = namedNode('http://www.w3.org/ns/ma-ont#format');
export const SLUG: NamedNode = namedNode('http://example.com/slug');
export const LAST_CHANGED: NamedNode = namedNode('http://example.com/lastChanged');
export const BYTE_SIZE: NamedNode = namedNode('http://example.com/byteSize');
export const ACL_RESOURCE: NamedNode = namedNode('http://example.com/acl');

View File

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

View File

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

View File

@@ -1,4 +1,5 @@
import { Setup } from '../../../src/init/Setup';
import { ResourceIdentifier } from '../../../src/ldp/representation/ResourceIdentifier';
describe('Setup', (): void => {
let httpServer: any;
@@ -10,7 +11,7 @@ describe('Setup', (): void => {
setRepresentation: jest.fn(async(): Promise<void> => undefined),
};
aclManager = {
getAcl: jest.fn(async(): Promise<void> => undefined),
getAcl: jest.fn(async(): Promise<ResourceIdentifier> => ({ path: 'http://test.com/.acl' })),
};
httpServer = {
listen: jest.fn(),

View File

@@ -1,10 +1,11 @@
import { EventEmitter } from 'events';
import { createResponse, MockResponse } from 'node-mocks-http';
import type { Quad } from 'rdf-js';
import streamifyArray from 'streamify-array';
import { BasicResponseWriter } from '../../../../src/ldp/http/BasicResponseWriter';
import { ResponseDescription } from '../../../../src/ldp/operations/ResponseDescription';
import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
describe('A BasicResponseWriter', (): void => {
const writer = new BasicResponseWriter();
@@ -32,10 +33,7 @@ describe('A BasicResponseWriter', (): void => {
const body = {
binary: true,
data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]),
metadata: {
raw: [] as Quad[],
profiles: [] as string[],
},
metadata: new RepresentationMetadata(),
};
response.on('end', (): void => {
@@ -50,14 +48,12 @@ describe('A BasicResponseWriter', (): void => {
});
it('responds with a content-type if the metadata has it.', async(done): Promise<void> => {
const metadata = new RepresentationMetadata();
metadata.add(CONTENT_TYPE, 'text/turtle');
const body = {
binary: true,
data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]),
metadata: {
raw: [] as Quad[],
profiles: [] as string[],
contentType: 'text/turtle',
},
metadata,
};
response.on('end', (): void => {

View File

@@ -1,9 +1,11 @@
import arrayifyStream from 'arrayify-stream';
import streamifyArray from 'streamify-array';
import { RawBodyParser } from '../../../../src/ldp/http/RawBodyParser';
import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata';
import { HttpRequest } from '../../../../src/server/HttpRequest';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
import 'jest-rdf';
import { CONTENT_TYPE, SLUG, TYPE } from '../../../../src/util/MetadataTypes';
describe('A RawBodyparser', (): void => {
const bodyParser = new RawBodyParser();
@@ -39,11 +41,9 @@ describe('A RawBodyparser', (): void => {
expect(result).toEqual({
binary: true,
data: input,
metadata: {
contentType: 'text/turtle',
raw: [],
},
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/turtle');
await expect(arrayifyStream(result.data)).resolves.toEqual(
[ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ],
);
@@ -53,11 +53,8 @@ describe('A RawBodyparser', (): void => {
const input = {} as HttpRequest;
input.headers = { 'transfer-encoding': 'chunked', 'content-type': 'text/turtle', slug: 'slugText' };
const result = (await bodyParser.handle(input))!;
expect(result.metadata).toEqual({
contentType: 'text/turtle',
raw: [],
slug: 'slugText',
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/turtle');
expect(result.metadata.get(SLUG)?.value).toEqual('slugText');
});
it('errors if there are multiple slugs.', async(): Promise<void> => {
@@ -68,33 +65,23 @@ describe('A RawBodyparser', (): void => {
await expect(bodyParser.handle(input)).rejects.toThrow(UnsupportedHttpError);
});
it('adds the link headers to the metadata.', async(): Promise<void> => {
it('adds the link type headers to the metadata.', async(): Promise<void> => {
const input = {} as HttpRequest;
input.headers = { 'transfer-encoding': 'chunked',
'content-type': 'text/turtle',
link: '<http://www.w3.org/ns/ldp#Container>; rel="type"' };
const result = (await bodyParser.handle(input))!;
expect(result.metadata).toEqual({
contentType: 'text/turtle',
raw: [],
linkRel: { type: new Set([ 'http://www.w3.org/ns/ldp#Container' ]) },
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/turtle');
expect(result.metadata.get(TYPE)?.value).toEqual('http://www.w3.org/ns/ldp#Container');
});
it('supports multiple link headers.', async(): Promise<void> => {
it('ignores unknown link headers.', async(): Promise<void> => {
const input = {} as HttpRequest;
input.headers = { 'transfer-encoding': 'chunked',
'content-type': 'text/turtle',
link: [ '<http://www.w3.org/ns/ldp#Container>; rel="type"',
'<http://www.w3.org/ns/ldp#Resource>; rel="type"',
'<unrelatedLink>',
'badLink',
]};
link: [ '<unrelatedLink>', 'badLink' ]};
const result = (await bodyParser.handle(input))!;
expect(result.metadata).toEqual({
contentType: 'text/turtle',
raw: [],
linkRel: { type: new Set([ 'http://www.w3.org/ns/ldp#Container', 'http://www.w3.org/ns/ldp#Resource' ]) },
});
expect(result.metadata.quads()).toHaveLength(1);
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/turtle');
});
});

View File

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

View File

@@ -0,0 +1,120 @@
import { literal, namedNode, quad } from '@rdfjs/data-model';
import { Literal, NamedNode, Quad } from 'rdf-js';
import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata';
describe('A RepresentationMetadata', (): void => {
let metadata: RepresentationMetadata;
const identifier = namedNode('http://example.com/id');
const inputQuads = [
quad(identifier, namedNode('has'), literal('data')),
quad(identifier, namedNode('has'), literal('moreData')),
quad(identifier, namedNode('hasOne'), literal('otherData')),
quad(namedNode('otherNode'), namedNode('linksTo'), identifier),
quad(namedNode('otherNode'), namedNode('has'), literal('otherData')),
];
describe('constructor', (): void => {
it('creates a blank node if no identifier was given.', async(): Promise<void> => {
metadata = new RepresentationMetadata();
expect(metadata.identifier.termType).toEqual('BlankNode');
expect(metadata.quads()).toHaveLength(0);
});
it('stores the given identifier if given.', async(): Promise<void> => {
metadata = new RepresentationMetadata(namedNode('identifier'));
expect(metadata.identifier).toEqualRdfTerm(namedNode('identifier'));
});
it('converts identifier strings to named nodes.', async(): Promise<void> => {
metadata = new RepresentationMetadata('identifier');
expect(metadata.identifier).toEqualRdfTerm(namedNode('identifier'));
});
it('stores input quads.', async(): Promise<void> => {
metadata = new RepresentationMetadata(identifier, inputQuads);
expect(metadata.quads()).toBeRdfIsomorphic(inputQuads);
});
});
describe('instantiated', (): void => {
beforeEach(async(): Promise<void> => {
metadata = new RepresentationMetadata(identifier, inputQuads);
});
it('can change the stored identifier.', async(): Promise<void> => {
const newIdentifier = namedNode('newNode');
metadata.identifier = newIdentifier;
const newQuads = inputQuads.map((triple): Quad => {
if (triple.subject.equals(identifier)) {
return quad(newIdentifier, triple.predicate, triple.object);
}
if (triple.object.equals(identifier)) {
return quad(triple.subject, triple.predicate, newIdentifier);
}
return triple;
});
expect(metadata.identifier).toEqualRdfTerm(newIdentifier);
expect(metadata.quads()).toBeRdfIsomorphic(newQuads);
});
it('can add quads.', async(): Promise<void> => {
const newQuads: Quad[] = [
quad(namedNode('random'), namedNode('new'), namedNode('triple')),
];
metadata.addQuads(newQuads);
expect(metadata.quads()).toBeRdfIsomorphic(newQuads.concat(inputQuads));
});
it('can remove quads.', async(): Promise<void> => {
metadata.removeQuads([ inputQuads[0] ]);
expect(metadata.quads()).toBeRdfIsomorphic(inputQuads.slice(1));
});
it('can add a single value for a predicate.', async(): Promise<void> => {
const newQuad = quad(identifier, namedNode('new'), namedNode('triple'));
metadata.add(newQuad.predicate as NamedNode, newQuad.object as NamedNode);
expect(metadata.quads()).toBeRdfIsomorphic([ newQuad ].concat(inputQuads));
});
it('can add single values as string.', async(): Promise<void> => {
const newQuad = quad(identifier, namedNode('new'), literal('triple'));
metadata.add(newQuad.predicate as NamedNode, newQuad.object.value);
expect(metadata.quads()).toBeRdfIsomorphic([ newQuad ].concat(inputQuads));
});
it('can remove a single value for a predicate.', async(): Promise<void> => {
metadata.remove(inputQuads[0].predicate as NamedNode, inputQuads[0].object as Literal);
expect(metadata.quads()).toBeRdfIsomorphic(inputQuads.slice(1));
});
it('can remove single values as string.', async(): Promise<void> => {
metadata.remove(inputQuads[0].predicate as NamedNode, inputQuads[0].object.value);
expect(metadata.quads()).toBeRdfIsomorphic(inputQuads.slice(1));
});
it('can remove all values for a predicate.', async(): Promise<void> => {
const pred = namedNode('has');
metadata.removeAll(pred);
const updatedNodes = inputQuads.filter((triple): boolean =>
!triple.subject.equals(identifier) || !triple.predicate.equals(pred));
expect(metadata.quads()).toBeRdfIsomorphic(updatedNodes);
});
it('can get the single value for a predicate.', async(): Promise<void> => {
expect(metadata.get(namedNode('hasOne'))).toEqualRdfTerm(literal('otherData'));
});
it('returns undefined if getting an undefined predicate.', async(): Promise<void> => {
expect(metadata.get(namedNode('doesntExist'))).toBeUndefined();
});
it('errors if there are multiple values when getting a value.', async(): Promise<void> => {
expect((): any => metadata.get(namedNode('has'))).toThrow(Error);
});
it('can set the value of predicate.', async(): Promise<void> => {
metadata.set(namedNode('has'), literal('singleValue'));
expect(metadata.get(namedNode('has'))).toEqualRdfTerm(literal('singleValue'));
});
});
});

View File

@@ -1,7 +1,7 @@
import fs, { promises as fsPromises, Stats, WriteStream } from 'fs';
import { posix } from 'path';
import { Readable } from 'stream';
import { literal, namedNode, quad as quadRDF, triple } from '@rdfjs/data-model';
import { literal, namedNode, quad as quadRDF } from '@rdfjs/data-model';
import arrayifyStream from 'arrayify-stream';
import { DataFactory } from 'n3';
import streamifyArray from 'streamify-array';
@@ -17,6 +17,7 @@ import { UnsupportedMediaTypeHttpError } from '../../../src/util/errors/Unsuppor
import { InteractionController } from '../../../src/util/InteractionController';
import { LINK_TYPE_LDP_BC, LINK_TYPE_LDPR } from '../../../src/util/LinkTypes';
import { MetadataController } from '../../../src/util/MetadataController';
import { BYTE_SIZE, CONTENT_TYPE, LAST_CHANGED, SLUG, TYPE } from '../../../src/util/MetadataTypes';
import { LDP, RDF, STAT, TERMS, XML } from '../../../src/util/Prefixes';
const { join: joinPath } = posix;
@@ -44,11 +45,6 @@ describe('A FileResourceStore', (): void => {
let stats: Stats;
let writeStream: WriteStream;
const rawData = 'lorem ipsum dolor sit amet consectetur adipiscing';
const quad = triple(
namedNode('http://test.com/s'),
namedNode('http://test.com/p'),
namedNode('http://test.com/o'),
);
beforeEach(async(): Promise<void> => {
jest.clearAllMocks();
@@ -62,13 +58,14 @@ describe('A FileResourceStore', (): void => {
representation = {
binary: true,
data: streamifyArray([ rawData ]),
metadata: { raw: [], linkRel: { type: new Set() }} as RepresentationMetadata,
metadata: new RepresentationMetadata(),
};
stats = {
isDirectory: jest.fn((): any => false) as Function,
isFile: jest.fn((): any => false) as Function,
mtime: new Date(),
size: 5,
} as jest.Mocked<Stats>;
writeStream = {
@@ -136,7 +133,8 @@ describe('A FileResourceStore', (): void => {
(fs.createReadStream as jest.Mock).mockImplementationOnce((): any => new Error('Metadata file does not exist.'));
// Write container (POST)
representation.metadata = { linkRel: { type: new Set([ LINK_TYPE_LDP_BC ]) }, slug: 'myContainer/', raw: []};
representation.metadata.add(TYPE, LINK_TYPE_LDP_BC);
representation.metadata.add(SLUG, 'myContainer/');
const identifier = await store.addResource({ path: base }, representation);
expect(fsPromises.mkdir as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'myContainer/'), { recursive: true });
expect(identifier.path).toBe(`${base}myContainer/`);
@@ -146,12 +144,10 @@ describe('A FileResourceStore', (): void => {
expect(result).toEqual({
binary: false,
data: expect.any(Readable),
metadata: {
raw: [],
dateTime: stats.mtime,
contentType: INTERNAL_QUADS,
},
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString());
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(result.data)).resolves.toBeDefined();
});
@@ -160,7 +156,8 @@ describe('A FileResourceStore', (): void => {
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
// Tests
representation.metadata = { linkRel: { type: new Set([ LINK_TYPE_LDP_BC ]) }, slug: 'myContainer/', raw: []};
representation.metadata.add(TYPE, LINK_TYPE_LDP_BC);
representation.metadata.add(SLUG, 'myContainer/');
await expect(store.addResource({ path: `${base}foo` }, representation)).rejects.toThrow(MethodNotAllowedHttpError);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo'));
});
@@ -176,17 +173,19 @@ describe('A FileResourceStore', (): void => {
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
// Tests
representation.metadata = { linkRel: { type: new Set([ LINK_TYPE_LDP_BC ]) }, slug: 'myContainer/', raw: []};
representation.metadata.add(TYPE, LINK_TYPE_LDP_BC);
representation.metadata.add(SLUG, 'myContainer/');
await expect(store.addResource({ path: `${base}doesnotexist` }, representation))
.rejects.toThrow(MethodNotAllowedHttpError);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'doesnotexist'));
representation.metadata = { linkRel: { type: new Set([ LINK_TYPE_LDPR ]) }, slug: 'file.txt', raw: []};
representation.metadata.set(TYPE, LINK_TYPE_LDPR);
representation.metadata.set(SLUG, 'file.txt');
await expect(store.addResource({ path: `${base}doesnotexist` }, representation))
.rejects.toThrow(MethodNotAllowedHttpError);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'doesnotexist'));
representation.metadata = { linkRel: { type: new Set() }, slug: 'file.txt', raw: []};
representation.metadata.removeAll(TYPE);
await expect(store.addResource({ path: `${base}existingresource` }, representation))
.rejects.toThrow(MethodNotAllowedHttpError);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'existingresource'));
@@ -216,13 +215,11 @@ describe('A FileResourceStore', (): void => {
expect(result).toEqual({
binary: true,
data: expect.any(Readable),
metadata: {
raw: [],
dateTime: stats.mtime,
byteSize: stats.size,
contentType: 'text/plain',
},
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString());
expect(result.metadata.get(BYTE_SIZE)?.value).toEqual(`${stats.size}`);
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/plain');
await expect(arrayifyStream(result.data)).resolves.toEqual([ rawData ]);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'file.txt'));
expect(fs.createReadStream as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'file.txt'));
@@ -257,7 +254,8 @@ describe('A FileResourceStore', (): void => {
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
// Tests
representation.metadata = { linkRel: { type: new Set([ LINK_TYPE_LDPR ]) }, slug: 'file.txt', raw: []};
representation.metadata.add(TYPE, LINK_TYPE_LDPR);
representation.metadata.add(SLUG, 'file.txt');
const identifier = await store.addResource({ path: `${base}doesnotexistyet/` }, representation);
expect(identifier.path).toBe(`${base}doesnotexistyet/file.txt`);
expect(fsPromises.mkdir as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'doesnotexistyet/'),
@@ -282,13 +280,12 @@ describe('A FileResourceStore', (): void => {
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
// Tests
representation.metadata = { linkRel: { type: new Set([ LINK_TYPE_LDPR ]) }, raw: [ quad ]};
representation.metadata.add(TYPE, LINK_TYPE_LDPR);
representation.data = readableMock;
await store.addResource({ path: `${base}foo/` }, representation);
expect(fsPromises.mkdir as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo/'), { recursive: true });
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo/'));
representation.metadata = { linkRel: { type: new Set([ LINK_TYPE_LDPR ]) }, raw: [ quad ]};
await store.setRepresentation({ path: `${base}foo/file.txt` }, representation);
expect(fs.createWriteStream as jest.Mock).toBeCalledTimes(4);
});
@@ -369,12 +366,10 @@ describe('A FileResourceStore', (): void => {
expect(result).toEqual({
binary: false,
data: expect.any(Readable),
metadata: {
raw: [],
dateTime: stats.mtime,
contentType: INTERNAL_QUADS,
},
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString());
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray(quads);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo/'));
expect(fsPromises.readdir as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo/'));
@@ -382,7 +377,7 @@ describe('A FileResourceStore', (): void => {
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo', '.nonresource'));
});
it('can overwrite representation with PUT.', async(): Promise<void> => {
it('can overwrite representation and its metadata with PUT.', async(): Promise<void> => {
// Mock the fs functions.
stats.isFile = jest.fn((): any => true);
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
@@ -392,9 +387,9 @@ describe('A FileResourceStore', (): void => {
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
// Tests
representation.metadata = { linkRel: { type: new Set([ LINK_TYPE_LDPR ]) }, raw: []};
representation.metadata.add(TYPE, LINK_TYPE_LDPR);
await store.setRepresentation({ path: `${base}alreadyexists.txt` }, representation);
expect(fs.createWriteStream as jest.Mock).toBeCalledTimes(1);
expect(fs.createWriteStream as jest.Mock).toBeCalledTimes(2);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'alreadyexists.txt'));
expect(fsPromises.mkdir as jest.Mock).toBeCalledWith(rootFilepath, { recursive: true });
});
@@ -408,7 +403,7 @@ describe('A FileResourceStore', (): void => {
await expect(store.setRepresentation({ path: `${base}alreadyexists` }, representation)).rejects
.toThrow(ConflictHttpError);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'alreadyexists'));
representation.metadata = { linkRel: { type: new Set([ LINK_TYPE_LDP_BC ]) }, raw: []};
representation.metadata.add(TYPE, LINK_TYPE_LDP_BC);
await expect(store.setRepresentation({ path: `${base}alreadyexists/` }, representation)).rejects
.toThrow(ConflictHttpError);
expect(fsPromises.access as jest.Mock).toBeCalledTimes(1);
@@ -422,7 +417,7 @@ describe('A FileResourceStore', (): void => {
(fsPromises.mkdir as jest.Mock).mockReturnValueOnce(true);
// Tests
representation.metadata = { linkRel: { type: new Set([ LINK_TYPE_LDP_BC ]) }, raw: []};
representation.metadata.add(TYPE, LINK_TYPE_LDP_BC);
await store.setRepresentation({ path: `${base}foo/` }, representation);
expect(fsPromises.mkdir as jest.Mock).toBeCalledTimes(1);
expect(fsPromises.access as jest.Mock).toBeCalledTimes(1);
@@ -440,7 +435,8 @@ describe('A FileResourceStore', (): void => {
(fsPromises.unlink as jest.Mock).mockReturnValueOnce(true);
// Tests
representation.metadata = { linkRel: { type: new Set([ LINK_TYPE_LDPR ]) }, slug: 'file.txt', raw: [ quad ]};
representation.metadata.add(TYPE, LINK_TYPE_LDPR);
representation.metadata.add(SLUG, 'file.txt');
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'));
@@ -456,7 +452,8 @@ describe('A FileResourceStore', (): void => {
(fsPromises.rmdir as jest.Mock).mockReturnValueOnce(true);
// Tests
representation.metadata = { linkRel: { type: new Set([ LINK_TYPE_LDP_BC ]) }, slug: 'foo/', raw: [ quad ]};
representation.metadata.add(TYPE, LINK_TYPE_LDP_BC);
representation.metadata.add(SLUG, 'foo/');
await expect(store.addResource({ path: base }, representation)).rejects.toThrow(Error);
expect(fsPromises.rmdir as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo/'));
});
@@ -467,7 +464,7 @@ describe('A FileResourceStore', (): void => {
(fsPromises.mkdir as jest.Mock).mockReturnValueOnce(true);
// Tests
representation.metadata = { slug: 'myContainer/', raw: []};
representation.metadata.add(SLUG, 'myContainer/');
const identifier = await store.addResource({ path: base }, representation);
expect(identifier.path).toBe(`${base}myContainer/`);
expect(fsPromises.mkdir as jest.Mock).toBeCalledTimes(1);
@@ -485,13 +482,11 @@ describe('A FileResourceStore', (): void => {
expect(result).toEqual({
binary: true,
data: expect.any(Readable),
metadata: {
raw: [],
contentType: 'application/octet-stream',
dateTime: stats.mtime,
byteSize: stats.size,
},
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('application/octet-stream');
expect(result.metadata.get(LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString());
expect(result.metadata.get(BYTE_SIZE)?.value).toEqual(`${stats.size}`);
});
it('errors when performing a PUT on the root path.', async(): Promise<void> => {
@@ -510,7 +505,6 @@ describe('A FileResourceStore', (): void => {
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
// Tests
representation.metadata = { raw: []};
await store.setRepresentation({ path: `${base}file.txt` }, representation);
expect(fsPromises.mkdir as jest.Mock).toBeCalledWith(rootFilepath, { recursive: true });
expect(fs.createWriteStream as jest.Mock).toBeCalledTimes(1);
@@ -525,10 +519,44 @@ describe('A FileResourceStore', (): void => {
(fsPromises.mkdir as jest.Mock).mockReturnValue(true);
// Tests
representation.metadata = { linkRel: { type: new Set([ LINK_TYPE_LDP_BC ]) }, slug: 'bar', raw: []};
representation.metadata.add(TYPE, LINK_TYPE_LDP_BC);
representation.metadata.add(SLUG, 'bar');
const identifier = await store.addResource({ path: `${base}foo` }, representation);
expect(identifier.path).toBe(`${base}foo/bar/`);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo'));
expect(fsPromises.mkdir as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo', 'bar/'), { recursive: false });
});
it('generates a new URI when adding without a slug.', async(): Promise<void> => {
// Mock the fs functions.
// Post
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
(fsPromises.mkdir as jest.Mock).mockReturnValue(true);
stats.isDirectory = jest.fn((): any => true);
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
// Mock: Get
stats.isFile = jest.fn((): any => true);
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
(fs.createReadStream as jest.Mock).mockReturnValueOnce(streamifyArray([ rawData ]));
(fs.createReadStream as jest.Mock).mockImplementationOnce((): any => new Error('Metadata file does not exist.'));
// Tests
await store.addResource({ path: base }, representation);
const filePath: string = (fs.createWriteStream as jest.Mock).mock.calls[0][0];
expect(filePath.startsWith(rootFilepath)).toBeTruthy();
const name = filePath.slice(rootFilepath.length);
const result = await store.getRepresentation({ path: `${base}${name}` });
expect(result).toEqual({
binary: true,
data: expect.any(Readable),
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString());
expect(result.metadata.get(BYTE_SIZE)?.value).toEqual(`${stats.size}`);
await expect(arrayifyStream(result.data)).resolves.toEqual([ rawData ]);
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}.metadata`));
});
});

View File

@@ -1,16 +1,20 @@
import { RepresentationMetadata } from '../../../src/ldp/representation/RepresentationMetadata';
import { RepresentationConverter } from '../../../src/storage/conversion/RepresentationConverter';
import { RepresentationConvertingStore } from '../../../src/storage/RepresentationConvertingStore';
import { ResourceStore } from '../../../src/storage/ResourceStore';
import { CONTENT_TYPE } from '../../../src/util/MetadataTypes';
describe('A RepresentationConvertingStore', (): void => {
let store: RepresentationConvertingStore;
let source: ResourceStore;
let handleSafeFn: jest.Mock<Promise<void>, []>;
let converter: RepresentationConverter;
const metadata = new RepresentationMetadata();
metadata.add(CONTENT_TYPE, 'text/turtle');
beforeEach(async(): Promise<void> => {
source = {
getRepresentation: jest.fn(async(): Promise<any> => ({ data: 'data', metadata: { contentType: 'text/turtle' }})),
getRepresentation: jest.fn(async(): Promise<any> => ({ data: 'data', metadata })),
} as unknown as ResourceStore;
handleSafeFn = jest.fn(async(): Promise<any> => 'converter');
@@ -20,12 +24,14 @@ describe('A RepresentationConvertingStore', (): void => {
});
it('returns the Representation from the source if no changes are required.', async(): Promise<void> => {
await expect(store.getRepresentation({ path: 'path' }, { type: [
const result = await store.getRepresentation({ path: 'path' }, { type: [
{ value: 'text/*', weight: 0 }, { value: 'text/turtle', weight: 1 },
]})).resolves.toEqual({
]});
expect(result).toEqual({
data: 'data',
metadata: { contentType: 'text/turtle' },
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/turtle');
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(source.getRepresentation).toHaveBeenLastCalledWith(
{ path: 'path' }, { type: [{ value: 'text/*', weight: 0 }, { value: 'text/turtle', weight: 1 }]}, undefined,
@@ -34,10 +40,12 @@ describe('A RepresentationConvertingStore', (): void => {
});
it('returns the Representation from the source if there are no preferences.', async(): Promise<void> => {
await expect(store.getRepresentation({ path: 'path' }, {})).resolves.toEqual({
const result = await store.getRepresentation({ path: 'path' }, {});
expect(result).toEqual({
data: 'data',
metadata: { contentType: 'text/turtle' },
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/turtle');
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(source.getRepresentation).toHaveBeenLastCalledWith(
{ path: 'path' }, {}, undefined,
@@ -53,7 +61,7 @@ describe('A RepresentationConvertingStore', (): void => {
expect(handleSafeFn).toHaveBeenCalledTimes(1);
expect(handleSafeFn).toHaveBeenLastCalledWith({
identifier: { path: 'path' },
representation: { data: 'data', metadata: { contentType: 'text/turtle' }},
representation: { data: 'data', metadata },
preferences: { type: [{ value: 'text/plain', weight: 1 }, { value: 'text/turtle', weight: 0 }]},
});
});

View File

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

View File

@@ -1,35 +1,43 @@
import { Representation } from '../../../../src/ldp/representation/Representation';
import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata';
import { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences';
import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
import { checkRequest, matchingTypes } from '../../../../src/storage/conversion/ConversionUtil';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
describe('A ConversionUtil', (): void => {
const identifier: ResourceIdentifier = { path: 'path' };
let representation: Representation;
let metadata: RepresentationMetadata;
beforeEach(async(): Promise<void> => {
metadata = new RepresentationMetadata();
representation = { metadata } as Representation;
});
describe('#checkRequest', (): void => {
it('requires an input type.', async(): Promise<void> => {
const representation = { metadata: {}} as Representation;
const preferences: RepresentationPreferences = {};
expect((): any => checkRequest({ identifier, representation, preferences }, [ '*/*' ], [ '*/*' ]))
.toThrow('Input type required for conversion.');
});
it('requires a matching input type.', async(): Promise<void> => {
const representation = { metadata: { contentType: 'a/x' }} as Representation;
metadata.add(CONTENT_TYPE, 'a/x');
const preferences: RepresentationPreferences = { type: [{ value: 'b/x', weight: 1 }]};
expect((): any => checkRequest({ identifier, representation, preferences }, [ 'c/x' ], [ '*/*' ]))
.toThrow('Can only convert from c/x to */*.');
});
it('requires a matching output type.', async(): Promise<void> => {
const representation = { metadata: { contentType: 'a/x' }} as Representation;
metadata.add(CONTENT_TYPE, 'a/x');
const preferences: RepresentationPreferences = { type: [{ value: 'b/x', weight: 1 }]};
expect((): any => checkRequest({ identifier, representation, preferences }, [ '*/*' ], [ 'c/x' ]))
.toThrow('Can only convert from */* to c/x.');
});
it('succeeds with a valid input and output type.', async(): Promise<void> => {
const representation = { metadata: { contentType: 'a/x' }} as Representation;
metadata.add(CONTENT_TYPE, 'a/x');
const preferences: RepresentationPreferences = { type: [{ value: 'b/x', weight: 1 }]};
expect(checkRequest({ identifier, representation, preferences }, [ '*/*' ], [ '*/*' ]))
.toBeUndefined();

View File

@@ -3,14 +3,18 @@ import rdfSerializer from 'rdf-serialize';
import stringifyStream from 'stream-to-string';
import streamifyArray from 'streamify-array';
import { Representation } from '../../../../src/ldp/representation/Representation';
import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata';
import { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences';
import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
import { QuadToRdfConverter } from '../../../../src/storage/conversion/QuadToRdfConverter';
import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
describe('A QuadToRdfConverter', (): void => {
const converter = new QuadToRdfConverter();
const identifier: ResourceIdentifier = { path: 'path' };
const metadata = new RepresentationMetadata();
metadata.set(CONTENT_TYPE, INTERNAL_QUADS);
it('supports parsing quads.', async(): Promise<void> => {
await expect(converter.getInputTypes()).resolves.toEqual({ [INTERNAL_QUADS]: 1 });
@@ -21,13 +25,13 @@ describe('A QuadToRdfConverter', (): void => {
});
it('can handle quad to turtle conversions.', async(): Promise<void> => {
const representation = { metadata: { contentType: INTERNAL_QUADS }} as Representation;
const representation = { metadata } as Representation;
const preferences: RepresentationPreferences = { type: [{ value: 'text/turtle', weight: 1 }]};
await expect(converter.canHandle({ identifier, representation, preferences })).resolves.toBeUndefined();
});
it('can handle quad to JSON-LD conversions.', async(): Promise<void> => {
const representation = { metadata: { contentType: INTERNAL_QUADS }} as Representation;
const representation = { metadata } as Representation;
const preferences: RepresentationPreferences = { type: [{ value: 'application/ld+json', weight: 1 }]};
await expect(converter.canHandle({ identifier, representation, preferences })).resolves.toBeUndefined();
});
@@ -39,16 +43,15 @@ describe('A QuadToRdfConverter', (): void => {
namedNode('http://test.com/p'),
namedNode('http://test.com/o'),
) ]),
metadata: { contentType: INTERNAL_QUADS },
metadata,
} as Representation;
const preferences: RepresentationPreferences = { type: [{ value: 'text/turtle', weight: 1 }]};
const result = await converter.handle({ identifier, representation, preferences });
expect(result).toMatchObject({
binary: true,
metadata: {
contentType: 'text/turtle',
},
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/turtle');
await expect(stringifyStream(result.data)).resolves.toEqual(
`<http://test.com/s> <http://test.com/p> <http://test.com/o>.
`,
@@ -56,22 +59,22 @@ describe('A QuadToRdfConverter', (): void => {
});
it('converts quads to JSON-LD.', async(): Promise<void> => {
metadata.set(CONTENT_TYPE, INTERNAL_QUADS);
const representation = {
data: streamifyArray([ triple(
namedNode('http://test.com/s'),
namedNode('http://test.com/p'),
namedNode('http://test.com/o'),
) ]),
metadata: { contentType: INTERNAL_QUADS },
metadata,
} as Representation;
const preferences: RepresentationPreferences = { type: [{ value: 'application/ld+json', weight: 1 }]};
const result = await converter.handle({ identifier, representation, preferences });
expect(result).toMatchObject({
binary: true,
metadata: {
contentType: 'application/ld+json',
},
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('application/ld+json');
await expect(stringifyStream(result.data)).resolves.toEqual(
`[
{

View File

@@ -2,17 +2,21 @@ import { namedNode, triple } from '@rdfjs/data-model';
import arrayifyStream from 'arrayify-stream';
import streamifyArray from 'streamify-array';
import { Representation } from '../../../../src/ldp/representation/Representation';
import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata';
import { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences';
import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
import { QuadToTurtleConverter } from '../../../../src/storage/conversion/QuadToTurtleConverter';
import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
describe('A QuadToTurtleConverter', (): void => {
const converter = new QuadToTurtleConverter();
const identifier: ResourceIdentifier = { path: 'path' };
const metadata = new RepresentationMetadata();
metadata.add(CONTENT_TYPE, INTERNAL_QUADS);
it('can handle quad to turtle conversions.', async(): Promise<void> => {
const representation = { metadata: { contentType: INTERNAL_QUADS }} as Representation;
const representation = { metadata } as Representation;
const preferences: RepresentationPreferences = { type: [{ value: 'text/turtle', weight: 1 }]};
await expect(converter.canHandle({ identifier, representation, preferences })).resolves.toBeUndefined();
});
@@ -24,16 +28,15 @@ describe('A QuadToTurtleConverter', (): void => {
namedNode('http://test.com/p'),
namedNode('http://test.com/o'),
) ]),
metadata: { contentType: INTERNAL_QUADS },
metadata,
} as Representation;
const preferences: RepresentationPreferences = { type: [{ value: 'text/turtle', weight: 1 }]};
const result = await converter.handle({ identifier, representation, preferences });
expect(result).toMatchObject({
binary: true,
metadata: {
contentType: 'text/turtle',
},
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/turtle');
await expect(arrayifyStream(result.data)).resolves.toContain(
'<http://test.com/s> <http://test.com/p> <http://test.com/o>',
);

View File

@@ -4,11 +4,13 @@ import arrayifyStream from 'arrayify-stream';
import rdfParser from 'rdf-parse';
import streamifyArray from 'streamify-array';
import { Representation } from '../../../../src/ldp/representation/Representation';
import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata';
import { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences';
import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
import { RdfToQuadConverter } from '../../../../src/storage/conversion/RdfToQuadConverter';
import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
describe('A RdfToQuadConverter.test.ts', (): void => {
const converter = new RdfToQuadConverter();
@@ -23,31 +25,36 @@ describe('A RdfToQuadConverter.test.ts', (): void => {
});
it('can handle turtle to quad conversions.', async(): Promise<void> => {
const representation = { metadata: { contentType: 'text/turtle' }} as Representation;
const metadata = new RepresentationMetadata();
metadata.set(CONTENT_TYPE, 'text/turtle');
const representation = { metadata } as Representation;
const preferences: RepresentationPreferences = { type: [{ value: INTERNAL_QUADS, weight: 1 }]};
await expect(converter.canHandle({ identifier, representation, preferences })).resolves.toBeUndefined();
});
it('can handle JSON-LD to quad conversions.', async(): Promise<void> => {
const representation = { metadata: { contentType: 'application/ld+json' }} as Representation;
const metadata = new RepresentationMetadata();
metadata.set(CONTENT_TYPE, 'application/ld+json');
const representation = { metadata } as Representation;
const preferences: RepresentationPreferences = { type: [{ value: INTERNAL_QUADS, weight: 1 }]};
await expect(converter.canHandle({ identifier, representation, preferences })).resolves.toBeUndefined();
});
it('converts turtle to quads.', async(): Promise<void> => {
const metadata = new RepresentationMetadata();
metadata.set(CONTENT_TYPE, 'text/turtle');
const representation = {
data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]),
metadata: { contentType: 'text/turtle' },
metadata,
} as Representation;
const preferences: RepresentationPreferences = { type: [{ value: INTERNAL_QUADS, weight: 1 }]};
const result = await converter.handle({ identifier, representation, preferences });
expect(result).toEqual({
binary: false,
data: expect.any(Readable),
metadata: {
contentType: INTERNAL_QUADS,
},
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray([ triple(
namedNode('http://test.com/s'),
namedNode('http://test.com/p'),
@@ -56,19 +63,20 @@ describe('A RdfToQuadConverter.test.ts', (): void => {
});
it('converts JSON-LD to quads.', async(): Promise<void> => {
const metadata = new RepresentationMetadata();
metadata.set(CONTENT_TYPE, 'application/ld+json');
const representation = {
data: streamifyArray([ '{"@id": "http://test.com/s", "http://test.com/p": { "@id": "http://test.com/o" }}' ]),
metadata: { contentType: 'application/ld+json' },
metadata,
} as Representation;
const preferences: RepresentationPreferences = { type: [{ value: INTERNAL_QUADS, weight: 1 }]};
const result = await converter.handle({ identifier, representation, preferences });
expect(result).toEqual({
binary: false,
data: expect.any(Readable),
metadata: {
contentType: INTERNAL_QUADS,
},
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray([ triple(
namedNode('http://test.com/s'),
namedNode('http://test.com/p'),
@@ -77,19 +85,20 @@ describe('A RdfToQuadConverter.test.ts', (): void => {
});
it('throws an UnsupportedHttpError on invalid triple data.', async(): Promise<void> => {
const metadata = new RepresentationMetadata();
metadata.set(CONTENT_TYPE, 'text/turtle');
const representation = {
data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.co' ]),
metadata: { contentType: 'text/turtle' },
metadata,
} as Representation;
const preferences: RepresentationPreferences = { type: [{ value: INTERNAL_QUADS, weight: 1 }]};
const result = await converter.handle({ identifier, representation, preferences });
expect(result).toEqual({
binary: false,
data: expect.any(Readable),
metadata: {
contentType: INTERNAL_QUADS,
},
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(result.data)).rejects.toThrow(UnsupportedHttpError);
});
});

View File

@@ -3,18 +3,22 @@ import { namedNode, triple } from '@rdfjs/data-model';
import arrayifyStream from 'arrayify-stream';
import streamifyArray from 'streamify-array';
import { Representation } from '../../../../src/ldp/representation/Representation';
import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata';
import { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences';
import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
import { TurtleToQuadConverter } from '../../../../src/storage/conversion/TurtleToQuadConverter';
import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
describe('A TurtleToQuadConverter', (): void => {
const converter = new TurtleToQuadConverter();
const identifier: ResourceIdentifier = { path: 'path' };
const metadata = new RepresentationMetadata();
metadata.add(CONTENT_TYPE, 'text/turtle');
it('can handle turtle to quad conversions.', async(): Promise<void> => {
const representation = { metadata: { contentType: 'text/turtle' }} as Representation;
const representation = { metadata } as Representation;
const preferences: RepresentationPreferences = { type: [{ value: INTERNAL_QUADS, weight: 1 }]};
await expect(converter.canHandle({ identifier, representation, preferences })).resolves.toBeUndefined();
});
@@ -22,17 +26,16 @@ describe('A TurtleToQuadConverter', (): void => {
it('converts turtle to quads.', async(): Promise<void> => {
const representation = {
data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]),
metadata: { contentType: 'text/turtle' },
metadata,
} as Representation;
const preferences: RepresentationPreferences = { type: [{ value: INTERNAL_QUADS, weight: 1 }]};
const result = await converter.handle({ identifier, representation, preferences });
expect(result).toEqual({
binary: false,
data: expect.any(Readable),
metadata: {
contentType: INTERNAL_QUADS,
},
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray([ triple(
namedNode('http://test.com/s'),
namedNode('http://test.com/p'),
@@ -43,17 +46,16 @@ describe('A TurtleToQuadConverter', (): void => {
it('throws an UnsupportedHttpError on invalid triple data.', async(): Promise<void> => {
const representation = {
data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.co' ]),
metadata: { contentType: 'text/turtle' },
metadata,
} as Representation;
const preferences: RepresentationPreferences = { type: [{ value: INTERNAL_QUADS, weight: 1 }]};
const result = await converter.handle({ identifier, representation, preferences });
expect(result).toEqual({
binary: false,
data: expect.any(Readable),
metadata: {
contentType: INTERNAL_QUADS,
},
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(result.data)).rejects.toThrow(UnsupportedHttpError);
});
});

View File

@@ -4,12 +4,14 @@ import type { Quad } from 'rdf-js';
import { translate } from 'sparqlalgebrajs';
import streamifyArray from 'streamify-array';
import { SparqlUpdatePatch } from '../../../../src/ldp/http/SparqlUpdatePatch';
import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata';
import { Lock } from '../../../../src/storage/Lock';
import { SparqlUpdatePatchHandler } from '../../../../src/storage/patch/SparqlUpdatePatchHandler';
import { ResourceLocker } from '../../../../src/storage/ResourceLocker';
import { ResourceStore } from '../../../../src/storage/ResourceStore';
import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
describe('A SparqlUpdatePatchHandler', (): void => {
let handler: SparqlUpdatePatchHandler;
@@ -73,8 +75,9 @@ describe('A SparqlUpdatePatchHandler', (): void => {
expect(setParams[0]).toEqual({ path: 'path' });
expect(setParams[1]).toEqual(expect.objectContaining({
binary: false,
metadata: { raw: [], profiles: [], contentType: INTERNAL_QUADS },
metadata: expect.any(RepresentationMetadata),
}));
expect(setParams[1].metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(setParams[1].data)).resolves.toBeRdfIsomorphic(quads);
};

9
tsdoc.json Normal file
View File

@@ -0,0 +1,9 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json",
"tagDefinitions": [
{
"tagName": "@ignored",
"syntaxKind": "modifier"
}
]
}