rdflib 2.2.35 → 2.2.36

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.
Files changed (90) hide show
  1. package/dist/rdflib.min.js +1 -1
  2. package/dist/rdflib.min.js.LICENSE.txt +0 -2
  3. package/dist/rdflib.min.js.map +1 -1
  4. package/esm/blank-node.js +57 -82
  5. package/esm/class-order.js +1 -1
  6. package/esm/collection.js +70 -103
  7. package/esm/default-graph.js +13 -30
  8. package/esm/empty.js +8 -23
  9. package/esm/factories/canonical-data-factory.js +33 -29
  10. package/esm/factories/extended-term-factory.js +18 -13
  11. package/esm/factories/factory-types.js +1 -1
  12. package/esm/factories/rdflib-data-factory.js +9 -11
  13. package/esm/fetcher.js +1366 -1651
  14. package/esm/formula.js +631 -736
  15. package/esm/index.js +31 -48
  16. package/esm/jsonldparser.js +19 -26
  17. package/esm/jsonparser.js +1 -1
  18. package/esm/lists.js +38 -86
  19. package/esm/literal.js +120 -154
  20. package/esm/log.js +7 -7
  21. package/esm/n3parser.js +1009 -1086
  22. package/esm/named-node.js +69 -96
  23. package/esm/namespace.js +2 -4
  24. package/esm/node-internal.js +73 -96
  25. package/esm/node.js +1 -1
  26. package/esm/parse.js +3 -3
  27. package/esm/patch-parser.js +1 -1
  28. package/esm/query.js +15 -16
  29. package/esm/rdfaparser.js +775 -841
  30. package/esm/rdfxmlparser.js +348 -364
  31. package/esm/serialize.js +2 -2
  32. package/esm/serializer.js +835 -877
  33. package/esm/statement.js +52 -71
  34. package/esm/store.js +853 -957
  35. package/esm/types.js +21 -21
  36. package/esm/update-manager.js +965 -1100
  37. package/esm/updates-via.js +104 -132
  38. package/esm/uri.js +3 -3
  39. package/esm/utils/default-graph-uri.js +2 -2
  40. package/esm/utils/terms.js +4 -5
  41. package/esm/utils-js.js +5 -5
  42. package/esm/utils.js +6 -6
  43. package/esm/variable.js +32 -55
  44. package/esm/xsd.js +2 -2
  45. package/lib/blank-node.js +57 -80
  46. package/lib/class-order.js +1 -1
  47. package/lib/collection.js +70 -101
  48. package/lib/default-graph.js +14 -29
  49. package/lib/empty.js +9 -22
  50. package/lib/factories/canonical-data-factory.js +35 -31
  51. package/lib/factories/extended-term-factory.js +18 -13
  52. package/lib/factories/factory-types.js +1 -1
  53. package/lib/factories/rdflib-data-factory.js +9 -11
  54. package/lib/fetcher.js +1375 -1654
  55. package/lib/formula.js +632 -735
  56. package/lib/index.js +80 -84
  57. package/lib/jsonldparser.js +21 -32
  58. package/lib/jsonparser.js +1 -1
  59. package/lib/lists.js +47 -87
  60. package/lib/literal.js +121 -153
  61. package/lib/log.js +7 -7
  62. package/lib/n3parser.js +1012 -1090
  63. package/lib/named-node.js +70 -95
  64. package/lib/namespace.js +2 -4
  65. package/lib/node-internal.js +73 -94
  66. package/lib/node.js +1 -1
  67. package/lib/parse.js +5 -6
  68. package/lib/patch-parser.js +1 -1
  69. package/lib/query.js +20 -18
  70. package/lib/rdfaparser.js +778 -843
  71. package/lib/rdfxmlparser.js +351 -365
  72. package/lib/serialize.js +2 -2
  73. package/lib/serializer.js +840 -880
  74. package/lib/statement.js +55 -73
  75. package/lib/store.js +860 -958
  76. package/lib/types.js +21 -21
  77. package/lib/update-manager.js +970 -1102
  78. package/lib/updates-via.js +107 -132
  79. package/lib/uri.js +3 -3
  80. package/lib/utils/default-graph-uri.js +2 -2
  81. package/lib/utils/terms.js +4 -6
  82. package/lib/utils-js.js +8 -9
  83. package/lib/utils.js +6 -6
  84. package/lib/variable.js +35 -57
  85. package/lib/xsd.js +2 -2
  86. package/package.json +2 -2
  87. package/src/n3parser.js +1 -1
  88. package/src/serializer.js +1 -1
  89. package/src/update-manager.ts +2 -1
  90. package/.babelrc +0 -20
package/esm/fetcher.js CHANGED
@@ -1,16 +1,4 @@
1
- import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
- import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
3
- import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
4
- import _inherits from "@babel/runtime/helpers/inherits";
5
- import _createClass from "@babel/runtime/helpers/createClass";
6
- import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
7
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
8
- import _regeneratorRuntime from "@babel/runtime/regenerator";
9
- function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
10
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
11
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
12
- function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
13
- function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
14
2
  /* global $SolidTestEnvironment */
15
3
  /**
16
4
  *
@@ -54,7 +42,7 @@ import crossFetch, { Headers } from 'cross-fetch';
54
42
  import { TurtleContentType, RDFXMLContentType, XHTMLContentType } from './types';
55
43
  import { termValue } from './utils/termValue';
56
44
  import jsonldParser from './jsonldparser';
57
- var Parsable = {
45
+ const Parsable = {
58
46
  'text/n3': true,
59
47
  'text/turtle': true,
60
48
  'application/rdf+xml': true,
@@ -64,7 +52,7 @@ var Parsable = {
64
52
  };
65
53
 
66
54
  // This is a minimal set to allow the use of damaged servers if necessary
67
- var CONTENT_TYPE_BY_EXT = {
55
+ const CONTENT_TYPE_BY_EXT = {
68
56
  'rdf': RDFXMLContentType,
69
57
  'owl': RDFXMLContentType,
70
58
  'n3': 'text/n3',
@@ -80,7 +68,7 @@ var CONTENT_TYPE_BY_EXT = {
80
68
  // make its own list and not rely on the prefixes used here,
81
69
  // and not be tempted to add to them, and them clash with those of another
82
70
  // application.
83
- var getNS = function getNS(factory) {
71
+ const getNS = factory => {
84
72
  return {
85
73
  link: Namespace('http://www.w3.org/2007/ont/link#', factory),
86
74
  http: Namespace('http://www.w3.org/2007/ont/http#', factory),
@@ -92,7 +80,7 @@ var getNS = function getNS(factory) {
92
80
  ldp: Namespace('http://www.w3.org/ns/ldp#', factory)
93
81
  };
94
82
  };
95
- var ns = getNS();
83
+ const ns = getNS();
96
84
 
97
85
  /** An extended interface of Response, since RDFlib.js adds some properties. */
98
86
 
@@ -101,435 +89,352 @@ var ns = getNS();
101
89
  /** All valid inputs for initFetchOptions */
102
90
 
103
91
  /** Initiated by initFetchOptions, which runs on load */
104
- var Handler = /*#__PURE__*/_createClass(function Handler(response, dom) {
105
- _classCallCheck(this, Handler);
106
- // TODO: Document, type
107
- _defineProperty(this, "response", void 0);
108
- // TODO: Document, type
109
- _defineProperty(this, "dom", void 0);
110
- this.response = response;
111
- // The type assertion operator here might need to be removed.
112
- this.dom = dom;
113
- });
92
+
93
+ class Handler {
94
+ constructor(response, dom) {
95
+ // TODO: Document, type
96
+ _defineProperty(this, "response", void 0);
97
+ // TODO: Document, type
98
+ _defineProperty(this, "dom", void 0);
99
+ this.response = response;
100
+ // The type assertion operator here might need to be removed.
101
+ this.dom = dom;
102
+ }
103
+ }
114
104
  _defineProperty(Handler, "pattern", void 0);
