mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
refactor: Remove Turtle to Quad and Quad to Turtle converters
This commit is contained in:
parent
f6f45c0ece
commit
d8e6c08859
2
index.ts
2
index.ts
@ -74,10 +74,8 @@ export * from './src/server/HttpResponse';
|
||||
// Storage/Conversion
|
||||
export * from './src/storage/conversion/ChainedConverter';
|
||||
export * from './src/storage/conversion/QuadToRdfConverter';
|
||||
export * from './src/storage/conversion/QuadToTurtleConverter';
|
||||
export * from './src/storage/conversion/RdfToQuadConverter';
|
||||
export * from './src/storage/conversion/RepresentationConverter';
|
||||
export * from './src/storage/conversion/TurtleToQuadConverter';
|
||||
export * from './src/storage/conversion/TypedRepresentationConverter';
|
||||
|
||||
// Storage/Patch
|
||||
|
@ -1,30 +0,0 @@
|
||||
import { StreamWriter } from 'n3';
|
||||
import type { Representation } from '../../ldp/representation/Representation';
|
||||
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
|
||||
import { INTERNAL_QUADS, TEXT_TURTLE } from '../../util/ContentTypes';
|
||||
import { CONTENT_TYPE } from '../../util/UriConstants';
|
||||
import { checkRequest } from './ConversionUtil';
|
||||
import type { RepresentationConverterArgs } from './RepresentationConverter';
|
||||
import { RepresentationConverter } from './RepresentationConverter';
|
||||
|
||||
/**
|
||||
* Converts `internal/quads` to `text/turtle`.
|
||||
*/
|
||||
export class QuadToTurtleConverter extends RepresentationConverter {
|
||||
public async canHandle(input: RepresentationConverterArgs): Promise<void> {
|
||||
checkRequest(input, [ INTERNAL_QUADS ], [ TEXT_TURTLE ]);
|
||||
}
|
||||
|
||||
public async handle(input: RepresentationConverterArgs): Promise<Representation> {
|
||||
return this.quadsToTurtle(input.representation);
|
||||
}
|
||||
|
||||
private quadsToTurtle(quads: Representation): Representation {
|
||||
const metadata = new RepresentationMetadata(quads.metadata, { [CONTENT_TYPE]: TEXT_TURTLE });
|
||||
return {
|
||||
binary: true,
|
||||
data: quads.data.pipe(new StreamWriter({ format: TEXT_TURTLE })),
|
||||
metadata,
|
||||
};
|
||||
}
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
import { PassThrough } from 'stream';
|
||||
import { StreamParser } from 'n3';
|
||||
import type { 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/UriConstants';
|
||||
import { checkRequest } from './ConversionUtil';
|
||||
import type { RepresentationConverterArgs } from './RepresentationConverter';
|
||||
import { RepresentationConverter } from './RepresentationConverter';
|
||||
|
||||
/**
|
||||
* Converts `text/turtle` to `internal/quads`.
|
||||
*/
|
||||
export class TurtleToQuadConverter extends RepresentationConverter {
|
||||
public async canHandle(input: RepresentationConverterArgs): Promise<void> {
|
||||
checkRequest(input, [ TEXT_TURTLE ], [ INTERNAL_QUADS ]);
|
||||
}
|
||||
|
||||
public async handle(input: RepresentationConverterArgs): Promise<Representation> {
|
||||
return this.turtleToQuads(input.representation, input.identifier.path);
|
||||
}
|
||||
|
||||
private turtleToQuads(turtle: Representation, baseIRI: string): Representation {
|
||||
const metadata = new RepresentationMetadata(turtle.metadata, { [CONTENT_TYPE]: INTERNAL_QUADS });
|
||||
|
||||
// Catch parsing errors and emit correct error
|
||||
// Node 10 requires both writableObjectMode and readableObjectMode
|
||||
const errorStream = new PassThrough({ writableObjectMode: true, readableObjectMode: true });
|
||||
const data = turtle.data.pipe(new StreamParser({ format: TEXT_TURTLE, baseIRI }));
|
||||
data.pipe(errorStream);
|
||||
data.on('error', (error): boolean => errorStream.emit('error', new UnsupportedHttpError(error.message)));
|
||||
|
||||
return {
|
||||
binary: false,
|
||||
data: errorStream,
|
||||
metadata,
|
||||
};
|
||||
}
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
import { namedNode, triple } from '@rdfjs/data-model';
|
||||
import arrayifyStream from 'arrayify-stream';
|
||||
import streamifyArray from 'streamify-array';
|
||||
import type { Representation } from '../../../../src/ldp/representation/Representation';
|
||||
import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata';
|
||||
import type { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences';
|
||||
import type { 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/UriConstants';
|
||||
|
||||
describe('A QuadToTurtleConverter', (): void => {
|
||||
const converter = new QuadToTurtleConverter();
|
||||
const identifier: ResourceIdentifier = { path: 'path' };
|
||||
const metadata = new RepresentationMetadata({ [CONTENT_TYPE]: INTERNAL_QUADS });
|
||||
|
||||
it('can handle quad to turtle conversions.', async(): Promise<void> => {
|
||||
const representation = { metadata } as Representation;
|
||||
const preferences: RepresentationPreferences = { type: [{ value: 'text/turtle', weight: 1 }]};
|
||||
await expect(converter.canHandle({ identifier, representation, preferences })).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('converts quads to turtle.', async(): Promise<void> => {
|
||||
const representation = {
|
||||
data: streamifyArray([ triple(
|
||||
namedNode('http://test.com/s'),
|
||||
namedNode('http://test.com/p'),
|
||||
namedNode('http://test.com/o'),
|
||||
) ]),
|
||||
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: expect.any(RepresentationMetadata),
|
||||
});
|
||||
expect(result.metadata.contentType).toEqual('text/turtle');
|
||||
await expect(arrayifyStream(result.data)).resolves.toContain(
|
||||
'<http://test.com/s> <http://test.com/p> <http://test.com/o>',
|
||||
);
|
||||
});
|
||||
});
|
@ -1,60 +0,0 @@
|
||||
import { Readable } from 'stream';
|
||||
import { namedNode, triple } from '@rdfjs/data-model';
|
||||
import arrayifyStream from 'arrayify-stream';
|
||||
import streamifyArray from 'streamify-array';
|
||||
import type { Representation } from '../../../../src/ldp/representation/Representation';
|
||||
import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata';
|
||||
import type { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences';
|
||||
import type { 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/UriConstants';
|
||||
|
||||
describe('A TurtleToQuadConverter', (): void => {
|
||||
const converter = new TurtleToQuadConverter();
|
||||
const identifier: ResourceIdentifier = { path: 'path' };
|
||||
const metadata = new RepresentationMetadata({ [CONTENT_TYPE]: 'text/turtle' });
|
||||
|
||||
it('can handle turtle to quad conversions.', async(): Promise<void> => {
|
||||
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 representation = {
|
||||
data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]),
|
||||
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: expect.any(RepresentationMetadata),
|
||||
});
|
||||
expect(result.metadata.contentType).toEqual(INTERNAL_QUADS);
|
||||
await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray([ triple(
|
||||
namedNode('http://test.com/s'),
|
||||
namedNode('http://test.com/p'),
|
||||
namedNode('http://test.com/o'),
|
||||
) ]);
|
||||
});
|
||||
|
||||
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,
|
||||
} 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: expect.any(RepresentationMetadata),
|
||||
});
|
||||
expect(result.metadata.contentType).toEqual(INTERNAL_QUADS);
|
||||
await expect(arrayifyStream(result.data)).rejects.toThrow(UnsupportedHttpError);
|
||||
});
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user