langchain 0.0.195 → 0.0.197-rc.0

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.
Files changed (236) hide show
  1. package/LICENSE +21 -0
  2. package/dist/agents/openai/index.cjs +6 -2
  3. package/dist/agents/openai/index.js +6 -2
  4. package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts +1 -1
  5. package/dist/base_language/count_tokens.cjs +5 -70
  6. package/dist/base_language/count_tokens.d.ts +1 -10
  7. package/dist/base_language/count_tokens.js +1 -65
  8. package/dist/base_language/index.cjs +6 -196
  9. package/dist/base_language/index.d.ts +1 -111
  10. package/dist/base_language/index.js +1 -191
  11. package/dist/cache/base.cjs +15 -37
  12. package/dist/cache/base.d.ts +1 -20
  13. package/dist/cache/base.js +1 -33
  14. package/dist/cache/index.cjs +2 -46
  15. package/dist/cache/index.d.ts +1 -29
  16. package/dist/cache/index.js +1 -45
  17. package/dist/callbacks/base.cjs +3 -139
  18. package/dist/callbacks/base.d.ts +1 -266
  19. package/dist/callbacks/base.js +1 -126
  20. package/dist/callbacks/handlers/console.cjs +14 -221
  21. package/dist/callbacks/handlers/console.d.ts +1 -117
  22. package/dist/callbacks/handlers/console.js +1 -217
  23. package/dist/callbacks/handlers/initialize.cjs +15 -30
  24. package/dist/callbacks/handlers/initialize.d.ts +1 -16
  25. package/dist/callbacks/handlers/initialize.js +1 -27
  26. package/dist/callbacks/handlers/log_stream.cjs +15 -293
  27. package/dist/callbacks/handlers/log_stream.d.ts +1 -100
  28. package/dist/callbacks/handlers/log_stream.js +1 -289
  29. package/dist/callbacks/handlers/run_collector.cjs +15 -48
  30. package/dist/callbacks/handlers/run_collector.d.ts +1 -26
  31. package/dist/callbacks/handlers/run_collector.js +1 -46
  32. package/dist/callbacks/handlers/tracer.cjs +15 -375
  33. package/dist/callbacks/handlers/tracer.d.ts +1 -70
  34. package/dist/callbacks/handlers/tracer.js +1 -373
  35. package/dist/callbacks/handlers/tracer_langchain.cjs +15 -104
  36. package/dist/callbacks/handlers/tracer_langchain.d.ts +1 -41
  37. package/dist/callbacks/handlers/tracer_langchain.js +1 -102
  38. package/dist/callbacks/handlers/tracer_langchain_v1.cjs +15 -197
  39. package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +1 -57
  40. package/dist/callbacks/handlers/tracer_langchain_v1.js +1 -195
  41. package/dist/callbacks/manager.cjs +15 -676
  42. package/dist/callbacks/manager.d.ts +1 -180
  43. package/dist/callbacks/manager.js +1 -666
  44. package/dist/callbacks/promises.cjs +14 -42
  45. package/dist/callbacks/promises.d.ts +1 -11
  46. package/dist/callbacks/promises.js +1 -37
  47. package/dist/chains/graph_qa/prompts.d.ts +1 -1
  48. package/dist/chains/openai_functions/structured_output.cjs +2 -2
  49. package/dist/chains/openai_functions/structured_output.d.ts +1 -1
  50. package/dist/chains/openai_functions/structured_output.js +1 -1
  51. package/dist/chat_models/anthropic.cjs +15 -348
  52. package/dist/chat_models/anthropic.d.ts +1 -156
  53. package/dist/chat_models/anthropic.js +1 -346
  54. package/dist/chat_models/baiduwenxin.d.ts +1 -1
  55. package/dist/chat_models/base.cjs +15 -296
  56. package/dist/chat_models/base.d.ts +1 -122
  57. package/dist/chat_models/base.js +1 -292
  58. package/dist/chat_models/bedrock/web.cjs +21 -1
  59. package/dist/chat_models/bedrock/web.d.ts +2 -2
  60. package/dist/chat_models/bedrock/web.js +21 -1
  61. package/dist/chat_models/fireworks.d.ts +1 -1
  62. package/dist/document.cjs +2 -24
  63. package/dist/document.d.ts +1 -12
  64. package/dist/document.js +1 -23
  65. package/dist/document_loaders/web/azure_blob_storage_file.d.ts +1 -1
  66. package/dist/document_loaders/web/github.cjs +105 -0
  67. package/dist/document_loaders/web/github.d.ts +26 -0
  68. package/dist/document_loaders/web/github.js +105 -0
  69. package/dist/document_loaders/web/s3.d.ts +1 -1
  70. package/dist/embeddings/base.cjs +15 -22
  71. package/dist/embeddings/base.d.ts +1 -33
  72. package/dist/embeddings/base.js +1 -20
  73. package/dist/embeddings/cache_backed.cjs +2 -2
  74. package/dist/embeddings/cache_backed.js +1 -1
  75. package/dist/evaluation/agents/trajectory.d.ts +1 -1
  76. package/dist/evaluation/criteria/prompt.d.ts +2 -2
  77. package/dist/evaluation/qa/prompt.d.ts +2 -2
  78. package/dist/experimental/hubs/makersuite/googlemakersuitehub.d.ts +1 -1
  79. package/dist/experimental/plan_and_execute/prompt.d.ts +1 -1
  80. package/dist/llms/base.cjs +15 -278
  81. package/dist/llms/base.d.ts +1 -115
  82. package/dist/llms/base.js +1 -275
  83. package/dist/llms/bedrock/web.cjs +21 -1
  84. package/dist/llms/bedrock/web.d.ts +2 -2
  85. package/dist/llms/bedrock/web.js +21 -1
  86. package/dist/llms/fireworks.d.ts +1 -1
  87. package/dist/load/import_map.cjs +2 -1
  88. package/dist/load/import_map.d.ts +1 -0
  89. package/dist/load/import_map.js +1 -0
  90. package/dist/load/index.cjs +7 -148
  91. package/dist/load/index.js +7 -148
  92. package/dist/load/map_keys.cjs +0 -24
  93. package/dist/load/map_keys.d.ts +0 -6
  94. package/dist/load/map_keys.js +1 -17
  95. package/dist/load/serializable.cjs +15 -178
  96. package/dist/load/serializable.d.ts +1 -66
  97. package/dist/load/serializable.js +1 -175
  98. package/dist/memory/base.cjs +17 -92
  99. package/dist/memory/base.d.ts +2 -68
  100. package/dist/memory/base.js +2 -87
  101. package/dist/output_parsers/list.cjs +4 -122
  102. package/dist/output_parsers/list.d.ts +1 -57
  103. package/dist/output_parsers/list.js +1 -119
  104. package/dist/output_parsers/openai_functions.cjs +2 -2
  105. package/dist/output_parsers/openai_functions.d.ts +1 -1
  106. package/dist/output_parsers/openai_functions.js +1 -1
  107. package/dist/output_parsers/regex.d.ts +1 -1
  108. package/dist/output_parsers/structured.d.ts +1 -1
  109. package/dist/prompts/base.cjs +8 -183
  110. package/dist/prompts/base.d.ts +3 -132
  111. package/dist/prompts/base.js +3 -178
  112. package/dist/prompts/chat.cjs +13 -477
  113. package/dist/prompts/chat.d.ts +2 -219
  114. package/dist/prompts/chat.js +2 -466
  115. package/dist/prompts/few_shot.cjs +3 -352
  116. package/dist/prompts/few_shot.d.ts +1 -192
  117. package/dist/prompts/few_shot.js +1 -350
  118. package/dist/prompts/index.cjs +3 -2
  119. package/dist/prompts/index.d.ts +2 -1
  120. package/dist/prompts/index.js +2 -1
  121. package/dist/prompts/pipeline.cjs +2 -141
  122. package/dist/prompts/pipeline.d.ts +1 -98
  123. package/dist/prompts/pipeline.js +1 -140
  124. package/dist/prompts/prompt.cjs +2 -145
  125. package/dist/prompts/prompt.d.ts +1 -92
  126. package/dist/prompts/prompt.js +1 -144
  127. package/dist/prompts/selectors/LengthBasedExampleSelector.cjs +2 -147
  128. package/dist/prompts/selectors/LengthBasedExampleSelector.d.ts +1 -89
  129. package/dist/prompts/selectors/LengthBasedExampleSelector.js +1 -146
  130. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.cjs +15 -137
  131. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.d.ts +1 -91
  132. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.js +1 -135
  133. package/dist/prompts/selectors/conditional.cjs +5 -73
  134. package/dist/prompts/selectors/conditional.d.ts +1 -63
  135. package/dist/prompts/selectors/conditional.js +1 -69
  136. package/dist/prompts/serde.d.ts +1 -43
  137. package/dist/prompts/template.cjs +8 -88
  138. package/dist/prompts/template.d.ts +1 -36
  139. package/dist/prompts/template.js +1 -83
  140. package/dist/{util/@cfworker/json-schema → runnables}/index.cjs +1 -1
  141. package/dist/runnables/index.d.ts +1 -0
  142. package/dist/runnables/index.js +1 -0
  143. package/dist/schema/document.cjs +3 -34
  144. package/dist/schema/document.d.ts +2 -29
  145. package/dist/schema/document.js +2 -32
  146. package/dist/schema/index.cjs +37 -612
  147. package/dist/schema/index.d.ts +11 -311
  148. package/dist/schema/index.js +8 -583
  149. package/dist/schema/output_parser.cjs +15 -309
  150. package/dist/schema/output_parser.d.ts +1 -173
  151. package/dist/schema/output_parser.js +1 -301
  152. package/dist/schema/retriever.cjs +15 -77
  153. package/dist/schema/retriever.d.ts +1 -43
  154. package/dist/schema/retriever.js +1 -75
  155. package/dist/schema/runnable/base.cjs +10 -1072
  156. package/dist/schema/runnable/base.d.ts +1 -356
  157. package/dist/schema/runnable/base.js +1 -1060
  158. package/dist/schema/runnable/branch.cjs +2 -131
  159. package/dist/schema/runnable/branch.d.ts +1 -94
  160. package/dist/schema/runnable/branch.js +1 -130
  161. package/dist/schema/runnable/config.cjs +0 -6
  162. package/dist/schema/runnable/config.d.ts +1 -3
  163. package/dist/schema/runnable/config.js +1 -4
  164. package/dist/schema/runnable/index.cjs +15 -16
  165. package/dist/schema/runnable/index.d.ts +1 -5
  166. package/dist/schema/runnable/index.js +1 -4
  167. package/dist/schema/runnable/passthrough.cjs +3 -113
  168. package/dist/schema/runnable/passthrough.d.ts +1 -72
  169. package/dist/schema/runnable/passthrough.js +1 -111
  170. package/dist/schema/runnable/router.cjs +2 -71
  171. package/dist/schema/runnable/router.d.ts +1 -29
  172. package/dist/schema/runnable/router.js +1 -70
  173. package/dist/schema/storage.cjs +15 -8
  174. package/dist/schema/storage.d.ts +1 -57
  175. package/dist/schema/storage.js +1 -6
  176. package/dist/tools/bingserpapi.d.ts +1 -1
  177. package/dist/tools/searchapi.d.ts +1 -1
  178. package/dist/tools/serpapi.d.ts +1 -1
  179. package/dist/tools/serper.d.ts +1 -1
  180. package/dist/util/async_caller.cjs +14 -128
  181. package/dist/util/async_caller.d.ts +1 -45
  182. package/dist/util/async_caller.js +1 -124
  183. package/dist/vectorstores/momento_vector_index.cjs +39 -0
  184. package/dist/vectorstores/momento_vector_index.d.ts +17 -1
  185. package/dist/vectorstores/momento_vector_index.js +40 -1
  186. package/dist/vectorstores/mongodb_atlas.cjs +22 -2
  187. package/dist/vectorstores/mongodb_atlas.d.ts +13 -0
  188. package/dist/vectorstores/mongodb_atlas.js +22 -2
  189. package/package.json +18 -11
  190. package/runnables.cjs +1 -0
  191. package/runnables.d.ts +1 -0
  192. package/runnables.js +1 -0
  193. package/dist/util/@cfworker/json-schema/index.d.ts +0 -1
  194. package/dist/util/@cfworker/json-schema/index.js +0 -1
  195. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.cjs +0 -43
  196. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.d.ts +0 -1
  197. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.js +0 -39
  198. package/dist/util/@cfworker/json-schema/src/dereference.cjs +0 -169
  199. package/dist/util/@cfworker/json-schema/src/dereference.d.ts +0 -12
  200. package/dist/util/@cfworker/json-schema/src/dereference.js +0 -165
  201. package/dist/util/@cfworker/json-schema/src/format.cjs +0 -139
  202. package/dist/util/@cfworker/json-schema/src/format.d.ts +0 -2
  203. package/dist/util/@cfworker/json-schema/src/format.js +0 -136
  204. package/dist/util/@cfworker/json-schema/src/index.cjs +0 -24
  205. package/dist/util/@cfworker/json-schema/src/index.d.ts +0 -8
  206. package/dist/util/@cfworker/json-schema/src/index.js +0 -8
  207. package/dist/util/@cfworker/json-schema/src/pointer.cjs +0 -11
  208. package/dist/util/@cfworker/json-schema/src/pointer.d.ts +0 -2
  209. package/dist/util/@cfworker/json-schema/src/pointer.js +0 -6
  210. package/dist/util/@cfworker/json-schema/src/types.cjs +0 -2
  211. package/dist/util/@cfworker/json-schema/src/types.d.ts +0 -72
  212. package/dist/util/@cfworker/json-schema/src/types.js +0 -1
  213. package/dist/util/@cfworker/json-schema/src/ucs2-length.cjs +0 -28
  214. package/dist/util/@cfworker/json-schema/src/ucs2-length.d.ts +0 -6
  215. package/dist/util/@cfworker/json-schema/src/ucs2-length.js +0 -24
  216. package/dist/util/@cfworker/json-schema/src/validate.cjs +0 -808
  217. package/dist/util/@cfworker/json-schema/src/validate.d.ts +0 -3
  218. package/dist/util/@cfworker/json-schema/src/validate.js +0 -804
  219. package/dist/util/@cfworker/json-schema/src/validator.cjs +0 -44
  220. package/dist/util/@cfworker/json-schema/src/validator.d.ts +0 -10
  221. package/dist/util/@cfworker/json-schema/src/validator.js +0 -40
  222. package/dist/util/fast-json-patch/index.cjs +0 -49
  223. package/dist/util/fast-json-patch/index.d.ts +0 -22
  224. package/dist/util/fast-json-patch/index.js +0 -16
  225. package/dist/util/fast-json-patch/src/core.cjs +0 -469
  226. package/dist/util/fast-json-patch/src/core.d.ts +0 -111
  227. package/dist/util/fast-json-patch/src/core.js +0 -459
  228. package/dist/util/fast-json-patch/src/duplex.cjs +0 -237
  229. package/dist/util/fast-json-patch/src/duplex.d.ts +0 -23
  230. package/dist/util/fast-json-patch/src/duplex.js +0 -230
  231. package/dist/util/fast-json-patch/src/helpers.cjs +0 -194
  232. package/dist/util/fast-json-patch/src/helpers.d.ts +0 -36
  233. package/dist/util/fast-json-patch/src/helpers.js +0 -181
  234. package/dist/util/js-sha1/hash.cjs +0 -358
  235. package/dist/util/js-sha1/hash.d.ts +0 -1
  236. package/dist/util/js-sha1/hash.js +0 -355
