Skip to content

Commit

Permalink
fix: Take baseIRI into account when calling parseQuads
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimvh committed Jan 7, 2021
1 parent 5995057 commit fea726a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 17 deletions.
14 changes: 8 additions & 6 deletions src/storage/DataAccessorBasedStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ export class DataAccessorBasedStore implements ResourceStore {
*/
protected async writeData(identifier: ResourceIdentifier, representation: Representation, isContainer: boolean,
createContainers?: boolean): Promise<void> {
// Make sure the metadata has the correct identifier and correct type quads
// Need to do this before handling container data to have the correct identifier
const { metadata } = representation;
metadata.identifier = DataFactory.namedNode(identifier.path);
metadata.addQuads(generateResourceQuads(metadata.identifier, isContainer));

if (isContainer) {
await this.handleContainerData(representation);
}
Expand All @@ -228,11 +234,6 @@ export class DataAccessorBasedStore implements ResourceStore {
await this.createRecursiveContainers(this.identifierStrategy.getParentContainer(identifier));
}

// Make sure the metadata has the correct identifier and correct type quads
const { metadata } = representation;
metadata.identifier = DataFactory.namedNode(identifier.path);
metadata.addQuads(generateResourceQuads(metadata.identifier, isContainer));

await (isContainer ?
this.accessor.writeContainer(identifier, representation.metadata) :
this.accessor.writeDocument(identifier, representation.data, representation.metadata));
Expand All @@ -251,7 +252,8 @@ export class DataAccessorBasedStore implements ResourceStore {
if (representation.metadata.contentType === INTERNAL_QUADS) {
quads = await arrayifyStream(representation.data);
} else {
quads = await parseQuads(representation.data, representation.metadata.contentType);
const { contentType, identifier } = representation.metadata;
quads = await parseQuads(representation.data, { format: contentType, baseIRI: identifier.value });
}
} catch (error: unknown) {
if (error instanceof Error) {
Expand Down
2 changes: 1 addition & 1 deletion src/storage/accessors/FileDataAccessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export class FileDataAccessor implements DataAccessor {
await fsPromises.lstat(metadataLink.filePath);

const readMetadataStream = guardStream(createReadStream(metadataLink.filePath));
return await parseQuads(readMetadataStream, metadataLink.contentType);
return await parseQuads(readMetadataStream, { format: metadataLink.contentType, baseIRI: identifier.path });
} catch (error: unknown) {
// Metadata file doesn't exist so lets keep `rawMetaData` an empty array.
if (!isSystemError(error) || error.code !== 'ENOENT') {
Expand Down
7 changes: 4 additions & 3 deletions src/util/QuadUtil.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Readable, PassThrough } from 'stream';
import arrayifyStream from 'arrayify-stream';
import type { ParserOptions } from 'n3';
import { DataFactory, StreamParser, StreamWriter } from 'n3';
import type { Literal, NamedNode, Quad } from 'rdf-js';
import streamifyArray from 'streamify-array';
Expand Down Expand Up @@ -33,10 +34,10 @@ export function serializeQuads(quads: Quad[], contentType?: string): Guarded<Rea
/**
* Helper function to convert a Readable into an array of quads.
* @param readable - The readable object.
* @param contentType - The content-type of the stream.
* @param options - Options for the parser.
*
* @returns A promise containing the array of quads.
*/
export async function parseQuads(readable: Guarded<Readable>, contentType?: string): Promise<Quad[]> {
return arrayifyStream(pipeSafely(readable, new StreamParser({ format: contentType })));
export async function parseQuads(readable: Guarded<Readable>, options: ParserOptions = {}): Promise<Quad[]> {
return arrayifyStream(pipeSafely(readable, new StreamParser(options)));
}
8 changes: 7 additions & 1 deletion test/unit/storage/DataAccessorBasedStore.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'jest-rdf';
import type { Readable } from 'stream';
import arrayifyStream from 'arrayify-stream';
import type { Quad } from 'n3';
import { DataFactory } from 'n3';
import type { Representation } from '../../../src/ldp/representation/Representation';
import { RepresentationMetadata } from '../../../src/ldp/representation/RepresentationMetadata';
Expand Down Expand Up @@ -187,13 +188,18 @@ describe('A DataAccessorBasedStore', (): void => {
const resourceID = { path: root };
representation.metadata.add(RDF.type, LDP.terms.Container);
representation.metadata.contentType = 'text/turtle';
representation.data = guardedStreamFrom([ `<${`${root}resource/`}> a <coolContainer>.` ]);
representation.data = guardedStreamFrom([ '<> a <http://test.com/coolContainer>.' ]);
const result = await store.addResource(resourceID, representation);
expect(result).toEqual({
path: expect.stringMatching(new RegExp(`^${root}[^/]+/$`, 'u')),
});
expect(accessor.data[result.path]).toBeTruthy();
expect(accessor.data[result.path].metadata.contentType).toBeUndefined();

const { data } = await store.getRepresentation(result);
const quads: Quad[] = await arrayifyStream(data);
expect(quads.some((entry): boolean => entry.subject.value === result.path &&
entry.object.value === 'http://test.com/coolContainer')).toBeTruthy();
});

it('creates a URI based on the incoming slug.', async(): Promise<void> => {
Expand Down
8 changes: 4 additions & 4 deletions test/unit/storage/accessors/FileDataAccessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ describe('A FileDataAccessor', (): void => {
});

it('adds stored metadata when requesting metadata.', async(): Promise<void> => {
cache.data = { resource: 'data', 'resource.meta': '<this> <is> <metadata>.' };
cache.data = { resource: 'data', 'resource.meta': '<http://this> <http://is> <http://metadata>.' };
metadata = await accessor.getMetadata({ path: `${base}resource` });
expect(metadata.quads().some((quad): boolean => quad.subject.value === 'this')).toBe(true);
expect(metadata.quads().some((quad): boolean => quad.subject.value === 'http://this')).toBe(true);

cache.data = { container: { '.meta': '<this> <is> <metadata>.' }};
cache.data = { container: { '.meta': '<http://this> <http://is> <http://metadata>.' }};
metadata = await accessor.getMetadata({ path: `${base}container/` });
expect(metadata.quads().some((quad): boolean => quad.subject.value === 'this')).toBe(true);
expect(metadata.quads().some((quad): boolean => quad.subject.value === 'http://this')).toBe(true);
});

it('throws an error if there is a problem with the internal metadata.', async(): Promise<void> => {
Expand Down
13 changes: 11 additions & 2 deletions test/unit/util/QuadUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,18 @@ describe('QuadUtil', (): void => {
});

describe('#parseQuads', (): void => {
it('parses quads from the requested format.', async(): Promise<void> => {
it('parses quads.', async(): Promise<void> => {
const stream = guardedStreamFrom([ '<pre:sub> <pre:pred> "obj".' ]);
await expect(parseQuads(stream, 'application/n-triples')).resolves.toEqualRdfQuadArray([ quad(
await expect(parseQuads(stream)).resolves.toEqualRdfQuadArray([ quad(
namedNode('pre:sub'),
namedNode('pre:pred'),
literal('obj'),
) ]);
});

it('parses quads with the given options.', async(): Promise<void> => {
const stream = guardedStreamFrom([ '<> <pre:pred> "obj".' ]);
await expect(parseQuads(stream, { baseIRI: 'pre:sub' })).resolves.toEqualRdfQuadArray([ quad(
namedNode('pre:sub'),
namedNode('pre:pred'),
literal('obj'),
Expand Down

0 comments on commit fea726a

Please sign in to comment.