@metamask-previews/json-rpc-engine 10.2.4-preview-d5ac72227 → 10.2.4-preview-08b3d87

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.
@@ -15,8 +15,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.JsonRpcServer = void 0;
16
16
  const rpc_errors_1 = require("@metamask/rpc-errors");
17
17
  const utils_1 = require("@metamask/utils");
18
- const JsonRpcEngineV2_1 = require("./JsonRpcEngineV2.cjs");
19
18
  const getUniqueId_1 = require("../getUniqueId.cjs");
19
+ const JsonRpcEngineV2_1 = require("./JsonRpcEngineV2.cjs");
20
20
  const jsonrpc = '2.0';
21
21
  /**
22
22
  * A JSON-RPC server that handles requests and notifications.
@@ -1 +1 @@
1
- {"version":3,"file":"JsonRpcServer.cjs","sourceRoot":"","sources":["../../src/v2/JsonRpcServer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,qDAAiE;AAQjE,2CAAwD;AASxD,2DAAoD;AAEpD,oDAA6C;AAe7C,MAAM,OAAO,GAAG,KAAc,CAAC;AAE/B;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAa,aAAa;IAUxB;;;;;;;;;;;;;OAaG;IACH,YAAY,OAA4B;QArB/B,wCAGP;QAEO,yCAA+B;QAiBtC,uBAAA,IAAI,0BAAY,OAAO,CAAC,OAAO,MAAA,CAAC;QAEhC,IAAI,IAAA,mBAAW,EAAC,OAAO,EAAE,QAAQ,CAAC,EAAE,CAAC;YACnC,2DAA2D;YAC3D,uBAAA,IAAI,yBAAW,OAAO,CAAC,MAAM,MAAA,CAAC;QAChC,CAAC;aAAM,CAAC;YACN,kGAAkG;YAClG,uBAAA,IAAI,yBAAW,iCAAe,CAAC,MAAM,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC,MAAA,CAAC;QAC5E,CAAC;IACH,CAAC;IA+DD,KAAK,CAAC,MAAM,CACV,UAA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CAAC;IAC/B,CAAC;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. If the `onError`\n * callback itself throws or rejects, the error is silently ignored.\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 try {\n const maybePromise: unknown = this.#onError?.(error);\n if (maybePromise instanceof Promise) {\n maybePromise.catch(() => {\n // Prevent unhandled promise rejection.\n });\n }\n } catch {\n // onError must not prevent handle() from honoring its \"never throws\" contract.\n }\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.cjs","sourceRoot":"","sources":["../../src/v2/JsonRpcServer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,qDAAiE;AAQjE,2CAAwD;AAExD,oDAA6C;AAQ7C,2DAAoD;AAgBpD,MAAM,OAAO,GAAG,KAAc,CAAC;AAE/B;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAa,aAAa;IAUxB;;;;;;;;;;;;;OAaG;IACH,YAAY,OAA4B;QArB/B,wCAGP;QAEO,yCAA+B;QAiBtC,uBAAA,IAAI,0BAAY,OAAO,CAAC,OAAO,MAAA,CAAC;QAEhC,IAAI,IAAA,mBAAW,EAAC,OAAO,EAAE,QAAQ,CAAC,EAAE,CAAC;YACnC,2DAA2D;YAC3D,uBAAA,IAAI,yBAAW,OAAO,CAAC,MAAM,MAAA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CAAC;AAED;;;;;;GAMG;AACH,SAAS,aAAa,CAAC,UAAmB;IACxC,IAAI,IAAA,gBAAQ,EAAC,UAAU,CAAC,IAAI,IAAA,mBAAW,EAAC,UAAU,EAAE,IAAI,CAAC,EAAE,CAAC;QAC1D,OAAO,CAAC,UAAU,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;IAC/B,CAAC;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 { getUniqueId } from '../getUniqueId';\nimport type {\n HandleOptions,\n JsonRpcMiddleware,\n MergedContextOf,\n MiddlewareConstraint,\n RequestOf,\n} from './JsonRpcEngineV2';\nimport { JsonRpcEngineV2 } from './JsonRpcEngineV2';\nimport type { JsonRpcCall } from './utils';\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. If the `onError`\n * callback itself throws or rejects, the error is silently ignored.\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 try {\n const maybePromise: unknown = this.#onError?.(error);\n if (maybePromise instanceof Promise) {\n maybePromise.catch(() => {\n // Prevent unhandled promise rejection.\n });\n }\n } catch {\n // onError must not prevent handle() from honoring its \"never throws\" contract.\n }\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 +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,EACV,aAAa,EACb,iBAAiB,EACjB,eAAe,EACf,oBAAoB,EAErB,8BAA0B;AAC3B,OAAO,EAAE,eAAe,EAAE,8BAA0B;AAIpD,KAAK,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ateFieldGet) || function (
12
12
  var _a, _JsonRpcServer_engine, _JsonRpcServer_onError, _JsonRpcServer_coerceRequest;
13
13
  import { rpcErrors, serializeError } from "@metamask/rpc-errors";
14
14
  import { hasProperty, isObject } from "@metamask/utils";
15
- import { JsonRpcEngineV2 } from "./JsonRpcEngineV2.mjs";
16
15
  import { getUniqueId } from "../getUniqueId.mjs";
16
+ import { JsonRpcEngineV2 } from "./JsonRpcEngineV2.mjs";
17
17
  const jsonrpc = '2.0';
18
18
  /**
19
19
  * A JSON-RPC server that handles requests and notifications.
@@ -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,eAAe,EAAE,8BAA0B;AAEpD,OAAO,EAAE,WAAW,EAAE,2BAAuB;AAe7C,MAAM,OAAO,GAAG,KAAc,CAAC;AAE/B;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,OAAO,aAAa;IAUxB;;;;;;;;;;;;;OAaG;IACH,YAAY,OAA4B;QArB/B,wCAGP;QAEO,yCAA+B;QAiBtC,uBAAA,IAAI,0BAAY,OAAO,CAAC,OAAO,MAAA,CAAC;QAEhC,IAAI,WAAW,CAAC,OAAO,EAAE,QAAQ,CAAC,EAAE,CAAC;YACnC,2DAA2D;YAC3D,uBAAA,IAAI,yBAAW,OAAO,CAAC,MAAM,MAAA,CAAC;QAChC,CAAC;aAAM,CAAC;YACN,kGAAkG;YAClG,uBAAA,IAAI,yBAAW,eAAe,CAAC,MAAM,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC,MAAA,CAAC;QAC5E,CAAC;IACH,CAAC;IA+DD,KAAK,CAAC,MAAM,CACV,UAAmB,EACnB,OAAoD;QAE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eError } 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. If the `onError`\n * callback itself throws or rejects, the error is silently ignored.\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 try {\n const maybePromise: unknown = this.#onError?.(error);\n if (maybePromise instanceof Promise) {\n maybePromise.catch(() => {\n // Prevent unhandled promise rejection.\n });\n }\n } catch {\n // onError must not prevent handle() from honoring its \"never throws\" contract.\n }\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;AAExD,OAAO,EAAE,WAAW,EAAE,2BAAuB;AAQ7C,OAAO,EAAE,eAAe,EAAE,8BAA0B;AAgBpD,MAAM,OAAO,GAAG,KAAc,CAAC;AAE/B;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,OAAO,aAAa;IAUxB;;;;;;;;;;;;;OAaG;IACH,YAAY,OAA4B;QArB/B,wCAGP;QAEO,yCAA+B;QAiBtC,uBAAA,IAAI,0BAAY,OAAO,CAAC,OAAO,MAAA,CAAC;QAEhC,IAAI,WAAW,CAAC,OAAO,EAAE,QAAQ,CAAC,EAAE,CAAC;YACnC,2DAA2D;YAC3D,uBAAA,IAAI,yBAAW,OAAO,CA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ACxC,IAAI,QAAQ,CAAC,UAAU,CAAC,IAAI,WAAW,CAAC,UAAU,EAAE,IAAI,CAAC,EAAE,CAAC;QAC1D,OAAO,CAAC,UAAU,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;IAC/B,CAAC;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 { getUniqueId } from '../getUniqueId';\nimport type {\n HandleOptions,\n JsonRpcMiddleware,\n MergedContextOf,\n MiddlewareConstraint,\n RequestOf,\n} from './JsonRpcEngineV2';\nimport { JsonRpcEngineV2 } from './JsonRpcEngineV2';\nimport type { JsonRpcCall } from './utils';\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. If the `onError`\n * callback itself throws or rejects, the error is silently ignored.\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 try {\n const maybePromise: unknown = this.#onError?.(error);\n if (maybePromise instanceof Promise) {\n maybePromise.catch(() => {\n // Prevent unhandled promise rejection.\n });\n }\n } catch {\n // onError must not prevent handle() from honoring its \"never throws\" contract.\n }\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,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.asLegacyMiddleware = void 0;
4
+ const __1 = require("../index.cjs");
4
5
  const compatibility_utils_1 = require("./compatibility-utils.cjs");
5
6
  const JsonRpcEngineV2_1 = require("./JsonRpcEngineV2.cjs");
6
- const __1 = require("../index.cjs");
7
7
  /**
8
8
  * The asLegacyMiddleware implementation.
9
9
  *
@@ -1 +1 @@
1
- {"version":3,"file":"asLegacyMiddleware.cjs","sourceRoot":"","sources":["../../src/v2/asLegacyMiddleware.ts"],"names":[],"mappings":";;;AA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,CAAC,CAAC;AACL,CAAC;AA1CD,gDA0CC","sourcesContent":["import type { JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\n\nimport {\n deepClone,\n fromLegacyRequest,\n makeContext,\n propagateToRequest,\n} from './compatibility-utils';\nimport type { JsonRpcMiddleware, ResultConstraint } from './JsonRpcEngineV2';\nimport { JsonRpcEngineV2 } from './JsonRpcEngineV2';\nimport { createAsyncMiddleware } from '..';\nimport type { JsonRpcMiddleware as LegacyMiddleware } from '..';\n\n/**\n * Convert a {@link JsonRpcEngineV2} into a legacy middleware.\n *\n * @param engine - The engine to convert.\n * @returns The legacy middleware.\n */\nexport function asLegacyMiddleware<\n Params extends JsonRpcParams,\n Request extends JsonRpcRequest<Params>,\n>(\n engine: JsonRpcEngineV2<Request>,\n): LegacyMiddleware<Params, ResultConstraint<Request>>;\n\n/**\n * Convert one or more V2 middlewares into a legacy middleware.\n *\n * @param middleware - The V2 middleware(s) to convert.\n * @returns The legacy middleware.\n */\nexport function asLegacyMiddleware<\n Params extends JsonRpcParams,\n Request extends JsonRpcRequest<Params>,\n>(\n ...middleware: JsonRpcMiddleware<Request, ResultConstraint<Request>>[]\n): LegacyMiddleware<Params, ResultConstraint<Request>>;\n\n/**\n * The asLegacyMiddleware implementation.\n *\n * @param engineOrMiddleware - A V2 engine or V2 middleware.\n * @param rest - Any additional V2 middleware when the first argument is a middleware.\n * @returns The legacy middleware.\n */\nexport function asLegacyMiddleware<\n Params extends JsonRpcParams,\n Request extends JsonRpcRequest<Params>,\n>(\n engineOrMiddleware:\n | JsonRpcEngineV2<Request>\n | JsonRpcMiddleware<Request, ResultConstraint<Request>>,\n ...rest: JsonRpcMiddleware<Request, ResultConstraint<Request>>[]\n): LegacyMiddleware<Params, ResultConstraint<Request>> {\n const v2Middleware =\n typeof engineOrMiddleware === 'function'\n ? JsonRpcEngineV2.create({\n middleware: [engineOrMiddleware, ...rest],\n }).asMiddleware()\n : engineOrMiddleware.asMiddleware();\n\n return createAsyncMiddleware(async (req, res, next) => {\n const request = fromLegacyRequest(req as Request);\n const context = makeContext(req);\n let modifiedRequest: Request | undefined;\n\n const result = await v2Middleware({\n request,\n context,\n next: (finalRequest) => {\n modifiedRequest = finalRequest;\n return Promise.resolve(undefined);\n },\n });\n\n if (modifiedRequest !== undefined && modifiedRequest !== request) {\n Object.assign(req, deepClone(modifiedRequest));\n }\n propagateToRequest(req, context);\n\n if (result !== undefined) {\n // Unclear why the `as unknown` is needed here, but the cast is safe.\n res.result = deepClone(result) as unknown as ResultConstraint<Request>;\n return undefined;\n }\n return next();\n });\n}\n"]}
1
+ {"version":3,"file":"asLegacyMiddleware.cjs","sourceRoot":"","sources":["../../src/v2/asLegacyMiddleware.ts"],"names":[],"mappings":";;;AAEA,oCAA2C;AAE3C,mEAK+B;AAE/B,2DAAoD;AA4BpD;;;;;;GAMG;AACH,SAAgB,kBAAkB,CAIhC,kBAEyD,EACzD,GAAG,IAA6D;IAEhE,MAAM,YAAY,GAChB,OAAO,kBAAkB,KAAK,UAAU;QACtC,CAAC,CAAC,iCAAe,CAAC,MAAM,CAAC;YACrB,UAAU,EAAE,CAAC,kBAAkB,EAAE,GAAG,IAAI,CAAC;SAC1C,CAAC,CAAC,YAAY,EAAE;QACnB,CAAC,CAAC,kBAAkB,CAAC,YAAY,EAAE,CAAC;IAExC,OAAO,IAAA,yBAAqB,EAAC,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QACpD,MAAM,OAAO,GAAG,IAAA,uCAAiB,EAAC,GAAc,CAAC,CAAC;QAClD,MAAM,OAAO,GAAG,IAAA,iCAAW,EAAC,GAAG,CAAC,CAAC;QACjC,IAAI,eAAoC,CAAC;QAEzC,MAAM,MAAM,GAAG,MAAM,YAAY,CAAC;YAChC,OAAO;YACP,OAAO;YACP,IAAI,EAAE,CAAC,YAAY,EAAE,EAAE;gBACrB,eAAe,GAAG,YAAY,CAAC;gBAC/B,OAAO,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;YACpC,CAAC;SACF,CAAC,CAAC;QAEH,IAAI,eAAe,KAAK,SAAS,IAAI,eAAe,KAAK,OAAO,EAAE,CAAC;YACjE,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,IAAA,+BAAS,EAAC,eAAe,CAAC,CAAC,CAAC;QACjD,CAAC;QACD,IAAA,wCAAkB,EAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QAEjC,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;YACzB,qEAAqE;YACrE,GAAG,CAAC,MAAM,GAAG,IAAA,+BAAS,EAAC,MAAM,CAAyC,CAAC;YACvE,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC,CAAC;AACL,CAAC;AA1CD,gDA0CC","sourcesContent":["import type { JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\n\nimport { createAsyncMiddleware } from '..';\nimport type { JsonRpcMiddleware as LegacyMiddleware } from '..';\nimport {\n deepClone,\n fromLegacyRequest,\n makeContext,\n propagateToRequest,\n} from './compatibility-utils';\nimport type { JsonRpcMiddleware, ResultConstraint } from './JsonRpcEngineV2';\nimport { JsonRpcEngineV2 } from './JsonRpcEngineV2';\n\n/**\n * Convert a {@link JsonRpcEngineV2} into a legacy middleware.\n *\n * @param engine - The engine to convert.\n * @returns The legacy middleware.\n */\nexport function asLegacyMiddleware<\n Params extends JsonRpcParams,\n Request extends JsonRpcRequest<Params>,\n>(\n engine: JsonRpcEngineV2<Request>,\n): LegacyMiddleware<Params, ResultConstraint<Request>>;\n\n/**\n * Convert one or more V2 middlewares into a legacy middleware.\n *\n * @param middleware - The V2 middleware(s) to convert.\n * @returns The legacy middleware.\n */\nexport function asLegacyMiddleware<\n Params extends JsonRpcParams,\n Request extends JsonRpcRequest<Params>,\n>(\n ...middleware: JsonRpcMiddleware<Request, ResultConstraint<Request>>[]\n): LegacyMiddleware<Params, ResultConstraint<Request>>;\n\n/**\n * The asLegacyMiddleware implementation.\n *\n * @param engineOrMiddleware - A V2 engine or V2 middleware.\n * @param rest - Any additional V2 middleware when the first argument is a middleware.\n * @returns The legacy middleware.\n */\nexport function asLegacyMiddleware<\n Params extends JsonRpcParams,\n Request extends JsonRpcRequest<Params>,\n>(\n engineOrMiddleware:\n | JsonRpcEngineV2<Request>\n | JsonRpcMiddleware<Request, ResultConstraint<Request>>,\n ...rest: JsonRpcMiddleware<Request, ResultConstraint<Request>>[]\n): LegacyMiddleware<Params, ResultConstraint<Request>> {\n const v2Middleware =\n typeof engineOrMiddleware === 'function'\n ? JsonRpcEngineV2.create({\n middleware: [engineOrMiddleware, ...rest],\n }).asMiddleware()\n : engineOrMiddleware.asMiddleware();\n\n return createAsyncMiddleware(async (req, res, next) => {\n const request = fromLegacyRequest(req as Request);\n const context = makeContext(req);\n let modifiedRequest: Request | undefined;\n\n const result = await v2Middleware({\n request,\n context,\n next: (finalRequest) => {\n modifiedRequest = finalRequest;\n return Promise.resolve(undefined);\n },\n });\n\n if (modifiedRequest !== undefined && modifiedRequest !== request) {\n Object.assign(req, deepClone(modifiedRequest));\n }\n propagateToRequest(req, context);\n\n if (result !== undefined) {\n // Unclear why the `as unknown` is needed here, but the cast is safe.\n res.result = deepClone(result) as unknown as ResultConstraint<Request>;\n return undefined;\n }\n return next();\n });\n}\n"]}
@@ -1,7 +1,7 @@
1
1
  import type { JsonRpcParams, JsonRpcRequest } from "@metamask/utils";
2
+ import type { JsonRpcMiddleware as LegacyMiddleware } from "../index.cjs";
2
3
  import type { JsonRpcMiddleware, ResultConstraint } from "./JsonRpcEngineV2.cjs";
3
4
  import { JsonRpcEngineV2 } from "./JsonRpcEngineV2.cjs";
4
- import type { JsonRpcMiddleware as LegacyMiddleware } from "../index.cjs";
5
5
  /**
6
6
  * Convert a {@link JsonRpcEngineV2} into a legacy middleware.
7
7
  *
@@ -1 +1 @@
1
- {"version":3,"file":"asLegacyMiddleware.d.cts","sourceRoot":"","sources":["../../src/v2/asLegacyMidd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BAAgB,CAAC,OAAO,CAAC,CAAC,CAAC;AAEvD;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,SAAS,aAAa,EAC5B,OAAO,SAAS,cAAc,CAAC,MAAM,CAAC,EAEtC,GAAG,UAAU,EAAE,iBAAiB,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,EAAE,GACrE,gBAAgB,CAAC,MAAM,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC"}
@@ -1,7 +1,7 @@
1
1
  import type { JsonRpcParams, JsonRpcRequest } from "@metamask/utils";
2
+ import type { JsonRpcMiddleware as LegacyMiddleware } from "../index.mjs";
2
3
  import type { JsonRpcMiddleware, ResultConstraint } from "./JsonRpcEngineV2.mjs";
3
4
  import { JsonRpcEngineV2 } from "./JsonRpcEngineV2.mjs";
4
- import type { JsonRpcMiddleware as LegacyMiddleware } from "../index.mjs";
5
5
  /**
6
6
  * Convert a {@link JsonRpcEngineV2} into a legacy middleware.
7
7
  *
@@ -1 +1 @@
1
- {"version":3,"file":"asLegacyMiddleware.d.mts","sourceRoot":"","sources":["../../src/v2/asLegacyMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,wBAAwB;AAQrE,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,8BAA0B;AAC7E,OAAO,EAAE,eAAe,EAAE,8BAA0B;AAEpD,OAAO,KAAK,EAAE,iBAAiB,IAAI,gBAAgB,EAAE,qBAAW;AAEhE;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,SAAS,aAAa,EAC5B,OAAO,SAAS,cAAc,CAAC,MAAM,CAAC,EAEtC,MAAM,EAAE,eAAe,CAAC,OAAO,CAAC,GAC/B,gBAAgB,CAAC,MAAM,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC;AAEvD;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,SAAS,aAAa,EAC5B,OAAO,SAAS,cAAc,CAAC,MAAM,CAAC,EAEtC,GAAG,UAAU,EAAE,iBAAiB,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,EAAE,GACrE,gBAAgB,CAAC,MAAM,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC"}
1
+ {"version":3,"file":"asLegacyMiddleware.d.mts","sourceRoot":"","sources":["../../src/v2/asLegacyMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,wBAAwB;AAGrE,OAAO,KAAK,EAAE,iBAAiB,IAAI,gBAAgB,EAAE,qBAAW;AAOhE,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,8BAA0B;AAC7E,OAAO,EAAE,eAAe,EAAE,8BAA0B;AAEpD;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,SAAS,aAAa,EAC5B,OAAO,SAAS,cAAc,CAAC,MAAM,CAAC,EAEtC,MAAM,EAAE,eAAe,CAAC,OAAO,CAAC,GAC/B,gBAAgB,CAAC,MAAM,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC;AAEvD;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,SAAS,aAAa,EAC5B,OAAO,SAAS,cAAc,CAAC,MAAM,CAAC,EAEtC,GAAG,UAAU,EAAE,iBAAiB,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,EAAE,GACrE,gBAAgB,CAAC,MAAM,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC"}
@@ -1,6 +1,6 @@
1
+ import { createAsyncMiddleware } from "../index.mjs";
1
2
  import { deepClone, fromLegacyRequest, makeContext, propagateToRequest } from "./compatibility-utils.mjs";
2
3
  import { JsonRpcEngineV2 } from "./JsonRpcEngineV2.mjs";
3
- import { createAsyncMiddleware } from "../index.mjs";
4
4
  /**
5
5
  * The asLegacyMiddleware implementation.
6
6
  *
@@ -1 +1 @@
1
- {"version":3,"file":"asLegacyMiddleware.mjs","sourceRoot":"","sources":["../../src/v2/asLegacyMiddleware.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,SAAS,EACT,iBAAiB,EACjB,WAAW,EACX,kBAAkB,EACnB,kCAA8B;AAE/B,OAAO,EAAE,eAAe,EAAE,8BAA0B;AACpD,OAAO,EAAE,qBAAqB,EAAE,qBAAW;AA6B3C;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAIhC,kBAEyD,EACzD,GAAG,IAA6D;IAEhE,MAAM,YAAY,GAChB,OAAO,kBAAkB,KAAK,UAAU;QACtC,CAAC,CAAC,eAAe,CAAC,MAAM,CAAC;YACrB,UAAU,EAAE,CAAC,kBAAkB,EAAE,GAAG,IAAI,CAAC;SAC1C,CAAC,CAAC,YAAY,EAAE;QACnB,CAAC,CAAC,kBAAkB,CAAC,YAAY,EAAE,CAAC;IAExC,OAAO,qBAAqB,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QACpD,MAAM,OAAO,GAAG,iBAAiB,CAAC,GAAc,CAAC,CAAC;QAClD,MAAM,OAAO,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC;QACjC,IAAI,eAAoC,CAAC;QAEzC,MAAM,MAAM,GAAG,MAAM,YAAY,CAAC;YAChC,OAAO;YACP,OAAO;YACP,IAAI,EAAE,CAAC,YAAY,EAAE,EAAE;gBACrB,eAAe,GAAG,YAAY,CAAC;gBAC/B,OAAO,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;YACpC,CAAC;SACF,CAAC,CAAC;QAEH,IAAI,eAAe,KAAK,SAAS,IAAI,eAAe,KAAK,OAAO,EAAE,CAAC;YACjE,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,SAAS,CAAC,eAAe,CAAC,CAAC,CAAC;QACjD,CAAC;QACD,kBAAkB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QAEjC,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;YACzB,qEAAqE;YACrE,GAAG,CAAC,MAAM,GAAG,SAAS,CAAC,MAAM,CAAyC,CAAC;YACvE,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type { JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\n\nimport {\n deepClone,\n fromLegacyRequest,\n makeContext,\n propagateToRequest,\n} from './compatibility-utils';\nimport type { JsonRpcMiddleware, ResultConstraint } from './JsonRpcEngineV2';\nimport { JsonRpcEngineV2 } from './JsonRpcEngineV2';\nimport { createAsyncMiddleware } from '..';\nimport type { JsonRpcMiddleware as LegacyMiddleware } from '..';\n\n/**\n * Convert a {@link JsonRpcEngineV2} into a legacy middleware.\n *\n * @param engine - The engine to convert.\n * @returns The legacy middleware.\n */\nexport function asLegacyMiddleware<\n Params extends JsonRpcParams,\n Request extends JsonRpcRequest<Params>,\n>(\n engine: JsonRpcEngineV2<Request>,\n): LegacyMiddleware<Params, ResultConstraint<Request>>;\n\n/**\n * Convert one or more V2 middlewares into a legacy middleware.\n *\n * @param middleware - The V2 middleware(s) to convert.\n * @returns The legacy middleware.\n */\nexport function asLegacyMiddleware<\n Params extends JsonRpcParams,\n Request extends JsonRpcRequest<Params>,\n>(\n ...middleware: JsonRpcMiddleware<Request, ResultConstraint<Request>>[]\n): LegacyMiddleware<Params, ResultConstraint<Request>>;\n\n/**\n * The asLegacyMiddleware implementation.\n *\n * @param engineOrMiddleware - A V2 engine or V2 middleware.\n * @param rest - Any additional V2 middleware when the first argument is a middleware.\n * @returns The legacy middleware.\n */\nexport function asLegacyMiddleware<\n Params extends JsonRpcParams,\n Request extends JsonRpcRequest<Params>,\n>(\n engineOrMiddleware:\n | JsonRpcEngineV2<Request>\n | JsonRpcMiddleware<Request, ResultConstraint<Request>>,\n ...rest: JsonRpcMiddleware<Request, ResultConstraint<Request>>[]\n): LegacyMiddleware<Params, ResultConstraint<Request>> {\n const v2Middleware =\n typeof engineOrMiddleware === 'function'\n ? JsonRpcEngineV2.create({\n middleware: [engineOrMiddleware, ...rest],\n }).asMiddleware()\n : engineOrMiddleware.asMiddleware();\n\n return createAsyncMiddleware(async (req, res, next) => {\n const request = fromLegacyRequest(req as Request);\n const context = makeContext(req);\n let modifiedRequest: Request | undefined;\n\n const result = await v2Middleware({\n request,\n context,\n next: (finalRequest) => {\n modifiedRequest = finalRequest;\n return Promise.resolve(undefined);\n },\n });\n\n if (modifiedRequest !== undefined && modifiedRequest !== request) {\n Object.assign(req, deepClone(modifiedRequest));\n }\n propagateToRequest(req, context);\n\n if (result !== undefined) {\n // Unclear why the `as unknown` is needed here, but the cast is safe.\n res.result = deepClone(result) as unknown as ResultConstraint<Request>;\n return undefined;\n }\n return next();\n });\n}\n"]}
1
+ {"version":3,"file":"asLegacyMiddleware.mjs","sourceRoot":"","sources":["../../src/v2/asLegacyMiddleware.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,qBAAW;AAE3C,OAAO,EACL,SAAS,EACT,iBAAiB,EACjB,WAAW,EACX,kBAAkB,EACnB,kCAA8B;AAE/B,OAAO,EAAE,eAAe,EAAE,8BAA0B;AA4BpD;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAIhC,kBAEyD,EACzD,GAAG,IAA6D;IAEhE,MAAM,YAAY,GAChB,OAAO,kBAAkB,KAAK,UAAU;QACtC,CAAC,CAAC,eAAe,CAAC,MAAM,CAAC;YACrB,UAAU,EAAE,CAAC,kBAAkB,EAAE,GAAG,IAAI,CAAC;SAC1C,CAAC,CAAC,YAAY,EAAE;QACnB,CAAC,CAAC,kBAAkB,CAAC,YAAY,EAAE,CAAC;IAExC,OAAO,qBAAqB,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QACpD,MAAM,OAAO,GAAG,iBAAiB,CAAC,GAAc,CAAC,CAAC;QAClD,MAAM,OAAO,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC;QACjC,IAAI,eAAoC,CAAC;QAEzC,MAAM,MAAM,GAAG,MAAM,YAAY,CAAC;YAChC,OAAO;YACP,OAAO;YACP,IAAI,EAAE,CAAC,YAAY,EAAE,EAAE;gBACrB,eAAe,GAAG,YAAY,CAAC;gBAC/B,OAAO,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;YACpC,CAAC;SACF,CAAC,CAAC;QAEH,IAAI,eAAe,KAAK,SAAS,IAAI,eAAe,KAAK,OAAO,EAAE,CAAC;YACjE,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,SAAS,CAAC,eAAe,CAAC,CAAC,CAAC;QACjD,CAAC;QACD,kBAAkB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QAEjC,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;YACzB,qEAAqE;YACrE,GAAG,CAAC,MAAM,GAAG,SAAS,CAAC,MAAM,CAAyC,CAAC;YACvE,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type { JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\n\nimport { createAsyncMiddleware } from '..';\nimport type { JsonRpcMiddleware as LegacyMiddleware } from '..';\nimport {\n deepClone,\n fromLegacyRequest,\n makeContext,\n propagateToRequest,\n} from './compatibility-utils';\nimport type { JsonRpcMiddleware, ResultConstraint } from './JsonRpcEngineV2';\nimport { JsonRpcEngineV2 } from './JsonRpcEngineV2';\n\n/**\n * Convert a {@link JsonRpcEngineV2} into a legacy middleware.\n *\n * @param engine - The engine to convert.\n * @returns The legacy middleware.\n */\nexport function asLegacyMiddleware<\n Params extends JsonRpcParams,\n Request extends JsonRpcRequest<Params>,\n>(\n engine: JsonRpcEngineV2<Request>,\n): LegacyMiddleware<Params, ResultConstraint<Request>>;\n\n/**\n * Convert one or more V2 middlewares into a legacy middleware.\n *\n * @param middleware - The V2 middleware(s) to convert.\n * @returns The legacy middleware.\n */\nexport function asLegacyMiddleware<\n Params extends JsonRpcParams,\n Request extends JsonRpcRequest<Params>,\n>(\n ...middleware: JsonRpcMiddleware<Request, ResultConstraint<Request>>[]\n): LegacyMiddleware<Params, ResultConstraint<Request>>;\n\n/**\n * The asLegacyMiddleware implementation.\n *\n * @param engineOrMiddleware - A V2 engine or V2 middleware.\n * @param rest - Any additional V2 middleware when the first argument is a middleware.\n * @returns The legacy middleware.\n */\nexport function asLegacyMiddleware<\n Params extends JsonRpcParams,\n Request extends JsonRpcRequest<Params>,\n>(\n engineOrMiddleware:\n | JsonRpcEngineV2<Request>\n | JsonRpcMiddleware<Request, ResultConstraint<Request>>,\n ...rest: JsonRpcMiddleware<Request, ResultConstraint<Request>>[]\n): LegacyMiddleware<Params, ResultConstraint<Request>> {\n const v2Middleware =\n typeof engineOrMiddleware === 'function'\n ? JsonRpcEngineV2.create({\n middleware: [engineOrMiddleware, ...rest],\n }).asMiddleware()\n : engineOrMiddleware.asMiddleware();\n\n return createAsyncMiddleware(async (req, res, next) => {\n const request = fromLegacyRequest(req as Request);\n const context = makeContext(req);\n let modifiedRequest: Request | undefined;\n\n const result = await v2Middleware({\n request,\n context,\n next: (finalRequest) => {\n modifiedRequest = finalRequest;\n return Promise.resolve(undefined);\n },\n });\n\n if (modifiedRequest !== undefined && modifiedRequest !== request) {\n Object.assign(req, deepClone(modifiedRequest));\n }\n propagateToRequest(req, context);\n\n if (result !== undefined) {\n // Unclear why the `as unknown` is needed here, but the cast is safe.\n res.result = deepClone(result) as unknown as ResultConstraint<Request>;\n return undefined;\n }\n return next();\n });\n}\n"]}
package/package.json CHANGED
@@ -1,21 +1,30 @@
1
1
  {
2
2
  "name": "@metamask-previews/json-rpc-engine",
3
- "version": "10.2.4-preview-d5ac72227",
3
+ "version": "10.2.4-preview-08b3d87",
4
4
  "description": "A tool for processing JSON-RPC messages",
5
5
  "keywords": [
6
- "MetaMask",
7
- "Ethereum"
6
+ "Ethereum",
7
+ "MetaMask"
8
8
  ],
9
9
  "homepage": "https://github.com/MetaMask/core/tree/main/packages/json-rpc-engine#readme",
10
10
  "bugs": {
11
11
  "url": "https://github.com/MetaMask/core/issues"
12
12
  },
13
+ "license": "ISC",
13
14
  "repository": {
14
15
  "type": "git",
15
16
  "url": "https://github.com/MetaMask/core.git"
16
17
  },
17
- "license": "ISC",
18
+ "directories": {
19
+ "test": "test"
20
+ },
21
+ "files": [
22
+ "dist/",
23
+ "v2.js"
24
+ ],
18
25
  "sideEffects": false,
26
+ "main": "./dist/index.cjs",
27
+ "types": "./dist/index.d.cts",
19
28
  "exports": {
20
29
  ".": {
21
30
  "import": {
@@ -39,15 +48,10 @@
39
48
  },
40
49
  "./package.json": "./package.json"
41
50
  },
42
- "main": "./dist/index.cjs",
43
- "types": "./dist/index.d.cts",
44
- "directories": {
45
- "test": "test"
51
+ "publishConfig": {
52
+ "access": "public",
53
+ "registry": "https://registry.npmjs.org/"
46
54
  },
47
- "files": [
48
- "dist/",
49
- "v2.js"
50
- ],
51
55
  "scripts": {
52
56
  "build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references",
53
57
  "build:all": "ts-bridge --project tsconfig.build.json --verbose --clean",
@@ -77,7 +81,7 @@
77
81
  "devDependencies": {
78
82
  "@lavamoat/allow-scripts": "^3.0.4",
79
83
  "@lavamoat/preinstall-always-fail": "^2.1.0",
80
- "@metamask/auto-changelog": "^3.4.4",
84
+ "@metamask/auto-changelog": "^6.0.0",
81
85
  "@ts-bridge/cli": "^0.6.4",
82
86
  "@types/jest": "^29.5.14",
83
87
  "deepmerge": "^4.2.2",
@@ -90,10 +94,6 @@
90
94
  "engines": {
91
95
  "node": "^18.18 || >=20"
92
96
  },
93
- "publishConfig": {
94
- "access": "public",
95
- "registry": "https://registry.npmjs.org/"
96
- },
97
97
  "lavamoat": {
98
98
  "allowScripts": {
99
99
  "@lavamoat/preinstall-always-fail": false