rdflib 2.2.21-d55d15fa → 2.2.21-e3e59ee9
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 +9 -1
- package/dist/rdflib.min.js.map +1 -1
- package/esm/blank-node.js +61 -114
- package/esm/class-order.js +1 -1
- package/esm/collection.js +70 -128
- package/esm/convert.js +1 -2
- package/esm/default-graph.js +14 -48
- package/esm/empty.js +8 -39
- package/esm/factories/canonical-data-factory.js +33 -65
- package/esm/factories/extended-term-factory.js +18 -25
- package/esm/factories/factory-types.js +3 -2
- package/esm/factories/rdflib-data-factory.js +9 -19
- package/esm/fetcher.js +1341 -1854
- package/esm/formula.js +639 -846
- package/esm/index.js +40 -76
- package/esm/jsonldparser.js +24 -49
- package/esm/jsonparser.js +1 -8
- package/esm/lists.js +47 -110
- package/esm/literal.js +120 -189
- package/esm/log.js +7 -7
- package/esm/n3parser.js +1015 -1412
- package/esm/named-node.js +70 -119
- package/esm/namespace.js +2 -5
- package/esm/node-internal.js +73 -110
- package/esm/node.js +2 -7
- package/esm/parse.js +12 -19
- package/esm/patch-parser.js +10 -30
- package/esm/query-to-sparql.js +0 -18
- package/esm/query.js +63 -147
- package/esm/rdfaparser.js +794 -997
- package/esm/rdfxmlparser.js +347 -461
- package/esm/serialize.js +10 -28
- package/esm/serializer.js +820 -1049
- package/esm/sparql-to-query.js +44 -134
- package/esm/statement.js +54 -85
- package/esm/store.js +829 -1103
- package/esm/types.js +22 -21
- package/esm/update-manager.js +862 -1094
- package/esm/updates-via.js +104 -161
- package/esm/uri.js +9 -53
- package/esm/utils/default-graph-uri.js +3 -2
- package/esm/utils/termValue.js +0 -1
- package/esm/utils/terms.js +19 -21
- package/esm/utils-js.js +20 -61
- package/esm/utils.js +10 -21
- package/esm/variable.js +32 -78
- package/esm/xsd.js +2 -2
- package/lib/blank-node.js +60 -113
- package/lib/class-order.js +1 -2
- package/lib/collection.js +69 -131
- package/lib/convert.js +3 -9
- package/lib/default-graph.js +13 -52
- package/lib/empty.js +8 -43
- package/lib/factories/canonical-data-factory.js +35 -79
- package/lib/factories/extended-term-factory.js +18 -32
- package/lib/factories/factory-types.d.ts +6 -6
- package/lib/factories/factory-types.js +1 -4
- package/lib/factories/rdflib-data-factory.js +9 -23
- package/lib/fetcher.d.ts +6 -6
- package/lib/fetcher.js +1370 -1843
- package/lib/formula.js +640 -855
- package/lib/index.js +66 -152
- package/lib/jsonldparser.js +23 -53
- package/lib/jsonparser.js +1 -10
- package/lib/lists.js +55 -112
- package/lib/literal.js +120 -195
- package/lib/log.d.ts +0 -6
- package/lib/log.js +7 -8
- package/lib/n3parser.js +1030 -1436
- package/lib/named-node.js +69 -126
- package/lib/namespace.js +2 -7
- package/lib/node-internal.js +74 -107
- package/lib/node.js +2 -12
- package/lib/parse.d.ts +1 -1
- package/lib/parse.js +12 -32
- package/lib/patch-parser.js +11 -34
- package/lib/query-to-sparql.js +0 -23
- package/lib/query.js +62 -167
- package/lib/rdfaparser.js +796 -1009
- package/lib/rdfxmlparser.js +349 -466
- package/lib/serialize.js +12 -38
- package/lib/serializer.js +823 -1064
- package/lib/sparql-to-query.js +42 -167
- package/lib/statement.js +55 -91
- package/lib/store.d.ts +1 -1
- package/lib/store.js +849 -1112
- package/lib/tf-types.d.ts +4 -4
- package/lib/types.d.ts +8 -8
- package/lib/types.js +23 -23
- package/lib/update-manager.d.ts +2 -2
- package/lib/update-manager.js +870 -1103
- package/lib/updates-via.js +105 -164
- package/lib/uri.js +8 -61
- package/lib/utils/default-graph-uri.js +3 -5
- package/lib/utils/termValue.js +0 -2
- package/lib/utils/terms.js +19 -40
- package/lib/utils-js.js +23 -88
- package/lib/utils.js +10 -27
- package/lib/variable.js +34 -85
- package/lib/xsd-internal.js +0 -3
- package/lib/xsd.js +2 -6
- package/package.json +37 -36
- package/src/fetcher.ts +2 -2
- package/src/update-manager.ts +18 -7
- package/changes.txt +0 -59
package/esm/update-manager.js
CHANGED
|
@@ -1,14 +1,4 @@
|
|
|
1
|
-
import _typeof from "@babel/runtime/helpers/typeof";
|
|
2
|
-
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
|
3
|
-
import _createClass from "@babel/runtime/helpers/createClass";
|
|
4
1
|
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
5
|
-
|
|
6
|
-
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; } } }; }
|
|
7
|
-
|
|
8
|
-
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); }
|
|
9
|
-
|
|
10
|
-
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; }
|
|
11
|
-
|
|
12
2
|
/* @file Update Manager Class
|
|
13
3
|
**
|
|
14
4
|
** 2007-07-15 originall sparl update module by Joe Presbrey <presbrey@mit.edu>
|
|
@@ -23,14 +13,13 @@ import Serializer from './serializer';
|
|
|
23
13
|
import { isBlankNode, isStore } from './utils/terms';
|
|
24
14
|
import * as Util from './utils-js';
|
|
25
15
|
import { termValue } from './utils/termValue';
|
|
26
|
-
|
|
27
16
|
/**
|
|
28
17
|
* The UpdateManager is a helper object for a store.
|
|
29
18
|
* Just as a Fetcher provides the store with the ability to read and write,
|
|
30
19
|
* the Update Manager provides functionality for making small patches in real time,
|
|
31
20
|
* and also looking out for concurrent updates from other agents
|
|
32
21
|
*/
|
|
33
|
-
|
|
22
|
+
export default class UpdateManager {
|
|
34
23
|
/** Index of objects for coordinating incoming and outgoing patches */
|
|
35
24
|
|
|
36
25
|
/** Object of namespaces */
|
|
@@ -38,29 +27,19 @@ var UpdateManager = /*#__PURE__*/function () {
|
|
|
38
27
|
/**
|
|
39
28
|
* @param store - The quadstore to store data and metadata. Created if not passed.
|
|
40
29
|
*/
|
|
41
|
-
|
|
42
|
-
_classCallCheck(this, UpdateManager);
|
|
43
|
-
|
|
30
|
+
constructor(store) {
|
|
44
31
|
_defineProperty(this, "store", void 0);
|
|
45
|
-
|
|
46
32
|
_defineProperty(this, "ifps", void 0);
|
|
47
|
-
|
|
48
33
|
_defineProperty(this, "fps", void 0);
|
|
49
|
-
|
|
50
34
|
_defineProperty(this, "patchControl", void 0);
|
|
51
|
-
|
|
52
35
|
_defineProperty(this, "ns", void 0);
|
|
53
|
-
|
|
54
36
|
store = store || new IndexedFormula();
|
|
55
|
-
|
|
56
37
|
if (store.updater) {
|
|
57
38
|
throw new Error("You can't have two UpdateManagers for the same store");
|
|
58
39
|
}
|
|
59
|
-
|
|
60
40
|
if (!store.fetcher) {
|
|
61
41
|
store.fetcher = new Fetcher(store);
|
|
62
42
|
}
|
|
63
|
-
|
|
64
43
|
this.store = store;
|
|
65
44
|
store.updater = this;
|
|
66
45
|
this.ifps = {};
|
|
@@ -76,1184 +55,973 @@ var UpdateManager = /*#__PURE__*/function () {
|
|
|
76
55
|
this.ns.owl = Namespace('http://www.w3.org/2002/07/owl#');
|
|
77
56
|
this.patchControl = [];
|
|
78
57
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
value: function patchControlFor(doc) {
|
|
83
|
-
if (!this.patchControl[doc.value]) {
|
|
84
|
-
this.patchControl[doc.value] = [];
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return this.patchControl[doc.value];
|
|
58
|
+
patchControlFor(doc) {
|
|
59
|
+
if (!this.patchControl[doc.value]) {
|
|
60
|
+
this.patchControl[doc.value] = [];
|
|
88
61
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Tests whether a file is editable.
|
|
96
|
-
* If the file has a specific annotation that it is machine written,
|
|
97
|
-
* for safety, it is editable (this doesn't actually check for write access)
|
|
98
|
-
* If the file has wac-allow and accept patch headers, those are respected.
|
|
99
|
-
* and local write access is determined by those headers.
|
|
100
|
-
* This version only looks at past HTTP requests, does not make new ones.
|
|
101
|
-
*
|
|
102
|
-
* @returns The method string SPARQL or DAV or
|
|
103
|
-
* LOCALFILE or false if known, undefined if not known.
|
|
104
|
-
*/
|
|
105
|
-
|
|
106
|
-
}, {
|
|
107
|
-
key: "editable",
|
|
108
|
-
value: function editable(uri, kb) {
|
|
109
|
-
if (!uri) {
|
|
110
|
-
return false; // Eg subject is bnode, no known doc to write to
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (!kb) {
|
|
114
|
-
kb = this.store;
|
|
115
|
-
}
|
|
62
|
+
return this.patchControl[doc.value];
|
|
63
|
+
}
|
|
64
|
+
isHttpUri(uri) {
|
|
65
|
+
return uri.slice(0, 4) === 'http';
|
|
66
|
+
}
|
|
116
67
|
|
|
117
|
-
|
|
68
|
+
/**
|
|
69
|
+
* Tests whether a file is editable.
|
|
70
|
+
* If the file has a specific annotation that it is machine written,
|
|
71
|
+
* for safety, it is editable (this doesn't actually check for write access)
|
|
72
|
+
* If the file has wac-allow and accept patch headers, those are respected.
|
|
73
|
+
* and local write access is determined by those headers.
|
|
74
|
+
* This version only looks at past HTTP requests, does not make new ones.
|
|
75
|
+
*
|
|
76
|
+
* @returns The method string SPARQL or DAV or
|
|
77
|
+
* LOCALFILE or false if known, undefined if not known.
|
|
78
|
+
*/
|
|
79
|
+
editable(uri, kb) {
|
|
80
|
+
if (!uri) {
|
|
81
|
+
return false; // Eg subject is bnode, no known doc to write to
|
|
82
|
+
}
|
|
118
83
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
84
|
+
if (!kb) {
|
|
85
|
+
kb = this.store;
|
|
86
|
+
}
|
|
87
|
+
uri = termValue(uri);
|
|
88
|
+
if (!this.isHttpUri(uri)) {
|
|
89
|
+
if (kb.holds(this.store.rdfFactory.namedNode(uri), this.store.rdfFactory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), this.store.rdfFactory.namedNode('http://www.w3.org/2007/ont/link#MachineEditableDocument'))) {
|
|
90
|
+
return 'LOCALFILE';
|
|
123
91
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
92
|
+
}
|
|
93
|
+
var request;
|
|
94
|
+
var definitive = false;
|
|
95
|
+
// @ts-ignore passes a string to kb.each, which expects a term. Should this work?
|
|
96
|
+
var requests = kb.each(undefined, this.ns.link('requestedURI'), docpart(uri));
|
|
97
|
+
var method;
|
|
98
|
+
for (var r = 0; r < requests.length; r++) {
|
|
99
|
+
request = requests[r];
|
|
100
|
+
if (request !== undefined) {
|
|
101
|
+
var response = kb.any(request, this.ns.link('response'));
|
|
134
102
|
if (request !== undefined) {
|
|
135
|
-
var
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
_step;
|
|
143
|
-
|
|
144
|
-
try {
|
|
145
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
146
|
-
var bit = _step.value;
|
|
147
|
-
var lr = bit.split('=');
|
|
148
|
-
|
|
149
|
-
if (lr[0].includes('user') && !lr[1].includes('write') && !lr[1].includes('append')) {
|
|
150
|
-
// console.log(' editable? excluded by WAC-Allow: ', wacAllow)
|
|
151
|
-
return false;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
} catch (err) {
|
|
155
|
-
_iterator.e(err);
|
|
156
|
-
} finally {
|
|
157
|
-
_iterator.f();
|
|
103
|
+
var wacAllow = kb.anyValue(response, this.ns.httph('wac-allow'));
|
|
104
|
+
if (wacAllow) {
|
|
105
|
+
for (var bit of wacAllow.split(',')) {
|
|
106
|
+
var lr = bit.split('=');
|
|
107
|
+
if (lr[0].includes('user') && !lr[1].includes('write') && !lr[1].includes('append')) {
|
|
108
|
+
// console.log(' editable? excluded by WAC-Allow: ', wacAllow)
|
|
109
|
+
return false;
|
|
158
110
|
}
|
|
159
111
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
if (method.indexOf('application/sparql-update-single-match') >= 0) return 'SPARQL';
|
|
168
|
-
}
|
|
112
|
+
}
|
|
113
|
+
var acceptPatch = kb.each(response, this.ns.httph('accept-patch'));
|
|
114
|
+
if (acceptPatch.length) {
|
|
115
|
+
for (let i = 0; i < acceptPatch.length; i++) {
|
|
116
|
+
method = acceptPatch[i].value.trim();
|
|
117
|
+
if (method.indexOf('application/sparql-update') >= 0) return 'SPARQL';
|
|
118
|
+
if (method.indexOf('application/sparql-update-single-match') >= 0) return 'SPARQL';
|
|
169
119
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
if (method.indexOf('DAV') >= 0) {
|
|
182
|
-
return 'DAV';
|
|
183
|
-
}
|
|
120
|
+
}
|
|
121
|
+
var authorVia = kb.each(response, this.ns.httph('ms-author-via'));
|
|
122
|
+
if (authorVia.length) {
|
|
123
|
+
for (let i = 0; i < authorVia.length; i++) {
|
|
124
|
+
method = authorVia[i].value.trim();
|
|
125
|
+
if (method.indexOf('SPARQL') >= 0) {
|
|
126
|
+
return 'SPARQL';
|
|
127
|
+
}
|
|
128
|
+
if (method.indexOf('DAV') >= 0) {
|
|
129
|
+
return 'DAV';
|
|
184
130
|
}
|
|
185
131
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
definitive = true; // return false // A definitive answer
|
|
198
|
-
}
|
|
132
|
+
}
|
|
133
|
+
if (!this.isHttpUri(uri)) {
|
|
134
|
+
if (!wacAllow) return false;else return 'LOCALFILE';
|
|
135
|
+
}
|
|
136
|
+
var status = kb.each(response, this.ns.http('status'));
|
|
137
|
+
if (status.length) {
|
|
138
|
+
for (let i = 0; i < status.length; i++) {
|
|
139
|
+
// @ts-ignore since statuses should be TFTerms, this should always be false
|
|
140
|
+
if (status[i] === 200 || status[i] === 404) {
|
|
141
|
+
definitive = true;
|
|
142
|
+
// return false // A definitive answer
|
|
199
143
|
}
|
|
200
144
|
}
|
|
201
|
-
} else {// console.log('UpdateManager.editable: No response for ' + uri + '\n')
|
|
202
145
|
}
|
|
146
|
+
} else {
|
|
147
|
+
// console.log('UpdateManager.editable: No response for ' + uri + '\n')
|
|
203
148
|
}
|
|
204
149
|
}
|
|
205
|
-
|
|
206
|
-
if (requests.length === 0) {// console.log('UpdateManager.editable: No request for ' + uri + '\n')
|
|
207
|
-
} else {
|
|
208
|
-
if (definitive) {
|
|
209
|
-
return false; // We have got a request and it did NOT say editable => not editable
|
|
210
|
-
}
|
|
211
|
-
} // console.log('UpdateManager.editable: inconclusive for ' + uri + '\n')
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
return undefined; // We don't know (yet) as we haven't had a response (yet)
|
|
215
|
-
}
|
|
216
|
-
}, {
|
|
217
|
-
key: "anonymize",
|
|
218
|
-
value: function anonymize(obj) {
|
|
219
|
-
return obj.toNT().substr(0, 2) === '_:' && this.mentioned(obj) ? '?' + obj.toNT().substr(2) : obj.toNT();
|
|
220
|
-
}
|
|
221
|
-
}, {
|
|
222
|
-
key: "anonymizeNT",
|
|
223
|
-
value: function anonymizeNT(stmt) {
|
|
224
|
-
return this.anonymize(stmt.subject) + ' ' + this.anonymize(stmt.predicate) + ' ' + this.anonymize(stmt.object) + ' .';
|
|
225
|
-
}
|
|
226
|
-
}, {
|
|
227
|
-
key: "nTriples",
|
|
228
|
-
value: function nTriples(stmt) {
|
|
229
|
-
return "".concat(stmt.subject.toNT(), " ").concat(stmt.predicate.toNT(), " ").concat(stmt.object.toNT(), " .");
|
|
230
|
-
}
|
|
231
|
-
/**
|
|
232
|
-
* Returns a list of all bnodes occurring in a statement
|
|
233
|
-
* @private
|
|
234
|
-
*/
|
|
235
|
-
|
|
236
|
-
}, {
|
|
237
|
-
key: "statementBnodes",
|
|
238
|
-
value: function statementBnodes(st) {
|
|
239
|
-
return [st.subject, st.predicate, st.object].filter(function (x) {
|
|
240
|
-
return isBlankNode(x);
|
|
241
|
-
});
|
|
242
150
|
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
}, {
|
|
249
|
-
key: "statementArrayBnodes",
|
|
250
|
-
value: function statementArrayBnodes(sts) {
|
|
251
|
-
var bnodes = [];
|
|
252
|
-
|
|
253
|
-
for (var i = 0; i < sts.length; i++) {
|
|
254
|
-
bnodes = bnodes.concat(this.statementBnodes(sts[i]));
|
|
151
|
+
if (requests.length === 0) {
|
|
152
|
+
// console.log('UpdateManager.editable: No request for ' + uri + '\n')
|
|
153
|
+
} else {
|
|
154
|
+
if (definitive) {
|
|
155
|
+
return false; // We have got a request and it did NOT say editable => not editable
|
|
255
156
|
}
|
|
256
|
-
|
|
257
|
-
bnodes.sort(); // in place sort - result may have duplicates
|
|
258
|
-
|
|
259
|
-
var bnodes2 = [];
|
|
260
|
-
|
|
261
|
-
for (var j = 0; j < bnodes.length; j++) {
|
|
262
|
-
if (j === 0 || !bnodes[j].equals(bnodes[j - 1])) {
|
|
263
|
-
bnodes2.push(bnodes[j]);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
return bnodes2;
|
|
268
157
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
*/
|
|
273
|
-
|
|
274
|
-
}, {
|
|
275
|
-
key: "cacheIfps",
|
|
276
|
-
value: function cacheIfps() {
|
|
277
|
-
this.ifps = {};
|
|
278
|
-
var a = this.store.each(undefined, this.ns.rdf('type'), this.ns.owl('InverseFunctionalProperty'));
|
|
158
|
+
// console.log('UpdateManager.editable: inconclusive for ' + uri + '\n')
|
|
159
|
+
return undefined; // We don't know (yet) as we haven't had a response (yet)
|
|
160
|
+
}
|
|
279
161
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
162
|
+
anonymize(obj) {
|
|
163
|
+
return obj.toNT().substr(0, 2) === '_:' && this.mentioned(obj) ? '?' + obj.toNT().substr(2) : obj.toNT();
|
|
164
|
+
}
|
|
165
|
+
anonymizeNT(stmt) {
|
|
166
|
+
return this.anonymize(stmt.subject) + ' ' + this.anonymize(stmt.predicate) + ' ' + this.anonymize(stmt.object) + ' .';
|
|
167
|
+
}
|
|
168
|
+
nTriples(stmt) {
|
|
169
|
+
return `${stmt.subject.toNT()} ${stmt.predicate.toNT()} ${stmt.object.toNT()} .`;
|
|
170
|
+
}
|
|
283
171
|
|
|
284
|
-
|
|
285
|
-
|
|
172
|
+
/**
|
|
173
|
+
* Returns a list of all bnodes occurring in a statement
|
|
174
|
+
* @private
|
|
175
|
+
*/
|
|
176
|
+
statementBnodes(st) {
|
|
177
|
+
return [st.subject, st.predicate, st.object].filter(function (x) {
|
|
178
|
+
return isBlankNode(x);
|
|
179
|
+
});
|
|
180
|
+
}
|
|
286
181
|
|
|
287
|
-
|
|
288
|
-
|
|
182
|
+
/**
|
|
183
|
+
* Returns a list of all bnodes occurring in a list of statements
|
|
184
|
+
* @private
|
|
185
|
+
*/
|
|
186
|
+
statementArrayBnodes(sts) {
|
|
187
|
+
var bnodes = [];
|
|
188
|
+
for (let i = 0; i < sts.length; i++) {
|
|
189
|
+
bnodes = bnodes.concat(this.statementBnodes(sts[i]));
|
|
190
|
+
}
|
|
191
|
+
bnodes.sort(); // in place sort - result may have duplicates
|
|
192
|
+
var bnodes2 = [];
|
|
193
|
+
for (let j = 0; j < bnodes.length; j++) {
|
|
194
|
+
if (j === 0 || !bnodes[j].equals(bnodes[j - 1])) {
|
|
195
|
+
bnodes2.push(bnodes[j]);
|
|
289
196
|
}
|
|
290
197
|
}
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
* @private
|
|
294
|
-
*/
|
|
295
|
-
|
|
296
|
-
}, {
|
|
297
|
-
key: "bnodeContext2",
|
|
298
|
-
value: function bnodeContext2(x, source, depth) {
|
|
299
|
-
// Return a list of statements which indirectly identify a node
|
|
300
|
-
// Depth > 1 if try further indirection.
|
|
301
|
-
// Return array of statements (possibly empty), or null if failure
|
|
302
|
-
var sts = this.store.statementsMatching(undefined, undefined, x, source); // incoming links
|
|
303
|
-
|
|
304
|
-
var y;
|
|
305
|
-
var res;
|
|
306
|
-
|
|
307
|
-
for (var i = 0; i < sts.length; i++) {
|
|
308
|
-
if (this.fps[sts[i].predicate.value]) {
|
|
309
|
-
y = sts[i].subject;
|
|
310
|
-
|
|
311
|
-
if (!y.isBlank) {
|
|
312
|
-
return [sts[i]];
|
|
313
|
-
}
|
|
198
|
+
return bnodes2;
|
|
199
|
+
}
|
|
314
200
|
|
|
315
|
-
|
|
316
|
-
|
|
201
|
+
/**
|
|
202
|
+
* Makes a cached list of [Inverse-]Functional properties
|
|
203
|
+
* @private
|
|
204
|
+
*/
|
|
205
|
+
cacheIfps() {
|
|
206
|
+
this.ifps = {};
|
|
207
|
+
var a = this.store.each(undefined, this.ns.rdf('type'), this.ns.owl('InverseFunctionalProperty'));
|
|
208
|
+
for (let i = 0; i < a.length; i++) {
|
|
209
|
+
this.ifps[a[i].value] = true;
|
|
210
|
+
}
|
|
211
|
+
this.fps = {};
|
|
212
|
+
a = this.store.each(undefined, this.ns.rdf('type'), this.ns.owl('FunctionalProperty'));
|
|
213
|
+
for (let i = 0; i < a.length; i++) {
|
|
214
|
+
this.fps[a[i].value] = true;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
317
217
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
218
|
+
/**
|
|
219
|
+
* Returns a context to bind a given node, up to a given depth
|
|
220
|
+
* @private
|
|
221
|
+
*/
|
|
222
|
+
bnodeContext2(x, source, depth) {
|
|
223
|
+
// Return a list of statements which indirectly identify a node
|
|
224
|
+
// Depth > 1 if try further indirection.
|
|
225
|
+
// Return array of statements (possibly empty), or null if failure
|
|
226
|
+
var sts = this.store.statementsMatching(undefined, undefined, x, source); // incoming links
|
|
227
|
+
var y;
|
|
228
|
+
var res;
|
|
229
|
+
for (let i = 0; i < sts.length; i++) {
|
|
230
|
+
if (this.fps[sts[i].predicate.value]) {
|
|
231
|
+
y = sts[i].subject;
|
|
232
|
+
if (!y.isBlank) {
|
|
233
|
+
return [sts[i]];
|
|
322
234
|
}
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
for (var _i4 = 0; _i4 < sts.length; _i4++) {
|
|
329
|
-
if (this.ifps[sts[_i4].predicate.value]) {
|
|
330
|
-
y = sts[_i4].object;
|
|
331
|
-
|
|
332
|
-
if (!y.isBlank) {
|
|
333
|
-
return [sts[_i4]];
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
if (depth) {
|
|
337
|
-
res = this.bnodeContext2(y, source, depth - 1);
|
|
338
|
-
|
|
339
|
-
if (res) {
|
|
340
|
-
return res.concat([sts[_i4]]);
|
|
341
|
-
}
|
|
235
|
+
if (depth) {
|
|
236
|
+
res = this.bnodeContext2(y, source, depth - 1);
|
|
237
|
+
if (res) {
|
|
238
|
+
return res.concat([sts[i]]);
|
|
342
239
|
}
|
|
343
240
|
}
|
|
344
241
|
}
|
|
345
|
-
|
|
346
|
-
return null; // Failure
|
|
347
242
|
}
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
if (con !== null) return con;
|
|
362
|
-
} // If we can't guarantee unique with logic just send all info about node
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
return this.store.connectedStatements(x, source); // was:
|
|
366
|
-
// throw new Error('Unable to uniquely identify bnode: ' + x.toNT())
|
|
367
|
-
}
|
|
368
|
-
/**
|
|
369
|
-
* @private
|
|
370
|
-
*/
|
|
371
|
-
|
|
372
|
-
}, {
|
|
373
|
-
key: "mentioned",
|
|
374
|
-
value: function mentioned(x) {
|
|
375
|
-
return this.store.statementsMatching(x, null, null, null).length !== 0 || // Don't pin fresh bnodes
|
|
376
|
-
this.store.statementsMatching(null, x).length !== 0 || this.store.statementsMatching(null, null, x).length !== 0;
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* @private
|
|
380
|
-
*/
|
|
381
|
-
|
|
382
|
-
}, {
|
|
383
|
-
key: "bnodeContext",
|
|
384
|
-
value: function bnodeContext(bnodes, doc) {
|
|
385
|
-
var context = [];
|
|
386
|
-
|
|
387
|
-
if (bnodes.length) {
|
|
388
|
-
this.cacheIfps();
|
|
389
|
-
|
|
390
|
-
for (var i = 0; i < bnodes.length; i++) {
|
|
391
|
-
// Does this occur in old graph?
|
|
392
|
-
var bnode = bnodes[i];
|
|
393
|
-
if (!this.mentioned(bnode)) continue;
|
|
394
|
-
context = context.concat(this.bnodeContext1(bnode, doc));
|
|
243
|
+
// outgoing links
|
|
244
|
+
sts = this.store.statementsMatching(x, undefined, undefined, source);
|
|
245
|
+
for (let i = 0; i < sts.length; i++) {
|
|
246
|
+
if (this.ifps[sts[i].predicate.value]) {
|
|
247
|
+
y = sts[i].object;
|
|
248
|
+
if (!y.isBlank) {
|
|
249
|
+
return [sts[i]];
|
|
250
|
+
}
|
|
251
|
+
if (depth) {
|
|
252
|
+
res = this.bnodeContext2(y, source, depth - 1);
|
|
253
|
+
if (res) {
|
|
254
|
+
return res.concat([sts[i]]);
|
|
255
|
+
}
|
|
395
256
|
}
|
|
396
257
|
}
|
|
397
|
-
|
|
398
|
-
return context;
|
|
399
258
|
}
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
* @private
|
|
403
|
-
*/
|
|
404
|
-
|
|
405
|
-
}, {
|
|
406
|
-
key: "statementContext",
|
|
407
|
-
value: function statementContext(st) {
|
|
408
|
-
var bnodes = this.statementBnodes(st);
|
|
409
|
-
return this.bnodeContext(bnodes, st.graph);
|
|
410
|
-
}
|
|
411
|
-
/**
|
|
412
|
-
* @private
|
|
413
|
-
*/
|
|
259
|
+
return null; // Failure
|
|
260
|
+
}
|
|
414
261
|
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
262
|
+
/**
|
|
263
|
+
* Returns the smallest context to bind a given single bnode
|
|
264
|
+
* @private
|
|
265
|
+
*/
|
|
266
|
+
bnodeContext1(x, source) {
|
|
267
|
+
// Return a list of statements which indirectly identify a node
|
|
268
|
+
// Breadth-first
|
|
269
|
+
for (var depth = 0; depth < 3; depth++) {
|
|
270
|
+
// Try simple first
|
|
271
|
+
var con = this.bnodeContext2(x, source, depth);
|
|
272
|
+
if (con !== null) return con;
|
|
422
273
|
}
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
}, {
|
|
428
|
-
key: "fire",
|
|
429
|
-
value: function fire(uri, query, callbackFunction) {
|
|
430
|
-
var _this = this;
|
|
431
|
-
|
|
432
|
-
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
433
|
-
return Promise.resolve().then(function () {
|
|
434
|
-
if (!uri) {
|
|
435
|
-
throw new Error('No URI given for remote editing operation: ' + query);
|
|
436
|
-
} // console.log('UpdateManager: sending update to <' + uri + '>')
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
options.noMeta = true;
|
|
440
|
-
options.contentType = 'application/sparql-update';
|
|
441
|
-
options.body = query;
|
|
442
|
-
return _this.store.fetcher.webOperation('PATCH', uri, options);
|
|
443
|
-
}).then(function (response) {
|
|
444
|
-
if (!response.ok) {
|
|
445
|
-
var _message = 'UpdateManager: update failed for <' + uri + '> status=' + response.status + ', ' + response.statusText + '\n for query: ' + query; // console.log(message)
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
throw new Error(_message);
|
|
449
|
-
} // console.log('UpdateManager: update Ok for <' + uri + '>')
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
callbackFunction(uri, response.ok, response.responseText, response);
|
|
453
|
-
}).catch(function (err) {
|
|
454
|
-
callbackFunction(uri, false, err.message, err);
|
|
455
|
-
});
|
|
456
|
-
} // ARE THESE THEE FUNCTIONS USED? DEPROCATE?
|
|
274
|
+
// If we can't guarantee unique with logic just send all info about node
|
|
275
|
+
return this.store.connectedStatements(x, source); // was:
|
|
276
|
+
// throw new Error('Unable to uniquely identify bnode: ' + x.toNT())
|
|
277
|
+
}
|
|
457
278
|
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
279
|
+
/**
|
|
280
|
+
* @private
|
|
281
|
+
*/
|
|
282
|
+
mentioned(x) {
|
|
283
|
+
return this.store.statementsMatching(x, null, null, null).length !== 0 ||
|
|
284
|
+
// Don't pin fresh bnodes
|
|
285
|
+
this.store.statementsMatching(null, x).length !== 0 || this.store.statementsMatching(null, null, x).length !== 0;
|
|
286
|
+
}
|
|
464
287
|
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
288
|
+
/**
|
|
289
|
+
* @private
|
|
290
|
+
*/
|
|
291
|
+
bnodeContext(bnodes, doc) {
|
|
292
|
+
var context = [];
|
|
293
|
+
if (bnodes.length) {
|
|
294
|
+
this.cacheIfps();
|
|
295
|
+
for (let i = 0; i < bnodes.length; i++) {
|
|
296
|
+
// Does this occur in old graph?
|
|
297
|
+
var bnode = bnodes[i];
|
|
298
|
+
if (!this.mentioned(bnode)) continue;
|
|
299
|
+
context = context.concat(this.bnodeContext1(bnode, doc));
|
|
470
300
|
}
|
|
471
|
-
|
|
472
|
-
var updater = this;
|
|
473
|
-
var context = this.statementContext(statement);
|
|
474
|
-
return {
|
|
475
|
-
statement: statement ? [statement.subject, statement.predicate, statement.object, statement.graph] : undefined,
|
|
476
|
-
statementNT: statement ? this.anonymizeNT(statement) : undefined,
|
|
477
|
-
where: updater.contextWhere(context),
|
|
478
|
-
set_object: function set_object(obj, callbackFunction) {
|
|
479
|
-
var query = this.where;
|
|
480
|
-
query += 'DELETE DATA { ' + this.statementNT + ' } ;\n';
|
|
481
|
-
query += 'INSERT DATA { ' + // @ts-ignore `this` might refer to the wrong scope. Does this work?
|
|
482
|
-
this.anonymize(this.statement[0]) + ' ' + // @ts-ignore
|
|
483
|
-
this.anonymize(this.statement[1]) + ' ' + // @ts-ignore
|
|
484
|
-
this.anonymize(obj) + ' ' + ' . }\n';
|
|
485
|
-
updater.fire(this.statement[3].value, query, callbackFunction);
|
|
486
|
-
}
|
|
487
|
-
};
|
|
488
301
|
}
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
value: function insert_statement(st, callbackFunction) {
|
|
492
|
-
var st0 = st instanceof Array ? st[0] : st;
|
|
493
|
-
var query = this.contextWhere(this.statementContext(st0));
|
|
302
|
+
return context;
|
|
303
|
+
}
|
|
494
304
|
|
|
495
|
-
|
|
496
|
-
|
|
305
|
+
/**
|
|
306
|
+
* Returns the best context for a single statement
|
|
307
|
+
* @private
|
|
308
|
+
*/
|
|
309
|
+
statementContext(st) {
|
|
310
|
+
var bnodes = this.statementBnodes(st);
|
|
311
|
+
return this.bnodeContext(bnodes, st.graph);
|
|
312
|
+
}
|
|
497
313
|
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
314
|
+
/**
|
|
315
|
+
* @private
|
|
316
|
+
*/
|
|
317
|
+
contextWhere(context) {
|
|
318
|
+
var updater = this;
|
|
319
|
+
return !context || context.length === 0 ? '' : 'WHERE { ' + context.map(function (x) {
|
|
320
|
+
return updater.anonymizeNT(x);
|
|
321
|
+
}).join('\n') + ' }\n';
|
|
322
|
+
}
|
|
501
323
|
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
324
|
+
/**
|
|
325
|
+
* @private
|
|
326
|
+
*/
|
|
327
|
+
fire(uri, query, callbackFunction) {
|
|
328
|
+
let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
329
|
+
return Promise.resolve().then(() => {
|
|
330
|
+
if (!uri) {
|
|
331
|
+
throw new Error('No URI given for remote editing operation: ' + query);
|
|
505
332
|
}
|
|
333
|
+
// console.log('UpdateManager: sending update to <' + uri + '>')
|
|
506
334
|
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
var stText = '';
|
|
517
|
-
|
|
518
|
-
for (var i = 0; i < st.length; i++) {
|
|
519
|
-
stText += st[i] + '\n';
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
query += 'DELETE DATA { ' + stText + ' }\n';
|
|
523
|
-
} else {
|
|
524
|
-
query += 'DELETE DATA { ' + this.anonymize(st.subject) + ' ' + this.anonymize(st.predicate) + ' ' + this.anonymize(st.object) + ' ' + ' . }\n';
|
|
335
|
+
options.noMeta = true;
|
|
336
|
+
options.contentType = 'application/sparql-update';
|
|
337
|
+
options.body = query;
|
|
338
|
+
return this.store.fetcher.webOperation('PATCH', uri, options);
|
|
339
|
+
}).then(response => {
|
|
340
|
+
if (!response.ok) {
|
|
341
|
+
let message = 'UpdateManager: update failed for <' + uri + '> status=' + response.status + ', ' + response.statusText + '\n for query: ' + query;
|
|
342
|
+
// console.log(message)
|
|
343
|
+
throw new Error(message);
|
|
525
344
|
}
|
|
526
345
|
|
|
527
|
-
|
|
528
|
-
} /// //////////////////////
|
|
529
|
-
|
|
530
|
-
/**
|
|
531
|
-
* Requests a now or future action to refresh changes coming downstream
|
|
532
|
-
* This is designed to allow the system to re-request the server version,
|
|
533
|
-
* when a websocket has pinged to say there are changes.
|
|
534
|
-
* If the websocket, by contrast, has sent a patch, then this may not be necessary.
|
|
535
|
-
*
|
|
536
|
-
* @param doc
|
|
537
|
-
* @param action
|
|
538
|
-
*/
|
|
346
|
+
// console.log('UpdateManager: update Ok for <' + uri + '>')
|
|
539
347
|
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
348
|
+
callbackFunction(uri, response.ok, response.responseText, response);
|
|
349
|
+
}).catch(err => {
|
|
350
|
+
callbackFunction(uri, false, err.message, err);
|
|
351
|
+
});
|
|
352
|
+
}
|
|
544
353
|
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
}
|
|
354
|
+
// ARE THESE THEE FUNCTIONS USED? DEPROCATE?
|
|
355
|
+
|
|
356
|
+
/** return a statemnet updating function
|
|
357
|
+
*
|
|
358
|
+
* This does NOT update the statement.
|
|
359
|
+
* It returns an object which includes
|
|
360
|
+
* function which can be used to change the object of the statement.
|
|
361
|
+
*/
|
|
362
|
+
update_statement(statement) {
|
|
363
|
+
if (statement && !statement.graph) {
|
|
364
|
+
return;
|
|
557
365
|
}
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
366
|
+
var updater = this;
|
|
367
|
+
var context = this.statementContext(statement);
|
|
368
|
+
return {
|
|
369
|
+
statement: statement ? [statement.subject, statement.predicate, statement.object, statement.graph] : undefined,
|
|
370
|
+
statementNT: statement ? this.anonymizeNT(statement) : undefined,
|
|
371
|
+
where: updater.contextWhere(context),
|
|
372
|
+
set_object: function (obj, callbackFunction) {
|
|
373
|
+
var query = this.where;
|
|
374
|
+
query += 'DELETE DATA { ' + this.statementNT + ' } ;\n';
|
|
375
|
+
query += 'INSERT DATA { ' +
|
|
376
|
+
// @ts-ignore `this` might refer to the wrong scope. Does this work?
|
|
377
|
+
this.anonymize(this.statement[0]) + ' ' +
|
|
378
|
+
// @ts-ignore
|
|
379
|
+
this.anonymize(this.statement[1]) + ' ' +
|
|
380
|
+
// @ts-ignore
|
|
381
|
+
this.anonymize(obj) + ' ' + ' . }\n';
|
|
382
|
+
updater.fire(this.statement[3].value, query, callbackFunction);
|
|
383
|
+
}
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
insert_statement(st, callbackFunction) {
|
|
387
|
+
var st0 = st instanceof Array ? st[0] : st;
|
|
388
|
+
var query = this.contextWhere(this.statementContext(st0));
|
|
389
|
+
if (st instanceof Array) {
|
|
390
|
+
var stText = '';
|
|
391
|
+
for (let i = 0; i < st.length; i++) stText += st[i] + '\n';
|
|
392
|
+
query += 'INSERT DATA { ' + stText + ' }\n';
|
|
393
|
+
} else {
|
|
394
|
+
query += 'INSERT DATA { ' + this.anonymize(st.subject) + ' ' + this.anonymize(st.predicate) + ' ' + this.anonymize(st.object) + ' ' + ' . }\n';
|
|
568
395
|
}
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
396
|
+
this.fire(st0.graph.value, query, callbackFunction);
|
|
397
|
+
}
|
|
398
|
+
delete_statement(st, callbackFunction) {
|
|
399
|
+
var st0 = st instanceof Array ? st[0] : st;
|
|
400
|
+
var query = this.contextWhere(this.statementContext(st0));
|
|
401
|
+
if (st instanceof Array) {
|
|
402
|
+
var stText = '';
|
|
403
|
+
for (let i = 0; i < st.length; i++) stText += st[i] + '\n';
|
|
404
|
+
query += 'DELETE DATA { ' + stText + ' }\n';
|
|
405
|
+
} else {
|
|
406
|
+
query += 'DELETE DATA { ' + this.anonymize(st.subject) + ' ' + this.anonymize(st.predicate) + ' ' + this.anonymize(st.object) + ' ' + ' . }\n';
|
|
575
407
|
}
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
value: function addDownstreamChangeListener(doc, listener) {
|
|
579
|
-
var _this2 = this;
|
|
408
|
+
this.fire(st0.graph.value, query, callbackFunction);
|
|
409
|
+
}
|
|
580
410
|
|
|
581
|
-
|
|
411
|
+
/// //////////////////////
|
|
582
412
|
|
|
583
|
-
|
|
584
|
-
|
|
413
|
+
/**
|
|
414
|
+
* Requests a now or future action to refresh changes coming downstream
|
|
415
|
+
* This is designed to allow the system to re-request the server version,
|
|
416
|
+
* when a websocket has pinged to say there are changes.
|
|
417
|
+
* If the websocket, by contrast, has sent a patch, then this may not be necessary.
|
|
418
|
+
*
|
|
419
|
+
* @param doc
|
|
420
|
+
* @param action
|
|
421
|
+
*/
|
|
422
|
+
requestDownstreamAction(doc, action) {
|
|
423
|
+
var control = this.patchControlFor(doc);
|
|
424
|
+
if (!control.pendingUpstream) {
|
|
425
|
+
action(doc);
|
|
426
|
+
} else {
|
|
427
|
+
if (control.downstreamAction) {
|
|
428
|
+
if ('' + control.downstreamAction !== '' + action) {
|
|
429
|
+
// Kludge compare
|
|
430
|
+
throw new Error("Can't wait for > 1 different downstream actions");
|
|
431
|
+
}
|
|
432
|
+
} else {
|
|
433
|
+
control.downstreamAction = action;
|
|
585
434
|
}
|
|
586
|
-
|
|
587
|
-
control.downstreamChangeListeners.push(listener);
|
|
588
|
-
this.setRefreshHandler(doc, function (doc) {
|
|
589
|
-
_this2.reloadAndSync(doc);
|
|
590
|
-
});
|
|
591
435
|
}
|
|
592
|
-
}
|
|
593
|
-
key: "reloadAndSync",
|
|
594
|
-
value: function reloadAndSync(doc) {
|
|
595
|
-
var control = this.patchControlFor(doc);
|
|
596
|
-
var updater = this;
|
|
597
|
-
|
|
598
|
-
if (control.reloading) {
|
|
599
|
-
// console.log(' Already reloading - note this load may be out of date')
|
|
600
|
-
control.outOfDate = true;
|
|
601
|
-
return; // once only needed @@ Not true, has changed again
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
control.reloading = true;
|
|
605
|
-
var retryTimeout = 1000; // ms
|
|
606
|
-
|
|
607
|
-
var tryReload = function tryReload() {
|
|
608
|
-
// console.log('try reload - timeout = ' + retryTimeout)
|
|
609
|
-
updater.reload(updater.store, doc, function (ok, message, response) {
|
|
610
|
-
if (ok) {
|
|
611
|
-
if (control.downstreamChangeListeners) {
|
|
612
|
-
for (var i = 0; i < control.downstreamChangeListeners.length; i++) {
|
|
613
|
-
// console.log(' Calling downstream listener ' + i)
|
|
614
|
-
control.downstreamChangeListeners[i]();
|
|
615
|
-
}
|
|
616
|
-
}
|
|
436
|
+
}
|
|
617
437
|
|
|
618
|
-
|
|
438
|
+
/**
|
|
439
|
+
* We want to start counting websocket notifications
|
|
440
|
+
* to distinguish the ones from others from our own.
|
|
441
|
+
*/
|
|
442
|
+
clearUpstreamCount(doc) {
|
|
443
|
+
var control = this.patchControlFor(doc);
|
|
444
|
+
control.upstreamCount = 0;
|
|
445
|
+
}
|
|
446
|
+
getUpdatesVia(doc) {
|
|
447
|
+
var linkHeaders = this.store.fetcher.getHeader(doc, 'updates-via');
|
|
448
|
+
if (!linkHeaders || !linkHeaders.length) return null;
|
|
449
|
+
return linkHeaders[0].trim();
|
|
450
|
+
}
|
|
451
|
+
addDownstreamChangeListener(doc, listener) {
|
|
452
|
+
var control = this.patchControlFor(doc);
|
|
453
|
+
if (!control.downstreamChangeListeners) {
|
|
454
|
+
control.downstreamChangeListeners = [];
|
|
455
|
+
}
|
|
456
|
+
control.downstreamChangeListeners.push(listener);
|
|
457
|
+
this.setRefreshHandler(doc, doc => {
|
|
458
|
+
this.reloadAndSync(doc);
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
reloadAndSync(doc) {
|
|
462
|
+
var control = this.patchControlFor(doc);
|
|
463
|
+
var updater = this;
|
|
464
|
+
if (control.reloading) {
|
|
465
|
+
// console.log(' Already reloading - note this load may be out of date')
|
|
466
|
+
control.outOfDate = true;
|
|
467
|
+
return; // once only needed @@ Not true, has changed again
|
|
468
|
+
}
|
|
619
469
|
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
470
|
+
control.reloading = true;
|
|
471
|
+
var retryTimeout = 1000; // ms
|
|
472
|
+
var tryReload = function () {
|
|
473
|
+
// console.log('try reload - timeout = ' + retryTimeout)
|
|
474
|
+
updater.reload(updater.store, doc, function (ok, message, response) {
|
|
475
|
+
if (ok) {
|
|
476
|
+
if (control.downstreamChangeListeners) {
|
|
477
|
+
for (let i = 0; i < control.downstreamChangeListeners.length; i++) {
|
|
478
|
+
// console.log(' Calling downstream listener ' + i)
|
|
479
|
+
control.downstreamChangeListeners[i]();
|
|
624
480
|
}
|
|
481
|
+
}
|
|
482
|
+
control.reloading = false;
|
|
483
|
+
if (control.outOfDate) {
|
|
484
|
+
// console.log(' Extra reload because of extra update.')
|
|
485
|
+
control.outOfDate = false;
|
|
486
|
+
tryReload();
|
|
487
|
+
}
|
|
488
|
+
} else {
|
|
489
|
+
control.reloading = false;
|
|
490
|
+
if (response.status === 0) {
|
|
491
|
+
// console.log('Network error refreshing the data. Retrying in ' +
|
|
492
|
+
// retryTimeout / 1000)
|
|
493
|
+
control.reloading = true;
|
|
494
|
+
retryTimeout = retryTimeout * 2;
|
|
495
|
+
setTimeout(tryReload, retryTimeout);
|
|
625
496
|
} else {
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
if (response.status === 0) {
|
|
629
|
-
// console.log('Network error refreshing the data. Retrying in ' +
|
|
630
|
-
// retryTimeout / 1000)
|
|
631
|
-
control.reloading = true;
|
|
632
|
-
retryTimeout = retryTimeout * 2;
|
|
633
|
-
setTimeout(tryReload, retryTimeout);
|
|
634
|
-
} else {// console.log('Error ' + (response as Response).status + 'refreshing the data:' +
|
|
635
|
-
// message + '. Stopped' + doc)
|
|
636
|
-
}
|
|
497
|
+
// console.log('Error ' + (response as Response).status + 'refreshing the data:' +
|
|
498
|
+
// message + '. Stopped' + doc)
|
|
637
499
|
}
|
|
638
|
-
}
|
|
639
|
-
};
|
|
500
|
+
}
|
|
501
|
+
});
|
|
502
|
+
};
|
|
503
|
+
tryReload();
|
|
504
|
+
}
|
|
640
505
|
|
|
641
|
-
|
|
506
|
+
/**
|
|
507
|
+
* Sets up websocket to listen on
|
|
508
|
+
*
|
|
509
|
+
* There is coordination between upstream changes and downstream ones
|
|
510
|
+
* so that a reload is not done in the middle of an upstream patch.
|
|
511
|
+
* If you use this API then you get called when a change happens, and you
|
|
512
|
+
* have to reload the file yourself, and then refresh the UI.
|
|
513
|
+
* Alternative is addDownstreamChangeListener(), where you do not
|
|
514
|
+
* have to do the reload yourself. Do mot mix them.
|
|
515
|
+
*
|
|
516
|
+
* kb contains the HTTP metadata from previous operations
|
|
517
|
+
*
|
|
518
|
+
* @param doc
|
|
519
|
+
* @param handler
|
|
520
|
+
*
|
|
521
|
+
* @returns {boolean}
|
|
522
|
+
*/
|
|
523
|
+
setRefreshHandler(doc, handler) {
|
|
524
|
+
let wssURI = this.getUpdatesVia(doc); // relative
|
|
525
|
+
// var kb = this.store
|
|
526
|
+
var theHandler = handler;
|
|
527
|
+
var self = this;
|
|
528
|
+
var updater = this;
|
|
529
|
+
var retryTimeout = 1500; // *2 will be 3 Seconds, 6, 12, etc
|
|
530
|
+
var retries = 0;
|
|
531
|
+
if (!wssURI) {
|
|
532
|
+
// console.log('Server does not support live updates through Updates-Via :-(')
|
|
533
|
+
return false;
|
|
642
534
|
}
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
*
|
|
658
|
-
* @returns {boolean}
|
|
659
|
-
*/
|
|
660
|
-
|
|
661
|
-
}, {
|
|
662
|
-
key: "setRefreshHandler",
|
|
663
|
-
value: function setRefreshHandler(doc, handler) {
|
|
664
|
-
var wssURI = this.getUpdatesVia(doc); // relative
|
|
665
|
-
// var kb = this.store
|
|
666
|
-
|
|
667
|
-
var theHandler = handler;
|
|
668
|
-
var self = this;
|
|
669
|
-
var updater = this;
|
|
670
|
-
var retryTimeout = 1500; // *2 will be 3 Seconds, 6, 12, etc
|
|
671
|
-
|
|
672
|
-
var retries = 0;
|
|
673
|
-
|
|
674
|
-
if (!wssURI) {
|
|
675
|
-
// console.log('Server does not support live updates through Updates-Via :-(')
|
|
676
|
-
return false;
|
|
535
|
+
wssURI = uriJoin(wssURI, doc.value);
|
|
536
|
+
const validWssURI = wssURI.replace(/^http:/, 'ws:').replace(/^https:/, 'wss:');
|
|
537
|
+
// console.log('Web socket URI ' + wssURI)
|
|
538
|
+
|
|
539
|
+
var openWebsocket = function () {
|
|
540
|
+
// From https://github.com/solid/solid-spec#live-updates
|
|
541
|
+
var socket;
|
|
542
|
+
if (typeof WebSocket !== 'undefined') {
|
|
543
|
+
socket = new WebSocket(validWssURI);
|
|
544
|
+
} else if (typeof window !== 'undefined' && window.WebSocket) {
|
|
545
|
+
socket = window.WebSocket(validWssURI);
|
|
546
|
+
} else {
|
|
547
|
+
// console.log('Live update disabled, as WebSocket not supported by platform :-(')
|
|
548
|
+
return;
|
|
677
549
|
}
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
if (typeof WebSocket !== 'undefined') {
|
|
687
|
-
socket = new WebSocket(validWssURI);
|
|
688
|
-
} else if (typeof window !== 'undefined' && window.WebSocket) {
|
|
689
|
-
socket = window.WebSocket(validWssURI);
|
|
690
|
-
} else {
|
|
691
|
-
// console.log('Live update disabled, as WebSocket not supported by platform :-(')
|
|
692
|
-
return;
|
|
550
|
+
socket.onopen = function () {
|
|
551
|
+
// console.log(' websocket open')
|
|
552
|
+
retryTimeout = 1500; // reset timeout to fast on success
|
|
553
|
+
this.send('sub ' + doc.value);
|
|
554
|
+
if (retries) {
|
|
555
|
+
// console.log('Web socket has been down, better check for any news.')
|
|
556
|
+
updater.requestDownstreamAction(doc, theHandler);
|
|
693
557
|
}
|
|
558
|
+
};
|
|
559
|
+
var control = self.patchControlFor(doc);
|
|
560
|
+
control.upstreamCount = 0;
|
|
561
|
+
socket.onerror = function onerror(err) {
|
|
562
|
+
// console.log('Error on Websocket:', err)
|
|
563
|
+
};
|
|
694
564
|
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
//
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
// console.log('*** Websocket closed with code ' + event.code +
|
|
728
|
-
// ", reason '" + event.reason + "' clean = " + event.wasClean)
|
|
729
|
-
retryTimeout *= 2;
|
|
730
|
-
retries += 1; // console.log('Retrying in ' + retryTimeout + 'ms') // (ask user?)
|
|
731
|
-
|
|
732
|
-
setTimeout(function () {
|
|
733
|
-
// console.log('Trying websocket again')
|
|
734
|
-
openWebsocket();
|
|
735
|
-
}, retryTimeout);
|
|
736
|
-
};
|
|
737
|
-
|
|
738
|
-
socket.onmessage = function (msg) {
|
|
739
|
-
if (msg.data && msg.data.slice(0, 3) === 'pub') {
|
|
740
|
-
if ('upstreamCount' in control) {
|
|
741
|
-
control.upstreamCount -= 1;
|
|
742
|
-
|
|
743
|
-
if (control.upstreamCount >= 0) {
|
|
744
|
-
// console.log('just an echo: ' + control.upstreamCount)
|
|
745
|
-
return; // Just an echo
|
|
746
|
-
}
|
|
747
|
-
} // console.log('Assume a real downstream change: ' + control.upstreamCount + ' -> 0')
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
control.upstreamCount = 0;
|
|
751
|
-
self.requestDownstreamAction(doc, theHandler);
|
|
565
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
|
|
566
|
+
//
|
|
567
|
+
// 1000 CLOSE_NORMAL Normal closure; the connection successfully completed whatever purpose for which it was created.
|
|
568
|
+
// 1001 CLOSE_GOING_AWAY The endpoint is going away, either
|
|
569
|
+
// because of a server failure or because the browser is navigating away from the page that opened the connection.
|
|
570
|
+
// 1002 CLOSE_PROTOCOL_ERROR The endpoint is terminating the connection due to a protocol error.
|
|
571
|
+
// 1003 CLOSE_UNSUPPORTED The connection is being terminated because the endpoint
|
|
572
|
+
// received data of a type it cannot accept (for example, a text-only endpoint received binary data).
|
|
573
|
+
// 1004 Reserved. A meaning might be defined in the future.
|
|
574
|
+
// 1005 CLOSE_NO_STATUS Reserved. Indicates that no status code was provided even though one was expected.
|
|
575
|
+
// 1006 CLOSE_ABNORMAL Reserved. Used to indicate that a connection was closed abnormally (
|
|
576
|
+
//
|
|
577
|
+
//
|
|
578
|
+
socket.onclose = function (event) {
|
|
579
|
+
// console.log('*** Websocket closed with code ' + event.code +
|
|
580
|
+
// ", reason '" + event.reason + "' clean = " + event.wasClean)
|
|
581
|
+
retryTimeout *= 2;
|
|
582
|
+
retries += 1;
|
|
583
|
+
// console.log('Retrying in ' + retryTimeout + 'ms') // (ask user?)
|
|
584
|
+
setTimeout(function () {
|
|
585
|
+
// console.log('Trying websocket again')
|
|
586
|
+
openWebsocket();
|
|
587
|
+
}, retryTimeout);
|
|
588
|
+
};
|
|
589
|
+
socket.onmessage = function (msg) {
|
|
590
|
+
if (msg.data && msg.data.slice(0, 3) === 'pub') {
|
|
591
|
+
if ('upstreamCount' in control) {
|
|
592
|
+
control.upstreamCount -= 1;
|
|
593
|
+
if (control.upstreamCount >= 0) {
|
|
594
|
+
// console.log('just an echo: ' + control.upstreamCount)
|
|
595
|
+
return; // Just an echo
|
|
596
|
+
}
|
|
752
597
|
}
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
598
|
+
// console.log('Assume a real downstream change: ' + control.upstreamCount + ' -> 0')
|
|
599
|
+
control.upstreamCount = 0;
|
|
600
|
+
self.requestDownstreamAction(doc, theHandler);
|
|
601
|
+
}
|
|
602
|
+
};
|
|
603
|
+
}; // openWebsocket
|
|
604
|
+
openWebsocket();
|
|
605
|
+
return true;
|
|
606
|
+
}
|
|
756
607
|
|
|
757
|
-
|
|
758
|
-
|
|
608
|
+
/**
|
|
609
|
+
* This high-level function updates the local store iff the web is changed successfully.
|
|
610
|
+
* Deletions, insertions may be undefined or single statements or lists or formulae (may contain bnodes which can be indirectly identified by a where clause).
|
|
611
|
+
* The `why` property of each statement must be the give the web document to be updated.
|
|
612
|
+
* The statements to be deleted and inserted may span more than one web document.
|
|
613
|
+
* @param deletions - Statement or statements to be deleted.
|
|
614
|
+
* @param insertions - Statement or statements to be inserted.
|
|
615
|
+
* @returns a promise
|
|
616
|
+
*/
|
|
617
|
+
updateMany(deletions) {
|
|
618
|
+
let insertions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
619
|
+
const docs = deletions.concat(insertions).map(st => st.why);
|
|
620
|
+
const thisUpdater = this;
|
|
621
|
+
const uniqueDocs = [];
|
|
622
|
+
docs.forEach(doc => {
|
|
623
|
+
if (!uniqueDocs.find(uniqueDoc => uniqueDoc.equals(doc))) uniqueDocs.push(doc);
|
|
624
|
+
});
|
|
625
|
+
const updates = uniqueDocs.map(doc => thisUpdater.update(deletions.filter(st => st.why.equals(doc)), insertions.filter(st => st.why.equals(doc))));
|
|
626
|
+
if (updates.length > 1) {
|
|
627
|
+
// console.log(`@@ updateMany to ${updates.length}: ${uniqueDocs}`)
|
|
759
628
|
}
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
* Deletions, insertions may be undefined or single statements or lists or formulae (may contain bnodes which can be indirectly identified by a where clause).
|
|
763
|
-
* The `why` property of each statement must be the give the web document to be updated.
|
|
764
|
-
* The statements to be deleted and inserted may span more than one web document.
|
|
765
|
-
* @param deletions - Statement or statements to be deleted.
|
|
766
|
-
* @param insertions - Statement or statements to be inserted.
|
|
767
|
-
* @returns a promise
|
|
768
|
-
*/
|
|
629
|
+
return Promise.all(updates);
|
|
630
|
+
}
|
|
769
631
|
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
632
|
+
/**
|
|
633
|
+
* This high-level function updates the local store iff the web is changed successfully.
|
|
634
|
+
* Deletions, insertions may be undefined or single statements or lists or formulae (may contain bnodes which can be indirectly identified by a where clause).
|
|
635
|
+
* The `why` property of each statement must be the same and give the web document to be updated.
|
|
636
|
+
* @param deletions - Statement or statements to be deleted.
|
|
637
|
+
* @param insertions - Statement or statements to be inserted.
|
|
638
|
+
* @param callback - called as callbackFunction(uri, success, errorbody)
|
|
639
|
+
* OR returns a promise
|
|
640
|
+
* @param options - Options for the fetch call
|
|
641
|
+
*/
|
|
642
|
+
update(deletions, insertions, callback, secondTry) {
|
|
643
|
+
let options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
|
|
644
|
+
if (!callback) {
|
|
777
645
|
var thisUpdater = this;
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
}));
|
|
790
|
-
});
|
|
646
|
+
return new Promise(function (resolve, reject) {
|
|
647
|
+
// Promise version
|
|
648
|
+
thisUpdater.update(deletions, insertions, function (uri, ok, errorBody) {
|
|
649
|
+
if (!ok) {
|
|
650
|
+
reject(new Error(errorBody));
|
|
651
|
+
} else {
|
|
652
|
+
resolve();
|
|
653
|
+
}
|
|
654
|
+
}, secondTry, options); // callbackFunction
|
|
655
|
+
}); // promise
|
|
656
|
+
} // if
|
|
791
657
|
|
|
792
|
-
|
|
658
|
+
try {
|
|
659
|
+
var kb = this.store;
|
|
660
|
+
var ds = !deletions ? [] : isStore(deletions) ? deletions.statements : deletions instanceof Array ? deletions : [deletions];
|
|
661
|
+
var is = !insertions ? [] : isStore(insertions) ? insertions.statements : insertions instanceof Array ? insertions : [insertions];
|
|
662
|
+
if (!(ds instanceof Array)) {
|
|
663
|
+
throw new Error('Type Error ' + typeof ds + ': ' + ds);
|
|
664
|
+
}
|
|
665
|
+
if (!(is instanceof Array)) {
|
|
666
|
+
throw new Error('Type Error ' + typeof is + ': ' + is);
|
|
667
|
+
}
|
|
668
|
+
if (ds.length === 0 && is.length === 0) {
|
|
669
|
+
return callback(null, true); // success -- nothing needed to be done.
|
|
793
670
|
}
|
|
794
671
|
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
if (
|
|
821
|
-
|
|
822
|
-
} else {
|
|
823
|
-
resolve();
|
|
824
|
-
}
|
|
825
|
-
}, secondTry, options); // callbackFunction
|
|
826
|
-
}); // promise
|
|
827
|
-
} // if
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
try {
|
|
831
|
-
var kb = this.store;
|
|
832
|
-
var ds = !deletions ? [] : isStore(deletions) ? deletions.statements : deletions instanceof Array ? deletions : [deletions];
|
|
833
|
-
var is = !insertions ? [] : isStore(insertions) ? insertions.statements : insertions instanceof Array ? insertions : [insertions];
|
|
834
|
-
|
|
835
|
-
if (!(ds instanceof Array)) {
|
|
836
|
-
throw new Error('Type Error ' + _typeof(ds) + ': ' + ds);
|
|
837
|
-
}
|
|
838
|
-
|
|
839
|
-
if (!(is instanceof Array)) {
|
|
840
|
-
throw new Error('Type Error ' + _typeof(is) + ': ' + is);
|
|
841
|
-
}
|
|
842
|
-
|
|
843
|
-
if (ds.length === 0 && is.length === 0) {
|
|
844
|
-
return callback(null, true); // success -- nothing needed to be done.
|
|
845
|
-
}
|
|
846
|
-
|
|
847
|
-
var doc = ds.length ? ds[0].graph : is[0].graph;
|
|
848
|
-
|
|
849
|
-
if (!doc) {
|
|
850
|
-
var _message2 = 'Error patching: statement does not specify which document to patch:' + ds[0] + ', ' + is[0]; // console.log(message)
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
throw new Error(_message2);
|
|
854
|
-
}
|
|
855
|
-
|
|
856
|
-
var control = this.patchControlFor(doc);
|
|
857
|
-
var startTime = Date.now();
|
|
858
|
-
var props = ['subject', 'predicate', 'object', 'why'];
|
|
859
|
-
var verbs = ['insert', 'delete'];
|
|
860
|
-
var clauses = {
|
|
861
|
-
'delete': ds,
|
|
862
|
-
'insert': is
|
|
863
|
-
};
|
|
864
|
-
verbs.map(function (verb) {
|
|
865
|
-
clauses[verb].map(function (st) {
|
|
866
|
-
if (!doc.equals(st.graph)) {
|
|
867
|
-
throw new Error('update: destination ' + doc + ' inconsistent with delete quad ' + st.graph);
|
|
672
|
+
var doc = ds.length ? ds[0].graph : is[0].graph;
|
|
673
|
+
if (!doc) {
|
|
674
|
+
let message = 'Error patching: statement does not specify which document to patch:' + ds[0] + ', ' + is[0];
|
|
675
|
+
// console.log(message)
|
|
676
|
+
throw new Error(message);
|
|
677
|
+
}
|
|
678
|
+
if (doc.termType !== 'NamedNode') {
|
|
679
|
+
let message = 'Error patching: document not a NamedNode:' + ds[0] + ', ' + is[0];
|
|
680
|
+
// console.log(message)
|
|
681
|
+
throw new Error(message);
|
|
682
|
+
}
|
|
683
|
+
var control = this.patchControlFor(doc);
|
|
684
|
+
var startTime = Date.now();
|
|
685
|
+
var props = ['subject', 'predicate', 'object', 'why'];
|
|
686
|
+
var verbs = ['insert', 'delete'];
|
|
687
|
+
var clauses = {
|
|
688
|
+
'delete': ds,
|
|
689
|
+
'insert': is
|
|
690
|
+
};
|
|
691
|
+
verbs.map(function (verb) {
|
|
692
|
+
clauses[verb].map(function (st) {
|
|
693
|
+
if (!doc.equals(st.graph)) {
|
|
694
|
+
throw new Error('update: destination ' + doc + ' inconsistent with delete quad ' + st.graph);
|
|
695
|
+
}
|
|
696
|
+
props.map(function (prop) {
|
|
697
|
+
if (typeof st[prop] === 'undefined') {
|
|
698
|
+
throw new Error('update: undefined ' + prop + ' of statement.');
|
|
868
699
|
}
|
|
869
|
-
|
|
870
|
-
props.map(function (prop) {
|
|
871
|
-
if (typeof st[prop] === 'undefined') {
|
|
872
|
-
throw new Error('update: undefined ' + prop + ' of statement.');
|
|
873
|
-
}
|
|
874
|
-
});
|
|
875
700
|
});
|
|
876
701
|
});
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
702
|
+
});
|
|
703
|
+
var protocol = this.editable(doc.value, kb);
|
|
704
|
+
if (protocol === false) {
|
|
705
|
+
throw new Error('Update: Can\'t make changes in uneditable ' + doc);
|
|
706
|
+
}
|
|
707
|
+
if (protocol === undefined) {
|
|
708
|
+
// Not enough metadata
|
|
709
|
+
if (secondTry) {
|
|
710
|
+
throw new Error('Update: Loaded ' + doc + "but stil can't figure out what editing protcol it supports.");
|
|
881
711
|
}
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
this.store.fetcher.load(doc).then(function (response) {
|
|
891
|
-
_this3.update(deletions, insertions, callback, true, options);
|
|
892
|
-
}, function (err) {
|
|
893
|
-
if (err.response.status === 404) {
|
|
894
|
-
// nonexistent files are fine
|
|
895
|
-
_this3.update(deletions, insertions, callback, true, options);
|
|
896
|
-
} else {
|
|
897
|
-
throw new Error("Update: Can't get updatability status ".concat(doc, " before patching: ").concat(err));
|
|
898
|
-
}
|
|
899
|
-
});
|
|
900
|
-
return;
|
|
901
|
-
} else if (protocol.indexOf('SPARQL') >= 0) {
|
|
902
|
-
var bnodes = [];
|
|
903
|
-
if (ds.length) bnodes = this.statementArrayBnodes(ds);
|
|
904
|
-
if (is.length) bnodes = bnodes.concat(this.statementArrayBnodes(is));
|
|
905
|
-
var context = this.bnodeContext(bnodes, doc);
|
|
906
|
-
var whereClause = this.contextWhere(context);
|
|
907
|
-
var query = '';
|
|
908
|
-
|
|
909
|
-
if (whereClause.length) {
|
|
910
|
-
// Is there a WHERE clause?
|
|
911
|
-
if (ds.length) {
|
|
912
|
-
query += 'DELETE { ';
|
|
913
|
-
|
|
914
|
-
for (var i = 0; i < ds.length; i++) {
|
|
915
|
-
query += this.anonymizeNT(ds[i]) + '\n';
|
|
916
|
-
}
|
|
917
|
-
|
|
918
|
-
query += ' }\n';
|
|
919
|
-
}
|
|
920
|
-
|
|
921
|
-
if (is.length) {
|
|
922
|
-
query += 'INSERT { ';
|
|
923
|
-
|
|
924
|
-
for (var _i5 = 0; _i5 < is.length; _i5++) {
|
|
925
|
-
query += this.anonymizeNT(is[_i5]) + '\n';
|
|
926
|
-
}
|
|
927
|
-
|
|
928
|
-
query += ' }\n';
|
|
929
|
-
}
|
|
930
|
-
|
|
931
|
-
query += whereClause;
|
|
712
|
+
// console.log(`Update: have not loaded ${doc} before: loading now...`);
|
|
713
|
+
this.store.fetcher.load(doc).then(response => {
|
|
714
|
+
this.update(deletions, insertions, callback, true, options);
|
|
715
|
+
}, err => {
|
|
716
|
+
if (err.response.status === 404) {
|
|
717
|
+
// nonexistent files are fine
|
|
718
|
+
this.update(deletions, insertions, callback, true, options);
|
|
932
719
|
} else {
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
720
|
+
throw new Error(`Update: Can't get updatability status ${doc} before patching: ${err}`);
|
|
721
|
+
}
|
|
722
|
+
});
|
|
723
|
+
return;
|
|
724
|
+
} else if (protocol.indexOf('SPARQL') >= 0) {
|
|
725
|
+
var bnodes = [];
|
|
726
|
+
// change ReadOnly type to Mutable type
|
|
727
|
+
|
|
728
|
+
if (ds.length) bnodes = this.statementArrayBnodes(ds);
|
|
729
|
+
if (is.length) bnodes = bnodes.concat(this.statementArrayBnodes(is));
|
|
730
|
+
var context = this.bnodeContext(bnodes, doc);
|
|
731
|
+
var whereClause = this.contextWhere(context);
|
|
732
|
+
var query = '';
|
|
733
|
+
if (whereClause.length) {
|
|
734
|
+
// Is there a WHERE clause?
|
|
735
|
+
if (ds.length) {
|
|
736
|
+
query += 'DELETE { ';
|
|
737
|
+
for (let i = 0; i < ds.length; i++) {
|
|
738
|
+
query += this.anonymizeNT(ds[i]) + '\n';
|
|
942
739
|
}
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
query += this.nTriples(is[_i7]) + '\n';
|
|
950
|
-
}
|
|
951
|
-
|
|
952
|
-
query += ' }\n';
|
|
740
|
+
query += ' }\n';
|
|
741
|
+
}
|
|
742
|
+
if (is.length) {
|
|
743
|
+
query += 'INSERT { ';
|
|
744
|
+
for (let i = 0; i < is.length; i++) {
|
|
745
|
+
query += this.anonymizeNT(is[i]) + '\n';
|
|
953
746
|
}
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
control.pendingUpstream = control.pendingUpstream ? control.pendingUpstream + 1 : 1;
|
|
958
|
-
|
|
959
|
-
if ('upstreamCount' in control) {
|
|
960
|
-
control.upstreamCount += 1; // count changes we originated ourselves
|
|
961
|
-
// console.log('upstream count up to : ' + control.upstreamCount)
|
|
747
|
+
query += ' }\n';
|
|
962
748
|
}
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
if (success) {
|
|
972
|
-
try {
|
|
973
|
-
kb.remove(ds);
|
|
974
|
-
} catch (e) {
|
|
975
|
-
success = false;
|
|
976
|
-
body = 'Remote Ok BUT error deleting ' + ds.length + ' from store!!! ' + e;
|
|
977
|
-
} // Add in any case -- help recover from weirdness??
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
for (var _i8 = 0; _i8 < is.length; _i8++) {
|
|
981
|
-
kb.add(is[_i8].subject, is[_i8].predicate, is[_i8].object, doc);
|
|
982
|
-
}
|
|
749
|
+
query += whereClause;
|
|
750
|
+
} else {
|
|
751
|
+
// no where clause
|
|
752
|
+
if (ds.length) {
|
|
753
|
+
query += 'DELETE DATA { ';
|
|
754
|
+
for (let i = 0; i < ds.length; i++) {
|
|
755
|
+
query += this.anonymizeNT(ds[i]) + '\n';
|
|
983
756
|
}
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
downstreamAction(doc);
|
|
757
|
+
query += ' } \n';
|
|
758
|
+
}
|
|
759
|
+
if (is.length) {
|
|
760
|
+
if (ds.length) query += ' ; ';
|
|
761
|
+
query += 'INSERT DATA { ';
|
|
762
|
+
for (let i = 0; i < is.length; i++) {
|
|
763
|
+
query += this.nTriples(is[i]) + '\n';
|
|
993
764
|
}
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
765
|
+
query += ' }\n';
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
// Track pending upstream patches until they have finished their callbackFunction
|
|
769
|
+
control.pendingUpstream = control.pendingUpstream ? control.pendingUpstream + 1 : 1;
|
|
770
|
+
if ('upstreamCount' in control) {
|
|
771
|
+
control.upstreamCount += 1; // count changes we originated ourselves
|
|
772
|
+
// console.log('upstream count up to : ' + control.upstreamCount)
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
this.fire(doc.value, query, (uri, success, body, response) => {
|
|
776
|
+
response.elapsedTimeMs = Date.now() - startTime;
|
|
777
|
+
/* console.log(' UpdateManager: Return ' +
|
|
778
|
+
(success ? 'success ' : 'FAILURE ') + (response as Response).status +
|
|
779
|
+
' elapsed ' + (response as any).elapsedTimeMs + 'ms')
|
|
780
|
+
*/
|
|
781
|
+
if (success) {
|
|
999
782
|
try {
|
|
1000
|
-
|
|
783
|
+
kb.remove(ds);
|
|
1001
784
|
} catch (e) {
|
|
1002
|
-
|
|
1003
|
-
|
|
785
|
+
success = false;
|
|
786
|
+
body = 'Remote Ok BUT error deleting ' + ds.length + ' from store!!! ' + e;
|
|
787
|
+
} // Add in any case -- help recover from weirdness??
|
|
788
|
+
for (let i = 0; i < is.length; i++) {
|
|
789
|
+
kb.add(is[i].subject, is[i].predicate, is[i].object, doc);
|
|
1004
790
|
}
|
|
1005
|
-
} else {
|
|
1006
|
-
throw new Error("Unhandled edit method: '" + protocol + "' for " + doc);
|
|
1007
791
|
}
|
|
792
|
+
callback(uri, success, body, response);
|
|
793
|
+
control.pendingUpstream -= 1;
|
|
794
|
+
// When upstream patches have been sent, reload state if downstream waiting
|
|
795
|
+
if (control.pendingUpstream === 0 && control.downstreamAction) {
|
|
796
|
+
var downstreamAction = control.downstreamAction;
|
|
797
|
+
delete control.downstreamAction;
|
|
798
|
+
// console.log('delayed downstream action:')
|
|
799
|
+
downstreamAction(doc);
|
|
800
|
+
}
|
|
801
|
+
}, options);
|
|
802
|
+
} else if (protocol.indexOf('DAV') >= 0) {
|
|
803
|
+
this.updateDav(doc, ds, is, callback, options);
|
|
804
|
+
} else {
|
|
805
|
+
if (protocol.indexOf('LOCALFILE') >= 0) {
|
|
806
|
+
try {
|
|
807
|
+
this.updateLocalFile(doc, ds, is, callback, options);
|
|
808
|
+
} catch (e) {
|
|
809
|
+
callback(doc.value, false, 'Exception trying to write back file <' + doc.value + '>\n'
|
|
810
|
+
// + tabulator.Util.stackString(e))
|
|
811
|
+
);
|
|
812
|
+
}
|
|
813
|
+
} else {
|
|
814
|
+
throw new Error("Unhandled edit method: '" + protocol + "' for " + doc);
|
|
1008
815
|
}
|
|
1009
|
-
} catch (e) {
|
|
1010
|
-
callback(undefined, false, 'Exception in update: ' + e + '\n' + Util.stackString(e));
|
|
1011
816
|
}
|
|
817
|
+
} catch (e) {
|
|
818
|
+
callback(undefined, false, 'Exception in update: ' + e + '\n' + Util.stackString(e));
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
updateDav(doc, ds, is, callbackFunction) {
|
|
822
|
+
let options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
|
|
823
|
+
let kb = this.store;
|
|
824
|
+
// The code below is derived from Kenny's UpdateCenter.js
|
|
825
|
+
var request = kb.any(doc, this.ns.link('request'));
|
|
826
|
+
if (!request) {
|
|
827
|
+
throw new Error('No record of our HTTP GET request for document: ' + doc);
|
|
828
|
+
} // should not happen
|
|
829
|
+
var response = kb.any(request, this.ns.link('response'));
|
|
830
|
+
if (!response) {
|
|
831
|
+
return null; // throw "No record HTTP GET response for document: "+doc
|
|
1012
832
|
}
|
|
1013
|
-
}, {
|
|
1014
|
-
key: "updateDav",
|
|
1015
|
-
value: function updateDav(doc, ds, is, callbackFunction) {
|
|
1016
|
-
var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
|
|
1017
|
-
var kb = this.store; // The code below is derived from Kenny's UpdateCenter.js
|
|
1018
|
-
|
|
1019
|
-
var request = kb.any(doc, this.ns.link('request'));
|
|
1020
|
-
|
|
1021
|
-
if (!request) {
|
|
1022
|
-
throw new Error('No record of our HTTP GET request for document: ' + doc);
|
|
1023
|
-
} // should not happen
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
var response = kb.any(request, this.ns.link('response'));
|
|
1027
|
-
|
|
1028
|
-
if (!response) {
|
|
1029
|
-
return null; // throw "No record HTTP GET response for document: "+doc
|
|
1030
|
-
}
|
|
1031
833
|
|
|
1032
|
-
|
|
834
|
+
var contentType = kb.the(response, this.ns.httph('content-type')).value;
|
|
1033
835
|
|
|
1034
|
-
|
|
836
|
+
// prepare contents of revised document
|
|
837
|
+
let newSts = kb.statementsMatching(undefined, undefined, undefined, doc).slice(); // copy!
|
|
838
|
+
for (let i = 0; i < ds.length; i++) {
|
|
839
|
+
Util.RDFArrayRemove(newSts, ds[i]);
|
|
840
|
+
}
|
|
841
|
+
for (let i = 0; i < is.length; i++) {
|
|
842
|
+
newSts.push(is[i]);
|
|
843
|
+
}
|
|
844
|
+
const documentString = this.serialize(doc.value, newSts, contentType);
|
|
1035
845
|
|
|
1036
|
-
|
|
1037
|
-
|
|
846
|
+
// Write the new version back
|
|
847
|
+
var candidateTarget = kb.the(response, this.ns.httph('content-location'));
|
|
848
|
+
var targetURI;
|
|
849
|
+
if (candidateTarget) {
|
|
850
|
+
targetURI = uriJoin(candidateTarget.value, targetURI);
|
|
851
|
+
}
|
|
852
|
+
options.contentType = contentType;
|
|
853
|
+
options.noMeta = true;
|
|
854
|
+
options.body = documentString;
|
|
855
|
+
return kb.fetcher.webOperation('PUT', targetURI, options).then(response => {
|
|
856
|
+
if (!response.ok) {
|
|
857
|
+
throw new Error(response.error);
|
|
1038
858
|
}
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
newSts.push(is[_i9]);
|
|
859
|
+
for (let i = 0; i < ds.length; i++) {
|
|
860
|
+
kb.remove(ds[i]);
|
|
1042
861
|
}
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
var candidateTarget = kb.the(response, this.ns.httph('content-location'));
|
|
1047
|
-
var targetURI;
|
|
1048
|
-
|
|
1049
|
-
if (candidateTarget) {
|
|
1050
|
-
targetURI = uriJoin(candidateTarget.value, targetURI);
|
|
862
|
+
for (let i = 0; i < is.length; i++) {
|
|
863
|
+
kb.add(is[i].subject, is[i].predicate, is[i].object, doc);
|
|
1051
864
|
}
|
|
865
|
+
callbackFunction(doc.value, response.ok, response.responseText, response);
|
|
866
|
+
}).catch(err => {
|
|
867
|
+
callbackFunction(doc.value, false, err.message, err);
|
|
868
|
+
});
|
|
869
|
+
}
|
|
1052
870
|
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
});
|
|
871
|
+
/**
|
|
872
|
+
* Likely deprecated, since this lib no longer deals with browser extension
|
|
873
|
+
*
|
|
874
|
+
* @param doc
|
|
875
|
+
* @param ds
|
|
876
|
+
* @param is
|
|
877
|
+
* @param callbackFunction
|
|
878
|
+
* @param options
|
|
879
|
+
*/
|
|
880
|
+
updateLocalFile(doc, ds, is, callbackFunction) {
|
|
881
|
+
let options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
|
|
882
|
+
const kb = this.store;
|
|
883
|
+
// console.log('Writing back to local file\n')
|
|
884
|
+
|
|
885
|
+
// prepare contents of revised document
|
|
886
|
+
let newSts = kb.statementsMatching(undefined, undefined, undefined, doc).slice(); // copy!
|
|
887
|
+
|
|
888
|
+
for (let i = 0; i < ds.length; i++) {
|
|
889
|
+
Util.RDFArrayRemove(newSts, ds[i]);
|
|
1073
890
|
}
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
for (var i = 0; i < ds.length; i++) {
|
|
1094
|
-
Util.RDFArrayRemove(newSts, ds[i]);
|
|
1095
|
-
}
|
|
1096
|
-
|
|
1097
|
-
for (var _i12 = 0; _i12 < is.length; _i12++) {
|
|
1098
|
-
newSts.push(is[_i12]);
|
|
1099
|
-
} // serialize to the appropriate format
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
var dot = doc.value.lastIndexOf('.');
|
|
1103
|
-
|
|
1104
|
-
if (dot < 1) {
|
|
1105
|
-
throw new Error('Rewriting file: No filename extension: ' + doc.value);
|
|
891
|
+
for (let i = 0; i < is.length; i++) {
|
|
892
|
+
newSts.push(is[i]);
|
|
893
|
+
}
|
|
894
|
+
// serialize to the appropriate format
|
|
895
|
+
var dot = doc.value.lastIndexOf('.');
|
|
896
|
+
if (dot < 1) {
|
|
897
|
+
throw new Error('Rewriting file: No filename extension: ' + doc.value);
|
|
898
|
+
}
|
|
899
|
+
var ext = doc.value.slice(dot + 1);
|
|
900
|
+
let contentType = Fetcher.CONTENT_TYPE_BY_EXT[ext];
|
|
901
|
+
if (!contentType) {
|
|
902
|
+
throw new Error('File extension .' + ext + ' not supported for data write');
|
|
903
|
+
}
|
|
904
|
+
options.body = this.serialize(doc.value, newSts, contentType);
|
|
905
|
+
options.contentType = contentType;
|
|
906
|
+
kb.fetcher.webOperation('PUT', doc.value, options).then(response => {
|
|
907
|
+
if (!response.ok) return callbackFunction(doc.value, false, response.error);
|
|
908
|
+
for (let i = 0; i < ds.length; i++) {
|
|
909
|
+
kb.remove(ds[i]);
|
|
1106
910
|
}
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
var contentType = Fetcher.CONTENT_TYPE_BY_EXT[ext];
|
|
1110
|
-
|
|
1111
|
-
if (!contentType) {
|
|
1112
|
-
throw new Error('File extension .' + ext + ' not supported for data write');
|
|
911
|
+
for (let i = 0; i < is.length; i++) {
|
|
912
|
+
kb.add(is[i].subject, is[i].predicate, is[i].object, doc);
|
|
1113
913
|
}
|
|
914
|
+
callbackFunction(doc.value, true, ''); // success!
|
|
915
|
+
});
|
|
916
|
+
}
|
|
1114
917
|
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
kb.add(is[_i14].subject, is[_i14].predicate, is[_i14].object, doc);
|
|
1126
|
-
}
|
|
1127
|
-
|
|
1128
|
-
callbackFunction(doc.value, true, ''); // success!
|
|
1129
|
-
});
|
|
918
|
+
/**
|
|
919
|
+
* @throws {Error} On unsupported content type
|
|
920
|
+
*
|
|
921
|
+
* @returns {string}
|
|
922
|
+
*/
|
|
923
|
+
serialize(uri, data, contentType) {
|
|
924
|
+
const kb = this.store;
|
|
925
|
+
let documentString;
|
|
926
|
+
if (typeof data === 'string') {
|
|
927
|
+
return data;
|
|
1130
928
|
}
|
|
1131
|
-
/**
|
|
1132
|
-
* @throws {Error} On unsupported content type
|
|
1133
|
-
*
|
|
1134
|
-
* @returns {string}
|
|
1135
|
-
*/
|
|
1136
|
-
|
|
1137
|
-
}, {
|
|
1138
|
-
key: "serialize",
|
|
1139
|
-
value: function serialize(uri, data, contentType) {
|
|
1140
|
-
var kb = this.store;
|
|
1141
|
-
var documentString;
|
|
1142
|
-
|
|
1143
|
-
if (typeof data === 'string') {
|
|
1144
|
-
return data;
|
|
1145
|
-
} // serialize to the appropriate format
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
var sz = Serializer(kb);
|
|
1149
|
-
sz.suggestNamespaces(kb.namespaces);
|
|
1150
|
-
sz.setBase(uri);
|
|
1151
929
|
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
return documentString;
|
|
930
|
+
// serialize to the appropriate format
|
|
931
|
+
var sz = Serializer(kb);
|
|
932
|
+
sz.suggestNamespaces(kb.namespaces);
|
|
933
|
+
sz.setBase(uri);
|
|
934
|
+
switch (contentType) {
|
|
935
|
+
case 'text/xml':
|
|
936
|
+
case 'application/rdf+xml':
|
|
937
|
+
documentString = sz.statementsToXML(data);
|
|
938
|
+
break;
|
|
939
|
+
case 'text/n3':
|
|
940
|
+
case 'text/turtle':
|
|
941
|
+
case 'application/x-turtle': // Legacy
|
|
942
|
+
case 'application/n3':
|
|
943
|
+
// Legacy
|
|
944
|
+
documentString = sz.statementsToN3(data);
|
|
945
|
+
break;
|
|
946
|
+
default:
|
|
947
|
+
throw new Error('Content-type ' + contentType + ' not supported for data serialization');
|
|
1172
948
|
}
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
*/
|
|
1176
|
-
|
|
1177
|
-
}, {
|
|
1178
|
-
key: "put",
|
|
1179
|
-
value: function put(doc, data, contentType, callback) {
|
|
1180
|
-
var _this4 = this;
|
|
1181
|
-
|
|
1182
|
-
var kb = this.store;
|
|
1183
|
-
var documentString;
|
|
1184
|
-
return Promise.resolve().then(function () {
|
|
1185
|
-
documentString = _this4.serialize(doc.value, data, contentType);
|
|
1186
|
-
return kb.fetcher.webOperation('PUT', doc.value, {
|
|
1187
|
-
contentType: contentType,
|
|
1188
|
-
body: documentString
|
|
1189
|
-
});
|
|
1190
|
-
}).then(function (response) {
|
|
1191
|
-
if (!response.ok) {
|
|
1192
|
-
return callback(doc.value, response.ok, response.error, response);
|
|
1193
|
-
}
|
|
1194
|
-
|
|
1195
|
-
delete kb.fetcher.nonexistent[doc.value];
|
|
1196
|
-
delete kb.fetcher.requested[doc.value]; // @@ could this mess with the requested state machine? if a fetch is in progress
|
|
1197
|
-
|
|
1198
|
-
if (typeof data !== 'string') {
|
|
1199
|
-
data.map(function (st) {
|
|
1200
|
-
kb.addStatement(st);
|
|
1201
|
-
});
|
|
1202
|
-
}
|
|
949
|
+
return documentString;
|
|
950
|
+
}
|
|
1203
951
|
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
952
|
+
/**
|
|
953
|
+
* This is suitable for an initial creation of a document.
|
|
954
|
+
*/
|
|
955
|
+
put(doc, data, contentType, callback) {
|
|
956
|
+
const kb = this.store;
|
|
957
|
+
let documentString;
|
|
958
|
+
return Promise.resolve().then(() => {
|
|
959
|
+
documentString = this.serialize(doc.value, data, contentType);
|
|
960
|
+
return kb.fetcher.webOperation('PUT', doc.value, {
|
|
961
|
+
contentType,
|
|
962
|
+
body: documentString
|
|
1207
963
|
});
|
|
1208
|
-
}
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
* Don't delete the statements before the load, or it will leave a broken
|
|
1215
|
-
* document in the meantime.
|
|
1216
|
-
*
|
|
1217
|
-
* @param kb
|
|
1218
|
-
* @param doc {RDFlibNamedNode}
|
|
1219
|
-
* @param callbackFunction
|
|
1220
|
-
*/
|
|
1221
|
-
|
|
1222
|
-
}, {
|
|
1223
|
-
key: "reload",
|
|
1224
|
-
value: function reload(kb, doc, callbackFunction) {
|
|
1225
|
-
var startTime = Date.now(); // force sets no-cache and
|
|
964
|
+
}).then(response => {
|
|
965
|
+
if (!response.ok) {
|
|
966
|
+
return callback(doc.value, response.ok, response.error, response);
|
|
967
|
+
}
|
|
968
|
+
delete kb.fetcher.nonexistent[doc.value];
|
|
969
|
+
delete kb.fetcher.requested[doc.value]; // @@ could this mess with the requested state machine? if a fetch is in progress
|
|
1226
970
|
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
}
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
// console.log(' Non-HTTP error reloading data! onErrorWasCalled=' +
|
|
1238
|
-
//@ts-ignore Where does onErrorWasCalled come from?
|
|
1239
|
-
// response.onErrorWasCalled + ' status: ' + response.status)
|
|
1240
|
-
callbackFunction(false, 'Non-HTTP error reloading data: ' + body, response);
|
|
1241
|
-
} else {
|
|
1242
|
-
var elapsedTimeMs = Date.now() - startTime;
|
|
1243
|
-
if (!doc.reloadTimeTotal) doc.reloadTimeTotal = 0;
|
|
1244
|
-
if (!doc.reloadTimeCount) doc.reloadTimeCount = 0;
|
|
1245
|
-
doc.reloadTimeTotal += elapsedTimeMs;
|
|
1246
|
-
doc.reloadTimeCount += 1; // console.log(' Fetch took ' + elapsedTimeMs + 'ms, av. of ' +
|
|
1247
|
-
// doc.reloadTimeCount + ' = ' +
|
|
1248
|
-
// (doc.reloadTimeTotal / doc.reloadTimeCount) + 'ms.')
|
|
971
|
+
if (typeof data !== 'string') {
|
|
972
|
+
data.map(st => {
|
|
973
|
+
kb.addStatement(st);
|
|
974
|
+
});
|
|
975
|
+
}
|
|
976
|
+
callback(doc.value, response.ok, '', response);
|
|
977
|
+
}).catch(err => {
|
|
978
|
+
callback(doc.value, false, err.message);
|
|
979
|
+
});
|
|
980
|
+
}
|
|
1249
981
|
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
982
|
+
/**
|
|
983
|
+
* Reloads a document.
|
|
984
|
+
*
|
|
985
|
+
* Fast and cheap, no metadata. Measure times for the document.
|
|
986
|
+
* Load it provisionally.
|
|
987
|
+
* Don't delete the statements before the load, or it will leave a broken
|
|
988
|
+
* document in the meantime.
|
|
989
|
+
*
|
|
990
|
+
* @param kb
|
|
991
|
+
* @param doc {RDFlibNamedNode}
|
|
992
|
+
* @param callbackFunction
|
|
993
|
+
*/
|
|
994
|
+
reload(kb, doc, callbackFunction) {
|
|
995
|
+
var startTime = Date.now();
|
|
996
|
+
// force sets no-cache and
|
|
997
|
+
const options = {
|
|
998
|
+
force: true,
|
|
999
|
+
noMeta: true,
|
|
1000
|
+
clearPreviousData: true
|
|
1001
|
+
};
|
|
1002
|
+
kb.fetcher.nowOrWhenFetched(doc.value, options, function (ok, body, response) {
|
|
1003
|
+
if (!ok) {
|
|
1004
|
+
// console.log(' ERROR reloading data: ' + body)
|
|
1005
|
+
callbackFunction(false, 'Error reloading data: ' + body, response);
|
|
1006
|
+
//@ts-ignore Where does onErrorWasCalled come from?
|
|
1007
|
+
} else if (response.onErrorWasCalled || response.status !== 200) {
|
|
1008
|
+
// console.log(' Non-HTTP error reloading data! onErrorWasCalled=' +
|
|
1009
|
+
//@ts-ignore Where does onErrorWasCalled come from?
|
|
1010
|
+
// response.onErrorWasCalled + ' status: ' + response.status)
|
|
1011
|
+
callbackFunction(false, 'Non-HTTP error reloading data: ' + body, response);
|
|
1012
|
+
} else {
|
|
1013
|
+
var elapsedTimeMs = Date.now() - startTime;
|
|
1014
|
+
if (!doc.reloadTimeTotal) doc.reloadTimeTotal = 0;
|
|
1015
|
+
if (!doc.reloadTimeCount) doc.reloadTimeCount = 0;
|
|
1016
|
+
doc.reloadTimeTotal += elapsedTimeMs;
|
|
1017
|
+
doc.reloadTimeCount += 1;
|
|
1255
1018
|
|
|
1256
|
-
|
|
1257
|
-
|
|
1019
|
+
// console.log(' Fetch took ' + elapsedTimeMs + 'ms, av. of ' +
|
|
1020
|
+
// doc.reloadTimeCount + ' = ' +
|
|
1021
|
+
// (doc.reloadTimeTotal / doc.reloadTimeCount) + 'ms.')
|
|
1258
1022
|
|
|
1259
|
-
|
|
1023
|
+
callbackFunction(true);
|
|
1024
|
+
}
|
|
1025
|
+
});
|
|
1026
|
+
}
|
|
1027
|
+
}
|