mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
refactor: Simplify TypedRepresentationConverter contruction.
This commit is contained in:
parent
27a115022b
commit
27a5711ec2
@ -1,4 +1,5 @@
|
|||||||
[
|
[
|
||||||
"Error",
|
"Error",
|
||||||
"EventEmitter"
|
"EventEmitter",
|
||||||
|
"ValuePreferencesArg"
|
||||||
]
|
]
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import type { Representation } from '../../ldp/representation/Representation';
|
import type { Representation } from '../../ldp/representation/Representation';
|
||||||
import type { ValuePreferences } from '../../ldp/representation/RepresentationPreferences';
|
|
||||||
import { getLoggerFor } from '../../logging/LogUtil';
|
import { getLoggerFor } from '../../logging/LogUtil';
|
||||||
import { matchesMediaType } from './ConversionUtil';
|
import { matchesMediaType } from './ConversionUtil';
|
||||||
import type { RepresentationConverterArgs } from './RepresentationConverter';
|
import type { RepresentationConverterArgs } from './RepresentationConverter';
|
||||||
@ -25,6 +24,8 @@ export class ChainedConverter extends TypedRepresentationConverter {
|
|||||||
throw new Error('At least 2 converters are required.');
|
throw new Error('At least 2 converters are required.');
|
||||||
}
|
}
|
||||||
this.converters = [ ...converters ];
|
this.converters = [ ...converters ];
|
||||||
|
this.inputTypes = this.first.getInputTypes();
|
||||||
|
this.outputTypes = this.last.getOutputTypes();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected get first(): TypedRepresentationConverter {
|
protected get first(): TypedRepresentationConverter {
|
||||||
@ -35,14 +36,6 @@ export class ChainedConverter extends TypedRepresentationConverter {
|
|||||||
return this.converters[this.converters.length - 1];
|
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> {
|
public async handle(input: RepresentationConverterArgs): Promise<Representation> {
|
||||||
const args = { ...input };
|
const args = { ...input };
|
||||||
for (let i = 0; i < this.converters.length - 1; ++i) {
|
for (let i = 0; i < this.converters.length - 1; ++i) {
|
||||||
|
@ -3,10 +3,7 @@ import { StreamWriter } from 'n3';
|
|||||||
import rdfSerializer from 'rdf-serialize';
|
import rdfSerializer from 'rdf-serialize';
|
||||||
import type { Representation } from '../../ldp/representation/Representation';
|
import type { Representation } from '../../ldp/representation/Representation';
|
||||||
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
|
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
|
||||||
import type {
|
import type { ValuePreferences } from '../../ldp/representation/RepresentationPreferences';
|
||||||
ValuePreferences,
|
|
||||||
RepresentationPreferences,
|
|
||||||
} from '../../ldp/representation/RepresentationPreferences';
|
|
||||||
import { INTERNAL_QUADS } from '../../util/ContentTypes';
|
import { INTERNAL_QUADS } from '../../util/ContentTypes';
|
||||||
import { guardStream } from '../../util/GuardedStream';
|
import { guardStream } from '../../util/GuardedStream';
|
||||||
import { pipeSafely } from '../../util/StreamUtil';
|
import { pipeSafely } from '../../util/StreamUtil';
|
||||||
@ -22,26 +19,14 @@ export class QuadToRdfConverter extends TypedRepresentationConverter {
|
|||||||
private readonly outputPreferences?: ValuePreferences;
|
private readonly outputPreferences?: ValuePreferences;
|
||||||
|
|
||||||
public constructor(options: { outputPreferences?: Record<string, number> } = {}) {
|
public constructor(options: { outputPreferences?: Record<string, number> } = {}) {
|
||||||
super();
|
super(
|
||||||
if (Object.keys(options.outputPreferences ?? {}).length > 0) {
|
INTERNAL_QUADS,
|
||||||
this.outputPreferences = { ...options.outputPreferences };
|
options.outputPreferences ?? rdfSerializer.getContentTypesPrioritized(),
|
||||||
}
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getInputTypes(): Promise<ValuePreferences> {
|
public async handle({ representation: quads, preferences }: RepresentationConverterArgs): Promise<Representation> {
|
||||||
return { [INTERNAL_QUADS]: 1 };
|
const contentType = matchingMediaTypes(preferences.type, await this.getOutputTypes())[0];
|
||||||
}
|
|
||||||
|
|
||||||
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];
|
|
||||||
const metadata = new RepresentationMetadata(quads.metadata, { [CONTENT_TYPE]: contentType });
|
const metadata = new RepresentationMetadata(quads.metadata, { [CONTENT_TYPE]: contentType });
|
||||||
let data: Readable;
|
let data: Readable;
|
||||||
|
|
||||||
|
@ -2,7 +2,6 @@ import { PassThrough } from 'stream';
|
|||||||
import rdfParser from 'rdf-parse';
|
import rdfParser from 'rdf-parse';
|
||||||
import type { Representation } from '../../ldp/representation/Representation';
|
import type { Representation } from '../../ldp/representation/Representation';
|
||||||
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
|
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
|
||||||
import type { ValuePreferences } from '../../ldp/representation/RepresentationPreferences';
|
|
||||||
import { INTERNAL_QUADS } from '../../util/ContentTypes';
|
import { INTERNAL_QUADS } from '../../util/ContentTypes';
|
||||||
import { BadRequestHttpError } from '../../util/errors/BadRequestHttpError';
|
import { BadRequestHttpError } from '../../util/errors/BadRequestHttpError';
|
||||||
import { pipeSafely } from '../../util/StreamUtil';
|
import { pipeSafely } from '../../util/StreamUtil';
|
||||||
@ -14,23 +13,15 @@ import { TypedRepresentationConverter } from './TypedRepresentationConverter';
|
|||||||
* Converts most major RDF serializations to `internal/quads`.
|
* Converts most major RDF serializations to `internal/quads`.
|
||||||
*/
|
*/
|
||||||
export class RdfToQuadConverter extends TypedRepresentationConverter {
|
export class RdfToQuadConverter extends TypedRepresentationConverter {
|
||||||
public async getInputTypes(): Promise<ValuePreferences> {
|
public constructor() {
|
||||||
return rdfParser.getContentTypesPrioritized();
|
super(rdfParser.getContentTypesPrioritized(), INTERNAL_QUADS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getOutputTypes(): Promise<ValuePreferences> {
|
public async handle({ representation, identifier }: RepresentationConverterArgs): Promise<Representation> {
|
||||||
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 {
|
|
||||||
const metadata = new RepresentationMetadata(representation.metadata, { [CONTENT_TYPE]: INTERNAL_QUADS });
|
const metadata = new RepresentationMetadata(representation.metadata, { [CONTENT_TYPE]: INTERNAL_QUADS });
|
||||||
const rawQuads = rdfParser.parse(representation.data, {
|
const rawQuads = rdfParser.parse(representation.data, {
|
||||||
contentType: representation.metadata.contentType!,
|
contentType: representation.metadata.contentType!,
|
||||||
baseIRI,
|
baseIRI: identifier.path,
|
||||||
});
|
});
|
||||||
|
|
||||||
const pass = new PassThrough({ objectMode: true });
|
const pass = new PassThrough({ objectMode: true });
|
||||||
|
@ -3,19 +3,49 @@ import { supportsMediaTypeConversion } from './ConversionUtil';
|
|||||||
import { RepresentationConverter } from './RepresentationConverter';
|
import { RepresentationConverter } from './RepresentationConverter';
|
||||||
import type { RepresentationConverterArgs } 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.
|
* A {@link RepresentationConverter} that allows requesting the supported types.
|
||||||
*/
|
*/
|
||||||
export abstract class TypedRepresentationConverter extends RepresentationConverter {
|
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.
|
* 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.
|
* 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.
|
* Verifies whether this converter supports the input.
|
||||||
|
@ -24,18 +24,12 @@ describe('A QuadToRdfConverter', (): void => {
|
|||||||
.resolves.toEqual({ [INTERNAL_QUADS]: 1 });
|
.resolves.toEqual({ [INTERNAL_QUADS]: 1 });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('defaults to rdfSerializer preferences when given no preferences.', async(): Promise<void> => {
|
it('defaults to rdfSerializer preferences when given no output preferences.', async(): Promise<void> => {
|
||||||
await expect(new QuadToRdfConverter().getOutputTypes())
|
await expect(new QuadToRdfConverter().getOutputTypes())
|
||||||
.resolves.toEqual(await rdfSerializer.getContentTypesPrioritized());
|
.resolves.toEqual(await rdfSerializer.getContentTypesPrioritized());
|
||||||
});
|
});
|
||||||
|
|
||||||
it('defaults to rdfSerializer preferences when given empty preferences.', async(): Promise<void> => {
|
it('supports overriding output preferences.', async(): Promise<void> => {
|
||||||
const outputPreferences = {};
|
|
||||||
await expect(new QuadToRdfConverter({ outputPreferences }).getOutputTypes())
|
|
||||||
.resolves.toEqual(await rdfSerializer.getContentTypesPrioritized());
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns custom preferences when given non-empty preferences.', async(): Promise<void> => {
|
|
||||||
const outputPreferences = { 'text/turtle': 1 };
|
const outputPreferences = { 'text/turtle': 1 };
|
||||||
await expect(new QuadToRdfConverter({ outputPreferences }).getOutputTypes())
|
await expect(new QuadToRdfConverter({ outputPreferences }).getOutputTypes())
|
||||||
.resolves.toEqual(outputPreferences);
|
.resolves.toEqual(outputPreferences);
|
||||||
|
@ -0,0 +1,47 @@
|
|||||||
|
import { TypedRepresentationConverter } from '../../../../src/storage/conversion/TypedRepresentationConverter';
|
||||||
|
|
||||||
|
class CustomTypedRepresentationConverter extends TypedRepresentationConverter {
|
||||||
|
public handle = jest.fn();
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('A TypedRepresentationConverter', (): void => {
|
||||||
|
it('defaults to allowing everything.', async(): Promise<void> => {
|
||||||
|
const converter = new CustomTypedRepresentationConverter();
|
||||||
|
await expect(converter.getInputTypes()).resolves.toEqual({
|
||||||
|
});
|
||||||
|
await expect(converter.getOutputTypes()).resolves.toEqual({
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('accepts strings.', async(): Promise<void> => {
|
||||||
|
const converter = new CustomTypedRepresentationConverter('a/b', 'c/d');
|
||||||
|
await expect(converter.getInputTypes()).resolves.toEqual({
|
||||||
|
'a/b': 1,
|
||||||
|
});
|
||||||
|
await expect(converter.getOutputTypes()).resolves.toEqual({
|
||||||
|
'c/d': 1,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('accepts string arrays.', async(): Promise<void> => {
|
||||||
|
const converter = new CustomTypedRepresentationConverter([ 'a/b', 'c/d' ], [ 'e/f', 'g/h' ]);
|
||||||
|
await expect(converter.getInputTypes()).resolves.toEqual({
|
||||||
|
'a/b': 1,
|
||||||
|
'c/d': 1,
|
||||||
|
});
|
||||||
|
await expect(converter.getOutputTypes()).resolves.toEqual({
|
||||||
|
'e/f': 1,
|
||||||
|
'g/h': 1,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('accepts records.', async(): Promise<void> => {
|
||||||
|
const converter = new CustomTypedRepresentationConverter({ 'a/b': 0.5 }, { 'c/d': 0.5 });
|
||||||
|
await expect(converter.getInputTypes()).resolves.toEqual({
|
||||||
|
'a/b': 0.5,
|
||||||
|
});
|
||||||
|
await expect(converter.getOutputTypes()).resolves.toEqual({
|
||||||
|
'c/d': 0.5,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user