@metamask-previews/json-rpc-engine 10.2.4-preview-dc38a2562 → 10.2.4-preview-495c91e

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 (42) hide show
  1. package/CHANGELOG.md +0 -4
  2. package/dist/index.cjs +1 -3
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +0 -2
  5. package/dist/index.d.cts.map +1 -1
  6. package/dist/index.d.mts +0 -2
  7. package/dist/index.d.mts.map +1 -1
  8. package/dist/index.mjs +0 -1
  9. package/dist/index.mjs.map +1 -1
  10. package/dist/v2/createMethodMiddleware.cjs +31 -8
  11. package/dist/v2/createMethodMiddleware.cjs.map +1 -1
  12. package/dist/v2/createMethodMiddleware.d.cts +13 -6
  13. package/dist/v2/createMethodMiddleware.d.cts.map +1 -1
  14. package/dist/v2/createMethodMiddleware.d.mts +13 -6
  15. package/dist/v2/createMethodMiddleware.d.mts.map +1 -1
  16. package/dist/v2/createMethodMiddleware.mjs +28 -6
  17. package/dist/v2/createMethodMiddleware.mjs.map +1 -1
  18. package/dist/v2/index.cjs +2 -2
  19. package/dist/v2/index.cjs.map +1 -1
  20. package/dist/v2/index.d.cts +2 -2
  21. package/dist/v2/index.d.cts.map +1 -1
  22. package/dist/v2/index.d.mts +2 -2
  23. package/dist/v2/index.d.mts.map +1 -1
  24. package/dist/v2/index.mjs +2 -2
  25. package/dist/v2/index.mjs.map +1 -1
  26. package/dist/v2/utils.cjs +1 -72
  27. package/dist/v2/utils.cjs.map +1 -1
  28. package/dist/v2/utils.d.cts +0 -39
  29. package/dist/v2/utils.d.cts.map +1 -1
  30. package/dist/v2/utils.d.mts +0 -39
  31. package/dist/v2/utils.d.mts.map +1 -1
  32. package/dist/v2/utils.mjs +0 -68
  33. package/dist/v2/utils.mjs.map +1 -1
  34. package/package.json +1 -1
  35. package/dist/createMethodMiddleware.cjs +0 -57
  36. package/dist/createMethodMiddleware.cjs.map +0 -1
  37. package/dist/createMethodMiddleware.d.cts +0 -76
  38. package/dist/createMethodMiddleware.d.cts.map +0 -1
  39. package/dist/createMethodMiddleware.d.mts +0 -76
  40. package/dist/createMethodMiddleware.d.mts.map +0 -1
  41. package/dist/createMethodMiddleware.mjs +0 -53
  42. package/dist/createMethodMiddleware.mjs.map +0 -1
