@metamask-previews/json-rpc-engine 10.1.1-preview-63ea58af → 10.1.1-preview-0458fe94

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 (116) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +647 -124
  3. package/dist/JsonRpcEngine.cjs +11 -13
  4. package/dist/JsonRpcEngine.cjs.map +1 -1
  5. package/dist/JsonRpcEngine.d.cts +18 -0
  6. package/dist/JsonRpcEngine.d.cts.map +1 -1
  7. package/dist/JsonRpcEngine.d.mts +18 -0
  8. package/dist/JsonRpcEngine.d.mts.map +1 -1
  9. package/dist/JsonRpcEngine.mjs +11 -13
  10. package/dist/JsonRpcEngine.mjs.map +1 -1
  11. package/dist/asV2Middleware.cjs +48 -0
  12. package/dist/asV2Middleware.cjs.map +1 -0
  13. package/dist/asV2Middleware.d.cts +11 -0
  14. package/dist/asV2Middleware.d.cts.map +1 -0
  15. package/dist/asV2Middleware.d.mts +11 -0
  16. package/dist/asV2Middleware.d.mts.map +1 -0
  17. package/dist/asV2Middleware.mjs +44 -0
  18. package/dist/asV2Middleware.mjs.map +1 -0
  19. package/dist/createAsyncMiddleware.cjs +1 -0
  20. package/dist/createAsyncMiddleware.cjs.map +1 -1
  21. package/dist/createAsyncMiddleware.d.cts +1 -0
  22. package/dist/createAsyncMiddleware.d.cts.map +1 -1
  23. package/dist/createAsyncMiddleware.d.mts +1 -0
  24. package/dist/createAsyncMiddleware.d.mts.map +1 -1
  25. package/dist/createAsyncMiddleware.mjs +1 -0
  26. package/dist/createAsyncMiddleware.mjs.map +1 -1
  27. package/dist/createScaffoldMiddleware.cjs +1 -0
  28. package/dist/createScaffoldMiddleware.cjs.map +1 -1
  29. package/dist/createScaffoldMiddleware.d.cts +1 -0
  30. package/dist/createScaffoldMiddleware.d.cts.map +1 -1
  31. package/dist/createScaffoldMiddleware.d.mts +1 -0
  32. package/dist/createScaffoldMiddleware.d.mts.map +1 -1
  33. package/dist/createScaffoldMiddleware.mjs +1 -0
  34. package/dist/createScaffoldMiddleware.mjs.map +1 -1
  35. package/dist/idRemapMiddleware.cjs +1 -0
  36. package/dist/idRemapMiddleware.cjs.map +1 -1
  37. package/dist/idRemapMiddleware.d.cts +1 -0
  38. package/dist/idRemapMiddleware.d.cts.map +1 -1
  39. package/dist/idRemapMiddleware.d.mts +1 -0
  40. package/dist/idRemapMiddleware.d.mts.map +1 -1
  41. package/dist/idRemapMiddleware.mjs +1 -0
  42. package/dist/idRemapMiddleware.mjs.map +1 -1
  43. package/dist/index.cjs +3 -1
  44. package/dist/index.cjs.map +1 -1
  45. package/dist/index.d.cts +1 -0
  46. package/dist/index.d.cts.map +1 -1
  47. package/dist/index.d.mts +1 -0
  48. package/dist/index.d.mts.map +1 -1
  49. package/dist/index.mjs +1 -0
  50. package/dist/index.mjs.map +1 -1
  51. package/dist/mergeMiddleware.cjs +1 -0
  52. package/dist/mergeMiddleware.cjs.map +1 -1
  53. package/dist/mergeMiddleware.d.cts +1 -0
  54. package/dist/mergeMiddleware.d.cts.map +1 -1
  55. package/dist/mergeMiddleware.d.mts +1 -0
  56. package/dist/mergeMiddleware.d.mts.map +1 -1
  57. package/dist/mergeMiddleware.mjs +1 -0
  58. package/dist/mergeMiddleware.mjs.map +1 -1
  59. package/dist/v2/JsonRpcEngineV2.cjs +213 -0
  60. package/dist/v2/JsonRpcEngineV2.cjs.map +1 -0
  61. package/dist/v2/JsonRpcEngineV2.d.cts +122 -0
  62. package/dist/v2/JsonRpcEngineV2.d.cts.map +1 -0
  63. package/dist/v2/JsonRpcEngineV2.d.mts +122 -0
  64. package/dist/v2/JsonRpcEngineV2.d.mts.map +1 -0
  65. package/dist/v2/JsonRpcEngineV2.mjs +213 -0
  66. package/dist/v2/JsonRpcEngineV2.mjs.map +1 -0
  67. package/dist/v2/JsonRpcServer.cjs +162 -0
  68. package/dist/v2/JsonRpcServer.cjs.map +1 -0
  69. package/dist/v2/JsonRpcServer.d.cts +85 -0
  70. package/dist/v2/JsonRpcServer.d.cts.map +1 -0
  71. package/dist/v2/JsonRpcServer.d.mts +85 -0
  72. package/dist/v2/JsonRpcServer.d.mts.map +1 -0
  73. package/dist/v2/JsonRpcServer.mjs +158 -0
  74. package/dist/v2/JsonRpcServer.mjs.map +1 -0
  75. package/dist/v2/MiddlewareContext.cjs +66 -0
  76. package/dist/v2/MiddlewareContext.cjs.map +1 -0
  77. package/dist/v2/MiddlewareContext.d.cts +95 -0
  78. package/dist/v2/MiddlewareContext.d.cts.map +1 -0
  79. package/dist/v2/MiddlewareContext.d.mts +95 -0
  80. package/dist/v2/MiddlewareContext.d.mts.map +1 -0
  81. package/dist/v2/MiddlewareContext.mjs +62 -0
  82. package/dist/v2/MiddlewareContext.mjs.map +1 -0
  83. package/dist/v2/asLegacyMiddleware.cjs +39 -0
  84. package/dist/v2/asLegacyMiddleware.cjs.map +1 -0
  85. package/dist/v2/asLegacyMiddleware.d.cts +11 -0
  86. package/dist/v2/asLegacyMiddleware.d.cts.map +1 -0
  87. package/dist/v2/asLegacyMiddleware.d.mts +11 -0
  88. package/dist/v2/asLegacyMiddleware.d.mts.map +1 -0
  89. package/dist/v2/asLegacyMiddleware.mjs +35 -0
  90. package/dist/v2/asLegacyMiddleware.mjs.map +1 -0
  91. package/dist/v2/compatibility-utils.cjs +151 -0
  92. package/dist/v2/compatibility-utils.cjs.map +1 -0
  93. package/dist/v2/compatibility-utils.d.cts +75 -0
  94. package/dist/v2/compatibility-utils.d.cts.map +1 -0
  95. package/dist/v2/compatibility-utils.d.mts +75 -0
  96. package/dist/v2/compatibility-utils.d.mts.map +1 -0
  97. package/dist/v2/compatibility-utils.mjs +142 -0
  98. package/dist/v2/compatibility-utils.mjs.map +1 -0
  99. package/dist/v2/index.cjs +29 -0
  100. package/dist/v2/index.cjs.map +1 -0
  101. package/dist/v2/index.d.cts +8 -0
  102. package/dist/v2/index.d.cts.map +1 -0
  103. package/dist/v2/index.d.mts +8 -0
  104. package/dist/v2/index.d.mts.map +1 -0
  105. package/dist/v2/index.mjs +6 -0
  106. package/dist/v2/index.mjs.map +1 -0
  107. package/dist/v2/utils.cjs +41 -0
  108. package/dist/v2/utils.cjs.map +1 -0
  109. package/dist/v2/utils.d.cts +35 -0
  110. package/dist/v2/utils.d.cts.map +1 -0
  111. package/dist/v2/utils.d.mts +35 -0
  112. package/dist/v2/utils.d.mts.map +1 -0
  113. package/dist/v2/utils.mjs +34 -0
  114. package/dist/v2/utils.mjs.map +1 -0
  115. package/package.json +17 -3
  116. package/v2.js +3 -0
