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