feat: Add Content-Type constructor to metadata.

This commit is contained in:
Ruben Verborgh
2021-01-10 21:31:55 +01:00
parent 775aaa79cd
commit be1af89b56
18 changed files with 67 additions and 47 deletions

View File

@@ -7,7 +7,6 @@ import { TEXT_TURTLE } from '../util/ContentTypes';
import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
import { ensureTrailingSlash } from '../util/PathUtil';
import { guardedStreamFrom } from '../util/StreamUtil';
import { CONTENT_TYPE } from '../util/Vocabularies';
import { Initializer } from './Initializer';
/**
@@ -67,7 +66,7 @@ export class AclInitializer extends Initializer {
acl:mode acl:Control;
acl:accessTo <${this.baseUrl}>;
acl:default <${this.baseUrl}>.`;
const metadata = new RepresentationMetadata(rootAcl, { [CONTENT_TYPE]: TEXT_TURTLE });
const metadata = new RepresentationMetadata(rootAcl, TEXT_TURTLE);
this.logger.debug(`Installing root ACL document at ${rootAcl.path}`);
await this.store.setRepresentation(
rootAcl,

View File

@@ -2,12 +2,13 @@ import { DataFactory, Store } from 'n3';
import type { BlankNode, Literal, NamedNode, Quad, Term } from 'rdf-js';
import { getLoggerFor } from '../../logging/LogUtil';
import { toSubjectTerm, toObjectTerm, toCachedNamedNode, isTerm } from '../../util/TermUtil';
import { CONTENT_TYPE_TERM } from '../../util/Vocabularies';
import { CONTENT_TYPE, CONTENT_TYPE_TERM } from '../../util/Vocabularies';
import type { ResourceIdentifier } from './ResourceIdentifier';
import { isResourceIdentifier } from './ResourceIdentifier';
export type MetadataIdentifier = ResourceIdentifier | NamedNode | BlankNode;
export type MetadataOverrideValue = NamedNode | Literal | string | (NamedNode | Literal | string)[];
export type MetadataValue = NamedNode | Literal | string | (NamedNode | Literal | string)[];
export type MetadataRecord = Record<string, MetadataValue>;
/**
* Determines whether the object is a `RepresentationMetadata`.
@@ -34,23 +35,40 @@ export class RepresentationMetadata {
*
* `@ignored` tag is necessary for Components-Generator.js
*/
public constructor(identifier?: MetadataIdentifier, overrides?: Record<string, MetadataOverrideValue>);
public constructor(identifier?: MetadataIdentifier, overrides?: MetadataRecord);
/**
* @param metadata - Starts as a copy of the input metadata.
* @param overrides - Key/value map of extra values that need to be added to the metadata.
* Will override values that were set by the input metadata.
*/
public constructor(metadata?: RepresentationMetadata, overrides?: Record<string, MetadataOverrideValue>);
public constructor(metadata?: RepresentationMetadata, overrides?: MetadataRecord);
/**
* @param overrides - Key/value map of extra values that need to be added to the metadata.
* @param identifier - Identifier of the resource relevant to this metadata.
* @param contentType - Override for the content type of the representation.
*/
public constructor(overrides?: Record<string, MetadataOverrideValue>);
public constructor(identifier?: MetadataIdentifier, contentType?: string);
/**
* @param metadata - Starts as a copy of the input metadata.
* @param contentType - Override for the content type of the representation.
*/
public constructor(metadata?: RepresentationMetadata, contentType?: string);
/**
* @param contentType - The content type of the representation.
*/
public constructor(contentType?: string);
/**
* @param overrides - Metadata values (defaulting to content type if a string)
*/
public constructor(metadata?: RepresentationMetadata | MetadataRecord | string);
public constructor(
input?: MetadataIdentifier | RepresentationMetadata | Record<string, MetadataOverrideValue>,
overrides?: Record<string, MetadataOverrideValue>,
input?: MetadataIdentifier | RepresentationMetadata | MetadataRecord | string,
overrides?: MetadataRecord | string,
) {
this.store = new Store();
if (isResourceIdentifier(input)) {
@@ -66,11 +84,14 @@ export class RepresentationMetadata {
}
if (overrides) {
if (typeof overrides === 'string') {
overrides = { [CONTENT_TYPE]: overrides };
}
this.setOverrides(overrides);
}
}
private setOverrides(overrides: Record<string, MetadataOverrideValue>): void {
private setOverrides(overrides: Record<string, MetadataValue>): void {
for (const predicate of Object.keys(overrides)) {
const namedPredicate = toCachedNamedNode(predicate);
this.removeAll(namedPredicate);

View File

@@ -2,7 +2,6 @@ import type { Representation } from '../../ldp/representation/Representation';
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
import type { ValuePreferences } from '../../ldp/representation/RepresentationPreferences';
import { NotImplementedHttpError } from '../../util/errors/NotImplementedHttpError';
import { CONTENT_TYPE } from '../../util/Vocabularies';
import { matchesMediaType, matchingMediaTypes } from './ConversionUtil';
import type { RepresentationConverterArgs } from './RepresentationConverter';
import { RepresentationConverter } from './RepresentationConverter';
@@ -50,7 +49,7 @@ export class ContentTypeReplacer extends RepresentationConverter {
*/
public async handle({ representation, preferences }: RepresentationConverterArgs): Promise<Representation> {
const contentType = this.getReplacementType(representation.metadata.contentType, preferences.type);
const metadata = new RepresentationMetadata(representation.metadata, { [CONTENT_TYPE]: contentType });
const metadata = new RepresentationMetadata(representation.metadata, contentType);
return { ...representation, metadata };
}

View File

@@ -7,7 +7,7 @@ import type { ValuePreferences } from '../../ldp/representation/RepresentationPr
import { INTERNAL_QUADS } from '../../util/ContentTypes';
import { guardStream } from '../../util/GuardedStream';
import { pipeSafely } from '../../util/StreamUtil';
import { CONTENT_TYPE, PREFERRED_PREFIX_TERM } from '../../util/Vocabularies';
import { PREFERRED_PREFIX_TERM } from '../../util/Vocabularies';
import { matchingMediaTypes } from './ConversionUtil';
import type { RepresentationConverterArgs } from './RepresentationConverter';
import { TypedRepresentationConverter } from './TypedRepresentationConverter';
@@ -27,7 +27,7 @@ export class QuadToRdfConverter extends TypedRepresentationConverter {
public async handle({ representation: quads, preferences }: RepresentationConverterArgs): Promise<Representation> {
const contentType = matchingMediaTypes(preferences.type, await this.getOutputTypes())[0];
const metadata = new RepresentationMetadata(quads.metadata, { [CONTENT_TYPE]: contentType });
const metadata = new RepresentationMetadata(quads.metadata, contentType);
let data: Readable;
// Use prefixes if possible (see https://github.com/rubensworks/rdf-serialize.js/issues/1)

View File

@@ -5,7 +5,6 @@ import { RepresentationMetadata } from '../../ldp/representation/RepresentationM
import { INTERNAL_QUADS } from '../../util/ContentTypes';
import { BadRequestHttpError } from '../../util/errors/BadRequestHttpError';
import { pipeSafely } from '../../util/StreamUtil';
import { CONTENT_TYPE } from '../../util/Vocabularies';
import type { RepresentationConverterArgs } from './RepresentationConverter';
import { TypedRepresentationConverter } from './TypedRepresentationConverter';
@@ -18,7 +17,7 @@ export class RdfToQuadConverter extends TypedRepresentationConverter {
}
public async handle({ representation, identifier }: RepresentationConverterArgs): Promise<Representation> {
const metadata = new RepresentationMetadata(representation.metadata, { [CONTENT_TYPE]: INTERNAL_QUADS });
const metadata = new RepresentationMetadata(representation.metadata, INTERNAL_QUADS);
const rawQuads = rdfParser.parse(representation.data, {
contentType: representation.metadata.contentType!,
baseIRI: identifier.path,

View File

@@ -14,7 +14,6 @@ import { NotFoundHttpError } from '../../util/errors/NotFoundHttpError';
import { NotImplementedHttpError } from '../../util/errors/NotImplementedHttpError';
import { guardStream } from '../../util/GuardedStream';
import type { ResourceLocker } from '../../util/locking/ResourceLocker';
import { CONTENT_TYPE } from '../../util/Vocabularies';
import type { ResourceStore } from '../ResourceStore';
import { PatchHandler } from './PatchHandler';
@@ -109,7 +108,7 @@ export class SparqlUpdatePatchHandler extends PatchHandler {
this.logger.debug(`${store.size} quads will be stored to ${identifier.path}.`);
// Write the result
const metadata = new RepresentationMetadata(identifier, { [CONTENT_TYPE]: INTERNAL_QUADS });
const metadata = new RepresentationMetadata(identifier, INTERNAL_QUADS);
const representation: Representation = {
binary: false,
data: guardStream(store.match() as Readable),