@@ -0,0 +1,35 @@
1
+ import { deepClone, fromLegacyRequest, makeContext, propagateToRequest } from "./compatibility-utils.mjs";
2
+ import { createAsyncMiddleware } from "../index.mjs";
3
+ /**
4
+ * Convert a {@link JsonRpcEngineV2} into a legacy middleware.
5
+ *
6
+ * @param engine - The engine to convert.
7
+ * @returns The legacy middleware.
8
+ */
9
+ export function asLegacyMiddleware(engine) {
10
+ const middleware = engine.asMiddleware();
11
+ return createAsyncMiddleware(async (req, res, next) => {
12
+ const request = fromLegacyRequest(req);
13
+ const context = makeContext(req);
14
+ let modifiedRequest;
15
+ const result = await middleware({
16
+ request,
17
+ context,
18
+ next: (finalRequest) => {
19
+ modifiedRequest = finalRequest;
20
+ return Promise.resolve(undefined);
21
+ },
22
+ });
23
+ if (modifiedRequest !== undefined && modifiedRequest !== request) {
24
+ Object.assign(req, deepClone(modifiedRequest));
25
+ }
26
+ propagateToRequest(req, context);
27
+ if (result !== undefined) {
28
+ // Unclear why the `as unknown` is needed here, but the cast is safe.
29
+ res.result = deepClone(result);
30
+ return undefined;
31
+ }
32
+ return next();
33
+ });
34
+ }
35
+ //# sourceMappingURL=asLegacyMiddleware.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"asLegacyMiddleware.mjs","sourceRoot":"","sources":["../../src/v2/asLegacyMiddleware.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,SAAS,EACT,iBAAiB,EACjB,WAAW,EACX,kBAAkB,EACnB,kCAA8B;AAE/B,OAAO,EAAE,qBAAqB,EAAE,qBAAW;AAG3C;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAIhC,MAAgC;IAEhC,MAAM,UAAU,GAAG,MAAM,CAAC,YAAY,EAAE,CAAC;IACzC,OAAO,qBAAqB,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QACpD,MAAM,OAAO,GAAG,iBAAiB,CAAC,GAAc,CAAC,CAAC;QAClD,MAAM,OAAO,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC;QACjC,IAAI,eAAoC,CAAC;QAEzC,MAAM,MAAM,GAAG,MAAM,UAAU,CAAC;YAC9B,OAAO;YACP,OAAO;YACP,IAAI,EAAE,CAAC,YAAY,EAAE,EAAE;gBACrB,eAAe,GAAG,YAAY,CAAC;gBAC/B,OAAO,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;YACpC,CAAC;SACF,CAAC,CAAC;QAEH,IAAI,eAAe,KAAK,SAAS,IAAI,eAAe,KAAK,OAAO,EAAE;YAChE,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,SAAS,CAAC,eAAe,CAAC,CAAC,CAAC;SAChD;QACD,kBAAkB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QAEjC,IAAI,MAAM,KAAK,SAAS,EAAE;YACxB,qEAAqE;YACrE,GAAG,CAAC,MAAM,GAAG,SAAS,CAAC,MAAM,CAAyC,CAAC;YACvE,OAAO,SAAS,CAAC;SAClB;QACD,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type { JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\n\nimport {\n deepClone,\n fromLegacyRequest,\n makeContext,\n propagateToRequest,\n} from './compatibility-utils';\nimport type { JsonRpcEngineV2, ResultConstraint } from './JsonRpcEngineV2';\nimport { createAsyncMiddleware } from '..';\nimport type { JsonRpcMiddleware as LegacyMiddleware } from '..';\n\n/**\n * Convert a {@link JsonRpcEngineV2} into a legacy middleware.\n *\n * @param engine - The engine to convert.\n * @returns The legacy middleware.\n */\nexport function asLegacyMiddleware<\n Params extends JsonRpcParams,\n Request extends JsonRpcRequest<Params>,\n>(\n engine: JsonRpcEngineV2<Request>,\n): LegacyMiddleware<Params, ResultConstraint<Request>> {\n const middleware = engine.asMiddleware();\n return createAsyncMiddleware(async (req, res, next) => {\n const request = fromLegacyRequest(req as Request);\n const context = makeContext(req);\n let modifiedRequest: Request | undefined;\n\n const result = await middleware({\n request,\n context,\n next: (finalRequest) => {\n modifiedRequest = finalRequest;\n return Promise.resolve(undefined);\n },\n });\n\n if (modifiedRequest !== undefined && modifiedRequest !== request) {\n Object.assign(req, deepClone(modifiedRequest));\n }\n propagateToRequest(req, context);\n\n if (result !== undefined) {\n // Unclear why the `as unknown` is needed here, but the cast is safe.\n res.result = deepClone(result) as unknown as ResultConstraint<Request>;\n return undefined;\n }\n return next();\n });\n}\n"]}
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.unserializeError = exports.propagateToRequest = exports.propagateToContext = exports.makeContext = exports.fromLegacyRequest = exports.requestProps = exports.deepClone = void 0;
4
+ const rpc_errors_1 = require("@metamask/rpc-errors");
5
+ const utils_1 = require("@metamask/utils");
6
+ // ATTN: We must NOT use 'klona/full' here because it freezes properties on the clone.
7
+ const klona_1 = require("klona");
8
+ const MiddlewareContext_1 = require("./MiddlewareContext.cjs");
9
+ const utils_2 = require("./utils.cjs");
10
+ // Legacy engine compatibility utils
11
+ /**
12
+ * Create a deep clone of a value as follows:
13
+ * - Assumes acyclical objects
14
+ * - Does not copy property descriptors (i.e. uses mutable defaults)
15
+ * - Ignores non-enumerable properties
16
+ * - Ignores getters and setters
17
+ *
18
+ * @throws If the value is an object with a circular reference.
19
+ * @param value - The value to clone.
20
+ * @returns The cloned value.
21
+ */
22
+ const deepClone = (value) => (0, klona_1.klona)(value);
23
+ exports.deepClone = deepClone;
24
+ /**
25
+ * Standard JSON-RPC request properties.
26
+ */
27
+ exports.requestProps = ['jsonrpc', 'method', 'params', 'id'];
28
+ /**
29
+ * Make a JSON-RPC request from a legacy request. Clones the params to avoid
30
+ * freezing them, which could cause errors in an involved legacy engine.
31
+ *
32
+ * @param req - The legacy request to make a request from.
33
+ * @returns The JSON-RPC request.
34
+ */
35
+ function fromLegacyRequest(req) {
36
+ const request = {
37
+ jsonrpc: '2.0',
38
+ method: req.method,
39
+ };
40
+ request.id = req.id;
41
+ if ((0, utils_1.hasProperty)(req, 'params') && req.params !== undefined) {
42
+ request.params = (0, exports.deepClone)(req.params);
43
+ }
44
+ return request;
45
+ }
46
+ exports.fromLegacyRequest = fromLegacyRequest;
47
+ /**
48
+ * Make a middleware context from a legacy request by copying over all non-JSON-RPC
49
+ * properties from the request to the context object.
50
+ *
51
+ * @param req - The legacy request to make a context from.
52
+ * @returns The middleware context.
53
+ */
54
+ function makeContext(req) {
55
+ const context = new MiddlewareContext_1.MiddlewareContext();
56
+ propagateToContext(req, context);
57
+ return context;
58
+ }
59
+ exports.makeContext = makeContext;
60
+ /**
61
+ * Copies non-JSON-RPC string properties from the request to the context.
62
+ *
63
+ * For compatibility with our problematic practice of appending non-standard
64
+ * fields to requests for inter-middleware communication in the legacy engine.
65
+ *
66
+ * **ATTN:** Only string properties that do not already exist in the context
67
+ * are copied.
68
+ *
69
+ * @param req - The request to propagate the context from.
70
+ * @param context - The context to propagate to.
71
+ */
72
+ function propagateToContext(req, context) {
73
+ Object.keys(req)
74
+ .filter((key) => typeof key === 'string' &&
75
+ !exports.requestProps.includes(key) &&
76
+ !context.has(key))
77
+ .forEach((key) => {
78
+ context.set(key, req[key]);
79
+ });
80
+ }
81
+ exports.propagateToContext = propagateToContext;
82
+ /**
83
+ * Copies non-JSON-RPC string properties from the context to the request.
84
+ *
85
+ * For compatibility with our problematic practice of appending non-standard
86
+ * fields to requests for inter-middleware communication in the legacy engine.
87
+ *
88
+ * **ATTN:** Only string properties are copied.
89
+ *
90
+ * @param req - The request to propagate the context to.
91
+ * @param context - The context to propagate from.
92
+ */
93
+ function propagateToRequest(req, context) {
94
+ Array.from(context.keys())
95
+ .filter(((key) => typeof key === 'string' && !exports.requestProps.includes(key)))
96
+ .forEach((key) => {
97
+ req[key] = context.get(key);
98
+ });
99
+ }
100
+ exports.propagateToRequest = propagateToRequest;
101
+ /**
102
+ * Unserialize an error from a thrown value. Creates a {@link JsonRpcError} if
103
+ * the thrown value is an object with a `code` property. Otherwise, creates a
104
+ * plain {@link Error}.
105
+ *
106
+ * @param thrown - The thrown value to unserialize.
107
+ * @returns The unserialized error.
108
+ */
109
+ function unserializeError(thrown) {
110
+ // @ts-expect-error - New, but preferred if available.
111
+ // See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/isError
112
+ if (typeof Error.isError === 'function' && Error.isError(thrown)) {
113
+ return thrown;
114
+ }
115
+ // Unlike Error.isError, instanceof does not work for Errors from other realms.
116
+ if (thrown instanceof Error) {
117
+ return thrown;
118
+ }
119
+ if (typeof thrown === 'string') {
120
+ return new Error(thrown);
121
+ }
122
+ if (!(0, utils_1.isObject)(thrown)) {
123
+ return new Error(`Unknown error: ${(0, utils_2.stringify)(thrown)}`);
124
+ }
125
+ const code = typeof thrown.code === 'number' && Number.isInteger(thrown.code)
126
+ ? thrown.code
127
+ : undefined;
128
+ let message = 'Unknown error';
129
+ if (typeof thrown.message === 'string') {
130
+ message = thrown.message;
131
+ }
132
+ else if (typeof code === 'number') {
133
+ message = (0, rpc_errors_1.getMessageFromCode)(code, message);
134
+ }
135
+ const { stack, cause, data } = thrown;
136
+ const error = code === undefined
137
+ ? // Jest complains if we use the `@ts-expect-error` directive here.
138
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
139
+ // @ts-ignore - Our error type is outdated.
140
+ new Error(message, { cause })
141
+ : new rpc_errors_1.JsonRpcError(code, message, {
142
+ ...((0, utils_1.isObject)(data) ? data : undefined),
143
+ cause,
144
+ });
145
+ if (typeof stack === 'string') {
146
+ error.stack = stack;
147
+ }
148
+ return error;
149
+ }
150
+ exports.unserializeError = unserializeError;
151
+ //# sourceMappingURL=compatibility-utils.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compatibility-utils.cjs","sourceRoot":"","sources":["../../src/v2/compatibility-utils.ts"],"names":[],"mappings":";;;AAAA,qDAAwE;AAExE,2CAAwD;AACxD,sFAAsF;AACtF,iCAA8B;AAE9B,+DAAwD;AACxD,uCAAyD;AAEzD,oCAAoC;AAEpC;;;;;;;;;;GAUG;AACI,MAAM,SAAS,GAAG,CAAI,KAAQ,EAAiB,EAAE,CACtD,IAAA,aAAK,EAAC,KAAK,CAAkB,CAAC;AADnB,QAAA,SAAS,aACU;AAWhC;;GAEG;AACU,QAAA,YAAY,GAAG,CAAC,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;AAElE;;;;;;GAMG;AACH,SAAgB,iBAAiB,CAC/B,GAAY;IAEZ,MAAM,OAAO,GAAG;QACd,OAAO,EAAE,KAAc;QACvB,MAAM,EAAE,GAAG,CAAC,MAAM;KACC,CAAC;IACtB,OAAO,CAAC,EAAE,GAAG,GAAG,CAAC,EAAE,CAAC;IACpB,IAAI,IAAA,mBAAW,EAAC,GAAG,EAAE,QAAQ,CAAC,IAAI,GAAG,CAAC,MAAM,KAAK,SAAS,EAAE;QAC1D,OAAO,CAAC,MAAM,GAAG,IAAA,iBAAS,EAAC,GAAG,CAAC,MAAM,CAAC,CAAC;KACxC;IACD,OAAO,OAAkB,CAAC;AAC5B,CAAC;AAZD,8CAYC;AAED;;;;;;GAMG;AACH,SAAgB,WAAW,CACzB,GAAY;IAEZ,MAAM,OAAO,GAAG,IAAI,qCAAiB,EAAE,CAAC;IACxC,kBAAkB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;IACjC,OAAO,OAAO,CAAC;AACjB,CAAC;AAND,kC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ageFromCode, JsonRpcError } from '@metamask/rpc-errors';\nimport type { Json } from '@metamask/utils';\nimport { hasProperty, isObject } from '@metamask/utils';\n// ATTN: We must NOT use 'klona/full' here because it freezes properties on the clone.\nimport { klona } from 'klona';\n\nimport { MiddlewareContext } from './MiddlewareContext';\nimport { stringify, type JsonRpcRequest } from './utils';\n\n// Legacy engine compatibility utils\n\n/**\n * Create a deep clone of a value as follows:\n * - Assumes acyclical objects\n * - Does not copy property descriptors (i.e. uses mutable defaults)\n * - Ignores non-enumerable properties\n * - Ignores getters and setters\n *\n * @throws If the value is an object with a circular reference.\n * @param value - The value to clone.\n * @returns The cloned value.\n */\nexport const deepClone = <T>(value: T): DeepCloned<T> =>\n klona(value) as DeepCloned<T>;\n\n// Matching the default implementation of klona, this type:\n// - Removes readonly modifiers\n// - Excludes non-enumerable / symbol properties\ntype DeepCloned<T> = T extends readonly (infer U)[]\n ? DeepCloned<U>[]\n : T extends object\n ? { -readonly [K in keyof T & (string | number)]: DeepCloned<T[K]> }\n : T;\n\n/**\n * Standard JSON-RPC request properties.\n */\nexport const requestProps = ['jsonrpc', 'method', 'params', 'id'];\n\n/**\n * Make a JSON-RPC request from a legacy request. Clones the params to avoid\n * freezing them, which could cause errors in an involved legacy engine.\n *\n * @param req - The legacy request to make a request from.\n * @returns The JSON-RPC request.\n */\nexport function fromLegacyRequest<Request extends JsonRpcRequest>(\n req: Request,\n): Request {\n const request = {\n jsonrpc: '2.0' as const,\n method: req.method,\n } as Partial<Request>;\n request.id = req.id;\n if (hasProperty(req, 'params') && req.params !== undefined) {\n request.params = deepClone(req.params);\n }\n return request as Request;\n}\n\n/**\n * Make a middleware context from a legacy request by copying over all non-JSON-RPC\n * properties from the request to the context object.\n *\n * @param req - The legacy request to make a context from.\n * @returns The middleware context.\n */\nexport function makeContext<Request extends Record<string | symbol, unknown>>(\n req: Request,\n): MiddlewareContext {\n const context = new MiddlewareContext();\n propagateToContext(req, context);\n return context;\n}\n\n/**\n * Copies non-JSON-RPC string properties from the request to the context.\n *\n * For compatibility with our problematic practice of appending non-standard\n * fields to requests for inter-middleware communication in the legacy engine.\n *\n * **ATTN:** Only string properties that do not already exist in the context\n * are copied.\n *\n * @param req - The request to propagate the context from.\n * @param context - The context to propagate to.\n */\nexport function propagateToContext(\n req: Record<string, unknown>,\n context: MiddlewareContext<Record<string, unknown>>,\n) {\n Object.keys(req)\n .filter(\n (key) =>\n typeof key === 'string' &&\n !requestProps.includes(key) &&\n !context.has(key),\n )\n .forEach((key) => {\n context.set(key, req[key]);\n });\n}\n\n/**\n * Copies non-JSON-RPC string properties from the context to the request.\n *\n * For compatibility with our problematic practice of appending non-standard\n * fields to requests for inter-middleware communication in the legacy engine.\n *\n * **ATTN:** Only string properties are copied.\n *\n * @param req - The request to propagate the context to.\n * @param context - The context to propagate from.\n */\nexport function propagateToRequest(\n req: Record<string, unknown>,\n context: MiddlewareContext,\n) {\n Array.from(context.keys())\n .filter(\n ((key) => typeof key === 'string' && !requestProps.includes(key)) as (\n value: unknown,\n ) => value is string,\n )\n .forEach((key) => {\n req[key] = context.get(key);\n });\n}\n\n/**\n * Unserialize an error from a thrown value. Creates a {@link JsonRpcError} if\n * the thrown value is an object with a `code` property. Otherwise, creates a\n * plain {@link Error}.\n *\n * @param thrown - The thrown value to unserialize.\n * @returns The unserialized error.\n */\nexport function unserializeError(thrown: unknown): Error | JsonRpcError<Json> {\n // @ts-expect-error - New, but preferred if available.\n // See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/isError\n if (typeof Error.isError === 'function' && Error.isError(thrown)) {\n return thrown as Error;\n }\n // Unlike Error.isError, instanceof does not work for Errors from other realms.\n if (thrown instanceof Error) {\n return thrown;\n }\n if (typeof thrown === 'string') {\n return new Error(thrown);\n }\n if (!isObject(thrown)) {\n return new Error(`Unknown error: ${stringify(thrown)}`);\n }\n\n const code =\n typeof thrown.code === 'number' && Number.isInteger(thrown.code)\n ? thrown.code\n : undefined;\n\n let message = 'Unknown error';\n if (typeof thrown.message === 'string') {\n message = thrown.message;\n } else if (typeof code === 'number') {\n message = getMessageFromCode(code, message);\n }\n\n const { stack, cause, data } = thrown;\n\n const error =\n code === undefined\n ? // Jest complains if we use the `@ts-expect-error` directive here.\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore - Our error type is outdated.\n new Error(message, { cause })\n : new JsonRpcError(code, message, {\n ...(isObject(data) ? data : undefined),\n cause,\n });\n\n if (typeof stack === 'string') {\n error.stack = stack;\n }\n\n return error;\n}\n"]}
@@ -0,0 +1,75 @@
1
+ import { JsonRpcError } from "@metamask/rpc-errors";
2
+ import type { Json } from "@metamask/utils";
3
+ import { MiddlewareContext } from "./MiddlewareContext.cjs";
4
+ import { type JsonRpcRequest } from "./utils.cjs";
5
+ /**
6
+ * Create a deep clone of a value as follows:
7
+ * - Assumes acyclical objects
8
+ * - Does not copy property descriptors (i.e. uses mutable defaults)
9
+ * - Ignores non-enumerable properties
10
+ * - Ignores getters and setters
11
+ *
12
+ * @throws If the value is an object with a circular reference.
13
+ * @param value - The value to clone.
14
+ * @returns The cloned value.
15
+ */
16
+ export declare const deepClone: <T>(value: T) => DeepCloned<T>;
17
+ type DeepCloned<T> = T extends readonly (infer U)[] ? DeepCloned<U>[] : T extends object ? {
18
+ -readonly [K in keyof T & (string | number)]: DeepCloned<T[K]>;
19
+ } : T;
20
+ /**
21
+ * Standard JSON-RPC request properties.
22
+ */
23
+ export declare const requestProps: string[];
24
+ /**
25
+ * Make a JSON-RPC request from a legacy request. Clones the params to avoid
26
+ * freezing them, which could cause errors in an involved legacy engine.
27
+ *
28
+ * @param req - The legacy request to make a request from.
29
+ * @returns The JSON-RPC request.
30
+ */
31
+ export declare function fromLegacyRequest<Request extends JsonRpcRequest>(req: Request): Request;
32
+ /**
33
+ * Make a middleware context from a legacy request by copying over all non-JSON-RPC
34
+ * properties from the request to the context object.
35
+ *
36
+ * @param req - The legacy request to make a context from.
37
+ * @returns The middleware context.
38
+ */
39
+ export declare function makeContext<Request extends Record<string | symbol, unknown>>(req: Request): MiddlewareContext;
40
+ /**
41
+ * Copies non-JSON-RPC string properties from the request to the context.
42
+ *
43
+ * For compatibility with our problematic practice of appending non-standard
44
+ * fields to requests for inter-middleware communication in the legacy engine.
45
+ *
46
+ * **ATTN:** Only string properties that do not already exist in the context
47
+ * are copied.
48
+ *
49
+ * @param req - The request to propagate the context from.
50
+ * @param context - The context to propagate to.
51
+ */
52
+ export declare function propagateToContext(req: Record<string, unknown>, context: MiddlewareContext<Record<string, unknown>>): void;
53
+ /**
54
+ * Copies non-JSON-RPC string properties from the context to the request.
55
+ *
56
+ * For compatibility with our problematic practice of appending non-standard
57
+ * fields to requests for inter-middleware communication in the legacy engine.
58
+ *
59
+ * **ATTN:** Only string properties are copied.
60
+ *
61
+ * @param req - The request to propagate the context to.
62
+ * @param context - The context to propagate from.
63
+ */
64
+ export declare function propagateToRequest(req: Record<string, unknown>, context: MiddlewareContext): void;
65
+ /**
66
+ * Unserialize an error from a thrown value. Creates a {@link JsonRpcError} if
67
+ * the thrown value is an object with a `code` property. Otherwise, creates a
68
+ * plain {@link Error}.
69
+ *
70
+ * @param thrown - The thrown value to unserialize.
71
+ * @returns The unserialized error.
72
+ */
73
+ export declare function unserializeError(thrown: unknown): Error | JsonRpcError<Json>;
74
+ export {};
75
+ //# sourceMappingURL=compatibility-utils.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compatibility-utils.d.cts","sourceRoot":"","sources":["../../src/v2/compatibility-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,YAAY,EAAE,6BAA6B;AACxE,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAK5C,OAAO,EAAE,iBAAiB,EAAE,gCAA4B;AACxD,OAAO,EAAa,KAAK,cAAc,EAAE,oBAAgB;AAIzD;;;;;;;;;;GAUG;AACH,eAAO,MAAM,SAAS,gCACS,CAAC;AAKhC,KAAK,UAAU,CAAC,CAAC,IAAI,CAAC,SAAS,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,GAC/C,UAAU,CAAC,CAAC,CAAC,EAAE,GACf,CAAC,SAAS,MAAM,GACd;IAAE,CAAC,UAAU,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAAE,GAClE,CAAC,CAAC;AAER;;GAEG;AACH,eAAO,MAAM,YAAY,UAAwC,CAAC;AAElE;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,SAAS,cAAc,EAC9D,GAAG,EAAE,OAAO,GACX,OAAO,CAUT;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,OAAO,SAAS,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,EAC1E,GAAG,EAAE,OAAO,GACX,iBAAiB,CAInB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,kBAAkB,CAChC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5B,OAAO,EAAE,iBAAiB,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,QAYpD;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAChC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5B,OAAO,EAAE,iBAAiB,QAW3B;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,OAAO,GAAG,KAAK,GAAG,YAAY,CAAC,IAAI,CAAC,CA+C5E"}
@@ -0,0 +1,75 @@
1
+ import { JsonRpcError } from "@metamask/rpc-errors";
2
+ import type { Json } from "@metamask/utils";
3
+ import { MiddlewareContext } from "./MiddlewareContext.mjs";
4
+ import { type JsonRpcRequest } from "./utils.mjs";
5
+ /**
6
+ * Create a deep clone of a value as follows:
7
+ * - Assumes acyclical objects
8
+ * - Does not copy property descriptors (i.e. uses mutable defaults)
9
+ * - Ignores non-enumerable properties
10
+ * - Ignores getters and setters
11
+ *
12
+ * @throws If the value is an object with a circular reference.
13
+ * @param value - The value to clone.
14
+ * @returns The cloned value.
15
+ */
16
+ export declare const deepClone: <T>(value: T) => DeepCloned<T>;
17
+ type DeepCloned<T> = T extends readonly (infer U)[] ? DeepCloned<U>[] : T extends object ? {
18
+ -readonly [K in keyof T & (string | number)]: DeepCloned<T[K]>;
19
+ } : T;
20
+ /**
21
+ * Standard JSON-RPC request properties.
22
+ */
23
+ export declare const requestProps: string[];
24
+ /**
25
+ * Make a JSON-RPC request from a legacy request. Clones the params to avoid
26
+ * freezing them, which could cause errors in an involved legacy engine.
27
+ *
28
+ * @param req - The legacy request to make a request from.
29
+ * @returns The JSON-RPC request.
30
+ */
31
+ export declare function fromLegacyRequest<Request extends JsonRpcRequest>(req: Request): Request;
32
+ /**
33
+ * Make a middleware context from a legacy request by copying over all non-JSON-RPC
34
+ * properties from the request to the context object.
35
+ *
36
+ * @param req - The legacy request to make a context from.
37
+ * @returns The middleware context.
38
+ */
39
+ export declare function makeContext<Request extends Record<string | symbol, unknown>>(req: Request): MiddlewareContext;
40
+ /**
41
+ * Copies non-JSON-RPC string properties from the request to the context.
42
+ *
43
+ * For compatibility with our problematic practice of appending non-standard
44
+ * fields to requests for inter-middleware communication in the legacy engine.
45
+ *
46
+ * **ATTN:** Only string properties that do not already exist in the context
47
+ * are copied.
48
+ *
49
+ * @param req - The request to propagate the context from.
50
+ * @param context - The context to propagate to.
51
+ */
52
+ export declare function propagateToContext(req: Record<string, unknown>, context: MiddlewareContext<Record<string, unknown>>): void;
53
+ /**
54
+ * Copies non-JSON-RPC string properties from the context to the request.
55
+ *
56
+ * For compatibility with our problematic practice of appending non-standard
57
+ * fields to requests for inter-middleware communication in the legacy engine.
58
+ *
59
+ * **ATTN:** Only string properties are copied.
60
+ *
61
+ * @param req - The request to propagate the context to.
62
+ * @param context - The context to propagate from.
63
+ */
64
+ export declare function propagateToRequest(req: Record<string, unknown>, context: MiddlewareContext): void;
65
+ /**
66
+ * Unserialize an error from a thrown value. Creates a {@link JsonRpcError} if
67
+ * the thrown value is an object with a `code` property. Otherwise, creates a
68
+ * plain {@link Error}.
69
+ *
70
+ * @param thrown - The thrown value to unserialize.
71
+ * @returns The unserialized error.
72
+ */
73
+ export declare function unserializeError(thrown: unknown): Error | JsonRpcError<Json>;
74
+ export {};
75
+ //# sourceMappingURL=compatibility-utils.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compatibility-utils.d.mts","sourceRoot":"","sources":["../../src/v2/compatibility-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,YAAY,EAAE,6BAA6B;AACxE,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAK5C,OAAO,EAAE,iBAAiB,EAAE,gCAA4B;AACxD,OAAO,EAAa,KAAK,cAAc,EAAE,oBAAgB;AAIzD;;;;;;;;;;GAUG;AACH,eAAO,MAAM,SAAS,gCACS,CAAC;AAKhC,KAAK,UAAU,CAAC,CAAC,IAAI,CAAC,SAAS,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,GAC/C,UAAU,CAAC,CAAC,CAAC,EAAE,GACf,CAAC,SAAS,MAAM,GACd;IAAE,CAAC,UAAU,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAAE,GAClE,CAAC,CAAC;AAER;;GAEG;AACH,eAAO,MAAM,YAAY,UAAwC,CAAC;AAElE;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,SAAS,cAAc,EAC9D,GAAG,EAAE,OAAO,GACX,OAAO,CAUT;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,OAAO,SAAS,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,EAC1E,GAAG,EAAE,OAAO,GACX,iBAAiB,CAInB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,kBAAkB,CAChC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5B,OAAO,EAAE,iBAAiB,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,QAYpD;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAChC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5B,OAAO,EAAE,iBAAiB,QAW3B;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,OAAO,GAAG,KAAK,GAAG,YAAY,CAAC,IAAI,CAAC,CA+C5E"}
@@ -0,0 +1,142 @@
1
+ import { getMessageFromCode, JsonRpcError } from "@metamask/rpc-errors";
2
+ import { hasProperty, isObject } from "@metamask/utils";
3
+ // ATTN: We must NOT use 'klona/full' here because it freezes properties on the clone.
4
+ import { klona } from "klona";
5
+ import { MiddlewareContext } from "./MiddlewareContext.mjs";
6
+ import { stringify } from "./utils.mjs";
7
+ // Legacy engine compatibility utils
8
+ /**
9
+ * Create a deep clone of a value as follows:
10
+ * - Assumes acyclical objects
11
+ * - Does not copy property descriptors (i.e. uses mutable defaults)
12
+ * - Ignores non-enumerable properties
13
+ * - Ignores getters and setters
14
+ *
15
+ * @throws If the value is an object with a circular reference.
16
+ * @param value - The value to clone.
17
+ * @returns The cloned value.
18
+ */
19
+ export const deepClone = (value) => klona(value);
20
+ /**
21
+ * Standard JSON-RPC request properties.
22
+ */
23
+ export const requestProps = ['jsonrpc', 'method', 'params', 'id'];
24
+ /**
25
+ * Make a JSON-RPC request from a legacy request. Clones the params to avoid
26
+ * freezing them, which could cause errors in an involved legacy engine.
27
+ *
28
+ * @param req - The legacy request to make a request from.
29
+ * @returns The JSON-RPC request.
30
+ */
31
+ export function fromLegacyRequest(req) {
32
+ const request = {
33
+ jsonrpc: '2.0',
34
+ method: req.method,
35
+ };
36
+ request.id = req.id;
37
+ if (hasProperty(req, 'params') && req.params !== undefined) {
38
+ request.params = deepClone(req.params);
39
+ }
40
+ return request;
41
+ }
42
+ /**
43
+ * Make a middleware context from a legacy request by copying over all non-JSON-RPC
44
+ * properties from the request to the context object.
45
+ *
46
+ * @param req - The legacy request to make a context from.
47
+ * @returns The middleware context.
48
+ */
49
+ export function makeContext(req) {
50
+ const context = new MiddlewareContext();
51
+ propagateToContext(req, context);
52
+ return context;
53
+ }
54
+ /**
55
+ * Copies non-JSON-RPC string properties from the request to the context.
56
+ *
57
+ * For compatibility with our problematic practice of appending non-standard
58
+ * fields to requests for inter-middleware communication in the legacy engine.
59
+ *
60
+ * **ATTN:** Only string properties that do not already exist in the context
61
+ * are copied.
62
+ *
63
+ * @param req - The request to propagate the context from.
64
+ * @param context - The context to propagate to.
65
+ */
66
+ export function propagateToContext(req, context) {
67
+ Object.keys(req)
68
+ .filter((key) => typeof key === 'string' &&
69
+ !requestProps.includes(key) &&
70
+ !context.has(key))
71
+ .forEach((key) => {
72
+ context.set(key, req[key]);
73
+ });
74
+ }
75
+ /**
76
+ * Copies non-JSON-RPC string properties from the context to the request.
77
+ *
78
+ * For compatibility with our problematic practice of appending non-standard
79
+ * fields to requests for inter-middleware communication in the legacy engine.
80
+ *
81
+ * **ATTN:** Only string properties are copied.
82
+ *
83
+ * @param req - The request to propagate the context to.
84
+ * @param context - The context to propagate from.
85
+ */
86
+ export function propagateToRequest(req, context) {
87
+ Array.from(context.keys())
88
+ .filter(((key) => typeof key === 'string' && !requestProps.includes(key)))
89
+ .forEach((key) => {
90
+ req[key] = context.get(key);
91
+ });
92
+ }
93
+ /**
94
+ * Unserialize an error from a thrown value. Creates a {@link JsonRpcError} if
95
+ * the thrown value is an object with a `code` property. Otherwise, creates a
96
+ * plain {@link Error}.
97
+ *
98
+ * @param thrown - The thrown value to unserialize.
99
+ * @returns The unserialized error.
100
+ */
101
+ export function unserializeError(thrown) {
102
+ // @ts-expect-error - New, but preferred if available.
103
+ // See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/isError
104
+ if (typeof Error.isError === 'function' && Error.isError(thrown)) {
105
+ return thrown;
106
+ }
107
+ // Unlike Error.isError, instanceof does not work for Errors from other realms.
108
+ if (thrown instanceof Error) {
109
+ return thrown;
110
+ }
111
+ if (typeof thrown === 'string') {
112
+ return new Error(thrown);
113
+ }
114
+ if (!isObject(thrown)) {
115
+ return new Error(`Unknown error: ${stringify(thrown)}`);
116
+ }
117
+ const code = typeof thrown.code === 'number' && Number.isInteger(thrown.code)
118
+ ? thrown.code
119
+ : undefined;
120
+ let message = 'Unknown error';
121
+ if (typeof thrown.message === 'string') {
122
+ message = thrown.message;
123
+ }
124
+ else if (typeof code === 'number') {
125
+ message = getMessageFromCode(code, message);
126
+ }
127
+ const { stack, cause, data } = thrown;
128
+ const error = code === undefined
129
+ ? // Jest complains if we use the `@ts-expect-error` directive here.
130
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
131
+ // @ts-ignore - Our error type is outdated.
132
+ new Error(message, { cause })
133
+ : new JsonRpcError(code, message, {
134
+ ...(isObject(data) ? data : undefined),
135
+ cause,
136
+ });
137
+ if (typeof stack === 'string') {
138
+ error.stack = stack;
139
+ }
140
+ return error;
141
+ }
142
+ //# sourceMappingURL=compatibility-utils.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compatibility-utils.mjs","sourceRoot":"","sources":["../../src/v2/compatibility-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,YAAY,EAAE,6BAA6B;AAExE,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,wBAAwB;AACxD,sFAAsF;AACtF,OAAO,EAAE,KAAK,EAAE,cAAc;AAE9B,OAAO,EAAE,iBAAiB,EAAE,gCAA4B;AACxD,OAAO,EAAE,SAAS,EAAuB,oBAAgB;AAEzD,oCAAoC;AAEpC;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,CAAI,KAAQ,EAAiB,EAAE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AO,CAAC;KAC1B;SAAM,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE;QACnC,OAAO,GAAG,kBAAkB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;KAC7C;IAED,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,MAAM,CAAC;IAEtC,MAAM,KAAK,GACT,IAAI,KAAK,SAAS;QAChB,CAAC,CAAC,kEAAkE;YAClE,6DAA6D;YAC7D,2CAA2C;YAC3C,IAAI,KAAK,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,CAAC;QAC/B,CAAC,CAAC,IAAI,YAAY,CAAC,IAAI,EAAE,OAAO,EAAE;YAC9B,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;YACtC,KAAK;SACN,CAAC,CAAC;IAET,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;QAC7B,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC;KACrB;IAED,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import { getMessageFromCode, JsonRpcError } from '@metamask/rpc-errors';\nimport type { Json } from '@metamask/utils';\nimport { hasProperty, isObject } from '@metamask/utils';\n// ATTN: We must NOT use 'klona/full' here because it freezes properties on the clone.\nimport { klona } from 'klona';\n\nimport { MiddlewareContext } from './MiddlewareContext';\nimport { stringify, type JsonRpcRequest } from './utils';\n\n// Legacy engine compatibility utils\n\n/**\n * Create a deep clone of a value as follows:\n * - Assumes acyclical objects\n * - Does not copy property descriptors (i.e. uses mutable defaults)\n * - Ignores non-enumerable properties\n * - Ignores getters and setters\n *\n * @throws If the value is an object with a circular reference.\n * @param value - The value to clone.\n * @returns The cloned value.\n */\nexport const deepClone = <T>(value: T): DeepCloned<T> =>\n klona(value) as DeepCloned<T>;\n\n// Matching the default implementation of klona, this type:\n// - Removes readonly modifiers\n// - Excludes non-enumerable / symbol properties\ntype DeepCloned<T> = T extends readonly (infer U)[]\n ? DeepCloned<U>[]\n : T extends object\n ? { -readonly [K in keyof T & (string | number)]: DeepCloned<T[K]> }\n : T;\n\n/**\n * Standard JSON-RPC request properties.\n */\nexport const requestProps = ['jsonrpc', 'method', 'params', 'id'];\n\n/**\n * Make a JSON-RPC request from a legacy request. Clones the params to avoid\n * freezing them, which could cause errors in an involved legacy engine.\n *\n * @param req - The legacy request to make a request from.\n * @returns The JSON-RPC request.\n */\nexport function fromLegacyRequest<Request extends JsonRpcRequest>(\n req: Request,\n): Request {\n const request = {\n jsonrpc: '2.0' as const,\n method: req.method,\n } as Partial<Request>;\n request.id = req.id;\n if (hasProperty(req, 'params') && req.params !== undefined) {\n request.params = deepClone(req.params);\n }\n return request as Request;\n}\n\n/**\n * Make a middleware context from a legacy request by copying over all non-JSON-RPC\n * properties from the request to the context object.\n *\n * @param req - The legacy request to make a context from.\n * @returns The middleware context.\n */\nexport function makeContext<Request extends Record<string | symbol, unknown>>(\n req: Request,\n): MiddlewareContext {\n const context = new MiddlewareContext();\n propagateToContext(req, context);\n return context;\n}\n\n/**\n * Copies non-JSON-RPC string properties from the request to the context.\n *\n * For compatibility with our problematic practice of appending non-standard\n * fields to requests for inter-middleware communication in the legacy engine.\n *\n * **ATTN:** Only string properties that do not already exist in the context\n * are copied.\n *\n * @param req - The request to propagate the context from.\n * @param context - The context to propagate to.\n */\nexport function propagateToContext(\n req: Record<string, unknown>,\n context: MiddlewareContext<Record<string, unknown>>,\n) {\n Object.keys(req)\n .filter(\n (key) =>\n typeof key === 'string' &&\n !requestProps.includes(key) &&\n !context.has(key),\n )\n .forEach((key) => {\n context.set(key, req[key]);\n });\n}\n\n/**\n * Copies non-JSON-RPC string properties from the context to the request.\n *\n * For compatibility with our problematic practice of appending non-standard\n * fields to requests for inter-middleware communication in the legacy engine.\n *\n * **ATTN:** Only string properties are copied.\n *\n * @param req - The request to propagate the context to.\n * @param context - The context to propagate from.\n */\nexport function propagateToRequest(\n req: Record<string, unknown>,\n context: MiddlewareContext,\n) {\n Array.from(context.keys())\n .filter(\n ((key) => typeof key === 'string' && !requestProps.includes(key)) as (\n value: unknown,\n ) => value is string,\n )\n .forEach((key) => {\n req[key] = context.get(key);\n });\n}\n\n/**\n * Unserialize an error from a thrown value. Creates a {@link JsonRpcError} if\n * the thrown value is an object with a `code` property. Otherwise, creates a\n * plain {@link Error}.\n *\n * @param thrown - The thrown value to unserialize.\n * @returns The unserialized error.\n */\nexport function unserializeError(thrown: unknown): Error | JsonRpcError<Json> {\n // @ts-expect-error - New, but preferred if available.\n // See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/isError\n if (typeof Error.isError === 'function' && Error.isError(thrown)) {\n return thrown as Error;\n }\n // Unlike Error.isError, instanceof does not work for Errors from other realms.\n if (thrown instanceof Error) {\n return thrown;\n }\n if (typeof thrown === 'string') {\n return new Error(thrown);\n }\n if (!isObject(thrown)) {\n return new Error(`Unknown error: ${stringify(thrown)}`);\n }\n\n const code =\n typeof thrown.code === 'number' && Number.isInteger(thrown.code)\n ? thrown.code\n : undefined;\n\n let message = 'Unknown error';\n if (typeof thrown.message === 'string') {\n message = thrown.message;\n } else if (typeof code === 'number') {\n message = getMessageFromCode(code, message);\n }\n\n const { stack, cause, data } = thrown;\n\n const error =\n code === undefined\n ? // Jest complains if we use the `@ts-expect-error` directive here.\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore - Our error type is outdated.\n new Error(message, { cause })\n : new JsonRpcError(code, message, {\n ...(isObject(data) ? data : undefined),\n cause,\n });\n\n if (typeof stack === 'string') {\n error.stack = stack;\n }\n\n return error;\n}\n"]}
@@ -0,0 +1,29 @@
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.JsonRpcEngineError = exports.isRequest = exports.isNotification = exports.JsonRpcServer = exports.getUniqueId = exports.asLegacyMiddleware = void 0;
18
+ var asLegacyMiddleware_1 = require("./asLegacyMiddleware.cjs");
19
+ Object.defineProperty(exports, "asLegacyMiddleware", { enumerable: true, get: function () { return asLegacyMiddleware_1.asLegacyMiddleware; } });
20
+ var getUniqueId_1 = require("../getUniqueId.cjs");
21
+ Object.defineProperty(exports, "getUniqueId", { enumerable: true, get: function () { return getUniqueId_1.getUniqueId; } });
22
+ __exportStar(require("./JsonRpcEngineV2.cjs"), exports);
23
+ var JsonRpcServer_1 = require("./JsonRpcServer.cjs");
24
+ Object.defineProperty(exports, "JsonRpcServer", { enumerable: true, get: function () { return JsonRpcServer_1.JsonRpcServer; } });
25
+ var utils_1 = require("./utils.cjs");
26
+ Object.defineProperty(exports, "isNotification", { enumerable: true, get: function () { return utils_1.isNotification; } });
27
+ Object.defineProperty(exports, "isRequest", { enumerable: true, get: function () { return utils_1.isRequest; } });
28
+ Object.defineProperty(exports, "JsonRpcEngineError", { enumerable: true, get: function () { return utils_1.JsonRpcEngineError; } });
29
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../src/v2/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,+DAA0D;AAAjD,wHAAA,kBAAkB,OAAA;AAC3B,kDAA6C;AAApC,0GAAA,WAAW,OAAA;AACpB,wDAAkC;AAClC,qDAAgD;AAAvC,8GAAA,aAAa,OAAA;AAEtB,qCAAwE;AAA/D,uGAAA,cAAc,OAAA;AAAE,kGAAA,SAAS,OAAA;AAAE,2GAAA,kBAAkB,OAAA","sourcesContent":["export { asLegacyMiddleware } from './asLegacyMiddleware';\nexport { getUniqueId } from '../getUniqueId';\nexport * from './JsonRpcEngineV2';\nexport { JsonRpcServer } from './JsonRpcServer';\nexport type { MiddlewareContext, EmptyContext } from './MiddlewareContext';\nexport { isNotification, isRequest, JsonRpcEngineError } from './utils';\nexport type {\n Json,\n JsonRpcCall,\n JsonRpcNotification,\n JsonRpcParams,\n JsonRpcRequest,\n} from './utils';\n"]}
@@ -0,0 +1,8 @@
1
+ export { asLegacyMiddleware } from "./asLegacyMiddleware.cjs";
2
+ export { getUniqueId } from "../getUniqueId.cjs";
3
+ export * from "./JsonRpcEngineV2.cjs";
4
+ export { JsonRpcServer } from "./JsonRpcServer.cjs";
5
+ export type { MiddlewareContext, EmptyContext } from "./MiddlewareContext.cjs";
6
+ export { isNotification, isRequest, JsonRpcEngineError } from "./utils.cjs";
7
+ export type { Json, JsonRpcCall, JsonRpcNotification, JsonRpcParams, JsonRpcRequest, } from "./utils.cjs";
8
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../src/v2/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,iCAA6B;AAC1D,OAAO,EAAE,WAAW,EAAE,2BAAuB;AAC7C,sCAAkC;AAClC,OAAO,EAAE,aAAa,EAAE,4BAAwB;AAChD,YAAY,EAAE,iBAAiB,EAAE,YAAY,EAAE,gCAA4B;AAC3E,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,kBAAkB,EAAE,oBAAgB;AACxE,YAAY,EACV,IAAI,EACJ,WAAW,EACX,mBAAmB,EACnB,aAAa,EACb,cAAc,GACf,oBAAgB"}
@@ -0,0 +1,8 @@
1
+ export { asLegacyMiddleware } from "./asLegacyMiddleware.mjs";
2
+ export { getUniqueId } from "../getUniqueId.mjs";
3
+ export * from "./JsonRpcEngineV2.mjs";
4
+ export { JsonRpcServer } from "./JsonRpcServer.mjs";
5
+ export type { MiddlewareContext, EmptyContext } from "./MiddlewareContext.mjs";
6
+ export { isNotification, isRequest, JsonRpcEngineError } from "./utils.mjs";
7
+ export type { Json, JsonRpcCall, JsonRpcNotification, JsonRpcParams, JsonRpcRequest, } from "./utils.mjs";
8
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/v2/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,iCAA6B;AAC1D,OAAO,EAAE,WAAW,EAAE,2BAAuB;AAC7C,sCAAkC;AAClC,OAAO,EAAE,aAAa,EAAE,4BAAwB;AAChD,YAAY,EAAE,iBAAiB,EAAE,YAAY,EAAE,gCAA4B;AAC3E,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,kBAAkB,EAAE,oBAAgB;AACxE,YAAY,EACV,IAAI,EACJ,WAAW,EACX,mBAAmB,EACnB,aAAa,EACb,cAAc,GACf,oBAAgB"}
@@ -0,0 +1,6 @@
1
+ export { asLegacyMiddleware } from "./asLegacyMiddleware.mjs";
2
+ export { getUniqueId } from "../getUniqueId.mjs";
3
+ export * from "./JsonRpcEngineV2.mjs";
4
+ export { JsonRpcServer } from "./JsonRpcServer.mjs";
5
+ export { isNotification, isRequest, JsonRpcEngineError } from "./utils.mjs";
6
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/v2/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,iCAA6B;AAC1D,OAAO,EAAE,WAAW,EAAE,2BAAuB;AAC7C,sCAAkC;AAClC,OAAO,EAAE,aAAa,EAAE,4BAAwB;AAEhD,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,kBAAkB,EAAE,oBAAgB","sourcesContent":["export { asLegacyMiddleware } from './asLegacyMiddleware';\nexport { getUniqueId } from '../getUniqueId';\nexport * from './JsonRpcEngineV2';\nexport { JsonRpcServer } from './JsonRpcServer';\nexport type { MiddlewareContext, EmptyContext } from './MiddlewareContext';\nexport { isNotification, isRequest, JsonRpcEngineError } from './utils';\nexport type {\n Json,\n JsonRpcCall,\n JsonRpcNotification,\n JsonRpcParams,\n JsonRpcRequest,\n} from './utils';\n"]}