@metamask-previews/json-rpc-engine 10.1.1-preview-c96ff8f → 10.1.1-preview-b289ac1c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +0 -27
  3. package/dist/v2/JsonRpcEngineV2.cjs +2 -5
  4. package/dist/v2/JsonRpcEngineV2.cjs.map +1 -1
  5. package/dist/v2/JsonRpcEngineV2.d.cts +4 -7
  6. package/dist/v2/JsonRpcEngineV2.d.cts.map +1 -1
  7. package/dist/v2/JsonRpcEngineV2.d.mts +4 -7
  8. package/dist/v2/JsonRpcEngineV2.d.mts.map +1 -1
  9. package/dist/v2/JsonRpcEngineV2.mjs +2 -5
  10. package/dist/v2/JsonRpcEngineV2.mjs.map +1 -1
  11. package/dist/v2/JsonRpcServer.cjs +6 -5
  12. package/dist/v2/JsonRpcServer.cjs.map +1 -1
  13. package/dist/v2/JsonRpcServer.d.cts +4 -10
  14. package/dist/v2/JsonRpcServer.d.cts.map +1 -1
  15. package/dist/v2/JsonRpcServer.d.mts +4 -10
  16. package/dist/v2/JsonRpcServer.d.mts.map +1 -1
  17. package/dist/v2/JsonRpcServer.mjs +6 -5
  18. package/dist/v2/JsonRpcServer.mjs.map +1 -1
  19. package/dist/v2/MiddlewareContext.cjs +1 -42
  20. package/dist/v2/MiddlewareContext.cjs.map +1 -1
  21. package/dist/v2/MiddlewareContext.d.cts +2 -12
  22. package/dist/v2/MiddlewareContext.d.cts.map +1 -1
  23. package/dist/v2/MiddlewareContext.d.mts +2 -12
  24. package/dist/v2/MiddlewareContext.d.mts.map +1 -1
  25. package/dist/v2/MiddlewareContext.mjs +1 -42
  26. package/dist/v2/MiddlewareContext.mjs.map +1 -1
  27. package/dist/v2/createScaffoldMiddleware.cjs.map +1 -1
  28. package/dist/v2/createScaffoldMiddleware.d.cts +3 -12
  29. package/dist/v2/createScaffoldMiddleware.d.cts.map +1 -1
  30. package/dist/v2/createScaffoldMiddleware.d.mts +3 -12
  31. package/dist/v2/createScaffoldMiddleware.d.mts.map +1 -1
  32. package/dist/v2/createScaffoldMiddleware.mjs.map +1 -1
  33. package/dist/v2/index.cjs.map +1 -1
  34. package/dist/v2/index.d.cts +1 -1
  35. package/dist/v2/index.d.cts.map +1 -1
  36. package/dist/v2/index.d.mts +1 -1
  37. package/dist/v2/index.d.mts.map +1 -1
  38. package/dist/v2/index.mjs.map +1 -1
  39. package/dist/v2/utils.cjs +2 -2
  40. package/dist/v2/utils.cjs.map +1 -1
  41. package/dist/v2/utils.d.cts.map +1 -1
  42. package/dist/v2/utils.d.mts.map +1 -1
  43. package/dist/v2/utils.mjs +2 -2
  44. package/dist/v2/utils.mjs.map +1 -1
  45. package/package.json +1 -1
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _a, _JsonRpcServer_engine, _JsonRpcServer_onError, _JsonRpcServer_coerceRequest;
12
+ var _JsonRpcServer_instances, _JsonRpcServer_engine, _JsonRpcServer_onError, _JsonRpcServer_coerceRequest;
13
13
  import { rpcErrors, serializeError } from "@metamask/rpc-errors";
14
14
  import { hasProperty, isObject } from "@metamask/utils";
15
15
  import { JsonRpcEngineV2 } from "./JsonRpcEngineV2.mjs";
