fix: Take baseIRI into account when calling parseQuads

This commit is contained in:
Joachim Van Herwegen
2021-01-06 17:19:57 +01:00
parent 5995057240
commit fea726ae7d
6 changed files with 35 additions and 17 deletions

View File

@@ -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);
}
@@ -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));
@@ -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) {

View File

@@ -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') {

View File

@@ -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';
@@ -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)));
}