Skip to content

Commit

Permalink
fix: Fix problem with piping streams for PATCH requests
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimvh committed Feb 8, 2021
1 parent 73acb9c commit 6c4378a
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions src/ldp/http/SparqlUpdateBodyParser.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { PassThrough } from 'stream';
import type { Algebra } from 'sparqlalgebrajs';
import { translate } from 'sparqlalgebrajs';
import { getLoggerFor } from '../../logging/LogUtil';
import { APPLICATION_SPARQL_UPDATE } from '../../util/ContentTypes';
import { BadRequestHttpError } from '../../util/errors/BadRequestHttpError';
import { isNativeError } from '../../util/errors/ErrorUtil';
import { UnsupportedMediaTypeHttpError } from '../../util/errors/UnsupportedMediaTypeHttpError';
import { pipeSafely, readableToString } from '../../util/StreamUtil';
import { guardedStreamFrom, readableToString } from '../../util/StreamUtil';
import type { BodyParserArgs } from './BodyParser';
import { BodyParser } from './BodyParser';
import type { SparqlUpdatePatch } from './SparqlUpdatePatch';
Expand All @@ -25,12 +24,9 @@ export class SparqlUpdateBodyParser extends BodyParser {
}

public async handle({ request, metadata }: BodyParserArgs): Promise<SparqlUpdatePatch> {
const options = { objectMode: request.readableObjectMode };
const toAlgebraStream = pipeSafely(request, new PassThrough(options));
const dataCopy = pipeSafely(request, new PassThrough(options));
const sparql = await readableToString(request);
let algebra: Algebra.Operation;
try {
const sparql = await readableToString(toAlgebraStream);
algebra = translate(sparql, { quads: true, baseIRI: metadata.identifier.value });
} catch (error: unknown) {
this.logger.warn('Could not translate SPARQL query to SPARQL algebra', { error });
Expand All @@ -44,7 +40,7 @@ export class SparqlUpdateBodyParser extends BodyParser {
return {
algebra,
binary: true,
data: dataCopy,
data: guardedStreamFrom(sparql),
metadata,
};
}
Expand Down

0 comments on commit 6c4378a

Please sign in to comment.