Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/RdfParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ActionContext, Actor } from "@comunica/core";
import * as RDF from "@rdfjs/types";
import { Readable, PassThrough } from "readable-stream";
import { MediatorRdfParseHandle, MediatorRdfParseMediaTypes } from '@comunica/bus-rdf-parse';
import mediaMappings from "./mediaMappings";
import {mediaMappings} from "./mediaMappings";

/**
* An RdfParser can parse any RDF serialization, based on a given content type.
Expand Down
6 changes: 4 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { RdfParser } from './RdfParser';
import {RdfParser} from './RdfParser';

export * from "./RdfParser";
// tslint:disable:no-var-requires
export default <RdfParser> require('../engine-default');
const rdfParser = <RdfParser>require('../engine-default');
export {rdfParser};
5 changes: 3 additions & 2 deletions scripts/getMediaMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import rdfDereference from 'rdf-dereference';
import * as fs from 'fs';
import * as path from 'path';

const mediaMappings: Record<string, string> = (<any> rdfDereference.mediatorDereferenceRdf.bus).actors[0].mediaMappings;
const mediaMappings: Record<string, string> = (<any>rdfDereference.mediatorDereferenceRdf.bus).actors[0].mediaMappings;

fs.writeFileSync(
path.join(__dirname, '..', 'lib', 'mediaMappings.ts'),
// This json override is here for backwards compatibility and may
// be removed in a major version bump
// @see https://github.com/rubensworks/rdf-parse.js/pull/51#issuecomment-1439977639
`export default ${JSON.stringify({ ...mediaMappings, json: "application/ld+json" }, null, 2)};\n`
`const mediaMappings = ${JSON.stringify({...mediaMappings, json: "application/ld+json"}, null, 2)};
export {mediaMappings};\n`
);
53 changes: 27 additions & 26 deletions test/RdfParser-test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import "jest-rdf";
import {RdfParser} from "../lib/RdfParser";

import {rdfParser} from "..";

const stringToStream = require('streamify-string');
const arrayifyStream = require('arrayify-stream');
const quad = require('rdf-quad');
import { RdfParser } from "../lib/RdfParser";

import parser from "..";

describe('parser', () => {
it('should be an RdfParser instance', () => {
expect(parser).toBeInstanceOf(RdfParser);
expect(rdfParser).toBeInstanceOf(RdfParser);
});

it('should get all content types', async () => {
expect((await parser.getContentTypes()).sort()).toEqual([
expect((await rdfParser.getContentTypes()).sort()).toEqual([
'application/ld+json',
'application/json',
'text/html',
Expand All @@ -32,7 +33,7 @@ describe('parser', () => {
});

it('should get all prioritized content types', async () => {
expect(await parser.getContentTypesPrioritized()).toEqual({
expect(await rdfParser.getContentTypesPrioritized()).toEqual({
'application/json': 0.45,
'application/ld+json': 0.9,
'application/n-quads': 1,
Expand All @@ -55,31 +56,31 @@ describe('parser', () => {
const stream = stringToStream(`
<http://ex.org/s> <http://ex.org/p> <http://ex.org/o1>, <http://ex.org/o2>.
`);
return expect(() => parser.parse(stream, <any>{}))
return expect(() => rdfParser.parse(stream, <any>{}))
.toThrow(new Error('Missing \'contentType\' or \'path\' option while parsing.'));
});

it('should fail to parse with path without extension', () => {
const stream = stringToStream(`
<http://ex.org/s> <http://ex.org/p> <http://ex.org/o1>, <http://ex.org/o2>.
`);
return expect(() => parser.parse(stream, { path: 'abc' }))
return expect(() => rdfParser.parse(stream, {path: 'abc'}))
.toThrow(new Error('No valid extension could be detected from the given \'path\' option: \'abc\''));
});

it('should fail to parse with path with unknown extension', () => {
const stream = stringToStream(`
<http://ex.org/s> <http://ex.org/p> <http://ex.org/o1>, <http://ex.org/o2>.
`);
return expect(() => parser.parse(stream, { path: 'abc.unknown' }))
return expect(() => rdfParser.parse(stream, {path: 'abc.unknown'}))
.toThrow(new Error('No valid extension could be detected from the given \'path\' option: \'abc.unknown\''));
});

it('should parse text/turtle without baseIRI', () => {
const stream = stringToStream(`
<http://ex.org/s> <http://ex.org/p> <http://ex.org/o1>, <http://ex.org/o2>.
`);
return expect(arrayifyStream(parser.parse(stream, { contentType: 'text/turtle' }))).resolves.toBeRdfIsomorphic([
return expect(arrayifyStream(rdfParser.parse(stream, {contentType: 'text/turtle'}))).resolves.toBeRdfIsomorphic([
quad('http://ex.org/s', 'http://ex.org/p', 'http://ex.org/o1'),
quad('http://ex.org/s', 'http://ex.org/p', 'http://ex.org/o2'),
]);
Expand All @@ -89,7 +90,7 @@ describe('parser', () => {
const stream = stringToStream(`
<http://ex.org/s> <http://ex.org/p> <http://ex.org/o1>, <http://ex.org/o2>.
`);
return expect(arrayifyStream(parser.parse(stream, { path: 'myfile.ttl' }))).resolves.toBeRdfIsomorphic([
return expect(arrayifyStream(rdfParser.parse(stream, {path: 'myfile.ttl'}))).resolves.toBeRdfIsomorphic([
quad('http://ex.org/s', 'http://ex.org/p', 'http://ex.org/o1'),
quad('http://ex.org/s', 'http://ex.org/p', 'http://ex.org/o2'),
]);
Expand All @@ -99,7 +100,7 @@ describe('parser', () => {
const stream = stringToStream(`
<s> <p> <o1>, <o2>.
`);
return expect(arrayifyStream(parser.parse(stream, { contentType: 'text/turtle', baseIRI: 'http://ex.org/' })))
return expect(arrayifyStream(rdfParser.parse(stream, {contentType: 'text/turtle', baseIRI: 'http://ex.org/'})))
.resolves.toBeRdfIsomorphic([
quad('http://ex.org/s', 'http://ex.org/p', 'http://ex.org/o1'),
quad('http://ex.org/s', 'http://ex.org/p', 'http://ex.org/o2'),
Expand All @@ -110,7 +111,7 @@ describe('parser', () => {
const stream = stringToStream(`
<s> <p> <o1>,
`);
return expect(arrayifyStream(parser.parse(stream, { contentType: 'text/turtle' })))
return expect(arrayifyStream(rdfParser.parse(stream, {contentType: 'text/turtle'})))
.rejects.toThrow(new Error('Expected entity but got eof on line 3.'));
});

Expand All @@ -125,7 +126,7 @@ describe('parser', () => {
}
`);
const contexts: string[] = [];
const result = await arrayifyStream(parser.parse(stream, { contentType: 'application/ld+json' })
const result = await arrayifyStream(rdfParser.parse(stream, {contentType: 'application/ld+json'})
.on('context', (context => contexts.push(context))));

expect(result).toBeRdfIsomorphic([]);
Expand All @@ -142,7 +143,7 @@ describe('parser', () => {
"url": ""
}
`);
return expect(arrayifyStream(parser.parse(stream, { contentType: 'application/ld+json' }))).resolves
return expect(arrayifyStream(rdfParser.parse(stream, {contentType: 'application/ld+json'}))).resolves
.toBeRdfIsomorphic([]);
});

Expand All @@ -157,8 +158,8 @@ describe('parser', () => {
"url": ""
}
`);
return expect(arrayifyStream(parser
.parse(stream, { contentType: 'application/ld+json', baseIRI: 'http://ex.org/' })))
return expect(arrayifyStream(rdfParser
.parse(stream, {contentType: 'application/ld+json', baseIRI: 'http://ex.org/'})))
.resolves.toBeRdfIsomorphic([
quad('http://ex.org/', 'http://schema.org/name', '"Jane Doe"'),
quad('http://ex.org/', 'http://schema.org/url', 'http://ex.org/'),
Expand All @@ -176,8 +177,8 @@ describe('parser', () => {
"url": ""
}
`);
return expect(arrayifyStream(parser
.parse(stream, { path: 'myfile.json', baseIRI: 'http://ex.org/' })))
return expect(arrayifyStream(rdfParser
.parse(stream, {path: 'myfile.json', baseIRI: 'http://ex.org/'})))
.resolves.toBeRdfIsomorphic([
quad('http://ex.org/', 'http://schema.org/name', '"Jane Doe"'),
quad('http://ex.org/', 'http://schema.org/url', 'http://ex.org/'),
Expand All @@ -189,13 +190,13 @@ describe('parser', () => {
const stream = stringToStream(`
<s> <p> <o1>,
`);
return expect(arrayifyStream(parser.parse(stream, { contentType: 'application/ld+json' })))
return expect(arrayifyStream(rdfParser.parse(stream, {contentType: 'application/ld+json'})))
.rejects.toThrow(new Error('Unexpected "s" at position 2 in state STOP'));
});

it('should fail to parse an unknown content type', () => {
const stream = stringToStream(``);
return expect(arrayifyStream(parser.parse(stream, { contentType: 'unknown' })))
return expect(arrayifyStream(rdfParser.parse(stream, {contentType: 'unknown'})))
.rejects.toBeTruthy();
});

Expand All @@ -207,8 +208,8 @@ describe('parser', () => {
</head>
</html>
`);
return expect(arrayifyStream(parser
.parse(stream, { contentType: 'text/html', baseIRI: 'http://ex.org/' })))
return expect(arrayifyStream(rdfParser
.parse(stream, {contentType: 'text/html', baseIRI: 'http://ex.org/'})))
.resolves.toBeRdfIsomorphic([
quad('http://ex.org/', 'http://schema.org/name', '"Title"'),
]);
Expand All @@ -233,7 +234,7 @@ describe('parser', () => {
foaf:name "Spiderman", "Человек-паук"@ru .`
const stream = stringToStream(turtle);
const prefixes: Record<string, string> = {};
const result = await arrayifyStream(parser.parse(stream, { path: 'myfile.ttl' })
const result = await arrayifyStream(rdfParser.parse(stream, {path: 'myfile.ttl'})
.on('prefix', (prefix, iri) => prefixes[prefix] = iri.value));
expect(result).toBeRdfIsomorphic([
quad('http://example.org/#green-goblin', 'http://www.perceive.net/schemas/relationship/enemyOf', 'http://example.org/#spiderman'),
Expand All @@ -259,7 +260,7 @@ describe('parser', () => {
const stream = stringToStream(`
<s> <p> <o1>, <o2>.
`);
return expect(arrayifyStream(parser.parse(stream, { contentType: 'text/turtle', baseIRI: 'http://ex.org/' })))
return expect(arrayifyStream(rdfParser.parse(stream, {contentType: 'text/turtle', baseIRI: 'http://ex.org/'})))
.resolves.toBeRdfIsomorphic([
quad('http://ex.org/s', 'http://ex.org/p', 'http://ex.org/o1'),
quad('http://ex.org/s', 'http://ex.org/p', 'http://ex.org/o2'),
Expand All @@ -273,7 +274,7 @@ describe('parser', () => {

shape cont:ContactsShape {}
`);
return expect(arrayifyStream(parser.parse(stream, { contentType: 'text/shaclc' })))
return expect(arrayifyStream(rdfParser.parse(stream, {contentType: 'text/shaclc'})))
.resolves.toBeRdfIsomorphic([
quad("http://localhost:3002/ContactsShape#ContactsShape", "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", "http://www.w3.org/ns/shacl#NodeShape"),
quad("http://localhost:3002/ContactsShape", "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", "http://www.w3.org/2002/07/owl#Ontology"),
Expand Down