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