refactor: Simplify TypedRepresentationConverter contruction.

This commit is contained in:
Ruben Verborgh
2021-01-09 00:07:44 +01:00
parent 27a115022b
commit 27a5711ec2
7 changed files with 96 additions and 55 deletions

View File

@@ -1,5 +1,4 @@
import type { Representation } from '../../ldp/representation/Representation';
import type { ValuePreferences } from '../../ldp/representation/RepresentationPreferences';
import { getLoggerFor } from '../../logging/LogUtil';
import { matchesMediaType } from './ConversionUtil';
import type { RepresentationConverterArgs } from './RepresentationConverter';
@@ -25,6 +24,8 @@ export class ChainedConverter extends TypedRepresentationConverter {
throw new Error('At least 2 converters are required.');
}
this.converters = [ ...converters ];
this.inputTypes = this.first.getInputTypes();
this.outputTypes = this.last.getOutputTypes();
}
protected get first(): TypedRepresentationConverter {
@@ -35,14 +36,6 @@ export class ChainedConverter extends TypedRepresentationConverter {
return this.converters[this.converters.length - 1];
}
public async getInputTypes(): Promise<ValuePreferences> {
return this.first.getInputTypes();
}
public async getOutputTypes(): Promise<ValuePreferences> {
return this.last.getOutputTypes();
}
public async handle(input: RepresentationConverterArgs): Promise<Representation> {
const args = { ...input };
for (let i = 0; i < this.converters.length - 1; ++i) {

View File

@@ -3,10 +3,7 @@ import { StreamWriter } from 'n3';
import rdfSerializer from 'rdf-serialize';
import type { Representation } from '../../ldp/representation/Representation';
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
import type {
ValuePreferences,
RepresentationPreferences,
} from '../../ldp/representation/RepresentationPreferences';
import type { ValuePreferences } from '../../ldp/representation/RepresentationPreferences';
import { INTERNAL_QUADS } from '../../util/ContentTypes';
import { guardStream } from '../../util/GuardedStream';
import { pipeSafely } from '../../util/StreamUtil';
@@ -22,26 +19,14 @@ export class QuadToRdfConverter extends TypedRepresentationConverter {
private readonly outputPreferences?: ValuePreferences;
public constructor(options: { outputPreferences?: Record<string, number> } = {}) {
super();
if (Object.keys(options.outputPreferences ?? {}).length > 0) {
this.outputPreferences = { ...options.outputPreferences };
}
super(
INTERNAL_QUADS,
options.outputPreferences ?? rdfSerializer.getContentTypesPrioritized(),
);
}
public async getInputTypes(): Promise<ValuePreferences> {
return { [INTERNAL_QUADS]: 1 };
}
public async getOutputTypes(): Promise<ValuePreferences> {
return this.outputPreferences ?? rdfSerializer.getContentTypesPrioritized();
}
public async handle(input: RepresentationConverterArgs): Promise<Representation> {
return this.quadsToRdf(input.representation, input.preferences);
}
private async quadsToRdf(quads: Representation, { type }: RepresentationPreferences): Promise<Representation> {
const contentType = matchingMediaTypes(type, await this.getOutputTypes())[0];
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 });
let data: Readable;

View File

@@ -2,7 +2,6 @@ import { PassThrough } from 'stream';
import rdfParser from 'rdf-parse';
import type { Representation } from '../../ldp/representation/Representation';
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
import type { ValuePreferences } from '../../ldp/representation/RepresentationPreferences';
import { INTERNAL_QUADS } from '../../util/ContentTypes';
import { BadRequestHttpError } from '../../util/errors/BadRequestHttpError';
import { pipeSafely } from '../../util/StreamUtil';
@@ -14,23 +13,15 @@ import { TypedRepresentationConverter } from './TypedRepresentationConverter';
* Converts most major RDF serializations to `internal/quads`.
*/
export class RdfToQuadConverter extends TypedRepresentationConverter {
public async getInputTypes(): Promise<ValuePreferences> {
return rdfParser.getContentTypesPrioritized();
public constructor() {
super(rdfParser.getContentTypesPrioritized(), INTERNAL_QUADS);
}
public async getOutputTypes(): Promise<ValuePreferences> {
return { [INTERNAL_QUADS]: 1 };
}
public async handle(input: RepresentationConverterArgs): Promise<Representation> {
return this.rdfToQuads(input.representation, input.identifier.path);
}
private rdfToQuads(representation: Representation, baseIRI: string): Representation {
public async handle({ representation, identifier }: RepresentationConverterArgs): Promise<Representation> {
const metadata = new RepresentationMetadata(representation.metadata, { [CONTENT_TYPE]: INTERNAL_QUADS });
const rawQuads = rdfParser.parse(representation.data, {
contentType: representation.metadata.contentType!,
baseIRI,
baseIRI: identifier.path,
});
const pass = new PassThrough({ objectMode: true });

View File

@@ -3,19 +3,49 @@ import { supportsMediaTypeConversion } from './ConversionUtil';
import { RepresentationConverter } from './RepresentationConverter';
import type { RepresentationConverterArgs } from './RepresentationConverter';
type PromiseOrValue<T> = T | Promise<T>;
type ValuePreferencesArg =
PromiseOrValue<string> |
PromiseOrValue<string[]> |
PromiseOrValue<ValuePreferences>;
async function toValuePreferences(arg: ValuePreferencesArg): Promise<ValuePreferences> {
const resolved = await arg;
if (typeof resolved === 'string') {
return { [resolved]: 1 };
}
if (Array.isArray(resolved)) {
return Object.fromEntries(resolved.map((type): [string, number] => [ type, 1 ]));
}
return resolved;
}
/**
* A {@link RepresentationConverter} that allows requesting the supported types.
*/
export abstract class TypedRepresentationConverter extends RepresentationConverter {
protected inputTypes: Promise<ValuePreferences>;
protected outputTypes: Promise<ValuePreferences>;
public constructor(inputTypes: ValuePreferencesArg = {}, outputTypes: ValuePreferencesArg = {}) {
super();
this.inputTypes = toValuePreferences(inputTypes);
this.outputTypes = toValuePreferences(outputTypes);
}
/**
* Gets the supported input content types for this converter, mapped to a numerical priority.
*/
public abstract getInputTypes(): Promise<ValuePreferences>;
public async getInputTypes(): Promise<ValuePreferences> {
return this.inputTypes;
}
/**
* Gets the supported output content types for this converter, mapped to a numerical quality.
*/
public abstract getOutputTypes(): Promise<ValuePreferences>;
public async getOutputTypes(): Promise<ValuePreferences> {
return this.outputTypes;
}
/**
* Verifies whether this converter supports the input.