rdflib 2.2.32 → 2.2.33

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.
@@ -26,9 +26,9 @@ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol
26
26
  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); }
27
27
  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; } /* @file Update Manager Class
28
28
  **
29
- ** 2007-07-15 originall sparl update module by Joe Presbrey <presbrey@mit.edu>
29
+ ** 2007-07-15 original SPARQL Update module by Joe Presbrey <presbrey@mit.edu>
30
30
  ** 2010-08-08 TimBL folded in Kenny's WEBDAV
31
- ** 2010-12-07 TimBL addred local file write code
31
+ ** 2010-12-07 TimBL added local file write code
32
32
  */
33
33
  /**
34
34
  * The UpdateManager is a helper object for a store.
@@ -86,8 +86,8 @@ var UpdateManager = /*#__PURE__*/function () {
86
86
  }
87
87
 
88
88
  /** Remove from the store HTTP authorization metadata
89
- * The editble function below relies on copies we have in the store
90
- * of the results of previous HTTP transactions. Howver, when
89
+ * The editable function below relies on copies we have in the store
90
+ * of the results of previous HTTP transactions. However, when
91
91
  * the user logs in, then that data misrepresents what would happen
92
92
  * if the user tried again.
93
93
  */
@@ -128,7 +128,7 @@ var UpdateManager = /*#__PURE__*/function () {
128
128
  * and local write access is determined by those headers.
129
129
  * This async version not only looks at past HTTP requests, it also makes new ones if necessary.
130
130
  *
131
- * @returns The method string SPARQL or DAV or
131
+ * @returns The method string N3PATCH or SPARQL or DAV or
132
132
  * LOCALFILE or false if known, undefined if not known.
133
133
  */
134
134
  }, {
@@ -240,6 +240,7 @@ var UpdateManager = /*#__PURE__*/function () {
240
240
  if (acceptPatch.length) {
241
241
  for (var i = 0; i < acceptPatch.length; i++) {
242
242
  method = acceptPatch[i].value.trim();
243
+ if (method.indexOf('text/n3') >= 0) return 'N3PATCH';
243
244
  if (method.indexOf('application/sparql-update') >= 0) return 'SPARQL';
244
245
  if (method.indexOf('application/sparql-update-single-match') >= 0) return 'SPARQL';
245
246
  }
@@ -287,7 +288,8 @@ var UpdateManager = /*#__PURE__*/function () {
287
288
  }, {
288
289
  key: "anonymize",
289
290
  value: function anonymize(obj) {
290
- return obj.toNT().substr(0, 2) === '_:' && this.mentioned(obj) ? '?' + obj.toNT().substr(2) : obj.toNT();
291
+ var anonymized = obj.toNT().substr(0, 2) === '_:' && this.mentioned(obj) ? '?' + obj.toNT().substr(2) : obj.toNT();
292
+ return anonymized;
291
293
  }
292
294
  }, {
293
295
  key: "anonymizeNT",
@@ -485,7 +487,7 @@ var UpdateManager = /*#__PURE__*/function () {
485
487
  // console.log('UpdateManager: sending update to <' + uri + '>')
486
488
 
487
489
  options.noMeta = true;
488
- options.contentType = 'application/sparql-update';
490
+ options.contentType = options.contentType || 'application/sparql-update';
489
491
  options.body = query;
490
492
  return _this.store.fetcher.webOperation('PATCH', uri, options);
491
493
  }).then(function (response) {
@@ -503,7 +505,7 @@ var UpdateManager = /*#__PURE__*/function () {
503
505
  });
504
506
  }
505
507
 
506
- // ARE THESE THEE FUNCTIONS USED? DEPROCATE?
508
+ // ARE THESE THREE FUNCTIONS USED? DEPRECATE?
507
509
 
508
510
  /** return a statemnet updating function
509
511
  *
@@ -813,7 +815,93 @@ var UpdateManager = /*#__PURE__*/function () {
813
815
  }
814
816
 
815
817
  /**
816
- * This high-level function updates the local store iff the web is changed successfully.
818
+ * @private
819
+ *
820
+ * This helper function constructs SPARQL Update query from resolved arguments.
821
+ *
822
+ * @param ds: deletions array.
823
+ * @param is: insertions array.
824
+ * @param bnodes_context: Additional context to uniquely identify any blank nodes.
825
+ */
826
+ }, {
827
+ key: "constructSparqlUpdateQuery",
828
+ value: function constructSparqlUpdateQuery(ds, is, bnodes_context) {
829
+ var whereClause = this.contextWhere(bnodes_context);
830
+ var query = '';
831
+ if (whereClause.length) {
832
+ // Is there a WHERE clause?
833
+ if (ds.length) {
834
+ query += 'DELETE { ';
835
+ for (var i = 0; i < ds.length; i++) {
836
+ query += this.anonymizeNT(ds[i]) + '\n';
837
+ }
838
+ query += ' }\n';
839
+ }
840
+ if (is.length) {
841
+ query += 'INSERT { ';
842
+ for (var _i5 = 0; _i5 < is.length; _i5++) {
843
+ query += this.anonymizeNT(is[_i5]) + '\n';
844
+ }
845
+ query += ' }\n';
846
+ }
847
+ query += whereClause;
848
+ } else {
849
+ // no where clause
850
+ if (ds.length) {
851
+ query += 'DELETE DATA { ';
852
+ for (var _i6 = 0; _i6 < ds.length; _i6++) {
853
+ query += this.anonymizeNT(ds[_i6]) + '\n';
854
+ }
855
+ query += ' } \n';
856
+ }
857
+ if (is.length) {
858
+ if (ds.length) query += ' ; ';
859
+ query += 'INSERT DATA { ';
860
+ for (var _i7 = 0; _i7 < is.length; _i7++) {
861
+ query += this.nTriples(is[_i7]) + '\n';
862
+ }
863
+ query += ' }\n';
864
+ }
865
+ }
866
+ return query;
867
+ }
868
+
869
+ /**
870
+ * @private
871
+ *
872
+ * This helper function constructs n3-patch query from resolved arguments.
873
+ *
874
+ * @param ds: deletions array.
875
+ * @param is: insertions array.
876
+ * @param bnodes_context: Additional context to uniquely identify any blanknodes.
877
+ */
878
+ }, {
879
+ key: "constructN3PatchQuery",
880
+ value: function constructN3PatchQuery(ds, is, bnodes_context) {
881
+ var _this3 = this;
882
+ var query = "\n@prefix solid: <http://www.w3.org/ns/solid/terms#>.\n@prefix ex: <http://www.example.org/terms#>.\n\n_:patch\n";
883
+ // If bnode context is non trivial, express it as ?conditions formula.
884
+ if (bnodes_context && bnodes_context.length > 0) {
885
+ query += "\n solid:where {\n ".concat(bnodes_context.map(function (x) {
886
+ return _this3.anonymizeNT(x);
887
+ }).join('\n '), "\n };");
888
+ }
889
+ if (ds.length > 0) {
890
+ query += "\n solid:deletes {\n ".concat(ds.map(function (x) {
891
+ return _this3.anonymizeNT(x);
892
+ }).join('\n '), "\n };");
893
+ }
894
+ if (is.length > 0) {
895
+ query += "\n solid:inserts {\n ".concat(is.map(function (x) {
896
+ return _this3.anonymizeNT(x);
897
+ }).join('\n '), "\n };");
898
+ }
899
+ query += " a solid:InsertDeletePatch .\n";
900
+ return query;
901
+ }
902
+
903
+ /**
904
+ * This high-level function updates the local store if the web is changed successfully.
817
905
  * Deletions, insertions may be undefined or single statements or lists or formulae (may contain bnodes which can be indirectly identified by a where clause).
818
906
  * The `why` property of each statement must be the same and give the web document to be updated.
819
907
  * @param deletions - Statement or statements to be deleted.
@@ -825,7 +913,7 @@ var UpdateManager = /*#__PURE__*/function () {
825
913
  }, {
826
914
  key: "update",
827
915
  value: function update(deletions, insertions, callback, secondTry) {
828
- var _this3 = this;
916
+ var _this4 = this;
829
917
  var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
830
918
  if (!callback) {
831
919
  var thisUpdater = this;
@@ -893,64 +981,31 @@ var UpdateManager = /*#__PURE__*/function () {
893
981
  if (protocol === undefined) {
894
982
  // Not enough metadata
895
983
  if (secondTry) {
896
- throw new Error('Update: Loaded ' + doc + "but stil can't figure out what editing protcol it supports.");
984
+ throw new Error('Update: Loaded ' + doc + "but still can't figure out what editing protocol it supports.");
897
985
  }
898
986
  // console.log(`Update: have not loaded ${doc} before: loading now...`);
899
987
  this.store.fetcher.load(doc).then(function (response) {
900
- _this3.update(deletions, insertions, callback, true, options);
988
+ _this4.update(deletions, insertions, callback, true, options);
901
989
  }, function (err) {
902
990
  if (err.response.status === 404) {
903
991
  // nonexistent files are fine
904
- _this3.update(deletions, insertions, callback, true, options);
992
+ _this4.update(deletions, insertions, callback, true, options);
905
993
  } else {
906
994
  throw new Error("Update: Can't get updatability status ".concat(doc, " before patching: ").concat(err));
907
995
  }
908
996
  });
909
997
  return;
910
- } else if (protocol.indexOf('SPARQL') >= 0) {
998
+ } else if (protocol.indexOf('SPARQL') >= 0 || protocol.indexOf('N3PATCH') >= 0) {
999
+ var isSparql = protocol.indexOf('SPARQL') >= 0;
911
1000
  var bnodes = [];
912
1001
  // change ReadOnly type to Mutable type
913
1002
 
914
1003
  if (ds.length) bnodes = this.statementArrayBnodes(ds);
915
1004
  if (is.length) bnodes = bnodes.concat(this.statementArrayBnodes(is));
916
1005
  var context = this.bnodeContext(bnodes, doc);
917
- var whereClause = this.contextWhere(context);
918
- var query = '';
919
- if (whereClause.length) {
920
- // Is there a WHERE clause?
921
- if (ds.length) {
922
- query += 'DELETE { ';
923
- for (var i = 0; i < ds.length; i++) {
924
- query += this.anonymizeNT(ds[i]) + '\n';
925
- }
926
- query += ' }\n';
927
- }
928
- if (is.length) {
929
- query += 'INSERT { ';
930
- for (var _i5 = 0; _i5 < is.length; _i5++) {
931
- query += this.anonymizeNT(is[_i5]) + '\n';
932
- }
933
- query += ' }\n';
934
- }
935
- query += whereClause;
936
- } else {
937
- // no where clause
938
- if (ds.length) {
939
- query += 'DELETE DATA { ';
940
- for (var _i6 = 0; _i6 < ds.length; _i6++) {
941
- query += this.anonymizeNT(ds[_i6]) + '\n';
942
- }
943
- query += ' } \n';
944
- }
945
- if (is.length) {
946
- if (ds.length) query += ' ; ';
947
- query += 'INSERT DATA { ';
948
- for (var _i7 = 0; _i7 < is.length; _i7++) {
949
- query += this.nTriples(is[_i7]) + '\n';
950
- }
951
- query += ' }\n';
952
- }
953
- }
1006
+ var query = isSparql ? this.constructSparqlUpdateQuery(ds, is, context) : this.constructN3PatchQuery(ds, is, context);
1007
+ options.contentType = isSparql ? 'application/sparql-update' : 'text/n3';
1008
+
954
1009
  // Track pending upstream patches until they have finished their callbackFunction
955
1010
  control.pendingUpstream = control.pendingUpstream ? control.pendingUpstream + 1 : 1;
956
1011
  if ('upstreamCount' in control) {
@@ -971,8 +1026,8 @@ var UpdateManager = /*#__PURE__*/function () {
971
1026
  success = false;
972
1027
  body = 'Remote Ok BUT error deleting ' + ds.length + ' from store!!! ' + e;
973
1028
  } // Add in any case -- help recover from weirdness??
974
- for (var _i8 = 0; _i8 < is.length; _i8++) {
975
- kb.add(is[_i8].subject, is[_i8].predicate, is[_i8].object, doc);
1029
+ for (var i = 0; i < is.length; i++) {
1030
+ kb.add(is[i].subject, is[i].predicate, is[i].object, doc);
976
1031
  }
977
1032
  }
978
1033
  callback(uri, success, body, response);
@@ -1026,8 +1081,8 @@ var UpdateManager = /*#__PURE__*/function () {
1026
1081
  for (var i = 0; i < ds.length; i++) {
1027
1082
  Util.RDFArrayRemove(newSts, ds[i]);
1028
1083
  }
1029
- for (var _i9 = 0; _i9 < is.length; _i9++) {
1030
- newSts.push(is[_i9]);
1084
+ for (var _i8 = 0; _i8 < is.length; _i8++) {
1085
+ newSts.push(is[_i8]);
1031
1086
  }
1032
1087
  var documentString = this.serialize(doc.value, newSts, contentType);
1033
1088
 
@@ -1044,11 +1099,11 @@ var UpdateManager = /*#__PURE__*/function () {
1044
1099
  if (!response.ok) {
1045
1100
  throw new Error(response.error);
1046
1101
  }
1047
- for (var _i10 = 0; _i10 < ds.length; _i10++) {
1048
- kb.remove(ds[_i10]);
1102
+ for (var _i9 = 0; _i9 < ds.length; _i9++) {
1103
+ kb.remove(ds[_i9]);
1049
1104
  }
1050
- for (var _i11 = 0; _i11 < is.length; _i11++) {
1051
- kb.add(is[_i11].subject, is[_i11].predicate, is[_i11].object, doc);
1105
+ for (var _i10 = 0; _i10 < is.length; _i10++) {
1106
+ kb.add(is[_i10].subject, is[_i10].predicate, is[_i10].object, doc);
1052
1107
  }
1053
1108
  callbackFunction(doc.value, response.ok, response.responseText, response);
1054
1109
  }).catch(function (err) {
@@ -1078,8 +1133,8 @@ var UpdateManager = /*#__PURE__*/function () {
1078
1133
  for (var i = 0; i < ds.length; i++) {
1079
1134
  Util.RDFArrayRemove(newSts, ds[i]);
1080
1135
  }
1081
- for (var _i12 = 0; _i12 < is.length; _i12++) {
1082
- newSts.push(is[_i12]);
1136
+ for (var _i11 = 0; _i11 < is.length; _i11++) {
1137
+ newSts.push(is[_i11]);
1083
1138
  }
1084
1139
  // serialize to the appropriate format
1085
1140
  var dot = doc.value.lastIndexOf('.');
@@ -1095,11 +1150,11 @@ var UpdateManager = /*#__PURE__*/function () {
1095
1150
  options.contentType = contentType;
1096
1151
  kb.fetcher.webOperation('PUT', doc.value, options).then(function (response) {
1097
1152
  if (!response.ok) return callbackFunction(doc.value, false, response.error);
1098
- for (var _i13 = 0; _i13 < ds.length; _i13++) {
1099
- kb.remove(ds[_i13]);
1153
+ for (var _i12 = 0; _i12 < ds.length; _i12++) {
1154
+ kb.remove(ds[_i12]);
1100
1155
  }
1101
- for (var _i14 = 0; _i14 < is.length; _i14++) {
1102
- kb.add(is[_i14].subject, is[_i14].predicate, is[_i14].object, doc);
1156
+ for (var _i13 = 0; _i13 < is.length; _i13++) {
1157
+ kb.add(is[_i13].subject, is[_i13].predicate, is[_i13].object, doc);
1103
1158
  }
1104
1159
  callbackFunction(doc.value, true, ''); // success!
1105
1160
  });
@@ -1147,11 +1202,11 @@ var UpdateManager = /*#__PURE__*/function () {
1147
1202
  }, {
1148
1203
  key: "put",
1149
1204
  value: function put(doc, data, contentType, callback) {
1150
- var _this4 = this;
1205
+ var _this5 = this;
1151
1206
  var kb = this.store;
1152
1207
  var documentString;
1153
1208
  return Promise.resolve().then(function () {
1154
- documentString = _this4.serialize(doc.value, data, contentType);
1209
+ documentString = _this5.serialize(doc.value, data, contentType);
1155
1210
  return kb.fetcher.webOperation('PUT', doc.value, {
1156
1211
  contentType: contentType,
1157
1212
  body: documentString
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rdflib",
3
3
  "description": "an RDF library for node.js. Suitable for client and server side.",
4
- "version": "2.2.32",
4
+ "version": "2.2.33",
5
5
  "private": false,
6
6
  "browserslist": [
7
7
  "> 0.5%"
@@ -64,6 +64,7 @@
64
64
  "@types/dirty-chai": "^2.0.2",
65
65
  "@types/express": "^4.17.17",
66
66
  "@types/jsonld": "^1.5.8",
67
+ "@types/mocha": "^10.0.4",
67
68
  "@types/sinon-chai": "^3.2.9",
68
69
  "babel-loader": "^9.1.2",
69
70
  "chai": "^4.3.7",
package/src/serializer.js CHANGED
@@ -261,7 +261,7 @@ export class Serializer {
261
261
  } else if (this.flags.indexOf('u') >= 0) { // Unicode encoding NTriples style
262
262
  uri = backslashUify(uri)
263
263
  } else {
264
- uri = hexify(uri)
264
+ uri = hexify(decodeURI(uri))
265
265
  }
266
266
  return '<' + uri + '>'
267
267
  }