fix: Make Patch more consistent with other Representations

This commit is contained in:
Joachim Van Herwegen
2020-08-04 10:05:48 +02:00
parent 14db5fed91
commit d6a35f9954
6 changed files with 24 additions and 18 deletions

View File

@@ -4,8 +4,4 @@ import { Representation } from '../representation/Representation';
* Represents the changes needed for a PATCH request.
*/
export interface Patch extends Representation {
/**
* The raw body of the PATCH request.
*/
raw: string;
}

View File

@@ -1,6 +1,7 @@
import { BodyParser } from './BodyParser';
import { DATA_TYPE_BINARY } from '../../util/ContentTypes';
import { HttpRequest } from '../../server/HttpRequest';
import { Readable } from 'stream';
import { PassThrough } from 'stream';
import { readableToString } from '../../util/Util';
import { SparqlUpdatePatch } from './SparqlUpdatePatch';
import { translate } from 'sparqlalgebrajs';
@@ -23,17 +24,21 @@ export class SimpleSparqlUpdateBodyParser extends BodyParser {
public async handle(input: HttpRequest): Promise<SparqlUpdatePatch> {
try {
const sparql = await readableToString(input);
// Note that readableObjectMode is only defined starting from Node 12
// It is impossible to check if object mode is enabled in Node 10 (without accessing private variables)
const options = { objectMode: input.readableObjectMode };
const toAlgebraStream = new PassThrough(options);
const dataCopy = new PassThrough(options);
input.pipe(toAlgebraStream);
input.pipe(dataCopy);
const sparql = await readableToString(toAlgebraStream);
const algebra = translate(sparql, { quads: true });
// Prevent body from being requested again
return {
algebra,
dataType: 'sparql-algebra',
raw: sparql,
get data(): Readable {
throw new Error('Body already parsed');
},
dataType: DATA_TYPE_BINARY,
data: dataCopy,
metadata: {
raw: [],
profiles: [],

View File

@@ -28,7 +28,7 @@ export class SimpleSparqlUpdatePatchHandler extends PatchHandler {
}
public async canHandle(input: {identifier: ResourceIdentifier; patch: SparqlUpdatePatch}): Promise<void> {
if (input.patch.dataType !== 'sparql-algebra' || !input.patch.algebra) {
if (typeof input.patch.algebra !== 'object') {
throw new UnsupportedHttpError('Only SPARQL update patch requests are supported.');
}
}