Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
163 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^1.0.0/components/context.jsonld", | ||
"@graph": [ | ||
{ | ||
"@id": "urn:solid-server:default:MetadataExtractor", | ||
"@type": "BasicMetadataExtractor", | ||
"BasicMetadataExtractor:_parsers": [ | ||
{ | ||
"@type": "ContentTypeParser" | ||
}, | ||
{ | ||
"@type": "LinkTypeParser" | ||
}, | ||
{ | ||
"@type": "SlugParser" | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,22 @@ | ||
import type { HttpRequest } from '../../server/HttpRequest'; | ||
import { AsyncHandler } from '../../util/AsyncHandler'; | ||
import type { Representation } from '../representation/Representation'; | ||
import type { RepresentationMetadata } from '../representation/RepresentationMetadata'; | ||
|
||
export interface BodyParserArgs { | ||
/** | ||
* Request that contains the (potential) body. | ||
*/ | ||
request: HttpRequest; | ||
/** | ||
* Metadata that has already been parsed from the request. | ||
* Can be updated by the BodyParser with extra metadata. | ||
*/ | ||
metadata: RepresentationMetadata; | ||
} | ||
|
||
/** | ||
* Parses the body of an incoming {@link HttpRequest} and converts it to a {@link Representation}. | ||
*/ | ||
export abstract class BodyParser extends AsyncHandler<HttpRequest, Representation | undefined> {} | ||
export abstract class BodyParser extends | ||
AsyncHandler<BodyParserArgs, Representation | undefined> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,50 @@ | ||
import { PassThrough } from 'stream'; | ||
import type { Algebra } from 'sparqlalgebrajs'; | ||
import { translate } from 'sparqlalgebrajs'; | ||
import type { HttpRequest } from '../../server/HttpRequest'; | ||
import { APPLICATION_SPARQL_UPDATE } from '../../util/ContentTypes'; | ||
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError'; | ||
import { UnsupportedMediaTypeHttpError } from '../../util/errors/UnsupportedMediaTypeHttpError'; | ||
import { CONTENT_TYPE } from '../../util/UriConstants'; | ||
import { readableToString } from '../../util/Util'; | ||
import { RepresentationMetadata } from '../representation/RepresentationMetadata'; | ||
import { pipeStreamsAndErrors, readableToString } from '../../util/Util'; | ||
import type { BodyParserArgs } from './BodyParser'; | ||
import { BodyParser } from './BodyParser'; | ||
import type { SparqlUpdatePatch } from './SparqlUpdatePatch'; | ||
|
||
/** | ||
* {@link BodyParser} that supports `application/sparql-update` content. | ||
* Will convert the incoming update string to algebra in a {@link SparqlUpdatePatch}. | ||
* Still needs access to a handler for parsing metadata. | ||
*/ | ||
export class SparqlUpdateBodyParser extends BodyParser { | ||
public async canHandle(input: HttpRequest): Promise<void> { | ||
if (input.headers['content-type'] !== APPLICATION_SPARQL_UPDATE) { | ||
public async canHandle({ request }: BodyParserArgs): Promise<void> { | ||
if (request.headers['content-type'] !== APPLICATION_SPARQL_UPDATE) { | ||
throw new UnsupportedMediaTypeHttpError('This parser only supports SPARQL UPDATE data.'); | ||
} | ||
} | ||
|
||
public async handle(input: HttpRequest): Promise<SparqlUpdatePatch> { | ||
public async handle({ request, metadata }: BodyParserArgs): Promise<SparqlUpdatePatch> { | ||
// 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: request.readableObjectMode }; | ||
const toAlgebraStream = new PassThrough(options); | ||
const dataCopy = new PassThrough(options); | ||
pipeStreamsAndErrors(request, toAlgebraStream); | ||
pipeStreamsAndErrors(request, dataCopy); | ||
let algebra: Algebra.Operation; | ||
try { | ||
// 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 }); | ||
|
||
const metadata = new RepresentationMetadata({ [CONTENT_TYPE]: APPLICATION_SPARQL_UPDATE }); | ||
|
||
// Prevent body from being requested again | ||
return { | ||
algebra, | ||
binary: true, | ||
data: dataCopy, | ||
metadata, | ||
}; | ||
algebra = translate(sparql, { quads: true }); | ||
} catch (error: unknown) { | ||
if (error instanceof Error) { | ||
throw new UnsupportedHttpError(error.message); | ||
} | ||
throw new UnsupportedHttpError(); | ||
} | ||
|
||
// Prevent body from being requested again | ||
return { | ||
algebra, | ||
binary: true, | ||
data: dataCopy, | ||
metadata, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.