@@ -54,6 +54,7 @@ export class JsonRpcServer {
54
54
  * `engine`.
55
55
  */
56
56
  constructor(options) {
57
+ _JsonRpcServer_instances.add(this);
57
58
  _JsonRpcServer_engine.set(this, void 0);
58
59
  _JsonRpcServer_onError.set(this, void 0);
59
60
  __classPrivateFieldSet(this, _JsonRpcServer_onError, options.onError, "f");
@@ -66,16 +67,16 @@ export class JsonRpcServer {
66
67
  __classPrivateFieldSet(this, _JsonRpcServer_engine, JsonRpcEngineV2.create({ middleware: options.middleware }), "f");
67
68
  }
68
69
  }
69
- async handle(rawRequest, options) {
70
+ async handle(rawRequest) {
70
71
  // If rawRequest is not a notification, the originalId will be attached
71
72
  // to the response. We attach our own, trusted id in #coerceRequest()
72
73
  // while the request is being handled.
73
74
  const [originalId, isRequest] = getOriginalId(rawRequest);
74
75
  try {
75
- const request = __classPrivateFieldGet(_a, _a, "m", _JsonRpcServer_coerceRequest).call(_a, rawRequest, isRequest);
76
+ const request = __classPrivateFieldGet(this, _JsonRpcServer_instances, "m", _JsonRpcServer_coerceRequest).call(this, rawRequest, isRequest);
76
77
  // @ts-expect-error - The request may not be of the type expected by the engine,
77
78
  // and we intentionally allow this to happen.
78
- const result = await __classPrivateFieldGet(this, _JsonRpcServer_engine, "f").handle(request, options);
79
+ const result = await __classPrivateFieldGet(this, _JsonRpcServer_engine, "f").handle(request);
79
80
  if (result !== undefined) {
80
81
  return {
81
82
  jsonrpc,
@@ -102,7 +103,7 @@ export class JsonRpcServer {
102
103
  return undefined;
103
104
  }
104
105
  }
105
- _a = JsonRpcServer, _JsonRpcServer_engine = new WeakMap(), _JsonRpcServer_onError = new WeakMap(), _JsonRpcServer_coerceRequest = function _JsonRpcServer_coerceRequest(rawRequest, isRequest) {
106
+ _JsonRpcServer_engine = new WeakMap(), _JsonRpcServer_onError = new WeakMap(), _JsonRpcServer_instances = new WeakSet(), _JsonRpcServer_coerceRequest = function _JsonRpcServer_coerceRequest(rawRequest, isRequest) {
106
107
  if (!isMinimalRequest(rawRequest)) {
107
108
  throw rpcErrors.invalidRequest({
108
109
  data: {
@@ -1 +1 @@
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;AASxD,OAAO,EAAE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;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 HandleOptions,\n JsonRpcMiddleware,\n MergedContextOf,\n MiddlewareConstraint,\n RequestOf,\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 MiddlewareConstraint> = {\n onError?: OnError;\n} & (\n | {\n engine: ReturnType<typeof JsonRpcEngineV2.create<Middleware>>;\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 MiddlewareConstraint = 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 * @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: JsonRpcRequest,\n options?: HandleOptions<MergedContextOf<Middleware>>,\n ): 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 * @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: JsonRpcNotification,\n options?: HandleOptions<MergedContextOf<Middleware>>,\n ): 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 * @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 request is a\n * notification.\n */\n async handle(\n rawRequest: unknown,\n options?: HandleOptions<MergedContextOf<Middleware>>,\n ): Promise<JsonRpcResponse | void>;\n\n async handle(\n rawRequest: unknown,\n options?: HandleOptions<MergedContextOf<Middleware>>,\n ): 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 = JsonRpcServer.#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, options);\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 static #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
+ {"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;IAUxB;;;;;;;;;;;;OAYG;IACH,YAAY,OAA4B;;QApB/B,wCAGP;QAEO,yCAA+B;QAgBtC,uBAAA,IAAI,0BAAY,OAAO,CAAC,O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amask/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 MiddlewareConstraint,\n RequestOf,\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 MiddlewareConstraint> = {\n onError?: OnError;\n} & (\n | {\n engine: ReturnType<typeof JsonRpcEngineV2.create<Middleware>>;\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 MiddlewareConstraint = 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,9 +1,6 @@
1
1
  "use strict";
2
- var _a;
3
2
  Object.defineProperty(exports, "__esModule", { value: true });
4
3
  exports.MiddlewareContext = void 0;
5
- const utils_1 = require("@metamask/utils");
6
- const MiddlewareContextSymbol = Symbol.for('json-rpc-engine#MiddlewareContext');
7
4
  /**
8
5
  * An context object for middleware that attempts to protect against accidental
9
6
  * modifications. Its interface is frozen.
@@ -29,23 +26,8 @@ const MiddlewareContextSymbol = Symbol.for('json-rpc-engine#MiddlewareContext');
29
26
  * context.get('fizz'); // Type error
30
27
  */
31
28
  class MiddlewareContext extends Map {
32
- /**
33
- * Check if a value is a {@link MiddlewareContext} instance.
34
- * Works across different package versions in the same realm.
35
- *
36
- * @param value - The value to check.
37
- * @returns Whether the value is a {@link MiddlewareContext} instance.
38
- */
39
- static isInstance(value) {
40
- return ((0, utils_1.isObject)(value) &&
41
- MiddlewareContextSymbol in value &&
42
- value[MiddlewareContextSymbol] === true);
43
- }
44
29
  constructor(entries) {
45
- super(entries && isIterable(entries)
46
- ? entries
47
- : entriesFromKeyValues(entries ?? {}));
48
- this[_a] = true;
30
+ super(entries);
49
31
  Object.freeze(this);
50
32
  }
51
33
  get(key) {
@@ -81,27 +63,4 @@ class MiddlewareContext extends Map {
81
63
  }
82
64
  }
83
65
  exports.MiddlewareContext = MiddlewareContext;
84
- _a = MiddlewareContextSymbol;
85
- /**
86
- * {@link Iterable} type guard.
87
- *
88
- * @param value - The value to check.
89
- * @returns Whether the value is an {@link Iterable}.
90
- */
91
- function isIterable(value) {
92
- return Symbol.iterator in value;
93
- }
94
- /**
95
- * Like Object.entries(), but includes symbol-keyed properties.
96
- *
97
- * @template KeyValues - The type of the keys and values in the object.
98
- * @param keyValues - The object to convert.
99
- * @returns The array of entries, including symbol-keyed properties.
100
- */
101
- function entriesFromKeyValues(keyValues) {
102
- return Reflect.ownKeys(keyValues).map((key) => [
103
- key,
104
- keyValues[key],
105
- ]);
106
- }
107
66
  //# sourceMappingURL=MiddlewareContext.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"MiddlewareContext.cjs","sourceRoot":"","sources":["../../src/v2/MiddlewareContext.ts"],"names":[],"mappings":";;;;AAAA,2CAA2C;AAI3C,MAAM,uBAAuB,GAAG,MAAM,CAAC,GAAG,CAAC,mCAAmC,CAAC,CAAC;AAEhF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAa,iBAEX,SAAQ,GAAgD;IAGxD;;;;;;OAMG;IACH,MAAM,CAAC,UAAU,CAAC,KAAc;QAC9B,OAAO,CACL,IAAA,gBAAQ,EAAC,KAAK,CAAC;YACf,uBAAuB,IAAI,KAAK;YAChC,KAAK,CAAC,uBAAuB,CAAC,KAAK,IAAI,CACxC,CAAC;IACJ,CAAC;IAED,YACE,OAEa;QAEb,KAAK,CACH,OAAO,IAAI,UAAU,CAAC,OAAO,CAAC;YAC5B,CAAC,CAAC,OAAO;YACT,CAAC,CAAC,oBAAoB,CAAC,OAAO,IAAI,EAAE,CAAC,CACxC,CAAC;QA1Ba,QAAyB,GAAG,IAAI,CAAC;QA2BhD,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC;IAED,GAAG,CAA4B,GAAM;QACnC,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,CAA6B,CAAC;IACpD,CAAC;IAED;;;;;OAKG;IACH,SAAS,CAA4B,GAAM;QACzC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;YACnB,MAAM,IAAI,KAAK,CAAC,gBAAgB,MAAM,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;SAC3D;QACD,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,CAAiB,CAAC;IACxC,CAAC;IAED;;;;;;;;OAQG;IACH,GAAG,CAA4B,GAAM,EAAE,KAAmB;QACxD,IAAI,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;YAClB,MAAM,IAAI,KAAK,CAAC,0BAA0B,MAAM,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC;SAC1E;QACD,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QACtB,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AAlED,8CAkEC;KA/DmB,uBAAuB;AAiE3C;;;;;GAKG;AACH,SAAS,UAAU,CACjB,KAAuD;IAEvD,OAAO,MAAM,CAAC,QAAQ,IAAI,KAAK,CAAC;AAClC,CAAC;AAED;;;;;;GAMG;AACH,SAAS,oBAAoB,CAC3B,SAAoB;IAEpB,OAAO,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,CAAC,GAAoB,EAAE,EAAE,CAAC;QAC9D,GAAG;QACH,SAAS,CAAC,GAAG,CAAC;KACf,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { isObject } from '@metamask/utils';\n\nimport type { UnionToIntersection } from './utils';\n\nconst MiddlewareContextSymbol = Symbol.for('json-rpc-engine#MiddlewareContext');\n\n/**\n * An context object for middleware that attempts to protect against accidental\n * modifications. Its interface is frozen.\n *\n * Map keys may not be directly overridden with {@link set}. Instead, use\n * {@link delete} to remove a key and then {@link set} to add a new value.\n *\n * The override protections are circumvented when using e.g. `Reflect.set`, so\n * don't do that.\n *\n * @template KeyValues - The type of the keys and values in the context.\n * @example\n * // By default, the context permits any PropertyKey as a key.\n * const context = new MiddlewareContext();\n * context.set('foo', 'bar');\n * context.get('foo'); // 'bar'\n * context.get('fizz'); // undefined\n * @example\n * // By specifying an object type, the context permits only the keys of the object.\n * type Context = MiddlewareContext<{ foo: string }>;\n * const context = new Context([['foo', 'bar']]);\n * context.get('foo'); // 'bar'\n * context.get('fizz'); // Type error\n */\nexport class MiddlewareContext<\n KeyValues extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>,\n> extends Map<keyof KeyValues, KeyValues[keyof KeyValues]> {\n private readonly [MiddlewareContextSymbol] = true;\n\n /**\n * Check if a value is a {@link MiddlewareContext} instance.\n * Works across different package versions in the same realm.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link MiddlewareContext} instance.\n */\n static isInstance(value: unknown): value is MiddlewareContext {\n return (\n isObject(value) &&\n MiddlewareContextSymbol in value &&\n value[MiddlewareContextSymbol] === true\n );\n }\n\n constructor(\n entries?:\n | Iterable<readonly [keyof KeyValues, KeyValues[keyof KeyValues]]>\n | KeyValues,\n ) {\n super(\n entries && isIterable(entries)\n ? entries\n : entriesFromKeyValues(entries ?? {}),\n );\n Object.freeze(this);\n }\n\n get<K extends keyof KeyValues>(key: K): KeyValues[K] | undefined {\n return super.get(key) as KeyValues[K] | undefined;\n }\n\n /**\n * Get a value from the context. Throws if the key is not found.\n *\n * @param key - The key to get the value for.\n * @returns The value.\n */\n assertGet<K extends keyof KeyValues>(key: K): KeyValues[K] {\n if (!super.has(key)) {\n throw new Error(`Context key \"${String(key)}\" not found`);\n }\n return super.get(key) as KeyValues[K];\n }\n\n /**\n * Set a value in the context. Throws if the key already exists.\n * {@link delete} an existing key before setting it to a new value.\n *\n * @throws If the key already exists.\n * @param key - The key to set the value for.\n * @param value - The value to set.\n * @returns The context.\n */\n set<K extends keyof KeyValues>(key: K, value: KeyValues[K]): this {\n if (super.has(key)) {\n throw new Error(`MiddlewareContext key \"${String(key)}\" already exists`);\n }\n super.set(key, value);\n return this;\n }\n}\n\n/**\n * {@link Iterable} type guard.\n *\n * @param value - The value to check.\n * @returns Whether the value is an {@link Iterable}.\n */\nfunction isIterable(\n value: Iterable<unknown> | Record<PropertyKey, unknown>,\n): value is Iterable<unknown> {\n return Symbol.iterator in value;\n}\n\n/**\n * Like Object.entries(), but includes symbol-keyed properties.\n *\n * @template KeyValues - The type of the keys and values in the object.\n * @param keyValues - The object to convert.\n * @returns The array of entries, including symbol-keyed properties.\n */\nfunction entriesFromKeyValues<KeyValues extends Record<PropertyKey, unknown>>(\n keyValues: KeyValues,\n): [keyof KeyValues, KeyValues[keyof KeyValues]][] {\n return Reflect.ownKeys(keyValues).map((key: keyof KeyValues) => [\n key,\n keyValues[key],\n ]);\n}\n\n/**\n * Infer the KeyValues type from a {@link MiddlewareContext}.\n */\nexport type InferKeyValues<T> =\n T extends MiddlewareContext<infer U> ? U : never;\n\n/**\n * Simplifies an object type by \"merging\" its properties.\n *\n * - Expands intersections into a single object type.\n * - Forces mapped/conditional results to resolve into a readable shape.\n * - No runtime effect; purely a type-level normalization.\n *\n * @example\n * type A = { a: string } & { b: number };\n * type B = Simplify<A>; // { a: string; b: number }\n */\ntype Simplify<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;\n\n/**\n * Rejects record types that contain any `never`-valued property.\n *\n * If any property of `T` resolves to `never`, the result is `never`; otherwise it returns `T` unchanged.\n * Useful as a guard to ensure computed/merged record types didn't collapse any fields to `never`.\n *\n * @example\n * type A = ExcludeNever<{ a: string; b: never }>; // never\n * type B = ExcludeNever<{ a: string; b: number }>; // { a: string; b: number }\n */\ntype ExcludeNever<T extends Record<PropertyKey, unknown>> = {\n [K in keyof T]-?: [T[K]] extends [never] ? K : never;\n}[keyof T] extends never\n ? T\n : never;\n\n/**\n * Merge a union of {@link MiddlewareContext}s into a single {@link MiddlewareContext}\n * supertype.\n *\n * @param Contexts - The union of {@link MiddlewareContext}s to merge.\n * @returns The merged {@link MiddlewareContext} supertype.\n * @example\n * type A = MiddlewareContext<{ a: string }> | MiddlewareContext<{ b: number }>;\n * type B = MergeContexts<A>; // MiddlewareContext<{ a: string, b: number }>\n */\nexport type MergeContexts<Contexts extends ContextConstraint> =\n ExcludeNever<\n Simplify<UnionToIntersection<InferKeyValues<Contexts>>>\n > extends never\n ? never\n : MiddlewareContext<\n ExcludeNever<Simplify<UnionToIntersection<InferKeyValues<Contexts>>>>\n >;\n\n/**\n * A constraint for {@link MiddlewareContext} generic parameters.\n */\n// Non-polluting `any` constraint.\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type ContextConstraint = MiddlewareContext<any>;\n\n/**\n * The empty context type, i.e. `MiddlewareContext<{}>`.\n */\n// The empty object type is literally an empty object in this context.\n// eslint-disable-next-line @typescript-eslint/no-empty-object-type\nexport type EmptyContext = MiddlewareContext<{}>;\n"]}
1
+ {"version":3,"file":"MiddlewareContext.cjs","sourceRoot":"","sources":["../../src/v2/MiddlewareContext.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAa,iBAEX,SAAQ,GAAgD;IACxD,YACE,OAA0E;QAE1E,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC;IAED,GAAG,CAA4B,GAAM;QACnC,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,CAA6B,CAAC;IACpD,CAAC;IAED;;;;;OAKG;IACH,SAAS,CAA4B,GAAM;QACzC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;YACnB,MAAM,IAAI,KAAK,CAAC,gBAAgB,MAAM,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;SAC3D;QACD,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,CAAiB,CAAC;IACxC,CAAC;IAED;;;;;;;;OAQG;IACH,GAAG,CAA4B,GAAM,EAAE,KAAmB;QACxD,IAAI,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;YAClB,MAAM,IAAI,KAAK,CAAC,0BAA0B,MAAM,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC;SAC1E;QACD,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QACtB,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AA3CD,8CA2CC","sourcesContent":["import type { UnionToIntersection } from './utils';\n\n/**\n * An context object for middleware that attempts to protect against accidental\n * modifications. Its interface is frozen.\n *\n * Map keys may not be directly overridden with {@link set}. Instead, use\n * {@link delete} to remove a key and then {@link set} to add a new value.\n *\n * The override protections are circumvented when using e.g. `Reflect.set`, so\n * don't do that.\n *\n * @template KeyValues - The type of the keys and values in the context.\n * @example\n * // By default, the context permits any PropertyKey as a key.\n * const context = new MiddlewareContext();\n * context.set('foo', 'bar');\n * context.get('foo'); // 'bar'\n * context.get('fizz'); // undefined\n * @example\n * // By specifying an object type, the context permits only the keys of the object.\n * type Context = MiddlewareContext<{ foo: string }>;\n * const context = new Context([['foo', 'bar']]);\n * context.get('foo'); // 'bar'\n * context.get('fizz'); // Type error\n */\nexport class MiddlewareContext<\n KeyValues extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>,\n> extends Map<keyof KeyValues, KeyValues[keyof KeyValues]> {\n constructor(\n entries?: Iterable<readonly [keyof KeyValues, KeyValues[keyof KeyValues]]>,\n ) {\n super(entries);\n Object.freeze(this);\n }\n\n get<K extends keyof KeyValues>(key: K): KeyValues[K] | undefined {\n return super.get(key) as KeyValues[K] | undefined;\n }\n\n /**\n * Get a value from the context. Throws if the key is not found.\n *\n * @param key - The key to get the value for.\n * @returns The value.\n */\n assertGet<K extends keyof KeyValues>(key: K): KeyValues[K] {\n if (!super.has(key)) {\n throw new Error(`Context key \"${String(key)}\" not found`);\n }\n return super.get(key) as KeyValues[K];\n }\n\n /**\n * Set a value in the context. Throws if the key already exists.\n * {@link delete} an existing key before setting it to a new value.\n *\n * @throws If the key already exists.\n * @param key - The key to set the value for.\n * @param value - The value to set.\n * @returns The context.\n */\n set<K extends keyof KeyValues>(key: K, value: KeyValues[K]): this {\n if (super.has(key)) {\n throw new Error(`MiddlewareContext key \"${String(key)}\" already exists`);\n }\n super.set(key, value);\n return this;\n }\n}\n\n/**\n * Infer the KeyValues type from a {@link MiddlewareContext}.\n */\ntype InferKeyValues<T> = T extends MiddlewareContext<infer U> ? U : never;\n\n/**\n * Simplifies an object type by \"merging\" its properties.\n *\n * - Expands intersections into a single object type.\n * - Forces mapped/conditional results to resolve into a readable shape.\n * - No runtime effect; purely a type-level normalization.\n *\n * @example\n * type A = { a: string } & { b: number };\n * type B = Simplify<A>; // { a: string; b: number }\n */\ntype Simplify<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;\n\n/**\n * Rejects record types that contain any `never`-valued property.\n *\n * If any property of `T` resolves to `never`, the result is `never`; otherwise it returns `T` unchanged.\n * Useful as a guard to ensure computed/merged record types didn't collapse any fields to `never`.\n *\n * @example\n * type A = ExcludeNever<{ a: string; b: never }>; // never\n * type B = ExcludeNever<{ a: string; b: number }>; // { a: string; b: number }\n */\ntype ExcludeNever<T extends Record<PropertyKey, unknown>> = {\n [K in keyof T]-?: [T[K]] extends [never] ? K : never;\n}[keyof T] extends never\n ? T\n : never;\n\n/**\n * Merge a union of {@link MiddlewareContext}s into a single {@link MiddlewareContext}\n * supertype.\n *\n * @param Contexts - The union of {@link MiddlewareContext}s to merge.\n * @returns The merged {@link MiddlewareContext} supertype.\n * @example\n * type A = MiddlewareContext<{ a: string }> | MiddlewareContext<{ b: number }>;\n * type B = MergeContexts<A>; // MiddlewareContext<{ a: string, b: number }>\n */\nexport type MergeContexts<Contexts extends ContextConstraint> =\n ExcludeNever<\n Simplify<UnionToIntersection<InferKeyValues<Contexts>>>\n > extends never\n ? never\n : MiddlewareContext<\n ExcludeNever<Simplify<UnionToIntersection<InferKeyValues<Contexts>>>>\n >;\n\n/**\n * A constraint for {@link MiddlewareContext} generic parameters.\n */\n// Non-polluting `any` constraint.\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type ContextConstraint = MiddlewareContext<any>;\n\n/**\n * The empty context type, i.e. `MiddlewareContext<{}>`.\n */\n// The empty object type is literally an empty object in this context.\n// eslint-disable-next-line @typescript-eslint/no-empty-object-type\nexport type EmptyContext = MiddlewareContext<{}>;\n"]}
@@ -1,5 +1,4 @@
1
1
  import type { UnionToIntersection } from "./utils.cjs";
2
- declare const MiddlewareContextSymbol: unique symbol;
3
2
  /**
4
3
  * An context object for middleware that attempts to protect against accidental
5
4
  * modifications. Its interface is frozen.
@@ -25,16 +24,7 @@ declare const MiddlewareContextSymbol: unique symbol;
25
24
  * context.get('fizz'); // Type error
26
25
  */
27
26
  export declare class MiddlewareContext<KeyValues extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>> extends Map<keyof KeyValues, KeyValues[keyof KeyValues]> {
28
- private readonly [MiddlewareContextSymbol];
29
- /**
30
- * Check if a value is a {@link MiddlewareContext} instance.
31
- * Works across different package versions in the same realm.
32
- *
33
- * @param value - The value to check.
34
- * @returns Whether the value is a {@link MiddlewareContext} instance.
35
- */
36
- static isInstance(value: unknown): value is MiddlewareContext;
37
- constructor(entries?: Iterable<readonly [keyof KeyValues, KeyValues[keyof KeyValues]]> | KeyValues);
27
+ constructor(entries?: Iterable<readonly [keyof KeyValues, KeyValues[keyof KeyValues]]>);
38
28
  get<K extends keyof KeyValues>(key: K): KeyValues[K] | undefined;
39
29
  /**
40
30
  * Get a value from the context. Throws if the key is not found.
@@ -57,7 +47,7 @@ export declare class MiddlewareContext<KeyValues extends Record<PropertyKey, unk
57
47
  /**
58
48
  * Infer the KeyValues type from a {@link MiddlewareContext}.
59
49
  */
60
- export type InferKeyValues<T> = T extends MiddlewareContext<infer U> ? U : never;
50
+ type InferKeyValues<T> = T extends MiddlewareContext<infer U> ? U : never;
61
51
  /**
62
52
  * Simplifies an object type by "merging" its properties.
63
53
  *
@@ -1 +1 @@
1
- {"version":3,"file":"MiddlewareContext.d.cts","sourceRoot":"","sources":["../../src/v2/MiddlewareContext.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oBAAgB;AAEnD,QAAA,MAAM,uBAAuB,eAAkD,CAAC;AAEhF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBAAa,iBAAiB,CAC5B,SAAS,SAAS,MAAM,CAAC,WAAW,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,WAAW,EAAE,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"}
@@ -1,5 +1,4 @@
1
1
  import type { UnionToIntersection } from "./utils.mjs";
2
- declare const MiddlewareContextSymbol: unique symbol;
3
2
  /**
4
3
  * An context object for middleware that attempts to protect against accidental
5
4
  * modifications. Its interface is frozen.
@@ -25,16 +24,7 @@ declare const MiddlewareContextSymbol: unique symbol;
25
24
  * context.get('fizz'); // Type error
26
25
  */
27
26
  export declare class MiddlewareContext<KeyValues extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>> extends Map<keyof KeyValues, KeyValues[keyof KeyValues]> {
28
- private readonly [MiddlewareContextSymbol];
29
- /**
30
- * Check if a value is a {@link MiddlewareContext} instance.
31
- * Works across different package versions in the same realm.
32
- *
33
- * @param value - The value to check.
34
- * @returns Whether the value is a {@link MiddlewareContext} instance.
35
- */
36
- static isInstance(value: unknown): value is MiddlewareContext;
37
- constructor(entries?: Iterable<readonly [keyof KeyValues, KeyValues[keyof KeyValues]]> | KeyValues);
27
+ constructor(entries?: Iterable<readonly [keyof KeyValues, KeyValues[keyof KeyValues]]>);
38
28
  get<K extends keyof KeyValues>(key: K): KeyValues[K] | undefined;
39
29
  /**
40
30
  * Get a value from the context. Throws if the key is not found.
@@ -57,7 +47,7 @@ export declare class MiddlewareContext<KeyValues extends Record<PropertyKey, unk
57
47
  /**
58
48
  * Infer the KeyValues type from a {@link MiddlewareContext}.
59
49
  */
60
- export type InferKeyValues<T> = T extends MiddlewareContext<infer U> ? U : never;
50
+ type InferKeyValues<T> = T extends MiddlewareContext<infer U> ? U : never;
61
51
  /**
62
52
  * Simplifies an object type by "merging" its properties.
63
53
  *
@@ -1 +1 @@
1
- {"version":3,"file":"MiddlewareContext.d.mts","sourceRoot":"","sources":["../../src/v2/MiddlewareContext.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oBAAgB;AAEnD,QAAA,MAAM,uBAAuB,eAAkD,CAAC;AAEhF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBAAa,iBAA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,CAAC,CAAC;AAEvD;;GAEG;AAGH,MAAM,MAAM,YAAY,GAAG,iBAAiB,CAAC,EAAE,CAAC,CAAC"}
@@ -1,6 +1,3 @@
1
- var _a;
2
- import { isObject } from "@metamask/utils";
3
- const MiddlewareContextSymbol = Symbol.for('json-rpc-engine#MiddlewareContext');
4
1
  /**
5
2
  * An context object for middleware that attempts to protect against accidental
6
3
  * modifications. Its interface is frozen.
@@ -26,23 +23,8 @@ const MiddlewareContextSymbol = Symbol.for('json-rpc-engine#MiddlewareContext');
26
23
  * context.get('fizz'); // Type error
27
24
  */
28
25
  export class MiddlewareContext extends Map {
29
- /**
30
- * Check if a value is a {@link MiddlewareContext} instance.
31
- * Works across different package versions in the same realm.
32
- *
33
- * @param value - The value to check.
34
- * @returns Whether the value is a {@link MiddlewareContext} instance.
35
- */
36
- static isInstance(value) {
37
- return (isObject(value) &&
38
- MiddlewareContextSymbol in value &&
39
- value[MiddlewareContextSymbol] === true);
40
- }
41
26
  constructor(entries) {
42
- super(entries && isIterable(entries)
43
- ? entries
44
- : entriesFromKeyValues(entries ?? {}));
45
- this[_a] = true;
27
+ super(entries);
46
28
  Object.freeze(this);
47
29
  }
48
30
  get(key) {
@@ -77,27 +59,4 @@ export class MiddlewareContext extends Map {
77
59
  return this;
78
60
  }
79
61
  }
80
- _a = MiddlewareContextSymbol;
81
- /**
82
- * {@link Iterable} type guard.
83
- *
84
- * @param value - The value to check.
85
- * @returns Whether the value is an {@link Iterable}.
86
- */
87
- function isIterable(value) {
88
- return Symbol.iterator in value;
89
- }
90
- /**
91
- * Like Object.entries(), but includes symbol-keyed properties.
92
- *
93
- * @template KeyValues - The type of the keys and values in the object.
94
- * @param keyValues - The object to convert.
95
- * @returns The array of entries, including symbol-keyed properties.
96
- */
97
- function entriesFromKeyValues(keyValues) {
98
- return Reflect.ownKeys(keyValues).map((key) => [
99
- key,
100
- keyValues[key],
101
- ]);
102
- }
103
62
  //# sourceMappingURL=MiddlewareContext.mjs.map
@@ -1 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CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC;SAC1E;QACD,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QACtB,OAAO,IAAI,CAAC;IACd,CAAC;CACF;KA/DmB,uBAAuB;AAiE3C;;;;;GAKG;AACH,SAAS,UAAU,CACjB,KAAuD;IAEvD,OAAO,MAAM,CAAC,QAAQ,IAAI,KAAK,CAAC;AAClC,CAAC;AAED;;;;;;GAMG;AACH,SAAS,oBAAoB,CAC3B,SAAoB;IAEpB,OAAO,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,CAAC,GAAoB,EAAE,EAAE,CAAC;QAC9D,GAAG;QACH,SAAS,CAAC,GAAG,CAAC;KACf,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { isObject } from '@metamask/utils';\n\nimport type { UnionToIntersection } from './utils';\n\nconst MiddlewareContextSymbol = Symbol.for('json-rpc-engine#MiddlewareContext');\n\n/**\n * An context object for middleware that attempts to protect against accidental\n * modifications. Its interface is frozen.\n *\n * Map keys may not be directly overridden with {@link set}. Instead, use\n * {@link delete} to remove a key and then {@link set} to add a new value.\n *\n * The override protections are circumvented when using e.g. `Reflect.set`, so\n * don't do that.\n *\n * @template KeyValues - The type of the keys and values in the context.\n * @example\n * // By default, the context permits any PropertyKey as a key.\n * const context = new MiddlewareContext();\n * context.set('foo', 'bar');\n * context.get('foo'); // 'bar'\n * context.get('fizz'); // undefined\n * @example\n * // By specifying an object type, the context permits only the keys of the object.\n * type Context = MiddlewareContext<{ foo: string }>;\n * const context = new Context([['foo', 'bar']]);\n * context.get('foo'); // 'bar'\n * context.get('fizz'); // Type error\n */\nexport class MiddlewareContext<\n KeyValues extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>,\n> extends Map<keyof KeyValues, KeyValues[keyof KeyValues]> {\n private readonly [MiddlewareContextSymbol] = true;\n\n /**\n * Check if a value is a {@link MiddlewareContext} instance.\n * Works across different package versions in the same realm.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link MiddlewareContext} instance.\n */\n static isInstance(value: unknown): value is MiddlewareContext {\n return (\n isObject(value) &&\n MiddlewareContextSymbol in value &&\n value[MiddlewareContextSymbol] === true\n );\n }\n\n constructor(\n entries?:\n | Iterable<readonly [keyof KeyValues, KeyValues[keyof KeyValues]]>\n | KeyValues,\n ) {\n super(\n entries && isIterable(entries)\n ? entries\n : entriesFromKeyValues(entries ?? {}),\n );\n Object.freeze(this);\n }\n\n get<K extends keyof KeyValues>(key: K): KeyValues[K] | undefined {\n return super.get(key) as KeyValues[K] | undefined;\n }\n\n /**\n * Get a value from the context. Throws if the key is not found.\n *\n * @param key - The key to get the value for.\n * @returns The value.\n */\n assertGet<K extends keyof KeyValues>(key: K): KeyValues[K] {\n if (!super.has(key)) {\n throw new Error(`Context key \"${String(key)}\" not found`);\n }\n return super.get(key) as KeyValues[K];\n }\n\n /**\n * Set a value in the context. Throws if the key already exists.\n * {@link delete} an existing key before setting it to a new value.\n *\n * @throws If the key already exists.\n * @param key - The key to set the value for.\n * @param value - The value to set.\n * @returns The context.\n */\n set<K extends keyof KeyValues>(key: K, value: KeyValues[K]): this {\n if (super.has(key)) {\n throw new Error(`MiddlewareContext key \"${String(key)}\" already exists`);\n }\n super.set(key, value);\n return this;\n }\n}\n\n/**\n * {@link Iterable} type guard.\n *\n * @param value - The value to check.\n * @returns Whether the value is an {@link Iterable}.\n */\nfunction isIterable(\n value: Iterable<unknown> | Record<PropertyKey, unknown>,\n): value is Iterable<unknown> {\n return Symbol.iterator in value;\n}\n\n/**\n * Like Object.entries(), but includes symbol-keyed properties.\n *\n * @template KeyValues - The type of the keys and values in the object.\n * @param keyValues - The object to convert.\n * @returns The array of entries, including symbol-keyed properties.\n */\nfunction entriesFromKeyValues<KeyValues extends Record<PropertyKey, unknown>>(\n keyValues: KeyValues,\n): [keyof KeyValues, KeyValues[keyof KeyValues]][] {\n return Reflect.ownKeys(keyValues).map((key: keyof KeyValues) => [\n key,\n keyValues[key],\n ]);\n}\n\n/**\n * Infer the KeyValues type from a {@link MiddlewareContext}.\n */\nexport type InferKeyValues<T> =\n T extends MiddlewareContext<infer U> ? U : never;\n\n/**\n * Simplifies an object type by \"merging\" its properties.\n *\n * - Expands intersections into a single object type.\n * - Forces mapped/conditional results to resolve into a readable shape.\n * - No runtime effect; purely a type-level normalization.\n *\n * @example\n * type A = { a: string } & { b: number };\n * type B = Simplify<A>; // { a: string; b: number }\n */\ntype Simplify<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;\n\n/**\n * Rejects record types that contain any `never`-valued property.\n *\n * If any property of `T` resolves to `never`, the result is `never`; otherwise it returns `T` unchanged.\n * Useful as a guard to ensure computed/merged record types didn't collapse any fields to `never`.\n *\n * @example\n * type A = ExcludeNever<{ a: string; b: never }>; // never\n * type B = ExcludeNever<{ a: string; b: number }>; // { a: string; b: number }\n */\ntype ExcludeNever<T extends Record<PropertyKey, unknown>> = {\n [K in keyof T]-?: [T[K]] extends [never] ? K : never;\n}[keyof T] extends never\n ? T\n : never;\n\n/**\n * Merge a union of {@link MiddlewareContext}s into a single {@link MiddlewareContext}\n * supertype.\n *\n * @param Contexts - The union of {@link MiddlewareContext}s to merge.\n * @returns The merged {@link MiddlewareContext} supertype.\n * @example\n * type A = MiddlewareContext<{ a: string }> | MiddlewareContext<{ b: number }>;\n * type B = MergeContexts<A>; // MiddlewareContext<{ a: string, b: number }>\n */\nexport type MergeContexts<Contexts extends ContextConstraint> =\n ExcludeNever<\n Simplify<UnionToIntersection<InferKeyValues<Contexts>>>\n > extends never\n ? never\n : MiddlewareContext<\n ExcludeNever<Simplify<UnionToIntersection<InferKeyValues<Contexts>>>>\n >;\n\n/**\n * A constraint for {@link MiddlewareContext} generic parameters.\n */\n// Non-polluting `any` constraint.\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type ContextConstraint = MiddlewareContext<any>;\n\n/**\n * The empty context type, i.e. `MiddlewareContext<{}>`.\n */\n// The empty object type is literally an empty object in this context.\n// eslint-disable-next-line @typescript-eslint/no-empty-object-type\nexport type EmptyContext = MiddlewareContext<{}>;\n"]}
1
+ {"version":3,"file":"MiddlewareContext.mjs","sourceRoot":"","sources":["../../src/v2/MiddlewareContext.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,OAAO,iBAEX,SAAQ,GAAgD;IACxD,YACE,OAA0E;QAE1E,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC;IAED,GAAG,CAA4B,GAAM;QACnC,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,CAA6B,CAAC;IACpD,CAAC;IAED;;;;;OAKG;IACH,SAAS,CAA4B,GAAM;QACzC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;YACnB,MAAM,IAAI,KAAK,CAAC,gBAAgB,MAAM,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;SAC3D;QACD,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,CAAiB,CAAC;IACxC,CAAC;IAED;;;;;;;;OAQG;IACH,GAAG,CAA4B,GAAM,EAAE,KAAmB;QACxD,IAAI,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;YAClB,MAAM,IAAI,KAAK,CAAC,0BAA0B,MAAM,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC;SAC1E;QACD,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QACtB,OAAO,IAAI,CAAC;IACd,CAAC;CACF","sourcesContent":["import type { UnionToIntersection } from './utils';\n\n/**\n * An context object for middleware that attempts to protect against accidental\n * modifications. Its interface is frozen.\n *\n * Map keys may not be directly overridden with {@link set}. Instead, use\n * {@link delete} to remove a key and then {@link set} to add a new value.\n *\n * The override protections are circumvented when using e.g. `Reflect.set`, so\n * don't do that.\n *\n * @template KeyValues - The type of the keys and values in the context.\n * @example\n * // By default, the context permits any PropertyKey as a key.\n * const context = new MiddlewareContext();\n * context.set('foo', 'bar');\n * context.get('foo'); // 'bar'\n * context.get('fizz'); // undefined\n * @example\n * // By specifying an object type, the context permits only the keys of the object.\n * type Context = MiddlewareContext<{ foo: string }>;\n * const context = new Context([['foo', 'bar']]);\n * context.get('foo'); // 'bar'\n * context.get('fizz'); // Type error\n */\nexport class MiddlewareContext<\n KeyValues extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>,\n> extends Map<keyof KeyValues, KeyValues[keyof KeyValues]> {\n constructor(\n entries?: Iterable<readonly [keyof KeyValues, KeyValues[keyof KeyValues]]>,\n ) {\n super(entries);\n Object.freeze(this);\n }\n\n get<K extends keyof KeyValues>(key: K): KeyValues[K] | undefined {\n return super.get(key) as KeyValues[K] | undefined;\n }\n\n /**\n * Get a value from the context. Throws if the key is not found.\n *\n * @param key - The key to get the value for.\n * @returns The value.\n */\n assertGet<K extends keyof KeyValues>(key: K): KeyValues[K] {\n if (!super.has(key)) {\n throw new Error(`Context key \"${String(key)}\" not found`);\n }\n return super.get(key) as KeyValues[K];\n }\n\n /**\n * Set a value in the context. Throws if the key already exists.\n * {@link delete} an existing key before setting it to a new value.\n *\n * @throws If the key already exists.\n * @param key - The key to set the value for.\n * @param value - The value to set.\n * @returns The context.\n */\n set<K extends keyof KeyValues>(key: K, value: KeyValues[K]): this {\n if (super.has(key)) {\n throw new Error(`MiddlewareContext key \"${String(key)}\" already exists`);\n }\n super.set(key, value);\n return this;\n }\n}\n\n/**\n * Infer the KeyValues type from a {@link MiddlewareContext}.\n */\ntype InferKeyValues<T> = T extends MiddlewareContext<infer U> ? U : never;\n\n/**\n * Simplifies an object type by \"merging\" its properties.\n *\n * - Expands intersections into a single object type.\n * - Forces mapped/conditional results to resolve into a readable shape.\n * - No runtime effect; purely a type-level normalization.\n *\n * @example\n * type A = { a: string } & { b: number };\n * type B = Simplify<A>; // { a: string; b: number }\n */\ntype Simplify<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;\n\n/**\n * Rejects record types that contain any `never`-valued property.\n *\n * If any property of `T` resolves to `never`, the result is `never`; otherwise it returns `T` unchanged.\n * Useful as a guard to ensure computed/merged record types didn't collapse any fields to `never`.\n *\n * @example\n * type A = ExcludeNever<{ a: string; b: never }>; // never\n * type B = ExcludeNever<{ a: string; b: number }>; // { a: string; b: number }\n */\ntype ExcludeNever<T extends Record<PropertyKey, unknown>> = {\n [K in keyof T]-?: [T[K]] extends [never] ? K : never;\n}[keyof T] extends never\n ? T\n : never;\n\n/**\n * Merge a union of {@link MiddlewareContext}s into a single {@link MiddlewareContext}\n * supertype.\n *\n * @param Contexts - The union of {@link MiddlewareContext}s to merge.\n * @returns The merged {@link MiddlewareContext} supertype.\n * @example\n * type A = MiddlewareContext<{ a: string }> | MiddlewareContext<{ b: number }>;\n * type B = MergeContexts<A>; // MiddlewareContext<{ a: string, b: number }>\n */\nexport type MergeContexts<Contexts extends ContextConstraint> =\n ExcludeNever<\n Simplify<UnionToIntersection<InferKeyValues<Contexts>>>\n > extends never\n ? never\n : MiddlewareContext<\n ExcludeNever<Simplify<UnionToIntersection<InferKeyValues<Contexts>>>>\n >;\n\n/**\n * A constraint for {@link MiddlewareContext} generic parameters.\n */\n// Non-polluting `any` constraint.\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type ContextConstraint = MiddlewareContext<any>;\n\n/**\n * The empty context type, i.e. `MiddlewareContext<{}>`.\n */\n// The empty object type is literally an empty object in this context.\n// eslint-disable-next-line @typescript-eslint/no-empty-object-type\nexport type EmptyContext = MiddlewareContext<{}>;\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"createScaffoldMiddleware.cjs","sourceRoot":"","sources":["../../src/v2/createScaffoldMiddleware.ts"],"names":[],"mappings":";;;AA+BA;;;;;;;;GAQG;AACH,SAAgB,wBAAwB,CACtC,QAAqC;IAErC,OAAO,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE;QACpC,MAAM,eAAe,GAAG,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACjD,IAAI,eAAe,KAAK,SAAS,EAAE;YACjC,OAAO,IAAI,EAAE,CAAC;SACf;QAED,OAAO,OAAO,eAAe,KAAK,UAAU;YAC1C,CAAC,CAAC,eAAe,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;YAC7C,CAAC,CAAC,eAAe,CAAC;IACtB,CAAC,CAAC;AACJ,CAAC;AAbD,4DAaC","sourcesContent":["import type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\n\nimport type { JsonRpcMiddleware } from './JsonRpcEngineV2';\nimport type { ContextConstraint } from './MiddlewareContext';\n\n// Only permit primitive values as hard-coded scaffold middleware results.\ntype JsonPrimitive = string | number | boolean | null;\n\n/**\n * A handler for a scaffold middleware function.\n *\n * @template Params - The parameters of the request.\n * @template Result - The result of the request.\n * @template Context - The context of the request.\n * @returns A JSON-RPC middleware function or a primitive JSON value.\n */\nexport type ScaffoldMiddlewareHandler<\n Params extends JsonRpcParams,\n Result extends Json,\n Context extends ContextConstraint,\n> = JsonRpcMiddleware<JsonRpcRequest<Params>, Result, Context> | JsonPrimitive;\n\n/**\n * A record of RPC method handler functions or hard-coded results, keyed to particular method names.\n * Only primitive JSON values are permitted as hard-coded results.\n */\nexport type MiddlewareScaffold<Context extends ContextConstraint> = Record<\n string,\n ScaffoldMiddlewareHandler<JsonRpcParams, Json, Context>\n>;\n\n/**\n * Creates a middleware function from an object of RPC method handler functions,\n * keyed to particular method names. If a method corresponding to a key of this\n * object is requested, this middleware will pass it to the corresponding\n * handler and return the result.\n *\n * @param handlers - The RPC method handler functions.\n * @returns The scaffold middleware function.\n */\nexport function createScaffoldMiddleware<Context extends ContextConstraint>(\n handlers: MiddlewareScaffold<Context>,\n): JsonRpcMiddleware<JsonRpcRequest, Json, Context> {\n return ({ request, context, next }) => {\n const handlerOrResult = handlers[request.method];\n if (handlerOrResult === undefined) {\n return next();\n }\n\n return typeof handlerOrResult === 'function'\n ? handlerOrResult({ request, context, next })\n : handlerOrResult;\n };\n}\n"]}
1
+ {"version":3,"file":"createScaffoldMiddleware.cjs","sourceRoot":"","sources":["../../src/v2/createScaffoldMiddleware.ts"],"names":[],"mappings":";;;AAqBA;;;;;;;;GAQG;AACH,SAAgB,wBAAwB,CACtC,QAA4B;IAE5B,OAAO,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE;QACpC,MAAM,eAAe,GAAG,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACjD,IAAI,eAAe,KAAK,SAAS,EAAE;YACjC,OAAO,IAAI,EAAE,CAAC;SACf;QAED,OAAO,OAAO,eAAe,KAAK,UAAU;YAC1C,CAAC,CAAC,eAAe,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;YAC7C,CAAC,CAAC,eAAe,CAAC;IACtB,CAAC,CAAC;AACJ,CAAC;AAbD,4DAaC","sourcesContent":["import type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\n\nimport type { JsonRpcMiddleware } from './JsonRpcEngineV2';\n\n// Only permit primitive values as hard-coded scaffold middleware results.\ntype JsonPrimitive = string | number | boolean | null;\n\nexport type ScaffoldMiddlewareHandler<\n Params extends JsonRpcParams,\n Result extends Json,\n> = JsonRpcMiddleware<JsonRpcRequest<Params>, Result> | JsonPrimitive;\n\n/**\n * A record of RPC method handler functions or hard-coded results, keyed to particular method names.\n * Only primitive JSON values are permitted as hard-coded results.\n */\nexport type MiddlewareScaffold = Record<\n string,\n ScaffoldMiddlewareHandler<JsonRpcParams, Json>\n>;\n\n/**\n * Creates a middleware function from an object of RPC method handler functions,\n * keyed to particular method names. If a method corresponding to a key of this\n * object is requested, this middleware will pass it to the corresponding\n * handler and return the result.\n *\n * @param handlers - The RPC method handler functions.\n * @returns The scaffold middleware function.\n */\nexport function createScaffoldMiddleware(\n handlers: MiddlewareScaffold,\n): JsonRpcMiddleware<JsonRpcRequest, Json> {\n return ({ request, context, next }) => {\n const handlerOrResult = handlers[request.method];\n if (handlerOrResult === undefined) {\n return next();\n }\n\n return typeof handlerOrResult === 'function'\n ? handlerOrResult({ request, context, next })\n : handlerOrResult;\n };\n}\n"]}
@@ -1,21 +1,12 @@
1
1
  import type { Json, JsonRpcParams, JsonRpcRequest } from "@metamask/utils";
2
2
  import type { JsonRpcMiddleware } from "./JsonRpcEngineV2.cjs";
3
- import type { ContextConstraint } from "./MiddlewareContext.cjs";
4
3
  type JsonPrimitive = string | number | boolean | null;
5
- /**
6
- * A handler for a scaffold middleware function.
7
- *
8
- * @template Params - The parameters of the request.
9
- * @template Result - The result of the request.
10
- * @template Context - The context of the request.
11
- * @returns A JSON-RPC middleware function or a primitive JSON value.
12
- */
13
- export type ScaffoldMiddlewareHandler<Params extends JsonRpcParams, Result extends Json, Context extends ContextConstraint> = JsonRpcMiddleware<JsonRpcRequest<Params>, Result, Context> | JsonPrimitive;
4
+ export type ScaffoldMiddlewareHandler<Params extends JsonRpcParams, Result extends Json> = JsonRpcMiddleware<JsonRpcRequest<Params>, Result> | JsonPrimitive;
14
5
  /**
15
6
  * A record of RPC method handler functions or hard-coded results, keyed to particular method names.
16
7
  * Only primitive JSON values are permitted as hard-coded results.
17
8
  */
18
- export type MiddlewareScaffold<Context extends ContextConstraint> = Record<string, ScaffoldMiddlewareHandler<JsonRpcParams, Json, Context>>;
9
+ export type MiddlewareScaffold = Record<string, ScaffoldMiddlewareHandler<JsonRpcParams, Json>>;
19
10
  /**
20
11
  * Creates a middleware function from an object of RPC method handler functions,
21
12
  * keyed to particular method names. If a method corresponding to a key of this
@@ -25,6 +16,6 @@ export type MiddlewareScaffold<Context extends ContextConstraint> = Record<strin
25
16
  * @param handlers - The RPC method handler functions.
26
17
  * @returns The scaffold middleware function.
27
18
  */
28
- export declare function createScaffoldMiddleware<Context extends ContextConstraint>(handlers: MiddlewareScaffold<Context>): JsonRpcMiddleware<JsonRpcRequest, Json, Context>;
19
+ export declare function createScaffoldMiddleware(handlers: MiddlewareScaffold): JsonRpcMiddleware<JsonRpcRequest, Json>;
29
20
  export {};
30
21
  //# sourceMappingURL=createScaffoldMiddleware.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"createScaffoldMiddleware.d.cts","sourceRoot":"","sources":["../../src/v2/createScaffoldMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,cAAc,EAAE,wBAAwB;AAE3E,OAAO,KAAK,EAAE,iBAAiB,EAAE,8BAA0B;AAC3D,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA4B;AAG7D,KAAK,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;AAEtD;;;;;;;GAOG;AACH,MAAM,MAAM,yBAAyB,CACnC,MAAM,SAAS,aAAa,EAC5B,MAAM,SAAS,IAAI,EACnB,OAAO,SAAS,iBAAiB,IAC/B,iBAAiB,CAAC,cAAc,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,GAAG,aAAa,CAAC;AAE/E;;;GAGG;AACH,MAAM,MAAM,kBAAkB,CAAC,OAAO,SAAS,iBAAiB,IAAI,MAAM,CACxE,MAAM,EACN,yBAAyB,CAAC,aAAa,EAAE,IAAI,EAAE,OAAO,CAAC,CACxD,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,SAAS,iBAAiB,EACxE,QAAQ,EAAE,kBAAkB,CAAC,OAAO,CAAC,GACpC,iBAAiB,CAAC,cAAc,EAAE,IAAI,EAAE,OAAO,CAAC,CAWlD"}
1
+ {"version":3,"file":"createScaffoldMiddleware.d.cts","sourceRoot":"","sources":["../../src/v2/createScaffoldMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,cAAc,EAAE,wBAAwB;AAE3E,OAAO,KAAK,EAAE,iBAAiB,EAAE,8BAA0B;AAG3D,KAAK,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;AAEtD,MAAM,MAAM,yBAAyB,CACnC,MAAM,SAAS,aAAa,EAC5B,MAAM,SAAS,IAAI,IACjB,iBAAiB,CAAC,cAAc,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,GAAG,aAAa,CAAC;AAEtE;;;GAGG;AACH,MAAM,MAAM,kBAAkB,GAAG,MAAM,CACrC,MAAM,EACN,yBAAyB,CAAC,aAAa,EAAE,IAAI,CAAC,CAC/C,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CACtC,QAAQ,EAAE,kBAAkB,GAC3B,iBAAiB,CAAC,cAAc,EAAE,IAAI,CAAC,CAWzC"}
@@ -1,21 +1,12 @@
1
1
  import type { Json, JsonRpcParams, JsonRpcRequest } from "@metamask/utils";
2
2
  import type { JsonRpcMiddleware } from "./JsonRpcEngineV2.mjs";
3
- import type { ContextConstraint } from "./MiddlewareContext.mjs";
4
3
  type JsonPrimitive = string | number | boolean | null;
5
- /**
6
- * A handler for a scaffold middleware function.
7
- *
8
- * @template Params - The parameters of the request.
9
- * @template Result - The result of the request.
10
- * @template Context - The context of the request.
11
- * @returns A JSON-RPC middleware function or a primitive JSON value.
12
- */
13
- export type ScaffoldMiddlewareHandler<Params extends JsonRpcParams, Result extends Json, Context extends ContextConstraint> = JsonRpcMiddleware<JsonRpcRequest<Params>, Result, Context> | JsonPrimitive;
4
+ export type ScaffoldMiddlewareHandler<Params extends JsonRpcParams, Result extends Json> = JsonRpcMiddleware<JsonRpcRequest<Params>, Result> | JsonPrimitive;
14
5
  /**
15
6
  * A record of RPC method handler functions or hard-coded results, keyed to particular method names.
16
7
  * Only primitive JSON values are permitted as hard-coded results.
17
8
  */
18
- export type MiddlewareScaffold<Context extends ContextConstraint> = Record<string, ScaffoldMiddlewareHandler<JsonRpcParams, Json, Context>>;
9
+ export type MiddlewareScaffold = Record<string, ScaffoldMiddlewareHandler<JsonRpcParams, Json>>;
19
10
  /**
20
11
  * Creates a middleware function from an object of RPC method handler functions,
21
12
  * keyed to particular method names. If a method corresponding to a key of this
@@ -25,6 +16,6 @@ export type MiddlewareScaffold<Context extends ContextConstraint> = Record<strin
25
16
  * @param handlers - The RPC method handler functions.
26
17
  * @returns The scaffold middleware function.
27
18
  */
28
- export declare function createScaffoldMiddleware<Context extends ContextConstraint>(handlers: MiddlewareScaffold<Context>): JsonRpcMiddleware<JsonRpcRequest, Json, Context>;
19
+ export declare function createScaffoldMiddleware(handlers: MiddlewareScaffold): JsonRpcMiddleware<JsonRpcRequest, Json>;
29
20
  export {};
30
21
  //# sourceMappingURL=createScaffoldMiddleware.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"createScaffoldMiddleware.d.mts","sourceRoot":"","sources":["../../src/v2/createScaffoldMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,cAAc,EAAE,wBAAwB;AAE3E,OAAO,KAAK,EAAE,iBAAiB,EAAE,8BAA0B;AAC3D,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA4B;AAG7D,KAAK,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;AAEtD;;;;;;;GAOG;AACH,MAAM,MAAM,yBAAyB,CACnC,MAAM,SAAS,aAAa,EAC5B,MAAM,SAAS,IAAI,EACnB,OAAO,SAAS,iBAAiB,IAC/B,iBAAiB,CAAC,cAAc,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,GAAG,aAAa,CAAC;AAE/E;;;GAGG;AACH,MAAM,MAAM,kBAAkB,CAAC,OAAO,SAAS,iBAAiB,IAAI,MAAM,CACxE,MAAM,EACN,yBAAyB,CAAC,aAAa,EAAE,IAAI,EAAE,OAAO,CAAC,CACxD,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,SAAS,iBAAiB,EACxE,QAAQ,EAAE,kBAAkB,CAAC,OAAO,CAAC,GACpC,iBAAiB,CAAC,cAAc,EAAE,IAAI,EAAE,OAAO,CAAC,CAWlD"}
1
+ {"version":3,"file":"createScaffoldMiddleware.d.mts","sourceRoot":"","sources":["../../src/v2/createScaffoldMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,cAAc,EAAE,wBAAwB;AAE3E,OAAO,KAAK,EAAE,iBAAiB,EAAE,8BAA0B;AAG3D,KAAK,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;AAEtD,MAAM,MAAM,yBAAyB,CACnC,MAAM,SAAS,aAAa,EAC5B,MAAM,SAAS,IAAI,IACjB,iBAAiB,CAAC,cAAc,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,GAAG,aAAa,CAAC;AAEtE;;;GAGG;AACH,MAAM,MAAM,kBAAkB,GAAG,MAAM,CACrC,MAAM,EACN,yBAAyB,CAAC,aAAa,EAAE,IAAI,CAAC,CAC/C,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CACtC,QAAQ,EAAE,kBAAkB,GAC3B,iBAAiB,CAAC,cAAc,EAAE,IAAI,CAAC,CAWzC"}
@@ -1 +1 @@
1
- {"version":3,"file":"createScaffoldMiddleware.mjs","sourceRoot":"","sources":["../../src/v2/createScaffoldMiddleware.ts"],"names":[],"mappings":"AA+BA;;;;;;;;GAQG;AACH,MAAM,UAAU,wBAAwB,CACtC,QAAqC;IAErC,OAAO,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE;QACpC,MAAM,eAAe,GAAG,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACjD,IAAI,eAAe,KAAK,SAAS,EAAE;YACjC,OAAO,IAAI,EAAE,CAAC;SACf;QAED,OAAO,OAAO,eAAe,KAAK,UAAU;YAC1C,CAAC,CAAC,eAAe,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;YAC7C,CAAC,CAAC,eAAe,CAAC;IACtB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\n\nimport type { JsonRpcMiddleware } from './JsonRpcEngineV2';\nimport type { ContextConstraint } from './MiddlewareContext';\n\n// Only permit primitive values as hard-coded scaffold middleware results.\ntype JsonPrimitive = string | number | boolean | null;\n\n/**\n * A handler for a scaffold middleware function.\n *\n * @template Params - The parameters of the request.\n * @template Result - The result of the request.\n * @template Context - The context of the request.\n * @returns A JSON-RPC middleware function or a primitive JSON value.\n */\nexport type ScaffoldMiddlewareHandler<\n Params extends JsonRpcParams,\n Result extends Json,\n Context extends ContextConstraint,\n> = JsonRpcMiddleware<JsonRpcRequest<Params>, Result, Context> | JsonPrimitive;\n\n/**\n * A record of RPC method handler functions or hard-coded results, keyed to particular method names.\n * Only primitive JSON values are permitted as hard-coded results.\n */\nexport type MiddlewareScaffold<Context extends ContextConstraint> = Record<\n string,\n ScaffoldMiddlewareHandler<JsonRpcParams, Json, Context>\n>;\n\n/**\n * Creates a middleware function from an object of RPC method handler functions,\n * keyed to particular method names. If a method corresponding to a key of this\n * object is requested, this middleware will pass it to the corresponding\n * handler and return the result.\n *\n * @param handlers - The RPC method handler functions.\n * @returns The scaffold middleware function.\n */\nexport function createScaffoldMiddleware<Context extends ContextConstraint>(\n handlers: MiddlewareScaffold<Context>,\n): JsonRpcMiddleware<JsonRpcRequest, Json, Context> {\n return ({ request, context, next }) => {\n const handlerOrResult = handlers[request.method];\n if (handlerOrResult === undefined) {\n return next();\n }\n\n return typeof handlerOrResult === 'function'\n ? handlerOrResult({ request, context, next })\n : handlerOrResult;\n };\n}\n"]}
1
+ {"version":3,"file":"createScaffoldMiddleware.mjs","sourceRoot":"","sources":["../../src/v2/createScaffoldMiddleware.ts"],"names":[],"mappings":"AAqBA;;;;;;;;GAQG;AACH,MAAM,UAAU,wBAAwB,CACtC,QAA4B;IAE5B,OAAO,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE;QACpC,MAAM,eAAe,GAAG,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACjD,IAAI,eAAe,KAAK,SAAS,EAAE;YACjC,OAAO,IAAI,EAAE,CAAC;SACf;QAED,OAAO,OAAO,eAAe,KAAK,UAAU;YAC1C,CAAC,CAAC,eAAe,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;YAC7C,CAAC,CAAC,eAAe,CAAC;IACtB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\n\nimport type { JsonRpcMiddleware } from './JsonRpcEngineV2';\n\n// Only permit primitive values as hard-coded scaffold middleware results.\ntype JsonPrimitive = string | number | boolean | null;\n\nexport type ScaffoldMiddlewareHandler<\n Params extends JsonRpcParams,\n Result extends Json,\n> = JsonRpcMiddleware<JsonRpcRequest<Params>, Result> | JsonPrimitive;\n\n/**\n * A record of RPC method handler functions or hard-coded results, keyed to particular method names.\n * Only primitive JSON values are permitted as hard-coded results.\n */\nexport type MiddlewareScaffold = Record<\n string,\n ScaffoldMiddlewareHandler<JsonRpcParams, Json>\n>;\n\n/**\n * Creates a middleware function from an object of RPC method handler functions,\n * keyed to particular method names. If a method corresponding to a key of this\n * object is requested, this middleware will pass it to the corresponding\n * handler and return the result.\n *\n * @param handlers - The RPC method handler functions.\n * @returns The scaffold middleware function.\n */\nexport function createScaffoldMiddleware(\n handlers: MiddlewareScaffold,\n): JsonRpcMiddleware<JsonRpcRequest, Json> {\n return ({ request, context, next }) => {\n const handlerOrResult = handlers[request.method];\n if (handlerOrResult === undefined) {\n return next();\n }\n\n return typeof handlerOrResult === 'function'\n ? handlerOrResult({ request, context, next })\n : handlerOrResult;\n };\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../src/v2/index.ts"],"names":[],"mappings":";;;AAAA,+DAA0D;AAAjD,wHAAA,kBAAkB,OAAA;AAC3B,kDAA6C;AAApC,0GAAA,WAAW,OAAA;AACpB,2EAAsE;AAA7D,oIAAA,wBAAwB,OAAA;AACjC,yDAAoD;AAA3C,kHAAA,eAAe,OAAA;AAWxB,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 HandleOptions,\n MergedContextOf,\n MiddlewareParams,\n MiddlewareConstraint,\n Next,\n RequestOf,\n ResultConstraint,\n} from './JsonRpcEngineV2';\nexport { JsonRpcServer } from './JsonRpcServer';\nexport { MiddlewareContext } from './MiddlewareContext';\nexport type { EmptyContext, ContextConstraint } from './MiddlewareContext';\nexport { isNotification, isRequest, JsonRpcEngineError } from './utils';\nexport type {\n Json,\n JsonRpcCall,\n JsonRpcNotification,\n JsonRpcParams,\n JsonRpcRequest,\n} from './utils';\n"]}
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;AAUxB,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 MergedContextOf,\n MiddlewareParams,\n MiddlewareConstraint,\n Next,\n RequestOf,\n ResultConstraint,\n} from './JsonRpcEngineV2';\nexport { JsonRpcServer } from './JsonRpcServer';\nexport { MiddlewareContext } from './MiddlewareContext';\nexport type { EmptyContext, ContextConstraint } from './MiddlewareContext';\nexport { isNotification, isRequest, JsonRpcEngineError } from './utils';\nexport type {\n Json,\n JsonRpcCall,\n JsonRpcNotification,\n JsonRpcParams,\n JsonRpcRequest,\n} from './utils';\n"]}
@@ -2,7 +2,7 @@ export { asLegacyMiddleware } from "./asLegacyMiddleware.cjs";
2
2
  export { getUniqueId } from "../getUniqueId.cjs";
3
3
  export { createScaffoldMiddleware } from "./createScaffoldMiddleware.cjs";
4
4
  export { JsonRpcEngineV2 } from "./JsonRpcEngineV2.cjs";
5
- export type { JsonRpcMiddleware, HandleOptions, MergedContextOf, MiddlewareParams, MiddlewareConstraint, Next, RequestOf, ResultConstraint, } from "./JsonRpcEngineV2.cjs";
5
+ export type { JsonRpcMiddleware, MergedContextOf, MiddlewareParams, MiddlewareConstraint, Next, RequestOf, ResultConstraint, } from "./JsonRpcEngineV2.cjs";
6
6
  export { JsonRpcServer } from "./JsonRpcServer.cjs";
7
7
  export { MiddlewareContext } from "./MiddlewareContext.cjs";
8
8
  export type { EmptyContext, ContextConstraint } from "./MiddlewareContext.cjs";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../src/v2/index.ts"],"names":[],"mappings":"AAAA,OAA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ware } from "./asLegacyMiddleware.mjs";
2
2
  export { getUniqueId } from "../getUniqueId.mjs";
3
3
  export { createScaffoldMiddleware } from "./createScaffoldMiddleware.mjs";
4
4
  export { JsonRpcEngineV2 } from "./JsonRpcEngineV2.mjs";
5
- export type { JsonRpcMiddleware, HandleOptions, MergedContextOf, MiddlewareParams, MiddlewareConstraint, Next, RequestOf, ResultConstraint, } from "./JsonRpcEngineV2.mjs";
5
+ export type { JsonRpcMiddleware, MergedContextOf, MiddlewareParams, MiddlewareConstraint, Next, RequestOf, ResultConstraint, } from "./JsonRpcEngineV2.mjs";
6
6
  export { JsonRpcServer } from "./JsonRpcServer.mjs";
7
7
  export { MiddlewareContext } from "./MiddlewareContext.mjs";
8
8
  export type { EmptyContext, ContextConstraint } from "./MiddlewareContext.mjs";
@@ -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,OAAO,EAAE,eAAe,EAAE,8BAA0B;AACpD,YAAY,EACV,iBAAiB,EACjB,aAAa,EACb,eAAe,EACf,gBAAgB,EAChB,oBAAoB,EACpB,IAAI,EACJ,SAAS,EACT,gBAAgB,GACjB,8BAA0B;AAC3B,OAAO,EAAE,aAAa,EAAE,4BAAwB;AAChD,OAAO,EAAE,iBAAiB,EAAE,gCAA4B;AACxD,YAAY,EAAE,YAAY,EAAE,iBAAiB,EAAE,gCAA4B;AAC3E,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,kBAAkB,EAAE,oBAAgB;AACxE,YAAY,EACV,IAAI,EACJ,WAAW,EACX,mBAAmB,EACnB,aAAa,EACb,cAAc,GACf,oBAAgB"}
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,eAAe,EACf,gBAAgB,EAChB,oBAAoB,EACpB,IAAI,EACJ,SAAS,EACT,gBAAgB,GACjB,8BAA0B;AAC3B,OAAO,EAAE,aAAa,EAAE,4BAAwB;AAChD,OAAO,EAAE,iBAAiB,EAAE,gCAA4B;AACxD,YAAY,EAAE,YAAY,EAAE,iBAAiB,EAAE,gCAA4B;AAC3E,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,kBAAkB,EAAE,oBAAgB;AACxE,YAAY,EACV,IAAI,EACJ,WAAW,EACX,mBAAmB,EACnB,aAAa,EACb,cAAc,GACf,oBAAgB"}
@@ -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,OAAO,EAAE,eAAe,EAAE,8BAA0B;AAWpD,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 HandleOptions,\n MergedContextOf,\n MiddlewareParams,\n MiddlewareConstraint,\n Next,\n RequestOf,\n ResultConstraint,\n} from './JsonRpcEngineV2';\nexport { JsonRpcServer } from './JsonRpcServer';\nexport { MiddlewareContext } from './MiddlewareContext';\nexport type { EmptyContext, ContextConstraint } from './MiddlewareContext';\nexport { isNotification, isRequest, JsonRpcEngineError } from './utils';\nexport type {\n Json,\n JsonRpcCall,\n JsonRpcNotification,\n JsonRpcParams,\n JsonRpcRequest,\n} from './utils';\n"]}
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;AAUpD,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 MergedContextOf,\n MiddlewareParams,\n MiddlewareConstraint,\n Next,\n RequestOf,\n ResultConstraint,\n} from './JsonRpcEngineV2';\nexport { JsonRpcServer } from './JsonRpcServer';\nexport { MiddlewareContext } from './MiddlewareContext';\nexport type { EmptyContext, ContextConstraint } 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/utils.cjs CHANGED
@@ -17,7 +17,7 @@ function stringify(value) {
17
17
  return JSON.stringify(value, null, 2);
18
18
  }
19
19
  exports.stringify = stringify;
20
- const JsonRpcEngineErrorSymbol = Symbol.for('json-rpc-engine#JsonRpcEngineError');
20
+ const JsonRpcEngineErrorSymbol = Symbol.for('JsonRpcEngineError');
21
21
  class JsonRpcEngineError extends Error {
22
22
  constructor(message) {
23
23
  super(message);
@@ -32,7 +32,7 @@ class JsonRpcEngineError extends Error {
32
32
  * @returns Whether the value is a {@link JsonRpcEngineError} instance.
33
33
  */
34
34
  static isInstance(value) {
35
- return (JsonRpcEngineErrorSymbol in value &&
35
+ return ((0, utils_1.hasProperty)(value, JsonRpcEngineErrorSymbol) &&
36
36
  value[JsonRpcEngineErrorSymbol] === true);
37
37
  }
38
38
  }
@@ -1 +1 @@
1
- {"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../../src/v2/utils.ts"],"names":[],"mappings":";;;;AAAA,2CAKyB;AAalB,MAAM,SAAS,GAAG,CACvB,GAAwD,EACzB,EAAE,CAAC,IAAA,mBAAW,EAAC,GAAG,EAAE,IAAI,CAAC,CAAC;AAF9C,QAAA,SAAS,aAEqC;AAEpD,MAAM,cAAc,GAAG,CAC5B,GAAwB,EACY,EAAE,CAAC,CAAC,IAAA,iBAAS,EAAC,GAAG,CAAC,CAAC;AAF5C,QAAA,cAAc,kBAE8B;AAgBzD;;;;;GAKG;AACH,SAAgB,SAAS,CAAC,KAAc;IACtC,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;AACxC,CAAC;AAFD,8BAEC;AAED,MAAM,wBAAwB,GAAG,MAAM,CAAC,GAAG,CACzC,oCAAoC,CACrC,CAAC;AAEF,MAAa,kBAAmB,SAAQ,KAAK;IAG3C,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QAHA,QAA0B,GAAG,IAAI,CAAC;QAIjD,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IACnC,CAAC;IAED;;;;;;OAMG;IACH,MAAM,CAAC,UAAU,CACf,KAAY;QAEZ,OAAO,CACL,wBAAwB,IAAI,KAAK;YACjC,KAAK,CAAC,wBAAwB,CAAC,KAAK,IAAI,CACzC,CAAC;IACJ,CAAC;CACF;AAvBD,gDAuBC;KAtBmB,wBAAwB","sourcesContent":["import {\n hasProperty,\n type JsonRpcNotification,\n type JsonRpcParams,\n type JsonRpcRequest,\n} from '@metamask/utils';\n\nexport type {\n Json,\n JsonRpcParams,\n JsonRpcRequest,\n JsonRpcNotification,\n} from '@metamask/utils';\n\nexport type JsonRpcCall<Params extends JsonRpcParams = JsonRpcParams> =\n | JsonRpcNotification<Params>\n | JsonRpcRequest<Params>;\n\nexport const isRequest = <Params extends JsonRpcParams>(\n msg: JsonRpcCall<Params> | Readonly<JsonRpcCall<Params>>,\n): msg is JsonRpcRequest<Params> => hasProperty(msg, 'id');\n\nexport const isNotification = <Params extends JsonRpcParams>(\n msg: JsonRpcCall<Params>,\n): msg is JsonRpcNotification<Params> => !isRequest(msg);\n\n/**\n * An unholy incantation that converts a union of object types into an\n * intersection of object types.\n *\n * @example\n * type A = { a: string } | { b: number };\n * type B = UnionToIntersection<A>; // { a: string } & { b: number }\n */\nexport type UnionToIntersection<U> = (\n U extends never ? never : (k: U) => void\n) extends (k: infer I) => void\n ? I\n : never;\n\n/**\n * JSON-stringifies a value.\n *\n * @param value - The value to stringify.\n * @returns The stringified value.\n */\nexport function stringify(value: unknown): string {\n return JSON.stringify(value, null, 2);\n}\n\nconst JsonRpcEngineErrorSymbol = Symbol.for(\n 'json-rpc-engine#JsonRpcEngineError',\n);\n\nexport class JsonRpcEngineError extends Error {\n private readonly [JsonRpcEngineErrorSymbol] = true;\n\n constructor(message: string) {\n super(message);\n this.name = 'JsonRpcEngineError';\n }\n\n /**\n * Check if a value is a {@link JsonRpcEngineError} instance.\n * Works across different package versions in the same realm.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link JsonRpcEngineError} instance.\n */\n static isInstance<Value extends Error>(\n value: Value,\n ): value is Value & JsonRpcEngineError {\n return (\n JsonRpcEngineErrorSymbol in value &&\n value[JsonRpcEngineErrorSymbol] === true\n );\n }\n}\n"]}
1
+ {"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../../src/v2/utils.ts"],"names":[],"mappings":";;;;AAAA,2CAKyB;AAalB,MAAM,SAAS,GAAG,CACvB,GAAwD,EACzB,EAAE,CAAC,IAAA,mBAAW,EAAC,GAAG,EAAE,IAAI,CAAC,CAAC;AAF9C,QAAA,SAAS,aAEqC;AAEpD,MAAM,cAAc,GAAG,CAC5B,GAAwB,EACY,EAAE,CAAC,CAAC,IAAA,iBAAS,EAAC,GAAG,CAAC,CAAC;AAF5C,QAAA,cAAc,kBAE8B;AAgBzD;;;;;GAKG;AACH,SAAgB,SAAS,CAAC,KAAc;IACtC,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;AACxC,CAAC;AAFD,8BAEC;AAED,MAAM,wBAAwB,GAAG,MAAM,CAAC,GAAG,CAAC,oBAAoB,CAAC,CAAC;AAElE,MAAa,kBAAmB,SAAQ,KAAK;IAG3C,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QAHA,QAA0B,GAAG,IAAI,CAAC;QAIjD,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IACnC,CAAC;IAED;;;;;;OAMG;IACH,MAAM,CAAC,UAAU,CACf,KAAY;QAEZ,OAAO,CACL,IAAA,mBAAW,EAAC,KAAK,EAAE,wBAAwB,CAAC;YAC5C,KAAK,CAAC,wBAAwB,CAAC,KAAK,IAAI,CACzC,CAAC;IACJ,CAAC;CACF;AAvBD,gDAuBC;KAtBmB,wBAAwB","sourcesContent":["import {\n hasProperty,\n type JsonRpcNotification,\n type JsonRpcParams,\n type JsonRpcRequest,\n} from '@metamask/utils';\n\nexport type {\n Json,\n JsonRpcParams,\n JsonRpcRequest,\n JsonRpcNotification,\n} from '@metamask/utils';\n\nexport type JsonRpcCall<Params extends JsonRpcParams = JsonRpcParams> =\n | JsonRpcNotification<Params>\n | JsonRpcRequest<Params>;\n\nexport const isRequest = <Params extends JsonRpcParams>(\n msg: JsonRpcCall<Params> | Readonly<JsonRpcCall<Params>>,\n): msg is JsonRpcRequest<Params> => hasProperty(msg, 'id');\n\nexport const isNotification = <Params extends JsonRpcParams>(\n msg: JsonRpcCall<Params>,\n): msg is JsonRpcNotification<Params> => !isRequest(msg);\n\n/**\n * An unholy incantation that converts a union of object types into an\n * intersection of object types.\n *\n * @example\n * type A = { a: string } | { b: number };\n * type B = UnionToIntersection<A>; // { a: string } & { b: number }\n */\nexport type UnionToIntersection<U> = (\n U extends never ? never : (k: U) => void\n) extends (k: infer I) => void\n ? I\n : never;\n\n/**\n * JSON-stringifies a value.\n *\n * @param value - The value to stringify.\n * @returns The stringified value.\n */\nexport function stringify(value: unknown): string {\n return JSON.stringify(value, null, 2);\n}\n\nconst JsonRpcEngineErrorSymbol = Symbol.for('JsonRpcEngineError');\n\nexport class JsonRpcEngineError extends Error {\n private readonly [JsonRpcEngineErrorSymbol] = true;\n\n constructor(message: string) {\n super(message);\n this.name = 'JsonRpcEngineError';\n }\n\n /**\n * Check if a value is a {@link JsonRpcEngineError} instance.\n * Works across different package versions in the same realm.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link JsonRpcEngineError} instance.\n */\n static isInstance<Value extends Error>(\n value: Value,\n ): value is Value & JsonRpcEngineError {\n return (\n hasProperty(value, JsonRpcEngineErrorSymbol) &&\n value[JsonRpcEngineErrorSymbol] === true\n );\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../../src/v2/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,mBAAmB,EACxB,KAAK,aAAa,EAClB,KAAK,cAAc,EACpB,wBAAwB;AAEzB,YAAY,EACV,IAAI,EACJ,aAAa,EACb,cAAc,EACd,mBAAmB,GACpB,wBAAwB;AAEzB,MAAM,MAAM,WAAW,CAAC,MAAM,SAAS,aAAa,GAAG,aAAa,IAChE,mBAAmB,CAAC,MAAM,CAAC,GAC3B,cAAc,CA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