@@ -1,111 +0,0 @@
1
- import { PatchError, _deepClone } from "./helpers.js";
2
- export declare const JsonPatchError: typeof PatchError;
3
- export declare const deepClone: typeof _deepClone;
4
- export type Operation = AddOperation<any> | RemoveOperation | ReplaceOperation<any> | MoveOperation | CopyOperation | TestOperation<any> | GetOperation<any>;
5
- export interface Validator<T> {
6
- (operation: Operation, index: number, document: T, existingPathFragment: string): void;
7
- }
8
- export interface OperationResult<T> {
9
- removed?: any;
10
- test?: boolean;
11
- newDocument: T;
12
- }
13
- export interface BaseOperation {
14
- path: string;
15
- }
16
- export interface AddOperation<T> extends BaseOperation {
17
- op: "add";
18
- value: T;
19
- }
20
- export interface RemoveOperation extends BaseOperation {
21
- op: "remove";
22
- }
23
- export interface ReplaceOperation<T> extends BaseOperation {
24
- op: "replace";
25
- value: T;
26
- }
27
- export interface MoveOperation extends BaseOperation {
28
- op: "move";
29
- from: string;
30
- }
31
- export interface CopyOperation extends BaseOperation {
32
- op: "copy";
33
- from: string;
34
- }
35
- export interface TestOperation<T> extends BaseOperation {
36
- op: "test";
37
- value: T;
38
- }
39
- export interface GetOperation<T> extends BaseOperation {
40
- op: "_get";
41
- value: T;
42
- }
43
- export interface PatchResult<T> extends Array<OperationResult<T>> {
44
- newDocument: T;
45
- }
46
- /**
47
- * Retrieves a value from a JSON document by a JSON pointer.
48
- * Returns the value.
49
- *
50
- * @param document The document to get the value from
51
- * @param pointer an escaped JSON pointer
52
- * @return The retrieved value
53
- */
54
- export declare function getValueByPointer(document: any, pointer: string): any;
55
- /**
56
- * Apply a single JSON Patch Operation on a JSON document.
57
- * Returns the {newDocument, result} of the operation.
58
- * It modifies the `document` and `operation` objects - it gets the values by reference.
59
- * If you would like to avoid touching your values, clone them:
60
- * `jsonpatch.applyOperation(document, jsonpatch._deepClone(operation))`.
61
- *
62
- * @param document The document to patch
63
- * @param operation The operation to apply
64
- * @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.
65
- * @param mutateDocument Whether to mutate the original document or clone it before applying
66
- * @param banPrototypeModifications Whether to ban modifications to `__proto__`, defaults to `true`.
67
- * @return `{newDocument, result}` after the operation
68
- */
69
- export declare function applyOperation<T>(document: T, operation: Operation, validateOperation?: boolean | Validator<T>, mutateDocument?: boolean, banPrototypeModifications?: boolean, index?: number): OperationResult<T>;
70
- /**
71
- * Apply a full JSON Patch array on a JSON document.
72
- * Returns the {newDocument, result} of the patch.
73
- * It modifies the `document` object and `patch` - it gets the values by reference.
74
- * If you would like to avoid touching your values, clone them:
75
- * `jsonpatch.applyPatch(document, jsonpatch._deepClone(patch))`.
76
- *
77
- * @param document The document to patch
78
- * @param patch The patch to apply
79
- * @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.
80
- * @param mutateDocument Whether to mutate the original document or clone it before applying
81
- * @param banPrototypeModifications Whether to ban modifications to `__proto__`, defaults to `true`.
82
- * @return An array of `{newDocument, result}` after the patch
83
- */
84
- export declare function applyPatch<T>(document: T, patch: ReadonlyArray<Operation>, validateOperation?: boolean | Validator<T>, mutateDocument?: boolean, banPrototypeModifications?: boolean): PatchResult<T>;
85
- /**
86
- * Apply a single JSON Patch Operation on a JSON document.
87
- * Returns the updated document.
88
- * Suitable as a reducer.
89
- *
90
- * @param document The document to patch
91
- * @param operation The operation to apply
92
- * @return The updated document
93
- */
94
- export declare function applyReducer<T>(document: T, operation: Operation, index: number): T;
95
- /**
96
- * Validates a single operation. Called from `jsonpatch.validate`. Throws `JsonPatchError` in case of an error.
97
- * @param {object} operation - operation object (patch)
98
- * @param {number} index - index of operation in the sequence
99
- * @param {object} [document] - object where the operation is supposed to be applied
100
- * @param {string} [existingPathFragment] - comes along with `document`
101
- */
102
- export declare function validator(operation: Operation, index: number, document?: any, existingPathFragment?: string): void;
103
- /**
104
- * Validates a sequence of operations. If `document` parameter is provided, the sequence is additionally validated against the object document.
105
- * If error is encountered, returns a JsonPatchError object
106
- * @param sequence
107
- * @param document
108
- * @returns {JsonPatchError|undefined}
109
- */
110
- export declare function validate<T>(sequence: ReadonlyArray<Operation>, document?: T, externalValidator?: Validator<T>): PatchError;
111
- export declare function _areEquals(a: any, b: any): boolean;
@@ -1,459 +0,0 @@
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
- }