rdflib 2.2.37-dbccd446 → 2.2.37-e1458833

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,18 +53,15 @@ 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 async function jsonldParser(str, kb, base) {
57
57
  const baseString = base && Object.prototype.hasOwnProperty.call(base, 'termType') ? base.value : base;
58
- return import('jsonld').then(jsonld => {
59
- // ⚠ Unit tests also work without accessing `jsonld.default` explicitly, but real browser usage will fail with
60
- // just calling `jsonld.flatten`, so please do not remove `default`
61
- return jsonld.default.flatten(JSON.parse(str), null, {
62
- base: baseString
63
- });
64
- }).then(flattened => flattened.reduce((store, flatResource) => {
65
- kb = processResource(kb, base, flatResource);
66
- return kb;
67
- }, kb)).then(callback).catch(callback);
58
+ const jsonld = await import('jsonld');
59
+ // ⚠ Unit tests also work without accessing `jsonld.default` explicitly, but real browser usage will fail with
60
+ // just calling `jsonld.flatten`, so please do not remove `default`
61
+ const flattened = await jsonld.default.flatten(JSON.parse(str), null, {
62
+ base: baseString
63
+ });
64
+ return flattened.reduce((store, flatResource) => processResource(store, base, flatResource), kb);
68
65
  }
69
66
  function nodeType(kb, obj) {
70
67
  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/esm/store.js CHANGED
@@ -129,7 +129,9 @@ export default class IndexedFormula extends Formula {
129
129
  /** Function to remove quads from the store arrays with */
130
130
  _defineProperty(this, "rdfArrayRemove", void 0);
131
131
  /** Callbacks which are triggered after a statement has been added to the store */
132
- _defineProperty(this, "dataCallbacks", void 0);
132
+ _defineProperty(this, "dataCallbacks", []);
133
+ /** Callbacks which are triggered after a statement has been removed from the store */
134
+ _defineProperty(this, "dataRemovalCallbacks", []);
133
135
  this.propertyActions = {};
134
136
  this.classActions = {};
135
137
  this.redirections = [];
@@ -151,6 +153,9 @@ export default class IndexedFormula extends Formula {
151
153
  if (opts.dataCallback) {
152
154
  this.dataCallbacks = [opts.dataCallback];
153
155
  }
156
+ if (opts.dataRemovalCallback) {
157
+ this.dataRemovalCallbacks = [opts.dataRemovalCallback];
158
+ }
154
159
  this.initPropertyActions(this.features);
155
160
  }
156
161
 
@@ -179,11 +184,11 @@ export default class IndexedFormula extends Formula {
179
184
  * @param cb
180
185
  */
181
186
  addDataCallback(cb) {
182
- if (!this.dataCallbacks) {
183
- this.dataCallbacks = [];
184
- }
185
187
  this.dataCallbacks.push(cb);
186
188
  }
189
+ addDataRemovalCallback(cb) {
190
+ this.dataRemovalCallbacks.push(cb);
191
+ }
187
192
 
188
193
  /**
189
194
  * Apply a set of statements to be deleted and to be inserted
@@ -397,10 +402,8 @@ export default class IndexedFormula extends Formula {
397
402
 
398
403
  // log.debug("ADDING {"+subj+" "+pred+" "+objNode+"} "+why)
399
404
  this.statements.push(st);
400
- if (this.dataCallbacks) {
401
- for (const callback of this.dataCallbacks) {
402
- callback(st);
403
- }
405
+ for (const callback of this.dataCallbacks) {
406
+ callback(st);
404
407
  }
405
408
  return st;
406
409
  }
@@ -850,6 +853,9 @@ export default class IndexedFormula extends Formula {
850
853
  }
851
854
  }
852
855
  this.rdfArrayRemove(this.statements, st);
856
+ for (const callback of this.dataRemovalCallbacks) {
857
+ callback(st);
858
+ }
853
859
  return this;
854
860
  }
855
861
 
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;
package/lib/formula.d.ts CHANGED
@@ -12,6 +12,7 @@ import BlankNode from './blank-node';
12
12
  import NamedNode from './named-node';
13
13
  export interface FormulaOpts {
14
14
  dataCallback?: (q: Quad) => void;
15
+ dataRemovalCallback?: (q: Quad) => void;
15
16
  rdfArrayRemove?: (arr: Quad[], q: Quad) => void;
16
17
  rdfFactory?: RdfJsDataFactory;
17
18
  }
@@ -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,18 +60,15 @@ 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
+ async function jsonldParser(str, kb, base) {
64
64
  const baseString = base && Object.prototype.hasOwnProperty.call(base, 'termType') ? base.value : base;
65
- return Promise.resolve().then(() => _interopRequireWildcard(require('jsonld'))).then(jsonld => {
66
- // ⚠ Unit tests also work without accessing `jsonld.default` explicitly, but real browser usage will fail with
67
- // just calling `jsonld.flatten`, so please do not remove `default`
68
- return jsonld.default.flatten(JSON.parse(str), null, {
69
- base: baseString
70
- });
71
- }).then(flattened => flattened.reduce((store, flatResource) => {
72
- kb = processResource(kb, base, flatResource);
73
- return kb;
74
- }, kb)).then(callback).catch(callback);
65
+ const jsonld = await Promise.resolve().then(() => _interopRequireWildcard(require('jsonld')));
66
+ // ⚠ Unit tests also work without accessing `jsonld.default` explicitly, but real browser usage will fail with
67
+ // just calling `jsonld.flatten`, so please do not remove `default`
68
+ const flattened = await jsonld.default.flatten(JSON.parse(str), null, {
69
+ base: baseString
70
+ });
71
+ return flattened.reduce((store, flatResource) => processResource(store, base, flatResource), kb);
75
72
  }
76
73
  function nodeType(kb, obj) {
77
74
  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/lib/store.d.ts CHANGED
@@ -61,7 +61,9 @@ export default class IndexedFormula extends Formula {
61
61
  /** Function to remove quads from the store arrays with */
62
62
  private rdfArrayRemove;
63
63
  /** Callbacks which are triggered after a statement has been added to the store */
64
- private dataCallbacks?;
64
+ private dataCallbacks;
65
+ /** Callbacks which are triggered after a statement has been removed from the store */
66
+ private dataRemovalCallbacks;
65
67
  /**
66
68
  * Creates a new formula
67
69
  * @param features - What sort of automatic processing to do? Array of string
@@ -86,6 +88,7 @@ export default class IndexedFormula extends Formula {
86
88
  * @param cb
87
89
  */
88
90
  addDataCallback(cb: (q: Quad) => void): void;
91
+ addDataRemovalCallback(cb: (q: Quad) => void): void;
89
92
  /**
90
93
  * Apply a set of statements to be deleted and to be inserted
91
94
  *
package/lib/store.js CHANGED
@@ -137,7 +137,9 @@ class IndexedFormula extends _formula.default {
137
137
  /** Function to remove quads from the store arrays with */
138
138
  (0, _defineProperty2.default)(this, "rdfArrayRemove", void 0);
139
139
  /** Callbacks which are triggered after a statement has been added to the store */
140
- (0, _defineProperty2.default)(this, "dataCallbacks", void 0);
140
+ (0, _defineProperty2.default)(this, "dataCallbacks", []);
141
+ /** Callbacks which are triggered after a statement has been removed from the store */
142
+ (0, _defineProperty2.default)(this, "dataRemovalCallbacks", []);
141
143
  this.propertyActions = {};
142
144
  this.classActions = {};
143
145
  this.redirections = [];
@@ -159,6 +161,9 @@ class IndexedFormula extends _formula.default {
159
161
  if (opts.dataCallback) {
160
162
  this.dataCallbacks = [opts.dataCallback];
161
163
  }
164
+ if (opts.dataRemovalCallback) {
165
+ this.dataRemovalCallbacks = [opts.dataRemovalCallback];
166
+ }
162
167
  this.initPropertyActions(this.features);
163
168
  }
164
169
 
@@ -187,11 +192,11 @@ class IndexedFormula extends _formula.default {
187
192
  * @param cb
188
193
  */
189
194
  addDataCallback(cb) {
190
- if (!this.dataCallbacks) {
191
- this.dataCallbacks = [];
192
- }
193
195
  this.dataCallbacks.push(cb);
194
196
  }
197
+ addDataRemovalCallback(cb) {
198
+ this.dataRemovalCallbacks.push(cb);
199
+ }
195
200
 
196
201
  /**
197
202
  * Apply a set of statements to be deleted and to be inserted
@@ -405,10 +410,8 @@ class IndexedFormula extends _formula.default {
405
410
 
406
411
  // log.debug("ADDING {"+subj+" "+pred+" "+objNode+"} "+why)
407
412
  this.statements.push(st);
408
- if (this.dataCallbacks) {
409
- for (const callback of this.dataCallbacks) {
410
- callback(st);
411
- }
413
+ for (const callback of this.dataCallbacks) {
414
+ callback(st);
412
415
  }
413
416
  return st;
414
417
  }
@@ -858,6 +861,9 @@ class IndexedFormula extends _formula.default {
858
861
  }
859
862
  }
860
863
  this.rdfArrayRemove(this.statements, st);
864
+ for (const callback of this.dataRemovalCallbacks) {
865
+ callback(st);
866
+ }
861
867
  return this;
862
868
  }
863
869
 
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-e1458833",
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
package/src/formula.ts CHANGED
@@ -33,6 +33,7 @@ import NamedNode from './named-node'
33
33
 
34
34
  export interface FormulaOpts {
35
35
  dataCallback?: (q: Quad) => void
36
+ dataRemovalCallback?: (q: Quad) => void;
36
37
  rdfArrayRemove?: (arr: Quad[], q: Quad) => void
37
38
  rdfFactory?: RdfJsDataFactory
38
39
  }
@@ -63,23 +63,16 @@ 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
68
-
69
- return import('jsonld')
70
- .then(jsonld => {
71
- // Unit tests also work without accessing `jsonld.default` explicitly, but real browser usage will fail with
72
- // just calling `jsonld.flatten`, so please do not remove `default`
73
- return jsonld.default.flatten(JSON.parse(str), null, {base: baseString})
74
- })
75
- .then((flattened) => flattened.reduce((store, flatResource) => {
76
-
77
- kb = processResource(kb, base, flatResource)
78
-
79
- return kb
80
- }, kb))
81
- .then(callback)
82
- .catch(callback)
66
+ export default async function jsonldParser(str, kb, base) {
67
+ const baseString = base && Object.prototype.hasOwnProperty.call(base, 'termType')
68
+ ? base.value
69
+ : base
70
+
71
+ const jsonld = await import('jsonld')
72
+ // Unit tests also work without accessing `jsonld.default` explicitly, but real browser usage will fail with
73
+ // just calling `jsonld.flatten`, so please do not remove `default`
74
+ const flattened = await jsonld.default.flatten(JSON.parse(str), null, {base: baseString})
75
+ return flattened.reduce((store, flatResource) => processResource(store, base, flatResource), kb)
83
76
  }
84
77
 
85
78
  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 })
package/src/store.ts CHANGED
@@ -168,7 +168,9 @@ export default class IndexedFormula extends Formula { // IN future - allow pass
168
168
  /** Function to remove quads from the store arrays with */
169
169
  private rdfArrayRemove: (arr: Quad[], q: Quad) => void
170
170
  /** Callbacks which are triggered after a statement has been added to the store */
171
- private dataCallbacks?: Array<(q: Quad) => void>
171
+ private dataCallbacks: Array<(q: Quad) => void> = []
172
+ /** Callbacks which are triggered after a statement has been removed from the store */
173
+ private dataRemovalCallbacks: Array<(q: Quad) => void> = []
172
174
 
173
175
  /**
174
176
  * Creates a new formula
@@ -208,6 +210,9 @@ export default class IndexedFormula extends Formula { // IN future - allow pass
208
210
  if (opts.dataCallback) {
209
211
  this.dataCallbacks = [opts.dataCallback]
210
212
  }
213
+ if (opts.dataRemovalCallback) {
214
+ this.dataRemovalCallbacks = [opts.dataRemovalCallback]
215
+ }
211
216
 
212
217
  this.initPropertyActions(this.features)
213
218
  }
@@ -237,12 +242,13 @@ export default class IndexedFormula extends Formula { // IN future - allow pass
237
242
  * @param cb
238
243
  */
239
244
  addDataCallback(cb: (q: Quad) => void): void {
240
- if (!this.dataCallbacks) {
241
- this.dataCallbacks = []
242
- }
243
245
  this.dataCallbacks.push(cb)
244
246
  }
245
247
 
248
+ addDataRemovalCallback(cb: (q: Quad) => void): void {
249
+ this.dataRemovalCallbacks.push(cb)
250
+ }
251
+
246
252
  /**
247
253
  * Apply a set of statements to be deleted and to be inserted
248
254
  *
@@ -486,10 +492,8 @@ export default class IndexedFormula extends Formula { // IN future - allow pass
486
492
  // log.debug("ADDING {"+subj+" "+pred+" "+objNode+"} "+why)
487
493
  this.statements.push(st)
488
494
 
489
- if (this.dataCallbacks) {
490
- for (const callback of this.dataCallbacks) {
491
- callback(st)
492
- }
495
+ for (const callback of this.dataCallbacks) {
496
+ callback(st)
493
497
  }
494
498
 
495
499
  return st
@@ -987,6 +991,9 @@ export default class IndexedFormula extends Formula { // IN future - allow pass
987
991
  }
988
992
  }
989
993
  this.rdfArrayRemove(this.statements, st)
994
+ for (const callback of this.dataRemovalCallbacks) {
995
+ callback(st)
996
+ }
990
997
  return this
991
998
  }
992
999