@metamask-previews/json-rpc-engine 10.1.1-preview-7bc334fb → 10.1.1-preview-fb233ab2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/README.md +8 -2
- package/dist/v2/JsonRpcEngineV2.cjs.map +1 -1
- package/dist/v2/JsonRpcEngineV2.d.cts +2 -1
- package/dist/v2/JsonRpcEngineV2.d.cts.map +1 -1
- package/dist/v2/JsonRpcEngineV2.d.mts +2 -1
- package/dist/v2/JsonRpcEngineV2.d.mts.map +1 -1
- package/dist/v2/JsonRpcEngineV2.mjs.map +1 -1
- package/dist/v2/JsonRpcServer.cjs +6 -0
- package/dist/v2/JsonRpcServer.cjs.map +1 -1
- package/dist/v2/JsonRpcServer.d.cts +19 -7
- package/dist/v2/JsonRpcServer.d.cts.map +1 -1
- package/dist/v2/JsonRpcServer.d.mts +19 -7
- package/dist/v2/JsonRpcServer.d.mts.map +1 -1
- package/dist/v2/JsonRpcServer.mjs +6 -0
- package/dist/v2/JsonRpcServer.mjs.map +1 -1
- package/dist/v2/index.cjs +3 -16
- package/dist/v2/index.cjs.map +1 -1
- package/dist/v2/index.d.cts +2 -1
- package/dist/v2/index.d.cts.map +1 -1
- package/dist/v2/index.d.mts +2 -1
- package/dist/v2/index.d.mts.map +1 -1
- package/dist/v2/index.mjs +1 -1
- package/dist/v2/index.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
9
9
|
|
|
10
10
|
### Added
|
|
11
11
|
|
|
12
|
-
- `JsonRpcEngineV2` ([#6176](https://github.com/MetaMask/core/pull/6176), [#6971](https://github.com/MetaMask/core/pull/6971), [#6975](https://github.com/MetaMask/core/pull/6975))
|
|
12
|
+
- `JsonRpcEngineV2` ([#6176](https://github.com/MetaMask/core/pull/6176), [#6971](https://github.com/MetaMask/core/pull/6971), [#6975](https://github.com/MetaMask/core/pull/6975), [#6990](https://github.com/MetaMask/core/pull/6990))
|
|
13
13
|
- This is a complete rewrite of `JsonRpcEngine`, intended to replace the original implementation.
|
|
14
14
|
See the readme for details.
|
|
15
15
|
|
package/README.md
CHANGED
|
@@ -551,8 +551,8 @@ The following middleware are incompatible due to mismatched request types:
|
|
|
551
551
|
|
|
552
552
|
> [!WARNING]
|
|
553
553
|
> Providing `JsonRpcRequest`- and `JsonRpcNotification`-only middleware to the same engine is
|
|
554
|
-
> unsound and should be avoided. However, doing so will **not** cause a type error,
|
|
555
|
-
> is the programmer's responsibility to prevent it from happening.
|
|
554
|
+
> generally unsound and should be avoided. However, doing so will **not** cause a type error,
|
|
555
|
+
> and it is the programmer's responsibility to prevent it from happening.
|
|
556
556
|
|
|
557
557
|
```ts
|
|
558
558
|
const middleware1: JsonRpcMiddleware<JsonRpcNotification> = /* ... */;
|
|
@@ -722,6 +722,12 @@ Errors thrown by the underlying engine are always passed to `onError` unmodified
|
|
|
722
722
|
If the request is not a notification, the error is subsequently serialized and attached
|
|
723
723
|
to the response object via the `error` property.
|
|
724
724
|
|
|
725
|
+
> [!WARNING]
|
|
726
|
+
> It is possible to construct a `JsonRpcServer` the only accepts either requests or notifications,
|
|
727
|
+
> but not both. If you do so, it is your responsibility to ensure that the server is only used with the
|
|
728
|
+
> appropriate request objects. `JsonRpcServer.handle()` will not type error at compile time if you attempt to pass
|
|
729
|
+
> it an unsupported request object.
|
|
730
|
+
|
|
725
731
|
## Contributing
|
|
726
732
|
|
|
727
733
|
This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JsonRpcEngineV2.cjs","sourceRoot":"","sources":["../../src/v2/JsonRpcEngineV2.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,2CAMyB;AACzB,4EAA4C;AAG5C,+DAAwD;AACxD,uCAK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AAA,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 = JsonRpcCall,\n Context extends MiddlewareContext = 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"]}
|
|
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;;;;;;;;;;;;;;;;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,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 = JsonRpcCall,\n Context extends MiddlewareContext = 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\nexport type 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\nexport type 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"]}
|
|
@@ -19,8 +19,9 @@ export type JsonRpcMiddleware<Request extends JsonRpcCall = JsonRpcCall, Result
|
|
|
19
19
|
type HandleOptions<Context extends MiddlewareContext> = {
|
|
20
20
|
context?: Context;
|
|
21
21
|
};
|
|
22
|
-
type RequestOf<Middleware> = Middleware extends JsonRpcMiddleware<infer Request, ResultConstraint<infer Request>, any> ? Request : never;
|
|
22
|
+
export type RequestOf<Middleware> = Middleware extends JsonRpcMiddleware<infer Request, ResultConstraint<infer Request>, any> ? Request : never;
|
|
23
23
|
type ContextOf<Middleware> = Middleware extends JsonRpcMiddleware<any, ResultConstraint<any>, infer C> ? C : never;
|
|
24
|
+
export type MergedContextOf<Middleware extends JsonRpcMiddleware<any, any, any>> = MergeContexts<ContextOf<Middleware>>;
|
|
24
25
|
declare const INVALID_ENGINE: unique symbol;
|
|
25
26
|
/**
|
|
26
27
|
* An internal type for invalid engines that explains why the engine is invalid.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JsonRpcEngineV2.d.cts","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,oBAAgB;AAG3C,KAAK,SAAS,CAAC,OAAO,SAAS,WAAW,IAAI,OAAO,GAAG;IAAE,EAAE,CAAC,EAAE,KAAK,CAAA;CAAE,CAAC;AAGvE,KAAK,UAAU,CAAC,OAAO,SAAS,WAAW,IAAI;IAC7C,OAAO,CAAC,OAAO,EAAE,cAAc,CAAC;CACjC,SAAS,CAAC,KAAK,CAAC,GACb,KAAK,GACL,CAAC,OAAO,CAAC,OAAO,EAAE,mBAAmB,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,GACrD,KAAK,GAED,OAAO,CAAC,OAAO,EAAE,cAAc,CAAC,GAChC,SAAS,CAAC,OAAO,CAAC,OAAO,EAAE,mBAAmB,CAAC,CAAC,CAAC;AAE3D,MAAM,MAAM,gBAAgB,CAAC,OAAO,SAAS,WAAW,IACtD,OAAO,SAAS,cAAc,GAAG,IAAI,GAAG,IAAI,CAAC;AAE/C,MAAM,MAAM,IAAI,CAAC,OAAO,SAAS,WAAW,IAAI,CAC9C,OAAO,CAAC,EAAE,QAAQ,CAAC,OAAO,CAAC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AAK,GACL,OAAO,CAAC,OAAO,EAAE,cAAc,CAAC,EACpC,OAAO,CAAC,EAAE,aAAa,CAAC,OAAO,CAAC,GAC/B,OAAO,CACR,OAAO,CAAC,OAAO,EAAE,cAAc,CAAC,SAAS,KAAK,GAC1C,KAAK,GACL,gBAAgB,CAAC,OAAO,CAAC,CAC9B;IAED;;;;;;OAMG;IACG,MAAM,CACV,YAAY,EAAE,OAAO,CAAC,OAAO,EAAE,mBAAmB,CAAC,SAAS,KAAK,GAC7D,KAAK,GACL,SAAS,CAAC,OAAO,CAAC,OAAO,EAAE,mBAAmB,CAAC,CAAC,EACpD,OAAO,CAAC,EAAE,aAAa,CAAC,OAAO,CAAC,GAC/B,OAAO,CACR,OAAO,CAAC,OAAO,EAAE,mBAAmB,CAAC,SAAS,KAAK,GAC/C,KAAK,GACL,gBAAgB,CAAC,OAAO,CAAC,CAC9B;IAED;;;;;;;;OAQG;IACG,MAAM,CACV,IAAI,EAAE,UAAU,CAAC,OAAO,CAAC,EACzB,OAAO,CAAC,EAAE,aAAa,CAAC,OAAO,CAAC,GAC/B,OAAO,CAAC,gBAAgB,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC;IAwK5C;;;;OAIG;IACH,YAAY,IAAI,iBAAiB,CAC/B,OAAO,EACP,gBAAgB,CAAC,OAAO,CAAC,EACzB,OAAO,CACR;IAYD;;;OAGG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;CA2B/B"}
|
|
@@ -19,8 +19,9 @@ export type JsonRpcMiddleware<Request extends JsonRpcCall = JsonRpcCall, Result
|
|
|
19
19
|
type HandleOptions<Context extends MiddlewareContext> = {
|
|
20
20
|
context?: Context;
|
|
21
21
|
};
|
|
22
|
-
type RequestOf<Middleware> = Middleware extends JsonRpcMiddleware<infer Request, ResultConstraint<infer Request>, any> ? Request : never;
|
|
22
|
+
export type RequestOf<Middleware> = Middleware extends JsonRpcMiddleware<infer Request, ResultConstraint<infer Request>, any> ? Request : never;
|
|
23
23
|
type ContextOf<Middleware> = Middleware extends JsonRpcMiddleware<any, ResultConstraint<any>, infer C> ? C : never;
|
|
24
|
+
export type MergedContextOf<Middleware extends JsonRpcMiddleware<any, any, any>> = MergeContexts<ContextOf<Middleware>>;
|
|
24
25
|
declare const INVALID_ENGINE: unique symbol;
|
|
25
26
|
/**
|
|
26
27
|
* An internal type for invalid engines that explains why the engine is invalid.
|
|
@@ -1 +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,oBAAgB;AAG3C,KAAK,SAAS,CAAC,OAAO,SAAS,WAAW,IAAI,OAAO,GAAG;IAAE,EAAE,CAAC,EAAE,KAAK,CAAA;CAAE,CAAC;AAGvE,KAAK,UAAU,CAAC,OAAO,SAAS,WAAW,IAAI;IAC7C,OAAO,CAAC,OAAO,EAAE,cAAc,CAAC;CACjC,SAAS,CAAC,KAAK,CAAC,GACb,KAAK,GACL,CAAC,OAAO,CAAC,OAAO,EAAE,mBAAmB,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,GACrD,KAAK,GAED,OAAO,CAAC,OAAO,EAAE,cAAc,CAAC,GAChC,SAAS,CAAC,OAAO,CAAC,OAAO,EAAE,mBAAmB,CAAC,CAAC,CAAC;AAE3D,MAAM,MAAM,gBAAgB,CAAC,OAAO,SAAS,WAAW,IACtD,OAAO,SAAS,cAAc,GAAG,IAAI,GAAG,IAAI,CAAC;AAE/C,MAAM,MAAM,IAAI,CAAC,OAAO,SAAS,WAAW,IAAI,CAC9C,OAAO,CAAC,EAAE,QAAQ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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 = JsonRpcCall,\n Context extends MiddlewareContext = 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"]}
|
|
1
|
+
{"version":3,"file":"JsonRpcEngineV2.mjs","sourceRoot":"","sources":["../../src/v2/JsonRpcEngineV2.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,OAAO,EAKL,WAAW,EACZ,wBAAwB;AACzB,OAAO,WAAU,2BAA2B;;AAG5C,OAAO,EAAE,iBAAiB,EAAE,gCAA4B;AACxD,OAAO,EACL,cAAc,EACd,SAAS,EACT,kBAAkB,EAClB,SAAS,EACV,oBAAgB;AAmFjB,MAAM,cAAc,GAAG,MAAM,CAAC,gBAAgB,CA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onEmptyArray,\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 = JsonRpcCall,\n Context extends MiddlewareContext = 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\nexport type 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\nexport type 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"]}
|
|
@@ -24,6 +24,9 @@ const jsonrpc = '2.0';
|
|
|
24
24
|
* Essentially wraps a {@link JsonRpcEngineV2} in order to create a conformant
|
|
25
25
|
* yet permissive JSON-RPC 2.0 server.
|
|
26
26
|
*
|
|
27
|
+
* Note that the server will accept both requests and notifications via {@link handle},
|
|
28
|
+
* even if the underlying engine is only able to handle one or the other.
|
|
29
|
+
*
|
|
27
30
|
* @example
|
|
28
31
|
* ```ts
|
|
29
32
|
* const server = new JsonRpcServer({
|
|
@@ -63,6 +66,7 @@ class JsonRpcServer {
|
|
|
63
66
|
__classPrivateFieldSet(this, _JsonRpcServer_engine, options.engine, "f");
|
|
64
67
|
}
|
|
65
68
|
else {
|
|
69
|
+
// @ts-expect-error - TypeScript complains that engine is of the wrong type, but clearly it's not.
|
|
66
70
|
__classPrivateFieldSet(this, _JsonRpcServer_engine, JsonRpcEngineV2_1.JsonRpcEngineV2.create({ middleware: options.middleware }), "f");
|
|
67
71
|
}
|
|
68
72
|
}
|
|
@@ -73,6 +77,8 @@ class JsonRpcServer {
|
|
|
73
77
|
const [originalId, isRequest] = getOriginalId(rawRequest);
|
|
74
78
|
try {
|
|
75
79
|
const request = __classPrivateFieldGet(this, _JsonRpcServer_instances, "m", _JsonRpcServer_coerceRequest).call(this, rawRequest, isRequest);
|
|
80
|
+
// @ts-expect-error - The request may not be of the type expected by the engine,
|
|
81
|
+
// and we intentionally allow this to happen.
|
|
76
82
|
const result = await __classPrivateFieldGet(this, _JsonRpcServer_engine, "f").handle(request);
|
|
77
83
|
if (result !== undefined) {
|
|
78
84
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JsonRpcServer.cjs","sourceRoot":"","sources":["../../src/v2/JsonRpcServer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,qDAAiE;AAQjE,2CAAwD;AAGxD,2DAAoD;AAEpD,oDAA6C;AAe7C,MAAM,OAAO,GAAG,KAAc,CAAC;AAE/B;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAa,aAAa;IAKxB;;;;;;;;;;;;OAYG;IACH,YAAY,OAAgB;;QAjBnB,wCAAyB;QAEzB,yCAA+B;QAgBtC,uBAAA,IAAI,0BAAY,OAAO,CAAC,OAAO,MAAA,CAAC;QAEhC,IAAI,IAAA,mBAAW,EAAC,OAAO,EAAE,QAAQ,CAAC,EAAE;YAClC,2DAA2D;YAC3D,uBAAA,IAAI,yBAAW,OAAO,CAAC,MAAM,MAAA,CAAC;SAC/B;aAAM;YACL,uBAAA,IAAI,yBAAW,iCAAe,CAAC,MAAM,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,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Q,CAAC,EAAE;QACrC,OAAO,CAAC,MAAM,GAAG,UAAU,CAAC,MAAuB,CAAC;KACrD;IAED,IAAI,SAAS,EAAE;QACZ,OAA0B,CAAC,EAAE,GAAG,IAAA,yBAAW,GAAE,CAAC;KAChD;IAED,OAAO,OAAO,CAAC;AACjB,CAAC;AAWH;;;;;GAKG;AACH,SAAS,gBAAgB,CAAC,UAAmB;IAC3C,OAAO,CACL,IAAA,gBAAQ,EAAC,UAAU,CAAC;QACpB,IAAA,mBAAW,EAAC,UAAU,EAAE,QAAQ,CAAC;QACjC,OAAO,UAAU,CAAC,MAAM,KAAK,QAAQ;QACrC,cAAc,CAAC,UAAU,CAAC,CAC3B,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,SAAS,cAAc,CACrB,UAAmC;IAEnC,IAAI,IAAA,mBAAW,EAAC,UAAU,EAAE,QAAQ,CAAC,EAAE;QACrC,OAAO,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,IAAA,gBAAQ,EAAC,UAAU,CAAC,MAAM,CAAC,CAAC;KACxE;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;GAMG;AACH,SAAS,aAAa,CAAC,UAAmB;IACxC,IAAI,IAAA,gBAAQ,EAAC,UAAU,CAAC,IAAI,IAAA,mBAAW,EAAC,UAAU,EAAE,IAAI,CAAC,EAAE;QACzD,OAAO,CAAC,UAAU,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;KAC9B;IAED,OAAO,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;AAC5B,CAAC","sourcesContent":["import { rpcErrors, serializeError } from '@metamask/rpc-errors';\nimport type {\n JsonRpcNotification,\n JsonRpcParams,\n JsonRpcRequest,\n JsonRpcResponse,\n NonEmptyArray,\n} from '@metamask/utils';\nimport { hasProperty, isObject } from '@metamask/utils';\n\nimport type { JsonRpcMiddleware } from './JsonRpcEngineV2';\nimport { JsonRpcEngineV2 } from './JsonRpcEngineV2';\nimport type { JsonRpcCall } from './utils';\nimport { getUniqueId } from '../getUniqueId';\n\ntype OnError = (error: unknown) => void;\n\ntype Options = {\n onError?: OnError;\n} & (\n | {\n engine: JsonRpcEngineV2;\n }\n | {\n middleware: NonEmptyArray<JsonRpcMiddleware>;\n }\n);\n\nconst jsonrpc = '2.0' as const;\n\n/**\n * A JSON-RPC server that handles requests and notifications.\n *\n * Essentially wraps a {@link JsonRpcEngineV2} in order to create a conformant\n * yet permissive JSON-RPC 2.0 server.\n *\n * @example\n * ```ts\n * const server = new JsonRpcServer({\n * engine,\n * onError,\n * });\n *\n * const response = await server.handle(request);\n * if ('result' in response) {\n * // Handle result\n * } else {\n * // Handle error\n * }\n * ```\n */\nexport class JsonRpcServer {\n readonly #engine: JsonRpcEngineV2;\n\n readonly #onError?: OnError | undefined;\n\n /**\n * Construct a new JSON-RPC server.\n *\n * @param options - The options for the server.\n * @param options.onError - The callback to handle errors thrown by the\n * engine. Errors always result in a failed response object, containing a\n * JSON-RPC 2.0 serialized version of the original error. If you need to\n * access the original error, use the `onError` callback.\n * @param options.engine - The engine to use. Mutually exclusive with\n * `middleware`.\n * @param options.middleware - The middleware to use. Mutually exclusive with\n * `engine`.\n */\n constructor(options: Options) {\n this.#onError = options.onError;\n\n if (hasProperty(options, 'engine')) {\n // @ts-expect-error - hasProperty fails to narrow the type.\n this.#engine = options.engine;\n } else {\n this.#engine = JsonRpcEngineV2.create({ middleware: options.middleware });\n }\n }\n\n /**\n * Handle a JSON-RPC request.\n *\n * This method never throws. For requests, a response is always returned.\n * All errors are passed to the engine's `onError` callback.\n *\n * @param request - The request to handle.\n * @returns The JSON-RPC response.\n */\n async handle(request: JsonRpcRequest): Promise<JsonRpcResponse>;\n\n /**\n * Handle a JSON-RPC notification.\n *\n * This method never throws. For notifications, `undefined` is always returned.\n * All errors are passed to the engine's `onError` callback.\n *\n * @param notification - The notification to handle.\n */\n async handle(notification: JsonRpcNotification): Promise<void>;\n\n /**\n * Handle an alleged JSON-RPC request or notification. Permits any plain\n * object with `{ method: string }`, so long as any present JSON-RPC 2.0\n * properties are valid. If the object has no `id`, it will be treated as\n * a notification and vice versa.\n *\n * This method never throws. All errors are passed to the engine's\n * `onError` callback. A JSON-RPC response is always returned for requests,\n * and `undefined` is returned for notifications.\n *\n * @param rawRequest - The raw request to handle.\n * @returns The JSON-RPC response, or `undefined` if the request is a\n * notification.\n */\n async handle(rawRequest: unknown): Promise<JsonRpcResponse | void>;\n\n async handle(rawRequest: unknown): Promise<JsonRpcResponse | void> {\n // If rawRequest is not a notification, the originalId will be attached\n // to the response. We attach our own, trusted id in #coerceRequest()\n // while the request is being handled.\n const [originalId, isRequest] = getOriginalId(rawRequest);\n\n try {\n const request = this.#coerceRequest(rawRequest, isRequest);\n const result = await this.#engine.handle(request);\n\n if (result !== undefined) {\n return {\n jsonrpc,\n // @ts-expect-error - Reassign the original id, regardless of its type.\n id: originalId,\n result,\n };\n }\n } catch (error) {\n this.#onError?.(error);\n\n if (isRequest) {\n return {\n jsonrpc,\n // @ts-expect-error - Reassign the original id, regardless of its type.\n id: originalId,\n error: serializeError(error, {\n shouldIncludeStack: false,\n shouldPreserveMessage: true,\n }),\n };\n }\n }\n return undefined;\n }\n\n #coerceRequest(rawRequest: unknown, isRequest: boolean): JsonRpcCall {\n if (!isMinimalRequest(rawRequest)) {\n throw rpcErrors.invalidRequest({\n data: {\n request: rawRequest,\n },\n });\n }\n\n const request: JsonRpcCall = {\n jsonrpc,\n method: rawRequest.method,\n };\n\n if (hasProperty(rawRequest, 'params')) {\n request.params = rawRequest.params as JsonRpcParams;\n }\n\n if (isRequest) {\n (request as JsonRpcRequest).id = getUniqueId();\n }\n\n return request;\n }\n}\n\n/**\n * The most minimally conformant request object that we will accept.\n */\ntype MinimalRequest = {\n method: string;\n params?: JsonRpcParams;\n} & Record<string, unknown>;\n\n/**\n * Check if an unvalidated request is a minimal request.\n *\n * @param rawRequest - The raw request to check.\n * @returns `true` if the request is a {@link MinimalRequest}, `false` otherwise.\n */\nfunction isMinimalRequest(rawRequest: unknown): rawRequest is MinimalRequest {\n return (\n isObject(rawRequest) &&\n hasProperty(rawRequest, 'method') &&\n typeof rawRequest.method === 'string' &&\n hasValidParams(rawRequest)\n );\n}\n\n/**\n * Check if a request has valid params, i.e. an array or object.\n * The contents of the params are not inspected.\n *\n * @param rawRequest - The request to check.\n * @returns `true` if the request has valid params, `false` otherwise.\n */\nfunction hasValidParams(\n rawRequest: Record<string, unknown>,\n): rawRequest is { params?: JsonRpcParams } {\n if (hasProperty(rawRequest, 'params')) {\n return Array.isArray(rawRequest.params) || isObject(rawRequest.params);\n }\n\n return true;\n}\n\n/**\n * Get the original id from a request.\n *\n * @param rawRequest - The request to get the original id from.\n * @returns The original id and a boolean indicating if the request is a request\n * (as opposed to a notification).\n */\nfunction getOriginalId(rawRequest: unknown): [unknown, boolean] {\n if (isObject(rawRequest) && hasProperty(rawRequest, 'id')) {\n return [rawRequest.id, true];\n }\n\n return [undefined, false];\n}\n"]}
|
|
1
|
+
{"version":3,"file":"JsonRpcServer.cjs","sourceRoot":"","sources":["../../src/v2/JsonRpcServer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,qDAAiE;AAQjE,2CAAwD;AAQxD,2DAAoD;AAEpD,oDAA6C;AAe7C,MAAM,OAAO,GAAG,KAAc,CAAC;AAE/B;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAa,aAAa;IAiBxB;;;;;;;;;;;;OAYG;IACH,YAAY,OAA4B;;QApB/B,wCAGP;QAEO,yCAA+B;QAgBtC,uBAAA,IAAI,0BAAY,OAAO,CAAC,OAAO,MAAA,CAAC;QAEhC,IAAI,IAAA,mBAAW,EAAC,OAAO,EAAE,QAAQ,CAAC,EAAE;YAClC,2DAA2D;YAC3D,uBAAA,IAAI,yBAAW,OAAO,CAAC,MAAM,MAAA,CAAC;SAC/B;aAAM;YACL,kGAAkG;YAClG,uBAAA,IAAI,yBAAW,iCAAe,CAAC,MAAM,CA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AU,CAAC,MAAM;KAC1B,CAAC;IAEF,IAAI,IAAA,mBAAW,EAAC,UAAU,EAAE,QAAQ,CAAC,EAAE;QACrC,OAAO,CAAC,MAAM,GAAG,UAAU,CAAC,MAAuB,CAAC;KACrD;IAED,IAAI,SAAS,EAAE;QACZ,OAA0B,CAAC,EAAE,GAAG,IAAA,yBAAW,GAAE,CAAC;KAChD;IAED,OAAO,OAAO,CAAC;AACjB,CAAC;AAWH;;;;;GAKG;AACH,SAAS,gBAAgB,CAAC,UAAmB;IAC3C,OAAO,CACL,IAAA,gBAAQ,EAAC,UAAU,CAAC;QACpB,IAAA,mBAAW,EAAC,UAAU,EAAE,QAAQ,CAAC;QACjC,OAAO,UAAU,CAAC,MAAM,KAAK,QAAQ;QACrC,cAAc,CAAC,UAAU,CAAC,CAC3B,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,SAAS,cAAc,CACrB,UAAmC;IAEnC,IAAI,IAAA,mBAAW,EAAC,UAAU,EAAE,QAAQ,CAAC,EAAE;QACrC,OAAO,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,IAAA,gBAAQ,EAAC,UAAU,CAAC,MAAM,CAAC,CAAC;KACxE;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;GAMG;AACH,SAAS,aAAa,CAAC,UAAmB;IACxC,IAAI,IAAA,gBAAQ,EAAC,UAAU,CAAC,IAAI,IAAA,mBAAW,EAAC,UAAU,EAAE,IAAI,CAAC,EAAE;QACzD,OAAO,CAAC,UAAU,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;KAC9B;IACD,OAAO,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;AAC5B,CAAC","sourcesContent":["import { rpcErrors, serializeError } from '@metamask/rpc-errors';\nimport type {\n JsonRpcNotification,\n JsonRpcParams,\n JsonRpcRequest,\n JsonRpcResponse,\n NonEmptyArray,\n} from '@metamask/utils';\nimport { hasProperty, isObject } from '@metamask/utils';\n\nimport type {\n JsonRpcMiddleware,\n MergedContextOf,\n RequestOf,\n ResultConstraint,\n} from './JsonRpcEngineV2';\nimport { JsonRpcEngineV2 } from './JsonRpcEngineV2';\nimport type { JsonRpcCall } from './utils';\nimport { getUniqueId } from '../getUniqueId';\n\ntype OnError = (error: unknown) => void;\n\ntype Options<Middleware extends JsonRpcMiddleware> = {\n onError?: OnError;\n} & (\n | {\n engine: JsonRpcEngineV2;\n }\n | {\n middleware: NonEmptyArray<Middleware>;\n }\n);\n\nconst jsonrpc = '2.0' as const;\n\n/**\n * A JSON-RPC server that handles requests and notifications.\n *\n * Essentially wraps a {@link JsonRpcEngineV2} in order to create a conformant\n * yet permissive JSON-RPC 2.0 server.\n *\n * Note that the server will accept both requests and notifications via {@link handle},\n * even if the underlying engine is only able to handle one or the other.\n *\n * @example\n * ```ts\n * const server = new JsonRpcServer({\n * engine,\n * onError,\n * });\n *\n * const response = await server.handle(request);\n * if ('result' in response) {\n * // Handle result\n * } else {\n * // Handle error\n * }\n * ```\n */\nexport class JsonRpcServer<\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> {\n readonly #engine: JsonRpcEngineV2<\n RequestOf<Middleware>,\n MergedContextOf<Middleware>\n >;\n\n readonly #onError?: OnError | undefined;\n\n /**\n * Construct a new JSON-RPC server.\n *\n * @param options - The options for the server.\n * @param options.onError - The callback to handle errors thrown by the\n * engine. Errors always result in a failed response object, containing a\n * JSON-RPC 2.0 serialized version of the original error. If you need to\n * access the original error, use the `onError` callback.\n * @param options.engine - The engine to use. Mutually exclusive with\n * `middleware`.\n * @param options.middleware - The middleware to use. Mutually exclusive with\n * `engine`.\n */\n constructor(options: Options<Middleware>) {\n this.#onError = options.onError;\n\n if (hasProperty(options, 'engine')) {\n // @ts-expect-error - hasProperty fails to narrow the type.\n this.#engine = options.engine;\n } else {\n // @ts-expect-error - TypeScript complains that engine is of the wrong type, but clearly it's not.\n this.#engine = JsonRpcEngineV2.create({ middleware: options.middleware });\n }\n }\n\n /**\n * Handle a JSON-RPC request.\n *\n * This method never throws. For requests, a response is always returned.\n * All errors are passed to the engine's `onError` callback.\n *\n * **WARNING**: This method is unaware of the request type of the underlying\n * engine. The request will fail if the engine can only handle notifications.\n *\n * @param request - The request to handle.\n * @returns The JSON-RPC response.\n */\n async handle(request: JsonRpcRequest): Promise<JsonRpcResponse>;\n\n /**\n * Handle a JSON-RPC notification.\n *\n * This method never throws. For notifications, `undefined` is always returned.\n * All errors are passed to the engine's `onError` callback.\n *\n * **WARNING**: This method is unaware of the request type of the underlying\n * engine. The request will fail if the engine cannot handle notifications.\n *\n * @param notification - The notification to handle.\n */\n async handle(notification: JsonRpcNotification): Promise<void>;\n\n /**\n * Handle an alleged JSON-RPC request or notification. Permits any plain\n * object with `{ method: string }`, so long as any present JSON-RPC 2.0\n * properties are valid. If the object has an `id` property, it will be\n * treated as a request, otherwise it will be treated as a notification.\n *\n * This method never throws. All errors are passed to the engine's\n * `onError` callback. A JSON-RPC response is always returned for requests,\n * and `undefined` is returned for notifications.\n *\n * **WARNING**: The request will fail if its coerced type (i.e. request or\n * response) is not of the type expected by the underlying engine.\n *\n * @param rawRequest - The raw request to handle.\n * @returns The JSON-RPC response, or `undefined` if the request is a\n * notification.\n */\n async handle(rawRequest: unknown): Promise<JsonRpcResponse | void>;\n\n async handle(rawRequest: unknown): Promise<JsonRpcResponse | void> {\n // If rawRequest is not a notification, the originalId will be attached\n // to the response. We attach our own, trusted id in #coerceRequest()\n // while the request is being handled.\n const [originalId, isRequest] = getOriginalId(rawRequest);\n\n try {\n const request = this.#coerceRequest(rawRequest, isRequest);\n // @ts-expect-error - The request may not be of the type expected by the engine,\n // and we intentionally allow this to happen.\n const result = await this.#engine.handle(request);\n\n if (result !== undefined) {\n return {\n jsonrpc,\n // @ts-expect-error - Reassign the original id, regardless of its type.\n id: originalId,\n result,\n };\n }\n } catch (error) {\n this.#onError?.(error);\n\n if (isRequest) {\n return {\n jsonrpc,\n // @ts-expect-error - Reassign the original id, regardless of its type.\n id: originalId,\n error: serializeError(error, {\n shouldIncludeStack: false,\n shouldPreserveMessage: true,\n }),\n };\n }\n }\n return undefined;\n }\n\n #coerceRequest(rawRequest: unknown, isRequest: boolean): JsonRpcCall {\n if (!isMinimalRequest(rawRequest)) {\n throw rpcErrors.invalidRequest({\n data: {\n request: rawRequest,\n },\n });\n }\n\n const request: JsonRpcCall = {\n jsonrpc,\n method: rawRequest.method,\n };\n\n if (hasProperty(rawRequest, 'params')) {\n request.params = rawRequest.params as JsonRpcParams;\n }\n\n if (isRequest) {\n (request as JsonRpcRequest).id = getUniqueId();\n }\n\n return request;\n }\n}\n\n/**\n * The most minimally conformant request object that we will accept.\n */\ntype MinimalRequest = {\n method: string;\n params?: JsonRpcParams;\n} & Record<string, unknown>;\n\n/**\n * Check if an unvalidated request is a minimal request.\n *\n * @param rawRequest - The raw request to check.\n * @returns `true` if the request is a {@link MinimalRequest}, `false` otherwise.\n */\nfunction isMinimalRequest(rawRequest: unknown): rawRequest is MinimalRequest {\n return (\n isObject(rawRequest) &&\n hasProperty(rawRequest, 'method') &&\n typeof rawRequest.method === 'string' &&\n hasValidParams(rawRequest)\n );\n}\n\n/**\n * Check if a request has valid params, i.e. an array or object.\n * The contents of the params are not inspected.\n *\n * @param rawRequest - The request to check.\n * @returns `true` if the request has valid params, `false` otherwise.\n */\nfunction hasValidParams(\n rawRequest: Record<string, unknown>,\n): rawRequest is { params?: JsonRpcParams } {\n if (hasProperty(rawRequest, 'params')) {\n return Array.isArray(rawRequest.params) || isObject(rawRequest.params);\n }\n return true;\n}\n\n/**\n * Get the original id from a request.\n *\n * @param rawRequest - The request to get the original id from.\n * @returns The original id and a boolean indicating if the request is a request\n * (as opposed to a notification).\n */\nfunction getOriginalId(rawRequest: unknown): [unknown, boolean] {\n if (isObject(rawRequest) && hasProperty(rawRequest, 'id')) {\n return [rawRequest.id, true];\n }\n return [undefined, false];\n}\n"]}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import type { JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, NonEmptyArray } from "@metamask/utils";
|
|
2
|
-
import type { JsonRpcMiddleware } from "./JsonRpcEngineV2.cjs";
|
|
2
|
+
import type { JsonRpcMiddleware, ResultConstraint } from "./JsonRpcEngineV2.cjs";
|
|
3
3
|
import { JsonRpcEngineV2 } from "./JsonRpcEngineV2.cjs";
|
|
4
4
|
type OnError = (error: unknown) => void;
|
|
5
|
-
type Options = {
|
|
5
|
+
type Options<Middleware extends JsonRpcMiddleware> = {
|
|
6
6
|
onError?: OnError;
|
|
7
7
|
} & ({
|
|
8
8
|
engine: JsonRpcEngineV2;
|
|
9
9
|
} | {
|
|
10
|
-
middleware: NonEmptyArray<
|
|
10
|
+
middleware: NonEmptyArray<Middleware>;
|
|
11
11
|
});
|
|
12
12
|
/**
|
|
13
13
|
* A JSON-RPC server that handles requests and notifications.
|
|
@@ -15,6 +15,9 @@ type Options = {
|
|
|
15
15
|
* Essentially wraps a {@link JsonRpcEngineV2} in order to create a conformant
|
|
16
16
|
* yet permissive JSON-RPC 2.0 server.
|
|
17
17
|
*
|
|
18
|
+
* Note that the server will accept both requests and notifications via {@link handle},
|
|
19
|
+
* even if the underlying engine is only able to handle one or the other.
|
|
20
|
+
*
|
|
18
21
|
* @example
|
|
19
22
|
* ```ts
|
|
20
23
|
* const server = new JsonRpcServer({
|
|
@@ -30,7 +33,7 @@ type Options = {
|
|
|
30
33
|
* }
|
|
31
34
|
* ```
|
|
32
35
|
*/
|
|
33
|
-
export declare class JsonRpcServer {
|
|
36
|
+
export declare class JsonRpcServer<Middleware extends JsonRpcMiddleware<any, ResultConstraint<any>, any> = JsonRpcMiddleware> {
|
|
34
37
|
#private;
|
|
35
38
|
/**
|
|
36
39
|
* Construct a new JSON-RPC server.
|
|
@@ -45,13 +48,16 @@ export declare class JsonRpcServer {
|
|
|
45
48
|
* @param options.middleware - The middleware to use. Mutually exclusive with
|
|
46
49
|
* `engine`.
|
|
47
50
|
*/
|
|
48
|
-
constructor(options: Options);
|
|
51
|
+
constructor(options: Options<Middleware>);
|
|
49
52
|
/**
|
|
50
53
|
* Handle a JSON-RPC request.
|
|
51
54
|
*
|
|
52
55
|
* This method never throws. For requests, a response is always returned.
|
|
53
56
|
* All errors are passed to the engine's `onError` callback.
|
|
54
57
|
*
|
|
58
|
+
* **WARNING**: This method is unaware of the request type of the underlying
|
|
59
|
+
* engine. The request will fail if the engine can only handle notifications.
|
|
60
|
+
*
|
|
55
61
|
* @param request - The request to handle.
|
|
56
62
|
* @returns The JSON-RPC response.
|
|
57
63
|
*/
|
|
@@ -62,19 +68,25 @@ export declare class JsonRpcServer {
|
|
|
62
68
|
* This method never throws. For notifications, `undefined` is always returned.
|
|
63
69
|
* All errors are passed to the engine's `onError` callback.
|
|
64
70
|
*
|
|
71
|
+
* **WARNING**: This method is unaware of the request type of the underlying
|
|
72
|
+
* engine. The request will fail if the engine cannot handle notifications.
|
|
73
|
+
*
|
|
65
74
|
* @param notification - The notification to handle.
|
|
66
75
|
*/
|
|
67
76
|
handle(notification: JsonRpcNotification): Promise<void>;
|
|
68
77
|
/**
|
|
69
78
|
* Handle an alleged JSON-RPC request or notification. Permits any plain
|
|
70
79
|
* object with `{ method: string }`, so long as any present JSON-RPC 2.0
|
|
71
|
-
* properties are valid. If the object has
|
|
72
|
-
* a
|
|
80
|
+
* properties are valid. If the object has an `id` property, it will be
|
|
81
|
+
* treated as a request, otherwise it will be treated as a notification.
|
|
73
82
|
*
|
|
74
83
|
* This method never throws. All errors are passed to the engine's
|
|
75
84
|
* `onError` callback. A JSON-RPC response is always returned for requests,
|
|
76
85
|
* and `undefined` is returned for notifications.
|
|
77
86
|
*
|
|
87
|
+
* **WARNING**: The request will fail if its coerced type (i.e. request or
|
|
88
|
+
* response) is not of the type expected by the underlying engine.
|
|
89
|
+
*
|
|
78
90
|
* @param rawRequest - The raw request to handle.
|
|
79
91
|
* @returns The JSON-RPC response, or `undefined` if the request is a
|
|
80
92
|
* notification.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JsonRpcServer.d.cts","sourceRoot":"","sources":["../../src/v2/JsonRpcServer.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,mBAAmB,EAEnB,cAAc,EACd,eAAe,EACf,aAAa,EACd,wBAAwB;AAGzB,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"JsonRpcServer.d.cts","sourceRoot":"","sources":["../../src/v2/JsonRpcServer.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,mBAAmB,EAEnB,cAAc,EACd,eAAe,EACf,aAAa,EACd,wBAAwB;AAGzB,OAAO,KAAK,EACV,iBAAiB,EAGjB,gBAAgB,EACjB,8BAA0B;AAC3B,OAAO,EAAE,eAAe,EAAE,8BAA0B;AAIpD,KAAK,OAAO,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;AAExC,KAAK,OAAO,CAAC,UAAU,SAAS,iBAAiB,IAAI;IACnD,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,GAAG,CACA;IACE,MAAM,EAAE,eAAe,CAAC;CACzB,GACD;IACE,UAAU,EAAE,aAAa,CAAC,UAAU,CAAC,CAAC;CACvC,CACJ,CAAC;AAIF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBAAa,aAAa,CACxB,UAAU,SAAS,iBAAiB,CAGlC,GAAG,EACH,gBAAgB,CAAC,GAAG,CAAC,EACrB,GAAG,CAEJ,GAAG,iBAAiB;;IASrB;;;;;;;;;;;;OAYG;gBACS,OAAO,EAAE,OAAO,CAAC,UAAU,CAAC;IAYxC;;;;;;;;;;;OAWG;IACG,MAAM,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,eAAe,CAAC;IAE/D;;;;;;;;;;OAUG;IACG,MAAM,CAAC,YAAY,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IAE9D;;;;;;;;;;;;;;;;OAgBG;IACG,MAAM,CAAC,UAAU,EAAE,OAAO,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC;CAgEnE"}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import type { JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, NonEmptyArray } from "@metamask/utils";
|
|
2
|
-
import type { JsonRpcMiddleware } from "./JsonRpcEngineV2.mjs";
|
|
2
|
+
import type { JsonRpcMiddleware, ResultConstraint } from "./JsonRpcEngineV2.mjs";
|
|
3
3
|
import { JsonRpcEngineV2 } from "./JsonRpcEngineV2.mjs";
|
|
4
4
|
type OnError = (error: unknown) => void;
|
|
5
|
-
type Options = {
|
|
5
|
+
type Options<Middleware extends JsonRpcMiddleware> = {
|
|
6
6
|
onError?: OnError;
|
|
7
7
|
} & ({
|
|
8
8
|
engine: JsonRpcEngineV2;
|
|
9
9
|
} | {
|
|
10
|
-
middleware: NonEmptyArray<
|
|
10
|
+
middleware: NonEmptyArray<Middleware>;
|
|
11
11
|
});
|
|
12
12
|
/**
|
|
13
13
|
* A JSON-RPC server that handles requests and notifications.
|
|
@@ -15,6 +15,9 @@ type Options = {
|
|
|
15
15
|
* Essentially wraps a {@link JsonRpcEngineV2} in order to create a conformant
|
|
16
16
|
* yet permissive JSON-RPC 2.0 server.
|
|
17
17
|
*
|
|
18
|
+
* Note that the server will accept both requests and notifications via {@link handle},
|
|
19
|
+
* even if the underlying engine is only able to handle one or the other.
|
|
20
|
+
*
|
|
18
21
|
* @example
|
|
19
22
|
* ```ts
|
|
20
23
|
* const server = new JsonRpcServer({
|
|
@@ -30,7 +33,7 @@ type Options = {
|
|
|
30
33
|
* }
|
|
31
34
|
* ```
|
|
32
35
|
*/
|
|
33
|
-
export declare class JsonRpcServer {
|
|
36
|
+
export declare class JsonRpcServer<Middleware extends JsonRpcMiddleware<any, ResultConstraint<any>, any> = JsonRpcMiddleware> {
|
|
34
37
|
#private;
|
|
35
38
|
/**
|
|
36
39
|
* Construct a new JSON-RPC server.
|
|
@@ -45,13 +48,16 @@ export declare class JsonRpcServer {
|
|
|
45
48
|
* @param options.middleware - The middleware to use. Mutually exclusive with
|
|
46
49
|
* `engine`.
|
|
47
50
|
*/
|
|
48
|
-
constructor(options: Options);
|
|
51
|
+
constructor(options: Options<Middleware>);
|
|
49
52
|
/**
|
|
50
53
|
* Handle a JSON-RPC request.
|
|
51
54
|
*
|
|
52
55
|
* This method never throws. For requests, a response is always returned.
|
|
53
56
|
* All errors are passed to the engine's `onError` callback.
|
|
54
57
|
*
|
|
58
|
+
* **WARNING**: This method is unaware of the request type of the underlying
|
|
59
|
+
* engine. The request will fail if the engine can only handle notifications.
|
|
60
|
+
*
|
|
55
61
|
* @param request - The request to handle.
|
|
56
62
|
* @returns The JSON-RPC response.
|
|
57
63
|
*/
|
|
@@ -62,19 +68,25 @@ export declare class JsonRpcServer {
|
|
|
62
68
|
* This method never throws. For notifications, `undefined` is always returned.
|
|
63
69
|
* All errors are passed to the engine's `onError` callback.
|
|
64
70
|
*
|
|
71
|
+
* **WARNING**: This method is unaware of the request type of the underlying
|
|
72
|
+
* engine. The request will fail if the engine cannot handle notifications.
|
|
73
|
+
*
|
|
65
74
|
* @param notification - The notification to handle.
|
|
66
75
|
*/
|
|
67
76
|
handle(notification: JsonRpcNotification): Promise<void>;
|
|
68
77
|
/**
|
|
69
78
|
* Handle an alleged JSON-RPC request or notification. Permits any plain
|
|
70
79
|
* object with `{ method: string }`, so long as any present JSON-RPC 2.0
|
|
71
|
-
* properties are valid. If the object has
|
|
72
|
-
* a
|
|
80
|
+
* properties are valid. If the object has an `id` property, it will be
|
|
81
|
+
* treated as a request, otherwise it will be treated as a notification.
|
|
73
82
|
*
|
|
74
83
|
* This method never throws. All errors are passed to the engine's
|
|
75
84
|
* `onError` callback. A JSON-RPC response is always returned for requests,
|
|
76
85
|
* and `undefined` is returned for notifications.
|
|
77
86
|
*
|
|
87
|
+
* **WARNING**: The request will fail if its coerced type (i.e. request or
|
|
88
|
+
* response) is not of the type expected by the underlying engine.
|
|
89
|
+
*
|
|
78
90
|
* @param rawRequest - The raw request to handle.
|
|
79
91
|
* @returns The JSON-RPC response, or `undefined` if the request is a
|
|
80
92
|
* notification.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JsonRpcServer.d.mts","sourceRoot":"","sources":["../../src/v2/JsonRpcServer.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,mBAAmB,EAEnB,cAAc,EACd,eAAe,EACf,aAAa,EACd,wBAAwB;AAGzB,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"JsonRpcServer.d.mts","sourceRoot":"","sources":["../../src/v2/JsonRpcServer.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,mBAAmB,EAEnB,cAAc,EACd,eAAe,EACf,aAAa,EACd,wBAAwB;AAGzB,OAAO,KAAK,EACV,iBAAiB,EAGjB,gBAAgB,EACjB,8BAA0B;AAC3B,OAAO,EAAE,eAAe,EAAE,8BAA0B;AAIpD,KAAK,OAAO,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;AAExC,KAAK,OAAO,CAAC,UAAU,SAAS,iBAAiB,IAAI;IACnD,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,GAAG,CACA;IACE,MAAM,EAAE,eAAe,CAAC;CACzB,GACD;IACE,UAAU,EAAE,aAAa,CAAC,UAAU,CAAC,CAAC;CACvC,CACJ,CAAC;AAIF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBAAa,aAAa,CACxB,UAAU,SAAS,iBAAiB,CAGlC,GAAG,EACH,gBAAgB,CAAC,GAAG,CAAC,EACrB,GAAG,CAEJ,GAAG,iBAAiB;;IASrB;;;;;;;;;;;;OAYG;gBACS,OAAO,EAAE,OAAO,CAAC,UAAU,CAAC;IAYxC;;;;;;;;;;;OAWG;IACG,MAAM,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,eAAe,CAAC;IAE/D;;;;;;;;;;OAUG;IACG,MAAM,CAAC,YAAY,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IAE9D;;;;;;;;;;;;;;;;OAgBG;IACG,MAAM,CAAC,UAAU,EAAE,OAAO,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC;CAgEnE"}
|
|
@@ -21,6 +21,9 @@ const jsonrpc = '2.0';
|
|
|
21
21
|
* Essentially wraps a {@link JsonRpcEngineV2} in order to create a conformant
|
|
22
22
|
* yet permissive JSON-RPC 2.0 server.
|
|
23
23
|
*
|
|
24
|
+
* Note that the server will accept both requests and notifications via {@link handle},
|
|
25
|
+
* even if the underlying engine is only able to handle one or the other.
|
|
26
|
+
*
|
|
24
27
|
* @example
|
|
25
28
|
* ```ts
|
|
26
29
|
* const server = new JsonRpcServer({
|
|
@@ -60,6 +63,7 @@ export class JsonRpcServer {
|
|
|
60
63
|
__classPrivateFieldSet(this, _JsonRpcServer_engine, options.engine, "f");
|
|
61
64
|
}
|
|
62
65
|
else {
|
|
66
|
+
// @ts-expect-error - TypeScript complains that engine is of the wrong type, but clearly it's not.
|
|
63
67
|
__classPrivateFieldSet(this, _JsonRpcServer_engine, JsonRpcEngineV2.create({ middleware: options.middleware }), "f");
|
|
64
68
|
}
|
|
65
69
|
}
|
|
@@ -70,6 +74,8 @@ export class JsonRpcServer {
|
|
|
70
74
|
const [originalId, isRequest] = getOriginalId(rawRequest);
|
|
71
75
|
try {
|
|
72
76
|
const request = __classPrivateFieldGet(this, _JsonRpcServer_instances, "m", _JsonRpcServer_coerceRequest).call(this, rawRequest, isRequest);
|
|
77
|
+
// @ts-expect-error - The request may not be of the type expected by the engine,
|
|
78
|
+
// and we intentionally allow this to happen.
|
|
73
79
|
const result = await __classPrivateFieldGet(this, _JsonRpcServer_engine, "f").handle(request);
|
|
74
80
|
if (result !== undefined) {
|
|
75
81
|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AAI,WAAW,CAAC,UAAU,EAAE,QAAQ,CAAC,EAAE;QACrC,OAAO,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,QAAQ,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;KACxE;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;GAMG;AACH,SAAS,aAAa,CAAC,UAAmB;IACxC,IAAI,QAAQ,CAAC,UAAU,CAAC,IAAI,WAAW,CAAC,UAAU,EAAE,IAAI,CAAC,EAAE;QACzD,OAAO,CAAC,UAAU,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;KAC9B;IAED,OAAO,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;AAC5B,CAAC","sourcesContent":["import { rpcErrors, serializeError } from '@metamask/rpc-errors';\nimport type {\n JsonRpcNotification,\n JsonRpcParams,\n JsonRpcRequest,\n JsonRpcResponse,\n NonEmptyArray,\n} from '@metamask/utils';\nimport { hasProperty, isObject } from '@metamask/utils';\n\nimport type { JsonRpcMiddleware } from './JsonRpcEngineV2';\nimport { JsonRpcEngineV2 } from './JsonRpcEngineV2';\nimport type { JsonRpcCall } from './utils';\nimport { getUniqueId } from '../getUniqueId';\n\ntype OnError = (error: unknown) => void;\n\ntype Options = {\n onError?: OnError;\n} & (\n | {\n engine: JsonRpcEngineV2;\n }\n | {\n middleware: NonEmptyArray<JsonRpcMiddleware>;\n }\n);\n\nconst jsonrpc = '2.0' as const;\n\n/**\n * A JSON-RPC server that handles requests and notifications.\n *\n * Essentially wraps a {@link JsonRpcEngineV2} in order to create a conformant\n * yet permissive JSON-RPC 2.0 server.\n *\n * @example\n * ```ts\n * const server = new JsonRpcServer({\n * engine,\n * onError,\n * });\n *\n * const response = await server.handle(request);\n * if ('result' in response) {\n * // Handle result\n * } else {\n * // Handle error\n * }\n * ```\n */\nexport class JsonRpcServer {\n readonly #engine: JsonRpcEngineV2;\n\n readonly #onError?: OnError | undefined;\n\n /**\n * Construct a new JSON-RPC server.\n *\n * @param options - The options for the server.\n * @param options.onError - The callback to handle errors thrown by the\n * engine. Errors always result in a failed response object, containing a\n * JSON-RPC 2.0 serialized version of the original error. If you need to\n * access the original error, use the `onError` callback.\n * @param options.engine - The engine to use. Mutually exclusive with\n * `middleware`.\n * @param options.middleware - The middleware to use. Mutually exclusive with\n * `engine`.\n */\n constructor(options: Options) {\n this.#onError = options.onError;\n\n if (hasProperty(options, 'engine')) {\n // @ts-expect-error - hasProperty fails to narrow the type.\n this.#engine = options.engine;\n } else {\n this.#engine = JsonRpcEngineV2.create({ middleware: options.middleware });\n }\n }\n\n /**\n * Handle a JSON-RPC request.\n *\n * This method never throws. For requests, a response is always returned.\n * All errors are passed to the engine's `onError` callback.\n *\n * @param request - The request to handle.\n * @returns The JSON-RPC response.\n */\n async handle(request: JsonRpcRequest): Promise<JsonRpcResponse>;\n\n /**\n * Handle a JSON-RPC notification.\n *\n * This method never throws. For notifications, `undefined` is always returned.\n * All errors are passed to the engine's `onError` callback.\n *\n * @param notification - The notification to handle.\n */\n async handle(notification: JsonRpcNotification): Promise<void>;\n\n /**\n * Handle an alleged JSON-RPC request or notification. Permits any plain\n * object with `{ method: string }`, so long as any present JSON-RPC 2.0\n * properties are valid. If the object has no `id`, it will be treated as\n * a notification and vice versa.\n *\n * This method never throws. All errors are passed to the engine's\n * `onError` callback. A JSON-RPC response is always returned for requests,\n * and `undefined` is returned for notifications.\n *\n * @param rawRequest - The raw request to handle.\n * @returns The JSON-RPC response, or `undefined` if the request is a\n * notification.\n */\n async handle(rawRequest: unknown): Promise<JsonRpcResponse | void>;\n\n async handle(rawRequest: unknown): Promise<JsonRpcResponse | void> {\n // If rawRequest is not a notification, the originalId will be attached\n // to the response. We attach our own, trusted id in #coerceRequest()\n // while the request is being handled.\n const [originalId, isRequest] = getOriginalId(rawRequest);\n\n try {\n const request = this.#coerceRequest(rawRequest, isRequest);\n const result = await this.#engine.handle(request);\n\n if (result !== undefined) {\n return {\n jsonrpc,\n // @ts-expect-error - Reassign the original id, regardless of its type.\n id: originalId,\n result,\n };\n }\n } catch (error) {\n this.#onError?.(error);\n\n if (isRequest) {\n return {\n jsonrpc,\n // @ts-expect-error - Reassign the original id, regardless of its type.\n id: originalId,\n error: serializeError(error, {\n shouldIncludeStack: false,\n shouldPreserveMessage: true,\n }),\n };\n }\n }\n return undefined;\n }\n\n #coerceRequest(rawRequest: unknown, isRequest: boolean): JsonRpcCall {\n if (!isMinimalRequest(rawRequest)) {\n throw rpcErrors.invalidRequest({\n data: {\n request: rawRequest,\n },\n });\n }\n\n const request: JsonRpcCall = {\n jsonrpc,\n method: rawRequest.method,\n };\n\n if (hasProperty(rawRequest, 'params')) {\n request.params = rawRequest.params as JsonRpcParams;\n }\n\n if (isRequest) {\n (request as JsonRpcRequest).id = getUniqueId();\n }\n\n return request;\n }\n}\n\n/**\n * The most minimally conformant request object that we will accept.\n */\ntype MinimalRequest = {\n method: string;\n params?: JsonRpcParams;\n} & Record<string, unknown>;\n\n/**\n * Check if an unvalidated request is a minimal request.\n *\n * @param rawRequest - The raw request to check.\n * @returns `true` if the request is a {@link MinimalRequest}, `false` otherwise.\n */\nfunction isMinimalRequest(rawRequest: unknown): rawRequest is MinimalRequest {\n return (\n isObject(rawRequest) &&\n hasProperty(rawRequest, 'method') &&\n typeof rawRequest.method === 'string' &&\n hasValidParams(rawRequest)\n );\n}\n\n/**\n * Check if a request has valid params, i.e. an array or object.\n * The contents of the params are not inspected.\n *\n * @param rawRequest - The request to check.\n * @returns `true` if the request has valid params, `false` otherwise.\n */\nfunction hasValidParams(\n rawRequest: Record<string, unknown>,\n): rawRequest is { params?: JsonRpcParams } {\n if (hasProperty(rawRequest, 'params')) {\n return Array.isArray(rawRequest.params) || isObject(rawRequest.params);\n }\n\n return true;\n}\n\n/**\n * Get the original id from a request.\n *\n * @param rawRequest - The request to get the original id from.\n * @returns The original id and a boolean indicating if the request is a request\n * (as opposed to a notification).\n */\nfunction getOriginalId(rawRequest: unknown): [unknown, boolean] {\n if (isObject(rawRequest) && hasProperty(rawRequest, 'id')) {\n return [rawRequest.id, true];\n }\n\n return [undefined, false];\n}\n"]}
|
|
1
|
+
{"version":3,"file":"JsonRpcServer.mjs","sourceRoot":"","sources":["../../src/v2/JsonRpcServer.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,6BAA6B;AAQjE,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,wBAAwB;AAQxD,OAAO,EAAE,eAAe,EAAE,8BAA0B;AAEpD,OAAO,EAAE,WAAW,EAAE,2BAAuB;AAe7C,MAAM,OAAO,GAAG,KAAc,CAAC;AAE/B;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,OAAO,aAAa;IAiBxB;;;;;;;;;;;;OAYG;IACH,YAAY,OAA4B;;QApB/B,wCAGP;QAEO,yCAA+B;QAgBtC,uBAAA,IAAI,0BAAY,OAAO,CAAC,OAAO,MAAA,CAAC;QAEhC,IAAI,WAAW,CAAC,OAAO,EAAE,QAAQ,CAAC,EAAE;YAClC,2DAA2D;YAC3D,uBAAA,IAAI,yBAAW,OAAO,CAAC,MAAM,MAAA,CAAC;SAC/B;aAAM;YACL,kGAAkG;YAClG,uBAAA,IAAI,yBAAW,eAAe,CAAC,MAAM,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC,MAAA,CAAC;SAC3E;IACH,CAAC;IAgDD,KAAK,CAAC,MAAM,CAAC,UAAmB;QAC9B,uEAAuE;QACvE,qEAAqE;QACrE,sCAAsC;QACtC,MAAM,CAAC,UAAU,EAAE,SAAS,CAAC,GAAG,aAAa,CAAC,UAAU,CAAC,CAAC;QAE1D,IAAI;YACF,MAAM,OAAO,GAAG,uBAAA,IAAI,8DAAe,MAAnB,IAAI,EAAgB,UAAU,EAAE,SAAS,CAAC,CAAC;YAC3D,gFAAgF;YAChF,6CAA6C;YAC7C,MAAM,MAAM,GAAG,MAAM,uBA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ACrC,cAAc,CAAC,UAAU,CAAC,CAC3B,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,SAAS,cAAc,CACrB,UAAmC;IAEnC,IAAI,WAAW,CAAC,UAAU,EAAE,QAAQ,CAAC,EAAE;QACrC,OAAO,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,QAAQ,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;KACxE;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;GAMG;AACH,SAAS,aAAa,CAAC,UAAmB;IACxC,IAAI,QAAQ,CAAC,UAAU,CAAC,IAAI,WAAW,CAAC,UAAU,EAAE,IAAI,CAAC,EAAE;QACzD,OAAO,CAAC,UAAU,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;KAC9B;IACD,OAAO,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;AAC5B,CAAC","sourcesContent":["import { rpcErrors, serializeError } from '@metamask/rpc-errors';\nimport type {\n JsonRpcNotification,\n JsonRpcParams,\n JsonRpcRequest,\n JsonRpcResponse,\n NonEmptyArray,\n} from '@metamask/utils';\nimport { hasProperty, isObject } from '@metamask/utils';\n\nimport type {\n JsonRpcMiddleware,\n MergedContextOf,\n RequestOf,\n ResultConstraint,\n} from './JsonRpcEngineV2';\nimport { JsonRpcEngineV2 } from './JsonRpcEngineV2';\nimport type { JsonRpcCall } from './utils';\nimport { getUniqueId } from '../getUniqueId';\n\ntype OnError = (error: unknown) => void;\n\ntype Options<Middleware extends JsonRpcMiddleware> = {\n onError?: OnError;\n} & (\n | {\n engine: JsonRpcEngineV2;\n }\n | {\n middleware: NonEmptyArray<Middleware>;\n }\n);\n\nconst jsonrpc = '2.0' as const;\n\n/**\n * A JSON-RPC server that handles requests and notifications.\n *\n * Essentially wraps a {@link JsonRpcEngineV2} in order to create a conformant\n * yet permissive JSON-RPC 2.0 server.\n *\n * Note that the server will accept both requests and notifications via {@link handle},\n * even if the underlying engine is only able to handle one or the other.\n *\n * @example\n * ```ts\n * const server = new JsonRpcServer({\n * engine,\n * onError,\n * });\n *\n * const response = await server.handle(request);\n * if ('result' in response) {\n * // Handle result\n * } else {\n * // Handle error\n * }\n * ```\n */\nexport class JsonRpcServer<\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> {\n readonly #engine: JsonRpcEngineV2<\n RequestOf<Middleware>,\n MergedContextOf<Middleware>\n >;\n\n readonly #onError?: OnError | undefined;\n\n /**\n * Construct a new JSON-RPC server.\n *\n * @param options - The options for the server.\n * @param options.onError - The callback to handle errors thrown by the\n * engine. Errors always result in a failed response object, containing a\n * JSON-RPC 2.0 serialized version of the original error. If you need to\n * access the original error, use the `onError` callback.\n * @param options.engine - The engine to use. Mutually exclusive with\n * `middleware`.\n * @param options.middleware - The middleware to use. Mutually exclusive with\n * `engine`.\n */\n constructor(options: Options<Middleware>) {\n this.#onError = options.onError;\n\n if (hasProperty(options, 'engine')) {\n // @ts-expect-error - hasProperty fails to narrow the type.\n this.#engine = options.engine;\n } else {\n // @ts-expect-error - TypeScript complains that engine is of the wrong type, but clearly it's not.\n this.#engine = JsonRpcEngineV2.create({ middleware: options.middleware });\n }\n }\n\n /**\n * Handle a JSON-RPC request.\n *\n * This method never throws. For requests, a response is always returned.\n * All errors are passed to the engine's `onError` callback.\n *\n * **WARNING**: This method is unaware of the request type of the underlying\n * engine. The request will fail if the engine can only handle notifications.\n *\n * @param request - The request to handle.\n * @returns The JSON-RPC response.\n */\n async handle(request: JsonRpcRequest): Promise<JsonRpcResponse>;\n\n /**\n * Handle a JSON-RPC notification.\n *\n * This method never throws. For notifications, `undefined` is always returned.\n * All errors are passed to the engine's `onError` callback.\n *\n * **WARNING**: This method is unaware of the request type of the underlying\n * engine. The request will fail if the engine cannot handle notifications.\n *\n * @param notification - The notification to handle.\n */\n async handle(notification: JsonRpcNotification): Promise<void>;\n\n /**\n * Handle an alleged JSON-RPC request or notification. Permits any plain\n * object with `{ method: string }`, so long as any present JSON-RPC 2.0\n * properties are valid. If the object has an `id` property, it will be\n * treated as a request, otherwise it will be treated as a notification.\n *\n * This method never throws. All errors are passed to the engine's\n * `onError` callback. A JSON-RPC response is always returned for requests,\n * and `undefined` is returned for notifications.\n *\n * **WARNING**: The request will fail if its coerced type (i.e. request or\n * response) is not of the type expected by the underlying engine.\n *\n * @param rawRequest - The raw request to handle.\n * @returns The JSON-RPC response, or `undefined` if the request is a\n * notification.\n */\n async handle(rawRequest: unknown): Promise<JsonRpcResponse | void>;\n\n async handle(rawRequest: unknown): Promise<JsonRpcResponse | void> {\n // If rawRequest is not a notification, the originalId will be attached\n // to the response. We attach our own, trusted id in #coerceRequest()\n // while the request is being handled.\n const [originalId, isRequest] = getOriginalId(rawRequest);\n\n try {\n const request = this.#coerceRequest(rawRequest, isRequest);\n // @ts-expect-error - The request may not be of the type expected by the engine,\n // and we intentionally allow this to happen.\n const result = await this.#engine.handle(request);\n\n if (result !== undefined) {\n return {\n jsonrpc,\n // @ts-expect-error - Reassign the original id, regardless of its type.\n id: originalId,\n result,\n };\n }\n } catch (error) {\n this.#onError?.(error);\n\n if (isRequest) {\n return {\n jsonrpc,\n // @ts-expect-error - Reassign the original id, regardless of its type.\n id: originalId,\n error: serializeError(error, {\n shouldIncludeStack: false,\n shouldPreserveMessage: true,\n }),\n };\n }\n }\n return undefined;\n }\n\n #coerceRequest(rawRequest: unknown, isRequest: boolean): JsonRpcCall {\n if (!isMinimalRequest(rawRequest)) {\n throw rpcErrors.invalidRequest({\n data: {\n request: rawRequest,\n },\n });\n }\n\n const request: JsonRpcCall = {\n jsonrpc,\n method: rawRequest.method,\n };\n\n if (hasProperty(rawRequest, 'params')) {\n request.params = rawRequest.params as JsonRpcParams;\n }\n\n if (isRequest) {\n (request as JsonRpcRequest).id = getUniqueId();\n }\n\n return request;\n }\n}\n\n/**\n * The most minimally conformant request object that we will accept.\n */\ntype MinimalRequest = {\n method: string;\n params?: JsonRpcParams;\n} & Record<string, unknown>;\n\n/**\n * Check if an unvalidated request is a minimal request.\n *\n * @param rawRequest - The raw request to check.\n * @returns `true` if the request is a {@link MinimalRequest}, `false` otherwise.\n */\nfunction isMinimalRequest(rawRequest: unknown): rawRequest is MinimalRequest {\n return (\n isObject(rawRequest) &&\n hasProperty(rawRequest, 'method') &&\n typeof rawRequest.method === 'string' &&\n hasValidParams(rawRequest)\n );\n}\n\n/**\n * Check if a request has valid params, i.e. an array or object.\n * The contents of the params are not inspected.\n *\n * @param rawRequest - The request to check.\n * @returns `true` if the request has valid params, `false` otherwise.\n */\nfunction hasValidParams(\n rawRequest: Record<string, unknown>,\n): rawRequest is { params?: JsonRpcParams } {\n if (hasProperty(rawRequest, 'params')) {\n return Array.isArray(rawRequest.params) || isObject(rawRequest.params);\n }\n return true;\n}\n\n/**\n * Get the original id from a request.\n *\n * @param rawRequest - The request to get the original id from.\n * @returns The original id and a boolean indicating if the request is a request\n * (as opposed to a notification).\n */\nfunction getOriginalId(rawRequest: unknown): [unknown, boolean] {\n if (isObject(rawRequest) && hasProperty(rawRequest, 'id')) {\n return [rawRequest.id, true];\n }\n return [undefined, false];\n}\n"]}
|
package/dist/v2/index.cjs
CHANGED
|
@@ -1,27 +1,14 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.JsonRpcEngineError = exports.isRequest = exports.isNotification = exports.MiddlewareContext = exports.JsonRpcServer = exports.createScaffoldMiddleware = exports.getUniqueId = exports.asLegacyMiddleware = void 0;
|
|
3
|
+
exports.JsonRpcEngineError = exports.isRequest = exports.isNotification = exports.MiddlewareContext = exports.JsonRpcServer = exports.JsonRpcEngineV2 = exports.createScaffoldMiddleware = exports.getUniqueId = exports.asLegacyMiddleware = void 0;
|
|
18
4
|
var asLegacyMiddleware_1 = require("./asLegacyMiddleware.cjs");
|
|
19
5
|
Object.defineProperty(exports, "asLegacyMiddleware", { enumerable: true, get: function () { return asLegacyMiddleware_1.asLegacyMiddleware; } });
|
|
20
6
|
var getUniqueId_1 = require("../getUniqueId.cjs");
|
|
21
7
|
Object.defineProperty(exports, "getUniqueId", { enumerable: true, get: function () { return getUniqueId_1.getUniqueId; } });
|
|
22
8
|
var createScaffoldMiddleware_1 = require("./createScaffoldMiddleware.cjs");
|
|
23
9
|
Object.defineProperty(exports, "createScaffoldMiddleware", { enumerable: true, get: function () { return createScaffoldMiddleware_1.createScaffoldMiddleware; } });
|
|
24
|
-
|
|
10
|
+
var JsonRpcEngineV2_1 = require("./JsonRpcEngineV2.cjs");
|
|
11
|
+
Object.defineProperty(exports, "JsonRpcEngineV2", { enumerable: true, get: function () { return JsonRpcEngineV2_1.JsonRpcEngineV2; } });
|
|
25
12
|
var JsonRpcServer_1 = require("./JsonRpcServer.cjs");
|
|
26
13
|
Object.defineProperty(exports, "JsonRpcServer", { enumerable: true, get: function () { return JsonRpcServer_1.JsonRpcServer; } });
|
|
27
14
|
var MiddlewareContext_1 = require("./MiddlewareContext.cjs");
|
package/dist/v2/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../src/v2/index.ts"],"names":[],"mappings":"
|
|
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,2EAAsE;AAA7D,oIAAA,wBAAwB,OAAA;AACjC,yDAAoD;AAA3C,kHAAA,eAAe,OAAA;AAOxB,qDAAgD;AAAvC,8GAAA,aAAa,OAAA;AACtB,6DAAwD;AAA/C,sHAAA,iBAAiB,OAAA;AAE1B,qCAAwE;AAA/D,uGAAA,cAAc,OAAA;AAAE,kGAAA,SAAS,OAAA;AAAE,2GAAA,kBAAkB,OAAA","sourcesContent":["export { asLegacyMiddleware } from './asLegacyMiddleware';\nexport { getUniqueId } from '../getUniqueId';\nexport { createScaffoldMiddleware } from './createScaffoldMiddleware';\nexport { JsonRpcEngineV2 } from './JsonRpcEngineV2';\nexport type {\n JsonRpcMiddleware,\n MiddlewareParams,\n Next,\n ResultConstraint,\n} from './JsonRpcEngineV2';\nexport { JsonRpcServer } from './JsonRpcServer';\nexport { MiddlewareContext } from './MiddlewareContext';\nexport type { 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"]}
|
package/dist/v2/index.d.cts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
export { asLegacyMiddleware } from "./asLegacyMiddleware.cjs";
|
|
2
2
|
export { getUniqueId } from "../getUniqueId.cjs";
|
|
3
3
|
export { createScaffoldMiddleware } from "./createScaffoldMiddleware.cjs";
|
|
4
|
-
export
|
|
4
|
+
export { JsonRpcEngineV2 } from "./JsonRpcEngineV2.cjs";
|
|
5
|
+
export type { JsonRpcMiddleware, MiddlewareParams, Next, ResultConstraint, } from "./JsonRpcEngineV2.cjs";
|
|
5
6
|
export { JsonRpcServer } from "./JsonRpcServer.cjs";
|
|
6
7
|
export { MiddlewareContext } from "./MiddlewareContext.cjs";
|
|
7
8
|
export type { EmptyContext } from "./MiddlewareContext.cjs";
|
package/dist/v2/index.d.cts.map
CHANGED
|
@@ -1 +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,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AACtE,
|
|
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,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AACtE,OAAO,EAAE,eAAe,EAAE,8BAA0B;AACpD,YAAY,EACV,iBAAiB,EACjB,gBAAgB,EAChB,IAAI,EACJ,gBAAgB,GACjB,8BAA0B;AAC3B,OAAO,EAAE,aAAa,EAAE,4BAAwB;AAChD,OAAO,EAAE,iBAAiB,EAAE,gCAA4B;AACxD,YAAY,EAAE,YAAY,EAAE,gCAA4B;AACxD,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,kBAAkB,EAAE,oBAAgB;AACxE,YAAY,EACV,IAAI,EACJ,WAAW,EACX,mBAAmB,EACnB,aAAa,EACb,cAAc,GACf,oBAAgB"}
|
package/dist/v2/index.d.mts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
export { asLegacyMiddleware } from "./asLegacyMiddleware.mjs";
|
|
2
2
|
export { getUniqueId } from "../getUniqueId.mjs";
|
|
3
3
|
export { createScaffoldMiddleware } from "./createScaffoldMiddleware.mjs";
|
|
4
|
-
export
|
|
4
|
+
export { JsonRpcEngineV2 } from "./JsonRpcEngineV2.mjs";
|
|
5
|
+
export type { JsonRpcMiddleware, MiddlewareParams, Next, ResultConstraint, } from "./JsonRpcEngineV2.mjs";
|
|
5
6
|
export { JsonRpcServer } from "./JsonRpcServer.mjs";
|
|
6
7
|
export { MiddlewareContext } from "./MiddlewareContext.mjs";
|
|
7
8
|
export type { EmptyContext } from "./MiddlewareContext.mjs";
|
package/dist/v2/index.d.mts.map
CHANGED
|
@@ -1 +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,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AACtE,
|
|
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,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AACtE,OAAO,EAAE,eAAe,EAAE,8BAA0B;AACpD,YAAY,EACV,iBAAiB,EACjB,gBAAgB,EAChB,IAAI,EACJ,gBAAgB,GACjB,8BAA0B;AAC3B,OAAO,EAAE,aAAa,EAAE,4BAAwB;AAChD,OAAO,EAAE,iBAAiB,EAAE,gCAA4B;AACxD,YAAY,EAAE,YAAY,EAAE,gCAA4B;AACxD,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,kBAAkB,EAAE,oBAAgB;AACxE,YAAY,EACV,IAAI,EACJ,WAAW,EACX,mBAAmB,EACnB,aAAa,EACb,cAAc,GACf,oBAAgB"}
|
package/dist/v2/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { asLegacyMiddleware } from "./asLegacyMiddleware.mjs";
|
|
2
2
|
export { getUniqueId } from "../getUniqueId.mjs";
|
|
3
3
|
export { createScaffoldMiddleware } from "./createScaffoldMiddleware.mjs";
|
|
4
|
-
export
|
|
4
|
+
export { JsonRpcEngineV2 } from "./JsonRpcEngineV2.mjs";
|
|
5
5
|
export { JsonRpcServer } from "./JsonRpcServer.mjs";
|
|
6
6
|
export { MiddlewareContext } from "./MiddlewareContext.mjs";
|
|
7
7
|
export { isNotification, isRequest, JsonRpcEngineError } from "./utils.mjs";
|
package/dist/v2/index.mjs.map
CHANGED
|
@@ -1 +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,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AACtE,
|
|
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,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AACtE,OAAO,EAAE,eAAe,EAAE,8BAA0B;AAOpD,OAAO,EAAE,aAAa,EAAE,4BAAwB;AAChD,OAAO,EAAE,iBAAiB,EAAE,gCAA4B;AAExD,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,kBAAkB,EAAE,oBAAgB","sourcesContent":["export { asLegacyMiddleware } from './asLegacyMiddleware';\nexport { getUniqueId } from '../getUniqueId';\nexport { createScaffoldMiddleware } from './createScaffoldMiddleware';\nexport { JsonRpcEngineV2 } from './JsonRpcEngineV2';\nexport type {\n JsonRpcMiddleware,\n MiddlewareParams,\n Next,\n ResultConstraint,\n} from './JsonRpcEngineV2';\nexport { JsonRpcServer } from './JsonRpcServer';\nexport { MiddlewareContext } from './MiddlewareContext';\nexport type { 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"]}
|