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