Skip to content

Commit

Permalink
Detect and handle streaming profile in content type header
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Mar 28, 2020
1 parent 7b1efa1 commit 8132bd6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ const myParser = JsonLdParser.fromHttpResponse(
const quads = myParser.import(response.body);
```

The `Headers` object must implement the [Headers interface from the WHATWG Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Headers).
The `Headers` object must implement the [Headers interface from the WHATWG Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Headers).

This function will automatically detect the `http://www.w3.org/ns/json-ld#streaming` profile and set the `streamingProfile` flag.

## Configuration

Expand Down
11 changes: 11 additions & 0 deletions lib/JsonLdParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,20 @@ export class JsonLdParser extends Transform implements RDF.Sink<EventEmitter, RD
}
}

// Check if the streaming profile is present
let streamingProfile: boolean | undefined;
if (headers && headers.has('Content-Type')) {
const contentType = <string> headers.get('Content-Type');
const match = /; *profile=([^"]*)/.exec(contentType);
if (match && match[1] === 'http://www.w3.org/ns/json-ld#streaming') {
streamingProfile = true;
}
}

return new JsonLdParser({
baseIRI,
context,
streamingProfile,
... options ? options : {},
});
}
Expand Down
16 changes: 16 additions & 0 deletions test/JsonLdParser-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ describe('JsonLdParser', () => {
expect((<any> parser).options.context).toEqual('my-context.jsonld');
});

it('should handle on a JSON response with non-escaped link header', () => {
const parser = JsonLdParser.fromHttpResponse('BASE', 'application/json',
new Headers({ 'link': '<my-context.jsonld>; rel="http://www.w3.org/ns/json-ld#context"' }));
(<any> parser).parsingContext.rootContext.catch(() => { return; }); // Ignore context parsing errors
expect((<any> parser).options.baseIRI).toEqual('BASE');
expect((<any> parser).options.context).toEqual('my-context.jsonld');
});

it('should handle on a JSON extension type with link header', () => {
const parser = JsonLdParser.fromHttpResponse('BASE', 'text/turtle+json',
new Headers({ 'link': '<my-context.jsonld>; rel=\"http://www.w3.org/ns/json-ld#context\"' }));
Expand Down Expand Up @@ -99,6 +107,14 @@ describe('JsonLdParser', () => {
expect((<any> parser).options.baseIRI).toEqual('BASE');
expect((<any> parser).options.context).toEqual('my-context1.jsonld');
});

it('should handle a JSON-LD response with a streaming profile', () => {
const parser = JsonLdParser.fromHttpResponse('BASE', 'application/ld+json', new Headers({
'content-type': 'application/ld+json; profile=http://www.w3.org/ns/json-ld#streaming'
}));
expect((<any> parser).options.baseIRI).toEqual('BASE');
expect((<any> parser).options.streamingProfile).toEqual(true);
});
});

describe('when instantiated without a data factory and context', () => {
Expand Down

0 comments on commit 8132bd6

Please sign in to comment.