Skip to content

Commit cf8ee9e

Browse files
committed
Fix blank node prefixes in datasources not being relative to base
1 parent 6c53fe2 commit cf8ee9e

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

Diff for: packages/core/lib/UrlData.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class UrlData {
1111
this.baseURLPath = this.baseURL.substr(this.baseURLRoot.length);
1212
this.blankNodePath = this.baseURLRoot ? '/.well-known/genid/' : '';
1313
this.blankNodePrefix = this.blankNodePath ? this.baseURLRoot + this.blankNodePath : 'genid:';
14+
this.blankNodePrefixLength = this.blankNodePrefix.length;
1415
this.assetsPath = this.baseURLPath + 'assets/' || options.assetsPath;
1516
this.protocol = options.protocol;
1617
if (!this.protocol) {

Diff for: packages/core/lib/datasources/Datasource.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ class Datasource extends EventEmitter {
1414

1515
// Set the options
1616
options = options || {};
17-
let urlData = options.urlData || new UrlData();
17+
this.urlData = options.urlData || new UrlData();
1818
let path = (options.path || '').replace(/^\//, '');
19-
this._datasourcePath = urlData.baseURLPath + encodeURI(path);
20-
this._blankNodePrefix = urlData.blankNodePath || 'genid:';
19+
this._datasourcePath = this.urlData.baseURLPath + encodeURI(path);
2120
this._skolemizeBlacklist = options.skolemizeBlacklist || {};
2221
this.title = options.title;
2322
this.id = options.id;
@@ -27,14 +26,12 @@ class Datasource extends EventEmitter {
2726
this.hide = true;
2827
this.description = options.description;
2928
this.path = this._datasourcePath;
30-
this.url = urlData.baseURLRoot + this._datasourcePath + '#dataset';
29+
this.url = this.urlData.baseURLRoot + this._datasourcePath + '#dataset';
3130
this.license = options.license;
3231
this.licenseUrl = options.licenseUrl;
3332
this.copyright = options.copyright;
3433
this.homepage = options.homepage;
3534
this._request = options.request || require('request');
36-
this._blankNodePrefix = options.blankNodePrefix || this._blankNodePrefix;
37-
this._blankNodePrefixLength = this._blankNodePrefix.length;
3835
this.dataFactory = options.dataFactory;
3936
if (options.graph) {
4037
this._graph = this.dataFactory.namedNode(options.graph);
@@ -121,12 +118,12 @@ class Datasource extends EventEmitter {
121118
query = { ...query };
122119

123120
// Translate blank nodes IRIs in the query to blank nodes
124-
let blankNodePrefix = this._blankNodePrefix, blankNodePrefixLength = this._blankNodePrefixLength;
125-
if (query.subject && query.subject.value.indexOf(blankNodePrefix) === 0)
121+
let blankNodePrefix = this.urlData.blankNodePrefix, blankNodePrefixLength = this.urlData.blankNodePrefixLength;
122+
if (query.subject && query.subject.termType === 'NamedNode' && query.subject.value.indexOf(blankNodePrefix) === 0)
126123
query.subject = this.dataFactory.blankNode(query.subject.value.substr(blankNodePrefixLength));
127-
if (query.object && query.object.value.indexOf(blankNodePrefix) === 0)
124+
if (query.object && query.object.termType === 'NamedNode' && query.object.value.indexOf(blankNodePrefix) === 0)
128125
query.object = this.dataFactory.blankNode(query.object.value.substr(blankNodePrefixLength));
129-
if (query.graph && query.graph.value.indexOf(blankNodePrefix) === 0)
126+
if (query.graph && query.graph.termType === 'NamedNode' && query.graph.value.indexOf(blankNodePrefix) === 0)
130127
query.graph = this.dataFactory.blankNode(query.graph.value.substr(blankNodePrefixLength));
131128

132129
// Force the default graph if QPF support is disable

Diff for: packages/datasource-hdt/test/datasources/HdtDatasource-test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
let HdtDatasource = require('../../').datasources.HdtDatasource;
33

44
let Datasource = require('@ldf/core').datasources.Datasource,
5+
UrlData = require('@ldf/core').UrlData,
56
path = require('path'),
67
dataFactory = require('n3').DataFactory,
78
RdfString = require('rdf-string');
@@ -149,7 +150,7 @@ describe('HdtDatasource', () => {
149150
datasource = new HdtDatasource({
150151
dataFactory,
151152
file: exampleHdtFileWithBlanks,
152-
blankNodePrefix: 'http://example.org/.well-known/genid/',
153+
urlData: new UrlData({ baseURL: 'http://example.org/' }),
153154
});
154155
datasource.initialize();
155156
datasource.on('initialized', done);

0 commit comments

Comments
 (0)