package/CHANGELOG.md CHANGED
@@ -12,10 +12,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
12
12
  - Add `createOriginMiddleware` utility to `v2` ([#8522](https://github.com/MetaMask/core/pull/8522))
13
13
  - Add `createMethodMiddleware` utility to `v2` ([#8506](https://github.com/MetaMask/core/pull/8506))
14
14
  - This utility allows JSON-RPC method implementations to use both the hooks pattern and the messenger.
15
- - Add legacy `createMethodMiddlewareFactory` ([#8583](https://github.com/MetaMask/core/pull/8583))
16
- - Consolidates bespoke `makeMethodMiddlewareMaker` implementations from the MetaMask extension and mobile clients.
17
- - Handlers may now declare `actionNames` and receive a delegated messenger as the sixth argument to `implementation`, mirroring the v2 `createMethodMiddleware`.
18
- - Deprecated in favor of the v2 `createMethodMiddleware`.
19
15
 
20
16
  ## [10.2.4]
21
17
 
package/dist/index.cjs CHANGED
@@ -1,12 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mergeMiddleware = exports.JsonRpcEngine = exports.createIdRemapMiddleware = exports.getUniqueId = exports.createScaffoldMiddleware = exports.createMethodMiddleware = exports.createAsyncMiddleware = exports.asV2Middleware = void 0;
3
+ exports.mergeMiddleware = exports.JsonRpcEngine = exports.createIdRemapMiddleware = exports.getUniqueId = exports.createScaffoldMiddleware = exports.createAsyncMiddleware = exports.asV2Middleware = void 0;
4
4
  var asV2Middleware_1 = require("./asV2Middleware.cjs");
5
5
  Object.defineProperty(exports, "asV2Middleware", { enumerable: true, get: function () { return asV2Middleware_1.asV2Middleware; } });
6
6
  var createAsyncMiddleware_1 = require("./createAsyncMiddleware.cjs");
7
7
  Object.defineProperty(exports, "createAsyncMiddleware", { enumerable: true, get: function () { return createAsyncMiddleware_1.createAsyncMiddleware; } });
8
- var createMethodMiddleware_1 = require("./createMethodMiddleware.cjs");
9
- Object.defineProperty(exports, "createMethodMiddleware", { enumerable: true, get: function () { return createMethodMiddleware_1.createMethodMiddleware; } });
10
8
  var createScaffoldMiddleware_1 = require("./createScaffoldMiddleware.cjs");
11
9
  Object.defineProperty(exports, "createScaffoldMiddleware", { enumerable: true, get: function () { return createScaffoldMiddleware_1.createScaffoldMiddleware; } });
12
10
  var getUniqueId_1 = require("./getUniqueId.cjs");
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,uDAAkD;AAAzC,gHAAA,cAAc,OAAA;AAKvB,qEAAgE;AAAvD,8HAAA,qBAAqB,OAAA;AAM9B,uEAAkE;AAAzD,gIAAA,sBAAsB,OAAA;AAC/B,2EAAsE;AAA7D,oIAAA,wBAAwB,OAAA;AACjC,iDAA4C;AAAnC,0GAAA,WAAW,OAAA;AACpB,6DAA8D;AAArD,4HAAA,uBAAuB,OAAA;AAShC,qDAAgD;AAAvC,8GAAA,aAAa,OAAA;AACtB,yDAAoD;AAA3C,kHAAA,eAAe,OAAA","sourcesContent":["export { asV2Middleware } from './asV2Middleware';\nexport type {\n AsyncJsonRpcEngineNextCallback,\n AsyncJsonrpcMiddleware,\n} from './createAsyncMiddleware';\nexport { createAsyncMiddleware } from './createAsyncMiddleware';\nexport type {\n CreateMethodMiddlewareOptions,\n MethodHandler,\n MethodHandlerImplementation,\n} from './createMethodMiddleware';\nexport { createMethodMiddleware } from './createMethodMiddleware';\nexport { createScaffoldMiddleware } from './createScaffoldMiddleware';\nexport { getUniqueId } from './getUniqueId';\nexport { createIdRemapMiddleware } from './idRemapMiddleware';\nexport type {\n JsonRpcEngineCallbackError,\n JsonRpcEngineReturnHandler,\n JsonRpcEngineNextCallback,\n JsonRpcEngineEndCallback,\n JsonRpcMiddleware,\n JsonRpcNotificationHandler,\n} from './JsonRpcEngine';\nexport { JsonRpcEngine } from './JsonRpcEngine';\nexport { mergeMiddleware } from './mergeMiddleware';\n"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,uDAAkD;AAAzC,gHAAA,cAAc,OAAA;AAKvB,qEAAgE;AAAvD,8HAAA,qBAAqB,OAAA;AAC9B,2EAAsE;AAA7D,oIAAA,wBAAwB,OAAA;AACjC,iDAA4C;AAAnC,0GAAA,WAAW,OAAA;AACpB,6DAA8D;AAArD,4HAAA,uBAAuB,OAAA;AAShC,qDAAgD;AAAvC,8GAAA,aAAa,OAAA;AACtB,yDAAoD;AAA3C,kHAAA,eAAe,OAAA","sourcesContent":["export { asV2Middleware } from './asV2Middleware';\nexport type {\n AsyncJsonRpcEngineNextCallback,\n AsyncJsonrpcMiddleware,\n} from './createAsyncMiddleware';\nexport { createAsyncMiddleware } from './createAsyncMiddleware';\nexport { createScaffoldMiddleware } from './createScaffoldMiddleware';\nexport { getUniqueId } from './getUniqueId';\nexport { createIdRemapMiddleware } from './idRemapMiddleware';\nexport type {\n JsonRpcEngineCallbackError,\n JsonRpcEngineReturnHandler,\n JsonRpcEngineNextCallback,\n JsonRpcEngineEndCallback,\n JsonRpcMiddleware,\n JsonRpcNotificationHandler,\n} from './JsonRpcEngine';\nexport { JsonRpcEngine } from './JsonRpcEngine';\nexport { mergeMiddleware } from './mergeMiddleware';\n"]}
package/dist/index.d.cts CHANGED
@@ -1,8 +1,6 @@
1
1
  export { asV2Middleware } from "./asV2Middleware.cjs";
2
2
  export type { AsyncJsonRpcEngineNextCallback, AsyncJsonrpcMiddleware, } from "./createAsyncMiddleware.cjs";
3
3
  export { createAsyncMiddleware } from "./createAsyncMiddleware.cjs";
4
- export type { CreateMethodMiddlewareOptions, MethodHandler, MethodHandlerImplementation, } from "./createMethodMiddleware.cjs";
5
- export { createMethodMiddleware } from "./createMethodMiddleware.cjs";
6
4
  export { createScaffoldMiddleware } from "./createScaffoldMiddleware.cjs";
7
5
  export { getUniqueId } from "./getUniqueId.cjs";
8
6
  export { createIdRemapMiddleware } from "./idRemapMiddleware.cjs";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,6BAAyB;AAClD,YAAY,EACV,8BAA8B,EAC9B,sBAAsB,GACvB,oCAAgC;AACjC,OAAO,EAAE,qBAAqB,EAAE,oCAAgC;AAChE,YAAY,EACV,6BAA6B,EAC7B,aAAa,EACb,2BAA2B,GAC5B,qCAAiC;AAClC,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAClE,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AACtE,OAAO,EAAE,WAAW,EAAE,0BAAsB;AAC5C,OAAO,EAAE,uBAAuB,EAAE,gCAA4B;AAC9D,YAAY,EACV,0BAA0B,EAC1B,0BAA0B,EAC1B,yBAAyB,EACzB,wBAAwB,EACxB,iBAAiB,EACjB,0BAA0B,GAC3B,4BAAwB;AACzB,OAAO,EAAE,aAAa,EAAE,4BAAwB;AAChD,OAAO,EAAE,eAAe,EAAE,8BAA0B"}
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,6BAAyB;AAClD,YAAY,EACV,8BAA8B,EAC9B,sBAAsB,GACvB,oCAAgC;AACjC,OAAO,EAAE,qBAAqB,EAAE,oCAAgC;AAChE,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AACtE,OAAO,EAAE,WAAW,EAAE,0BAAsB;AAC5C,OAAO,EAAE,uBAAuB,EAAE,gCAA4B;AAC9D,YAAY,EACV,0BAA0B,EAC1B,0BAA0B,EAC1B,yBAAyB,EACzB,wBAAwB,EACxB,iBAAiB,EACjB,0BAA0B,GAC3B,4BAAwB;AACzB,OAAO,EAAE,aAAa,EAAE,4BAAwB;AAChD,OAAO,EAAE,eAAe,EAAE,8BAA0B"}
package/dist/index.d.mts CHANGED
@@ -1,8 +1,6 @@
1
1
  export { asV2Middleware } from "./asV2Middleware.mjs";
2
2
  export type { AsyncJsonRpcEngineNextCallback, AsyncJsonrpcMiddleware, } from "./createAsyncMiddleware.mjs";
3
3
  export { createAsyncMiddleware } from "./createAsyncMiddleware.mjs";
4
- export type { CreateMethodMiddlewareOptions, MethodHandler, MethodHandlerImplementation, } from "./createMethodMiddleware.mjs";
5
- export { createMethodMiddleware } from "./createMethodMiddleware.mjs";
6
4
  export { createScaffoldMiddleware } from "./createScaffoldMiddleware.mjs";
7
5
  export { getUniqueId } from "./getUniqueId.mjs";
8
6
  export { createIdRemapMiddleware } from "./idRemapMiddleware.mjs";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,6BAAyB;AAClD,YAAY,EACV,8BAA8B,EAC9B,sBAAsB,GACvB,oCAAgC;AACjC,OAAO,EAAE,qBAAqB,EAAE,oCAAgC;AAChE,YAAY,EACV,6BAA6B,EAC7B,aAAa,EACb,2BAA2B,GAC5B,qCAAiC;AAClC,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAClE,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AACtE,OAAO,EAAE,WAAW,EAAE,0BAAsB;AAC5C,OAAO,EAAE,uBAAuB,EAAE,gCAA4B;AAC9D,YAAY,EACV,0BAA0B,EAC1B,0BAA0B,EAC1B,yBAAyB,EACzB,wBAAwB,EACxB,iBAAiB,EACjB,0BAA0B,GAC3B,4BAAwB;AACzB,OAAO,EAAE,aAAa,EAAE,4BAAwB;AAChD,OAAO,EAAE,eAAe,EAAE,8BAA0B"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,6BAAyB;AAClD,YAAY,EACV,8BAA8B,EAC9B,sBAAsB,GACvB,oCAAgC;AACjC,OAAO,EAAE,qBAAqB,EAAE,oCAAgC;AAChE,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AACtE,OAAO,EAAE,WAAW,EAAE,0BAAsB;AAC5C,OAAO,EAAE,uBAAuB,EAAE,gCAA4B;AAC9D,YAAY,EACV,0BAA0B,EAC1B,0BAA0B,EAC1B,yBAAyB,EACzB,wBAAwB,EACxB,iBAAiB,EACjB,0BAA0B,GAC3B,4BAAwB;AACzB,OAAO,EAAE,aAAa,EAAE,4BAAwB;AAChD,OAAO,EAAE,eAAe,EAAE,8BAA0B"}
package/dist/index.mjs CHANGED
@@ -1,6 +1,5 @@
1
1
  export { asV2Middleware } from "./asV2Middleware.mjs";
2
2
  export { createAsyncMiddleware } from "./createAsyncMiddleware.mjs";
3
- export { createMethodMiddleware } from "./createMethodMiddleware.mjs";
4
3
  export { createScaffoldMiddleware } from "./createScaffoldMiddleware.mjs";
5
4
  export { getUniqueId } from "./getUniqueId.mjs";
6
5
  export { createIdRemapMiddleware } from "./idRemapMiddleware.mjs";
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,6BAAyB;AAKlD,OAAO,EAAE,qBAAqB,EAAE,oCAAgC;AAMhE,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAClE,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AACtE,OAAO,EAAE,WAAW,EAAE,0BAAsB;AAC5C,OAAO,EAAE,uBAAuB,EAAE,gCAA4B;AAS9D,OAAO,EAAE,aAAa,EAAE,4BAAwB;AAChD,OAAO,EAAE,eAAe,EAAE,8BAA0B","sourcesContent":["export { asV2Middleware } from './asV2Middleware';\nexport type {\n AsyncJsonRpcEngineNextCallback,\n AsyncJsonrpcMiddleware,\n} from './createAsyncMiddleware';\nexport { createAsyncMiddleware } from './createAsyncMiddleware';\nexport type {\n CreateMethodMiddlewareOptions,\n MethodHandler,\n MethodHandlerImplementation,\n} from './createMethodMiddleware';\nexport { createMethodMiddleware } from './createMethodMiddleware';\nexport { createScaffoldMiddleware } from './createScaffoldMiddleware';\nexport { getUniqueId } from './getUniqueId';\nexport { createIdRemapMiddleware } from './idRemapMiddleware';\nexport type {\n JsonRpcEngineCallbackError,\n JsonRpcEngineReturnHandler,\n JsonRpcEngineNextCallback,\n JsonRpcEngineEndCallback,\n JsonRpcMiddleware,\n JsonRpcNotificationHandler,\n} from './JsonRpcEngine';\nexport { JsonRpcEngine } from './JsonRpcEngine';\nexport { mergeMiddleware } from './mergeMiddleware';\n"]}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,6BAAyB;AAKlD,OAAO,EAAE,qBAAqB,EAAE,oCAAgC;AAChE,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AACtE,OAAO,EAAE,WAAW,EAAE,0BAAsB;AAC5C,OAAO,EAAE,uBAAuB,EAAE,gCAA4B;AAS9D,OAAO,EAAE,aAAa,EAAE,4BAAwB;AAChD,OAAO,EAAE,eAAe,EAAE,8BAA0B","sourcesContent":["export { asV2Middleware } from './asV2Middleware';\nexport type {\n AsyncJsonRpcEngineNextCallback,\n AsyncJsonrpcMiddleware,\n} from './createAsyncMiddleware';\nexport { createAsyncMiddleware } from './createAsyncMiddleware';\nexport { createScaffoldMiddleware } from './createScaffoldMiddleware';\nexport { getUniqueId } from './getUniqueId';\nexport { createIdRemapMiddleware } from './idRemapMiddleware';\nexport type {\n JsonRpcEngineCallbackError,\n JsonRpcEngineReturnHandler,\n JsonRpcEngineNextCallback,\n JsonRpcEngineEndCallback,\n JsonRpcMiddleware,\n JsonRpcNotificationHandler,\n} from './JsonRpcEngine';\nexport { JsonRpcEngine } from './JsonRpcEngine';\nexport { mergeMiddleware } from './mergeMiddleware';\n"]}
@@ -1,8 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createMethodMiddleware = void 0;
3
+ exports.selectHooks = exports.createMethodMiddleware = void 0;
4
4
  const messenger_1 = require("@metamask/messenger");
5
- const utils_1 = require("./utils.cjs");
6
5
  /**
7
6
  * Create a JSON-RPC middleware that handles the passed JSON-RPC method handlers using the messenger and hooks.
8
7
  *
@@ -15,14 +14,15 @@ const utils_1 = require("./utils.cjs");
15
14
  function createMethodMiddleware(options) {
16
15
  const { messenger: rootMessenger } = options;
17
16
  const allHooks = options.hooks;
18
- const expectedHookNames = new Set(Object.values(options.handlers).flatMap((handler) => handler.hookNames ? Object.getOwnPropertyNames(handler.hookNames) : []));
19
- (0, utils_1.assertExpectedHooks)(allHooks, expectedHookNames);
20
17
  const handlers = Object.entries(options.handlers).reduce((accumulator, [handlerName, handler]) => {
21
- const handlerHooks = (0, utils_1.selectHooks)(allHooks, handler.hookNames) ?? {};
22
- const handlerMessenger = (0, utils_1.createHandlerMessenger)({
18
+ const handlerHooks = selectHooks(allHooks, handler.hookNames) ?? {};
19
+ const handlerMessenger = new messenger_1.Messenger({
23
20
  namespace: handlerName,
24
- actionNames: handler.actionNames,
25
- rootMessenger,
21
+ parent: rootMessenger,
22
+ });
23
+ rootMessenger.delegate({
24
+ actions: (handler.actionNames ?? []),
25
+ messenger: handlerMessenger,
26
26
  });
27
27
  accumulator[handlerName] = {
28
28
  implementation: handler.implementation,
@@ -41,4 +41,27 @@ function createMethodMiddleware(options) {
41
41
  };
42
42
  }
43
43
  exports.createMethodMiddleware = createMethodMiddleware;
44
+ /**
45
+ * Returns the subset of the specified `hooks` that are included in the
46
+ * `hookNames` object. This is a Principle of Least Authority (POLA) measure
47
+ * to ensure that each RPC method implementation only has access to the
48
+ * API "hooks" it needs to do its job.
49
+ *
50
+ * @param hooks - The hooks to select from.
51
+ * @param hookNames - The names of the hooks to select.
52
+ * @returns The selected hooks.
53
+ * @template Hooks - The hooks to select from.
54
+ * @template HookName - The names of the hooks to select.
55
+ */
56
+ function selectHooks(hooks, hookNames) {
57
+ if (hookNames) {
58
+ return Object.keys(hookNames).reduce((hookSubset, _hookName) => {
59
+ const hookName = _hookName;
60
+ hookSubset[hookName] = hooks[hookName];
61
+ return hookSubset;
62
+ }, {});
63
+ }
64
+ return undefined;
65
+ }
66
+ exports.selectHooks = selectHooks;
44
67
  //# sourceMappingURL=createMethodMiddleware.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"createMethodMiddleware.cjs","sourceRoot":"","sources":["../../src/v2/createMethodMiddleware.ts"],"names":[],"mappings":";;;AAAA,mDAAkE;AAIlE,uCAQiB;AA0EjB;;;;;;;;GAQG;AACH,SAAgB,sBAAsB,CAIpC,OAAgD;IAEhD,MAAM,EAAE,SAAS,EAAE,aAAa,EAAE,GAAG,OAAO,CAAC;IAC7C,MAAM,QAAQ,GAAG,OAAO,CAAC,KAAgC,CAAC;IAE1D,MAAM,iBAAiB,GAAG,IAAI,GAAG,CAC/B,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAClD,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,mBAAmB,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,EAAE,CACvE,CACF,CAAC;IACF,IAAA,2BAAmB,EAAC,QAAQ,EAAE,iBAAiB,CAAC,CAAC;IAEjD,MAAM,QAAQ,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,MAAM,CAEtD,CAAC,WAAW,EAAE,CAAC,WAAW,EAAE,OAAO,CAAC,EAAE,EAAE;QACxC,MAAM,YAAY,GAAG,IAAA,mBAAW,EAAC,QAAQ,EAAE,OAAO,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC;QACpE,MAAM,gBAAgB,GAAG,IAAA,8BAAsB,EAE7C;YACA,SAAS,EAAE,WAAW;YACtB,WAAW,EAAE,OAAO,CAAC,WAER;YACb,aAAa;SACd,CAAC,CAAC;QAEH,WAAW,CAAC,WAAW,CAAC,GAAG;YACzB,cAAc,EAAE,OAAO,CAAC,cAAc;YACtC,KAAK,EAAE,YAAY;YACnB,SAAS,EAAE,gBAAgB;SAC5B,CAAC;QACF,OAAO,WAAW,CAAC;IACrB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE;QACpC,MAAM,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACzC,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YAC1B,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,MAAM,EAAE,cAAc,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,OAAO,CAAC;QAErD,OAAO,cAAc,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC;IACtE,CAAC,CAAC;AACJ,CAAC;AAhDD,wDAgDC","sourcesContent":["import { ActionConstraint, Messenger } from '@metamask/messenger';\n\nimport { JsonRpcMiddleware, Next } from './JsonRpcEngineV2';\nimport { ContextConstraint } from './MiddlewareContext';\nimport {\n assertExpectedHooks,\n createHandlerMessenger,\n selectHooks,\n Json,\n JsonRpcParams,\n JsonRpcRequest,\n UnionToIntersection,\n} from './utils';\n\ntype HandlerActions<Handler> = Handler extends {\n implementation: (options: infer Options) => unknown;\n}\n ? Options extends { messenger: Messenger<string, infer Actions> }\n ? Actions\n : never\n : never;\n\ntype HandlerHooks<Handler> = Handler extends {\n implementation: (options: infer Options) => unknown;\n}\n ? Options extends { hooks: infer Hooks }\n ? Hooks\n : never\n : never;\n\n/**\n * A `JsonRpcEngineV2` method middleware handler.\n */\nexport type MethodHandler<\n Hooks extends Record<string, unknown> = never,\n MessengerActions extends ActionConstraint = never,\n Parameters extends JsonRpcParams = JsonRpcParams,\n Result extends Json = Json,\n Context extends ContextConstraint = ContextConstraint,\n> = {\n implementation: (options: {\n request: Readonly<JsonRpcRequest<Parameters>>;\n context: Context;\n next: Next<JsonRpcRequest>;\n messenger: Messenger<string, MessengerActions>;\n hooks: Hooks;\n }) => Promise<Result> | Result;\n} & ([Hooks] extends [never]\n ? { hookNames?: undefined }\n : { hookNames: { [Key in keyof Hooks]: true } }) &\n ([MessengerActions] extends [never]\n ? { actionNames?: undefined }\n : { actionNames: MessengerActions['type'][] });\n\ntype AnyMethodHandler = {\n implementation(\n this: void,\n options: {\n request: Readonly<JsonRpcRequest>;\n context: ContextConstraint;\n next: Next<JsonRpcRequest>;\n messenger: unknown;\n hooks: unknown;\n },\n ): Promise<Json> | Json;\n hookNames?: Record<string, true>;\n actionNames?: readonly string[];\n};\n\n/**\n * Options for {@link createMethodMiddleware}.\n */\nexport type CreateMethodMiddlewareOptions<\n Handlers extends Record<string, AnyMethodHandler>,\n> = {\n handlers: Handlers;\n messenger: Messenger<string, HandlerActions<Handlers[keyof Handlers]>>;\n hooks: UnionToIntersection<HandlerHooks<Handlers[keyof Handlers]>>;\n};\n\ntype ResolvedHandler = {\n implementation: AnyMethodHandler['implementation'];\n hooks: Record<string, unknown>;\n messenger: Messenger<string, ActionConstraint>;\n};\n\n/**\n * Create a JSON-RPC middleware that handles the passed JSON-RPC method handlers using the messenger and hooks.\n *\n * @param options The options.\n * @param options.handlers - The JSON-RPC method handler implementations.\n * @param options.messenger - The messenger to be used by the handlers.\n * @param options.hooks - The hooks to be used by the handlers.\n * @returns A JsonRpcEngineV2 middleware.\n */\nexport function createMethodMiddleware<\n Handlers extends Record<string, AnyMethodHandler>,\n Context extends ContextConstraint,\n>(\n options: CreateMethodMiddlewareOptions<Handlers>,\n): JsonRpcMiddleware<JsonRpcRequest, Json, Context> {\n const { messenger: rootMessenger } = options;\n const allHooks = options.hooks as Record<string, unknown>;\n\n const expectedHookNames = new Set(\n Object.values(options.handlers).flatMap((handler) =>\n handler.hookNames ? Object.getOwnPropertyNames(handler.hookNames) : [],\n ),\n );\n assertExpectedHooks(allHooks, expectedHookNames);\n\n const handlers = Object.entries(options.handlers).reduce<\n Record<string, ResolvedHandler>\n >((accumulator, [handlerName, handler]) => {\n const handlerHooks = selectHooks(allHooks, handler.hookNames) ?? {};\n const handlerMessenger = createHandlerMessenger<\n HandlerActions<Handlers[keyof Handlers]>\n >({\n namespace: handlerName,\n actionNames: handler.actionNames as\n | readonly HandlerActions<Handlers[keyof Handlers]>['type'][]\n | undefined,\n rootMessenger,\n });\n\n accumulator[handlerName] = {\n implementation: handler.implementation,\n hooks: handlerHooks,\n messenger: handlerMessenger,\n };\n return accumulator;\n }, {});\n\n return ({ request, context, next }) => {\n const handler = handlers[request.method];\n if (handler === undefined) {\n return next();\n }\n\n const { implementation, hooks, messenger } = handler;\n\n return implementation({ request, context, next, hooks, messenger });\n };\n}\n"]}
1
+ {"version":3,"file":"createMethodMiddleware.cjs","sourceRoot":"","sources":["../../src/v2/createMethodMiddleware.ts"],"names":[],"mappings":";;;AAAA,mDAAkE;AA8ElE;;;;;;;;GAQG;AACH,SAAgB,sBAAsB,CAIpC,OAAgD;IAEhD,MAAM,EAAE,SAAS,EAAE,aAAa,EAAE,GAAG,OAAO,CAAC;IAC7C,MAAM,QAAQ,GAAG,OAAO,CAAC,KAAgC,CAAC;IAE1D,MAAM,QAAQ,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,MAAM,CAEtD,CAAC,WAAW,EAAE,CAAC,WAAW,EAAE,OAAO,CAAC,EAAE,EAAE;QACxC,MAAM,YAAY,GAAG,WAAW,CAAC,QAAQ,EAAE,OAAO,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC;QACpE,MAAM,gBAAgB,GAAG,IAAI,qBAAS,CAKpC;YACA,SAAS,EAAE,WAAW;YACtB,MAAM,EAAE,aAAa;SACtB,CAAC,CAAC;QA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metamask/messenger';\n\nimport { JsonRpcMiddleware, Next } from './JsonRpcEngineV2';\nimport { ContextConstraint } from './MiddlewareContext';\nimport {\n Json,\n JsonRpcParams,\n JsonRpcRequest,\n UnionToIntersection,\n} from './utils';\n\n// The helpers below seem excessive, but they are required for inference of hooks/actions.\ntype HandlerActions<Handler> = Handler extends {\n implementation: (options: infer Options) => unknown;\n}\n ? Options extends { messenger: Messenger<string, infer Actions> }\n ? Actions\n : never\n : never;\n\ntype HandlerHooks<Handler> = Handler extends {\n implementation: (options: infer Options) => unknown;\n}\n ? Options extends { hooks: infer Hooks }\n ? Hooks\n : never\n : never;\n\nexport type MethodHandler<\n Hooks extends Record<string, unknown> = never,\n MessengerActions extends ActionConstraint = never,\n Parameters extends JsonRpcParams = JsonRpcParams,\n Result extends Json = Json,\n Context extends ContextConstraint = ContextConstraint,\n> = {\n implementation: (options: {\n request: Readonly<JsonRpcRequest<Parameters>>;\n context: Context;\n next: Next<JsonRpcRequest>;\n messenger: Messenger<string, MessengerActions>;\n hooks: Hooks;\n }) => Promise<Result> | Result;\n} & ([Hooks] extends [never]\n ? { hookNames?: undefined }\n : { hookNames: { [Key in keyof Hooks]: true } }) &\n ([MessengerActions] extends [never]\n ? { actionNames?: undefined }\n : { actionNames: MessengerActions['type'][] });\n\ntype AnyMethodHandler = {\n implementation(\n this: void,\n options: {\n request: Readonly<JsonRpcRequest>;\n context: ContextConstraint;\n next: Next<JsonRpcRequest>;\n messenger: unknown;\n hooks: unknown;\n },\n ): Promise<Json> | Json;\n hookNames?: Record<string, true>;\n actionNames?: readonly string[];\n};\n\nexport type CreateMethodMiddlewareOptions<\n Handlers extends Record<string, AnyMethodHandler>,\n> = {\n handlers: Handlers;\n messenger: Messenger<string, HandlerActions<Handlers[keyof Handlers]>>;\n hooks: UnionToIntersection<HandlerHooks<Handlers[keyof Handlers]>>;\n};\n\ntype ResolvedHandler = {\n implementation: AnyMethodHandler['implementation'];\n hooks: Record<string, unknown>;\n messenger: Messenger<string, ActionConstraint>;\n};\n\n/**\n * Create a JSON-RPC middleware that handles the passed JSON-RPC method handlers using the messenger and hooks.\n *\n * @param options The options.\n * @param options.handlers - The JSON-RPC method handler implementations.\n * @param options.messenger - The messenger to be used by the handlers.\n * @param options.hooks - The hooks to be used by the handlers.\n * @returns A JsonRpcEngineV2 middleware.\n */\nexport function createMethodMiddleware<\n Handlers extends Record<string, AnyMethodHandler>,\n Context extends ContextConstraint,\n>(\n options: CreateMethodMiddlewareOptions<Handlers>,\n): JsonRpcMiddleware<JsonRpcRequest, Json, Context> {\n const { messenger: rootMessenger } = options;\n const allHooks = options.hooks as Record<string, unknown>;\n\n const handlers = Object.entries(options.handlers).reduce<\n Record<string, ResolvedHandler>\n >((accumulator, [handlerName, handler]) => {\n const handlerHooks = selectHooks(allHooks, handler.hookNames) ?? {};\n const handlerMessenger = new Messenger<\n string,\n HandlerActions<Handlers[keyof Handlers]>,\n never,\n typeof rootMessenger\n >({\n namespace: handlerName,\n parent: rootMessenger,\n });\n\n rootMessenger.delegate({\n actions: (handler.actionNames ?? []) as HandlerActions<\n Handlers[keyof Handlers]\n >['type'][],\n messenger: handlerMessenger,\n });\n\n accumulator[handlerName] = {\n implementation: handler.implementation,\n hooks: handlerHooks,\n messenger: handlerMessenger,\n };\n return accumulator;\n }, {});\n\n return ({ request, context, next }) => {\n const handler = handlers[request.method];\n if (handler === undefined) {\n return next();\n }\n\n const { implementation, hooks, messenger } = handler;\n\n return implementation({ request, context, next, hooks, messenger });\n };\n}\n\n/**\n * Returns the subset of the specified `hooks` that are included in the\n * `hookNames` object. This is a Principle of Least Authority (POLA) measure\n * to ensure that each RPC method implementation only has access to the\n * API \"hooks\" it needs to do its job.\n *\n * @param hooks - The hooks to select from.\n * @param hookNames - The names of the hooks to select.\n * @returns The selected hooks.\n * @template Hooks - The hooks to select from.\n * @template HookName - The names of the hooks to select.\n */\nexport function selectHooks<\n Hooks extends Record<string, unknown>,\n HookName extends keyof Hooks,\n>(\n hooks: Hooks,\n hookNames?: Record<HookName, true>,\n): Pick<Hooks, HookName> | undefined {\n if (hookNames) {\n return Object.keys(hookNames).reduce<Partial<Pick<Hooks, HookName>>>(\n (hookSubset, _hookName) => {\n const hookName = _hookName as HookName;\n hookSubset[hookName] = hooks[hookName];\n return hookSubset;\n },\n {},\n ) as Pick<Hooks, HookName>;\n }\n return undefined;\n}\n"]}
@@ -12,9 +12,6 @@ type HandlerHooks<Handler> = Handler extends {
12
12
  } ? Options extends {
13
13
  hooks: infer Hooks;
14
14
  } ? Hooks : never : never;
15
- /**
16
- * A `JsonRpcEngineV2` method middleware handler.
17
- */
18
15
  export type MethodHandler<Hooks extends Record<string, unknown> = never, MessengerActions extends ActionConstraint = never, Parameters extends JsonRpcParams = JsonRpcParams, Result extends Json = Json, Context extends ContextConstraint = ContextConstraint> = {
19
16
  implementation: (options: {
20
17
  request: Readonly<JsonRpcRequest<Parameters>>;
@@ -45,9 +42,6 @@ type AnyMethodHandler = {
45
42
  hookNames?: Record<string, true>;
46
43
  actionNames?: readonly string[];
47
44
  };
48
- /**
49
- * Options for {@link createMethodMiddleware}.
50
- */
51
45
  export type CreateMethodMiddlewareOptions<Handlers extends Record<string, AnyMethodHandler>> = {
52
46
  handlers: Handlers;
53
47
  messenger: Messenger<string, HandlerActions<Handlers[keyof Handlers]>>;
@@ -63,5 +57,18 @@ export type CreateMethodMiddlewareOptions<Handlers extends Record<string, AnyMet
63
57
  * @returns A JsonRpcEngineV2 middleware.
64
58
  */
65
59
  export declare function createMethodMiddleware<Handlers extends Record<string, AnyMethodHandler>, Context extends ContextConstraint>(options: CreateMethodMiddlewareOptions<Handlers>): JsonRpcMiddleware<JsonRpcRequest, Json, Context>;
60
+ /**
61
+ * Returns the subset of the specified `hooks` that are included in the
62
+ * `hookNames` object. This is a Principle of Least Authority (POLA) measure
63
+ * to ensure that each RPC method implementation only has access to the
64
+ * API "hooks" it needs to do its job.
65
+ *
66
+ * @param hooks - The hooks to select from.
67
+ * @param hookNames - The names of the hooks to select.
68
+ * @returns The selected hooks.
69
+ * @template Hooks - The hooks to select from.
70
+ * @template HookName - The names of the hooks to select.
71
+ */
72
+ export declare function selectHooks<Hooks extends Record<string, unknown>, HookName extends keyof Hooks>(hooks: Hooks, hookNames?: Record<HookName, true>): Pick<Hooks, HookName> | undefined;
66
73
  export {};
67
74
  //# sourceMappingURL=createMethodMiddleware.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"createMethodMiddleware.d.cts","sourceRoot":"","sources":["../../src/v2/createMethodMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,4BAA4B;AAElE,OAAO,EAAE,iBAAiB,EAAE,IAAI,EAAE,8BAA0B;AAC5D,OAAO,EAAE,iBAAiB,EAAE,gCAA4B;AACxD,OAAO,EAIL,IAAI,EACJ,aAAa,EACb,cAAc,EACd,mBAAmB,EACpB,oBAAgB;AAEjB,KAAK,cAAc,CAAC,OAAO,IAAI,OAAO,SAAS;IAC7C,cAAc,EAAE,CAAC,OAAO,EAAE,MAAM,OAAO,KAAK,OAAO,CAAC;CACrD,GACG,OAAO,SAAS;IAAE,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,MAAM,OAAO,CAAC,CAAA;CAAE,GAC7D,OAAO,GACP,KAAK,GACP,KAAK,CAAC;AAEV,KAAK,YAAY,CAAC,OAAO,IAAI,OAAO,SAAS;IAC3C,cAAc,EAAE,CAAC,OAAO,EAAE,MAAM,OAAO,KAAK,OAAO,CAAC;CACrD,GACG,OAAO,SAAS;IAAE,KAAK,EAAE,MAAM,KAAK,CAAA;CAAE,GACpC,KAAK,GACL,KAAK,GACP,KAAK,CAAC;AAEV;;GAEG;AACH,MAAM,MAAM,aAAa,CACvB,KAAK,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,KAAK,EAC7C,gBAAgB,SAAS,gBAAgB,GAAG,KAAK,EACjD,UAAU,SAAS,aAAa,GAAG,aAAa,EAChD,MAAM,S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
12
  } ? Options extends {
13
13
  hooks: infer Hooks;
14
14
  } ? Hooks : never : never;
15
- /**
16
- * A `JsonRpcEngineV2` method middleware handler.
17
- */
18
15
  export type MethodHandler<Hooks extends Record<string, unknown> = never, MessengerActions extends ActionConstraint = never, Parameters extends JsonRpcParams = JsonRpcParams, Result extends Json = Json, Context extends ContextConstraint = ContextConstraint> = {
19
16
  implementation: (options: {
20
17
  request: Readonly<JsonRpcRequest<Parameters>>;
@@ -45,9 +42,6 @@ type AnyMethodHandler = {
45
42
  hookNames?: Record<string, true>;
46
43
  actionNames?: readonly string[];
47
44
  };
48
- /**
49
- * Options for {@link createMethodMiddleware}.
50
- */
51
45
  export type CreateMethodMiddlewareOptions<Handlers extends Record<string, AnyMethodHandler>> = {
52
46
  handlers: Handlers;
53
47
  messenger: Messenger<string, HandlerActions<Handlers[keyof Handlers]>>;
@@ -63,5 +57,18 @@ export type CreateMethodMiddlewareOptions<Handlers extends Record<string, AnyMet
63
57
  * @returns A JsonRpcEngineV2 middleware.
64
58
  */
65
59
  export declare function createMethodMiddleware<Handlers extends Record<string, AnyMethodHandler>, Context extends ContextConstraint>(options: CreateMethodMiddlewareOptions<Handlers>): JsonRpcMiddleware<JsonRpcRequest, Json, Context>;
60
+ /**
61
+ * Returns the subset of the specified `hooks` that are included in the
62
+ * `hookNames` object. This is a Principle of Least Authority (POLA) measure
63
+ * to ensure that each RPC method implementation only has access to the
64
+ * API "hooks" it needs to do its job.
65
+ *
66
+ * @param hooks - The hooks to select from.
67
+ * @param hookNames - The names of the hooks to select.
68
+ * @returns The selected hooks.
69
+ * @template Hooks - The hooks to select from.
70
+ * @template HookName - The names of the hooks to select.
71
+ */
72
+ export declare function selectHooks<Hooks extends Record<string, unknown>, HookName extends keyof Hooks>(hooks: Hooks, hookNames?: Record<HookName, true>): Pick<Hooks, HookName> | undefined;
66
73
  export {};
67
74
  //# sourceMappingURL=createMethodMiddleware.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"createMethodMi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,WAAW,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACjC,CAAC;AAEF,MAAM,MAAM,6BAA6B,CACvC,QAAQ,SAAS,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,IAC/C;IACF,QAAQ,EAAE,QAAQ,CAAC;IACnB,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,cAAc,CAAC,QAAQ,CAAC,MAAM,QAAQ,CAAC,CAAC,CAAC,CAAC;IACvE,KAAK,EAAE,mBAAmB,CAAC,YAAY,CAAC,QAAQ,CAAC,MAAM,QAAQ,CAAC,CAAC,CAAC,CAAC;CACpE,CAAC;AAQF;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,CACpC,QAAQ,SAAS,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,EACjD,OAAO,SAAS,iBAAiB,EAEjC,OAAO,EAAE,6BAA6B,CAAC,QAAQ,CAAC,GAC/C,iBAAiB,CAAC,cAAc,EAAE,IAAI,EAAE,OAAO,CAAC,CA2ClD;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CACzB,KAAK,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACrC,QAAQ,SAAS,MAAM,KAAK,EAE5B,KAAK,EAAE,KAAK,EACZ,SAAS,CAAC,EAAE,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAC,GACjC,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,SAAS,CAYnC"}
@@ -1,5 +1,4 @@
1
1
  import { Messenger } from "@metamask/messenger";
2
- import { assertExpectedHooks, createHandlerMessenger, selectHooks } from "./utils.mjs";
3
2
  /**
4
3
  * Create a JSON-RPC middleware that handles the passed JSON-RPC method handlers using the messenger and hooks.
5
4
  *
@@ -12,14 +11,15 @@ import { assertExpectedHooks, createHandlerMessenger, selectHooks } from "./util
12
11
  export function createMethodMiddleware(options) {
13
12
  const { messenger: rootMessenger } = options;
14
13
  const allHooks = options.hooks;
15
- const expectedHookNames = new Set(Object.values(options.handlers).flatMap((handler) => handler.hookNames ? Object.getOwnPropertyNames(handler.hookNames) : []));
16
- assertExpectedHooks(allHooks, expectedHookNames);
17
14
  const handlers = Object.entries(options.handlers).reduce((accumulator, [handlerName, handler]) => {
18
15
  const handlerHooks = selectHooks(allHooks, handler.hookNames) ?? {};
19
- const handlerMessenger = createHandlerMessenger({
16
+ const handlerMessenger = new Messenger({
20
17
  namespace: handlerName,
21
- actionNames: handler.actionNames,
22
- rootMessenger,
18
+ parent: rootMessenger,
19
+ });
20
+ rootMessenger.delegate({
21
+ actions: (handler.actionNames ?? []),
22
+ messenger: handlerMessenger,
23
23
  });
24
24
  accumulator[handlerName] = {
25
25
  implementation: handler.implementation,
@@ -37,4 +37,26 @@ export function createMethodMiddleware(options) {
37
37
  return implementation({ request, context, next, hooks, messenger });
38
38
  };
39
39
  }
40
+ /**
41
+ * Returns the subset of the specified `hooks` that are included in the
42
+ * `hookNames` object. This is a Principle of Least Authority (POLA) measure
43
+ * to ensure that each RPC method implementation only has access to the
44
+ * API "hooks" it needs to do its job.
45
+ *
46
+ * @param hooks - The hooks to select from.
47
+ * @param hookNames - The names of the hooks to select.
48
+ * @returns The selected hooks.
49
+ * @template Hooks - The hooks to select from.
50
+ * @template HookName - The names of the hooks to select.
51
+ */
52
+ export function selectHooks(hooks, hookNames) {
53
+ if (hookNames) {
54
+ return Object.keys(hookNames).reduce((hookSubset, _hookName) => {
55
+ const hookName = _hookName;
56
+ hookSubset[hookName] = hooks[hookName];
57
+ return hookSubset;
58
+ }, {});
59
+ }
60
+ return undefined;
61
+ }
40
62
  //# sourceMappingURL=createMethodMiddleware.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"createMethodMiddleware.mjs","sourceRoot":"","sources":["../../src/v2/createMethodMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,SAAS,EAAE,4BAA4B;AAIlE,OAAO,EACL,mBAAmB,EACnB,sBAAsB,EACtB,WAAW,EAKZ,oBAAgB;AA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AAS,EAAE,CAAC;YAC1B,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,MAAM,EAAE,cAAc,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,OAAO,CAAC;QAErD,OAAO,cAAc,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC;IACtE,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import { ActionConstraint, Messenger } from '@metamask/messenger';\n\nimport { JsonRpcMiddleware, Next } from './JsonRpcEngineV2';\nimport { ContextConstraint } from './MiddlewareContext';\nimport {\n assertExpectedHooks,\n createHandlerMessenger,\n selectHooks,\n Json,\n JsonRpcParams,\n JsonRpcRequest,\n UnionToIntersection,\n} from './utils';\n\ntype HandlerActions<Handler> = Handler extends {\n implementation: (options: infer Options) => unknown;\n}\n ? Options extends { messenger: Messenger<string, infer Actions> }\n ? Actions\n : never\n : never;\n\ntype HandlerHooks<Handler> = Handler extends {\n implementation: (options: infer Options) => unknown;\n}\n ? Options extends { hooks: infer Hooks }\n ? Hooks\n : never\n : never;\n\n/**\n * A `JsonRpcEngineV2` method middleware handler.\n */\nexport type MethodHandler<\n Hooks extends Record<string, unknown> = never,\n MessengerActions extends ActionConstraint = never,\n Parameters extends JsonRpcParams = JsonRpcParams,\n Result extends Json = Json,\n Context extends ContextConstraint = ContextConstraint,\n> = {\n implementation: (options: {\n request: Readonly<JsonRpcRequest<Parameters>>;\n context: Context;\n next: Next<JsonRpcRequest>;\n messenger: Messenger<string, MessengerActions>;\n hooks: Hooks;\n }) => Promise<Result> | Result;\n} & ([Hooks] extends [never]\n ? { hookNames?: undefined }\n : { hookNames: { [Key in keyof Hooks]: true } }) &\n ([MessengerActions] extends [never]\n ? { actionNames?: undefined }\n : { actionNames: MessengerActions['type'][] });\n\ntype AnyMethodHandler = {\n implementation(\n this: void,\n options: {\n request: Readonly<JsonRpcRequest>;\n context: ContextConstraint;\n next: Next<JsonRpcRequest>;\n messenger: unknown;\n hooks: unknown;\n },\n ): Promise<Json> | Json;\n hookNames?: Record<string, true>;\n actionNames?: readonly string[];\n};\n\n/**\n * Options for {@link createMethodMiddleware}.\n */\nexport type CreateMethodMiddlewareOptions<\n Handlers extends Record<string, AnyMethodHandler>,\n> = {\n handlers: Handlers;\n messenger: Messenger<string, HandlerActions<Handlers[keyof Handlers]>>;\n hooks: UnionToIntersection<HandlerHooks<Handlers[keyof Handlers]>>;\n};\n\ntype ResolvedHandler = {\n implementation: AnyMethodHandler['implementation'];\n hooks: Record<string, unknown>;\n messenger: Messenger<string, ActionConstraint>;\n};\n\n/**\n * Create a JSON-RPC middleware that handles the passed JSON-RPC method handlers using the messenger and hooks.\n *\n * @param options The options.\n * @param options.handlers - The JSON-RPC method handler implementations.\n * @param options.messenger - The messenger to be used by the handlers.\n * @param options.hooks - The hooks to be used by the handlers.\n * @returns A JsonRpcEngineV2 middleware.\n */\nexport function createMethodMiddleware<\n Handlers extends Record<string, AnyMethodHandler>,\n Context extends ContextConstraint,\n>(\n options: CreateMethodMiddlewareOptions<Handlers>,\n): JsonRpcMiddleware<JsonRpcRequest, Json, Context> {\n const { messenger: rootMessenger } = options;\n const allHooks = options.hooks as Record<string, unknown>;\n\n const expectedHookNames = new Set(\n Object.values(options.handlers).flatMap((handler) =>\n handler.hookNames ? Object.getOwnPropertyNames(handler.hookNames) : [],\n ),\n );\n assertExpectedHooks(allHooks, expectedHookNames);\n\n const handlers = Object.entries(options.handlers).reduce<\n Record<string, ResolvedHandler>\n >((accumulator, [handlerName, handler]) => {\n const handlerHooks = selectHooks(allHooks, handler.hookNames) ?? {};\n const handlerMessenger = createHandlerMessenger<\n HandlerActions<Handlers[keyof Handlers]>\n >({\n namespace: handlerName,\n actionNames: handler.actionNames as\n | readonly HandlerActions<Handlers[keyof Handlers]>['type'][]\n | undefined,\n rootMessenger,\n });\n\n accumulator[handlerName] = {\n implementation: handler.implementation,\n hooks: handlerHooks,\n messenger: handlerMessenger,\n };\n return accumulator;\n }, {});\n\n return ({ request, context, next }) => {\n const handler = handlers[request.method];\n if (handler === undefined) {\n return next();\n }\n\n const { implementation, hooks, messenger } = handler;\n\n return implementation({ request, context, next, hooks, messenger });\n };\n}\n"]}
1
+ {"version":3,"file":"createMethodMiddleware.mjs","sourceRoot":"","sources":["../../src/v2/createMethodMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,SAAS,EAAE,4BAA4B;AA8ElE;;;;;;;;GAQG;AACH,MAAM,UAAU,sBAAsB,CAIpC,OAAgD;IAEhD,MAAM,EAAE,SAAS,EAAE,aAAa,EAAE,GAAG,OAAO,CAAC;IAC7C,MAAM,QAAQ,GAAG,OAAO,CAAC,KAAgC,CAAC;IAE1D,MAAM,QAAQ,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,MAAM,CAEtD,CAAC,WAAW,EAAE,CAAC,WAAW,EAAE,OAAO,CAAC,EAAE,EAAE;QACxC,MAAM,YAAY,GAAG,WAAW,CAAC,QAAQ,EAAE,OAAO,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC;QACpE,MAAM,gBAAgB,GAAG,IAAI,SAAS,CAKpC;YACA,SAAS,EAAE,WAAW;YACtB,MAAM,EAAE,aAAa;SACtB,CAAC,CAAC;QAEH,aAAa,CAAC,QAAQ,CAAC;YACrB,OAAO,EAAE,CAAC,OAAO,CAAC,WAAW,IAAI,EAAE,CAExB;YACX,SAAS,EAAE,gBAAgB;SAC5B,CAAC,CAAC;QAEH,WAAW,CAAC,WAAW,CAAC,GAAG;YACzB,cAAc,EAAE,OAAO,CAAC,cAAc;YACtC,KAAK,EAAE,YAAY;YACnB,SAAS,EAAE,gBAAgB;SAC5B,CAAC;QACF,OAAO,WAAW,CAAC;IACrB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE;QACpC,MAAM,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACzC,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YAC1B,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,MAAM,EAAE,cAAc,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,OAAO,CAAC;QAErD,OAAO,cAAc,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC;IACtE,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,WAAW,CAIzB,KAAY,EACZ,SAAkC;IAElC,IAAI,SAAS,EAAE,CAAC;QACd,OAAO,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,MAAM,CAClC,CAAC,UAAU,EAAE,SAAS,EAAE,EAAE;YACxB,MAAM,QAAQ,GAAG,SAAqB,CAAC;YACvC,UAAU,CAAC,QAAQ,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC;YACvC,OAAO,UAAU,CAAC;QACpB,CAAC,EACD,EAAE,CACsB,CAAC;IAC7B,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["import { ActionConstraint, Messenger } from '@metamask/messenger';\n\nimport { JsonRpcMiddleware, Next } from './JsonRpcEngineV2';\nimport { ContextConstraint } from './MiddlewareContext';\nimport {\n Json,\n JsonRpcParams,\n JsonRpcRequest,\n UnionToIntersection,\n} from './utils';\n\n// The helpers below seem excessive, but they are required for inference of hooks/actions.\ntype HandlerActions<Handler> = Handler extends {\n implementation: (options: infer Options) => unknown;\n}\n ? Options extends { messenger: Messenger<string, infer Actions> }\n ? Actions\n : never\n : never;\n\ntype HandlerHooks<Handler> = Handler extends {\n implementation: (options: infer Options) => unknown;\n}\n ? Options extends { hooks: infer Hooks }\n ? Hooks\n : never\n : never;\n\nexport type MethodHandler<\n Hooks extends Record<string, unknown> = never,\n MessengerActions extends ActionConstraint = never,\n Parameters extends JsonRpcParams = JsonRpcParams,\n Result extends Json = Json,\n Context extends ContextConstraint = ContextConstraint,\n> = {\n implementation: (options: {\n request: Readonly<JsonRpcRequest<Parameters>>;\n context: Context;\n next: Next<JsonRpcRequest>;\n messenger: Messenger<string, MessengerActions>;\n hooks: Hooks;\n }) => Promise<Result> | Result;\n} & ([Hooks] extends [never]\n ? { hookNames?: undefined }\n : { hookNames: { [Key in keyof Hooks]: true } }) &\n ([MessengerActions] extends [never]\n ? { actionNames?: undefined }\n : { actionNames: MessengerActions['type'][] });\n\ntype AnyMethodHandler = {\n implementation(\n this: void,\n options: {\n request: Readonly<JsonRpcRequest>;\n context: ContextConstraint;\n next: Next<JsonRpcRequest>;\n messenger: unknown;\n hooks: unknown;\n },\n ): Promise<Json> | Json;\n hookNames?: Record<string, true>;\n actionNames?: readonly string[];\n};\n\nexport type CreateMethodMiddlewareOptions<\n Handlers extends Record<string, AnyMethodHandler>,\n> = {\n handlers: Handlers;\n messenger: Messenger<string, HandlerActions<Handlers[keyof Handlers]>>;\n hooks: UnionToIntersection<HandlerHooks<Handlers[keyof Handlers]>>;\n};\n\ntype ResolvedHandler = {\n implementation: AnyMethodHandler['implementation'];\n hooks: Record<string, unknown>;\n messenger: Messenger<string, ActionConstraint>;\n};\n\n/**\n * Create a JSON-RPC middleware that handles the passed JSON-RPC method handlers using the messenger and hooks.\n *\n * @param options The options.\n * @param options.handlers - The JSON-RPC method handler implementations.\n * @param options.messenger - The messenger to be used by the handlers.\n * @param options.hooks - The hooks to be used by the handlers.\n * @returns A JsonRpcEngineV2 middleware.\n */\nexport function createMethodMiddleware<\n Handlers extends Record<string, AnyMethodHandler>,\n Context extends ContextConstraint,\n>(\n options: CreateMethodMiddlewareOptions<Handlers>,\n): JsonRpcMiddleware<JsonRpcRequest, Json, Context> {\n const { messenger: rootMessenger } = options;\n const allHooks = options.hooks as Record<string, unknown>;\n\n const handlers = Object.entries(options.handlers).reduce<\n Record<string, ResolvedHandler>\n >((accumulator, [handlerName, handler]) => {\n const handlerHooks = selectHooks(allHooks, handler.hookNames) ?? {};\n const handlerMessenger = new Messenger<\n string,\n HandlerActions<Handlers[keyof Handlers]>,\n never,\n typeof rootMessenger\n >({\n namespace: handlerName,\n parent: rootMessenger,\n });\n\n rootMessenger.delegate({\n actions: (handler.actionNames ?? []) as HandlerActions<\n Handlers[keyof Handlers]\n >['type'][],\n messenger: handlerMessenger,\n });\n\n accumulator[handlerName] = {\n implementation: handler.implementation,\n hooks: handlerHooks,\n messenger: handlerMessenger,\n };\n return accumulator;\n }, {});\n\n return ({ request, context, next }) => {\n const handler = handlers[request.method];\n if (handler === undefined) {\n return next();\n }\n\n const { implementation, hooks, messenger } = handler;\n\n return implementation({ request, context, next, hooks, messenger });\n };\n}\n\n/**\n * Returns the subset of the specified `hooks` that are included in the\n * `hookNames` object. This is a Principle of Least Authority (POLA) measure\n * to ensure that each RPC method implementation only has access to the\n * API \"hooks\" it needs to do its job.\n *\n * @param hooks - The hooks to select from.\n * @param hookNames - The names of the hooks to select.\n * @returns The selected hooks.\n * @template Hooks - The hooks to select from.\n * @template HookName - The names of the hooks to select.\n */\nexport function selectHooks<\n Hooks extends Record<string, unknown>,\n HookName extends keyof Hooks,\n>(\n hooks: Hooks,\n hookNames?: Record<HookName, true>,\n): Pick<Hooks, HookName> | undefined {\n if (hookNames) {\n return Object.keys(hookNames).reduce<Partial<Pick<Hooks, HookName>>>(\n (hookSubset, _hookName) => {\n const hookName = _hookName as HookName;\n hookSubset[hookName] = hooks[hookName];\n return hookSubset;\n },\n {},\n ) as Pick<Hooks, HookName>;\n }\n return undefined;\n}\n"]}
package/dist/v2/index.cjs CHANGED
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.selectHooks = exports.JsonRpcEngineError = exports.isRequest = exports.isNotification = exports.MiddlewareContext = exports.JsonRpcServer = exports.JsonRpcEngineV2 = exports.createScaffoldMiddleware = exports.createOriginMiddleware = exports.createMethodMiddleware = exports.getUniqueId = exports.asLegacyMiddleware = void 0;
3
+ exports.JsonRpcEngineError = exports.isRequest = exports.isNotification = exports.MiddlewareContext = exports.JsonRpcServer = exports.JsonRpcEngineV2 = exports.createScaffoldMiddleware = exports.createOriginMiddleware = exports.createMethodMiddleware = exports.selectHooks = exports.getUniqueId = exports.asLegacyMiddleware = void 0;
4
4
  var asLegacyMiddleware_1 = require("./asLegacyMiddleware.cjs");
5
5
  Object.defineProperty(exports, "asLegacyMiddleware", { enumerable: true, get: function () { return asLegacyMiddleware_1.asLegacyMiddleware; } });
6
6
  var getUniqueId_1 = require("../getUniqueId.cjs");
7
7
  Object.defineProperty(exports, "getUniqueId", { enumerable: true, get: function () { return getUniqueId_1.getUniqueId; } });
8
8
  var createMethodMiddleware_1 = require("./createMethodMiddleware.cjs");
9
+ Object.defineProperty(exports, "selectHooks", { enumerable: true, get: function () { return createMethodMiddleware_1.selectHooks; } });
9
10
  Object.defineProperty(exports, "createMethodMiddleware", { enumerable: true, get: function () { return createMethodMiddleware_1.createMethodMiddleware; } });
10
11
  var createOriginMiddleware_1 = require("./createOriginMiddleware.cjs");
11
12
  Object.defineProperty(exports, "createOriginMiddleware", { enumerable: true, get: function () { return createOriginMiddleware_1.createOriginMiddleware; } });
@@ -21,5 +22,4 @@ var utils_1 = require("./utils.cjs");
21
22
  Object.defineProperty(exports, "isNotification", { enumerable: true, get: function () { return utils_1.isNotification; } });
22
23
  Object.defineProperty(exports, "isRequest", { enumerable: true, get: function () { return utils_1.isRequest; } });
23
24
  Object.defineProperty(exports, "JsonRpcEngineError", { enumerable: true, get: function () { return utils_1.JsonRpcEngineError; } });
24
- Object.defineProperty(exports, "selectHooks", { enumerable: true, get: function () { return utils_1.selectHooks; } });
25
25
  //# sourceMappingURL=index.cjs.map
@@ -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,uEAAkE;AAAzD,gIAAA,sBAAsB,OAAA;AAE/B,uEAAkE;AAAzD,gIAAA,sBAAsB,OAAA;AAC/B,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,qCAKiB;AAJf,uGAAA,cAAc,OAAA;AACd,kGAAA,SAAS,OAAA;AACT,2GAAA,kBAAkB,OAAA;AAClB,oGAAA,WAAW,OAAA","sourcesContent":["export { asLegacyMiddleware } from './asLegacyMiddleware';\nexport { getUniqueId } from '../getUniqueId';\nexport { createMethodMiddleware } from './createMethodMiddleware';\nexport type { MethodHandler } from './createMethodMiddleware';\nexport { createOriginMiddleware } from './createOriginMiddleware';\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 {\n isNotification,\n isRequest,\n JsonRpcEngineError,\n selectHooks,\n} 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,uEAA+E;AAAtE,qHAAA,WAAW,OAAA;AAAE,gIAAA,sBAAsB,OAAA;AAE5C,uEAAkE;AAAzD,gIAAA,sBAAsB,OAAA;AAC/B,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 { selectHooks, createMethodMiddleware } from './createMethodMiddleware';\nexport type { MethodHandler } from './createMethodMiddleware';\nexport { createOriginMiddleware } from './createOriginMiddleware';\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,6 +1,6 @@
1
1
  export { asLegacyMiddleware } from "./asLegacyMiddleware.cjs";
2
2
  export { getUniqueId } from "../getUniqueId.cjs";
3
- export { createMethodMiddleware } from "./createMethodMiddleware.cjs";
3
+ export { selectHooks, createMethodMiddleware } from "./createMethodMiddleware.cjs";
4
4
  export type { MethodHandler } from "./createMethodMiddleware.cjs";
5
5
  export { createOriginMiddleware } from "./createOriginMiddleware.cjs";
6
6
  export { createScaffoldMiddleware } from "./createScaffoldMiddleware.cjs";
@@ -9,6 +9,6 @@ export type { JsonRpcMiddleware, HandleOptions, MergedContextOf, MiddlewareParam
9
9
  export { JsonRpcServer } from "./JsonRpcServer.cjs";
10
10
  export { MiddlewareContext } from "./MiddlewareContext.cjs";
11
11
  export type { EmptyContext, ContextConstraint } from "./MiddlewareContext.cjs";
12
- export { isNotification, isRequest, JsonRpcEngineError, selectHooks, } from "./utils.cjs";
12
+ export { isNotification, isRequest, JsonRpcEngineError } from "./utils.cjs";
13
13
  export type { Json, JsonRpcCall, JsonRpcNotification, JsonRpcParams, JsonRpcRequest, } from "./utils.cjs";
14
14
  //# sourceMappingURL=index.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../src/v2/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,iCAA6B;AAC1D,OAAO,EAAE,WAAW,EAAE,2BAAuB;AAC7C,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAClE,YAAY,EAAE,aAAa,EAAE,qCAAiC;AAC9D,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAClE,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,EACL,cAAc,EACd,SAAS,EACT,kBAAkB,EAClB,WAAW,GACZ,oBAAgB;AACjB,YAAY,EACV,IAAI,EACJ,WAAW,EACX,mBAAmB,EACnB,aAAa,EACb,cAAc,GACf,oBAAgB"}
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../src/v2/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,iCAA6B;AAC1D,OAAO,EAAE,WAAW,EAAE,2BAAuB;AAC7C,OAAO,EAAE,WAAW,EAAE,sBAAsB,EAAE,qCAAiC;AAC/E,YAAY,EAAE,aAAa,EAAE,qCAAiC;AAC9D,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAClE,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,6 +1,6 @@
1
1
  export { asLegacyMiddleware } from "./asLegacyMiddleware.mjs";
2
2
  export { getUniqueId } from "../getUniqueId.mjs";
3
- export { createMethodMiddleware } from "./createMethodMiddleware.mjs";
3
+ export { selectHooks, createMethodMiddleware } from "./createMethodMiddleware.mjs";
4
4
  export type { MethodHandler } from "./createMethodMiddleware.mjs";
5
5
  export { createOriginMiddleware } from "./createOriginMiddleware.mjs";
6
6
  export { createScaffoldMiddleware } from "./createScaffoldMiddleware.mjs";
@@ -9,6 +9,6 @@ export type { JsonRpcMiddleware, HandleOptions, MergedContextOf, MiddlewareParam
9
9
  export { JsonRpcServer } from "./JsonRpcServer.mjs";
10
10
  export { MiddlewareContext } from "./MiddlewareContext.mjs";
11
11
  export type { EmptyContext, ContextConstraint } from "./MiddlewareContext.mjs";
12
- export { isNotification, isRequest, JsonRpcEngineError, selectHooks, } from "./utils.mjs";
12
+ export { isNotification, isRequest, JsonRpcEngineError } from "./utils.mjs";
13
13
  export type { Json, JsonRpcCall, JsonRpcNotification, JsonRpcParams, JsonRpcRequest, } from "./utils.mjs";
14
14
  //# sourceMappingURL=index.d.mts.map
@@ -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,sBAAsB,EAAE,qCAAiC;AAClE,YAAY,EAAE,aAAa,EAAE,qCAAiC;AAC9D,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAClE,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,EACL,cAAc,EACd,SAAS,EACT,kBAAkB,EAClB,WAAW,GACZ,oBAAgB;AACjB,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,WAAW,EAAE,sBAAsB,EAAE,qCAAiC;AAC/E,YAAY,EAAE,aAAa,EAAE,qCAAiC;AAC9D,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAClE,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"}
package/dist/v2/index.mjs CHANGED
@@ -1,10 +1,10 @@
1
1
  export { asLegacyMiddleware } from "./asLegacyMiddleware.mjs";
2
2
  export { getUniqueId } from "../getUniqueId.mjs";
3
- export { createMethodMiddleware } from "./createMethodMiddleware.mjs";
3
+ export { selectHooks, createMethodMiddleware } from "./createMethodMiddleware.mjs";
4
4
  export { createOriginMiddleware } from "./createOriginMiddleware.mjs";
5
5
  export { createScaffoldMiddleware } from "./createScaffoldMiddleware.mjs";
6
6
  export { JsonRpcEngineV2 } from "./JsonRpcEngineV2.mjs";
7
7
  export { JsonRpcServer } from "./JsonRpcServer.mjs";
8
8
  export { MiddlewareContext } from "./MiddlewareContext.mjs";
9
- export { isNotification, isRequest, JsonRpcEngineError, selectHooks } from "./utils.mjs";
9
+ export { isNotification, isRequest, JsonRpcEngineError } from "./utils.mjs";
10
10
  //# sourceMappingURL=index.mjs.map
@@ -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,sBAAsB,EAAE,qCAAiC;AAElE,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAClE,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,EACL,cAAc,EACd,SAAS,EACT,kBAAkB,EAClB,WAAW,EACZ,oBAAgB","sourcesContent":["export { asLegacyMiddleware } from './asLegacyMiddleware';\nexport { getUniqueId } from '../getUniqueId';\nexport { createMethodMiddleware } from './createMethodMiddleware';\nexport type { MethodHandler } from './createMethodMiddleware';\nexport { createOriginMiddleware } from './createOriginMiddleware';\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 {\n isNotification,\n isRequest,\n JsonRpcEngineError,\n selectHooks,\n} 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,WAAW,EAAE,sBAAsB,EAAE,qCAAiC;AAE/E,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAClE,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 { selectHooks, createMethodMiddleware } from './createMethodMiddleware';\nexport type { MethodHandler } from './createMethodMiddleware';\nexport { createOriginMiddleware } from './createOriginMiddleware';\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"]}
package/dist/v2/utils.cjs CHANGED
@@ -1,8 +1,7 @@
1
1
  "use strict";
2
2
  var _a;
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.createHandlerMessenger = exports.assertExpectedHooks = exports.selectHooks = exports.JsonRpcEngineError = exports.isInstance = exports.stringify = exports.isNotification = exports.isRequest = void 0;
5
- const messenger_1 = require("@metamask/messenger");
4
+ exports.JsonRpcEngineError = exports.isInstance = exports.stringify = exports.isNotification = exports.isRequest = void 0;
6
5
  const utils_1 = require("@metamask/utils");
7
6
  const isRequest = (message) => (0, utils_1.hasProperty)(message, 'id');
8
7
  exports.isRequest = isRequest;
@@ -50,74 +49,4 @@ class JsonRpcEngineError extends Error {
50
49
  }
51
50
  exports.JsonRpcEngineError = JsonRpcEngineError;
52
51
  _a = JsonRpcEngineErrorSymbol;
53
- // Method middleware utils
54
- /**
55
- * Returns the subset of the specified `hooks` that are included in the
56
- * `hookNames` object. This is a Principle of Least Authority (POLA) measure
57
- * to ensure that each RPC method implementation only has access to the
58
- * API "hooks" it needs to do its job.
59
- *
60
- * @param hooks - The hooks to select from.
61
- * @param hookNames - The names of the hooks to select.
62
- * @returns The selected hooks, or `undefined` if `hookNames` is not provided.
63
- * @template Hooks - The hooks to select from.
64
- * @template HookName - The names of the hooks to select.
65
- */
66
- function selectHooks(hooks, hookNames) {
67
- if (hookNames) {
68
- return Object.keys(hookNames).reduce((subset, name) => {
69
- const hookName = name;
70
- subset[hookName] = hooks[hookName];
71
- return subset;
72
- }, {});
73
- }
74
- return undefined;
75
- }
76
- exports.selectHooks = selectHooks;
77
- /**
78
- * Asserts that `hooks` contains exactly the hook names in `expectedHookNames`.
79
- * Throws on any missing hooks, then on any extraneous hooks.
80
- *
81
- * @param hooks - The hooks object to validate.
82
- * @param expectedHookNames - The expected hook names.
83
- */
84
- function assertExpectedHooks(hooks, expectedHookNames) {
85
- const missingHookNames = [];
86
- expectedHookNames.forEach((hookName) => {
87
- if (!(0, utils_1.hasProperty)(hooks, hookName)) {
88
- missingHookNames.push(hookName);
89
- }
90
- });
91
- if (missingHookNames.length > 0) {
92
- throw new Error(`Missing expected hooks:\n\n${missingHookNames.join('\n')}\n`);
93
- }
94
- const extraneousHookNames = Object.getOwnPropertyNames(hooks).filter((hookName) => !expectedHookNames.has(hookName));
95
- if (extraneousHookNames.length > 0) {
96
- throw new Error(`Received unexpected hooks:\n\n${extraneousHookNames.join('\n')}\n`);
97
- }
98
- }
99
- exports.assertExpectedHooks = assertExpectedHooks;
100
- /**
101
- * Creates a per-handler messenger namespaced to `namespace`, and delegates the
102
- * specified `actionNames` from `rootMessenger` to it. This lets each handler
103
- * call only the actions it declared, per POLA.
104
- *
105
- * @param options - The options.
106
- * @param options.namespace - The namespace for the handler messenger.
107
- * @param options.actionNames - Actions to delegate from the root messenger.
108
- * @param options.rootMessenger - The root messenger to delegate from.
109
- * @returns The per-handler messenger.
110
- */
111
- function createHandlerMessenger({ namespace, actionNames, rootMessenger, }) {
112
- const handlerMessenger = new messenger_1.Messenger({
113
- namespace,
114
- parent: rootMessenger,
115
- });
116
- rootMessenger.delegate({
117
- actions: (actionNames ?? []),
118
- messenger: handlerMessenger,
119
- });
120
- return handlerMessenger;
121
- }
122
- exports.createHandlerMessenger = createHandlerMessenger;
123
52
  //# sourceMappingURL=utils.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../../src/v2/utils.ts"],"names":[],"mappings":";;;;AACA,mDAAgD;AAChD,2CAAwD;AAkBjD,MAAM,SAAS,GAAG,CACvB,OAA4D,EACzB,EAAE,CAAC,IAAA,mBAAW,EAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AAFtD,QAAA,SAAS,aAE6C;AAE5D,MAAM,cAAc,GAAG,CAC5B,OAA4B,EACY,EAAE,CAAC,CAAC,IAAA,iBAAS,EAAC,OAAO,CAAC,CAAC;AAFpD,QAAA,cAAc,kBAEsC;AAgBjE;;;;;GAKG;AACH,SAAgB,SAAS,CAAC,KAAc;IACtC,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;AACxC,CAAC;AAFD,8BAEC;AAED;;;;;;GAMG;AACI,MAAM,UAAU,GAAG,CACxB,KAAc,EACd,MAAc,EACoB,EAAE,CACpC,IAAA,gBAAQ,EAAC,KAAK,CAAC,IAAI,MAAM,IAAI,KAAK,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC;AAJlD,QAAA,UAAU,cAIwC;AAE/D,MAAM,wBAAwB,GAAG,MAAM,CAAC,GAAG,CACzC,oCAAoC,CACrC,CAAC;AAEF,MAAa,kBAAmB,SAAQ,KAAK;IAM3C,YAAY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AAI,CAAC,IAAI,CAC9D,CAAC;IACJ,CAAC;IAED,MAAM,mBAAmB,GAAG,MAAM,CAAC,mBAAmB,CAAC,KAAK,CAAC,CAAC,MAAM,CAClE,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,iBAAiB,CAAC,GAAG,CAAC,QAAQ,CAAC,CAC/C,CAAC;IACF,IAAI,mBAAmB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACnC,MAAM,IAAI,KAAK,CACb,iCAAiC,mBAAmB,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CACpE,CAAC;IACJ,CAAC;AACH,CAAC;AAxBD,kDAwBC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,sBAAsB,CAAmC,EACvE,SAAS,EACT,WAAW,EACX,aAAa,GAKd;IACC,MAAM,gBAAgB,GAAG,IAAI,qBAAS,CAKpC;QACA,SAAS;QACT,MAAM,EAAE,aAAa;KACtB,CAAC,CAAC;IAEH,aAAa,CAAC,QAAQ,CAAC;QACrB,OAAO,EAAE,CAAC,WAAW,IAAI,EAAE,CAAsB;QACjD,SAAS,EAAE,gBAAgB;KAC5B,CAAC,CAAC;IAEH,OAAO,gBAAgB,CAAC;AAC1B,CAAC;AAzBD,wDAyBC","sourcesContent":["import type { ActionConstraint } from '@metamask/messenger';\nimport { Messenger } from '@metamask/messenger';\nimport { hasProperty, isObject } from '@metamask/utils';\nimport type {\n JsonRpcNotification,\n JsonRpcParams,\n 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 message: JsonRpcCall<Params> | Readonly<JsonRpcCall<Params>>,\n): message is JsonRpcRequest<Params> => hasProperty(message, 'id');\n\nexport const isNotification = <Params extends JsonRpcParams>(\n message: JsonRpcCall<Params>,\n): message is JsonRpcNotification<Params> => !isRequest(message);\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<Union> = (\n Union extends never ? never : (k: Union) => void\n) extends (k: infer Args) => void\n ? Args\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\n/**\n * The implementation of static `isInstance` methods for classes that have them.\n *\n * @param value - The value to check.\n * @param symbol - The symbol property to check for.\n * @returns Whether the value has `{ [symbol]: true }` in its prototype chain.\n */\nexport const isInstance = (\n value: unknown,\n symbol: symbol,\n): value is { [key: symbol]: true } =>\n isObject(value) && symbol in value && value[symbol] === true;\n\nconst JsonRpcEngineErrorSymbol = Symbol.for(\n 'json-rpc-engine#JsonRpcEngineError',\n);\n\nexport class JsonRpcEngineError extends Error {\n // This is a computed property name, and it doesn't seem possible to make it\n // hash private using `#`.\n // eslint-disable-next-line no-restricted-syntax\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: unknown): value is JsonRpcEngineError {\n return isInstance(value, JsonRpcEngineErrorSymbol);\n }\n}\n\n// Method middleware utils\n\n/**\n * Returns the subset of the specified `hooks` that are included in the\n * `hookNames` object. This is a Principle of Least Authority (POLA) measure\n * to ensure that each RPC method implementation only has access to the\n * API \"hooks\" it needs to do its job.\n *\n * @param hooks - The hooks to select from.\n * @param hookNames - The names of the hooks to select.\n * @returns The selected hooks, or `undefined` if `hookNames` is not provided.\n * @template Hooks - The hooks to select from.\n * @template HookName - The names of the hooks to select.\n */\nexport function selectHooks<Hooks, HookName extends keyof Hooks>(\n hooks: Hooks,\n hookNames?: Record<HookName, true>,\n): Pick<Hooks, HookName> | undefined {\n if (hookNames) {\n return Object.keys(hookNames).reduce<Partial<Pick<Hooks, HookName>>>(\n (subset, name) => {\n const hookName = name as HookName;\n subset[hookName] = hooks[hookName];\n return subset;\n },\n {},\n ) as Pick<Hooks, HookName>;\n }\n return undefined;\n}\n\n/**\n * Asserts that `hooks` contains exactly the hook names in `expectedHookNames`.\n * Throws on any missing hooks, then on any extraneous hooks.\n *\n * @param hooks - The hooks object to validate.\n * @param expectedHookNames - The expected hook names.\n */\nexport function assertExpectedHooks(\n hooks: Record<string, unknown>,\n expectedHookNames: Set<string>,\n): void {\n const missingHookNames: string[] = [];\n expectedHookNames.forEach((hookName) => {\n if (!hasProperty(hooks, hookName)) {\n missingHookNames.push(hookName);\n }\n });\n if (missingHookNames.length > 0) {\n throw new Error(\n `Missing expected hooks:\\n\\n${missingHookNames.join('\\n')}\\n`,\n );\n }\n\n const extraneousHookNames = Object.getOwnPropertyNames(hooks).filter(\n (hookName) => !expectedHookNames.has(hookName),\n );\n if (extraneousHookNames.length > 0) {\n throw new Error(\n `Received unexpected hooks:\\n\\n${extraneousHookNames.join('\\n')}\\n`,\n );\n }\n}\n\n/**\n * Creates a per-handler messenger namespaced to `namespace`, and delegates the\n * specified `actionNames` from `rootMessenger` to it. This lets each handler\n * call only the actions it declared, per POLA.\n *\n * @param options - The options.\n * @param options.namespace - The namespace for the handler messenger.\n * @param options.actionNames - Actions to delegate from the root messenger.\n * @param options.rootMessenger - The root messenger to delegate from.\n * @returns The per-handler messenger.\n */\nexport function createHandlerMessenger<Actions extends ActionConstraint>({\n namespace,\n actionNames,\n rootMessenger,\n}: {\n namespace: string;\n actionNames: readonly Actions['type'][] | undefined;\n rootMessenger: Messenger<string, Actions>;\n}): Messenger<string, Actions> {\n const handlerMessenger = new Messenger<\n string,\n Actions,\n never,\n typeof rootMessenger\n >({\n namespace,\n parent: rootMessenger,\n });\n\n rootMessenger.delegate({\n actions: (actionNames ?? []) as Actions['type'][],\n messenger: handlerMessenger,\n });\n\n return handlerMessenger;\n}\n"]}
1
+ {"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../../src/v2/utils.ts"],"names":[],"mappings":";;;;AAAA,2CAAwD;AAkBjD,MAAM,SAAS,GAAG,CACvB,OAA4D,EACzB,EAAE,CAAC,IAAA,mBAAW,EAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AAFtD,QAAA,SAAS,aAE6C;AAE5D,MAAM,cAAc,GAAG,CAC5B,OAA4B,EACY,EAAE,CAAC,CAAC,IAAA,iBAAS,EAAC,OAAO,CAAC,CAAC;AAFpD,QAAA,cAAc,kBAEsC;AAgBjE;;;;;GAKG;AACH,SAAgB,SAAS,CAAC,KAAc;IACtC,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;AACxC,CAAC;AAFD,8BAEC;AAED;;;;;;GAMG;AACI,MAAM,UAAU,GAAG,CACxB,KAAc,EACd,MAAc,EACoB,EAAE,CACpC,IAAA,gBAAQ,EAAC,KAAK,CAAC,IAAI,MAAM,IAAI,KAAK,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC;AAJlD,QAAA,UAAU,cAIwC;AAE/D,MAAM,wBAAwB,GAAG,MAAM,CAAC,GAAG,CACzC,oCAAoC,CACrC,CAAC;AAEF,MAAa,kBAAmB,SAAQ,KAAK;IAM3C,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QANjB,4EAA4E;QAC5E,0BAA0B;QAC1B,gDAAgD;QAC/B,QAA0B,GAAG,IAAI,CAAC;QAIjD,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IACnC,CAAC;IAED;;;;;;OAMG;IACH,MAAM,CAAC,UAAU,CAAC,KAAc;QAC9B,OAAO,IAAA,kBAAU,EAAC,KAAK,EAAE,wBAAwB,CAAC,CAAC;IACrD,CAAC;CACF;AArBD,gDAqBC;KAjBmB,wBAAwB","sourcesContent":["import { hasProperty, isObject } from '@metamask/utils';\nimport type {\n JsonRpcNotification,\n JsonRpcParams,\n 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 message: JsonRpcCall<Params> | Readonly<JsonRpcCall<Params>>,\n): message is JsonRpcRequest<Params> => hasProperty(message, 'id');\n\nexport const isNotification = <Params extends JsonRpcParams>(\n message: JsonRpcCall<Params>,\n): message is JsonRpcNotification<Params> => !isRequest(message);\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<Union> = (\n Union extends never ? never : (k: Union) => void\n) extends (k: infer Args) => void\n ? Args\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\n/**\n * The implementation of static `isInstance` methods for classes that have them.\n *\n * @param value - The value to check.\n * @param symbol - The symbol property to check for.\n * @returns Whether the value has `{ [symbol]: true }` in its prototype chain.\n */\nexport const isInstance = (\n value: unknown,\n symbol: symbol,\n): value is { [key: symbol]: true } =>\n isObject(value) && symbol in value && value[symbol] === true;\n\nconst JsonRpcEngineErrorSymbol = Symbol.for(\n 'json-rpc-engine#JsonRpcEngineError',\n);\n\nexport class JsonRpcEngineError extends Error {\n // This is a computed property name, and it doesn't seem possible to make it\n // hash private using `#`.\n // eslint-disable-next-line no-restricted-syntax\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: unknown): value is JsonRpcEngineError {\n return isInstance(value, JsonRpcEngineErrorSymbol);\n }\n}\n"]}
@@ -1,5 +1,3 @@
1
- import type { ActionConstraint } from "@metamask/messenger";
2
- import { Messenger } from "@metamask/messenger";
3
1
  import type { JsonRpcNotification, JsonRpcParams, JsonRpcRequest } from "@metamask/utils";
4
2
  export type { Json, JsonRpcParams, JsonRpcRequest, JsonRpcNotification, } from "@metamask/utils";
5
3
  export type JsonRpcCall<Params extends JsonRpcParams = JsonRpcParams> = JsonRpcNotification<Params> | JsonRpcRequest<Params>;
@@ -44,41 +42,4 @@ export declare class JsonRpcEngineError extends Error {
44
42
  */
45
43
  static isInstance(value: unknown): value is JsonRpcEngineError;
46
44
  }
47
- /**
48
- * Returns the subset of the specified `hooks` that are included in the
49
- * `hookNames` object. This is a Principle of Least Authority (POLA) measure
50
- * to ensure that each RPC method implementation only has access to the
51
- * API "hooks" it needs to do its job.
52
- *
53
- * @param hooks - The hooks to select from.
54
- * @param hookNames - The names of the hooks to select.
55
- * @returns The selected hooks, or `undefined` if `hookNames` is not provided.
56
- * @template Hooks - The hooks to select from.
57
- * @template HookName - The names of the hooks to select.
58
- */
59
- export declare function selectHooks<Hooks, HookName extends keyof Hooks>(hooks: Hooks, hookNames?: Record<HookName, true>): Pick<Hooks, HookName> | undefined;
60
- /**
61
- * Asserts that `hooks` contains exactly the hook names in `expectedHookNames`.
62
- * Throws on any missing hooks, then on any extraneous hooks.
63
- *
64
- * @param hooks - The hooks object to validate.
65
- * @param expectedHookNames - The expected hook names.
66
- */
67
- export declare function assertExpectedHooks(hooks: Record<string, unknown>, expectedHookNames: Set<string>): void;
68
- /**
69
- * Creates a per-handler messenger namespaced to `namespace`, and delegates the
70
- * specified `actionNames` from `rootMessenger` to it. This lets each handler
71
- * call only the actions it declared, per POLA.
72
- *
73
- * @param options - The options.
74
- * @param options.namespace - The namespace for the handler messenger.
75
- * @param options.actionNames - Actions to delegate from the root messenger.
76
- * @param options.rootMessenger - The root messenger to delegate from.
77
- * @returns The per-handler messenger.
78
- */
79
- export declare function createHandlerMessenger<Actions extends ActionConstraint>({ namespace, actionNames, rootMessenger, }: {
80
- namespace: string;
81
- actionNames: readonly Actions['type'][] | undefined;
82
- rootMessenger: Messenger<string, Actions>;
83
- }): Messenger<string, Actions>;
84
45
  //# sourceMappingURL=utils.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../../src/v2/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,4BAA4B;AAC5D,OAAO,EAAE,SAAS,EAAE,4BAA4B;AAEhD,OAAO,KAAK,EACV,mBAAmB,EACnB,aAAa,EACb,cAAc,EACf,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,CAAC,MAAM,CAAC,CAAC;AAE3B,eAAO,MAAM,SAAS,mIAE4C,CAAC;AAEnE,eAAO,MAAM,cAAc,wGAEqC,CAAC;AAEjE;;;;;;;GAOG;AACH,MAAM,MAAM,mBAAmB,CAAC,KAAK,IAAI,CACvC,KAAK,SAAS,KAAK,GAAG,KAAK,GAAG,CAAC,CAAC,EAAE,KAAK,KAAK,IAAI,CACjD,SAAS,CAAC,CAAC,EAAE,MAAM,IAAI,KAAK,IAAI,GAC7B,IAAI,GACJ,KAAK,CAAC;AAEV;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAEhD;A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I,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,CAAC,MAAM,CAAC,CAAC;AAE3B,eAAO,MAAM,SAAS,mIAE4C,CAAC;AAEnE,eAAO,MAAM,cAAc,wGAEqC,CAAC;AAEjE;;;;;;;GAOG;AACH,MAAM,MAAM,mBAAmB,CAAC,KAAK,IAAI,CACvC,KAAK,SAAS,KAAK,GAAG,KAAK,GAAG,CAAC,CAAC,EAAE,KAAK,KAAK,IAAI,CACjD,SAAS,CAAC,CAAC,EAAE,MAAM,IAAI,KAAK,IAAI,GAC7B,IAAI,GACJ,KAAK,CAAC;AAEV;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAEhD;AAED;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,UACd,OAAO,UACN,MAAM;;CAE8C,CAAC;AAE/D,QAAA,MAAM,wBAAwB,eAE7B,CAAC;AAEF,qBAAa,kBAAmB,SAAQ,KAAK;IAI3C,OAAO,CAAC,QAAQ,CAAC,CAAC,wBAAwB,CAAC,CAAQ;gBAEvC,OAAO,EAAE,MAAM;IAK3B;;;;;;OAMG;IACH,MAAM,CAAC,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,kBAAkB;CAG/D"}
@@ -1,5 +1,3 @@
1
- import type { ActionConstraint } from "@metamask/messenger";
2
- import { Messenger } from "@metamask/messenger";
3
1
  import type { JsonRpcNotification, JsonRpcParams, JsonRpcRequest } from "@metamask/utils";
4
2
  export type { Json, JsonRpcParams, JsonRpcRequest, JsonRpcNotification, } from "@metamask/utils";
5
3
  export type JsonRpcCall<Params extends JsonRpcParams = JsonRpcParams> = JsonRpcNotification<Params> | JsonRpcRequest<Params>;
@@ -44,41 +42,4 @@ export declare class JsonRpcEngineError extends Error {
44
42
  */
45
43
  static isInstance(value: unknown): value is JsonRpcEngineError;
46
44
  }
47
- /**
48
- * Returns the subset of the specified `hooks` that are included in the
49
- * `hookNames` object. This is a Principle of Least Authority (POLA) measure
50
- * to ensure that each RPC method implementation only has access to the
51
- * API "hooks" it needs to do its job.
52
- *
53
- * @param hooks - The hooks to select from.
54
- * @param hookNames - The names of the hooks to select.
55
- * @returns The selected hooks, or `undefined` if `hookNames` is not provided.
56
- * @template Hooks - The hooks to select from.
57
- * @template HookName - The names of the hooks to select.
58
- */
59
- export declare function selectHooks<Hooks, HookName extends keyof Hooks>(hooks: Hooks, hookNames?: Record<HookName, true>): Pick<Hooks, HookName> | undefined;
60
- /**
61
- * Asserts that `hooks` contains exactly the hook names in `expectedHookNames`.
62
- * Throws on any missing hooks, then on any extraneous hooks.
63
- *
64
- * @param hooks - The hooks object to validate.
65
- * @param expectedHookNames - The expected hook names.
66
- */
67
- export declare function assertExpectedHooks(hooks: Record<string, unknown>, expectedHookNames: Set<string>): void;
68
- /**
69
- * Creates a per-handler messenger namespaced to `namespace`, and delegates the
70
- * specified `actionNames` from `rootMessenger` to it. This lets each handler
71
- * call only the actions it declared, per POLA.
72
- *
73
- * @param options - The options.
74
- * @param options.namespace - The namespace for the handler messenger.
75
- * @param options.actionNames - Actions to delegate from the root messenger.
76
- * @param options.rootMessenger - The root messenger to delegate from.
77
- * @returns The per-handler messenger.
78
- */
79
- export declare function createHandlerMessenger<Actions extends ActionConstraint>({ namespace, actionNames, rootMessenger, }: {
80
- namespace: string;
81
- actionNames: readonly Actions['type'][] | undefined;
82
- rootMessenger: Messenger<string, Actions>;
83
- }): Messenger<string, Actions>;
84
45
  //# sourceMappingURL=utils.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../../src/v2/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,4BAA4B;AAC5D,OAAO,EAAE,SAAS,EAAE,4BAA4B;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AAED;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,UACd,OAAO,UACN,MAAM;;CAE8C,CAAC;AAE/D,QAAA,MAAM,wBAAwB,eAE7B,CAAC;AAEF,qBAAa,kBAAmB,SAAQ,KAAK;IAI3C,OAAO,CAAC,QAAQ,CAAC,CAAC,wBAAwB,CAAC,CAAQ;gBAEvC,OAAO,EAAE,MAAM;IAK3B;;;;;;OAMG;IACH,MAAM,CAAC,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,kBAAkB;CAG/D"}
package/dist/v2/utils.mjs CHANGED
@@ -1,5 +1,4 @@
1
1
  var _a;
2
- import { Messenger } from "@metamask/messenger";
3
2
  import { hasProperty, isObject } from "@metamask/utils";
4
3
  export const isRequest = (message) => hasProperty(message, 'id');
5
4
  export const isNotification = (message) => !isRequest(message);
@@ -42,71 +41,4 @@ export class JsonRpcEngineError extends Error {
42
41
  }
43
42
  }
44
43
  _a = JsonRpcEngineErrorSymbol;
45
- // Method middleware utils
46
- /**
47
- * Returns the subset of the specified `hooks` that are included in the
48
- * `hookNames` object. This is a Principle of Least Authority (POLA) measure
49
- * to ensure that each RPC method implementation only has access to the
50
- * API "hooks" it needs to do its job.
51
- *
52
- * @param hooks - The hooks to select from.
53
- * @param hookNames - The names of the hooks to select.
54
- * @returns The selected hooks, or `undefined` if `hookNames` is not provided.
55
- * @template Hooks - The hooks to select from.
56
- * @template HookName - The names of the hooks to select.
57
- */
58
- export function selectHooks(hooks, hookNames) {
59
- if (hookNames) {
60
- return Object.keys(hookNames).reduce((subset, name) => {
61
- const hookName = name;
62
- subset[hookName] = hooks[hookName];
63
- return subset;
64
- }, {});
65
- }
66
- return undefined;
67
- }
68
- /**
69
- * Asserts that `hooks` contains exactly the hook names in `expectedHookNames`.
70
- * Throws on any missing hooks, then on any extraneous hooks.
71
- *
72
- * @param hooks - The hooks object to validate.
73
- * @param expectedHookNames - The expected hook names.
74
- */
75
- export function assertExpectedHooks(hooks, expectedHookNames) {
76
- const missingHookNames = [];
77
- expectedHookNames.forEach((hookName) => {
78
- if (!hasProperty(hooks, hookName)) {
79
- missingHookNames.push(hookName);
80
- }
81
- });
82
- if (missingHookNames.length > 0) {
83
- throw new Error(`Missing expected hooks:\n\n${missingHookNames.join('\n')}\n`);
84
- }
85
- const extraneousHookNames = Object.getOwnPropertyNames(hooks).filter((hookName) => !expectedHookNames.has(hookName));
86
- if (extraneousHookNames.length > 0) {
87
- throw new Error(`Received unexpected hooks:\n\n${extraneousHookNames.join('\n')}\n`);
88
- }
89
- }
90
- /**
91
- * Creates a per-handler messenger namespaced to `namespace`, and delegates the
92
- * specified `actionNames` from `rootMessenger` to it. This lets each handler
93
- * call only the actions it declared, per POLA.
94
- *
95
- * @param options - The options.
96
- * @param options.namespace - The namespace for the handler messenger.
97
- * @param options.actionNames - Actions to delegate from the root messenger.
98
- * @param options.rootMessenger - The root messenger to delegate from.
99
- * @returns The per-handler messenger.
100
- */
101
- export function createHandlerMessenger({ namespace, actionNames, rootMessenger, }) {
102
- const handlerMessenger = new Messenger({
103
- namespace,
104
- parent: rootMessenger,
105
- });
106
- rootMessenger.delegate({
107
- actions: (actionNames ?? []),
108
- messenger: handlerMessenger,
109
- });
110
- return handlerMessenger;
111
- }
112
44
  //# sourceMappingURL=utils.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../../src/v2/utils.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,SAAS,EAAE,4BAA4B;AAChD,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,wBAAwB;AAkBxD,MAAM,CAAC,MAAM,SAAS,GAAG,CACvB,OAA4D,EACzB,EAAE,CAAC,WAAW,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AAEnE,MAAM,CAAC,MAAM,cAAc,GAAG,CAC5B,OAA4B,EACY,EAAE,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;AAgBjE;;;;;GAKG;AACH,MAAM,UAAU,S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,EAC9B,iBAA8B;IAE9B,MAAM,gBAAgB,GAAa,EAAE,CAAC;IACtC,iBAAiB,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;QACrC,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,QAAQ,CAAC,EAAE,CAAC;YAClC,gBAAgB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QAClC,CAAC;IACH,CAAC,CAAC,CAAC;IACH,IAAI,gBAAgB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAChC,MAAM,IAAI,KAAK,CACb,8BAA8B,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAC9D,CAAC;IACJ,CAAC;IAED,MAAM,mBAAmB,GAAG,MAAM,CAAC,mBAAmB,CAAC,KAAK,CAAC,CAAC,MAAM,CAClE,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,iBAAiB,CAAC,GAAG,CAAC,QAAQ,CAAC,CAC/C,CAAC;IACF,IAAI,mBAAmB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACnC,MAAM,IAAI,KAAK,CACb,iCAAiC,mBAAmB,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CACpE,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,sBAAsB,CAAmC,EACvE,SAAS,EACT,WAAW,EACX,aAAa,GAKd;IACC,MAAM,gBAAgB,GAAG,IAAI,SAAS,CAKpC;QACA,SAAS;QACT,MAAM,EAAE,aAAa;KACtB,CAAC,CAAC;IAEH,aAAa,CAAC,QAAQ,CAAC;QACrB,OAAO,EAAE,CAAC,WAAW,IAAI,EAAE,CAAsB;QACjD,SAAS,EAAE,gBAAgB;KAC5B,CAAC,CAAC;IAEH,OAAO,gBAAgB,CAAC;AAC1B,CAAC","sourcesContent":["import type { ActionConstraint } from '@metamask/messenger';\nimport { Messenger } from '@metamask/messenger';\nimport { hasProperty, isObject } from '@metamask/utils';\nimport type {\n JsonRpcNotification,\n JsonRpcParams,\n 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 message: JsonRpcCall<Params> | Readonly<JsonRpcCall<Params>>,\n): message is JsonRpcRequest<Params> => hasProperty(message, 'id');\n\nexport const isNotification = <Params extends JsonRpcParams>(\n message: JsonRpcCall<Params>,\n): message is JsonRpcNotification<Params> => !isRequest(message);\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<Union> = (\n Union extends never ? never : (k: Union) => void\n) extends (k: infer Args) => void\n ? Args\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\n/**\n * The implementation of static `isInstance` methods for classes that have them.\n *\n * @param value - The value to check.\n * @param symbol - The symbol property to check for.\n * @returns Whether the value has `{ [symbol]: true }` in its prototype chain.\n */\nexport const isInstance = (\n value: unknown,\n symbol: symbol,\n): value is { [key: symbol]: true } =>\n isObject(value) && symbol in value && value[symbol] === true;\n\nconst JsonRpcEngineErrorSymbol = Symbol.for(\n 'json-rpc-engine#JsonRpcEngineError',\n);\n\nexport class JsonRpcEngineError extends Error {\n // This is a computed property name, and it doesn't seem possible to make it\n // hash private using `#`.\n // eslint-disable-next-line no-restricted-syntax\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: unknown): value is JsonRpcEngineError {\n return isInstance(value, JsonRpcEngineErrorSymbol);\n }\n}\n\n// Method middleware utils\n\n/**\n * Returns the subset of the specified `hooks` that are included in the\n * `hookNames` object. This is a Principle of Least Authority (POLA) measure\n * to ensure that each RPC method implementation only has access to the\n * API \"hooks\" it needs to do its job.\n *\n * @param hooks - The hooks to select from.\n * @param hookNames - The names of the hooks to select.\n * @returns The selected hooks, or `undefined` if `hookNames` is not provided.\n * @template Hooks - The hooks to select from.\n * @template HookName - The names of the hooks to select.\n */\nexport function selectHooks<Hooks, HookName extends keyof Hooks>(\n hooks: Hooks,\n hookNames?: Record<HookName, true>,\n): Pick<Hooks, HookName> | undefined {\n if (hookNames) {\n return Object.keys(hookNames).reduce<Partial<Pick<Hooks, HookName>>>(\n (subset, name) => {\n const hookName = name as HookName;\n subset[hookName] = hooks[hookName];\n return subset;\n },\n {},\n ) as Pick<Hooks, HookName>;\n }\n return undefined;\n}\n\n/**\n * Asserts that `hooks` contains exactly the hook names in `expectedHookNames`.\n * Throws on any missing hooks, then on any extraneous hooks.\n *\n * @param hooks - The hooks object to validate.\n * @param expectedHookNames - The expected hook names.\n */\nexport function assertExpectedHooks(\n hooks: Record<string, unknown>,\n expectedHookNames: Set<string>,\n): void {\n const missingHookNames: string[] = [];\n expectedHookNames.forEach((hookName) => {\n if (!hasProperty(hooks, hookName)) {\n missingHookNames.push(hookName);\n }\n });\n if (missingHookNames.length > 0) {\n throw new Error(\n `Missing expected hooks:\\n\\n${missingHookNames.join('\\n')}\\n`,\n );\n }\n\n const extraneousHookNames = Object.getOwnPropertyNames(hooks).filter(\n (hookName) => !expectedHookNames.has(hookName),\n );\n if (extraneousHookNames.length > 0) {\n throw new Error(\n `Received unexpected hooks:\\n\\n${extraneousHookNames.join('\\n')}\\n`,\n );\n }\n}\n\n/**\n * Creates a per-handler messenger namespaced to `namespace`, and delegates the\n * specified `actionNames` from `rootMessenger` to it. This lets each handler\n * call only the actions it declared, per POLA.\n *\n * @param options - The options.\n * @param options.namespace - The namespace for the handler messenger.\n * @param options.actionNames - Actions to delegate from the root messenger.\n * @param options.rootMessenger - The root messenger to delegate from.\n * @returns The per-handler messenger.\n */\nexport function createHandlerMessenger<Actions extends ActionConstraint>({\n namespace,\n actionNames,\n rootMessenger,\n}: {\n namespace: string;\n actionNames: readonly Actions['type'][] | undefined;\n rootMessenger: Messenger<string, Actions>;\n}): Messenger<string, Actions> {\n const handlerMessenger = new Messenger<\n string,\n Actions,\n never,\n typeof rootMessenger\n >({\n namespace,\n parent: rootMessenger,\n });\n\n rootMessenger.delegate({\n actions: (actionNames ?? []) as Actions['type'][],\n messenger: handlerMessenger,\n });\n\n return handlerMessenger;\n}\n"]}
1
+ {"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../../src/v2/utils.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,wBAAwB;AAkBxD,MAAM,CAAC,MAAM,SAAS,GAAG,CACvB,OAA4D,EACzB,EAAE,CAAC,WAAW,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AAEnE,MAAM,CAAC,MAAM,cAAc,GAAG,CAC5B,OAA4B,EACY,EAAE,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;AAgBjE;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CAAC,KAAc;IACtC,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;AACxC,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CACxB,KAAc,EACd,MAAc,EACoB,EAAE,CACpC,QAAQ,CAAC,KAAK,CAAC,IAAI,MAAM,IAAI,KAAK,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC;AAE/D,MAAM,wBAAwB,GAAG,MAAM,CAAC,GAAG,CACzC,oCAAoC,CACrC,CAAC;AAEF,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAM3C,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QANjB,4EAA4E;QAC5E,0BAA0B;QAC1B,gDAAgD;QAC/B,QAA0B,GAAG,IAAI,CAAC;QAIjD,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IACnC,CAAC;IAED;;;;;;OAMG;IACH,MAAM,CAAC,UAAU,CAAC,KAAc;QAC9B,OAAO,UAAU,CAAC,KAAK,EAAE,wBAAwB,CAAC,CAAC;IACrD,CAAC;CACF;KAjBmB,wBAAwB","sourcesContent":["import { hasProperty, isObject } from '@metamask/utils';\nimport type {\n JsonRpcNotification,\n JsonRpcParams,\n 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 message: JsonRpcCall<Params> | Readonly<JsonRpcCall<Params>>,\n): message is JsonRpcRequest<Params> => hasProperty(message, 'id');\n\nexport const isNotification = <Params extends JsonRpcParams>(\n message: JsonRpcCall<Params>,\n): message is JsonRpcNotification<Params> => !isRequest(message);\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<Union> = (\n Union extends never ? never : (k: Union) => void\n) extends (k: infer Args) => void\n ? Args\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\n/**\n * The implementation of static `isInstance` methods for classes that have them.\n *\n * @param value - The value to check.\n * @param symbol - The symbol property to check for.\n * @returns Whether the value has `{ [symbol]: true }` in its prototype chain.\n */\nexport const isInstance = (\n value: unknown,\n symbol: symbol,\n): value is { [key: symbol]: true } =>\n isObject(value) && symbol in value && value[symbol] === true;\n\nconst JsonRpcEngineErrorSymbol = Symbol.for(\n 'json-rpc-engine#JsonRpcEngineError',\n);\n\nexport class JsonRpcEngineError extends Error {\n // This is a computed property name, and it doesn't seem possible to make it\n // hash private using `#`.\n // eslint-disable-next-line no-restricted-syntax\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: unknown): value is JsonRpcEngineError {\n return isInstance(value, JsonRpcEngineErrorSymbol);\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/json-rpc-engine",
3
- "version": "10.2.4-preview-dc38a2562",
3
+ "version": "10.2.4-preview-495c91e",
4
4
  "description": "A tool for processing JSON-RPC messages",
5
5
  "keywords": [
6
6
  "Ethereum",
@@ -1,57 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createMethodMiddleware = void 0;
4
- const messenger_1 = require("@metamask/messenger");
5
- const rpc_errors_1 = require("@metamask/rpc-errors");
6
- const utils_1 = require("./v2/utils.cjs");
7
- /**
8
- * Create a JSON-RPC middleware that handles the passed JSON-RPC method handlers using the messenger and hooks.
9
- *
10
- * @deprecated Use the v2 `createMethodMiddleware` instead.
11
- * @param options The options.
12
- * @param options.handlers - The JSON-RPC method handler implementations.
13
- * @param options.messenger - The messenger to be used by the handlers.
14
- * @param options.hooks - The hooks to be used by the handlers.
15
- * @returns A JsonRpcEngineV2 middleware.
16
- */
17
- function createMethodMiddleware(options) {
18
- const { messenger: rootMessenger, onError } = options;
19
- const allHooks = options.hooks;
20
- const expectedHookNames = new Set(Object.values(options.handlers).flatMap((handler) => handler.hookNames ? Object.getOwnPropertyNames(handler.hookNames) : []));
21
- (0, utils_1.assertExpectedHooks)(allHooks, expectedHookNames);
22
- const handlers = Object.entries(options.handlers).reduce((accumulator, [handlerName, handler]) => {
23
- const handlerHooks = (0, utils_1.selectHooks)(allHooks, handler.hookNames) ?? {};
24
- const handlerMessenger = (0, utils_1.createHandlerMessenger)({
25
- namespace: handlerName,
26
- actionNames: handler.actionNames,
27
- rootMessenger,
28
- });
29
- accumulator[handlerName] = {
30
- implementation: handler.implementation,
31
- hooks: handlerHooks,
32
- messenger: handlerMessenger,
33
- };
34
- return accumulator;
35
- }, {});
36
- // This should technically use createAsyncMiddleware, but we get around this by catching
37
- // all handler errors.
38
- // eslint-disable-next-line @typescript-eslint/no-misused-promises
39
- return async (req, res, next, end) => {
40
- const resolved = handlers[req.method];
41
- if (resolved) {
42
- const { implementation, hooks: handlerHooks, messenger } = resolved;
43
- try {
44
- return await implementation(req, res, next, end, handlerHooks, messenger);
45
- }
46
- catch (error) {
47
- onError?.(error, req);
48
- return end(error instanceof Error
49
- ? error
50
- : rpc_errors_1.rpcErrors.internal({ data: error }));
51
- }
52
- }
53
- return next();
54
- };
55
- }
56
- exports.createMethodMiddleware = createMethodMiddleware;
57
- //# sourceMappingURL=createMethodMiddleware.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createMethodMiddleware.cjs","sourceRoot":"","sources":["../src/createMethodMiddleware.ts"],"names":[],"mappings":";;;AACA,mDAAgD;AAChD,qDAAiD;AAajD,0CAKoB;AA+GpB;;;;;;;;;GASG;AACH,SAAgB,sBAAsB,CAGpC,OAAgD;IAEhD,MAAM,EAAE,SAAS,EAAE,aAAa,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;IACtD,MAAM,QAAQ,GAAG,OAAO,CAAC,KAAgC,CAAC;IAE1D,MAAM,iBAAiB,GAAG,IAAI,GAAG,CAC/B,MAAM,C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H,IAAI,EACJ,GAAG,EACH,YAAY,EACZ,SAAS,CACV,CAAC;YACJ,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,OAAO,EAAE,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;gBACtB,OAAO,GAAG,CACR,KAAK,YAAY,KAAK;oBACpB,CAAC,CAAC,KAAK;oBACP,CAAC,CAAC,sBAAS,CAAC,QAAQ,CAAC,EAAE,IAAI,EAAE,KAAa,EAAE,CAAC,CAChD,CAAC;YACJ,CAAC;QACH,CAAC;QAED,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC;AAjED,wDAiEC","sourcesContent":["import type { ActionConstraint } from '@metamask/messenger';\nimport { Messenger } from '@metamask/messenger';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n Json,\n JsonRpcParams,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\n\nimport type {\n JsonRpcEngineEndCallback,\n JsonRpcEngineNextCallback,\n JsonRpcMiddleware,\n} from './JsonRpcEngine';\nimport {\n assertExpectedHooks,\n createHandlerMessenger,\n selectHooks,\n UnionToIntersection,\n} from './v2/utils';\n\ntype HandlerActions<Handler> = Handler extends {\n implementation: (...args: infer Args) => unknown;\n}\n ? Args extends [\n unknown,\n unknown,\n unknown,\n unknown,\n unknown,\n infer HandlerMessenger,\n ]\n ? HandlerMessenger extends Messenger<string, infer Actions>\n ? Actions\n : never\n : never\n : never;\n\ntype HandlerHooks<Handler> = Handler extends {\n implementation: (...args: infer Args) => unknown;\n}\n ? Args extends [unknown, unknown, unknown, unknown, infer ArgHooks, unknown]\n ? ArgHooks extends Record<string, unknown>\n ? ArgHooks\n : never\n : never\n : never;\n\n/**\n * A {@link MethodHandler} implementation.\n *\n * @deprecated Use the v2 `createMethodMiddleware` instead.\n */\nexport type MethodHandlerImplementation<\n Hooks extends Record<string, unknown> = never,\n MessengerActions extends ActionConstraint = never,\n Params extends JsonRpcParams = JsonRpcParams,\n Result extends Json = Json,\n> = (\n req: JsonRpcRequest<Params>,\n res: PendingJsonRpcResponse<Result>,\n next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n hooks: Hooks,\n messenger: Messenger<string, MessengerActions>,\n) => Promise<void> | void;\n\n/**\n * A handler for {@link createMethodMiddleware}.\n *\n * @deprecated Use the v2 `createMethodMiddleware` instead.\n */\nexport type MethodHandler<\n Hooks extends Record<string, unknown> = never,\n MessengerActions extends ActionConstraint = never,\n Params extends JsonRpcParams = JsonRpcParams,\n Result extends Json = Json,\n> = {\n implementation: MethodHandlerImplementation<\n Hooks,\n MessengerActions,\n Params,\n Result\n >;\n methodNames: string[];\n} & ([Hooks] extends [never]\n ? { hookNames?: undefined }\n : { hookNames: { [Key in keyof Hooks]: true } }) &\n ([MessengerActions] extends [never]\n ? { actionNames?: undefined }\n : { actionNames: readonly MessengerActions['type'][] });\n\ntype AnyMethodHandler = {\n implementation(\n this: void,\n req: JsonRpcRequest,\n res: PendingJsonRpcResponse,\n next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n hooks: unknown,\n messenger: unknown,\n ): Promise<void> | void;\n hookNames?: Record<string, true>;\n actionNames?: readonly string[];\n};\n\n/**\n * Options for {@link createMethodMiddleware}.\n *\n * @deprecated Use the v2 `createMethodMiddleware` instead.\n */\nexport type CreateMethodMiddlewareOptions<\n Handlers extends Record<string, AnyMethodHandler>,\n> = {\n handlers: Handlers;\n messenger: Messenger<string, HandlerActions<Handlers[keyof Handlers]>>;\n hooks: UnionToIntersection<HandlerHooks<Handlers[keyof Handlers]>>;\n /**\n * Called when a handler throws, before the error is forwarded to `end`.\n * Intended for logging; must not throw.\n */\n onError?: (error: unknown, request: JsonRpcRequest) => void;\n};\n\ntype ResolvedHandler = {\n implementation: AnyMethodHandler['implementation'];\n hooks: Record<string, unknown>;\n messenger: Messenger<string, ActionConstraint>;\n};\n\n/**\n * Create a JSON-RPC middleware that handles the passed JSON-RPC method handlers using the messenger and hooks.\n *\n * @deprecated Use the v2 `createMethodMiddleware` instead.\n * @param options The options.\n * @param options.handlers - The JSON-RPC method handler implementations.\n * @param options.messenger - The messenger to be used by the handlers.\n * @param options.hooks - The hooks to be used by the handlers.\n * @returns A JsonRpcEngineV2 middleware.\n */\nexport function createMethodMiddleware<\n Handlers extends Record<string, AnyMethodHandler>,\n>(\n options: CreateMethodMiddlewareOptions<Handlers>,\n): JsonRpcMiddleware<JsonRpcParams, Json> {\n const { messenger: rootMessenger, onError } = options;\n const allHooks = options.hooks as Record<string, unknown>;\n\n const expectedHookNames = new Set(\n Object.values(options.handlers).flatMap((handler) =>\n handler.hookNames ? Object.getOwnPropertyNames(handler.hookNames) : [],\n ),\n );\n assertExpectedHooks(allHooks, expectedHookNames);\n\n const handlers = Object.entries(options.handlers).reduce<\n Record<string, ResolvedHandler>\n >((accumulator, [handlerName, handler]) => {\n const handlerHooks = selectHooks(allHooks, handler.hookNames) ?? {};\n const handlerMessenger = createHandlerMessenger<\n HandlerActions<Handlers[keyof Handlers]>\n >({\n namespace: handlerName,\n actionNames: handler.actionNames as\n | readonly HandlerActions<Handlers[keyof Handlers]>['type'][]\n | undefined,\n rootMessenger,\n });\n\n accumulator[handlerName] = {\n implementation: handler.implementation,\n hooks: handlerHooks,\n messenger: handlerMessenger,\n };\n return accumulator;\n }, {});\n\n // This should technically use createAsyncMiddleware, but we get around this by catching\n // all handler errors.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n return async (req, res, next, end) => {\n const resolved = handlers[req.method];\n if (resolved) {\n const { implementation, hooks: handlerHooks, messenger } = resolved;\n try {\n return await implementation(\n req,\n res,\n next,\n end,\n handlerHooks,\n messenger,\n );\n } catch (error) {\n onError?.(error, req);\n return end(\n error instanceof Error\n ? error\n : rpcErrors.internal({ data: error as Json }),\n );\n }\n }\n\n return next();\n };\n}\n"]}
@@ -1,76 +0,0 @@
1
- import type { ActionConstraint } from "@metamask/messenger";
2
- import { Messenger } from "@metamask/messenger";
3
- import type { Json, JsonRpcParams, JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
4
- import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback, JsonRpcMiddleware } from "./JsonRpcEngine.cjs";
5
- import { UnionToIntersection } from "./v2/utils.cjs";
6
- type HandlerActions<Handler> = Handler extends {
7
- implementation: (...args: infer Args) => unknown;
8
- } ? Args extends [
9
- unknown,
10
- unknown,
11
- unknown,
12
- unknown,
13
- unknown,
14
- infer HandlerMessenger
15
- ] ? HandlerMessenger extends Messenger<string, infer Actions> ? Actions : never : never : never;
16
- type HandlerHooks<Handler> = Handler extends {
17
- implementation: (...args: infer Args) => unknown;
18
- } ? Args extends [unknown, unknown, unknown, unknown, infer ArgHooks, unknown] ? ArgHooks extends Record<string, unknown> ? ArgHooks : never : never : never;
19
- /**
20
- * A {@link MethodHandler} implementation.
21
- *
22
- * @deprecated Use the v2 `createMethodMiddleware` instead.
23
- */
24
- export type MethodHandlerImplementation<Hooks extends Record<string, unknown> = never, MessengerActions extends ActionConstraint = never, Params extends JsonRpcParams = JsonRpcParams, Result extends Json = Json> = (req: JsonRpcRequest<Params>, res: PendingJsonRpcResponse<Result>, next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback, hooks: Hooks, messenger: Messenger<string, MessengerActions>) => Promise<void> | void;
25
- /**
26
- * A handler for {@link createMethodMiddleware}.
27
- *
28
- * @deprecated Use the v2 `createMethodMiddleware` instead.
29
- */
30
- export type MethodHandler<Hooks extends Record<string, unknown> = never, MessengerActions extends ActionConstraint = never, Params extends JsonRpcParams = JsonRpcParams, Result extends Json = Json> = {
31
- implementation: MethodHandlerImplementation<Hooks, MessengerActions, Params, Result>;
32
- methodNames: string[];
33
- } & ([Hooks] extends [never] ? {
34
- hookNames?: undefined;
35
- } : {
36
- hookNames: {
37
- [Key in keyof Hooks]: true;
38
- };
39
- }) & ([MessengerActions] extends [never] ? {
40
- actionNames?: undefined;
41
- } : {
42
- actionNames: readonly MessengerActions['type'][];
43
- });
44
- type AnyMethodHandler = {
45
- implementation(this: void, req: JsonRpcRequest, res: PendingJsonRpcResponse, next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback, hooks: unknown, messenger: unknown): Promise<void> | void;
46
- hookNames?: Record<string, true>;
47
- actionNames?: readonly string[];
48
- };
49
- /**
50
- * Options for {@link createMethodMiddleware}.
51
- *
52
- * @deprecated Use the v2 `createMethodMiddleware` instead.
53
- */
54
- export type CreateMethodMiddlewareOptions<Handlers extends Record<string, AnyMethodHandler>> = {
55
- handlers: Handlers;
56
- messenger: Messenger<string, HandlerActions<Handlers[keyof Handlers]>>;
57
- hooks: UnionToIntersection<HandlerHooks<Handlers[keyof Handlers]>>;
58
- /**
59
- * Called when a handler throws, before the error is forwarded to `end`.
60
- * Intended for logging; must not throw.
61
- */
62
- onError?: (error: unknown, request: JsonRpcRequest) => void;
63
- };
64
- /**
65
- * Create a JSON-RPC middleware that handles the passed JSON-RPC method handlers using the messenger and hooks.
66
- *
67
- * @deprecated Use the v2 `createMethodMiddleware` instead.
68
- * @param options The options.
69
- * @param options.handlers - The JSON-RPC method handler implementations.
70
- * @param options.messenger - The messenger to be used by the handlers.
71
- * @param options.hooks - The hooks to be used by the handlers.
72
- * @returns A JsonRpcEngineV2 middleware.
73
- */
74
- export declare function createMethodMiddleware<Handlers extends Record<string, AnyMethodHandler>>(options: CreateMethodMiddlewareOptions<Handlers>): JsonRpcMiddleware<JsonRpcParams, Json>;
75
- export {};
76
- //# sourceMappingURL=createMethodMiddleware.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createMethodMiddleware.d.cts","sourceRoot":"","sources":["../src/createMethodMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,4BAA4B;AAC5D,OAAO,EAAE,SAAS,EAAE,4BAA4B;AAEhD,OAAO,KAAK,EACV,IAAI,EACJ,aAAa,EACb,cAAc,EACd,sBAAsB,EACvB,wBAAwB;AAEzB,OA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,SAAS,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,IAC/C;IACF,QAAQ,EAAE,QAAQ,CAAC;IACnB,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,cAAc,CAAC,QAAQ,CAAC,MAAM,QAAQ,CAAC,CAAC,CAAC,CAAC;IACvE,KAAK,EAAE,mBAAmB,CAAC,YAAY,CAAC,QAAQ,CAAC,MAAM,QAAQ,CAAC,CAAC,CAAC,CAAC;IACnE;;;OAGG;IACH,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,cAAc,KAAK,IAAI,CAAC;CAC7D,CAAC;AAQF;;;;;;;;;GASG;AACH,wBAAgB,sBAAsB,CACpC,QAAQ,SAAS,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,EAEjD,OAAO,EAAE,6BAA6B,CAAC,QAAQ,CAAC,GAC/C,iBAAiB,CAAC,aAAa,EAAE,IAAI,CAAC,CA6DxC"}
@@ -1,76 +0,0 @@
1
- import type { ActionConstraint } from "@metamask/messenger";
2
- import { Messenger } from "@metamask/messenger";
3
- import type { Json, JsonRpcParams, JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
4
- import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback, JsonRpcMiddleware } from "./JsonRpcEngine.mjs";
5
- import { UnionToIntersection } from "./v2/utils.mjs";
6
- type HandlerActions<Handler> = Handler extends {
7
- implementation: (...args: infer Args) => unknown;
8
- } ? Args extends [
9
- unknown,
10
- unknown,
11
- unknown,
12
- unknown,
13
- unknown,
14
- infer HandlerMessenger
15
- ] ? HandlerMessenger extends Messenger<string, infer Actions> ? Actions : never : never : never;
16
- type HandlerHooks<Handler> = Handler extends {
17
- implementation: (...args: infer Args) => unknown;
18
- } ? Args extends [unknown, unknown, unknown, unknown, infer ArgHooks, unknown] ? ArgHooks extends Record<string, unknown> ? ArgHooks : never : never : never;
19
- /**
20
- * A {@link MethodHandler} implementation.
21
- *
22
- * @deprecated Use the v2 `createMethodMiddleware` instead.
23
- */
24
- export type MethodHandlerImplementation<Hooks extends Record<string, unknown> = never, MessengerActions extends ActionConstraint = never, Params extends JsonRpcParams = JsonRpcParams, Result extends Json = Json> = (req: JsonRpcRequest<Params>, res: PendingJsonRpcResponse<Result>, next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback, hooks: Hooks, messenger: Messenger<string, MessengerActions>) => Promise<void> | void;
25
- /**
26
- * A handler for {@link createMethodMiddleware}.
27
- *
28
- * @deprecated Use the v2 `createMethodMiddleware` instead.
29
- */
30
- export type MethodHandler<Hooks extends Record<string, unknown> = never, MessengerActions extends ActionConstraint = never, Params extends JsonRpcParams = JsonRpcParams, Result extends Json = Json> = {
31
- implementation: MethodHandlerImplementation<Hooks, MessengerActions, Params, Result>;
32
- methodNames: string[];
33
- } & ([Hooks] extends [never] ? {
34
- hookNames?: undefined;
35
- } : {
36
- hookNames: {
37
- [Key in keyof Hooks]: true;
38
- };
39
- }) & ([MessengerActions] extends [never] ? {
40
- actionNames?: undefined;
41
- } : {
42
- actionNames: readonly MessengerActions['type'][];
43
- });
44
- type AnyMethodHandler = {
45
- implementation(this: void, req: JsonRpcRequest, res: PendingJsonRpcResponse, next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback, hooks: unknown, messenger: unknown): Promise<void> | void;
46
- hookNames?: Record<string, true>;
47
- actionNames?: readonly string[];
48
- };
49
- /**
50
- * Options for {@link createMethodMiddleware}.
51
- *
52
- * @deprecated Use the v2 `createMethodMiddleware` instead.
53
- */
54
- export type CreateMethodMiddlewareOptions<Handlers extends Record<string, AnyMethodHandler>> = {
55
- handlers: Handlers;
56
- messenger: Messenger<string, HandlerActions<Handlers[keyof Handlers]>>;
57
- hooks: UnionToIntersection<HandlerHooks<Handlers[keyof Handlers]>>;
58
- /**
59
- * Called when a handler throws, before the error is forwarded to `end`.
60
- * Intended for logging; must not throw.
61
- */
62
- onError?: (error: unknown, request: JsonRpcRequest) => void;
63
- };
64
- /**
65
- * Create a JSON-RPC middleware that handles the passed JSON-RPC method handlers using the messenger and hooks.
66
- *
67
- * @deprecated Use the v2 `createMethodMiddleware` instead.
68
- * @param options The options.
69
- * @param options.handlers - The JSON-RPC method handler implementations.
70
- * @param options.messenger - The messenger to be used by the handlers.
71
- * @param options.hooks - The hooks to be used by the handlers.
72
- * @returns A JsonRpcEngineV2 middleware.
73
- */
74
- export declare function createMethodMiddleware<Handlers extends Record<string, AnyMethodHandler>>(options: CreateMethodMiddlewareOptions<Handlers>): JsonRpcMiddleware<JsonRpcParams, Json>;
75
- export {};
76
- //# sourceMappingURL=createMethodMiddleware.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createMethodMiddleware.d.mts","sourceRoot":"","sources":["../src/createMethodMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,4BAA4B;AAC5D,OAAO,EAAE,SAAS,EAAE,4BAA4B;AAEhD,OAAO,KAAK,EACV,IAAI,EACJ,aAAa,EACb,cAAc,EACd,sBAAsB,EACvB,wBAAwB;AAEzB,OAAO,KAAK,EACV,wBAAwB,EACxB,yBAAyB,EACzB,iBAAiB,EAClB,4BAAwB;AACzB,OAAO,EAIL,mBAAmB,EACpB,uBAAmB;AAEpB,KAAK,cAAc,CAAC,OAAO,IAAI,OAAO,SAAS;IAC7C,cAAc,EAAE,CAAC,GAAG,IAAI,EAAE,MAAM,IAAI,KAAK,OAAO,CAAC;CAClD,GACG,IAAI,SAAS;IACX,OAAO;IACP,OAAO;IACP,OAAO;IACP,OAAO;IACP,OAAO;IACP,MAAM,gBAAgB;CACvB,GACC,gBAAgB,SAAS,SAAS,CAAC,MAAM,EAAE,MAAM,OAAO,CAAC,GACvD,OAAO,GACP,KAAK,GACP,KAAK,GACP,KAAK,CAAC;AAEV,KAAK,YAAY,CAAC,OAAO,IAAI,OAAO,SAAS;IAC3C,cAAc,EAAE,CAAC,GAAG,IAAI,EAAE,MAAM,IAAI,KAA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/C,iBAAiB,CAAC,aAAa,EAAE,IAAI,CAAC,CA6DxC"}
@@ -1,53 +0,0 @@
1
- import { Messenger } from "@metamask/messenger";
2
- import { rpcErrors } from "@metamask/rpc-errors";
3
- import { assertExpectedHooks, createHandlerMessenger, selectHooks } from "./v2/utils.mjs";
4
- /**
5
- * Create a JSON-RPC middleware that handles the passed JSON-RPC method handlers using the messenger and hooks.
6
- *
7
- * @deprecated Use the v2 `createMethodMiddleware` instead.
8
- * @param options The options.
9
- * @param options.handlers - The JSON-RPC method handler implementations.
10
- * @param options.messenger - The messenger to be used by the handlers.
11
- * @param options.hooks - The hooks to be used by the handlers.
12
- * @returns A JsonRpcEngineV2 middleware.
13
- */
14
- export function createMethodMiddleware(options) {
15
- const { messenger: rootMessenger, onError } = options;
16
- const allHooks = options.hooks;
17
- const expectedHookNames = new Set(Object.values(options.handlers).flatMap((handler) => handler.hookNames ? Object.getOwnPropertyNames(handler.hookNames) : []));
18
- assertExpectedHooks(allHooks, expectedHookNames);
19
- const handlers = Object.entries(options.handlers).reduce((accumulator, [handlerName, handler]) => {
20
- const handlerHooks = selectHooks(allHooks, handler.hookNames) ?? {};
21
- const handlerMessenger = createHandlerMessenger({
22
- namespace: handlerName,
23
- actionNames: handler.actionNames,
24
- rootMessenger,
25
- });
26
- accumulator[handlerName] = {
27
- implementation: handler.implementation,
28
- hooks: handlerHooks,
29
- messenger: handlerMessenger,
30
- };
31
- return accumulator;
32
- }, {});
33
- // This should technically use createAsyncMiddleware, but we get around this by catching
34
- // all handler errors.
35
- // eslint-disable-next-line @typescript-eslint/no-misused-promises
36
- return async (req, res, next, end) => {
37
- const resolved = handlers[req.method];
38
- if (resolved) {
39
- const { implementation, hooks: handlerHooks, messenger } = resolved;
40
- try {
41
- return await implementation(req, res, next, end, handlerHooks, messenger);
42
- }
43
- catch (error) {
44
- onError?.(error, req);
45
- return end(error instanceof Error
46
- ? error
47
- : rpcErrors.internal({ data: error }));
48
- }
49
- }
50
- return next();
51
- };
52
- }
53
- //# sourceMappingURL=createMethodMiddleware.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createMethodMiddleware.mjs","sourceRoot":"","sources":["../src/createMethodMiddleware.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,4BAA4B;AAChD,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAajD,OAAO,EACL,mBAAmB,EACnB,sBAAsB,EACtB,WAAW,EAEZ,uBAAmB;AA+GpB;;;;;;;;;GASG;AACH,MAAM,UAAU,sBAAsB,CAGpC,OAAgD;IAEhD,MAAM,EAAE,SAAS,EAAE,aAAa,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;IACtD,MAAM,QAAQ,GAAG,OAAO,CAAC,KAAgC,CAAC;IAE1D,MAAM,iBAAiB,GAAG,IAAI,GAAG,CAC/B,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAClD,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,mBAAmB,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,EAAE,CACvE,CACF,CAAC;IACF,mBAAmB,CAAC,QAAQ,EAAE,iBAAiB,CAAC,CAAC;IAEjD,MAAM,QAAQ,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,MAAM,CAEtD,CAAC,WAAW,EAAE,CAAC,WAAW,EAAE,OAAO,CAAC,EAAE,EAAE;QACxC,MAAM,YAAY,GAAG,WAAW,CAAC,QAAQ,EAAE,OAAO,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC;QACpE,MAAM,gBAAgB,GAAG,sBAAsB,CAE7C;YACA,SAAS,EAAE,WAAW;YACtB,WAAW,EAAE,OAAO,CAAC,WAER;YACb,aAAa;SACd,CAAC,CAAC;QAEH,WAAW,CAAC,WAAW,CAAC,GAAG;YACzB,cAAc,EAAE,OAAO,CAAC,cAAc;YACtC,KAAK,EAAE,YAAY;YACnB,SAAS,EAAE,gBAAgB;SAC5B,CAAC;QACF,OAAO,WAAW,CAAC;IACrB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,wFAAwF;IACxF,sBAAsB;IACtB,kEAAkE;IAClE,OAAO,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,EAAE;QACnC,MAAM,QAAQ,GAAG,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACtC,IAAI,QAAQ,EAAE,CAAC;YACb,MAAM,EAAE,cAAc,EAAE,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,GAAG,QAAQ,CAAC;YACpE,IAAI,CAAC;gBACH,OAAO,MAAM,cAAc,CACzB,GAAG,EACH,GAAG,EACH,IAAI,EACJ,GAAG,EACH,YAAY,EACZ,SAAS,CACV,CAAC;YACJ,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,OAAO,EAAE,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;gBACtB,OAAO,GAAG,CACR,KAAK,YAAY,KAAK;oBACpB,CAAC,CAAC,KAAK;oBACP,CAAC,CAAC,SAAS,CAAC,QAAQ,CAAC,EAAE,IAAI,EAAE,KAAa,EAAE,CAAC,CAChD,CAAC;YACJ,CAAC;QACH,CAAC;QAED,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { ActionConstraint } from '@metamask/messenger';\nimport { Messenger } from '@metamask/messenger';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n Json,\n JsonRpcParams,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\n\nimport type {\n JsonRpcEngineEndCallback,\n JsonRpcEngineNextCallback,\n JsonRpcMiddleware,\n} from './JsonRpcEngine';\nimport {\n assertExpectedHooks,\n createHandlerMessenger,\n selectHooks,\n UnionToIntersection,\n} from './v2/utils';\n\ntype HandlerActions<Handler> = Handler extends {\n implementation: (...args: infer Args) => unknown;\n}\n ? Args extends [\n unknown,\n unknown,\n unknown,\n unknown,\n unknown,\n infer HandlerMessenger,\n ]\n ? HandlerMessenger extends Messenger<string, infer Actions>\n ? Actions\n : never\n : never\n : never;\n\ntype HandlerHooks<Handler> = Handler extends {\n implementation: (...args: infer Args) => unknown;\n}\n ? Args extends [unknown, unknown, unknown, unknown, infer ArgHooks, unknown]\n ? ArgHooks extends Record<string, unknown>\n ? ArgHooks\n : never\n : never\n : never;\n\n/**\n * A {@link MethodHandler} implementation.\n *\n * @deprecated Use the v2 `createMethodMiddleware` instead.\n */\nexport type MethodHandlerImplementation<\n Hooks extends Record<string, unknown> = never,\n MessengerActions extends ActionConstraint = never,\n Params extends JsonRpcParams = JsonRpcParams,\n Result extends Json = Json,\n> = (\n req: JsonRpcRequest<Params>,\n res: PendingJsonRpcResponse<Result>,\n next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n hooks: Hooks,\n messenger: Messenger<string, MessengerActions>,\n) => Promise<void> | void;\n\n/**\n * A handler for {@link createMethodMiddleware}.\n *\n * @deprecated Use the v2 `createMethodMiddleware` instead.\n */\nexport type MethodHandler<\n Hooks extends Record<string, unknown> = never,\n MessengerActions extends ActionConstraint = never,\n Params extends JsonRpcParams = JsonRpcParams,\n Result extends Json = Json,\n> = {\n implementation: MethodHandlerImplementation<\n Hooks,\n MessengerActions,\n Params,\n Result\n >;\n methodNames: string[];\n} & ([Hooks] extends [never]\n ? { hookNames?: undefined }\n : { hookNames: { [Key in keyof Hooks]: true } }) &\n ([MessengerActions] extends [never]\n ? { actionNames?: undefined }\n : { actionNames: readonly MessengerActions['type'][] });\n\ntype AnyMethodHandler = {\n implementation(\n this: void,\n req: JsonRpcRequest,\n res: PendingJsonRpcResponse,\n next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n hooks: unknown,\n messenger: unknown,\n ): Promise<void> | void;\n hookNames?: Record<string, true>;\n actionNames?: readonly string[];\n};\n\n/**\n * Options for {@link createMethodMiddleware}.\n *\n * @deprecated Use the v2 `createMethodMiddleware` instead.\n */\nexport type CreateMethodMiddlewareOptions<\n Handlers extends Record<string, AnyMethodHandler>,\n> = {\n handlers: Handlers;\n messenger: Messenger<string, HandlerActions<Handlers[keyof Handlers]>>;\n hooks: UnionToIntersection<HandlerHooks<Handlers[keyof Handlers]>>;\n /**\n * Called when a handler throws, before the error is forwarded to `end`.\n * Intended for logging; must not throw.\n */\n onError?: (error: unknown, request: JsonRpcRequest) => void;\n};\n\ntype ResolvedHandler = {\n implementation: AnyMethodHandler['implementation'];\n hooks: Record<string, unknown>;\n messenger: Messenger<string, ActionConstraint>;\n};\n\n/**\n * Create a JSON-RPC middleware that handles the passed JSON-RPC method handlers using the messenger and hooks.\n *\n * @deprecated Use the v2 `createMethodMiddleware` instead.\n * @param options The options.\n * @param options.handlers - The JSON-RPC method handler implementations.\n * @param options.messenger - The messenger to be used by the handlers.\n * @param options.hooks - The hooks to be used by the handlers.\n * @returns A JsonRpcEngineV2 middleware.\n */\nexport function createMethodMiddleware<\n Handlers extends Record<string, AnyMethodHandler>,\n>(\n options: CreateMethodMiddlewareOptions<Handlers>,\n): JsonRpcMiddleware<JsonRpcParams, Json> {\n const { messenger: rootMessenger, onError } = options;\n const allHooks = options.hooks as Record<string, unknown>;\n\n const expectedHookNames = new Set(\n Object.values(options.handlers).flatMap((handler) =>\n handler.hookNames ? Object.getOwnPropertyNames(handler.hookNames) : [],\n ),\n );\n assertExpectedHooks(allHooks, expectedHookNames);\n\n const handlers = Object.entries(options.handlers).reduce<\n Record<string, ResolvedHandler>\n >((accumulator, [handlerName, handler]) => {\n const handlerHooks = selectHooks(allHooks, handler.hookNames) ?? {};\n const handlerMessenger = createHandlerMessenger<\n HandlerActions<Handlers[keyof Handlers]>\n >({\n namespace: handlerName,\n actionNames: handler.actionNames as\n | readonly HandlerActions<Handlers[keyof Handlers]>['type'][]\n | undefined,\n rootMessenger,\n });\n\n accumulator[handlerName] = {\n implementation: handler.implementation,\n hooks: handlerHooks,\n messenger: handlerMessenger,\n };\n return accumulator;\n }, {});\n\n // This should technically use createAsyncMiddleware, but we get around this by catching\n // all handler errors.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n return async (req, res, next, end) => {\n const resolved = handlers[req.method];\n if (resolved) {\n const { implementation, hooks: handlerHooks, messenger } = resolved;\n try {\n return await implementation(\n req,\n res,\n next,\n end,\n handlerHooks,\n messenger,\n );\n } catch (error) {\n onError?.(error, req);\n return end(\n error instanceof Error\n ? error\n : rpcErrors.internal({ data: error as Json }),\n );\n }\n }\n\n return next();\n };\n}\n"]}