@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,213 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ var _JsonRpcEngineV2_instances, _JsonRpcEngineV2_middleware, _JsonRpcEngineV2_isDestroyed, _JsonRpcEngineV2_handle, _JsonRpcEngineV2_makeNextFactory, _JsonRpcEngineV2_makeMiddlewareIterator, _JsonRpcEngineV2_updateResult, _JsonRpcEngineV2_assertValidNextRequest, _JsonRpcEngineV2_assertIsNotDestroyed;
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.JsonRpcEngineV2 = void 0;
19
+ const utils_1 = require("@metamask/utils");
20
+ const deep_freeze_strict_1 = __importDefault(require("deep-freeze-strict"));
21
+ const MiddlewareContext_1 = require("./MiddlewareContext.cjs");
22
+ const utils_2 = require("./utils.cjs");
23
+ const INVALID_ENGINE = Symbol('Invalid engine');
24
+ /**
25
+ * A JSON-RPC request and response processor.
26
+ *
27
+ * Give it a stack of middleware, pass it requests, and get back responses.
28
+ *
29
+ * #### Requests vs. notifications
30
+ *
31
+ * JSON-RPC requests come in two flavors:
32
+ *
33
+ * - [Requests](https://www.jsonrpc.org/specification#request_object), i.e. request objects _with_ an `id`
34
+ * - [Notifications](https://www.jsonrpc.org/specification#notification), i.e. request objects _without_ an `id`
35
+ *
36
+ * For requests, one of the engine's middleware must "end" the request by returning a non-`undefined` result,
37
+ * or {@link handle} will throw an error:
38
+ *
39
+ * For notifications, on the other hand, one of the engine's middleware must return `undefined` to end the request,
40
+ * and any non-`undefined` return values will cause an error:
41
+ *
42
+ * @template Request - The type of request to handle.
43
+ * @template Result - The type of result to return.
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * const engine = JsonRpcEngineV2.create({
48
+ * middleware,
49
+ * });
50
+ *
51
+ * try {
52
+ * const result = await engine.handle(request);
53
+ * // Handle result
54
+ * } catch (error) {
55
+ * // Handle error
56
+ * }
57
+ * ```
58
+ */
59
+ class JsonRpcEngineV2 {
60
+ // See .create() for why this is private.
61
+ constructor({ middleware }) {
62
+ _JsonRpcEngineV2_instances.add(this);
63
+ _JsonRpcEngineV2_middleware.set(this, void 0);
64
+ _JsonRpcEngineV2_isDestroyed.set(this, false);
65
+ __classPrivateFieldSet(this, _JsonRpcEngineV2_middleware, [...middleware], "f");
66
+ }
67
+ // We use a static factory method in order to construct a supertype of all middleware contexts,
68
+ // which enables us to instantiate an engine despite different middleware expecting different
69
+ // context types.
70
+ /**
71
+ * Create a new JSON-RPC engine.
72
+ *
73
+ * @throws If the middleware array is empty.
74
+ * @param options - The options for the engine.
75
+ * @param options.middleware - The middleware to use.
76
+ * @returns The JSON-RPC engine.
77
+ */
78
+ static create({ middleware }) {
79
+ // We can't use NonEmptyArray for the params because it ruins type inference.
80
+ if (middleware.length === 0) {
81
+ throw new utils_2.JsonRpcEngineError('Middleware array cannot be empty');
82
+ }
83
+ const mw = middleware;
84
+ return new JsonRpcEngineV2({
85
+ middleware: mw,
86
+ });
87
+ }
88
+ async handle(request, { context } = {}) {
89
+ const isReq = (0, utils_2.isRequest)(request);
90
+ const { result } = await __classPrivateFieldGet(this, _JsonRpcEngineV2_instances, "m", _JsonRpcEngineV2_handle).call(this, request, context);
91
+ if (isReq && result === undefined) {
92
+ throw new utils_2.JsonRpcEngineError(`Nothing ended request: ${(0, utils_2.stringify)(request)}`);
93
+ }
94
+ return result;
95
+ }
96
+ /**
97
+ * Convert the engine into a JSON-RPC middleware.
98
+ *
99
+ * @returns The JSON-RPC middleware.
100
+ */
101
+ asMiddleware() {
102
+ __classPrivateFieldGet(this, _JsonRpcEngineV2_instances, "m", _JsonRpcEngineV2_assertIsNotDestroyed).call(this);
103
+ return async ({ request, context, next }) => {
104
+ const { result, request: finalRequest } = await __classPrivateFieldGet(this, _JsonRpcEngineV2_instances, "m", _JsonRpcEngineV2_handle).call(this, request, context);
105
+ return result === undefined ? await next(finalRequest) : result;
106
+ };
107
+ }
108
+ /**
109
+ * Destroy the engine. Calls the `destroy()` method of any middleware that has
110
+ * one. Attempting to use the engine after destroying it will throw an error.
111
+ */
112
+ async destroy() {
113
+ if (__classPrivateFieldGet(this, _JsonRpcEngineV2_isDestroyed, "f")) {
114
+ return;
115
+ }
116
+ __classPrivateFieldSet(this, _JsonRpcEngineV2_isDestroyed, true, "f");
117
+ const destructionPromise = Promise.all(__classPrivateFieldGet(this, _JsonRpcEngineV2_middleware, "f").map(async (middleware) => {
118
+ if (
119
+ // Intentionally using `in` to walk the prototype chain.
120
+ 'destroy' in middleware &&
121
+ typeof middleware.destroy === 'function') {
122
+ return middleware.destroy();
123
+ }
124
+ return undefined;
125
+ }));
126
+ __classPrivateFieldSet(this, _JsonRpcEngineV2_middleware, [], "f");
127
+ await destructionPromise;
128
+ }
129
+ }
130
+ exports.JsonRpcEngineV2 = JsonRpcEngineV2;
131
+ _JsonRpcEngineV2_middleware = new WeakMap(), _JsonRpcEngineV2_isDestroyed = new WeakMap(), _JsonRpcEngineV2_instances = new WeakSet(), _JsonRpcEngineV2_handle =
132
+ /**
133
+ * Handle a JSON-RPC request. Throws if a middleware performs an invalid
134
+ * operation. Permits returning an `undefined` result.
135
+ *
136
+ * @param originalRequest - The JSON-RPC request to handle.
137
+ * @param context - The context to pass to the middleware.
138
+ * @returns The result from the middleware.
139
+ */
140
+ async function _JsonRpcEngineV2_handle(originalRequest, context = new MiddlewareContext_1.MiddlewareContext()) {
141
+ __classPrivateFieldGet(this, _JsonRpcEngineV2_instances, "m", _JsonRpcEngineV2_assertIsNotDestroyed).call(this);
142
+ (0, deep_freeze_strict_1.default)(originalRequest);
143
+ const state = {
144
+ request: originalRequest,
145
+ result: undefined,
146
+ };
147
+ const middlewareIterator = __classPrivateFieldGet(this, _JsonRpcEngineV2_instances, "m", _JsonRpcEngineV2_makeMiddlewareIterator).call(this);
148
+ const firstMiddleware = middlewareIterator.next().value;
149
+ const makeNext = __classPrivateFieldGet(this, _JsonRpcEngineV2_instances, "m", _JsonRpcEngineV2_makeNextFactory).call(this, middlewareIterator, state, context);
150
+ const result = await firstMiddleware({
151
+ request: originalRequest,
152
+ context,
153
+ next: makeNext(),
154
+ });
155
+ __classPrivateFieldGet(this, _JsonRpcEngineV2_instances, "m", _JsonRpcEngineV2_updateResult).call(this, result, state);
156
+ return state;
157
+ }, _JsonRpcEngineV2_makeNextFactory = function _JsonRpcEngineV2_makeNextFactory(middlewareIterator, state, context) {
158
+ const makeNext = () => {
159
+ let wasCalled = false;
160
+ const next = async (request = state.request) => {
161
+ if (wasCalled) {
162
+ throw new utils_2.JsonRpcEngineError(`Middleware attempted to call next() multiple times for request: ${(0, utils_2.stringify)(request)}`);
163
+ }
164
+ wasCalled = true;
165
+ if (request !== state.request) {
166
+ __classPrivateFieldGet(this, _JsonRpcEngineV2_instances, "m", _JsonRpcEngineV2_assertValidNextRequest).call(this, state.request, request);
167
+ state.request = (0, deep_freeze_strict_1.default)(request);
168
+ }
169
+ const { value: nextMiddleware, done } = middlewareIterator.next();
170
+ if (done) {
171
+ // This will cause the last middleware to return `undefined`. See the class
172
+ // JSDoc or package README for more details.
173
+ return undefined;
174
+ }
175
+ const result = await nextMiddleware({
176
+ request,
177
+ context,
178
+ next: makeNext(),
179
+ });
180
+ __classPrivateFieldGet(this, _JsonRpcEngineV2_instances, "m", _JsonRpcEngineV2_updateResult).call(this, result, state);
181
+ return state.result;
182
+ };
183
+ return next;
184
+ };
185
+ return makeNext;
186
+ }, _JsonRpcEngineV2_makeMiddlewareIterator = function _JsonRpcEngineV2_makeMiddlewareIterator() {
187
+ return __classPrivateFieldGet(this, _JsonRpcEngineV2_middleware, "f")[Symbol.iterator]();
188
+ }, _JsonRpcEngineV2_updateResult = function _JsonRpcEngineV2_updateResult(result, state) {
189
+ if ((0, utils_2.isNotification)(state.request) && result !== undefined) {
190
+ throw new utils_2.JsonRpcEngineError(`Result returned for notification: ${(0, utils_2.stringify)(state.request)}`);
191
+ }
192
+ if (result !== undefined && result !== state.result) {
193
+ if (typeof result === 'object' && result !== null) {
194
+ (0, deep_freeze_strict_1.default)(result);
195
+ }
196
+ state.result = result;
197
+ }
198
+ }, _JsonRpcEngineV2_assertValidNextRequest = function _JsonRpcEngineV2_assertValidNextRequest(currentRequest, nextRequest) {
199
+ if (nextRequest.jsonrpc !== currentRequest.jsonrpc) {
200
+ throw new utils_2.JsonRpcEngineError(`Middleware attempted to modify readonly property "jsonrpc" for request: ${(0, utils_2.stringify)(currentRequest)}`);
201
+ }
202
+ if ((0, utils_1.hasProperty)(nextRequest, 'id') !== (0, utils_1.hasProperty)(currentRequest, 'id') ||
203
+ // @ts-expect-error - "id" does not exist on notifications, but we can still
204
+ // check the value of the property at runtime.
205
+ nextRequest.id !== currentRequest.id) {
206
+ throw new utils_2.JsonRpcEngineError(`Middleware attempted to modify readonly property "id" for request: ${(0, utils_2.stringify)(currentRequest)}`);
207
+ }
208
+ }, _JsonRpcEngineV2_assertIsNotDestroyed = function _JsonRpcEngineV2_assertIsNotDestroyed() {
209
+ if (__classPrivateFieldGet(this, _JsonRpcEngineV2_isDestroyed, "f")) {
210
+ throw new utils_2.JsonRpcEngineError('Engine is destroyed');
211
+ }
212
+ };
213
+ //# sourceMappingURL=JsonRpcEngineV2.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"JsonRpcEngineV2.cjs","sourceRoot":"","sources":["../../src/v2/JsonRpcEngineV2.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,2CAMyB;AACzB,4EAA4C;AAG5C,+DAAwD;AACxD,uCAKiB;AAmFjB,MAAM,cAAc,GAAG,MAAM,CAAC,gBAAgB,CAAC,CAAC;AAShD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAa,eAAe;IAY1B,yCAAyC;IACzC,YAAoB,EAAE,UAAU,EAAwC;;QATxE,8CAIE;QAEF,uCAAe,KAAK,EAAC;QAInB,uBAAA,IAAI,+BAAe,CAAC,GAAG,UAAU,CAAC,MAAA,CAAC;IACrC,CAAC;IAED,+FAA+F;IAC/F,6FAA6F;IAC7F,iBAAiB;IACjB;;;;;;;OAOG;IACH,MAAM,CAAC,MAAM,CASX,EAAE,UAAU,EAAgC;QAC5C,6EAA6E;QAC7E,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE;YAC3B,MAAM,IAAI,0BAAkB,CAAC,kCAAkC,CAAC,CAAC;SAClE;QAID,MAAM,EAAE,GAAG,UAMV,CAAC;QACF,OAAO,IAAI,eAAe,CAA8B;YACtD,UAAU,EAAE,EAAE;SACf,CAE+C,CAAC;IACnD,CAA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U,EAAC,MAAM,CAAC,CAAC;SACpB;QACD,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC;KACvB;AACH,CAAC,6FAQuB,cAAuB,EAAE,WAAoB;IACnE,IAAI,WAAW,CAAC,OAAO,KAAK,cAAc,CAAC,OAAO,EAAE;QAClD,MAAM,IAAI,0BAAkB,CAC1B,2EAA2E,IAAA,iBAAS,EAAC,cAAc,CAAC,EAAE,CACvG,CAAC;KACH;IACD,IACE,IAAA,mBAAW,EAAC,WAAW,EAAE,IAAI,CAAC,KAAK,IAAA,mBAAW,EAAC,cAAc,EAAE,IAAI,CAAC;QACpE,4EAA4E;QAC5E,8CAA8C;QAC9C,WAAW,CAAC,EAAE,KAAK,cAAc,CAAC,EAAE,EACpC;QACA,MAAM,IAAI,0BAAkB,CAC1B,sEAAsE,IAAA,iBAAS,EAAC,cAAc,CAAC,EAAE,CAClG,CAAC;KACH;AACH,CAAC;IAkDC,IAAI,uBAAA,IAAI,oCAAa,EAAE;QACrB,MAAM,IAAI,0BAAkB,CAAC,qBAAqB,CAAC,CAAC;KACrD;AACH,CAAC","sourcesContent":["import {\n type Json,\n type JsonRpcRequest,\n type JsonRpcNotification,\n type NonEmptyArray,\n hasProperty,\n} from '@metamask/utils';\nimport deepFreeze from 'deep-freeze-strict';\n\nimport type { ContextConstraint, MergeContexts } from './MiddlewareContext';\nimport { MiddlewareContext } from './MiddlewareContext';\nimport {\n isNotification,\n isRequest,\n JsonRpcEngineError,\n stringify,\n} from './utils';\nimport type { JsonRpcCall } from './utils';\n\n// Helper to forbid `id` on notifications\ntype WithoutId<Request extends JsonRpcCall> = Request & { id?: never };\n\n// Helper to enable JsonRpcCall overload of handle()\ntype MixedParam<Request extends JsonRpcCall> = [\n Extract<Request, JsonRpcRequest>,\n] extends [never]\n ? never\n : [Extract<Request, JsonRpcNotification>] extends [never]\n ? never\n :\n | Extract<Request, JsonRpcRequest>\n | WithoutId<Extract<Request, JsonRpcNotification>>;\n\nexport type ResultConstraint<Request extends JsonRpcCall> =\n Request extends JsonRpcRequest ? Json : void;\n\nexport type Next<Request extends JsonRpcCall> = (\n request?: Readonly<Request>,\n) => Promise<Readonly<ResultConstraint<Request>> | undefined>;\n\nexport type MiddlewareParams<\n Request extends JsonRpcCall,\n Context extends MiddlewareContext,\n> = {\n request: Readonly<Request>;\n context: Context;\n next: Next<Request>;\n};\n\nexport type JsonRpcMiddleware<\n Request extends JsonRpcCall = JsonRpcCall,\n Result extends ResultConstraint<Request> = ResultConstraint<Request>,\n Context extends ContextConstraint = MiddlewareContext,\n> = (\n params: MiddlewareParams<Request, Context>,\n) => Readonly<Result> | undefined | Promise<Readonly<Result> | undefined>;\n\ntype RequestState<Request extends JsonRpcCall> = {\n request: Request;\n result: Readonly<ResultConstraint<Request>> | undefined;\n};\n\ntype HandleOptions<Context extends MiddlewareContext> = {\n context?: Context;\n};\n\ntype ConstructorOptions<\n Request extends JsonRpcCall,\n Context extends MiddlewareContext,\n> = {\n middleware: NonEmptyArray<\n JsonRpcMiddleware<Request, ResultConstraint<Request>, Context>\n >;\n};\n\ntype RequestOf<Middleware> =\n Middleware extends JsonRpcMiddleware<\n infer Request,\n ResultConstraint<infer Request>,\n // Non-polluting `any` constraint.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any\n >\n ? Request\n : never;\n\ntype ContextOf<Middleware> =\n // Non-polluting `any` constraint.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n Middleware extends JsonRpcMiddleware<any, ResultConstraint<any>, infer C>\n ? C\n : never;\n\ntype MergedContextOf<\n // Non-polluting `any` constraint.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n Middleware extends JsonRpcMiddleware<any, any, any>,\n> = MergeContexts<ContextOf<Middleware>>;\n\nconst INVALID_ENGINE = Symbol('Invalid engine');\n\n/**\n * An internal type for invalid engines that explains why the engine is invalid.\n *\n * @template Message - The message explaining why the engine is invalid.\n */\ntype InvalidEngine<Message extends string> = { [INVALID_ENGINE]: Message };\n\n/**\n * A JSON-RPC request and response processor.\n *\n * Give it a stack of middleware, pass it requests, and get back responses.\n *\n * #### Requests vs. notifications\n *\n * JSON-RPC requests come in two flavors:\n *\n * - [Requests](https://www.jsonrpc.org/specification#request_object), i.e. request objects _with_ an `id`\n * - [Notifications](https://www.jsonrpc.org/specification#notification), i.e. request objects _without_ an `id`\n *\n * For requests, one of the engine's middleware must \"end\" the request by returning a non-`undefined` result,\n * or {@link handle} will throw an error:\n *\n * For notifications, on the other hand, one of the engine's middleware must return `undefined` to end the request,\n * and any non-`undefined` return values will cause an error:\n *\n * @template Request - The type of request to handle.\n * @template Result - The type of result to return.\n *\n * @example\n * ```ts\n * const engine = JsonRpcEngineV2.create({\n * middleware,\n * });\n *\n * try {\n * const result = await engine.handle(request);\n * // Handle result\n * } catch (error) {\n * // Handle error\n * }\n * ```\n */\nexport class JsonRpcEngineV2<\n Request extends JsonRpcCall = JsonRpcCall,\n Context extends ContextConstraint = MiddlewareContext,\n> {\n #middleware: Readonly<\n NonEmptyArray<\n JsonRpcMiddleware<Request, ResultConstraint<Request>, Context>\n >\n >;\n\n #isDestroyed = false;\n\n // See .create() for why this is private.\n private constructor({ middleware }: ConstructorOptions<Request, Context>) {\n this.#middleware = [...middleware];\n }\n\n // We use a static factory method in order to construct a supertype of all middleware contexts,\n // which enables us to instantiate an engine despite different middleware expecting different\n // context types.\n /**\n * Create a new JSON-RPC engine.\n *\n * @throws If the middleware array is empty.\n * @param options - The options for the engine.\n * @param options.middleware - The middleware to use.\n * @returns The JSON-RPC engine.\n */\n static create<\n Middleware extends JsonRpcMiddleware<\n // Non-polluting `any` constraint.\n /* eslint-disable @typescript-eslint/no-explicit-any */\n any,\n ResultConstraint<any>,\n any\n /* eslint-enable @typescript-eslint/no-explicit-any */\n > = JsonRpcMiddleware,\n >({ middleware }: { middleware: Middleware[] }) {\n // We can't use NonEmptyArray for the params because it ruins type inference.\n if (middleware.length === 0) {\n throw new JsonRpcEngineError('Middleware array cannot be empty');\n }\n\n type MergedContext = MergedContextOf<Middleware>;\n type InputRequest = RequestOf<Middleware>;\n const mw = middleware as unknown as NonEmptyArray<\n JsonRpcMiddleware<\n InputRequest,\n ResultConstraint<InputRequest>,\n MergedContext\n >\n >;\n return new JsonRpcEngineV2<InputRequest, MergedContext>({\n middleware: mw,\n }) as MergedContext extends never\n ? InvalidEngine<'Some middleware have incompatible context types'>\n : JsonRpcEngineV2<InputRequest, MergedContext>;\n }\n\n /**\n * Handle a JSON-RPC request.\n *\n * @param request - The JSON-RPC request to handle.\n * @param options - The options for the handle operation.\n * @param options.context - The context to pass to the middleware.\n * @returns The JSON-RPC response.\n */\n async handle(\n request: Extract<Request, JsonRpcRequest> extends never\n ? never\n : Extract<Request, JsonRpcRequest>,\n options?: HandleOptions<Context>,\n ): Promise<\n Extract<Request, JsonRpcRequest> extends never\n ? never\n : ResultConstraint<Request>\n >;\n\n /**\n * Handle a JSON-RPC notification. Notifications do not return a result.\n *\n * @param notification - The JSON-RPC notification to handle.\n * @param options - The options for the handle operation.\n * @param options.context - The context to pass to the middleware.\n */\n async handle(\n notification: Extract<Request, JsonRpcNotification> extends never\n ? never\n : WithoutId<Extract<Request, JsonRpcNotification>>,\n options?: HandleOptions<Context>,\n ): Promise<\n Extract<Request, JsonRpcNotification> extends never\n ? never\n : ResultConstraint<Request>\n >;\n\n /**\n * Handle a JSON-RPC call, i.e. request or notification. Requests return a\n * result, notifications do not.\n *\n * @param call - The JSON-RPC call to handle.\n * @param options - The options for the handle operation.\n * @param options.context - The context to pass to the middleware.\n * @returns The JSON-RPC response, or `undefined` if the call is a notification.\n */\n async handle(\n call: MixedParam<Request>,\n options?: HandleOptions<Context>,\n ): Promise<ResultConstraint<Request> | void>;\n\n async handle(\n request: Request,\n { context }: HandleOptions<Context> = {},\n ): Promise<Readonly<ResultConstraint<Request>> | void> {\n const isReq = isRequest(request);\n const { result } = await this.#handle(request, context);\n\n if (isReq && result === undefined) {\n throw new JsonRpcEngineError(\n `Nothing ended request: ${stringify(request)}`,\n );\n }\n return result;\n }\n\n /**\n * Handle a JSON-RPC request. Throws if a middleware performs an invalid\n * operation. Permits returning an `undefined` result.\n *\n * @param originalRequest - The JSON-RPC request to handle.\n * @param context - The context to pass to the middleware.\n * @returns The result from the middleware.\n */\n async #handle(\n originalRequest: Request,\n context: Context = new MiddlewareContext() as Context,\n ): Promise<RequestState<Request>> {\n this.#assertIsNotDestroyed();\n\n deepFreeze(originalRequest);\n\n const state: RequestState<Request> = {\n request: originalRequest,\n result: undefined,\n };\n const middlewareIterator = this.#makeMiddlewareIterator();\n const firstMiddleware = middlewareIterator.next().value;\n\n const makeNext = this.#makeNextFactory(middlewareIterator, state, context);\n\n const result = await firstMiddleware({\n request: originalRequest,\n context,\n next: makeNext(),\n });\n this.#updateResult(result, state);\n\n return state;\n }\n\n /**\n * Create a factory of `next()` functions for use with a particular request.\n * The factory is recursive, and a new `next()` is created for each middleware\n * invocation.\n *\n * @param middlewareIterator - The iterator of middleware for the current\n * request.\n * @param state - The current values of the request and result.\n * @param context - The context to pass to the middleware.\n * @returns The `next()` function factory.\n */\n #makeNextFactory(\n middlewareIterator: Iterator<\n JsonRpcMiddleware<Request, ResultConstraint<Request>, Context>\n >,\n state: RequestState<Request>,\n context: Context,\n ): () => Next<Request> {\n const makeNext = (): Next<Request> => {\n let wasCalled = false;\n\n const next = async (\n request: Request = state.request,\n ): Promise<Readonly<ResultConstraint<Request>> | undefined> => {\n if (wasCalled) {\n throw new JsonRpcEngineError(\n `Middleware attempted to call next() multiple times for request: ${stringify(request)}`,\n );\n }\n wasCalled = true;\n\n if (request !== state.request) {\n this.#assertValidNextRequest(state.request, request);\n state.request = deepFreeze(request);\n }\n\n const { value: nextMiddleware, done } = middlewareIterator.next();\n if (done) {\n // This will cause the last middleware to return `undefined`. See the class\n // JSDoc or package README for more details.\n return undefined;\n }\n\n const result = await nextMiddleware({\n request,\n context,\n next: makeNext(),\n });\n this.#updateResult(result, state);\n\n return state.result;\n };\n return next;\n };\n\n return makeNext;\n }\n\n #makeMiddlewareIterator(): Iterator<\n JsonRpcMiddleware<Request, ResultConstraint<Request>, Context>\n > {\n return this.#middleware[Symbol.iterator]();\n }\n\n /**\n * Validate the result from a middleware and, if it's a new value, update the\n * current result.\n *\n * @param result - The result from the middleware.\n * @param state - The current values of the request and result.\n */\n #updateResult(\n result:\n | Readonly<ResultConstraint<Request>>\n | ResultConstraint<Request>\n | void,\n state: RequestState<Request>,\n ): void {\n if (isNotification(state.request) && result !== undefined) {\n throw new JsonRpcEngineError(\n `Result returned for notification: ${stringify(state.request)}`,\n );\n }\n\n if (result !== undefined && result !== state.result) {\n if (typeof result === 'object' && result !== null) {\n deepFreeze(result);\n }\n state.result = result;\n }\n }\n\n /**\n * Assert that a request modified by a middleware is valid.\n *\n * @param currentRequest - The current request.\n * @param nextRequest - The next request.\n */\n #assertValidNextRequest(currentRequest: Request, nextRequest: Request): void {\n if (nextRequest.jsonrpc !== currentRequest.jsonrpc) {\n throw new JsonRpcEngineError(\n `Middleware attempted to modify readonly property \"jsonrpc\" for request: ${stringify(currentRequest)}`,\n );\n }\n if (\n hasProperty(nextRequest, 'id') !== hasProperty(currentRequest, 'id') ||\n // @ts-expect-error - \"id\" does not exist on notifications, but we can still\n // check the value of the property at runtime.\n nextRequest.id !== currentRequest.id\n ) {\n throw new JsonRpcEngineError(\n `Middleware attempted to modify readonly property \"id\" for request: ${stringify(currentRequest)}`,\n );\n }\n }\n\n /**\n * Convert the engine into a JSON-RPC middleware.\n *\n * @returns The JSON-RPC middleware.\n */\n asMiddleware(): JsonRpcMiddleware<\n Request,\n ResultConstraint<Request>,\n Context\n > {\n this.#assertIsNotDestroyed();\n\n return async ({ request, context, next }) => {\n const { result, request: finalRequest } = await this.#handle(\n request,\n context,\n );\n return result === undefined ? await next(finalRequest) : result;\n };\n }\n\n /**\n * Destroy the engine. Calls the `destroy()` method of any middleware that has\n * one. Attempting to use the engine after destroying it will throw an error.\n */\n async destroy(): Promise<void> {\n if (this.#isDestroyed) {\n return;\n }\n this.#isDestroyed = true;\n\n const destructionPromise = Promise.all(\n this.#middleware.map(async (middleware) => {\n if (\n // Intentionally using `in` to walk the prototype chain.\n 'destroy' in middleware &&\n typeof middleware.destroy === 'function'\n ) {\n return middleware.destroy();\n }\n return undefined;\n }),\n );\n this.#middleware = [] as never;\n await destructionPromise;\n }\n\n #assertIsNotDestroyed(): void {\n if (this.#isDestroyed) {\n throw new JsonRpcEngineError('Engine is destroyed');\n }\n }\n}\n"]}
@@ -0,0 +1,122 @@
1
+ import { type Json, type JsonRpcRequest, type JsonRpcNotification } from "@metamask/utils";
2
+ import type { ContextConstraint, MergeContexts } from "./MiddlewareContext.cjs";
3
+ import { MiddlewareContext } from "./MiddlewareContext.cjs";
4
+ import type { JsonRpcCall } from "./utils.cjs";
5
+ type WithoutId<Request extends JsonRpcCall> = Request & {
6
+ id?: never;
7
+ };
8
+ type MixedParam<Request extends JsonRpcCall> = [
9
+ Extract<Request, JsonRpcRequest>
10
+ ] extends [never] ? never : [Extract<Request, JsonRpcNotification>] extends [never] ? never : Extract<Request, JsonRpcRequest> | WithoutId<Extract<Request, JsonRpcNotification>>;
11
+ export type ResultConstraint<Request extends JsonRpcCall> = Request extends JsonRpcRequest ? Json : void;
12
+ export type Next<Request extends JsonRpcCall> = (request?: Readonly<Request>) => Promise<Readonly<ResultConstraint<Request>> | undefined>;
13
+ export type MiddlewareParams<Request extends JsonRpcCall, Context extends MiddlewareContext> = {
14
+ request: Readonly<Request>;
15
+ context: Context;
16
+ next: Next<Request>;
17
+ };
18
+ export type JsonRpcMiddleware<Request extends JsonRpcCall = JsonRpcCall, Result extends ResultConstraint<Request> = ResultConstraint<Request>, Context extends ContextConstraint = MiddlewareContext> = (params: MiddlewareParams<Request, Context>) => Readonly<Result> | undefined | Promise<Readonly<Result> | undefined>;
19
+ type HandleOptions<Context extends MiddlewareContext> = {
20
+ context?: Context;
21
+ };
22
+ type RequestOf<Middleware> = Middleware extends JsonRpcMiddleware<infer Request, ResultConstraint<infer Request>, any> ? Request : never;
23
+ type ContextOf<Middleware> = Middleware extends JsonRpcMiddleware<any, ResultConstraint<any>, infer C> ? C : never;
24
+ declare const INVALID_ENGINE: unique symbol;
25
+ /**
26
+ * An internal type for invalid engines that explains why the engine is invalid.
27
+ *
28
+ * @template Message - The message explaining why the engine is invalid.
29
+ */
30
+ type InvalidEngine<Message extends string> = {
31
+ [INVALID_ENGINE]: Message;
32
+ };
33
+ /**
34
+ * A JSON-RPC request and response processor.
35
+ *
36
+ * Give it a stack of middleware, pass it requests, and get back responses.
37
+ *
38
+ * #### Requests vs. notifications
39
+ *
40
+ * JSON-RPC requests come in two flavors:
41
+ *
42
+ * - [Requests](https://www.jsonrpc.org/specification#request_object), i.e. request objects _with_ an `id`
43
+ * - [Notifications](https://www.jsonrpc.org/specification#notification), i.e. request objects _without_ an `id`
44
+ *
45
+ * For requests, one of the engine's middleware must "end" the request by returning a non-`undefined` result,
46
+ * or {@link handle} will throw an error:
47
+ *
48
+ * For notifications, on the other hand, one of the engine's middleware must return `undefined` to end the request,
49
+ * and any non-`undefined` return values will cause an error:
50
+ *
51
+ * @template Request - The type of request to handle.
52
+ * @template Result - The type of result to return.
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * const engine = JsonRpcEngineV2.create({
57
+ * middleware,
58
+ * });
59
+ *
60
+ * try {
61
+ * const result = await engine.handle(request);
62
+ * // Handle result
63
+ * } catch (error) {
64
+ * // Handle error
65
+ * }
66
+ * ```
67
+ */
68
+ export declare class JsonRpcEngineV2<Request extends JsonRpcCall = JsonRpcCall, Context extends ContextConstraint = MiddlewareContext> {
69
+ #private;
70
+ private constructor();
71
+ /**
72
+ * Create a new JSON-RPC engine.
73
+ *
74
+ * @throws If the middleware array is empty.
75
+ * @param options - The options for the engine.
76
+ * @param options.middleware - The middleware to use.
77
+ * @returns The JSON-RPC engine.
78
+ */
79
+ static create<Middleware extends JsonRpcMiddleware<any, ResultConstraint<any>, any> = JsonRpcMiddleware>({ middleware }: {
80
+ middleware: Middleware[];
81
+ }): MergeContexts<ContextOf<Middleware>> extends never ? InvalidEngine<"Some middleware have incompatible context types"> : JsonRpcEngineV2<RequestOf<Middleware>, MergeContexts<ContextOf<Middleware>>>;
82
+ /**
83
+ * Handle a JSON-RPC request.
84
+ *
85
+ * @param request - The JSON-RPC request to handle.
86
+ * @param options - The options for the handle operation.
87
+ * @param options.context - The context to pass to the middleware.
88
+ * @returns The JSON-RPC response.
89
+ */
90
+ handle(request: Extract<Request, JsonRpcRequest> extends never ? never : Extract<Request, JsonRpcRequest>, options?: HandleOptions<Context>): Promise<Extract<Request, JsonRpcRequest> extends never ? never : ResultConstraint<Request>>;
91
+ /**
92
+ * Handle a JSON-RPC notification. Notifications do not return a result.
93
+ *
94
+ * @param notification - The JSON-RPC notification to handle.
95
+ * @param options - The options for the handle operation.
96
+ * @param options.context - The context to pass to the middleware.
97
+ */
98
+ handle(notification: Extract<Request, JsonRpcNotification> extends never ? never : WithoutId<Extract<Request, JsonRpcNotification>>, options?: HandleOptions<Context>): Promise<Extract<Request, JsonRpcNotification> extends never ? never : ResultConstraint<Request>>;
99
+ /**
100
+ * Handle a JSON-RPC call, i.e. request or notification. Requests return a
101
+ * result, notifications do not.
102
+ *
103
+ * @param call - The JSON-RPC call to handle.
104
+ * @param options - The options for the handle operation.
105
+ * @param options.context - The context to pass to the middleware.
106
+ * @returns The JSON-RPC response, or `undefined` if the call is a notification.
107
+ */
108
+ handle(call: MixedParam<Request>, options?: HandleOptions<Context>): Promise<ResultConstraint<Request> | void>;
109
+ /**
110
+ * Convert the engine into a JSON-RPC middleware.
111
+ *
112
+ * @returns The JSON-RPC middleware.
113
+ */
114
+ asMiddleware(): JsonRpcMiddleware<Request, ResultConstraint<Request>, Context>;
115
+ /**
116
+ * Destroy the engine. Calls the `destroy()` method of any middleware that has
117
+ * one. Attempting to use the engine after destroying it will throw an error.
118
+ */
119
+ destroy(): Promise<void>;
120
+ }
121
+ export {};
122
+ //# sourceMappingURL=Json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RpcNotification } from "@metamask/utils";
2
+ import type { ContextConstraint, MergeContexts } from "./MiddlewareContext.mjs";
3
+ import { MiddlewareContext } from "./MiddlewareContext.mjs";
4
+ import type { JsonRpcCall } from "./utils.mjs";
5
+ type WithoutId<Request extends JsonRpcCall> = Request & {
6
+ id?: never;
7
+ };
8
+ type MixedParam<Request extends JsonRpcCall> = [
9
+ Extract<Request, JsonRpcRequest>
10
+ ] extends [never] ? never : [Extract<Request, JsonRpcNotification>] extends [never] ? never : Extract<Request, JsonRpcRequest> | WithoutId<Extract<Request, JsonRpcNotification>>;
11
+ export type ResultConstraint<Request extends JsonRpcCall> = Request extends JsonRpcRequest ? Json : void;
12
+ export type Next<Request extends JsonRpcCall> = (request?: Readonly<Request>) => Promise<Readonly<ResultConstraint<Request>> | undefined>;
13
+ export type MiddlewareParams<Request extends JsonRpcCall, Context extends MiddlewareContext> = {
14
+ request: Readonly<Request>;
15
+ context: Context;
16
+ next: Next<Request>;
17
+ };
18
+ export type JsonRpcMiddleware<Request extends JsonRpcCall = JsonRpcCall, Result extends ResultConstraint<Request> = ResultConstraint<Request>, Context extends ContextConstraint = MiddlewareContext> = (params: MiddlewareParams<Request, Context>) => Readonly<Result> | undefined | Promise<Readonly<Result> | undefined>;
19
+ type HandleOptions<Context extends MiddlewareContext> = {
20
+ context?: Context;
21
+ };
22
+ type RequestOf<Middleware> = Middleware extends JsonRpcMiddleware<infer Request, ResultConstraint<infer Request>, any> ? Request : never;
23
+ type ContextOf<Middleware> = Middleware extends JsonRpcMiddleware<any, ResultConstraint<any>, infer C> ? C : never;
24
+ declare const INVALID_ENGINE: unique symbol;
25
+ /**
26
+ * An internal type for invalid engines that explains why the engine is invalid.
27
+ *
28
+ * @template Message - The message explaining why the engine is invalid.
29
+ */
30
+ type InvalidEngine<Message extends string> = {
31
+ [INVALID_ENGINE]: Message;
32
+ };
33
+ /**
34
+ * A JSON-RPC request and response processor.
35
+ *
36
+ * Give it a stack of middleware, pass it requests, and get back responses.
37
+ *
38
+ * #### Requests vs. notifications
39
+ *
40
+ * JSON-RPC requests come in two flavors:
41
+ *
42
+ * - [Requests](https://www.jsonrpc.org/specification#request_object), i.e. request objects _with_ an `id`
43
+ * - [Notifications](https://www.jsonrpc.org/specification#notification), i.e. request objects _without_ an `id`
44
+ *
45
+ * For requests, one of the engine's middleware must "end" the request by returning a non-`undefined` result,
46
+ * or {@link handle} will throw an error:
47
+ *
48
+ * For notifications, on the other hand, one of the engine's middleware must return `undefined` to end the request,
49
+ * and any non-`undefined` return values will cause an error:
50
+ *
51
+ * @template Request - The type of request to handle.
52
+ * @template Result - The type of result to return.
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * const engine = JsonRpcEngineV2.create({
57
+ * middleware,
58
+ * });
59
+ *
60
+ * try {
61
+ * const result = await engine.handle(request);
62
+ * // Handle result
63
+ * } catch (error) {
64
+ * // Handle error
65
+ * }
66
+ * ```
67
+ */
68
+ export declare class JsonRpcEngineV2<Request extends JsonRpcCall = JsonRpcCall, Context extends ContextConstraint = MiddlewareContext> {
69
+ #private;
70
+ private constructor();
71
+ /**
72
+ * Create a new JSON-RPC engine.
73
+ *
74
+ * @throws If the middleware array is empty.
75
+ * @param options - The options for the engine.
76
+ * @param options.middleware - The middleware to use.
77
+ * @returns The JSON-RPC engine.
78
+ */
79
+ static create<Middleware extends JsonRpcMiddleware<any, ResultConstraint<any>, any> = JsonRpcMiddleware>({ middleware }: {
80
+ middleware: Middleware[];
81
+ }): MergeContexts<ContextOf<Middleware>> extends never ? InvalidEngine<"Some middleware have incompatible context types"> : JsonRpcEngineV2<RequestOf<Middleware>, MergeContexts<ContextOf<Middleware>>>;
82
+ /**
83
+ * Handle a JSON-RPC request.
84
+ *
85
+ * @param request - The JSON-RPC request to handle.
86
+ * @param options - The options for the handle operation.
87
+ * @param options.context - The context to pass to the middleware.
88
+ * @returns The JSON-RPC response.
89
+ */
90
+ handle(request: Extract<Request, JsonRpcRequest> extends never ? never : Extract<Request, JsonRpcRequest>, options?: HandleOptions<Context>): Promise<Extract<Request, JsonRpcRequest> extends never ? never : ResultConstraint<Request>>;
91
+ /**
92
+ * Handle a JSON-RPC notification. Notifications do not return a result.
93
+ *
94
+ * @param notification - The JSON-RPC notification to handle.
95
+ * @param options - The options for the handle operation.
96
+ * @param options.context - The context to pass to the middleware.
97
+ */
98
+ handle(notification: Extract<Request, JsonRpcNotification> extends never ? never : WithoutId<Extract<Request, JsonRpcNotification>>, options?: HandleOptions<Context>): Promise<Extract<Request, JsonRpcNotification> extends never ? never : ResultConstraint<Request>>;
99
+ /**
100
+ * Handle a JSON-RPC call, i.e. request or notification. Requests return a
101
+ * result, notifications do not.
102
+ *
103
+ * @param call - The JSON-RPC call to handle.
104
+ * @param options - The options for the handle operation.
105
+ * @param options.context - The context to pass to the middleware.
106
+ * @returns The JSON-RPC response, or `undefined` if the call is a notification.
107
+ */
108
+ handle(call: MixedParam<Request>, options?: HandleOptions<Context>): Promise<ResultConstraint<Request> | void>;
109
+ /**
110
+ * Convert the engine into a JSON-RPC middleware.
111
+ *
112
+ * @returns The JSON-RPC middleware.
113
+ */
114
+ asMiddleware(): JsonRpcMiddleware<Request, ResultConstraint<Request>, Context>;
115
+ /**
116
+ * Destroy the engine. Calls the `destroy()` method of any middleware that has
117
+ * one. Attempting to use the engine after destroying it will throw an error.
118
+ */
119
+ destroy(): Promise<void>;
120
+ }
121
+ export {};
122
+ //# sourceMappingURL=JsonRpcEngineV2.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"JsonRpcEngineV2.d.mts","sourceRoot":"","sources":["../../src/v2/JsonRpcEngineV2.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,IAAI,EACT,KAAK,cAAc,EACnB,KAAK,mBAAmB,EAGzB,wBAAwB;AAGzB,OAAO,KAAK,EAAE,iBAAiB,EAAE,aAAa,EAAE,gCAA4B;AAC5E,OAAO,EAAE,iBAAiB,EAAE,gCAA4B;AAOxD,OAAO,KAAK,EAAE,WAAW,EAAE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