langchain 0.0.154 → 0.0.155
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/callbacks/base.d.ts +42 -28
- package/dist/callbacks/handlers/log_stream.cjs +283 -0
- package/dist/callbacks/handlers/log_stream.d.ts +99 -0
- package/dist/callbacks/handlers/log_stream.js +277 -0
- package/dist/callbacks/handlers/tracer.cjs +34 -18
- package/dist/callbacks/handlers/tracer.d.ts +18 -16
- package/dist/callbacks/handlers/tracer.js +34 -18
- package/dist/document_loaders/web/notionapi.cjs +8 -4
- package/dist/document_loaders/web/notionapi.js +8 -4
- package/dist/document_loaders/web/searchapi.cjs +134 -0
- package/dist/document_loaders/web/searchapi.d.ts +65 -0
- package/dist/document_loaders/web/searchapi.js +130 -0
- package/dist/load/import_constants.cjs +1 -0
- package/dist/load/import_constants.js +1 -0
- package/dist/load/import_map.cjs +3 -2
- package/dist/load/import_map.d.ts +1 -0
- package/dist/load/import_map.js +1 -0
- package/dist/schema/runnable/base.cjs +64 -5
- package/dist/schema/runnable/base.d.ts +13 -0
- package/dist/schema/runnable/base.js +64 -5
- package/dist/tools/index.cjs +3 -1
- package/dist/tools/index.d.ts +1 -0
- package/dist/tools/index.js +1 -0
- package/dist/tools/searchapi.cjs +139 -0
- package/dist/tools/searchapi.d.ts +64 -0
- package/dist/tools/searchapi.js +135 -0
- package/dist/util/fast-json-patch/index.cjs +48 -0
- package/dist/util/fast-json-patch/index.d.ts +21 -0
- package/dist/util/fast-json-patch/index.js +15 -0
- package/dist/util/fast-json-patch/src/core.cjs +469 -0
- package/dist/util/fast-json-patch/src/core.d.ts +111 -0
- package/dist/util/fast-json-patch/src/core.js +459 -0
- package/dist/util/fast-json-patch/src/helpers.cjs +194 -0
- package/dist/util/fast-json-patch/src/helpers.d.ts +36 -0
- package/dist/util/fast-json-patch/src/helpers.js +181 -0
- package/dist/util/googlevertexai-webauth.cjs +6 -2
- package/dist/util/googlevertexai-webauth.d.ts +1 -0
- package/dist/util/googlevertexai-webauth.js +6 -2
- package/dist/util/stream.cjs +2 -40
- package/dist/util/stream.d.ts +1 -2
- package/dist/util/stream.js +1 -38
- package/dist/vectorstores/pgvector.cjs +1 -1
- package/dist/vectorstores/pgvector.js +1 -1
- package/dist/vectorstores/vercel_postgres.cjs +300 -0
- package/dist/vectorstores/vercel_postgres.d.ts +145 -0
- package/dist/vectorstores/vercel_postgres.js +296 -0
- package/document_loaders/web/searchapi.cjs +1 -0
- package/document_loaders/web/searchapi.d.ts +1 -0
- package/document_loaders/web/searchapi.js +1 -0
- package/package.json +22 -1
- package/vectorstores/vercel_postgres.cjs +1 -0
- package/vectorstores/vercel_postgres.d.ts +1 -0
- package/vectorstores/vercel_postgres.js +1 -0
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
import { PatchError, _deepClone, isInteger, unescapePathComponent, hasUndefined, } from "./helpers.js";
|
|
3
|
+
export const JsonPatchError = PatchError;
|
|
4
|
+
export const deepClone = _deepClone;
|
|
5
|
+
/* We use a Javascript hash to store each
|
|
6
|
+
function. Each hash entry (property) uses
|
|
7
|
+
the operation identifiers specified in rfc6902.
|
|
8
|
+
In this way, we can map each patch operation
|
|
9
|
+
to its dedicated function in efficient way.
|
|
10
|
+
*/
|
|
11
|
+
/* The operations applicable to an object */
|
|
12
|
+
const objOps = {
|
|
13
|
+
add: function (obj, key, document) {
|
|
14
|
+
obj[key] = this.value;
|
|
15
|
+
return { newDocument: document };
|
|
16
|
+
},
|
|
17
|
+
remove: function (obj, key, document) {
|
|
18
|
+
var removed = obj[key];
|
|
19
|
+
delete obj[key];
|
|
20
|
+
return { newDocument: document, removed };
|
|
21
|
+
},
|
|
22
|
+
replace: function (obj, key, document) {
|
|
23
|
+
var removed = obj[key];
|
|
24
|
+
obj[key] = this.value;
|
|
25
|
+
return { newDocument: document, removed };
|
|
26
|
+
},
|
|
27
|
+
move: function (obj, key, document) {
|
|
28
|
+
/* in case move target overwrites an existing value,
|
|
29
|
+
return the removed value, this can be taxing performance-wise,
|
|
30
|
+
and is potentially unneeded */
|
|
31
|
+
let removed = getValueByPointer(document, this.path);
|
|
32
|
+
if (removed) {
|
|
33
|
+
removed = _deepClone(removed);
|
|
34
|
+
}
|
|
35
|
+
const originalValue = applyOperation(document, {
|
|
36
|
+
op: "remove",
|
|
37
|
+
path: this.from,
|
|
38
|
+
}).removed;
|
|
39
|
+
applyOperation(document, {
|
|
40
|
+
op: "add",
|
|
41
|
+
path: this.path,
|
|
42
|
+
value: originalValue,
|
|
43
|
+
});
|
|
44
|
+
return { newDocument: document, removed };
|
|
45
|
+
},
|
|
46
|
+
copy: function (obj, key, document) {
|
|
47
|
+
const valueToCopy = getValueByPointer(document, this.from);
|
|
48
|
+
// enforce copy by value so further operations don't affect source (see issue #177)
|
|
49
|
+
applyOperation(document, {
|
|
50
|
+
op: "add",
|
|
51
|
+
path: this.path,
|
|
52
|
+
value: _deepClone(valueToCopy),
|
|
53
|
+
});
|
|
54
|
+
return { newDocument: document };
|
|
55
|
+
},
|
|
56
|
+
test: function (obj, key, document) {
|
|
57
|
+
return { newDocument: document, test: _areEquals(obj[key], this.value) };
|
|
58
|
+
},
|
|
59
|
+
_get: function (obj, key, document) {
|
|
60
|
+
this.value = obj[key];
|
|
61
|
+
return { newDocument: document };
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
/* The operations applicable to an array. Many are the same as for the object */
|
|
65
|
+
var arrOps = {
|
|
66
|
+
add: function (arr, i, document) {
|
|
67
|
+
if (isInteger(i)) {
|
|
68
|
+
arr.splice(i, 0, this.value);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
// array props
|
|
72
|
+
arr[i] = this.value;
|
|
73
|
+
}
|
|
74
|
+
// this may be needed when using '-' in an array
|
|
75
|
+
return { newDocument: document, index: i };
|
|
76
|
+
},
|
|
77
|
+
remove: function (arr, i, document) {
|
|
78
|
+
var removedList = arr.splice(i, 1);
|
|
79
|
+
return { newDocument: document, removed: removedList[0] };
|
|
80
|
+
},
|
|
81
|
+
replace: function (arr, i, document) {
|
|
82
|
+
var removed = arr[i];
|
|
83
|
+
arr[i] = this.value;
|
|
84
|
+
return { newDocument: document, removed };
|
|
85
|
+
},
|
|
86
|
+
move: objOps.move,
|
|
87
|
+
copy: objOps.copy,
|
|
88
|
+
test: objOps.test,
|
|
89
|
+
_get: objOps._get,
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Retrieves a value from a JSON document by a JSON pointer.
|
|
93
|
+
* Returns the value.
|
|
94
|
+
*
|
|
95
|
+
* @param document The document to get the value from
|
|
96
|
+
* @param pointer an escaped JSON pointer
|
|
97
|
+
* @return The retrieved value
|
|
98
|
+
*/
|
|
99
|
+
export function getValueByPointer(document, pointer) {
|
|
100
|
+
if (pointer == "") {
|
|
101
|
+
return document;
|
|
102
|
+
}
|
|
103
|
+
var getOriginalDestination = { op: "_get", path: pointer };
|
|
104
|
+
applyOperation(document, getOriginalDestination);
|
|
105
|
+
return getOriginalDestination.value;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Apply a single JSON Patch Operation on a JSON document.
|
|
109
|
+
* Returns the {newDocument, result} of the operation.
|
|
110
|
+
* It modifies the `document` and `operation` objects - it gets the values by reference.
|
|
111
|
+
* If you would like to avoid touching your values, clone them:
|
|
112
|
+
* `jsonpatch.applyOperation(document, jsonpatch._deepClone(operation))`.
|
|
113
|
+
*
|
|
114
|
+
* @param document The document to patch
|
|
115
|
+
* @param operation The operation to apply
|
|
116
|
+
* @param validateOperation `false` is without validation, `true` to use default jsonpatch's validation, or you can pass a `validateOperation` callback to be used for validation.
|
|
117
|
+
* @param mutateDocument Whether to mutate the original document or clone it before applying
|
|
118
|
+
* @param banPrototypeModifications Whether to ban modifications to `__proto__`, defaults to `true`.
|
|
119
|
+
* @return `{newDocument, result}` after the operation
|
|
120
|
+
*/
|
|
121
|
+
export function applyOperation(document, operation, validateOperation = false, mutateDocument = true, banPrototypeModifications = true, index = 0) {
|
|
122
|
+
if (validateOperation) {
|
|
123
|
+
if (typeof validateOperation == "function") {
|
|
124
|
+
validateOperation(operation, 0, document, operation.path);
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
validator(operation, 0);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/* ROOT OPERATIONS */
|
|
131
|
+
if (operation.path === "") {
|
|
132
|
+
let returnValue = { newDocument: document };
|
|
133
|
+
if (operation.op === "add") {
|
|
134
|
+
returnValue.newDocument = operation.value;
|
|
135
|
+
return returnValue;
|
|
136
|
+
}
|
|
137
|
+
else if (operation.op === "replace") {
|
|
138
|
+
returnValue.newDocument = operation.value;
|
|
139
|
+
returnValue.removed = document; //document we removed
|
|
140
|
+
return returnValue;
|
|
141
|
+
}
|
|
142
|
+
else if (operation.op === "move" || operation.op === "copy") {
|
|
143
|
+
// it's a move or copy to root
|
|
144
|
+
returnValue.newDocument = getValueByPointer(document, operation.from); // get the value by json-pointer in `from` field
|
|
145
|
+
if (operation.op === "move") {
|
|
146
|
+
// report removed item
|
|
147
|
+
returnValue.removed = document;
|
|
148
|
+
}
|
|
149
|
+
return returnValue;
|
|
150
|
+
}
|
|
151
|
+
else if (operation.op === "test") {
|
|
152
|
+
returnValue.test = _areEquals(document, operation.value);
|
|
153
|
+
if (returnValue.test === false) {
|
|
154
|
+
throw new JsonPatchError("Test operation failed", "TEST_OPERATION_FAILED", index, operation, document);
|
|
155
|
+
}
|
|
156
|
+
returnValue.newDocument = document;
|
|
157
|
+
return returnValue;
|
|
158
|
+
}
|
|
159
|
+
else if (operation.op === "remove") {
|
|
160
|
+
// a remove on root
|
|
161
|
+
returnValue.removed = document;
|
|
162
|
+
returnValue.newDocument = null;
|
|
163
|
+
return returnValue;
|
|
164
|
+
}
|
|
165
|
+
else if (operation.op === "_get") {
|
|
166
|
+
operation.value = document;
|
|
167
|
+
return returnValue;
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
/* bad operation */
|
|
171
|
+
if (validateOperation) {
|
|
172
|
+
throw new JsonPatchError("Operation `op` property is not one of operations defined in RFC-6902", "OPERATION_OP_INVALID", index, operation, document);
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
return returnValue;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
} /* END ROOT OPERATIONS */
|
|
179
|
+
else {
|
|
180
|
+
if (!mutateDocument) {
|
|
181
|
+
document = _deepClone(document);
|
|
182
|
+
}
|
|
183
|
+
const path = operation.path || "";
|
|
184
|
+
const keys = path.split("/");
|
|
185
|
+
let obj = document;
|
|
186
|
+
let t = 1; //skip empty element - http://jsperf.com/to-shift-or-not-to-shift
|
|
187
|
+
let len = keys.length;
|
|
188
|
+
let existingPathFragment = undefined;
|
|
189
|
+
let key;
|
|
190
|
+
let validateFunction;
|
|
191
|
+
if (typeof validateOperation == "function") {
|
|
192
|
+
validateFunction = validateOperation;
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
validateFunction = validator;
|
|
196
|
+
}
|
|
197
|
+
while (true) {
|
|
198
|
+
key = keys[t];
|
|
199
|
+
if (key && key.indexOf("~") != -1) {
|
|
200
|
+
key = unescapePathComponent(key);
|
|
201
|
+
}
|
|
202
|
+
if (banPrototypeModifications &&
|
|
203
|
+
(key == "__proto__" ||
|
|
204
|
+
(key == "prototype" && t > 0 && keys[t - 1] == "constructor"))) {
|
|
205
|
+
throw new TypeError("JSON-Patch: modifying `__proto__` or `constructor/prototype` prop is banned for security reasons, if this was on purpose, please set `banPrototypeModifications` flag false and pass it to this function. More info in fast-json-patch README");
|
|
206
|
+
}
|
|
207
|
+
if (validateOperation) {
|
|
208
|
+
if (existingPathFragment === undefined) {
|
|
209
|
+
if (obj[key] === undefined) {
|
|
210
|
+
existingPathFragment = keys.slice(0, t).join("/");
|
|
211
|
+
}
|
|
212
|
+
else if (t == len - 1) {
|
|
213
|
+
existingPathFragment = operation.path;
|
|
214
|
+
}
|
|
215
|
+
if (existingPathFragment !== undefined) {
|
|
216
|
+
validateFunction(operation, 0, document, existingPathFragment);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
t++;
|
|
221
|
+
if (Array.isArray(obj)) {
|
|
222
|
+
if (key === "-") {
|
|
223
|
+
key = obj.length;
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
if (validateOperation && !isInteger(key)) {
|
|
227
|
+
throw new JsonPatchError("Expected an unsigned base-10 integer value, making the new referenced value the array element with the zero-based index", "OPERATION_PATH_ILLEGAL_ARRAY_INDEX", index, operation, document);
|
|
228
|
+
} // only parse key when it's an integer for `arr.prop` to work
|
|
229
|
+
else if (isInteger(key)) {
|
|
230
|
+
key = ~~key;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (t >= len) {
|
|
234
|
+
if (validateOperation && operation.op === "add" && key > obj.length) {
|
|
235
|
+
throw new JsonPatchError("The specified index MUST NOT be greater than the number of elements in the array", "OPERATION_VALUE_OUT_OF_BOUNDS", index, operation, document);
|
|
236
|
+
}
|
|
237
|
+
const returnValue = arrOps[operation.op].call(operation, obj, key, document); // Apply patch
|
|
238
|
+
if (returnValue.test === false) {
|
|
239
|
+
throw new JsonPatchError("Test operation failed", "TEST_OPERATION_FAILED", index, operation, document);
|
|
240
|
+
}
|
|
241
|
+
return returnValue;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
if (t >= len) {
|
|
246
|
+
const returnValue = objOps[operation.op].call(operation, obj, key, document); // Apply patch
|
|
247
|
+
if (returnValue.test === false) {
|
|
248
|
+
throw new JsonPatchError("Test operation failed", "TEST_OPERATION_FAILED", index, operation, document);
|
|
249
|
+
}
|
|
250
|
+
return returnValue;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
obj = obj[key];
|
|
254
|
+
// If we have more keys in the path, but the next value isn't a non-null object,
|
|
255
|
+
// throw an OPERATION_PATH_UNRESOLVABLE error instead of iterating again.
|
|
256
|
+
if (validateOperation && t < len && (!obj || typeof obj !== "object")) {
|
|
257
|
+
throw new JsonPatchError("Cannot perform operation at the desired path", "OPERATION_PATH_UNRESOLVABLE", index, operation, document);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Apply a full JSON Patch array on a JSON document.
|
|
264
|
+
* Returns the {newDocument, result} of the patch.
|
|
265
|
+
* It modifies the `document` object and `patch` - it gets the values by reference.
|
|
266
|
+
* If you would like to avoid touching your values, clone them:
|
|
267
|
+
* `jsonpatch.applyPatch(document, jsonpatch._deepClone(patch))`.
|
|
268
|
+
*
|
|
269
|
+
* @param document The document to patch
|
|
270
|
+
* @param patch The patch to apply
|
|
271
|
+
* @param validateOperation `false` is without validation, `true` to use default jsonpatch's validation, or you can pass a `validateOperation` callback to be used for validation.
|
|
272
|
+
* @param mutateDocument Whether to mutate the original document or clone it before applying
|
|
273
|
+
* @param banPrototypeModifications Whether to ban modifications to `__proto__`, defaults to `true`.
|
|
274
|
+
* @return An array of `{newDocument, result}` after the patch
|
|
275
|
+
*/
|
|
276
|
+
export function applyPatch(document, patch, validateOperation, mutateDocument = true, banPrototypeModifications = true) {
|
|
277
|
+
if (validateOperation) {
|
|
278
|
+
if (!Array.isArray(patch)) {
|
|
279
|
+
throw new JsonPatchError("Patch sequence must be an array", "SEQUENCE_NOT_AN_ARRAY");
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (!mutateDocument) {
|
|
283
|
+
document = _deepClone(document);
|
|
284
|
+
}
|
|
285
|
+
const results = new Array(patch.length);
|
|
286
|
+
for (let i = 0, length = patch.length; i < length; i++) {
|
|
287
|
+
// we don't need to pass mutateDocument argument because if it was true, we already deep cloned the object, we'll just pass `true`
|
|
288
|
+
results[i] = applyOperation(document, patch[i], validateOperation, true, banPrototypeModifications, i);
|
|
289
|
+
document = results[i].newDocument; // in case root was replaced
|
|
290
|
+
}
|
|
291
|
+
results.newDocument = document;
|
|
292
|
+
return results;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Apply a single JSON Patch Operation on a JSON document.
|
|
296
|
+
* Returns the updated document.
|
|
297
|
+
* Suitable as a reducer.
|
|
298
|
+
*
|
|
299
|
+
* @param document The document to patch
|
|
300
|
+
* @param operation The operation to apply
|
|
301
|
+
* @return The updated document
|
|
302
|
+
*/
|
|
303
|
+
export function applyReducer(document, operation, index) {
|
|
304
|
+
const operationResult = applyOperation(document, operation);
|
|
305
|
+
if (operationResult.test === false) {
|
|
306
|
+
// failed test
|
|
307
|
+
throw new JsonPatchError("Test operation failed", "TEST_OPERATION_FAILED", index, operation, document);
|
|
308
|
+
}
|
|
309
|
+
return operationResult.newDocument;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Validates a single operation. Called from `jsonpatch.validate`. Throws `JsonPatchError` in case of an error.
|
|
313
|
+
* @param {object} operation - operation object (patch)
|
|
314
|
+
* @param {number} index - index of operation in the sequence
|
|
315
|
+
* @param {object} [document] - object where the operation is supposed to be applied
|
|
316
|
+
* @param {string} [existingPathFragment] - comes along with `document`
|
|
317
|
+
*/
|
|
318
|
+
export function validator(operation, index, document, existingPathFragment) {
|
|
319
|
+
if (typeof operation !== "object" ||
|
|
320
|
+
operation === null ||
|
|
321
|
+
Array.isArray(operation)) {
|
|
322
|
+
throw new JsonPatchError("Operation is not an object", "OPERATION_NOT_AN_OBJECT", index, operation, document);
|
|
323
|
+
}
|
|
324
|
+
else if (!objOps[operation.op]) {
|
|
325
|
+
throw new JsonPatchError("Operation `op` property is not one of operations defined in RFC-6902", "OPERATION_OP_INVALID", index, operation, document);
|
|
326
|
+
}
|
|
327
|
+
else if (typeof operation.path !== "string") {
|
|
328
|
+
throw new JsonPatchError("Operation `path` property is not a string", "OPERATION_PATH_INVALID", index, operation, document);
|
|
329
|
+
}
|
|
330
|
+
else if (operation.path.indexOf("/") !== 0 && operation.path.length > 0) {
|
|
331
|
+
// paths that aren't empty string should start with "/"
|
|
332
|
+
throw new JsonPatchError('Operation `path` property must start with "/"', "OPERATION_PATH_INVALID", index, operation, document);
|
|
333
|
+
}
|
|
334
|
+
else if ((operation.op === "move" || operation.op === "copy") &&
|
|
335
|
+
typeof operation.from !== "string") {
|
|
336
|
+
throw new JsonPatchError("Operation `from` property is not present (applicable in `move` and `copy` operations)", "OPERATION_FROM_REQUIRED", index, operation, document);
|
|
337
|
+
}
|
|
338
|
+
else if ((operation.op === "add" ||
|
|
339
|
+
operation.op === "replace" ||
|
|
340
|
+
operation.op === "test") &&
|
|
341
|
+
operation.value === undefined) {
|
|
342
|
+
throw new JsonPatchError("Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)", "OPERATION_VALUE_REQUIRED", index, operation, document);
|
|
343
|
+
}
|
|
344
|
+
else if ((operation.op === "add" ||
|
|
345
|
+
operation.op === "replace" ||
|
|
346
|
+
operation.op === "test") &&
|
|
347
|
+
hasUndefined(operation.value)) {
|
|
348
|
+
throw new JsonPatchError("Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)", "OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED", index, operation, document);
|
|
349
|
+
}
|
|
350
|
+
else if (document) {
|
|
351
|
+
if (operation.op == "add") {
|
|
352
|
+
var pathLen = operation.path.split("/").length;
|
|
353
|
+
var existingPathLen = existingPathFragment.split("/").length;
|
|
354
|
+
if (pathLen !== existingPathLen + 1 && pathLen !== existingPathLen) {
|
|
355
|
+
throw new JsonPatchError("Cannot perform an `add` operation at the desired path", "OPERATION_PATH_CANNOT_ADD", index, operation, document);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
else if (operation.op === "replace" ||
|
|
359
|
+
operation.op === "remove" ||
|
|
360
|
+
operation.op === "_get") {
|
|
361
|
+
if (operation.path !== existingPathFragment) {
|
|
362
|
+
throw new JsonPatchError("Cannot perform the operation at a path that does not exist", "OPERATION_PATH_UNRESOLVABLE", index, operation, document);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
else if (operation.op === "move" || operation.op === "copy") {
|
|
366
|
+
var existingValue = {
|
|
367
|
+
op: "_get",
|
|
368
|
+
path: operation.from,
|
|
369
|
+
value: undefined,
|
|
370
|
+
};
|
|
371
|
+
var error = validate([existingValue], document);
|
|
372
|
+
if (error && error.name === "OPERATION_PATH_UNRESOLVABLE") {
|
|
373
|
+
throw new JsonPatchError("Cannot perform the operation from a path that does not exist", "OPERATION_FROM_UNRESOLVABLE", index, operation, document);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Validates a sequence of operations. If `document` parameter is provided, the sequence is additionally validated against the object document.
|
|
380
|
+
* If error is encountered, returns a JsonPatchError object
|
|
381
|
+
* @param sequence
|
|
382
|
+
* @param document
|
|
383
|
+
* @returns {JsonPatchError|undefined}
|
|
384
|
+
*/
|
|
385
|
+
export function validate(sequence, document, externalValidator) {
|
|
386
|
+
try {
|
|
387
|
+
if (!Array.isArray(sequence)) {
|
|
388
|
+
throw new JsonPatchError("Patch sequence must be an array", "SEQUENCE_NOT_AN_ARRAY");
|
|
389
|
+
}
|
|
390
|
+
if (document) {
|
|
391
|
+
//clone document and sequence so that we can safely try applying operations
|
|
392
|
+
applyPatch(_deepClone(document), _deepClone(sequence), externalValidator || true);
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
externalValidator = externalValidator || validator;
|
|
396
|
+
for (var i = 0; i < sequence.length; i++) {
|
|
397
|
+
externalValidator(sequence[i], i, document, undefined);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
catch (e) {
|
|
402
|
+
if (e instanceof JsonPatchError) {
|
|
403
|
+
return e;
|
|
404
|
+
}
|
|
405
|
+
else {
|
|
406
|
+
throw e;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
// based on https://github.com/epoberezkin/fast-deep-equal
|
|
411
|
+
// MIT License
|
|
412
|
+
// Copyright (c) 2017 Evgeny Poberezkin
|
|
413
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
414
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
415
|
+
// in the Software without restriction, including without limitation the rights
|
|
416
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
417
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
418
|
+
// furnished to do so, subject to the following conditions:
|
|
419
|
+
// The above copyright notice and this permission notice shall be included in all
|
|
420
|
+
// copies or substantial portions of the Software.
|
|
421
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
422
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
423
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
424
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
425
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
426
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
427
|
+
// SOFTWARE.
|
|
428
|
+
export function _areEquals(a, b) {
|
|
429
|
+
if (a === b)
|
|
430
|
+
return true;
|
|
431
|
+
if (a && b && typeof a == "object" && typeof b == "object") {
|
|
432
|
+
var arrA = Array.isArray(a), arrB = Array.isArray(b), i, length, key;
|
|
433
|
+
if (arrA && arrB) {
|
|
434
|
+
length = a.length;
|
|
435
|
+
if (length != b.length)
|
|
436
|
+
return false;
|
|
437
|
+
for (i = length; i-- !== 0;)
|
|
438
|
+
if (!_areEquals(a[i], b[i]))
|
|
439
|
+
return false;
|
|
440
|
+
return true;
|
|
441
|
+
}
|
|
442
|
+
if (arrA != arrB)
|
|
443
|
+
return false;
|
|
444
|
+
var keys = Object.keys(a);
|
|
445
|
+
length = keys.length;
|
|
446
|
+
if (length !== Object.keys(b).length)
|
|
447
|
+
return false;
|
|
448
|
+
for (i = length; i-- !== 0;)
|
|
449
|
+
if (!b.hasOwnProperty(keys[i]))
|
|
450
|
+
return false;
|
|
451
|
+
for (i = length; i-- !== 0;) {
|
|
452
|
+
key = keys[i];
|
|
453
|
+
if (!_areEquals(a[key], b[key]))
|
|
454
|
+
return false;
|
|
455
|
+
}
|
|
456
|
+
return true;
|
|
457
|
+
}
|
|
458
|
+
return a !== a && b !== b;
|
|
459
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @ts-nocheck
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.PatchError = exports.hasUndefined = exports.getPath = exports._getPathRecursive = exports.unescapePathComponent = exports.escapePathComponent = exports.isInteger = exports._deepClone = exports._objectKeys = exports.hasOwnProperty = void 0;
|
|
5
|
+
// Inlined because of ESM import issues
|
|
6
|
+
/*!
|
|
7
|
+
* https://github.com/Starcounter-Jack/JSON-Patch
|
|
8
|
+
* (c) 2017-2022 Joachim Wester
|
|
9
|
+
* MIT licensed
|
|
10
|
+
*/
|
|
11
|
+
const _hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
12
|
+
function hasOwnProperty(obj, key) {
|
|
13
|
+
return _hasOwnProperty.call(obj, key);
|
|
14
|
+
}
|
|
15
|
+
exports.hasOwnProperty = hasOwnProperty;
|
|
16
|
+
function _objectKeys(obj) {
|
|
17
|
+
if (Array.isArray(obj)) {
|
|
18
|
+
const keys = new Array(obj.length);
|
|
19
|
+
for (let k = 0; k < keys.length; k++) {
|
|
20
|
+
keys[k] = "" + k;
|
|
21
|
+
}
|
|
22
|
+
return keys;
|
|
23
|
+
}
|
|
24
|
+
if (Object.keys) {
|
|
25
|
+
return Object.keys(obj);
|
|
26
|
+
}
|
|
27
|
+
let keys = [];
|
|
28
|
+
for (let i in obj) {
|
|
29
|
+
if (hasOwnProperty(obj, i)) {
|
|
30
|
+
keys.push(i);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return keys;
|
|
34
|
+
}
|
|
35
|
+
exports._objectKeys = _objectKeys;
|
|
36
|
+
/**
|
|
37
|
+
* Deeply clone the object.
|
|
38
|
+
* https://jsperf.com/deep-copy-vs-json-stringify-json-parse/25 (recursiveDeepCopy)
|
|
39
|
+
* @param {any} obj value to clone
|
|
40
|
+
* @return {any} cloned obj
|
|
41
|
+
*/
|
|
42
|
+
function _deepClone(obj) {
|
|
43
|
+
switch (typeof obj) {
|
|
44
|
+
case "object":
|
|
45
|
+
return JSON.parse(JSON.stringify(obj)); //Faster than ES5 clone - http://jsperf.com/deep-cloning-of-objects/5
|
|
46
|
+
case "undefined":
|
|
47
|
+
return null; //this is how JSON.stringify behaves for array items
|
|
48
|
+
default:
|
|
49
|
+
return obj; //no need to clone primitives
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports._deepClone = _deepClone;
|
|
53
|
+
//3x faster than cached /^\d+$/.test(str)
|
|
54
|
+
function isInteger(str) {
|
|
55
|
+
let i = 0;
|
|
56
|
+
const len = str.length;
|
|
57
|
+
let charCode;
|
|
58
|
+
while (i < len) {
|
|
59
|
+
charCode = str.charCodeAt(i);
|
|
60
|
+
if (charCode >= 48 && charCode <= 57) {
|
|
61
|
+
i++;
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
exports.isInteger = isInteger;
|
|
69
|
+
/**
|
|
70
|
+
* Escapes a json pointer path
|
|
71
|
+
* @param path The raw pointer
|
|
72
|
+
* @return the Escaped path
|
|
73
|
+
*/
|
|
74
|
+
function escapePathComponent(path) {
|
|
75
|
+
if (path.indexOf("/") === -1 && path.indexOf("~") === -1)
|
|
76
|
+
return path;
|
|
77
|
+
return path.replace(/~/g, "~0").replace(/\//g, "~1");
|
|
78
|
+
}
|
|
79
|
+
exports.escapePathComponent = escapePathComponent;
|
|
80
|
+
/**
|
|
81
|
+
* Unescapes a json pointer path
|
|
82
|
+
* @param path The escaped pointer
|
|
83
|
+
* @return The unescaped path
|
|
84
|
+
*/
|
|
85
|
+
function unescapePathComponent(path) {
|
|
86
|
+
return path.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
87
|
+
}
|
|
88
|
+
exports.unescapePathComponent = unescapePathComponent;
|
|
89
|
+
function _getPathRecursive(root, obj) {
|
|
90
|
+
let found;
|
|
91
|
+
for (let key in root) {
|
|
92
|
+
if (hasOwnProperty(root, key)) {
|
|
93
|
+
if (root[key] === obj) {
|
|
94
|
+
return escapePathComponent(key) + "/";
|
|
95
|
+
}
|
|
96
|
+
else if (typeof root[key] === "object") {
|
|
97
|
+
found = _getPathRecursive(root[key], obj);
|
|
98
|
+
if (found != "") {
|
|
99
|
+
return escapePathComponent(key) + "/" + found;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return "";
|
|
105
|
+
}
|
|
106
|
+
exports._getPathRecursive = _getPathRecursive;
|
|
107
|
+
function getPath(root, obj) {
|
|
108
|
+
if (root === obj) {
|
|
109
|
+
return "/";
|
|
110
|
+
}
|
|
111
|
+
const path = _getPathRecursive(root, obj);
|
|
112
|
+
if (path === "") {
|
|
113
|
+
throw new Error("Object not found in root");
|
|
114
|
+
}
|
|
115
|
+
return `/${path}`;
|
|
116
|
+
}
|
|
117
|
+
exports.getPath = getPath;
|
|
118
|
+
/**
|
|
119
|
+
* Recursively checks whether an object has any undefined values inside.
|
|
120
|
+
*/
|
|
121
|
+
function hasUndefined(obj) {
|
|
122
|
+
if (obj === undefined) {
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
if (obj) {
|
|
126
|
+
if (Array.isArray(obj)) {
|
|
127
|
+
for (let i = 0, len = obj.length; i < len; i++) {
|
|
128
|
+
if (hasUndefined(obj[i])) {
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
else if (typeof obj === "object") {
|
|
134
|
+
const objKeys = _objectKeys(obj);
|
|
135
|
+
const objKeysLength = objKeys.length;
|
|
136
|
+
for (var i = 0; i < objKeysLength; i++) {
|
|
137
|
+
if (hasUndefined(obj[objKeys[i]])) {
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
exports.hasUndefined = hasUndefined;
|
|
146
|
+
function patchErrorMessageFormatter(message, args) {
|
|
147
|
+
const messageParts = [message];
|
|
148
|
+
for (const key in args) {
|
|
149
|
+
const value = typeof args[key] === "object"
|
|
150
|
+
? JSON.stringify(args[key], null, 2)
|
|
151
|
+
: args[key]; // pretty print
|
|
152
|
+
if (typeof value !== "undefined") {
|
|
153
|
+
messageParts.push(`${key}: ${value}`);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return messageParts.join("\n");
|
|
157
|
+
}
|
|
158
|
+
class PatchError extends Error {
|
|
159
|
+
constructor(message, name, index, operation, tree) {
|
|
160
|
+
super(patchErrorMessageFormatter(message, { name, index, operation, tree }));
|
|
161
|
+
Object.defineProperty(this, "name", {
|
|
162
|
+
enumerable: true,
|
|
163
|
+
configurable: true,
|
|
164
|
+
writable: true,
|
|
165
|
+
value: name
|
|
166
|
+
});
|
|
167
|
+
Object.defineProperty(this, "index", {
|
|
168
|
+
enumerable: true,
|
|
169
|
+
configurable: true,
|
|
170
|
+
writable: true,
|
|
171
|
+
value: index
|
|
172
|
+
});
|
|
173
|
+
Object.defineProperty(this, "operation", {
|
|
174
|
+
enumerable: true,
|
|
175
|
+
configurable: true,
|
|
176
|
+
writable: true,
|
|
177
|
+
value: operation
|
|
178
|
+
});
|
|
179
|
+
Object.defineProperty(this, "tree", {
|
|
180
|
+
enumerable: true,
|
|
181
|
+
configurable: true,
|
|
182
|
+
writable: true,
|
|
183
|
+
value: tree
|
|
184
|
+
});
|
|
185
|
+
Object.setPrototypeOf(this, new.target.prototype); // restore prototype chain, see https://stackoverflow.com/a/48342359
|
|
186
|
+
this.message = patchErrorMessageFormatter(message, {
|
|
187
|
+
name,
|
|
188
|
+
index,
|
|
189
|
+
operation,
|
|
190
|
+
tree,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
exports.PatchError = PatchError;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export declare function hasOwnProperty(obj: any, key: any): boolean;
|
|
2
|
+
export declare function _objectKeys(obj: any): any[];
|
|
3
|
+
/**
|
|
4
|
+
* Deeply clone the object.
|
|
5
|
+
* https://jsperf.com/deep-copy-vs-json-stringify-json-parse/25 (recursiveDeepCopy)
|
|
6
|
+
* @param {any} obj value to clone
|
|
7
|
+
* @return {any} cloned obj
|
|
8
|
+
*/
|
|
9
|
+
export declare function _deepClone(obj: any): any;
|
|
10
|
+
export declare function isInteger(str: string): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Escapes a json pointer path
|
|
13
|
+
* @param path The raw pointer
|
|
14
|
+
* @return the Escaped path
|
|
15
|
+
*/
|
|
16
|
+
export declare function escapePathComponent(path: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* Unescapes a json pointer path
|
|
19
|
+
* @param path The escaped pointer
|
|
20
|
+
* @return The unescaped path
|
|
21
|
+
*/
|
|
22
|
+
export declare function unescapePathComponent(path: string): string;
|
|
23
|
+
export declare function _getPathRecursive(root: Object, obj: Object): string;
|
|
24
|
+
export declare function getPath(root: Object, obj: Object): string;
|
|
25
|
+
/**
|
|
26
|
+
* Recursively checks whether an object has any undefined values inside.
|
|
27
|
+
*/
|
|
28
|
+
export declare function hasUndefined(obj: any): boolean;
|
|
29
|
+
export type JsonPatchErrorName = "SEQUENCE_NOT_AN_ARRAY" | "OPERATION_NOT_AN_OBJECT" | "OPERATION_OP_INVALID" | "OPERATION_PATH_INVALID" | "OPERATION_FROM_REQUIRED" | "OPERATION_VALUE_REQUIRED" | "OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED" | "OPERATION_PATH_CANNOT_ADD" | "OPERATION_PATH_UNRESOLVABLE" | "OPERATION_FROM_UNRESOLVABLE" | "OPERATION_PATH_ILLEGAL_ARRAY_INDEX" | "OPERATION_VALUE_OUT_OF_BOUNDS" | "TEST_OPERATION_FAILED";
|
|
30
|
+
export declare class PatchError extends Error {
|
|
31
|
+
name: JsonPatchErrorName;
|
|
32
|
+
index?: number | undefined;
|
|
33
|
+
operation?: any;
|
|
34
|
+
tree?: any;
|
|
35
|
+
constructor(message: string, name: JsonPatchErrorName, index?: number | undefined, operation?: any, tree?: any);
|
|
36
|
+
}
|