rdflib 2.2.22-2f49e8ef → 2.2.22-53d65d90

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