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