115
- var RDFXMLHandler = /*#__PURE__*/function (_Handler2) {
116
- function RDFXMLHandler() {
117
- _classCallCheck(this, RDFXMLHandler);
118
- return _callSuper(this, RDFXMLHandler, arguments);
119
- }
120
- _inherits(RDFXMLHandler, _Handler2);
121
- return _createClass(RDFXMLHandler, [{
122
- key: "parse",
123
- value: function parse(fetcher, /** An XML String */
124
- responseText, /** Requires .original */
125
- options) {
126
- var kb = fetcher.store;
127
- if (!this.dom) {
128
- this.dom = Util.parseXML(responseText);
129
- }
130
- var root = this.dom.documentElement;
131
- if (root.nodeName === 'parsererror') {
132
- // Mozilla only See issue/issue110
133
- // have to fail the request
134
- return fetcher.failFetch(options, 'Badly formed XML in ' + options.resource.value, 'parse_error');
135
- }
136
- var parser = new RDFParser(kb);
137
- try {
138
- parser.parse(this.dom, options.original.value, options.original);
139
- } catch (err) {
140
- return fetcher.failFetch(options, 'Syntax error parsing RDF/XML! ' + err, 'parse_error');
141
- }
142
- if (!options.noMeta) {
143
- kb.add(options.original, ns.rdf('type'), ns.link('RDFDocument'), fetcher.appNode);
144
- }
145
- return fetcher.doneFetch(options, this.response);
146
- }
147
- }], [{
148
- key: "toString",
149
- value: function toString() {
150
- return 'RDFXMLHandler';
105
+ class RDFXMLHandler extends Handler {
106
+ static toString() {
107
+ return 'RDFXMLHandler';
108
+ }
109
+ static register(fetcher) {
110
+ fetcher.mediatypes[RDFXMLContentType] = {
111
+ 'q': 0.9
112
+ };
113
+ }
114
+ parse(fetcher, /** An XML String */
115
+ responseText, /** Requires .original */
116
+ options) {
117
+ let kb = fetcher.store;
118
+ if (!this.dom) {
119
+ this.dom = Util.parseXML(responseText);
120
+ }
121
+ let root = this.dom.documentElement;
122
+ if (root.nodeName === 'parsererror') {
123
+ // Mozilla only See issue/issue110
124
+ // have to fail the request
125
+ return fetcher.failFetch(options, 'Badly formed XML in ' + options.resource.value, 'parse_error');
126
+ }
127
+ let parser = new RDFParser(kb);
128
+ try {
129
+ parser.parse(this.dom, options.original.value, options.original);
130
+ } catch (err) {
131
+ return fetcher.failFetch(options, 'Syntax error parsing RDF/XML! ' + err, 'parse_error');
132
+ }
133
+ if (!options.noMeta) {
134
+ kb.add(options.original, ns.rdf('type'), ns.link('RDFDocument'), fetcher.appNode);
135
+ }
136
+ return fetcher.doneFetch(options, this.response);
137
+ }
138
+ }
139
+ RDFXMLHandler.pattern = new RegExp('application/rdf\\+xml');
140
+ class XHTMLHandler extends Handler {
141
+ static toString() {
142
+ return 'XHTMLHandler';
143
+ }
144
+ static register(fetcher) {
145
+ fetcher.mediatypes[XHTMLContentType] = {};
146
+ }
147
+ parse(fetcher, responseText, options) {
148
+ let relation, reverse;
149
+ if (!this.dom) {
150
+ this.dom = Util.parseXML(responseText);
151
151
  }
152
- }, {
153
- key: "register",
154
- value: function register(fetcher) {
155
- fetcher.mediatypes[RDFXMLContentType] = {
156
- 'q': 0.9
157
- };
152
+ let kb = fetcher.store;
153
+
154
+ // dc:title
155
+ let title = this.dom.getElementsByTagName('title');
156
+ if (title.length > 0) {
157
+ kb.add(options.resource, ns.dc('title'), kb.rdfFactory.literal(title[0].textContent), options.resource);
158
+ // log.info("Inferring title of " + xhr.resource)
158
159
  }
159
- }]);
160
- }(Handler);
161
- RDFXMLHandler.pattern = new RegExp('application/rdf\\+xml');
162
- var XHTMLHandler = /*#__PURE__*/function (_Handler3) {
163
- function XHTMLHandler() {
164
- _classCallCheck(this, XHTMLHandler);
165
- return _callSuper(this, XHTMLHandler, arguments);
166
- }
167
- _inherits(XHTMLHandler, _Handler3);
168
- return _createClass(XHTMLHandler, [{
169
- key: "parse",
170
- value: function parse(fetcher, responseText, options) {
171
- var relation, reverse;
172
- if (!this.dom) {
173
- this.dom = Util.parseXML(responseText);
174
- }
175
- var kb = fetcher.store;
176
160
 
177
- // dc:title
178
- var title = this.dom.getElementsByTagName('title');
179
- if (title.length > 0) {
180
- kb.add(options.resource, ns.dc('title'), kb.rdfFactory.literal(title[0].textContent), options.resource);
181
- // log.info("Inferring title of " + xhr.resource)
161
+ // link rel
162
+ let links = this.dom.getElementsByTagName('link');
163
+ for (let x = links.length - 1; x >= 0; x--) {
164
+ // @@ rev
165
+ relation = links[x].getAttribute('rel');
166
+ reverse = false;
167
+ if (!relation) {
168
+ relation = links[x].getAttribute('rev');
169
+ reverse = true;
182
170
  }
183
-
184
- // link rel
185
- var links = this.dom.getElementsByTagName('link');
186
- for (var x = links.length - 1; x >= 0; x--) {
187
- // @@ rev
188
- relation = links[x].getAttribute('rel');
189
- reverse = false;
190
- if (!relation) {
191
- relation = links[x].getAttribute('rev');
192
- reverse = true;
193
- }
194
- if (relation) {
195
- fetcher.linkData(options.original, relation, links[x].getAttribute('href'), options.resource, reverse);
196
- }
171
+ if (relation) {
172
+ fetcher.linkData(options.original, relation, links[x].getAttribute('href'), options.resource, reverse);
197
173
  }
174
+ }
198
175
 
199
- // Data Islands
200
- var scripts = this.dom.getElementsByTagName('script');
201
- for (var i = 0; i < scripts.length; i++) {
202
- var contentType = scripts[i].getAttribute('type');
203
- if (Parsable[contentType]) {
204
- // @ts-ignore incompatibility between Store.add and Formula.add
205
- rdfParse(scripts[i].textContent, kb, options.original.value, contentType);
206
- // @ts-ignore incompatibility between Store.add and Formula.add
207
- rdfParse(scripts[i].textContent, kb, options.original.value, contentType);
208
- }
209
- }
210
- if (!options.noMeta) {
211
- kb.add(options.resource, ns.rdf('type'), ns.link('WebPage'), fetcher.appNode);
176
+ // Data Islands
177
+ let scripts = this.dom.getElementsByTagName('script');
178
+ for (let i = 0; i < scripts.length; i++) {
179
+ let contentType = scripts[i].getAttribute('type');
180
+ if (Parsable[contentType]) {
181
+ // @ts-ignore incompatibility between Store.add and Formula.add
182
+ rdfParse(scripts[i].textContent, kb, options.original.value, contentType);
183
+ // @ts-ignore incompatibility between Store.add and Formula.add
184
+ rdfParse(scripts[i].textContent, kb, options.original.value, contentType);
212
185
  }
213
- if (!options.noRDFa && parseRDFaDOM) {
214
- // enable by default
215
- try {
216
- parseRDFaDOM(this.dom, kb, options.original.value);
217
- } catch (err) {
218
- // @ts-ignore
219
- var msg = 'Error trying to parse ' + options.resource + ' as RDFa:\n' + err + ':\n' + err.stack;
220
- return fetcher.failFetch(options, msg, 'parse_error');
221
- }
222
- }
223
- return fetcher.doneFetch(options, this.response);
224
186
  }
225
- }], [{
226
- key: "toString",
227
- value: function toString() {
228
- return 'XHTMLHandler';
187
+ if (!options.noMeta) {
188
+ kb.add(options.resource, ns.rdf('type'), ns.link('WebPage'), fetcher.appNode);
229
189
  }
230
- }, {
231
- key: "register",
232
- value: function register(fetcher) {
233
- fetcher.mediatypes[XHTMLContentType] = {};
190
+ if (!options.noRDFa && parseRDFaDOM) {
191
+ // enable by default
192
+ try {
193
+ parseRDFaDOM(this.dom, kb, options.original.value);
194
+ } catch (err) {
195
+ // @ts-ignore
196
+ let msg = 'Error trying to parse ' + options.resource + ' as RDFa:\n' + err + ':\n' + err.stack;
197
+ return fetcher.failFetch(options, msg, 'parse_error');
198
+ }
234
199
  }
235
- }]);
236
- }(Handler);
200
+ return fetcher.doneFetch(options, this.response);
201
+ }
202
+ }
237
203
  XHTMLHandler.pattern = new RegExp('application/xhtml');
238
- var XMLHandler = /*#__PURE__*/function (_Handler4) {
239
- function XMLHandler() {
240
- _classCallCheck(this, XMLHandler);
241
- return _callSuper(this, XMLHandler, arguments);
242
- }
243
- _inherits(XMLHandler, _Handler4);
244
- return _createClass(XMLHandler, [{
245
- key: "parse",
246
- value: function parse(fetcher, responseText, options) {
247
- var dom = Util.parseXML(responseText);
248
-
249
- // XML Semantics defined by root element namespace
250
- // figure out the root element
251
- for (var c = 0; c < dom.childNodes.length; c++) {
252
- var node = dom.childNodes[c];
253
- // is this node an element?
254
- if (XMLHandler.isElement(node)) {
255
- // We've found the first element, it's the root
256
- var _ns = node.namespaceURI;
257
-
258
- // Is it RDF/XML?
259
- if (_ns && _ns === _ns['rdf']) {
260
- fetcher.addStatus(options.req, 'Has XML root element in the RDF namespace, so assume RDF/XML.');
261
- var rdfHandler = new RDFXMLHandler(this.response, dom);
262
- return rdfHandler.parse(fetcher, responseText, options);
263
- }
264
- break;
265
- }
266
- }
267
-
268
- // Or it could be XHTML?
269
- // Maybe it has an XHTML DOCTYPE?
270
- if (dom.doctype) {
271
- // log.info("We found a DOCTYPE in " + xhr.resource)
272
- if (dom.doctype.name === 'html' && dom.doctype.publicId.match(/^-\/\/W3C\/\/DTD XHTML/) && dom.doctype.systemId.match(/http:\/\/www.w3.org\/TR\/xhtml/)) {
273
- fetcher.addStatus(options.req, 'Has XHTML DOCTYPE. Switching to XHTML Handler.\n');
274
- var xhtmlHandler = new XHTMLHandler(this.response, dom);
275
- return xhtmlHandler.parse(fetcher, responseText, options);
276
- }
277
- }
278
-
279
- // Or what about an XHTML namespace?
280
- var html = dom.getElementsByTagName('html')[0];
281
- if (html) {
282
- var xmlns = html.getAttribute('xmlns');
283
- if (xmlns && xmlns.match(/^http:\/\/www.w3.org\/1999\/xhtml/)) {
284
- fetcher.addStatus(options.req, 'Has a default namespace for ' + 'XHTML. Switching to XHTMLHandler.\n');
285
- var _xhtmlHandler = new XHTMLHandler(this.response, dom);
286
- return _xhtmlHandler.parse(fetcher, responseText, options);
204
+ class XMLHandler extends Handler {
205
+ static toString() {
206
+ return 'XMLHandler';
207
+ }
208
+ static register(fetcher) {
209
+ fetcher.mediatypes['text/xml'] = {
210
+ 'q': 0.5
211
+ };
212
+ fetcher.mediatypes['application/xml'] = {
213
+ 'q': 0.5
214
+ };
215
+ }
216
+ static isElement(node) {
217
+ return node.nodeType === Node.ELEMENT_NODE;
218
+ }
219
+ parse(fetcher, responseText, options) {
220
+ let dom = Util.parseXML(responseText);
221
+
222
+ // XML Semantics defined by root element namespace
223
+ // figure out the root element
224
+ for (let c = 0; c < dom.childNodes.length; c++) {
225
+ const node = dom.childNodes[c];
226
+ // is this node an element?
227
+ if (XMLHandler.isElement(node)) {
228
+ // We've found the first element, it's the root
229
+ let ns = node.namespaceURI;
230
+
231
+ // Is it RDF/XML?
232
+ if (ns && ns === ns['rdf']) {
233
+ fetcher.addStatus(options.req, 'Has XML root element in the RDF namespace, so assume RDF/XML.');
234
+ let rdfHandler = new RDFXMLHandler(this.response, dom);
235
+ return rdfHandler.parse(fetcher, responseText, options);
287
236
  }
237
+ break;
288
238
  }
289
-
290
- // At this point we should check the namespace document (cache it!) and
291
- // look for a GRDDL transform
292
- // @@ Get namespace document <n>, parse it, look for <n> grddl:namespaceTransform ?y
293
- // Apply ?y to dom
294
- // We give up. What dialect is this?
295
- return fetcher.failFetch(options, 'Unsupported dialect of XML: not RDF or XHTML namespace, etc.\n' + responseText.slice(0, 80), 901);
296
- }
297
- }], [{
298
- key: "toString",
299
- value: function toString() {
300
- return 'XMLHandler';
301
- }
302
- }, {
303
- key: "register",
304
- value: function register(fetcher) {
305
- fetcher.mediatypes['text/xml'] = {
306
- 'q': 0.5
307
- };
308
- fetcher.mediatypes['application/xml'] = {
309
- 'q': 0.5
310
- };
311
- }
312
- }, {
313
- key: "isElement",
314
- value: function isElement(node) {
315
- return node.nodeType === Node.ELEMENT_NODE;
316
239
  }
317
- }]);
318
- }(Handler);
319
- XMLHandler.pattern = new RegExp('(text|application)/(.*)xml');
320
- var HTMLHandler = /*#__PURE__*/function (_Handler5) {
321
- function HTMLHandler() {
322
- _classCallCheck(this, HTMLHandler);
323
- return _callSuper(this, HTMLHandler, arguments);
324
- }
325
- _inherits(HTMLHandler, _Handler5);
326
- return _createClass(HTMLHandler, [{
327
- key: "parse",
328
- value: function parse(fetcher, responseText, options) {
329
- var kb = fetcher.store;
330
-
331
- // We only handle XHTML so we have to figure out if this is XML
332
- // log.info("Sniffing HTML " + xhr.resource + " for XHTML.")
333
- if (isXML(responseText)) {
334
- fetcher.addStatus(options.req, "Has an XML declaration. We'll assume " + "it's XHTML as the content-type was text/html.\n");
335
- var xhtmlHandler = new XHTMLHandler(this.response);
240
+
241
+ // Or it could be XHTML?
242
+ // Maybe it has an XHTML DOCTYPE?
243
+ if (dom.doctype) {
244
+ // log.info("We found a DOCTYPE in " + xhr.resource)
245
+ if (dom.doctype.name === 'html' && dom.doctype.publicId.match(/^-\/\/W3C\/\/DTD XHTML/) && dom.doctype.systemId.match(/http:\/\/www.w3.org\/TR\/xhtml/)) {
246
+ fetcher.addStatus(options.req, 'Has XHTML DOCTYPE. Switching to XHTML Handler.\n');
247
+ let xhtmlHandler = new XHTMLHandler(this.response, dom);
336
248
  return xhtmlHandler.parse(fetcher, responseText, options);
337
249
  }
250
+ }
338
251
 
339
- // DOCTYPE html
340
- if (isXHTML(responseText)) {
341
- fetcher.addStatus(options.req, 'Has XHTML DOCTYPE. Switching to XHTMLHandler.\n');
342
- var _xhtmlHandler2 = new XHTMLHandler(this.response);
343
- return _xhtmlHandler2.parse(fetcher, responseText, options);
252
+ // Or what about an XHTML namespace?
253
+ let html = dom.getElementsByTagName('html')[0];
254
+ if (html) {
255
+ let xmlns = html.getAttribute('xmlns');
256
+ if (xmlns && xmlns.match(/^http:\/\/www.w3.org\/1999\/xhtml/)) {
257
+ fetcher.addStatus(options.req, 'Has a default namespace for ' + 'XHTML. Switching to XHTMLHandler.\n');
258
+ let xhtmlHandler = new XHTMLHandler(this.response, dom);
259
+ return xhtmlHandler.parse(fetcher, responseText, options);
344
260
  }
261
+ }
345
262
 
346
- // xmlns
347
- if (isXMLNS(responseText)) {
348
- fetcher.addStatus(options.req, 'Has default namespace for XHTML, so switching to XHTMLHandler.\n');
349
- var _xhtmlHandler3 = new XHTMLHandler(this.response);
350
- return _xhtmlHandler3.parse(fetcher, responseText, options);
351
- }
263
+ // At this point we should check the namespace document (cache it!) and
264
+ // look for a GRDDL transform
265
+ // @@ Get namespace document <n>, parse it, look for <n> grddl:namespaceTransform ?y
266
+ // Apply ?y to dom
267
+ // We give up. What dialect is this?
268
+ return fetcher.failFetch(options, 'Unsupported dialect of XML: not RDF or XHTML namespace, etc.\n' + responseText.slice(0, 80), 901);
269
+ }
270
+ }
271
+ XMLHandler.pattern = new RegExp('(text|application)/(.*)xml');
272
+ class HTMLHandler extends Handler {
273
+ static toString() {
274
+ return 'HTMLHandler';
275
+ }
276
+ static register(fetcher) {
277
+ fetcher.mediatypes['text/html'] = {
278
+ 'q': 0.9
279
+ };
280
+ }
281
+ parse(fetcher, responseText, options) {
282
+ let kb = fetcher.store;
352
283
 
353
- // dc:title
354
- // no need to escape '/' here
355
- var titleMatch = new RegExp('<title>([\\s\\S]+?)</title>', 'im').exec(responseText);
356
- if (titleMatch) {
357
- kb.add(options.resource, ns.dc('title'), kb.rdfFactory.literal(titleMatch[1]), options.resource); // think about xml:lang later
358
- }
359
- kb.add(options.resource, ns.rdf('type'), ns.link('WebPage'), fetcher.appNode);
360
- fetcher.addStatus(options.req, 'non-XML HTML document, not parsed for data.');
361
- return fetcher.doneFetch(options, this.response);
362
- }
363
- }], [{
364
- key: "toString",
365
- value: function toString() {
366
- return 'HTMLHandler';
367
- }
368
- }, {
369
- key: "register",
370
- value: function register(fetcher) {
371
- fetcher.mediatypes['text/html'] = {
372
- 'q': 0.9
373
- };
284
+ // We only handle XHTML so we have to figure out if this is XML
285
+ // log.info("Sniffing HTML " + xhr.resource + " for XHTML.")
286
+ if (isXML(responseText)) {
287
+ fetcher.addStatus(options.req, "Has an XML declaration. We'll assume " + "it's XHTML as the content-type was text/html.\n");
288
+ let xhtmlHandler = new XHTMLHandler(this.response);
289
+ return xhtmlHandler.parse(fetcher, responseText, options);
374
290
  }
375
- }]);
376
- }(Handler);
377
- HTMLHandler.pattern = new RegExp('text/html');
378
- var JsonLdHandler = /*#__PURE__*/function (_Handler6) {
379
- function JsonLdHandler() {
380
- _classCallCheck(this, JsonLdHandler);
381
- return _callSuper(this, JsonLdHandler, arguments);
382
- }
383
- _inherits(JsonLdHandler, _Handler6);
384
- return _createClass(JsonLdHandler, [{
385
- key: "parse",
386
- value: function parse(fetcher, responseText, options, response) {
387
- var kb = fetcher.store;
388
- return new Promise(function (resolve, reject) {
389
- try {
390
- jsonldParser(responseText, kb, options.original.value, function () {
391
- resolve(fetcher.doneFetch(options, response));
392
- });
393
- } catch (err) {
394
- var msg = 'Error trying to parse ' + options.resource + ' as JSON-LD:\n' + err; // not err.stack -- irrelevant
395
- resolve(fetcher.failFetch(options, msg, 'parse_error', response));
396
- }
397
- });
398
- }
399
- }], [{
400
- key: "toString",
401
- value: function toString() {
402
- return 'JsonLdHandler';
291
+
292
+ // DOCTYPE html
293
+ if (isXHTML(responseText)) {
294
+ fetcher.addStatus(options.req, 'Has XHTML DOCTYPE. Switching to XHTMLHandler.\n');
295
+ let xhtmlHandler = new XHTMLHandler(this.response);
296
+ return xhtmlHandler.parse(fetcher, responseText, options);
403
297
  }
404
- }, {
405
- key: "register",
406
- value: function register(fetcher) {
407
- fetcher.mediatypes['application/ld+json'] = {
408
- 'q': 0.9
409
- };
298
+
299
+ // xmlns
300
+ if (isXMLNS(responseText)) {
301
+ fetcher.addStatus(options.req, 'Has default namespace for XHTML, so switching to XHTMLHandler.\n');
302
+ let xhtmlHandler = new XHTMLHandler(this.response);
303
+ return xhtmlHandler.parse(fetcher, responseText, options);
410
304
  }
411
- }]);
412
- }(Handler);
413
- JsonLdHandler.pattern = /application\/ld\+json/;
414
- var TextHandler = /*#__PURE__*/function (_Handler7) {
415
- function TextHandler() {
416
- _classCallCheck(this, TextHandler);
417
- return _callSuper(this, TextHandler, arguments);
418
- }
419
- _inherits(TextHandler, _Handler7);
420
- return _createClass(TextHandler, [{
421
- key: "parse",
422
- value: function parse(fetcher, responseText, options) {
423
- // We only speak dialects of XML right now. Is this XML?
424
-
425
- // Look for an XML declaration
426
- if (isXML(responseText)) {
427
- fetcher.addStatus(options.req, 'Warning: ' + options.resource + " has an XML declaration. We'll assume " + "it's XML but its content-type wasn't XML.\n");
428
- var xmlHandler = new XMLHandler(this.response);
429
- return xmlHandler.parse(fetcher, responseText, options);
430
- }
431
305
 
432
- // Look for an XML declaration
433
- if (responseText.slice(0, 500).match(/xmlns:/)) {
434
- fetcher.addStatus(options.req, "May have an XML namespace. We'll assume " + "it's XML but its content-type wasn't XML.\n");
435
- var _xmlHandler = new XMLHandler(this.response);
436
- return _xmlHandler.parse(fetcher, responseText, options);
306
+ // dc:title
307
+ // no need to escape '/' here
308
+ let titleMatch = new RegExp('<title>([\\s\\S]+?)</title>', 'im').exec(responseText);
309
+ if (titleMatch) {
310
+ kb.add(options.resource, ns.dc('title'), kb.rdfFactory.literal(titleMatch[1]), options.resource); // think about xml:lang later
311
+ }
312
+ kb.add(options.resource, ns.rdf('type'), ns.link('WebPage'), fetcher.appNode);
313
+ fetcher.addStatus(options.req, 'non-XML HTML document, not parsed for data.');
314
+ return fetcher.doneFetch(options, this.response);
315
+ }
316
+ }
317
+ HTMLHandler.pattern = new RegExp('text/html');
318
+ class JsonLdHandler extends Handler {
319
+ static toString() {
320
+ return 'JsonLdHandler';
321
+ }
322
+ static register(fetcher) {
323
+ fetcher.mediatypes['application/ld+json'] = {
324
+ 'q': 0.9
325
+ };
326
+ }
327
+ parse(fetcher, responseText, options, response) {
328
+ 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));
437
337
  }
338
+ });
339
+ }
340
+ }
341
+ JsonLdHandler.pattern = /application\/ld\+json/;
342
+ class TextHandler extends Handler {
343
+ static toString() {
344
+ return 'TextHandler';
345
+ }
346
+ static register(fetcher) {
347
+ fetcher.mediatypes['text/plain'] = {
348
+ 'q': 0.5
349
+ };
350
+ }
351
+ parse(fetcher, responseText, options) {
352
+ // We only speak dialects of XML right now. Is this XML?
438
353
 
439
- // We give up finding semantics - this is not an error, just no data
440
- fetcher.addStatus(options.req, 'Plain text document, no known RDF semantics.');
441
- return fetcher.doneFetch(options, this.response);
354
+ // Look for an XML declaration
355
+ if (isXML(responseText)) {
356
+ fetcher.addStatus(options.req, 'Warning: ' + options.resource + " has an XML declaration. We'll assume " + "it's XML but its content-type wasn't XML.\n");
357
+ let xmlHandler = new XMLHandler(this.response);
358
+ return xmlHandler.parse(fetcher, responseText, options);
442
359
  }
443
- }], [{
444
- key: "toString",
445
- value: function toString() {
446
- return 'TextHandler';
447
- }
448
- }, {
449
- key: "register",
450
- value: function register(fetcher) {
451
- fetcher.mediatypes['text/plain'] = {
452
- 'q': 0.5
453
- };
360
+
361
+ // Look for an XML declaration
362
+ if (responseText.slice(0, 500).match(/xmlns:/)) {
363
+ fetcher.addStatus(options.req, "May have an XML namespace. We'll assume " + "it's XML but its content-type wasn't XML.\n");
364
+ let xmlHandler = new XMLHandler(this.response);
365
+ return xmlHandler.parse(fetcher, responseText, options);
454
366
  }
455
- }]);
456
- }(Handler);
367
+
368
+ // We give up finding semantics - this is not an error, just no data
369
+ fetcher.addStatus(options.req, 'Plain text document, no known RDF semantics.');
370
+ return fetcher.doneFetch(options, this.response);
371
+ }
372
+ }
457
373
  TextHandler.pattern = new RegExp('text/plain');
458
- var N3Handler = /*#__PURE__*/function (_Handler8) {
459
- function N3Handler() {
460
- _classCallCheck(this, N3Handler);
461
- return _callSuper(this, N3Handler, arguments);
462
- }
463
- _inherits(N3Handler, _Handler8);
464
- return _createClass(N3Handler, [{
465
- key: "parse",
466
- value: function parse(fetcher, responseText, options, response) {
467
- // Parse the text of this N3 file
468
- var kb = fetcher.store;
469
- var p = N3Parser(kb, kb, options.original.value, options.original.value, null, null, '', null);
470
- // p.loadBuf(xhr.responseText)
471
- try {
472
- p.loadBuf(responseText);
473
- } catch (err) {
474
- var msg = 'Error trying to parse ' + options.resource + ' as Notation3:\n' + err; // not err.stack -- irrelevant
475
- return fetcher.failFetch(options, msg, 'parse_error', response);
476
- }
477
- fetcher.addStatus(options.req, 'N3 parsed: ' + p.statementCount + ' triples in ' + p.lines + ' lines.');
478
- fetcher.store.add(options.original, ns.rdf('type'), ns.link('RDFDocument'), fetcher.appNode);
479
- return fetcher.doneFetch(options, this.response);
480
- }
481
- }], [{
482
- key: "toString",
483
- value: function toString() {
484
- return 'N3Handler';
485
- }
486
- }, {
487
- key: "register",
488
- value: function register(fetcher) {
489
- fetcher.mediatypes['text/n3'] = {
490
- 'q': '1.0'
491
- }; // as per 2008 spec
492
- /*
493
- fetcher.mediatypes['application/x-turtle'] = {
494
- 'q': 1.0
495
- } // pre 2008
496
- */
497
- fetcher.mediatypes['text/turtle'] = {
498
- 'q': 1.0
499
- }; // post 2008
500
- }
501
- }]);
502
- }(Handler);
374
+ class N3Handler extends Handler {
375
+ static toString() {
376
+ return 'N3Handler';
377
+ }
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
390
+ }
391
+ parse(fetcher, responseText, options, response) {
392
+ // Parse the text of this N3 file
393
+ let kb = fetcher.store;
394
+ let p = N3Parser(kb, kb, options.original.value, options.original.value, null, null, '', null);
395
+ // p.loadBuf(xhr.responseText)
396
+ try {
397
+ p.loadBuf(responseText);
398
+ } catch (err) {
399
+ let msg = 'Error trying to parse ' + options.resource + ' as Notation3:\n' + err; // not err.stack -- irrelevant
400
+ return fetcher.failFetch(options, msg, 'parse_error', response);
401
+ }
402
+ fetcher.addStatus(options.req, 'N3 parsed: ' + p.statementCount + ' triples in ' + p.lines + ' lines.');
403
+ fetcher.store.add(options.original, ns.rdf('type'), ns.link('RDFDocument'), fetcher.appNode);
404
+ return fetcher.doneFetch(options, this.response);
405
+ }
406
+ }
503
407
  N3Handler.pattern = new RegExp('(application|text)/(x-)?(rdf\\+)?(n3|turtle)');
504
- var defaultHandlers = {
505
- RDFXMLHandler: RDFXMLHandler,
506
- XHTMLHandler: XHTMLHandler,
507
- XMLHandler: XMLHandler,
508
- HTMLHandler: HTMLHandler,
509
- TextHandler: TextHandler,
510
- N3Handler: N3Handler,
511
- JsonLdHandler: JsonLdHandler
408
+ const defaultHandlers = {
409
+ RDFXMLHandler,
410
+ XHTMLHandler,
411
+ XMLHandler,
412
+ HTMLHandler,
413
+ TextHandler,
414
+ N3Handler,
415
+ JsonLdHandler
512
416
  };
513
417
  function isXHTML(responseText) {
514
- var docTypeStart = responseText.indexOf('<!DOCTYPE html');
515
- var docTypeEnd = responseText.indexOf('>');
418
+ const docTypeStart = responseText.indexOf('<!DOCTYPE html');
419
+ const docTypeEnd = responseText.indexOf('>');
516
420
  if (docTypeStart === -1 || docTypeEnd === -1 || docTypeStart > docTypeEnd) {
517
421
  return false;
518
422
  }
519
423
  return responseText.substr(docTypeStart, docTypeEnd - docTypeStart).indexOf('XHTML') !== -1;
520
424
  }
521
425
  function isXML(responseText) {
522
- var match = responseText.match(/\s*<\?xml\s+version\s*=[^<>]+\?>/);
426
+ const match = responseText.match(/\s*<\?xml\s+version\s*=[^<>]+\?>/);
523
427
  return !!match;
524
428
  }
525
429
  function isXMLNS(responseText) {
526
- var match = responseText.match(/[^(<html)]*<html\s+[^<]*xmlns=['"]http:\/\/www.w3.org\/1999\/xhtml["'][^<]*>/);
430
+ const match = responseText.match(/[^(<html)]*<html\s+[^<]*xmlns=['"]http:\/\/www.w3.org\/1999\/xhtml["'][^<]*>/);
527
431
  return !!match;
528
432
  }
529
433
 
530
434
  // Not sure about the shapes of this. Response? FetchError?
531
435
 
532
436
  /** Differs from normal Fetch, has an extended Response type */
437
+
533
438
  /** Fetcher
534
439
  *
535
440
  * The Fetcher object is a helper object for a quadstore
@@ -538,11 +443,9 @@ function isXMLNS(responseText) {
538
443
  * figuring how to parse them. It will also refresh, remove, the data
539
444
  * and put back the data to the web.
540
445
  */
541
- var Fetcher = /*#__PURE__*/function () {
542
- function Fetcher(store) {
543
- var _this = this;
544
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
545
- _classCallCheck(this, Fetcher);
446
+ export default class Fetcher {
447
+ constructor(store) {
448
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
546
449
  _defineProperty(this, "store", void 0);
547
450
  _defineProperty(this, "timeout", void 0);
548
451
  _defineProperty(this, "_fetch", void 0);
@@ -611,280 +514,287 @@ var Fetcher = /*#__PURE__*/function () {
611
514
  // 'refresh', 'retract', 'done'])
612
515
  // In switching to fetch(), 'recv', 'headers' and 'load' do not make sense
613
516
  Util.callbackify(this, ['request', 'fail', 'refresh', 'retract', 'done']);
614
- Object.keys(options.handlers || defaultHandlers).map(function (key) {
615
- return _this.addHandler(defaultHandlers[key]);
616
- });
517
+ Object.keys(options.handlers || defaultHandlers).map(key => this.addHandler(defaultHandlers[key]));
617
518
  }
618
- return _createClass(Fetcher, [{
619
- key: "load",
620
- value:
621
- /**
622
- * Promise-based load function
623
- *
624
- * Loads a web resource or resources into the store.
625
- *
626
- * A resource may be given as NamedNode object, or as a plain URI.
627
- * an array of resources will be given, in which they will be fetched in parallel.
628
- * By default, the HTTP headers are recorded also, in the same store, in a separate graph.
629
- * This allows code like editable() for example to test things about the resource.
630
- *
631
- * @param uri {Array<RDFlibNamedNode>|Array<string>|RDFlibNamedNode|string}
632
- *
633
- * @param [options={}] {Object}
634
- *
635
- * @param [options.fetch] {Function}
636
- *
637
- * @param [options.referringTerm] {RDFlibNamedNode} Referring term, the resource which
638
- * referred to this (for tracking bad links)
639
- *
640
- * @param [options.contentType] {string} Provided content type (for writes)
641
- *
642
- * @param [options.forceContentType] {string} Override the incoming header to
643
- * force the data to be treated as this content-type (for reads)
644
- *
645
- * @param [options.force] {boolean} Load the data even if loaded before.
646
- * Also sets the `Cache-Control:` header to `no-cache`
647
- *
648
- * @param [options.baseURI=docuri] {Node|string} Original uri to preserve
649
- * through proxying etc (`xhr.original`).
650
- *
651
- * @param [options.proxyUsed] {boolean} Whether this request is a retry via
652
- * a proxy (generally done from an error handler)
653
- *
654
- * @param [options.withCredentials] {boolean} flag for XHR/CORS etc
655
- *
656
- * @param [options.clearPreviousData] {boolean} Before we parse new data,
657
- * clear old, but only on status 200 responses
658
- *
659
- * @param [options.noMeta] {boolean} Prevents the addition of various metadata
660
- * triples (about the fetch request) to the store
661
- *
662
- * @param [options.noRDFa] {boolean}
663
- *
664
- * @returns {Promise<Result>}
665
- */
666
- function load(uri) {
667
- var _this2 = this;
668
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
669
- options = Object.assign({}, options); // Take a copy as we add stuff to the options!!
670
- if (uri instanceof Array) {
671
- return Promise.all(uri.map(function (x) {
672
- return _this2.load(x, Object.assign({}, options));
673
- }));
519
+ static crossSiteProxy(uri) {
520
+ if (Fetcher.crossSiteProxyTemplate) {
521
+ return Fetcher.crossSiteProxyTemplate.replace('{uri}', encodeURIComponent(uri));
522
+ } else {
523
+ return undefined;
524
+ }
525
+ }
526
+ static offlineOverride(uri) {
527
+ // Map the URI to a localhost proxy if we are running on localhost
528
+ // This is used for working offline, e.g. on planes.
529
+ // Is the script itself is running in localhost, then access all
530
+ // data in a localhost mirror.
531
+ // Do not remove without checking with TimBL
532
+ let requestedURI = uri;
533
+ var UI;
534
+ if (typeof window !== 'undefined' && window.panes && (UI = window.panes.UI) && UI.preferences && UI.preferences.get('offlineModeUsingLocalhost')) {
535
+ if (requestedURI.slice(0, 7) === 'http://' && requestedURI.slice(7, 17) !== 'localhost/') {
536
+ requestedURI = 'http://localhost/' + requestedURI.slice(7);
537
+ log.warn('Localhost kludge for offline use: actually getting <' + requestedURI + '>');
538
+ } else {
539
+ // log.warn("Localhost kludge NOT USED <" + requestedURI + ">")
674
540
  }
675
- var uriIn = uri;
676
- var docuri = termValue(uriIn);
677
- docuri = docuri.split('#')[0];
678
- options = this.initFetchOptions(docuri, options);
679
- // if metadata flaged clear cache and removeDocument
680
- var meta = this.appNode;
681
- var kb = this.store;
682
- var requests = kb.statementsMatching(undefined, this.ns.link('requestedURI'), kb.sym(docuri), meta).map(function (st) {
683
- return st.subject;
684
- });
685
- var _iterator = _createForOfIteratorHelper(requests),
686
- _step;
687
- try {
688
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
689
- var request = _step.value;
690
- var _response = kb.any(request, this.ns.link('response'), null, meta);
691
- if (_response != undefined) {
692
- // ts
693
- var quad = kb.statementsMatching(_response, this.ns.link('outOfDate'), true, meta);
694
- kb.remove(quad);
695
- options.force = true;
696
- options.clearPreviousData = true;
697
- }
541
+ } else {
542
+ // log.warn("Localhost kludge OFF offline use: actually getting <" +
543
+ // requestedURI + ">")
544
+ }
545
+ return requestedURI;
546
+ }
547
+ static proxyIfNecessary(uri) {
548
+ var UI;
549
+ if (typeof window !== 'undefined' && window.panes && (UI = window.panes.UI) && UI.isExtension) {
550
+ return uri;
551
+ } // Extension does not need proxy
552
+
553
+ if (typeof $SolidTestEnvironment !== 'undefined' && $SolidTestEnvironment.localSiteMap) {
554
+ // nested dictionaries of URI parts from origin down
555
+ let hostpath = uri.split('/').slice(2); // the bit after the //
556
+
557
+ const lookup = (parts, index) => {
558
+ let z = index[parts.shift()];
559
+ if (!z) {
560
+ return null;
698
561
  }
699
- } catch (err) {
700
- _iterator.e(err);
701
- } finally {
702
- _iterator.f();
703
- }
704
- var initialisedOptions = this.initFetchOptions(docuri, options);
705
- return this.pendingFetchPromise(docuri, initialisedOptions.baseURI, initialisedOptions);
706
- }
707
- }, {
708
- key: "pendingFetchPromise",
709
- value: function () {
710
- var _pendingFetchPromise = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(uri, originalUri, options) {
711
- var _this3 = this;
712
- var pendingPromise;
713
- return _regeneratorRuntime.wrap(function _callee$(_context) {
714
- while (1) switch (_context.prev = _context.next) {
715
- case 0:
716
- _context.t0 = !options.force;
717
- if (!_context.t0) {
718
- _context.next = 5;
719
- break;
720
- }
721
- _context.next = 4;
722
- return this.fetchQueue[originalUri];
723
- case 4:
724
- _context.t0 = _context.sent;
725
- case 5:
726
- if (!_context.t0) {
727
- _context.next = 9;
728
- break;
729
- }
730
- pendingPromise = this.fetchQueue[originalUri];
731
- _context.next = 12;
732
- break;
733
- case 9:
734
- pendingPromise = Promise.race([this.setRequestTimeout(uri, options), this.fetchUri(uri, options)]);
735
- this.fetchQueue[originalUri] = pendingPromise;
736
-
737
- // Clean up the queued promise after a time, if it's resolved
738
- this.cleanupFetchRequest(originalUri, undefined, this.timeout);
739
- case 12:
740
- return _context.abrupt("return", pendingPromise.then(function (x) {
741
- if (uri in _this3.timeouts) {
742
- _this3.timeouts[uri].forEach(clearTimeout);
743
- delete _this3.timeouts[uri];
744
- }
745
- return x;
746
- }));
747
- case 13:
748
- case "end":
749
- return _context.stop();
750
- }
751
- }, _callee, this);
752
- }));
753
- function pendingFetchPromise(_x, _x2, _x3) {
754
- return _pendingFetchPromise.apply(this, arguments);
755
- }
756
- return pendingFetchPromise;
757
- }()
758
- /**
759
- * @param _options - DEPRECATED
760
- */
761
- }, {
762
- key: "cleanupFetchRequest",
763
- value: function cleanupFetchRequest(originalUri, _options, timeout) {
764
- var _this4 = this;
765
- if (_options !== undefined) {
766
- console.warn("_options is deprecated");
767
- }
768
- this.timeouts[originalUri] = (this.timeouts[originalUri] || []).concat(setTimeout(function () {
769
- if (!_this4.isPending(originalUri)) {
770
- delete _this4.fetchQueue[originalUri];
562
+ if (typeof z === 'string') {
563
+ return z + parts.join('/');
771
564
  }
772
- }, timeout));
773
- }
774
- }, {
775
- key: "initFetchOptions",
776
- value: function initFetchOptions(uri, options) {
777
- var kb = this.store;
778
- var isGet = !options.method || options.method.toUpperCase() === 'GET';
779
- if (!isGet) {
780
- options.force = true;
781
- }
782
- options.resource = kb.rdfFactory.namedNode(uri); // This might be proxified
783
- options.baseURI = options.baseURI || uri; // Preserve though proxying etc
784
- options.original = kb.rdfFactory.namedNode(options.baseURI);
785
- options.req = kb.bnode();
786
- options.headers = options.headers || new Headers();
787
- if (options.contentType) {
788
- // @ts-ignore
789
- options.headers['content-type'] = options.contentType;
790
- }
791
- if (options.force) {
792
- options.cache = 'no-cache';
565
+ if (!parts) {
566
+ return null;
567
+ }
568
+ return lookup(parts, z);
569
+ };
570
+ const y = lookup(hostpath, $SolidTestEnvironment.localSiteMap);
571
+ if (y) {
572
+ return y;
793
573
  }
794
- var acceptString = this.acceptString();
795
- // @ts-ignore
796
- options.headers['accept'] = acceptString;
797
- var requestedURI = Fetcher.offlineOverride(uri);
798
- options.requestedURI = requestedURI;
799
- Fetcher.setCredentials(requestedURI, options);
800
- var actualProxyURI = Fetcher.proxyIfNecessary(requestedURI);
801
- if (requestedURI !== actualProxyURI) {
802
- options.proxyUsed = true;
574
+ }
575
+
576
+ // browser does 2014 on as https browser script not trusted
577
+ // If the web app origin is https: then the mixed content rules
578
+ // prevent it loading insecure http: stuff so we need proxy.
579
+ if (Fetcher.crossSiteProxyTemplate && typeof document !== 'undefined' && document.location && ('' + document.location).slice(0, 6) === 'https:' &&
580
+ // origin is secure
581
+ uri.slice(0, 5) === 'http:') {
582
+ // requested data is not
583
+ return Fetcher.crossSiteProxyTemplate.replace('{uri}', encodeURIComponent(uri));
584
+ }
585
+ return uri;
586
+ }
587
+
588
+ /**
589
+ * Tests whether the uri's protocol is supported by the Fetcher.
590
+ * @param uri
591
+ */
592
+ static unsupportedProtocol(uri) {
593
+ let pcol = Uri.protocol(uri);
594
+ return pcol === 'tel' || pcol === 'mailto' || pcol === 'urn';
595
+ }
596
+
597
+ /** Decide on credentials using old XXHR api or new fetch() one
598
+ * @param requestedURI
599
+ * @param options
600
+ */
601
+ static setCredentials(requestedURI) {
602
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
603
+ // 2014 CORS problem:
604
+ // XMLHttpRequest cannot load http://www.w3.org/People/Berners-Lee/card.
605
+ // A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin'
606
+ // header when the credentials flag is true.
607
+ // @ Many ontology files under http: and need CORS wildcard ->
608
+ // can't have credentials
609
+ if (options.credentials === undefined) {
610
+ // Caller using new fetch convention
611
+ if (options.withCredentials !== undefined) {
612
+ // XHR style is what Fetcher specified before
613
+ options.credentials = options.withCredentials ? 'include' : 'omit';
614
+ } else {
615
+ options.credentials = 'include'; // default is to be logged on
803
616
  }
804
- options.actualProxyURI = actualProxyURI;
805
- return options;
806
617
  }
618
+ }
807
619
 
808
- /**
809
- * (The promise chain ends in either a `failFetch()` or a `doneFetch()`)
810
- *
811
- * @param docuri {string}
812
- * @param options {Object}
813
- *
814
- * @returns {Promise<Object>} fetch() result or an { error, status } object
815
- */
816
- }, {
817
- key: "fetchUri",
818
- value: function fetchUri(docuri, options) {
819
- var _this5 = this;
820
- if (!docuri) {
821
- return Promise.reject(new Error('Cannot fetch an empty uri'));
620
+ /**
621
+ * Promise-based load function
622
+ *
623
+ * Loads a web resource or resources into the store.
624
+ *
625
+ * A resource may be given as NamedNode object, or as a plain URI.
626
+ * an array of resources will be given, in which they will be fetched in parallel.
627
+ * By default, the HTTP headers are recorded also, in the same store, in a separate graph.
628
+ * This allows code like editable() for example to test things about the resource.
629
+ *
630
+ * @param uri {Array<RDFlibNamedNode>|Array<string>|RDFlibNamedNode|string}
631
+ *
632
+ * @param [options={}] {Object}
633
+ *
634
+ * @param [options.fetch] {Function}
635
+ *
636
+ * @param [options.referringTerm] {RDFlibNamedNode} Referring term, the resource which
637
+ * referred to this (for tracking bad links)
638
+ *
639
+ * @param [options.contentType] {string} Provided content type (for writes)
640
+ *
641
+ * @param [options.forceContentType] {string} Override the incoming header to
642
+ * force the data to be treated as this content-type (for reads)
643
+ *
644
+ * @param [options.force] {boolean} Load the data even if loaded before.
645
+ * Also sets the `Cache-Control:` header to `no-cache`
646
+ *
647
+ * @param [options.baseURI=docuri] {Node|string} Original uri to preserve
648
+ * through proxying etc (`xhr.original`).
649
+ *
650
+ * @param [options.proxyUsed] {boolean} Whether this request is a retry via
651
+ * a proxy (generally done from an error handler)
652
+ *
653
+ * @param [options.withCredentials] {boolean} flag for XHR/CORS etc
654
+ *
655
+ * @param [options.clearPreviousData] {boolean} Before we parse new data,
656
+ * clear old, but only on status 200 responses
657
+ *
658
+ * @param [options.noMeta] {boolean} Prevents the addition of various metadata
659
+ * triples (about the fetch request) to the store
660
+ *
661
+ * @param [options.noRDFa] {boolean}
662
+ *
663
+ * @returns {Promise<Result>}
664
+ */
665
+ load(uri) {
666
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
667
+ options = Object.assign({}, options); // Take a copy as we add stuff to the options!!
668
+ if (uri instanceof Array) {
669
+ return Promise.all(uri.map(x => {
670
+ return this.load(x, Object.assign({}, options));
671
+ }));
672
+ }
673
+ const uriIn = uri;
674
+ let docuri = termValue(uriIn);
675
+ docuri = docuri.split('#')[0];
676
+ options = this.initFetchOptions(docuri, options);
677
+ // if metadata flaged clear cache and removeDocument
678
+ const meta = this.appNode;
679
+ const kb = this.store;
680
+ const requests = kb.statementsMatching(undefined, this.ns.link('requestedURI'), kb.sym(docuri), meta).map(st => st.subject);
681
+ for (const request of requests) {
682
+ const response = kb.any(request, this.ns.link('response'), null, meta);
683
+ if (response != undefined) {
684
+ // ts
685
+ const quad = kb.statementsMatching(response, this.ns.link('outOfDate'), true, meta);
686
+ kb.remove(quad);
687
+ options.force = true;
688
+ options.clearPreviousData = true;
822
689
  }
823
- if (Fetcher.unsupportedProtocol(docuri)) {
824
- return this.failFetch(options, 'fetcher: Unsupported protocol', 'unsupported_protocol');
690
+ }
691
+ const initialisedOptions = this.initFetchOptions(docuri, options);
692
+ return this.pendingFetchPromise(docuri, initialisedOptions.baseURI, initialisedOptions);
693
+ }
694
+ async pendingFetchPromise(uri, originalUri, options) {
695
+ let pendingPromise;
696
+
697
+ // Check to see if some request is already dealing with this uri
698
+ if (!options.force && (await this.fetchQueue[originalUri])) {
699
+ pendingPromise = this.fetchQueue[originalUri];
700
+ } else {
701
+ pendingPromise = Promise.race([this.setRequestTimeout(uri, options), this.fetchUri(uri, options)]);
702
+ this.fetchQueue[originalUri] = pendingPromise;
703
+
704
+ // Clean up the queued promise after a time, if it's resolved
705
+ this.cleanupFetchRequest(originalUri, undefined, this.timeout);
706
+ }
707
+ return pendingPromise.then(x => {
708
+ if (uri in this.timeouts) {
709
+ this.timeouts[uri].forEach(clearTimeout);
710
+ delete this.timeouts[uri];
825
711
  }
826
- var state = this.getState(docuri);
827
- if (!options.force) {
828
- if (state === 'fetched') {
829
- // URI already fetched and added to store
830
- return Promise.resolve(
831
- // @ts-ignore This is not a valid response object
832
- this.doneFetch(options, {
833
- status: 200,
834
- ok: true,
835
- statusText: 'Already loaded into quadstore.'
836
- }));
837
- }
838
- if (state === 'failed' && this.requested[docuri] === 404) {
839
- // Remember nonexistence
840
- var _message = 'Previously failed: ' + this.requested[docuri];
841
- // @ts-ignore This is not a valid response object
842
- var dummyResponse = {
843
- url: docuri,
844
- // This does not comply to Fetch spec, it can be a string value in rdflib
845
- status: this.requested[docuri],
846
- statusText: _message,
847
- responseText: _message,
848
- headers: new Headers(),
849
- // Headers() ???
850
- ok: false,
851
- body: null,
852
- bodyUsed: false,
853
- size: 0,
854
- timeout: 0
855
- };
856
- return this.failFetch(options, _message, this.requested[docuri], dummyResponse);
857
- }
858
- } else {
859
- // options.force == true
860
- delete this.nonexistent[docuri];
712
+ return x;
713
+ });
714
+ }
715
+
716
+ /**
717
+ * @param _options - DEPRECATED
718
+ */
719
+ cleanupFetchRequest(originalUri, _options, timeout) {
720
+ if (_options !== undefined) {
721
+ console.warn("_options is deprecated");
722
+ }
723
+ this.timeouts[originalUri] = (this.timeouts[originalUri] || []).concat(setTimeout(() => {
724
+ if (!this.isPending(originalUri)) {
725
+ delete this.fetchQueue[originalUri];
861
726
  }
862
- this.fireCallbacks('request', [docuri]);
863
- this.requested[docuri] = true; // mark this uri as 'requested'
727
+ }, timeout));
728
+ }
729
+ initFetchOptions(uri, options) {
730
+ let kb = this.store;
731
+ let isGet = !options.method || options.method.toUpperCase() === 'GET';
732
+ if (!isGet) {
733
+ options.force = true;
734
+ }
735
+ options.resource = kb.rdfFactory.namedNode(uri); // This might be proxified
736
+ options.baseURI = options.baseURI || uri; // Preserve though proxying etc
737
+ options.original = kb.rdfFactory.namedNode(options.baseURI);
738
+ options.req = kb.bnode();
739
+ options.headers = options.headers || new Headers();
740
+ if (options.contentType) {
741
+ // @ts-ignore
742
+ options.headers['content-type'] = options.contentType;
743
+ }
744
+ if (options.force) {
745
+ options.cache = 'no-cache';
746
+ }
747
+ let acceptString = this.acceptString();
748
+ // @ts-ignore
749
+ options.headers['accept'] = acceptString;
750
+ let requestedURI = Fetcher.offlineOverride(uri);
751
+ options.requestedURI = requestedURI;
752
+ Fetcher.setCredentials(requestedURI, options);
753
+ let actualProxyURI = Fetcher.proxyIfNecessary(requestedURI);
754
+ if (requestedURI !== actualProxyURI) {
755
+ options.proxyUsed = true;
756
+ }
757
+ options.actualProxyURI = actualProxyURI;
758
+ return options;
759
+ }
864
760
 
865
- if (!options.noMeta) {
866
- this.saveRequestMetadata(docuri, options);
761
+ /**
762
+ * (The promise chain ends in either a `failFetch()` or a `doneFetch()`)
763
+ *
764
+ * @param docuri {string}
765
+ * @param options {Object}
766
+ *
767
+ * @returns {Promise<Object>} fetch() result or an { error, status } object
768
+ */
769
+ fetchUri(docuri, options) {
770
+ if (!docuri) {
771
+ return Promise.reject(new Error('Cannot fetch an empty uri'));
772
+ }
773
+ if (Fetcher.unsupportedProtocol(docuri)) {
774
+ return this.failFetch(options, 'fetcher: Unsupported protocol', 'unsupported_protocol');
775
+ }
776
+ let state = this.getState(docuri);
777
+ if (!options.force) {
778
+ if (state === 'fetched') {
779
+ // URI already fetched and added to store
780
+ return Promise.resolve(
781
+ // @ts-ignore This is not a valid response object
782
+ this.doneFetch(options, {
783
+ status: 200,
784
+ ok: true,
785
+ statusText: 'Already loaded into quadstore.'
786
+ }));
867
787
  }
868
- var actualProxyURI = options.actualProxyURI;
869
-
870
- // Map might get mistakenly added into headers
871
- // error TS2339: Property 'map' does not exist on type 'Headers'.
872
- /* let map
873
- if (options.headers && map in options.headers) {
874
- delete options.headers.map
875
- } */
876
-
877
- return this._fetch(actualProxyURI, options).then(function (response) {
878
- return _this5.handleResponse(response, docuri, options);
879
- }, function (error) {
880
- // @@ handleError?
881
- // @ts-ignore Invalid response object
882
- var dummyResponse = {
883
- url: actualProxyURI,
884
- status: 999,
885
- // @@ what number/string should fetch failures report?
886
- statusText: (error.name || 'network failure') + ': ' + (error.errno || error.code || error.type),
887
- responseText: error.message,
788
+ if (state === 'failed' && this.requested[docuri] === 404) {
789
+ // Remember nonexistence
790
+ let message = 'Previously failed: ' + this.requested[docuri];
791
+ // @ts-ignore This is not a valid response object
792
+ let dummyResponse = {
793
+ url: docuri,
794
+ // This does not comply to Fetch spec, it can be a string value in rdflib
795
+ status: this.requested[docuri],
796
+ statusText: message,
797
+ responseText: message,
888
798
  headers: new Headers(),
889
799
  // Headers() ???
890
800
  ok: false,
@@ -893,1084 +803,889 @@ var Fetcher = /*#__PURE__*/function () {
893
803
  size: 0,
894
804
  timeout: 0
895
805
  };
896
- // console.log('Fetcher: <' + actualProxyURI + '> Non-HTTP fetch exception: ' + error)
897
- return _this5.handleError(dummyResponse, docuri, options); // possible credentials retry
898
- // return this.failFetch(options, 'fetch failed: ' + error, 999, dummyResponse) // Fake status code: fetch exception
899
-
900
- // handleError expects a response so we fake some important bits.
901
- /*
902
- this.handleError(, docuri, options)
903
- */
904
- });
905
- }
806
+ return this.failFetch(options, message, this.requested[docuri], dummyResponse);
807
+ }
808
+ } else {
809
+ // options.force == true
810
+ delete this.nonexistent[docuri];
811
+ }
812
+ this.fireCallbacks('request', [docuri]);
813
+ this.requested[docuri] = true; // mark this uri as 'requested'
814
+
815
+ if (!options.noMeta) {
816
+ this.saveRequestMetadata(docuri, options);
817
+ }
818
+ let {
819
+ actualProxyURI
820
+ } = options;
821
+
822
+ // Map might get mistakenly added into headers
823
+ // error TS2339: Property 'map' does not exist on type 'Headers'.
824
+ /* let map
825
+ if (options.headers && map in options.headers) {
826
+ delete options.headers.map
827
+ } */
828
+
829
+ return this._fetch(actualProxyURI, options).then(response => this.handleResponse(response, docuri, options), error => {
830
+ // @@ handleError?
831
+ // @ts-ignore Invalid response object
832
+ let dummyResponse = {
833
+ url: actualProxyURI,
834
+ status: 999,
835
+ // @@ what number/string should fetch failures report?
836
+ statusText: (error.name || 'network failure') + ': ' + (error.errno || error.code || error.type),
837
+ responseText: error.message,
838
+ headers: new Headers(),
839
+ // Headers() ???
840
+ ok: false,
841
+ body: null,
842
+ bodyUsed: false,
843
+ size: 0,
844
+ timeout: 0
845
+ };
846
+ // console.log('Fetcher: <' + actualProxyURI + '> Non-HTTP fetch exception: ' + error)
847
+ return this.handleError(dummyResponse, docuri, options); // possible credentials retry
848
+ // return this.failFetch(options, 'fetch failed: ' + error, 999, dummyResponse) // Fake status code: fetch exception
906
849
 
907
- /**
908
- * Asks for a doc to be loaded if necessary then calls back
909
- *
910
- * Calling methods:
911
- * nowOrWhenFetched (uri, userCallback)
912
- * nowOrWhenFetched (uri, options, userCallback)
913
- * nowOrWhenFetched (uri, referringTerm, userCallback, options) <-- old
914
- * nowOrWhenFetched (uri, referringTerm, userCallback) <-- old
915
- *
916
- * Options include:
917
- * referringTerm The document in which this link was found.
918
- * this is valuable when finding the source of bad URIs
919
- * force boolean. Never mind whether you have tried before,
920
- * load this from scratch.
921
- * forceContentType Override the incoming header to force the data to be
922
- * treated as this content-type.
923
- *
924
- * Callback function takes:
925
- *
926
- * ok True if the fetch worked, and got a 200 response.
927
- * False if any error happened
928
- *
929
- * errmessage Text error message if not OK.
930
- *
931
- * response The fetch Response object (was: XHR) if there was was one
932
- * includes response.status as the HTTP status if any.
933
- */
934
- }, {
935
- key: "nowOrWhenFetched",
936
- value: function nowOrWhenFetched(uriIn, p2, userCallback) {
937
- var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
938
- var uri = termValue(uriIn);
939
- if (typeof p2 === 'function') {
940
- // nowOrWhenFetched (uri, userCallback)
941
- userCallback = p2;
942
- } else if (typeof p2 === 'undefined') {// original calling signature
943
- // referringTerm = undefined
944
- } else if (isNamedNode(p2)) {
945
- // referringTerm = p2
946
- options.referringTerm = p2;
947
- } else {
948
- // nowOrWhenFetched (uri, options, userCallback)
949
- options = p2;
950
- }
951
- this.load(uri, options).then(function (fetchResponse) {
952
- if (userCallback) {
953
- if (fetchResponse) {
954
- if (fetchResponse.ok) {
955
- userCallback(true, 'OK', fetchResponse);
956
- } else {
957
- var oops = 'HTTP error: Status ' + fetchResponse.status + ' (' + fetchResponse.statusText + ')';
958
- if (fetchResponse.responseText) {
959
- oops += ' ' + fetchResponse.responseText; // not in 404, dns error, nock failure
960
- }
961
- userCallback(false, oops, fetchResponse);
962
- }
850
+ // handleError expects a response so we fake some important bits.
851
+ /*
852
+ this.handleError(, docuri, options)
853
+ */
854
+ });
855
+ }
856
+
857
+ /**
858
+ * Asks for a doc to be loaded if necessary then calls back
859
+ *
860
+ * Calling methods:
861
+ * nowOrWhenFetched (uri, userCallback)
862
+ * nowOrWhenFetched (uri, options, userCallback)
863
+ * nowOrWhenFetched (uri, referringTerm, userCallback, options) <-- old
864
+ * nowOrWhenFetched (uri, referringTerm, userCallback) <-- old
865
+ *
866
+ * Options include:
867
+ * referringTerm The document in which this link was found.
868
+ * this is valuable when finding the source of bad URIs
869
+ * force boolean. Never mind whether you have tried before,
870
+ * load this from scratch.
871
+ * forceContentType Override the incoming header to force the data to be
872
+ * treated as this content-type.
873
+ *
874
+ * Callback function takes:
875
+ *
876
+ * ok True if the fetch worked, and got a 200 response.
877
+ * False if any error happened
878
+ *
879
+ * errmessage Text error message if not OK.
880
+ *
881
+ * response The fetch Response object (was: XHR) if there was was one
882
+ * includes response.status as the HTTP status if any.
883
+ */
884
+ nowOrWhenFetched(uriIn, p2, userCallback) {
885
+ let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
886
+ const uri = termValue(uriIn);
887
+ if (typeof p2 === 'function') {
888
+ // nowOrWhenFetched (uri, userCallback)
889
+ userCallback = p2;
890
+ } else if (typeof p2 === 'undefined') {// original calling signature
891
+ // referringTerm = undefined
892
+ } else if (isNamedNode(p2)) {
893
+ // referringTerm = p2
894
+ options.referringTerm = p2;
895
+ } else {
896
+ // nowOrWhenFetched (uri, options, userCallback)
897
+ options = p2;
898
+ }
899
+ this.load(uri, options).then(fetchResponse => {
900
+ if (userCallback) {
901
+ if (fetchResponse) {
902
+ if (fetchResponse.ok) {
903
+ userCallback(true, 'OK', fetchResponse);
963
904
  } else {
964
- var _oops = '@@ nowOrWhenFetched: no response object!';
965
- userCallback(false, _oops);
905
+ let oops = 'HTTP error: Status ' + fetchResponse.status + ' (' + fetchResponse.statusText + ')';
906
+ if (fetchResponse.responseText) {
907
+ oops += ' ' + fetchResponse.responseText; // not in 404, dns error, nock failure
908
+ }
909
+ userCallback(false, oops, fetchResponse);
966
910
  }
967
- }
968
- }, function (err) {
969
- var message = err.message || err.statusText;
970
- message = 'Failed to load <' + uri + '> ' + message;
971
- if (err.response && err.response.status) {
972
- message += ' status: ' + err.response.status;
973
- }
974
- userCallback(false, message, err.response);
975
- });
976
- }
977
-
978
- /**
979
- * Records a status message (as a literal node) by appending it to the
980
- * request's metadata status collection.
981
- *
982
- */
983
- }, {
984
- key: "addStatus",
985
- value: function addStatus(req, statusMessage) {
986
- // <Debug about="parsePerformance">
987
- var now = new Date();
988
- statusMessage = '[' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds() + '.' + now.getMilliseconds() + '] ' + statusMessage;
989
- // </Debug>
990
- var kb = this.store;
991
- var statusNode = kb.the(req, this.ns.link('status'));
992
- if (isCollection(statusNode)) {
993
- statusNode.append(kb.rdfFactory.literal(statusMessage));
994
- } else {
995
- log.warn('web.js: No list to add to: ' + statusNode + ',' + statusMessage);
996
- }
997
- }
998
-
999
- /**
1000
- * Records errors in the system on failure:
1001
- *
1002
- * - Adds an entry to the request status collection
1003
- * - Adds an error triple with the fail message to the metadata
1004
- * - Fires the 'fail' callback
1005
- * - Rejects with an error result object, which has a response object if any
1006
- */
1007
- }, {
1008
- key: "failFetch",
1009
- value: function failFetch(options, errorMessage, statusCode, response) {
1010
- this.addStatus(options.req, errorMessage);
1011
- if (!options.noMeta) {
1012
- this.store.add(options.original, this.ns.link('error'), this.store.rdfFactory.literal(errorMessage));
1013
- }
1014
- var meth = (options.method || 'GET').toUpperCase();
1015
- var isGet = meth === 'GET' || meth === 'HEAD';
1016
- if (isGet) {
1017
- // only cache the status code on GET or HEAD
1018
- if (!options.resource.equals(options.original)) {
1019
- // console.log('@@ Recording failure ' + meth + ' original ' + options.original +option '( as ' + options.resource + ') : ' + statusCode)
1020
911
  } else {
1021
- // console.log('@@ Recording ' + meth + ' failure for ' + options.original + ': ' + statusCode)
912
+ let oops = '@@ nowOrWhenFetched: no response object!';
913
+ userCallback(false, oops);
1022
914
  }
1023
- this.requested[Uri.docpart(options.original.value)] = statusCode;
1024
- this.fireCallbacks('fail', [options.original.value, errorMessage]);
1025
915
  }
1026
- var err = new Error('Fetcher: ' + errorMessage);
1027
-
1028
- // err.ok = false // Is taken as a response, will work too @@ phase out?
1029
- err.status = statusCode;
1030
- err.statusText = errorMessage;
1031
- err.response = response;
1032
- return Promise.reject(err);
1033
- }
1034
-
1035
- // in the why part of the quad distinguish between HTML and HTTP header
1036
- // Reverse is set iif the link was rev= as opposed to rel=
1037
- }, {
1038
- key: "linkData",
1039
- value: function linkData(originalUri, rel, uri, why, reverse) {
1040
- if (!uri) return;
1041
- var kb = this.store;
1042
- var predicate;
1043
- // See http://www.w3.org/TR/powder-dr/#httplink for describedby 2008-12-10
1044
- var obj = kb.rdfFactory.namedNode(Uri.join(uri, originalUri.value));
1045
- if (rel === 'alternate' || rel === 'seeAlso' || rel === 'meta' || rel === 'describedby') {
1046
- if (obj.value === originalUri.value) {
1047
- return;
1048
- }
1049
- predicate = this.ns.rdfs('seeAlso');
1050
- } else if (rel === 'type') {
1051
- predicate = kb.rdfFactory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type');
1052
- } else {
1053
- // See https://www.iana.org/assignments/link-relations/link-relations.xml
1054
- // Alas not yet in RDF yet for each predicate
1055
- // encode space in e.g. rel="shortcut icon"
1056
- predicate = kb.rdfFactory.namedNode(Uri.join(encodeURIComponent(rel), 'http://www.iana.org/assignments/link-relations/'));
916
+ }, function (err) {
917
+ var message = err.message || err.statusText;
918
+ message = 'Failed to load <' + uri + '> ' + message;
919
+ if (err.response && err.response.status) {
920
+ message += ' status: ' + err.response.status;
1057
921
  }
1058
- if (reverse) {
1059
- kb.add(obj, predicate, originalUri, why);
922
+ userCallback(false, message, err.response);
923
+ });
924
+ }
925
+
926
+ /**
927
+ * Records a status message (as a literal node) by appending it to the
928
+ * request's metadata status collection.
929
+ *
930
+ */
931
+ addStatus(req, statusMessage) {
932
+ // <Debug about="parsePerformance">
933
+ let now = new Date();
934
+ statusMessage = '[' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds() + '.' + now.getMilliseconds() + '] ' + statusMessage;
935
+ // </Debug>
936
+ let kb = this.store;
937
+ const statusNode = kb.the(req, this.ns.link('status'));
938
+ if (isCollection(statusNode)) {
939
+ statusNode.append(kb.rdfFactory.literal(statusMessage));
940
+ } else {
941
+ log.warn('web.js: No list to add to: ' + statusNode + ',' + statusMessage);
942
+ }
943
+ }
944
+
945
+ /**
946
+ * Records errors in the system on failure:
947
+ *
948
+ * - Adds an entry to the request status collection
949
+ * - Adds an error triple with the fail message to the metadata
950
+ * - Fires the 'fail' callback
951
+ * - Rejects with an error result object, which has a response object if any
952
+ */
953
+ failFetch(options, errorMessage, statusCode, response) {
954
+ this.addStatus(options.req, errorMessage);
955
+ if (!options.noMeta) {
956
+ this.store.add(options.original, this.ns.link('error'), this.store.rdfFactory.literal(errorMessage));
957
+ }
958
+ let meth = (options.method || 'GET').toUpperCase();
959
+ let isGet = meth === 'GET' || meth === 'HEAD';
960
+ if (isGet) {
961
+ // only cache the status code on GET or HEAD
962
+ if (!options.resource.equals(options.original)) {
963
+ // console.log('@@ Recording failure ' + meth + ' original ' + options.original +option '( as ' + options.resource + ') : ' + statusCode)
1060
964
  } else {
1061
- kb.add(originalUri, predicate, obj, why);
965
+ // console.log('@@ Recording ' + meth + ' failure for ' + options.original + ': ' + statusCode)
1062
966
  }
967
+ this.requested[Uri.docpart(options.original.value)] = statusCode;
968
+ this.fireCallbacks('fail', [options.original.value, errorMessage]);
1063
969
  }
1064
- }, {
1065
- key: "parseLinkHeader",
1066
- value: function parseLinkHeader(linkHeader, originalUri, reqNode) {
1067
- if (!linkHeader) {
970
+ var err = new Error('Fetcher: ' + errorMessage);
971
+
972
+ // err.ok = false // Is taken as a response, will work too @@ phase out?
973
+ err.status = statusCode;
974
+ err.statusText = errorMessage;
975
+ err.response = response;
976
+ return Promise.reject(err);
977
+ }
978
+
979
+ // in the why part of the quad distinguish between HTML and HTTP header
980
+ // Reverse is set iif the link was rev= as opposed to rel=
981
+ linkData(originalUri, rel, uri, why, reverse) {
982
+ if (!uri) return;
983
+ let kb = this.store;
984
+ let predicate;
985
+ // See http://www.w3.org/TR/powder-dr/#httplink for describedby 2008-12-10
986
+ let obj = kb.rdfFactory.namedNode(Uri.join(uri, originalUri.value));
987
+ if (rel === 'alternate' || rel === 'seeAlso' || rel === 'meta' || rel === 'describedby') {
988
+ if (obj.value === originalUri.value) {
1068
989
  return;
1069
990
  }
1070
-
1071
- // const linkexp = /<[^>]*>\s*(\s*;\s*[^()<>@,;:"/[\]?={} \t]+=(([^()<>@,;:"/[]?={} \t]+)|("[^"]*")))*(,|$)/g
1072
- // const paramexp = /[^()<>@,;:"/[]?={} \t]+=(([^()<>@,;:"/[]?={} \t]+)|("[^"]*"))/g
1073
-
1074
- // From https://www.dcode.fr/regular-expression-simplificator:
1075
- // const linkexp = /<[^>]*>\s*(\s*;\s*[^()<>@,;:"/[\]?={} t]+=["]))*[,$]/g
1076
- // const paramexp = /[^\\<>@,;:"\/\[\]?={} \t]+=["])/g
1077
- // Original:
1078
- var linkexp = /<[^>]*>\s*(\s*;\s*[^()<>@,;:"/[\]?={} \t]+=(([^\(\)<>@,;:"\/\[\]\?={} \t]+)|("[^"]*")))*(,|$)/g;
1079
- var paramexp = /[^\(\)<>@,;:"\/\[\]\?={} \t]+=(([^\(\)<>@,;:"\/\[\]\?={} \t]+)|("[^"]*"))/g;
1080
- var matches = linkHeader.match(linkexp);
1081
- if (matches == null) return;
1082
- for (var i = 0; i < matches.length; i++) {
1083
- var split = matches[i].split('>');
1084
- var href = split[0].substring(1);
1085
- var ps = split[1];
1086
- var s = ps.match(paramexp);
1087
- if (s == null) return;
1088
- for (var j = 0; j < s.length; j++) {
1089
- var p = s[j];
1090
- var paramsplit = p.split('=');
1091
- // var name = paramsplit[0]
1092
- var rel = paramsplit[1].replace(/["']/g, ''); // '"
1093
- this.linkData(originalUri, rel, href, reqNode);
1094
- }
991
+ predicate = this.ns.rdfs('seeAlso');
992
+ } else if (rel === 'type') {
993
+ predicate = kb.rdfFactory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type');
994
+ } else {
995
+ // See https://www.iana.org/assignments/link-relations/link-relations.xml
996
+ // Alas not yet in RDF yet for each predicate
997
+ // encode space in e.g. rel="shortcut icon"
998
+ predicate = kb.rdfFactory.namedNode(Uri.join(encodeURIComponent(rel), 'http://www.iana.org/assignments/link-relations/'));
999
+ }
1000
+ if (reverse) {
1001
+ kb.add(obj, predicate, originalUri, why);
1002
+ } else {
1003
+ kb.add(originalUri, predicate, obj, why);
1004
+ }
1005
+ }
1006
+ parseLinkHeader(linkHeader, originalUri, reqNode) {
1007
+ if (!linkHeader) {
1008
+ return;
1009
+ }
1010
+
1011
+ // const linkexp = /<[^>]*>\s*(\s*;\s*[^()<>@,;:"/[\]?={} \t]+=(([^()<>@,;:"/[]?={} \t]+)|("[^"]*")))*(,|$)/g
1012
+ // const paramexp = /[^()<>@,;:"/[]?={} \t]+=(([^()<>@,;:"/[]?={} \t]+)|("[^"]*"))/g
1013
+
1014
+ // From https://www.dcode.fr/regular-expression-simplificator:
1015
+ // const linkexp = /<[^>]*>\s*(\s*;\s*[^()<>@,;:"/[\]?={} t]+=["]))*[,$]/g
1016
+ // const paramexp = /[^\\<>@,;:"\/\[\]?={} \t]+=["])/g
1017
+ // Original:
1018
+ const linkexp = /<[^>]*>\s*(\s*;\s*[^()<>@,;:"/[\]?={} \t]+=(([^\(\)<>@,;:"\/\[\]\?={} \t]+)|("[^"]*")))*(,|$)/g;
1019
+ const paramexp = /[^\(\)<>@,;:"\/\[\]\?={} \t]+=(([^\(\)<>@,;:"\/\[\]\?={} \t]+)|("[^"]*"))/g;
1020
+ const matches = linkHeader.match(linkexp);
1021
+ if (matches == null) return;
1022
+ for (let i = 0; i < matches.length; i++) {
1023
+ let split = matches[i].split('>');
1024
+ let href = split[0].substring(1);
1025
+ let ps = split[1];
1026
+ let s = ps.match(paramexp);
1027
+ if (s == null) return;
1028
+ for (let j = 0; j < s.length; j++) {
1029
+ let p = s[j];
1030
+ let paramsplit = p.split('=');
1031
+ // var name = paramsplit[0]
1032
+ let rel = paramsplit[1].replace(/["']/g, ''); // '"
1033
+ this.linkData(originalUri, rel, href, reqNode);
1095
1034
  }
1096
1035
  }
1097
- }, {
1098
- key: "doneFetch",
1099
- value: function doneFetch(options, response) {
1100
- this.addStatus(options.req, 'Done.');
1101
- this.requested[options.original.value] = 'done';
1102
- this.fireCallbacks('done', [options.original.value]);
1103
- response.req = options.req; // Set the request meta blank node
1036
+ }
1037
+ doneFetch(options, response) {
1038
+ this.addStatus(options.req, 'Done.');
1039
+ this.requested[options.original.value] = 'done';
1040
+ this.fireCallbacks('done', [options.original.value]);
1041
+ response.req = options.req; // Set the request meta blank node
1104
1042
 
1105
- return response;
1106
- }
1043
+ return response;
1044
+ }
1107
1045
 
1108
- /**
1109
- * Note two nodes are now smushed
1110
- * If only one was flagged as looked up, then the new node is looked up again,
1111
- * which will make sure all the URIs are dereferenced
1112
- */
1113
- }, {
1114
- key: "nowKnownAs",
1115
- value: function nowKnownAs(was, now) {
1116
- if (this.lookedUp[was.value]) {
1117
- // Transfer userCallback
1118
- if (!this.lookedUp[now.value]) {
1119
- this.lookUpThing(now, was);
1120
- }
1121
- } else if (this.lookedUp[now.value]) {
1122
- if (!this.lookedUp[was.value]) {
1123
- this.lookUpThing(was, now);
1124
- }
1046
+ /**
1047
+ * Note two nodes are now smushed
1048
+ * If only one was flagged as looked up, then the new node is looked up again,
1049
+ * which will make sure all the URIs are dereferenced
1050
+ */
1051
+ nowKnownAs(was, now) {
1052
+ if (this.lookedUp[was.value]) {
1053
+ // Transfer userCallback
1054
+ if (!this.lookedUp[now.value]) {
1055
+ this.lookUpThing(now, was);
1056
+ }
1057
+ } else if (this.lookedUp[now.value]) {
1058
+ if (!this.lookedUp[was.value]) {
1059
+ this.lookUpThing(was, now);
1125
1060
  }
1126
1061
  }
1062
+ }
1127
1063
 
1128
- /**
1129
- * Writes back to the web what we have in the store for this uri
1130
- */
1131
- }, {
1132
- key: "putBack",
1133
- value: function putBack(uri) {
1134
- var _this6 = this;
1135
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1136
- var uriSting = termValue(uri);
1137
- var doc = new RDFlibNamedNode(uriSting).doc(); // strip off #
1138
- options.contentType = options["content-type"] || options["Content-Type"] || options.contentType || TurtleContentType;
1139
- if (options.contentType === 'application/ld+json') {
1140
- return new Promise(function (resolve, reject) {
1141
- serialize(doc, _this6.store, doc.uri, options.contentType, function (err, jsonString) {
1142
- if (err) {
1143
- reject(err);
1144
- } else {
1145
- // @ts-ignore
1146
- options.data = jsonString;
1147
- _this6.webOperation('PUT', uri, options).then(function (res) {
1148
- return resolve(res);
1149
- }).catch(function (error) {
1150
- return reject(error);
1151
- });
1152
- }
1153
- });
1154
- });
1155
- }
1156
- options.data = serialize(doc, this.store, doc.value, options.contentType);
1157
- return this.webOperation('PUT', uriSting, options);
1158
- }
1159
- }, {
1160
- key: "webCopy",
1161
- value: function webCopy(here, there, contentType) {
1162
- var _this7 = this;
1163
- return this.webOperation('GET', here).then(function (result) {
1164
- return _this7.webOperation('PUT',
1165
- // change to binary from text
1166
- there, {
1167
- data: result.responseText,
1168
- contentType: contentType
1064
+ /**
1065
+ * Writes back to the web what we have in the store for this uri
1066
+ */
1067
+ putBack(uri) {
1068
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1069
+ const uriSting = termValue(uri);
1070
+ let doc = new RDFlibNamedNode(uriSting).doc(); // strip off #
1071
+ options.contentType = options["content-type"] || options["Content-Type"] || options.contentType || TurtleContentType;
1072
+ if (options.contentType === 'application/ld+json') {
1073
+ return new Promise((resolve, reject) => {
1074
+ serialize(doc, this.store, doc.uri, options.contentType, (err, jsonString) => {
1075
+ if (err) {
1076
+ reject(err);
1077
+ } else {
1078
+ // @ts-ignore
1079
+ options.data = jsonString;
1080
+ this.webOperation('PUT', uri, options).then(res => resolve(res)).catch(error => reject(error));
1081
+ }
1169
1082
  });
1170
1083
  });
1171
1084
  }
1172
- }, {
1173
- key: "delete",
1174
- value: function _delete(uri, options) {
1175
- var _this8 = this;
1176
- return this.webOperation('DELETE', uri, options).then(function (response) {
1177
- _this8.requested[uri] = 404;
1178
- _this8.nonexistent[uri] = true;
1179
- _this8.unload(_this8.store.rdfFactory.namedNode(uri));
1180
- return response;
1085
+ options.data = serialize(doc, this.store, doc.value, options.contentType);
1086
+ return this.webOperation('PUT', uriSting, options);
1087
+ }
1088
+ webCopy(here, there, contentType) {
1089
+ return this.webOperation('GET', here).then(result => {
1090
+ return this.webOperation('PUT',
1091
+ // change to binary from text
1092
+ there, {
1093
+ data: result.responseText,
1094
+ contentType
1181
1095
  });
1182
- }
1183
-
1184
- /** Create an empty resource if it really does not exist
1185
- * Be absolutely sure something does not exist before creating a new empty file
1186
- * as otherwise existing could be deleted.
1187
- * @param doc - The resource
1188
- */
1189
- }, {
1190
- key: "createIfNotExists",
1191
- value: (function () {
1192
- var _createIfNotExists = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(doc) {
1193
- var contentType,
1194
- data,
1195
- fetcher,
1196
- response,
1197
- _args2 = arguments;
1198
- return _regeneratorRuntime.wrap(function _callee2$(_context2) {
1199
- while (1) switch (_context2.prev = _context2.next) {
1200
- case 0:
1201
- contentType = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : TurtleContentType;
1202
- data = _args2.length > 2 && _args2[2] !== undefined ? _args2[2] : '';
1203
- fetcher = this;
1204
- _context2.prev = 3;
1205
- _context2.next = 6;
1206
- return fetcher.load(doc);
1207
- case 6:
1208
- response = _context2.sent;
1209
- _context2.next = 26;
1210
- break;
1211
- case 9:
1212
- _context2.prev = 9;
1213
- _context2.t0 = _context2["catch"](3);
1214
- if (!(_context2.t0.response.status === 404)) {
1215
- _context2.next = 25;
1216
- break;
1217
- }
1218
- _context2.prev = 12;
1219
- _context2.next = 15;
1220
- return fetcher.webOperation('PUT', doc.value, {
1221
- data: data,
1222
- contentType: contentType
1223
- });
1224
- case 15:
1225
- response = _context2.sent;
1226
- _context2.next = 21;
1227
- break;
1228
- case 18:
1229
- _context2.prev = 18;
1230
- _context2.t1 = _context2["catch"](12);
1231
- throw _context2.t1;
1232
- case 21:
1233
- delete fetcher.requested[doc.value]; // delete cached 404 error
1234
- // console.log('createIfNotExists doc created ok ' + doc)
1235
- return _context2.abrupt("return", response);
1236
- case 25:
1237
- throw _context2.t0;
1238
- case 26:
1239
- return _context2.abrupt("return", response);
1240
- case 27:
1241
- case "end":
1242
- return _context2.stop();
1243
- }
1244
- }, _callee2, this, [[3, 9], [12, 18]]);
1245
- }));
1246
- function createIfNotExists(_x4) {
1247
- return _createIfNotExists.apply(this, arguments);
1248
- }
1249
- return createIfNotExists;
1250
- }()
1251
- /**
1252
- * @param parentURI URI of parent container
1253
- * @param folderName - Optional folder name (slug)
1254
- * @param data - Optional folder metadata
1255
- */
1256
- )
1257
- }, {
1258
- key: "createContainer",
1259
- value: function createContainer(parentURI, folderName, data) {
1260
- var headers = {
1261
- // Force the right mime type for containers
1262
- 'content-type': TurtleContentType,
1263
- 'link': this.ns.ldp('BasicContainer') + '; rel="type"'
1264
- };
1265
- if (folderName) {
1266
- headers['slug'] = folderName;
1267
- }
1096
+ });
1097
+ }
1098
+ delete(uri, options) {
1099
+ return this.webOperation('DELETE', uri, options).then(response => {
1100
+ this.requested[uri] = 404;
1101
+ this.nonexistent[uri] = true;
1102
+ this.unload(this.store.rdfFactory.namedNode(uri));
1103
+ return response;
1104
+ });
1105
+ }
1268
1106
 
1269
- // @ts-ignore These headers lack some of the required operators.
1270
- var options = {
1271
- headers: headers
1272
- };
1273
- if (data) {
1274
- options.body = data;
1275
- }
1276
- return this.webOperation('POST', parentURI, options);
1277
- }
1278
- }, {
1279
- key: "invalidateCache",
1280
- value: function invalidateCache(iri) {
1281
- var uri = termValue(iri);
1282
- var fetcher = this;
1107
+ /** Create an empty resource if it really does not exist
1108
+ * Be absolutely sure something does not exist before creating a new empty file
1109
+ * as otherwise existing could be deleted.
1110
+ * @param doc - The resource
1111
+ */
1112
+ async createIfNotExists(doc) {
1113
+ let contentType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : TurtleContentType;
1114
+ let data = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
1115
+ const fetcher = this;
1116
+ try {
1117
+ var response = await fetcher.load(doc);
1118
+ } catch (err) {
1283
1119
  // @ts-ignore
1284
- if (fetcher.fetchQueue && fetcher.fetchQueue[uri]) {
1285
- // console.log('Internal error - fetchQueue exists ' + uri)
1286
- var promise = fetcher.fetchQueue[uri];
1287
- if (promise['PromiseStatus'] === 'resolved') {
1288
- delete fetcher.fetchQueue[uri];
1289
- } else {
1290
- // pending
1291
- delete fetcher.fetchQueue[uri];
1292
- // console.log('*** Fetcher: pending fetchQueue deleted ' + uri)
1120
+ if (err.response.status === 404) {
1121
+ // console.log('createIfNotExists: doc does NOT exist, will create... ' + doc)
1122
+ try {
1123
+ response = await fetcher.webOperation('PUT', doc.value, {
1124
+ data,
1125
+ contentType
1126
+ });
1127
+ } catch (err) {
1128
+ // console.log('createIfNotExists doc FAILED: ' + doc + ': ' + err)
1129
+ throw err;
1293
1130
  }
1294
- }
1295
- if (fetcher.requested[uri] && fetcher.requested[uri] !== 'done' && fetcher.requested[uri] !== 'failed' && fetcher.requested[uri] !== 404) {
1296
- var msg = "Rdflib: fetcher: Destructive operation on <".concat(fetcher.requested[uri], "> file being fetched! ") + uri;
1297
- console.error(msg);
1298
- // alert(msg)
1131
+ delete fetcher.requested[doc.value]; // delete cached 404 error
1132
+ // console.log('createIfNotExists doc created ok ' + doc)
1133
+ return response;
1299
1134
  } else {
1300
- delete fetcher.requested[uri]; // invalidate read cache -- @@ messes up logic if request in progress ??
1301
- delete fetcher.nonexistent[uri];
1135
+ // console.log('createIfNotExists doc load error NOT 404: ' + doc + ': ' + err)
1136
+ throw err;
1302
1137
  }
1303
1138
  }
1139
+ // console.log('createIfNotExists: doc exists, all good: ' + doc)
1140
+ return response;
1141
+ }
1142
+
1143
+ /**
1144
+ * @param parentURI URI of parent container
1145
+ * @param folderName - Optional folder name (slug)
1146
+ * @param data - Optional folder metadata
1147
+ */
1148
+ createContainer(parentURI, folderName, data) {
1149
+ let headers = {
1150
+ // Force the right mime type for containers
1151
+ 'content-type': TurtleContentType,
1152
+ 'link': this.ns.ldp('BasicContainer') + '; rel="type"'
1153
+ };
1154
+ if (folderName) {
1155
+ headers['slug'] = folderName;
1156
+ }
1304
1157
 
1305
- /**
1306
- * A generic web operation, at the fetch() level.
1307
- * does not involve the quad store.
1308
- *
1309
- * Returns promise of Response
1310
- * If data is returned, copies it to response.responseText before returning
1311
- */
1312
- }, {
1313
- key: "webOperation",
1314
- value: function webOperation(method, uriIn) {
1315
- var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
1316
- var uri = termValue(uriIn);
1317
- options.method = method;
1318
- options.body = options.data || options.body;
1319
- options.force = true;
1320
- var fetcher = this;
1321
- if (options.body && !options.contentType) {
1322
- throw new Error('Web operation sending data must have a defined contentType.');
1323
- }
1324
- if (options.contentType) {
1325
- options.headers = options.headers || {};
1326
- options.headers['content-type'] = options.contentType;
1158
+ // @ts-ignore These headers lack some of the required operators.
1159
+ let options = {
1160
+ headers
1161
+ };
1162
+ if (data) {
1163
+ options.body = data;
1164
+ }
1165
+ return this.webOperation('POST', parentURI, options);
1166
+ }
1167
+ invalidateCache(iri) {
1168
+ const uri = termValue(iri);
1169
+ const fetcher = this;
1170
+ // @ts-ignore
1171
+ if (fetcher.fetchQueue && fetcher.fetchQueue[uri]) {
1172
+ // console.log('Internal error - fetchQueue exists ' + uri)
1173
+ var promise = fetcher.fetchQueue[uri];
1174
+ if (promise['PromiseStatus'] === 'resolved') {
1175
+ delete fetcher.fetchQueue[uri];
1176
+ } else {
1177
+ // pending
1178
+ delete fetcher.fetchQueue[uri];
1179
+ // console.log('*** Fetcher: pending fetchQueue deleted ' + uri)
1327
1180
  }
1328
- Fetcher.setCredentials(uri, options);
1329
- return new Promise(function (resolve, reject) {
1330
- fetcher._fetch(uri, options).then(function (response) {
1331
- if (response.ok) {
1332
- if (method === 'PUT' || method === 'PATCH' || method === 'POST' || method === 'DELETE') {
1333
- fetcher.invalidateCache(uri);
1334
- } // response.body with Chrome can't be relied on
1335
- if (response.text) {
1336
- // Was: response.body https://github.com/linkeddata/rdflib.js/issues/506
1337
- response.text().then(function (data) {
1338
- response.responseText = data;
1339
- resolve(response);
1340
- });
1341
- } else {
1181
+ }
1182
+ if (fetcher.requested[uri] && fetcher.requested[uri] !== 'done' && fetcher.requested[uri] !== 'failed' && fetcher.requested[uri] !== 404) {
1183
+ let msg = `Rdflib: fetcher: Destructive operation on <${fetcher.requested[uri]}> file being fetched! ` + uri;
1184
+ console.error(msg);
1185
+ // alert(msg)
1186
+ } else {
1187
+ delete fetcher.requested[uri]; // invalidate read cache -- @@ messes up logic if request in progress ??
1188
+ delete fetcher.nonexistent[uri];
1189
+ }
1190
+ }
1191
+
1192
+ /**
1193
+ * A generic web operation, at the fetch() level.
1194
+ * does not involve the quad store.
1195
+ *
1196
+ * Returns promise of Response
1197
+ * If data is returned, copies it to response.responseText before returning
1198
+ */
1199
+ webOperation(method, uriIn) {
1200
+ let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
1201
+ const uri = termValue(uriIn);
1202
+ options.method = method;
1203
+ options.body = options.data || options.body;
1204
+ options.force = true;
1205
+ const fetcher = this;
1206
+ if (options.body && !options.contentType) {
1207
+ throw new Error('Web operation sending data must have a defined contentType.');
1208
+ }
1209
+ if (options.contentType) {
1210
+ options.headers = options.headers || {};
1211
+ options.headers['content-type'] = options.contentType;
1212
+ }
1213
+ Fetcher.setCredentials(uri, options);
1214
+ return new Promise(function (resolve, reject) {
1215
+ fetcher._fetch(uri, options).then(response => {
1216
+ if (response.ok) {
1217
+ if (method === 'PUT' || method === 'PATCH' || method === 'POST' || method === 'DELETE') {
1218
+ fetcher.invalidateCache(uri);
1219
+ } // response.body with Chrome can't be relied on
1220
+ if (response.text) {
1221
+ // Was: response.body https://github.com/linkeddata/rdflib.js/issues/506
1222
+ response.text().then(data => {
1223
+ response.responseText = data;
1342
1224
  resolve(response);
1343
- }
1225
+ });
1344
1226
  } else {
1345
- var msg = 'Web error: ' + response.status;
1346
- if (response.statusText) msg += ' (' + response.statusText + ')';
1347
- msg += ' on ' + method + ' of <' + uri + '>';
1348
- if (response.responseText) msg += ': ' + response.responseText;
1349
- var e2 = new Error(msg);
1350
- e2.response = response;
1351
- reject(e2);
1227
+ resolve(response);
1352
1228
  }
1353
- }, function (err) {
1354
- var msg = 'Fetch error for ' + method + ' of <' + uri + '>:' + err;
1355
- reject(new Error(msg));
1356
- });
1229
+ } else {
1230
+ let msg = 'Web error: ' + response.status;
1231
+ if (response.statusText) msg += ' (' + response.statusText + ')';
1232
+ msg += ' on ' + method + ' of <' + uri + '>';
1233
+ if (response.responseText) msg += ': ' + response.responseText;
1234
+ let e2 = new Error(msg);
1235
+ e2.response = response;
1236
+ reject(e2);
1237
+ }
1238
+ }, err => {
1239
+ let msg = 'Fetch error for ' + method + ' of <' + uri + '>:' + err;
1240
+ reject(new Error(msg));
1357
1241
  });
1358
- }
1242
+ });
1243
+ }
1359
1244
 
1360
- /**
1361
- * Looks up something.
1362
- * Looks up all the URIs a things has.
1363
- *
1364
- * @param term - canonical term for the thing whose URI is
1365
- * to be dereferenced
1366
- * @param rterm - the resource which referred to this
1367
- * (for tracking bad links)
1368
- */
1369
- }, {
1370
- key: "lookUpThing",
1371
- value: function lookUpThing(term, rterm) {
1372
- var _this9 = this;
1373
- var uris = this.store.uris(term); // Get all URIs
1374
- uris = uris.map(function (u) {
1375
- return Uri.docpart(u);
1376
- }); // Drop hash fragments
1377
-
1378
- uris.forEach(function (u) {
1379
- _this9.lookedUp[u] = true;
1380
- });
1245
+ /**
1246
+ * Looks up something.
1247
+ * Looks up all the URIs a things has.
1248
+ *
1249
+ * @param term - canonical term for the thing whose URI is
1250
+ * to be dereferenced
1251
+ * @param rterm - the resource which referred to this
1252
+ * (for tracking bad links)
1253
+ */
1254
+ lookUpThing(term, rterm) {
1255
+ let uris = this.store.uris(term); // Get all URIs
1256
+ uris = uris.map(u => Uri.docpart(u)); // Drop hash fragments
1257
+
1258
+ uris.forEach(u => {
1259
+ this.lookedUp[u] = true;
1260
+ });
1381
1261
 
1382
- // @ts-ignore Recursive type
1383
- return this.load(uris, {
1384
- referringTerm: rterm
1385
- });
1386
- }
1262
+ // @ts-ignore Recursive type
1263
+ return this.load(uris, {
1264
+ referringTerm: rterm
1265
+ });
1266
+ }
1387
1267
 
1388
- /**
1389
- * Looks up response header.
1390
- *
1391
- * @returns {Array|undefined} a list of header values found in a stored HTTP
1392
- * response, or [] if response was found but no header found,
1393
- * or undefined if no response is available.
1394
- * Looks for { [] link:requestedURI ?uri; link:response [ httph:header-name ?value ] }
1395
- */
1396
- }, {
1397
- key: "getHeader",
1398
- value: function getHeader(doc, header) {
1399
- var kb = this.store; // look for the URI (AS A STRING NOT A NODE) for a stored request
1400
- var docuri = doc.value;
1401
- var requests = kb.each(undefined, this.ns.link('requestedURI'), kb.rdfFactory.literal(docuri));
1402
- for (var r = 0; r < requests.length; r++) {
1403
- var request = requests[r];
1404
- if (request !== undefined) {
1405
- var _response2 = kb.any(request, this.ns.link('response'));
1406
- if (_response2 !== undefined && kb.anyValue(_response2, this.ns.http('status')) && kb.anyValue(_response2, this.ns.http('status')).startsWith('2')) {
1407
- // Only look at success returns - not 401 error messagess etc
1408
- var results = kb.each(_response2, this.ns.httph(header.toLowerCase()));
1409
- if (results.length) {
1410
- return results.map(function (v) {
1411
- return v.value;
1412
- });
1413
- }
1414
- return [];
1268
+ /**
1269
+ * Looks up response header.
1270
+ *
1271
+ * @returns {Array|undefined} a list of header values found in a stored HTTP
1272
+ * response, or [] if response was found but no header found,
1273
+ * or undefined if no response is available.
1274
+ * Looks for { [] link:requestedURI ?uri; link:response [ httph:header-name ?value ] }
1275
+ */
1276
+ getHeader(doc, header) {
1277
+ const kb = this.store; // look for the URI (AS A STRING NOT A NODE) for a stored request
1278
+ const docuri = doc.value;
1279
+ const requests = kb.each(undefined, this.ns.link('requestedURI'), kb.rdfFactory.literal(docuri));
1280
+ for (let r = 0; r < requests.length; r++) {
1281
+ let request = requests[r];
1282
+ if (request !== undefined) {
1283
+ let response = kb.any(request, this.ns.link('response'));
1284
+ if (response !== undefined && kb.anyValue(response, this.ns.http('status')) && kb.anyValue(response, this.ns.http('status')).startsWith('2')) {
1285
+ // Only look at success returns - not 401 error messagess etc
1286
+ let results = kb.each(response, this.ns.httph(header.toLowerCase()));
1287
+ if (results.length) {
1288
+ return results.map(v => {
1289
+ return v.value;
1290
+ });
1415
1291
  }
1292
+ return [];
1416
1293
  }
1417
1294
  }
1418
- return undefined;
1419
1295
  }
1420
- }, {
1421
- key: "saveRequestMetadata",
1422
- value: function saveRequestMetadata(docuri, options) {
1423
- var req = options.req;
1424
- var kb = this.store;
1425
- var rterm = options.referringTerm;
1426
- this.addStatus(options.req, 'Accept: ' + options.headers['accept']);
1427
- if (isNamedNode(rterm)) {
1428
- kb.add(kb.rdfFactory.namedNode(docuri), this.ns.link('requestedBy'), rterm, this.appNode);
1429
- }
1430
- if (options.original && options.original.value !== docuri) {
1431
- kb.add(req, this.ns.link('orginalURI'), kb.rdfFactory.literal(options.original.value), this.appNode);
1296
+ return undefined;
1297
+ }
1298
+ saveRequestMetadata(docuri, options) {
1299
+ let req = options.req;
1300
+ let kb = this.store;
1301
+ let rterm = options.referringTerm;
1302
+ this.addStatus(options.req, 'Accept: ' + options.headers['accept']);
1303
+ if (isNamedNode(rterm)) {
1304
+ kb.add(kb.rdfFactory.namedNode(docuri), this.ns.link('requestedBy'), rterm, this.appNode);
1305
+ }
1306
+ if (options.original && options.original.value !== docuri) {
1307
+ kb.add(req, this.ns.link('orginalURI'), kb.rdfFactory.literal(options.original.value), this.appNode);
1308
+ }
1309
+ const now = new Date();
1310
+ const timeNow = '[' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds() + '] ';
1311
+ kb.add(req, this.ns.rdfs('label'), kb.rdfFactory.literal(timeNow + ' Request for ' + docuri), this.appNode);
1312
+ // We store the docuri as a string, not as a node,
1313
+ // see https://github.com/linkeddata/rdflib.js/pull/427#pullrequestreview-447910061
1314
+ kb.add(req, this.ns.link('requestedURI'), kb.rdfFactory.literal(docuri), this.appNode);
1315
+ kb.add(req, this.ns.link('status'), kb.collection(), this.appNode);
1316
+ }
1317
+ saveResponseMetadata(response, options) {
1318
+ const kb = this.store;
1319
+ let responseNode = kb.bnode();
1320
+ kb.add(options.req, this.ns.link('response'), responseNode, this.appNode);
1321
+ kb.add(responseNode, this.ns.http('status'), kb.rdfFactory.literal(response.status), this.appNode);
1322
+ kb.add(responseNode, this.ns.http('statusText'), kb.rdfFactory.literal(response.statusText), this.appNode);
1323
+
1324
+ // Save the response headers
1325
+ response.headers.forEach((value, header) => {
1326
+ kb.add(responseNode, this.ns.httph(header), this.store.rdfFactory.literal(value), this.appNode);
1327
+ if (header === 'content-type') {
1328
+ kb.add(options.resource, this.ns.rdf('type'), kb.rdfFactory.namedNode(Util.mediaTypeClass(value).value), this.appNode // responseNode
1329
+ );
1432
1330
  }
1433
- var now = new Date();
1434
- var timeNow = '[' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds() + '] ';
1435
- kb.add(req, this.ns.rdfs('label'), kb.rdfFactory.literal(timeNow + ' Request for ' + docuri), this.appNode);
1436
- // We store the docuri as a string, not as a node,
1437
- // see https://github.com/linkeddata/rdflib.js/pull/427#pullrequestreview-447910061
1438
- kb.add(req, this.ns.link('requestedURI'), kb.rdfFactory.literal(docuri), this.appNode);
1439
- kb.add(req, this.ns.link('status'), kb.collection(), this.appNode);
1440
- }
1441
- }, {
1442
- key: "saveResponseMetadata",
1443
- value: function saveResponseMetadata(response, options) {
1444
- var _this10 = this;
1445
- var kb = this.store;
1446
- var responseNode = kb.bnode();
1447
- kb.add(options.req, this.ns.link('response'), responseNode, this.appNode);
1448
- kb.add(responseNode, this.ns.http('status'), kb.rdfFactory.literal(response.status), this.appNode);
1449
- kb.add(responseNode, this.ns.http('statusText'), kb.rdfFactory.literal(response.statusText), this.appNode);
1450
-
1451
- // Save the response headers
1452
- response.headers.forEach(function (value, header) {
1453
- kb.add(responseNode, _this10.ns.httph(header), _this10.store.rdfFactory.literal(value), _this10.appNode);
1454
- if (header === 'content-type') {
1455
- kb.add(options.resource, _this10.ns.rdf('type'), kb.rdfFactory.namedNode(Util.mediaTypeClass(value).value), _this10.appNode // responseNode
1456
- );
1457
- }
1458
- });
1459
- return responseNode;
1460
- }
1461
- }, {
1462
- key: "objectRefresh",
1463
- value: function objectRefresh(term) {
1464
- var uris = this.store.uris(term); // Get all URIs
1465
- if (typeof uris !== 'undefined') {
1466
- for (var i = 0; i < uris.length; i++) {
1467
- this.refresh(this.store.rdfFactory.namedNode(Uri.docpart(uris[i])));
1468
- // what about rterm?
1469
- }
1331
+ });
1332
+ return responseNode;
1333
+ }
1334
+ objectRefresh(term) {
1335
+ let uris = this.store.uris(term); // Get all URIs
1336
+ if (typeof uris !== 'undefined') {
1337
+ for (let i = 0; i < uris.length; i++) {
1338
+ this.refresh(this.store.rdfFactory.namedNode(Uri.docpart(uris[i])));
1339
+ // what about rterm?
1470
1340
  }
1471
1341
  }
1342
+ }
1472
1343
 
1473
- /* refresh Reload data from a given document
1474
- **
1475
- ** @param term - An RDF Named Node for the eodcument in question
1476
- ** @param userCallback - A function userCallback(ok, message, response)
1477
- */
1478
- }, {
1479
- key: "refresh",
1480
- value: function refresh(term, userCallback) {
1481
- // sources_refresh
1482
- this.fireCallbacks('refresh', arguments);
1483
- this.nowOrWhenFetched(term, {
1484
- force: true,
1485
- clearPreviousData: true
1486
- }, userCallback);
1487
- }
1488
-
1489
- /* refreshIfExpired Conditional refresh if Expired
1490
- **
1491
- ** @param term - An RDF Named Node for the eodcument in question
1492
- ** @param userCallback - A function userCallback(ok, message, response)
1493
- */
1494
- }, {
1495
- key: "refreshIfExpired",
1496
- value: function refreshIfExpired(term, userCallback) {
1497
- var exp = this.getHeader(term, 'Expires');
1498
- if (!exp || new Date(exp[0]).getTime() <= new Date().getTime()) {
1499
- this.refresh(term, userCallback);
1500
- } else {
1501
- userCallback(true, 'Not expired', {});
1502
- }
1344
+ /* refresh Reload data from a given document
1345
+ **
1346
+ ** @param term - An RDF Named Node for the eodcument in question
1347
+ ** @param userCallback - A function userCallback(ok, message, response)
1348
+ */
1349
+ refresh(term, userCallback) {
1350
+ // sources_refresh
1351
+ this.fireCallbacks('refresh', arguments);
1352
+ this.nowOrWhenFetched(term, {
1353
+ force: true,
1354
+ clearPreviousData: true
1355
+ }, userCallback);
1356
+ }
1357
+
1358
+ /* refreshIfExpired Conditional refresh if Expired
1359
+ **
1360
+ ** @param term - An RDF Named Node for the eodcument in question
1361
+ ** @param userCallback - A function userCallback(ok, message, response)
1362
+ */
1363
+ refreshIfExpired(term, userCallback) {
1364
+ let exp = this.getHeader(term, 'Expires');
1365
+ if (!exp || new Date(exp[0]).getTime() <= new Date().getTime()) {
1366
+ this.refresh(term, userCallback);
1367
+ } else {
1368
+ userCallback(true, 'Not expired', {});
1503
1369
  }
1504
- }, {
1505
- key: "retract",
1506
- value: function retract(term) {
1507
- // sources_retract
1508
- this.store.removeMany(undefined, undefined, undefined, term);
1509
- if (term.value) {
1510
- delete this.requested[Uri.docpart(term.value)];
1511
- }
1512
- this.fireCallbacks('retract', arguments);
1513
- }
1514
- }, {
1515
- key: "getState",
1516
- value: function getState(docuri) {
1517
- if (typeof this.requested[docuri] === 'undefined') {
1518
- return 'unrequested';
1519
- } else if (this.requested[docuri] === true) {
1520
- return 'requested';
1521
- } else if (this.requested[docuri] === 'done') {
1522
- return 'fetched';
1523
- } else if (this.requested[docuri] === 'redirected') {
1524
- return this.getState(this.redirectedTo[docuri]);
1525
- } else {
1526
- // An non-200 HTTP error status
1527
- return 'failed';
1528
- }
1370
+ }
1371
+ retract(term) {
1372
+ // sources_retract
1373
+ this.store.removeMany(undefined, undefined, undefined, term);
1374
+ if (term.value) {
1375
+ delete this.requested[Uri.docpart(term.value)];
1529
1376
  }
1530
- }, {
1531
- key: "isPending",
1532
- value: function isPending(docuri) {
1533
- // sources_pending
1534
- // doing anyStatementMatching is wasting time
1535
- // if it's not pending: false -> flailed
1536
- // 'done' -> done 'redirected' -> redirected
1537
- return this.requested[docuri] === true;
1538
- }
1539
- }, {
1540
- key: "unload",
1541
- value: function unload(term) {
1542
- this.store.removeDocument(term);
1543
- delete this.requested[term.value]; // So it can be load2ed again
1544
- }
1545
- }, {
1546
- key: "addHandler",
1547
- value: function addHandler(handler) {
1548
- this.handlers.push(handler);
1549
- handler.register(this);
1550
- }
1551
- }, {
1552
- key: "retryNoCredentials",
1553
- value: function retryNoCredentials(docuri, options) {
1554
- // console.log('Fetcher: CORS: RETRYING with NO CREDENTIALS for ' + options.resource)
1555
-
1556
- options.retriedWithNoCredentials = true; // protect against being called twice
1557
-
1558
- delete this.requested[docuri]; // forget the original request happened
1559
- delete this.fetchQueue[docuri];
1560
- // Note: XHR property was withCredentials, but fetch property is just credentials
1561
- var newOptions = Object.assign({}, options, {
1562
- credentials: 'omit'
1563
- });
1564
- this.addStatus(options.req, 'Abort: Will retry with credentials SUPPRESSED to see if that helps');
1565
- return this.load(docuri, newOptions);
1377
+ this.fireCallbacks('retract', arguments);
1378
+ }
1379
+ getState(docuri) {
1380
+ if (typeof this.requested[docuri] === 'undefined') {
1381
+ return 'unrequested';
1382
+ } else if (this.requested[docuri] === true) {
1383
+ return 'requested';
1384
+ } else if (this.requested[docuri] === 'done') {
1385
+ return 'fetched';
1386
+ } else if (this.requested[docuri] === 'redirected') {
1387
+ return this.getState(this.redirectedTo[docuri]);
1388
+ } else {
1389
+ // An non-200 HTTP error status
1390
+ return 'failed';
1566
1391
  }
1392
+ }
1393
+ isPending(docuri) {
1394
+ // sources_pending
1395
+ // doing anyStatementMatching is wasting time
1396
+ // if it's not pending: false -> flailed
1397
+ // 'done' -> done 'redirected' -> redirected
1398
+ return this.requested[docuri] === true;
1399
+ }
1400
+ unload(term) {
1401
+ this.store.removeDocument(term);
1402
+ delete this.requested[term.value]; // So it can be load2ed again
1403
+ }
1404
+ addHandler(handler) {
1405
+ this.handlers.push(handler);
1406
+ handler.register(this);
1407
+ }
1408
+ retryNoCredentials(docuri, options) {
1409
+ // console.log('Fetcher: CORS: RETRYING with NO CREDENTIALS for ' + options.resource)
1567
1410
 
1568
- /**
1569
- * Tests whether a request is being made to a cross-site URI (for purposes
1570
- * of retrying with a proxy)
1571
- */
1572
- }, {
1573
- key: "isCrossSite",
1574
- value: function isCrossSite(uri) {
1575
- // Mashup situation, not node etc
1576
- if (typeof document === 'undefined' || !document.location) {
1577
- return false;
1578
- }
1579
- var hostpart = Uri.hostpart;
1580
- var here = '' + document.location;
1581
- return (hostpart(here) && hostpart(uri) && hostpart(here)) !== hostpart(uri);
1582
- }
1411
+ options.retriedWithNoCredentials = true; // protect against being called twice
1583
1412
 
1584
- /**
1585
- * Called when there's a network error in fetch(), or a response
1586
- * with status of 0.
1587
- */
1588
- }, {
1589
- key: "handleError",
1590
- value: function handleError(response, docuri, options) {
1591
- if (this.isCrossSite(docuri)) {
1592
- // Make sure we haven't retried already
1593
- if (options.credentials && options.credentials === 'include' && !options.retriedWithNoCredentials) {
1594
- return this.retryNoCredentials(docuri, options);
1595
- }
1413
+ delete this.requested[docuri]; // forget the original request happened
1414
+ delete this.fetchQueue[docuri];
1415
+ // Note: XHR property was withCredentials, but fetch property is just credentials
1416
+ let newOptions = Object.assign({}, options, {
1417
+ credentials: 'omit'
1418
+ });
1419
+ this.addStatus(options.req, 'Abort: Will retry with credentials SUPPRESSED to see if that helps');
1420
+ return this.load(docuri, newOptions);
1421
+ }
1596
1422
 
1597
- // Now attempt retry via proxy
1598
- var proxyUri = Fetcher.crossSiteProxy(docuri);
1599
- if (proxyUri && !options.proxyUsed) {
1600
- // console.log('web: Direct failed so trying proxy ' + proxyUri)
1601
- return this.redirectToProxy(proxyUri, options);
1602
- }
1603
- }
1604
- var message;
1605
- if (response instanceof Error) {
1606
- message = 'Fetch error: ' + response.message;
1607
- } else {
1608
- message = response.statusText;
1609
- if (response.responseText) {
1610
- message += " ".concat(response.responseText);
1611
- }
1612
- }
1423
+ /**
1424
+ * Tests whether a request is being made to a cross-site URI (for purposes
1425
+ * of retrying with a proxy)
1426
+ */
1427
+ isCrossSite(uri) {
1428
+ // Mashup situation, not node etc
1429
+ if (typeof document === 'undefined' || !document.location) {
1430
+ return false;
1431
+ }
1432
+ const hostpart = Uri.hostpart;
1433
+ const here = '' + document.location;
1434
+ return (hostpart(here) && hostpart(uri) && hostpart(here)) !== hostpart(uri);
1435
+ }
1613
1436
 
1614
- // This is either not a CORS error, or retries have been made
1615
- return this.failFetch(options, message, response.status || 998, response);
1616
- }
1437
+ /**
1438
+ * Called when there's a network error in fetch(), or a response
1439
+ * with status of 0.
1440
+ */
1441
+ handleError(response, docuri, options) {
1442
+ if (this.isCrossSite(docuri)) {
1443
+ // Make sure we haven't retried already
1444
+ if (options.credentials && options.credentials === 'include' && !options.retriedWithNoCredentials) {
1445
+ return this.retryNoCredentials(docuri, options);
1446
+ }
1617
1447
 
1618
- // deduce some things from the HTTP transaction
1619
- }, {
1620
- key: "addType",
1621
- value: function addType(rdfType, req, kb, locURI) {
1622
- // add type to all redirected resources too
1623
- var prev = req;
1624
- if (locURI) {
1625
- var reqURI = kb.any(prev, this.ns.link('requestedURI'));
1626
- if (reqURI && reqURI.value !== locURI) {
1627
- kb.add(kb.rdfFactory.namedNode(locURI), this.ns.rdf('type'), rdfType, this.appNode);
1628
- }
1448
+ // Now attempt retry via proxy
1449
+ let proxyUri = Fetcher.crossSiteProxy(docuri);
1450
+ if (proxyUri && !options.proxyUsed) {
1451
+ // console.log('web: Direct failed so trying proxy ' + proxyUri)
1452
+ return this.redirectToProxy(proxyUri, options);
1629
1453
  }
1630
- for (;;) {
1631
- var doc = kb.any(prev, this.ns.link('requestedURI'));
1632
- if (doc && doc.value) {
1633
- kb.add(kb.rdfFactory.namedNode(doc.value), this.ns.rdf('type'), rdfType, this.appNode);
1634
- } // convert Literal
1635
- prev = kb.any(undefined, kb.rdfFactory.namedNode('http://www.w3.org/2007/ont/link#redirectedRequest'), prev);
1636
- if (!prev) {
1637
- break;
1638
- }
1639
- var response = kb.any(prev, kb.rdfFactory.namedNode('http://www.w3.org/2007/ont/link#response'));
1640
- if (!response) {
1641
- break;
1642
- }
1643
- var redirection = kb.any(response, kb.rdfFactory.namedNode('http://www.w3.org/2007/ont/http#status'));
1644
- if (!redirection) {
1645
- break;
1646
- }
1647
- // @ts-ignore always true?
1648
- if (redirection !== '301' && redirection !== '302') {
1649
- break;
1650
- }
1454
+ }
1455
+ var message;
1456
+ if (response instanceof Error) {
1457
+ message = 'Fetch error: ' + response.message;
1458
+ } else {
1459
+ message = response.statusText;
1460
+ if (response.responseText) {
1461
+ message += ` ${response.responseText}`;
1651
1462
  }
1652
1463
  }
1653
1464
 
1654
- /**
1655
- * Handle fetch() response
1656
- */
1657
- }, {
1658
- key: "handleResponse",
1659
- value: function handleResponse(response, docuri, options) {
1660
- var _this11 = this;
1661
- var kb = this.store;
1662
- var headers = response.headers;
1663
- var reqNode = options.req;
1664
- var responseNode = this.saveResponseMetadata(response, options);
1665
- var contentType = this.normalizedContentType(options, headers) || '';
1666
- var contentLocation = headers.get('content-location');
1667
-
1668
- // this.fireCallbacks('recv', xhr.args)
1669
- // this.fireCallbacks('headers', [{uri: docuri, headers: xhr.headers}])
1670
-
1671
- // Check for masked errors (CORS, etc)
1672
- if (response.status === 0) {
1673
- // console.log('Masked error - status 0 for ' + docuri)
1674
- return this.handleError(response, docuri, options);
1465
+ // This is either not a CORS error, or retries have been made
1466
+ return this.failFetch(options, message, response.status || 998, response);
1467
+ }
1468
+
1469
+ // deduce some things from the HTTP transaction
1470
+ addType(rdfType, req, kb, locURI) {
1471
+ // add type to all redirected resources too
1472
+ let prev = req;
1473
+ if (locURI) {
1474
+ var reqURI = kb.any(prev, this.ns.link('requestedURI'));
1475
+ if (reqURI && reqURI.value !== locURI) {
1476
+ kb.add(kb.rdfFactory.namedNode(locURI), this.ns.rdf('type'), rdfType, this.appNode);
1675
1477
  }
1676
- if (response.status >= 400) {
1677
- if (response.status === 404) {
1678
- this.nonexistent[options.original.value] = true;
1679
- this.nonexistent[docuri] = true;
1680
- }
1681
- return this.saveErrorResponse(response, responseNode).then(function () {
1682
- var errorMessage = options.resource + ' ' + response.statusText;
1683
- return _this11.failFetch(options, errorMessage, response.status, response);
1684
- });
1478
+ }
1479
+ for (;;) {
1480
+ const doc = kb.any(prev, this.ns.link('requestedURI'));
1481
+ if (doc && doc.value) {
1482
+ kb.add(kb.rdfFactory.namedNode(doc.value), this.ns.rdf('type'), rdfType, this.appNode);
1483
+ } // convert Literal
1484
+ prev = kb.any(undefined, kb.rdfFactory.namedNode('http://www.w3.org/2007/ont/link#redirectedRequest'), prev);
1485
+ if (!prev) {
1486
+ break;
1685
1487
  }
1686
- var diffLocation = null;
1687
- var absContentLocation = null;
1688
- if (contentLocation) {
1689
- absContentLocation = Uri.join(contentLocation, docuri);
1690
- if (absContentLocation !== docuri) {
1691
- diffLocation = absContentLocation;
1692
- }
1488
+ var response = kb.any(prev, kb.rdfFactory.namedNode('http://www.w3.org/2007/ont/link#response'));
1489
+ if (!response) {
1490
+ break;
1693
1491
  }
1694
- if (response.status === 200) {
1695
- this.addType(this.ns.link('Document'), reqNode, kb, docuri);
1696
- if (diffLocation) {
1697
- this.addType(this.ns.link('Document'), reqNode, kb, diffLocation);
1698
- }
1699
-
1700
- // Before we parse new data clear old but only on 200
1701
- if (options.clearPreviousData) {
1702
- // kb.removeDocument(options.resource)
1703
- // only remove content, keep metatdata
1704
- var sts = kb.statementsMatching(undefined, undefined, undefined, options.resource).slice(); // Take a copy as this is the actual index
1705
- for (var i = 0; i < sts.length; i++) {
1706
- kb.removeStatement(sts[i]);
1707
- }
1708
- }
1709
- var isImage = contentType.includes('image/') || contentType.includes('application/pdf');
1710
- if (contentType && isImage) {
1711
- this.addType(kb.rdfFactory.namedNode('http://purl.org/dc/terms/Image'), reqNode, kb, docuri);
1712
- if (diffLocation) {
1713
- this.addType(kb.rdfFactory.namedNode('http://purl.org/dc/terms/Image'), reqNode, kb, diffLocation);
1714
- }
1715
- }
1492
+ var redirection = kb.any(response, kb.rdfFactory.namedNode('http://www.w3.org/2007/ont/http#status'));
1493
+ if (!redirection) {
1494
+ break;
1716
1495
  }
1717
-
1718
- // If we have already got the thing at this location, abort
1719
- if (contentLocation) {
1720
- if (!options.force && diffLocation && this.requested[absContentLocation] === 'done') {
1721
- // we have already fetched this
1722
- // should we smush too?
1723
- // log.info("HTTP headers indicate we have already" + " retrieved " +
1724
- // xhr.resource + " as " + absContentLocation + ". Aborting.")
1725
- return this.doneFetch(options, response);
1726
- }
1727
- this.requested[absContentLocation] = true;
1728
- }
1729
- this.parseLinkHeader(headers.get('link'), options.original, reqNode);
1730
- var handler = this.handlerForContentType(contentType, response);
1731
- if (!handler) {
1732
- // Not a problem, we just don't extract data
1733
- this.addStatus(reqNode, 'Fetch over. No data handled.');
1734
- return this.doneFetch(options, response);
1496
+ // @ts-ignore always true?
1497
+ if (redirection !== '301' && redirection !== '302') {
1498
+ break;
1735
1499
  }
1736
- return response.text()
1737
- // @ts-ignore Types seem right
1738
- .then(function (responseText) {
1739
- response.responseText = responseText;
1740
- return handler.parse(_this11, responseText, options, response);
1741
- });
1742
1500
  }
1743
- }, {
1744
- key: "saveErrorResponse",
1745
- value: function saveErrorResponse(response, responseNode) {
1746
- var _this12 = this;
1747
- var kb = this.store;
1748
- return response.text().then(function (content) {
1749
- if (content.length > 10) {
1750
- kb.add(responseNode, _this12.ns.http('content'), kb.rdfFactory.literal(content), responseNode);
1751
- }
1501
+ }
1502
+
1503
+ /**
1504
+ * Handle fetch() response
1505
+ */
1506
+ handleResponse(response, docuri, options) {
1507
+ const kb = this.store;
1508
+ const headers = response.headers;
1509
+ const reqNode = options.req;
1510
+ const responseNode = this.saveResponseMetadata(response, options);
1511
+ const contentType = this.normalizedContentType(options, headers) || '';
1512
+ let contentLocation = headers.get('content-location');
1513
+
1514
+ // this.fireCallbacks('recv', xhr.args)
1515
+ // this.fireCallbacks('headers', [{uri: docuri, headers: xhr.headers}])
1516
+
1517
+ // Check for masked errors (CORS, etc)
1518
+ if (response.status === 0) {
1519
+ // console.log('Masked error - status 0 for ' + docuri)
1520
+ return this.handleError(response, docuri, options);
1521
+ }
1522
+ if (response.status >= 400) {
1523
+ if (response.status === 404) {
1524
+ this.nonexistent[options.original.value] = true;
1525
+ this.nonexistent[docuri] = true;
1526
+ }
1527
+ return this.saveErrorResponse(response, responseNode).then(() => {
1528
+ let errorMessage = options.resource + ' ' + response.statusText;
1529
+ return this.failFetch(options, errorMessage, response.status, response);
1752
1530
  });
1753
1531
  }
1754
- }, {
1755
- key: "handlerForContentType",
1756
- value: function handlerForContentType(contentType, response) {
1757
- if (!contentType) {
1758
- return null;
1532
+ var diffLocation = null;
1533
+ var absContentLocation = null;
1534
+ if (contentLocation) {
1535
+ absContentLocation = Uri.join(contentLocation, docuri);
1536
+ if (absContentLocation !== docuri) {
1537
+ diffLocation = absContentLocation;
1759
1538
  }
1760
- var Handler = this.handlers.find(function (handler) {
1761
- return contentType.match(handler.pattern);
1762
- });
1763
-
1764
- // @ts-ignore in practice all Handlers have constructors.
1765
- return Handler ? new Handler(response) : null;
1766
1539
  }
1767
- }, {
1768
- key: "guessContentType",
1769
- value: function guessContentType(uri) {
1770
- return CONTENT_TYPE_BY_EXT[uri.split('.').pop()];
1771
- }
1772
- }, {
1773
- key: "normalizedContentType",
1774
- value: function normalizedContentType(options, headers) {
1775
- if (options.forceContentType) {
1776
- return options.forceContentType;
1540
+ if (response.status === 200) {
1541
+ this.addType(this.ns.link('Document'), reqNode, kb, docuri);
1542
+ if (diffLocation) {
1543
+ this.addType(this.ns.link('Document'), reqNode, kb, diffLocation);
1777
1544
  }
1778
- var contentType = headers.get('content-type');
1779
- if (!contentType || contentType.includes('application/octet-stream')) {
1780
- var guess = this.guessContentType(options.resource.value);
1781
- if (guess) {
1782
- return guess;
1545
+
1546
+ // Before we parse new data clear old but only on 200
1547
+ if (options.clearPreviousData) {
1548
+ // kb.removeDocument(options.resource)
1549
+ // only remove content, keep metatdata
1550
+ const sts = kb.statementsMatching(undefined, undefined, undefined, options.resource).slice(); // Take a copy as this is the actual index
1551
+ for (let i = 0; i < sts.length; i++) {
1552
+ kb.removeStatement(sts[i]);
1783
1553
  }
1784
1554
  }
1785
- var protocol = Uri.protocol(options.resource.value);
1786
- if (!contentType && ['file', 'chrome'].includes(protocol)) {
1787
- return 'text/xml';
1555
+ let isImage = contentType.includes('image/') || contentType.includes('application/pdf');
1556
+ if (contentType && isImage) {
1557
+ this.addType(kb.rdfFactory.namedNode('http://purl.org/dc/terms/Image'), reqNode, kb, docuri);
1558
+ if (diffLocation) {
1559
+ this.addType(kb.rdfFactory.namedNode('http://purl.org/dc/terms/Image'), reqNode, kb, diffLocation);
1560
+ }
1788
1561
  }
1789
- return contentType;
1790
1562
  }
1791
1563
 
1792
- /**
1793
- * Sends a new request to the specified uri. (Extracted from `onerrorFactory()`)
1794
- */
1795
- }, {
1796
- key: "redirectToProxy",
1797
- value: function redirectToProxy(newURI, options) {
1798
- var _this13 = this;
1799
- this.addStatus(options.req, 'BLOCKED -> Cross-site Proxy to <' + newURI + '>');
1800
- options.proxyUsed = true;
1801
- var kb = this.store;
1802
- var oldReq = options.req; // request metadata blank node
1803
-
1804
- if (!options.noMeta) {
1805
- kb.add(oldReq, this.ns.link('redirectedTo'), kb.rdfFactory.namedNode(newURI), oldReq);
1806
- this.addStatus(oldReq, 'redirected to new request'); // why
1564
+ // If we have already got the thing at this location, abort
1565
+ if (contentLocation) {
1566
+ if (!options.force && diffLocation && this.requested[absContentLocation] === 'done') {
1567
+ // we have already fetched this
1568
+ // should we smush too?
1569
+ // log.info("HTTP headers indicate we have already" + " retrieved " +
1570
+ // xhr.resource + " as " + absContentLocation + ". Aborting.")
1571
+ return this.doneFetch(options, response);
1807
1572
  }
1808
- this.requested[options.resource.value] = 'redirected';
1809
- this.redirectedTo[options.resource.value] = newURI;
1810
- var newOptions = Object.assign({}, options);
1811
- newOptions.baseURI = options.resource.value;
1812
- return this.fetchUri(newURI, newOptions).then(function (response) {
1813
- if (!newOptions.noMeta) {
1814
- kb.add(oldReq, _this13.ns.link('redirectedRequest'), newOptions.req, _this13.appNode);
1815
- }
1816
- return response;
1817
- });
1573
+ this.requested[absContentLocation] = true;
1818
1574
  }
1819
- }, {
1820
- key: "setRequestTimeout",
1821
- value: function setRequestTimeout(uri, options) {
1822
- var _this14 = this;
1823
- return new Promise(function (resolve) {
1824
- _this14.timeouts[uri] = (_this14.timeouts[uri] || []).concat(setTimeout(function () {
1825
- if (_this14.isPending(uri) && !options.retriedWithNoCredentials && !options.proxyUsed) {
1826
- resolve(_this14.failFetch(options, "Request to ".concat(uri, " timed out"), 'timeout'));
1827
- }
1828
- }, _this14.timeout));
1829
- });
1830
- }
1831
- }, {
1832
- key: "addFetchCallback",
1833
- value: function addFetchCallback(uri, callback) {
1834
- if (!this.fetchCallbacks[uri]) {
1835
- this.fetchCallbacks[uri] = [callback];
1836
- } else {
1837
- this.fetchCallbacks[uri].push(callback);
1838
- }
1575
+ this.parseLinkHeader(headers.get('link'), options.original, reqNode);
1576
+ let handler = this.handlerForContentType(contentType, response);
1577
+ if (!handler) {
1578
+ // Not a problem, we just don't extract data
1579
+ this.addStatus(reqNode, 'Fetch over. No data handled.');
1580
+ return this.doneFetch(options, response);
1839
1581
  }
1840
- }, {
1841
- key: "acceptString",
1842
- value: function acceptString() {
1843
- var acceptstring = '';
1844
- for (var mediaType in this.mediatypes) {
1845
- if (acceptstring !== '') {
1846
- acceptstring += ', ';
1847
- }
1848
- acceptstring += mediaType;
1849
- for (var property in this.mediatypes[mediaType]) {
1850
- acceptstring += ';' + property + '=' + this.mediatypes[mediaType][property];
1851
- }
1852
- }
1853
- return acceptstring;
1854
- }
1855
- // var updatesVia = new $rdf.UpdatesVia(this) // Subscribe to headers
1856
- // @@@@@@@@ This is turned off because it causes a websocket to be set up for ANY fetch
1857
- // whether we want to track it ot not. including ontologies loaed though the XSSproxy
1858
- }], [{
1859
- key: "crossSiteProxy",
1860
- value: function crossSiteProxy(uri) {
1861
- if (Fetcher.crossSiteProxyTemplate) {
1862
- return Fetcher.crossSiteProxyTemplate.replace('{uri}', encodeURIComponent(uri));
1863
- } else {
1864
- return undefined;
1582
+ return response.text()
1583
+ // @ts-ignore Types seem right
1584
+ .then(responseText => {
1585
+ response.responseText = responseText;
1586
+ return handler.parse(this, responseText, options, response);
1587
+ });
1588
+ }
1589
+ saveErrorResponse(response, responseNode) {
1590
+ let kb = this.store;
1591
+ return response.text().then(content => {
1592
+ if (content.length > 10) {
1593
+ kb.add(responseNode, this.ns.http('content'), kb.rdfFactory.literal(content), responseNode);
1865
1594
  }
1595
+ });
1596
+ }
1597
+ handlerForContentType(contentType, response) {
1598
+ if (!contentType) {
1599
+ return null;
1866
1600
  }
1867
- }, {
1868
- key: "offlineOverride",
1869
- value: function offlineOverride(uri) {
1870
- // Map the URI to a localhost proxy if we are running on localhost
1871
- // This is used for working offline, e.g. on planes.
1872
- // Is the script itself is running in localhost, then access all
1873
- // data in a localhost mirror.
1874
- // Do not remove without checking with TimBL
1875
- var requestedURI = uri;
1876
- var UI;
1877
- if (typeof window !== 'undefined' && window.panes && (UI = window.panes.UI) && UI.preferences && UI.preferences.get('offlineModeUsingLocalhost')) {
1878
- if (requestedURI.slice(0, 7) === 'http://' && requestedURI.slice(7, 17) !== 'localhost/') {
1879
- requestedURI = 'http://localhost/' + requestedURI.slice(7);
1880
- log.warn('Localhost kludge for offline use: actually getting <' + requestedURI + '>');
1881
- } else {
1882
- // log.warn("Localhost kludge NOT USED <" + requestedURI + ">")
1883
- }
1884
- } else {
1885
- // log.warn("Localhost kludge OFF offline use: actually getting <" +
1886
- // requestedURI + ">")
1887
- }
1888
- return requestedURI;
1889
- }
1890
- }, {
1891
- key: "proxyIfNecessary",
1892
- value: function proxyIfNecessary(uri) {
1893
- var UI;
1894
- if (typeof window !== 'undefined' && window.panes && (UI = window.panes.UI) && UI.isExtension) {
1895
- return uri;
1896
- } // Extension does not need proxy
1897
-
1898
- if (typeof $SolidTestEnvironment !== 'undefined' && $SolidTestEnvironment.localSiteMap) {
1899
- // nested dictionaries of URI parts from origin down
1900
- var hostpath = uri.split('/').slice(2); // the bit after the //
1901
-
1902
- var lookup = function lookup(parts, index) {
1903
- var z = index[parts.shift()];
1904
- if (!z) {
1905
- return null;
1906
- }
1907
- if (typeof z === 'string') {
1908
- return z + parts.join('/');
1909
- }
1910
- if (!parts) {
1911
- return null;
1912
- }
1913
- return lookup(parts, z);
1914
- };
1915
- var y = lookup(hostpath, $SolidTestEnvironment.localSiteMap);
1916
- if (y) {
1917
- return y;
1918
- }
1919
- }
1601
+ let Handler = this.handlers.find(handler => {
1602
+ return contentType.match(handler.pattern);
1603
+ });
1920
1604
 
1921
- // browser does 2014 on as https browser script not trusted
1922
- // If the web app origin is https: then the mixed content rules
1923
- // prevent it loading insecure http: stuff so we need proxy.
1924
- if (Fetcher.crossSiteProxyTemplate && typeof document !== 'undefined' && document.location && ('' + document.location).slice(0, 6) === 'https:' &&
1925
- // origin is secure
1926
- uri.slice(0, 5) === 'http:') {
1927
- // requested data is not
1928
- return Fetcher.crossSiteProxyTemplate.replace('{uri}', encodeURIComponent(uri));
1605
+ // @ts-ignore in practice all Handlers have constructors.
1606
+ return Handler ? new Handler(response) : null;
1607
+ }
1608
+ guessContentType(uri) {
1609
+ return CONTENT_TYPE_BY_EXT[uri.split('.').pop()];
1610
+ }
1611
+ normalizedContentType(options, headers) {
1612
+ if (options.forceContentType) {
1613
+ return options.forceContentType;
1614
+ }
1615
+ let contentType = headers.get('content-type');
1616
+ if (!contentType || contentType.includes('application/octet-stream')) {
1617
+ let guess = this.guessContentType(options.resource.value);
1618
+ if (guess) {
1619
+ return guess;
1929
1620
  }
1930
- return uri;
1931
1621
  }
1932
-
1933
- /**
1934
- * Tests whether the uri's protocol is supported by the Fetcher.
1935
- * @param uri
1936
- */
1937
- }, {
1938
- key: "unsupportedProtocol",
1939
- value: function unsupportedProtocol(uri) {
1940
- var pcol = Uri.protocol(uri);
1941
- return pcol === 'tel' || pcol === 'mailto' || pcol === 'urn';
1622
+ let protocol = Uri.protocol(options.resource.value);
1623
+ if (!contentType && ['file', 'chrome'].includes(protocol)) {
1624
+ return 'text/xml';
1942
1625
  }
1626
+ return contentType;
1627
+ }
1943
1628
 
1944
- /** Decide on credentials using old XXHR api or new fetch() one
1945
- * @param requestedURI
1946
- * @param options
1947
- */
1948
- }, {
1949
- key: "setCredentials",
1950
- value: function setCredentials(requestedURI) {
1951
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1952
- // 2014 CORS problem:
1953
- // XMLHttpRequest cannot load http://www.w3.org/People/Berners-Lee/card.
1954
- // A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin'
1955
- // header when the credentials flag is true.
1956
- // @ Many ontology files under http: and need CORS wildcard ->
1957
- // can't have credentials
1958
- if (options.credentials === undefined) {
1959
- // Caller using new fetch convention
1960
- if (options.withCredentials !== undefined) {
1961
- // XHR style is what Fetcher specified before
1962
- options.credentials = options.withCredentials ? 'include' : 'omit';
1963
- } else {
1964
- options.credentials = 'include'; // default is to be logged on
1629
+ /**
1630
+ * Sends a new request to the specified uri. (Extracted from `onerrorFactory()`)
1631
+ */
1632
+ redirectToProxy(newURI, options) {
1633
+ this.addStatus(options.req, 'BLOCKED -> Cross-site Proxy to <' + newURI + '>');
1634
+ options.proxyUsed = true;
1635
+ const kb = this.store;
1636
+ const oldReq = options.req; // request metadata blank node
1637
+
1638
+ if (!options.noMeta) {
1639
+ kb.add(oldReq, this.ns.link('redirectedTo'), kb.rdfFactory.namedNode(newURI), oldReq);
1640
+ this.addStatus(oldReq, 'redirected to new request'); // why
1641
+ }
1642
+ this.requested[options.resource.value] = 'redirected';
1643
+ this.redirectedTo[options.resource.value] = newURI;
1644
+ let newOptions = Object.assign({}, options);
1645
+ newOptions.baseURI = options.resource.value;
1646
+ return this.fetchUri(newURI, newOptions).then(response => {
1647
+ if (!newOptions.noMeta) {
1648
+ kb.add(oldReq, this.ns.link('redirectedRequest'), newOptions.req, this.appNode);
1649
+ }
1650
+ return response;
1651
+ });
1652
+ }
1653
+ setRequestTimeout(uri, options) {
1654
+ return new Promise(resolve => {
1655
+ this.timeouts[uri] = (this.timeouts[uri] || []).concat(setTimeout(() => {
1656
+ if (this.isPending(uri) && !options.retriedWithNoCredentials && !options.proxyUsed) {
1657
+ resolve(this.failFetch(options, `Request to ${uri} timed out`, 'timeout'));
1965
1658
  }
1659
+ }, this.timeout));
1660
+ });
1661
+ }
1662
+ addFetchCallback(uri, callback) {
1663
+ if (!this.fetchCallbacks[uri]) {
1664
+ this.fetchCallbacks[uri] = [callback];
1665
+ } else {
1666
+ this.fetchCallbacks[uri].push(callback);
1667
+ }
1668
+ }
1669
+ acceptString() {
1670
+ let acceptstring = '';
1671
+ for (let mediaType in this.mediatypes) {
1672
+ if (acceptstring !== '') {
1673
+ acceptstring += ', ';
1674
+ }
1675
+ acceptstring += mediaType;
1676
+ for (let property in this.mediatypes[mediaType]) {
1677
+ acceptstring += ';' + property + '=' + this.mediatypes[mediaType][property];
1966
1678
  }
1967
1679
  }
1968
- }]);
1969
- }();
1680
+ return acceptstring;
1681
+ }
1682
+ // var updatesVia = new $rdf.UpdatesVia(this) // Subscribe to headers
1683
+ // @@@@@@@@ This is turned off because it causes a websocket to be set up for ANY fetch
1684
+ // whether we want to track it ot not. including ontologies loaed though the XSSproxy
1685
+ }
1970
1686
  _defineProperty(Fetcher, "HANDLERS", void 0);
1971
1687
  _defineProperty(Fetcher, "CONTENT_TYPE_BY_EXT", void 0);
1972
1688
  // TODO: Document this
1973
1689
  _defineProperty(Fetcher, "crossSiteProxyTemplate", void 0);
1974
- export { Fetcher as default };
1975
1690
  Fetcher.HANDLERS = defaultHandlers;
1976
1691
  Fetcher.CONTENT_TYPE_BY_EXT = CONTENT_TYPE_BY_EXT;