fix: metadata file error in FileResourceStore

* Fix: metadata file error in FileResourceStore

* fix: ensure full test coverage

* add stream piping function in util

* Fix typing in util function

* Add requested changes

* add suggested changes

* add suggested change

Co-authored-by: freyavs <freyavanspeybroeck@outlook.com>
This commit is contained in:
Joachim Van Herwegen
2020-09-04 09:12:35 +02:00
committed by GitHub
parent 7fae3203d5
commit c808dfeff0
5 changed files with 36 additions and 21 deletions

View File

@@ -6,6 +6,7 @@ import { NamedNode, Quad } from 'rdf-js';
import streamifyArray from 'streamify-array';
import { TEXT_TURTLE } from '../util/ContentTypes';
import { LDP, RDF, STAT, TERMS, XML } from './Prefixes';
import { pipeStreamsAndErrors } from './Util';
export const TYPE_PREDICATE = DataFactory.namedNode(`${RDF}type`);
export const MODIFIED_PREDICATE = DataFactory.namedNode(`${TERMS}modified`);
@@ -64,13 +65,13 @@ export class MetadataController {
}
/**
* Helper function to convert an array of quads into a Readable object.
* Helper function for serializing an array of quads, with as result a Readable object.
* @param quads - The array of quads.
*
* @returns The Readable object.
*/
public generateReadableFromQuads(quads: Quad[]): Readable {
return streamifyArray(quads).pipe(new StreamWriter({ format: TEXT_TURTLE }));
public serializeQuads(quads: Quad[]): Readable {
return pipeStreamsAndErrors(streamifyArray(quads), new StreamWriter({ format: TEXT_TURTLE }));
}
/**
@@ -79,7 +80,7 @@ export class MetadataController {
*
* @returns A promise containing the array of quads.
*/
public async generateQuadsFromReadable(readable: Readable): Promise<Quad[]> {
return arrayifyStream(readable.pipe(new StreamParser({ format: TEXT_TURTLE })));
public async parseQuads(readable: Readable): Promise<Quad[]> {
return await arrayifyStream(pipeStreamsAndErrors(readable, new StreamParser({ format: TEXT_TURTLE })));
}
}

View File

@@ -1,5 +1,6 @@
import { Readable } from 'stream';
import { Readable, Writable } from 'stream';
import arrayifyStream from 'arrayify-stream';
import { UnsupportedHttpError } from './errors/UnsupportedHttpError';
/**
* Makes sure the input path has exactly 1 slash at the end.
@@ -51,3 +52,17 @@ export const matchingMediaType = (mediaA: string, mediaB: string): boolean => {
}
return subTypeA === subTypeB;
};
/**
* Pipes one stream into another.
* Makes sure an error of the first stream gets passed to the second.
* @param readable - Initial readable stream.
* @param destination - The destination for writing data.
*
* @returns The destination stream.
*/
export const pipeStreamsAndErrors = <T extends Writable>(readable: Readable, destination: T): T => {
readable.pipe(destination);
readable.on('error', (error): boolean => destination.emit('error', new UnsupportedHttpError(error.message)));
return destination;
};