rdflib 2.2.37-dbccd446 → 2.2.37-ed84c0fa

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/esm/fetcher.js CHANGED
@@ -142,7 +142,9 @@ class XHTMLHandler extends Handler {
142
142
  return 'XHTMLHandler';
143
143
  }
144
144
  static register(fetcher) {
145
- fetcher.mediatypes[XHTMLContentType] = {};
145
+ fetcher.mediatypes[XHTMLContentType] = {
146
+ 'q': 0.8
147
+ };
146
148
  }
147
149
  parse(fetcher, responseText, options) {
148
150
  let relation, reverse;
@@ -275,7 +277,7 @@ class HTMLHandler extends Handler {
275
277
  }
276
278
  static register(fetcher) {
277
279
  fetcher.mediatypes['text/html'] = {
278
- 'q': 0.9
280
+ 'q': 0.8
279
281
  };
280
282
  }
281
283
  parse(fetcher, responseText, options) {
@@ -324,21 +326,19 @@ class JsonLdHandler extends Handler {
324
326
  'q': 0.9
325
327
  };
326
328
  }
327
- parse(fetcher, responseText, options, response) {
329
+ async parse(fetcher, responseText, options, response) {
328
330
  const kb = fetcher.store;
329
- return new Promise((resolve, reject) => {
330
- try {
331
- jsonldParser(responseText, kb, options.original.value, () => {
332
- resolve(fetcher.doneFetch(options, response));
333
- });
334
- } catch (err) {
335
- const msg = 'Error trying to parse ' + options.resource + ' as JSON-LD:\n' + err; // not err.stack -- irrelevant
336
- resolve(fetcher.failFetch(options, msg, 'parse_error', response));
337
- }
338
- });
331
+ try {
332
+ await jsonldParser(responseText, kb, options.original.value);
333
+ fetcher.store.add(options.original, ns.rdf('type'), ns.link('RDFDocument'), fetcher.appNode);
334
+ return fetcher.doneFetch(options, response);
335
+ } catch (err) {
336
+ const msg = 'Error trying to parse ' + options.resource + ' as JSON-LD:\n' + err; // not err.stack -- irrelevant
337
+ return fetcher.failFetch(options, msg, 'parse_error', response);
338
+ }
339
339
  }
340
340
  }
341
- JsonLdHandler.pattern = /application\/ld\+json/;
341
+ JsonLdHandler.pattern = /application\/(ld\+json|activity\+json)/;
342
342
  class TextHandler extends Handler {
343
343
  static toString() {
344
344
  return 'TextHandler';
@@ -376,17 +376,8 @@ class N3Handler extends Handler {
376
376
  return 'N3Handler';
377
377
  }
378
378
  static register(fetcher) {
379
- fetcher.mediatypes['text/n3'] = {
380
- 'q': '1.0'
381
- }; // as per 2008 spec
382
- /*
383
- fetcher.mediatypes['application/x-turtle'] = {
384
- 'q': 1.0
385
- } // pre 2008
386
- */
387
- fetcher.mediatypes['text/turtle'] = {
388
- 'q': 1.0
389
- }; // post 2008
379
+ fetcher.mediatypes['text/n3'] = {};
380
+ fetcher.mediatypes['text/turtle'] = {};
390
381
  }
391
382
  parse(fetcher, responseText, options, response) {
392
383
  // Parse the text of this N3 file
@@ -736,7 +727,7 @@ export default class Fetcher {
736
727
  options.baseURI = options.baseURI || uri; // Preserve though proxying etc
737
728
  options.original = kb.rdfFactory.namedNode(options.baseURI);
738
729
  options.req = kb.bnode();
739
- options.headers = options.headers || new Headers();
730
+ options.headers = options.headers || {};
740
731
  if (options.contentType) {
741
732
  // @ts-ignore
742
733
  options.headers['content-type'] = options.contentType;
@@ -53,7 +53,7 @@ function listToCollection(kb, obj) {
53
53
  *
54
54
  * Ensure that {kb.rdfFactory} is a DataFactory.
55
55
  */
56
- export default function jsonldParser(str, kb, base, callback) {
56
+ export default function jsonldParser(str, kb, base) {
57
57
  const baseString = base && Object.prototype.hasOwnProperty.call(base, 'termType') ? base.value : base;
58
58
  return import('jsonld').then(jsonld => {
59
59
  // ⚠ Unit tests also work without accessing `jsonld.default` explicitly, but real browser usage will fail with
@@ -64,7 +64,7 @@ export default function jsonldParser(str, kb, base, callback) {
64
64
  }).then(flattened => flattened.reduce((store, flatResource) => {
65
65
  kb = processResource(kb, base, flatResource);
66
66
  return kb;
67
- }, kb)).then(callback).catch(callback);
67
+ }, kb));
68
68
  }
69
69
  function nodeType(kb, obj) {
70
70
  if (obj['@id'].startsWith('_:')) {
package/esm/parse.js CHANGED
@@ -12,7 +12,7 @@ import { TurtleContentType, N3ContentType, RDFXMLContentType, XHTMLContentType,
12
12
  * Parse a string and put the result into the graph kb.
13
13
  * Normal method is sync.
14
14
  * Unfortunately jsdonld is currently written to need to be called async.
15
- * Hence the mess below with executeCallback.
15
+ * If you are parsing JSON-LD and want to know when and whether it succeeded, you need to use the callback param.
16
16
  * @param str - The input string to parse
17
17
  * @param kb - The store to use
18
18
  * @param base - The base URI to use
@@ -48,7 +48,10 @@ export default function parse(str, kb, base) {
48
48
  sparqlUpdateParser(str, kb, base);
49
49
  executeCallback();
50
50
  } else if (contentType === JSONLDContentType) {
51
- jsonldParser(str, kb, base, executeCallback);
51
+ // since we do not await the promise here, rejections will not be covered by the surrounding try catch
52
+ // we do not use await, because parse() should stay sync
53
+ // so, to not lose the async error, we need to catch the rejection and call the error callback here too
54
+ jsonldParser(str, kb, base).then(executeCallback).catch(executeErrorCallback);
52
55
  } else if (contentType === NQuadsContentType || contentType === NQuadsAltContentType) {
53
56
  var n3Parser = new N3jsParser({
54
57
  factory: DataFactory
package/lib/fetcher.js CHANGED
@@ -151,7 +151,9 @@ class XHTMLHandler extends Handler {
151
151
  return 'XHTMLHandler';
152
152
  }
153
153
  static register(fetcher) {
154
- fetcher.mediatypes[_types.XHTMLContentType] = {};
154
+ fetcher.mediatypes[_types.XHTMLContentType] = {
155
+ 'q': 0.8
156
+ };
155
157
  }
156
158
  parse(fetcher, responseText, options) {
157
159
  let relation, reverse;
@@ -284,7 +286,7 @@ class HTMLHandler extends Handler {
284
286
  }
285
287
  static register(fetcher) {
286
288
  fetcher.mediatypes['text/html'] = {
287
- 'q': 0.9
289
+ 'q': 0.8
288
290
  };
289
291
  }
290
292
  parse(fetcher, responseText, options) {
@@ -333,21 +335,19 @@ class JsonLdHandler extends Handler {
333
335
  'q': 0.9
334
336
  };
335
337
  }
336
- parse(fetcher, responseText, options, response) {
338
+ async parse(fetcher, responseText, options, response) {
337
339
  const kb = fetcher.store;
338
- return new Promise((resolve, reject) => {
339
- try {
340
- (0, _jsonldparser.default)(responseText, kb, options.original.value, () => {
341
- resolve(fetcher.doneFetch(options, response));
342
- });
343
- } catch (err) {
344
- const msg = 'Error trying to parse ' + options.resource + ' as JSON-LD:\n' + err; // not err.stack -- irrelevant
345
- resolve(fetcher.failFetch(options, msg, 'parse_error', response));
346
- }
347
- });
340
+ try {
341
+ await (0, _jsonldparser.default)(responseText, kb, options.original.value);
342
+ fetcher.store.add(options.original, ns.rdf('type'), ns.link('RDFDocument'), fetcher.appNode);
343
+ return fetcher.doneFetch(options, response);
344
+ } catch (err) {
345
+ const msg = 'Error trying to parse ' + options.resource + ' as JSON-LD:\n' + err; // not err.stack -- irrelevant
346
+ return fetcher.failFetch(options, msg, 'parse_error', response);
347
+ }
348
348
  }
349
349
  }
350
- JsonLdHandler.pattern = /application\/ld\+json/;
350
+ JsonLdHandler.pattern = /application\/(ld\+json|activity\+json)/;
351
351
  class TextHandler extends Handler {
352
352
  static toString() {
353
353
  return 'TextHandler';
@@ -385,17 +385,8 @@ class N3Handler extends Handler {
385
385
  return 'N3Handler';
386
386
  }
387
387
  static register(fetcher) {
388
- fetcher.mediatypes['text/n3'] = {
389
- 'q': '1.0'
390
- }; // as per 2008 spec
391
- /*
392
- fetcher.mediatypes['application/x-turtle'] = {
393
- 'q': 1.0
394
- } // pre 2008
395
- */
396
- fetcher.mediatypes['text/turtle'] = {
397
- 'q': 1.0
398
- }; // post 2008
388
+ fetcher.mediatypes['text/n3'] = {};
389
+ fetcher.mediatypes['text/turtle'] = {};
399
390
  }
400
391
  parse(fetcher, responseText, options, response) {
401
392
  // Parse the text of this N3 file
@@ -745,7 +736,7 @@ class Fetcher {
745
736
  options.baseURI = options.baseURI || uri; // Preserve though proxying etc
746
737
  options.original = kb.rdfFactory.namedNode(options.baseURI);
747
738
  options.req = kb.bnode();
748
- options.headers = options.headers || new _crossFetch.Headers();
739
+ options.headers = options.headers || {};
749
740
  if (options.contentType) {
750
741
  // @ts-ignore
751
742
  options.headers['content-type'] = options.contentType;
@@ -10,4 +10,4 @@ export function jsonldObjectToTerm(kb: any, obj: any): Literal | NamedNode | Bla
10
10
  *
11
11
  * Ensure that {kb.rdfFactory} is a DataFactory.
12
12
  */
13
- export default function jsonldParser(str: any, kb: any, base: any, callback: any): Promise<any>;
13
+ export default function jsonldParser(str: any, kb: any, base: any): Promise<any>;
@@ -60,7 +60,7 @@ function listToCollection(kb, obj) {
60
60
  *
61
61
  * Ensure that {kb.rdfFactory} is a DataFactory.
62
62
  */
63
- function jsonldParser(str, kb, base, callback) {
63
+ function jsonldParser(str, kb, base) {
64
64
  const baseString = base && Object.prototype.hasOwnProperty.call(base, 'termType') ? base.value : base;
65
65
  return Promise.resolve().then(() => _interopRequireWildcard(require('jsonld'))).then(jsonld => {
66
66
  // ⚠ Unit tests also work without accessing `jsonld.default` explicitly, but real browser usage will fail with
@@ -71,7 +71,7 @@ function jsonldParser(str, kb, base, callback) {
71
71
  }).then(flattened => flattened.reduce((store, flatResource) => {
72
72
  kb = processResource(kb, base, flatResource);
73
73
  return kb;
74
- }, kb)).then(callback).catch(callback);
74
+ }, kb));
75
75
  }
76
76
  function nodeType(kb, obj) {
77
77
  if (obj['@id'].startsWith('_:')) {
package/lib/parse.d.ts CHANGED
@@ -5,7 +5,7 @@ type CallbackFunc = (error: any, kb: Formula | null) => void;
5
5
  * Parse a string and put the result into the graph kb.
6
6
  * Normal method is sync.
7
7
  * Unfortunately jsdonld is currently written to need to be called async.
8
- * Hence the mess below with executeCallback.
8
+ * If you are parsing JSON-LD and want to know when and whether it succeeded, you need to use the callback param.
9
9
  * @param str - The input string to parse
10
10
  * @param kb - The store to use
11
11
  * @param base - The base URI to use
package/lib/parse.js CHANGED
@@ -22,7 +22,7 @@ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r
22
22
  * Parse a string and put the result into the graph kb.
23
23
  * Normal method is sync.
24
24
  * Unfortunately jsdonld is currently written to need to be called async.
25
- * Hence the mess below with executeCallback.
25
+ * If you are parsing JSON-LD and want to know when and whether it succeeded, you need to use the callback param.
26
26
  * @param str - The input string to parse
27
27
  * @param kb - The store to use
28
28
  * @param base - The base URI to use
@@ -58,7 +58,10 @@ function parse(str, kb, base) {
58
58
  (0, _patchParser.default)(str, kb, base);
59
59
  executeCallback();
60
60
  } else if (contentType === _types.JSONLDContentType) {
61
- (0, _jsonldparser.default)(str, kb, base, executeCallback);
61
+ // since we do not await the promise here, rejections will not be covered by the surrounding try catch
62
+ // we do not use await, because parse() should stay sync
63
+ // so, to not lose the async error, we need to catch the rejection and call the error callback here too
64
+ (0, _jsonldparser.default)(str, kb, base).then(executeCallback).catch(executeErrorCallback);
62
65
  } else if (contentType === _types.NQuadsContentType || contentType === _types.NQuadsAltContentType) {
63
66
  var n3Parser = new _n.Parser({
64
67
  factory: _extendedTermFactory.default
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rdflib",
3
3
  "description": "an RDF library for node.js. Suitable for client and server side.",
4
- "version": "2.2.37-dbccd446",
4
+ "version": "2.2.37-ed84c0fa",
5
5
  "private": false,
6
6
  "browserslist": [
7
7
  "> 0.5%"
package/src/fetcher.ts CHANGED
@@ -262,7 +262,9 @@ class XHTMLHandler extends Handler {
262
262
  }
263
263
 
264
264
  static register (fetcher: Fetcher) {
265
- fetcher.mediatypes[XHTMLContentType] = {}
265
+ fetcher.mediatypes[XHTMLContentType] = {
266
+ 'q': 0.8
267
+ }
266
268
  }
267
269
 
268
270
  parse (
@@ -429,7 +431,7 @@ class HTMLHandler extends Handler {
429
431
 
430
432
  static register (fetcher: Fetcher) {
431
433
  fetcher.mediatypes['text/html'] = {
432
- 'q': 0.9
434
+ 'q': 0.8
433
435
  }
434
436
  }
435
437
 
@@ -496,31 +498,30 @@ class JsonLdHandler extends Handler {
496
498
  'q': 0.9
497
499
  }
498
500
  }
499
- parse (
500
- fetcher: Fetcher,
501
- responseText: string,
502
- options: {
503
- req: Quad_Subject
504
- original: Quad_Subject
505
- resource: Quad_Subject
506
- } & Options,
507
- response: ExtendedResponse
501
+ async parse(
502
+ fetcher: Fetcher,
503
+ responseText: string,
504
+ options: {
505
+ req: Quad_Subject
506
+ original: Quad_Subject
507
+ resource: Quad_Subject
508
+ } & Options,
509
+ response: ExtendedResponse
508
510
  ): Promise<ExtendedResponse | FetchError> {
509
511
  const kb = fetcher.store
510
- return new Promise((resolve, reject) => {
511
- try {
512
- jsonldParser (responseText, kb, options.original.value, () => {
513
- resolve(fetcher.doneFetch(options, response))
514
- })
515
- } catch (err) {
516
- const msg = 'Error trying to parse ' + options.resource +
512
+ try {
513
+ await jsonldParser(responseText, kb, options.original.value)
514
+ fetcher.store.add(options.original, ns.rdf('type'), ns.link('RDFDocument'), fetcher.appNode)
515
+ return fetcher.doneFetch(options, response)
516
+ } catch (err) {
517
+ const msg = 'Error trying to parse ' + options.resource +
517
518
  ' as JSON-LD:\n' + err // not err.stack -- irrelevant
518
- resolve(fetcher.failFetch(options, msg, 'parse_error', response))
519
- }
520
- })
519
+ return fetcher.failFetch(options, msg, 'parse_error', response)
520
+ }
521
521
  }
522
522
  }
523
- JsonLdHandler.pattern = /application\/ld\+json/
523
+
524
+ JsonLdHandler.pattern = /application\/(ld\+json|activity\+json)/
524
525
 
525
526
  class TextHandler extends Handler {
526
527
  static toString () {
@@ -577,17 +578,8 @@ class N3Handler extends Handler {
577
578
  }
578
579
 
579
580
  static register (fetcher: Fetcher) {
580
- fetcher.mediatypes['text/n3'] = {
581
- 'q': '1.0'
582
- } // as per 2008 spec
583
- /*
584
- fetcher.mediatypes['application/x-turtle'] = {
585
- 'q': 1.0
586
- } // pre 2008
587
- */
588
- fetcher.mediatypes['text/turtle'] = {
589
- 'q': 1.0
590
- } // post 2008
581
+ fetcher.mediatypes['text/n3'] = {}
582
+ fetcher.mediatypes['text/turtle'] = {}
591
583
  }
592
584
 
593
585
  parse (
@@ -1057,7 +1049,7 @@ export default class Fetcher implements CallbackifyInterface {
1057
1049
  options.baseURI = options.baseURI || uri // Preserve though proxying etc
1058
1050
  options.original = kb.rdfFactory.namedNode(options.baseURI)
1059
1051
  options.req = kb.bnode()
1060
- options.headers = options.headers || new Headers()
1052
+ options.headers = options.headers || {}
1061
1053
 
1062
1054
  if (options.contentType) {
1063
1055
  // @ts-ignore
@@ -63,8 +63,10 @@ function listToCollection(kb, obj) {
63
63
  *
64
64
  * Ensure that {kb.rdfFactory} is a DataFactory.
65
65
  */
66
- export default function jsonldParser(str, kb, base, callback) {
67
- const baseString = base && Object.prototype.hasOwnProperty.call(base, 'termType') ? base.value : base
66
+ export default function jsonldParser(str, kb, base) {
67
+ const baseString = base && Object.prototype.hasOwnProperty.call(base, 'termType')
68
+ ? base.value
69
+ : base
68
70
 
69
71
  return import('jsonld')
70
72
  .then(jsonld => {
@@ -73,13 +75,9 @@ export default function jsonldParser(str, kb, base, callback) {
73
75
  return jsonld.default.flatten(JSON.parse(str), null, {base: baseString})
74
76
  })
75
77
  .then((flattened) => flattened.reduce((store, flatResource) => {
76
-
77
78
  kb = processResource(kb, base, flatResource)
78
-
79
79
  return kb
80
80
  }, kb))
81
- .then(callback)
82
- .catch(callback)
83
81
  }
84
82
 
85
83
  function nodeType(kb, obj) {
package/src/parse.ts CHANGED
@@ -17,7 +17,7 @@ type CallbackFunc = (error: any, kb: Formula | null) => void
17
17
  * Parse a string and put the result into the graph kb.
18
18
  * Normal method is sync.
19
19
  * Unfortunately jsdonld is currently written to need to be called async.
20
- * Hence the mess below with executeCallback.
20
+ * If you are parsing JSON-LD and want to know when and whether it succeeded, you need to use the callback param.
21
21
  * @param str - The input string to parse
22
22
  * @param kb - The store to use
23
23
  * @param base - The base URI to use
@@ -52,7 +52,12 @@ export default function parse (
52
52
  sparqlUpdateParser(str, kb, base)
53
53
  executeCallback()
54
54
  } else if (contentType === JSONLDContentType) {
55
- jsonldParser(str, kb, base, executeCallback)
55
+ // since we do not await the promise here, rejections will not be covered by the surrounding try catch
56
+ // we do not use await, because parse() should stay sync
57
+ // so, to not lose the async error, we need to catch the rejection and call the error callback here too
58
+ jsonldParser(str, kb, base)
59
+ .then(executeCallback)
60
+ .catch(executeErrorCallback)
56
61
  } else if (contentType === NQuadsContentType ||
57
62
  contentType === NQuadsAltContentType) {
58
63
  var n3Parser = new N3jsParser({ factory: DataFactory })