@metamask/multichain-api-middleware 2.0.0 → 3.1.0

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 (62) hide show
  1. package/CHANGELOG.md +43 -3
  2. package/dist/handlers/index.cjs +20 -0
  3. package/dist/handlers/index.cjs.map +1 -0
  4. package/dist/handlers/index.d.cts +25 -0
  5. package/dist/handlers/index.d.cts.map +1 -0
  6. package/dist/handlers/index.d.mts +25 -0
  7. package/dist/handlers/index.d.mts.map +1 -0
  8. package/dist/handlers/index.mjs +17 -0
  9. package/dist/handlers/index.mjs.map +1 -0
  10. package/dist/handlers/types.cjs +1 -0
  11. package/dist/handlers/types.cjs.map +1 -1
  12. package/dist/handlers/types.d.cts +13 -9
  13. package/dist/handlers/types.d.cts.map +1 -1
  14. package/dist/handlers/types.d.mts +13 -9
  15. package/dist/handlers/types.d.mts.map +1 -1
  16. package/dist/handlers/types.mjs +1 -0
  17. package/dist/handlers/types.mjs.map +1 -1
  18. package/dist/handlers/wallet-createSession.cjs +5 -6
  19. package/dist/handlers/wallet-createSession.cjs.map +1 -1
  20. package/dist/handlers/wallet-createSession.d.cts +35 -32
  21. package/dist/handlers/wallet-createSession.d.cts.map +1 -1
  22. package/dist/handlers/wallet-createSession.d.mts +35 -32
  23. package/dist/handlers/wallet-createSession.d.mts.map +1 -1
  24. package/dist/handlers/wallet-createSession.mjs +4 -5
  25. package/dist/handlers/wallet-createSession.mjs.map +1 -1
  26. package/dist/handlers/wallet-getSession.cjs +4 -5
  27. package/dist/handlers/wallet-getSession.cjs.map +1 -1
  28. package/dist/handlers/wallet-getSession.d.cts +18 -19
  29. package/dist/handlers/wallet-getSession.d.cts.map +1 -1
  30. package/dist/handlers/wallet-getSession.d.mts +18 -19
  31. package/dist/handlers/wallet-getSession.d.mts.map +1 -1
  32. package/dist/handlers/wallet-getSession.mjs +3 -4
  33. package/dist/handlers/wallet-getSession.mjs.map +1 -1
  34. package/dist/handlers/wallet-invokeMethod.cjs +7 -5
  35. package/dist/handlers/wallet-invokeMethod.cjs.map +1 -1
  36. package/dist/handlers/wallet-invokeMethod.d.cts +32 -31
  37. package/dist/handlers/wallet-invokeMethod.d.cts.map +1 -1
  38. package/dist/handlers/wallet-invokeMethod.d.mts +32 -31
  39. package/dist/handlers/wallet-invokeMethod.d.mts.map +1 -1
  40. package/dist/handlers/wallet-invokeMethod.mjs +7 -5
  41. package/dist/handlers/wallet-invokeMethod.mjs.map +1 -1
  42. package/dist/handlers/wallet-revokeSession.cjs +9 -6
  43. package/dist/handlers/wallet-revokeSession.cjs.map +1 -1
  44. package/dist/handlers/wallet-revokeSession.d.cts +22 -14
  45. package/dist/handlers/wallet-revokeSession.d.cts.map +1 -1
  46. package/dist/handlers/wallet-revokeSession.d.mts +22 -14
  47. package/dist/handlers/wallet-revokeSession.d.mts.map +1 -1
  48. package/dist/handlers/wallet-revokeSession.mjs +8 -5
  49. package/dist/handlers/wallet-revokeSession.mjs.map +1 -1
  50. package/dist/index.cjs +3 -9
  51. package/dist/index.cjs.map +1 -1
  52. package/dist/index.d.cts +2 -4
  53. package/dist/index.d.cts.map +1 -1
  54. package/dist/index.d.mts +2 -4
  55. package/dist/index.d.mts.map +1 -1
  56. package/dist/index.mjs +1 -4
  57. package/dist/index.mjs.map +1 -1
  58. package/dist/middlewares/MultichainSubscriptionManager.cjs.map +1 -1
  59. package/dist/middlewares/MultichainSubscriptionManager.d.cts.map +1 -1
  60. package/dist/middlewares/MultichainSubscriptionManager.d.mts.map +1 -1
  61. package/dist/middlewares/MultichainSubscriptionManager.mjs.map +1 -1
  62. package/package.json +22 -20
@@ -1 +1 @@
1
- {"version":3,"file":"MultichainSubscriptionManager.mjs","sourceRoot":"","sources":["../../src/middlewares/MultichainSubscriptionManager.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AACA,OAAO,EAAE,KAAK,EAAE,mCAAmC;AACnD,OAAO,0BAAyB,8DAA2D;;AAE3F,OAAO,gBAAgB,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,CAAC,OAAO,CAAC,CAAC,iBAAiB,EAAE,EAAE;YAChD,IACE,iBAAiB,CAAC,MAAM,KAAK,MAAM;gBACnC,iBAAiB,CAAC,KAAK,KAAK,KAAK,EACjC,CAAC;gBACD,uBAAA,IAAI,4FAAa,MAAjB,IAAI,EAAc,iBAAiB,CAAC,CAAC;YACvC,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF;6XAhGuB,EACpB,KAAK,EACL,MAAM,EACN,KAAK,GACW;IAChB,OAAO,uBAAA,IAAI,oDAAe,CAAC,IAAI,CAAC,CAAC,iBAAiB,EAAE,EAAE;QACpD,OAAO,CACL,iBAAiB,CAAC,KAAK,KAAK,KAAK;YACjC,iBAAiB,CAAC,MAAM,KAAK,MAAM;YACnC,iBAAiB,CAAC,KAAK,KAAK,KAAK,CAClC,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,2HAEwB,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAqB;IAClE,uBAAA,IAAI,gDAAkB,uBAAA,IAAI,oDAAe,CAAC,MAAM,CAAC,CAAC,iBAAiB,EAAE,EAAE;QACrE,OAAO,CACL,iBAAiB,CAAC,KAAK,KAAK,KAAK;YACjC,iBAAiB,CAAC,MAAM,KAAK,MAAM;YACnC,iBAAiB,CAAC,KAAK,KAAK,KAAK,CAClC,CAAC;IACJ,CAAC,CAAC,MAAA,CAAC;AACL,CAAC,mGAuCY,iBAAoC;IAC/C,iBAAiB,CAAC,mBAAmB,CAAC,OAAO,EAAE,EAAE,CAAC;IAElD,uBAAA,IAAI,wGAAyB,MAA7B,IAAI,EAA0B,iBAAiB,CAAC,CAAC;AACnD,CAAC","sourcesContent":["import type { ExternalScopeString } from '@metamask/chain-agnostic-permission';\nimport { toHex } from '@metamask/controller-utils';\nimport createSubscriptionManager from '@metamask/eth-json-rpc-filters/subscriptionManager';\nimport type { NetworkController } from '@metamask/network-controller';\nimport SafeEventEmitter from '@metamask/safe-event-emitter';\nimport type { CaipChainId, Hex } from '@metamask/utils';\nimport { parseCaipChainId } from '@metamask/utils';\n\nimport type { ExtendedJsonRpcMiddleware } from './MultichainMiddlewareManager';\nimport { MultichainApiNotifications } from '../handlers/types';\n\nexport type SubscriptionManager = {\n events: SafeEventEmitter;\n destroy?: () => void;\n middleware: ExtendedJsonRpcMiddleware;\n};\n\ntype SubscriptionNotificationEvent = {\n jsonrpc: '2.0';\n method: 'eth_subscription';\n params: {\n subscription: Hex;\n result: unknown;\n };\n};\n\ntype SubscriptionKey = {\n scope: ExternalScopeString;\n origin: string;\n tabId?: number;\n};\ntype SubscriptionEntry = SubscriptionKey & {\n subscriptionManager: SubscriptionManager;\n};\n\ntype MultichainSubscriptionManagerOptions = {\n findNetworkClientIdByChainId: NetworkController['findNetworkClientIdByChainId'];\n getNetworkClientById: NetworkController['getNetworkClientById'];\n};\n\n/**\n * A helper that facilates the lifecycle of a SubscriptionManager instance that\n * is meant to handle subscriptons for only one specific scope, origin, and tabId combination.\n */\nexport class MultichainSubscriptionManager extends SafeEventEmitter {\n readonly #findNetworkClientIdByChainId: NetworkController['findNetworkClientIdByChainId'];\n\n readonly #getNetworkClientById: NetworkController['getNetworkClientById'];\n\n #subscriptions: SubscriptionEntry[] = [];\n\n /**\n * Construct a MultichainSubscriptionManager.\n *\n * @param options - The controller options.\n * @param options.findNetworkClientIdByChainId - The hook to get the networkClientId from a chainId.\n * @param options.getNetworkClientById - The hook to get the network client instance by its networkClientId.\n */\n constructor(options: MultichainSubscriptionManagerOptions) {\n super();\n this.#findNetworkClientIdByChainId = options.findNetworkClientIdByChainId;\n this.#getNetworkClientById = options.getNetworkClientById;\n }\n\n notify(\n { scope, origin, tabId }: SubscriptionKey,\n { method, params }: SubscriptionNotificationEvent,\n ) {\n this.emit('notification', origin, tabId, {\n method: MultichainApiNotifications.walletNotify,\n params: {\n scope,\n notification: { method, params },\n },\n });\n }\n\n #getSubscriptionEntry({\n scope,\n origin,\n tabId,\n }: SubscriptionKey): SubscriptionEntry | undefined {\n return this.#subscriptions.find((subscriptionEntry) => {\n return (\n subscriptionEntry.scope === scope &&\n subscriptionEntry.origin === origin &&\n subscriptionEntry.tabId === tabId\n );\n });\n }\n\n #removeSubscriptionEntry({ scope, origin, tabId }: SubscriptionEntry) {\n this.#subscriptions = this.#subscriptions.filter((subscriptionEntry) => {\n return (\n subscriptionEntry.scope !== scope ||\n subscriptionEntry.origin !== origin ||\n subscriptionEntry.tabId !== tabId\n );\n });\n }\n\n subscribe(subscriptionKey: SubscriptionKey) {\n const subscriptionEntry = this.#getSubscriptionEntry(subscriptionKey);\n if (subscriptionEntry) {\n return subscriptionEntry.subscriptionManager;\n }\n\n const networkClientId = this.#findNetworkClientIdByChainId(\n toHex(parseCaipChainId(subscriptionKey.scope as CaipChainId).reference),\n );\n const networkClient = this.#getNetworkClientById(networkClientId);\n const subscriptionManager = createSubscriptionManager({\n blockTracker: networkClient.blockTracker,\n provider: networkClient.provider,\n });\n\n subscriptionManager.events.on(\n 'notification',\n (message: SubscriptionNotificationEvent) => {\n this.notify(subscriptionKey, message);\n },\n );\n\n const newSubscriptionManagerEntry = {\n ...subscriptionKey,\n subscriptionManager,\n };\n subscriptionManager.destroy = subscriptionManager.middleware.destroy;\n subscriptionManager.middleware.destroy = this.#unsubscribe.bind(\n this,\n newSubscriptionManagerEntry,\n );\n\n this.#subscriptions.push(newSubscriptionManagerEntry);\n\n return subscriptionManager;\n }\n\n #unsubscribe(subscriptionEntry: SubscriptionEntry) {\n subscriptionEntry.subscriptionManager.destroy?.();\n\n this.#removeSubscriptionEntry(subscriptionEntry);\n }\n\n unsubscribeByScope(scope: ExternalScopeString) {\n this.#subscriptions.forEach((subscriptionEntry) => {\n if (subscriptionEntry.scope === scope) {\n this.#unsubscribe(subscriptionEntry);\n }\n });\n }\n\n unsubscribeByScopeAndOrigin(scope: ExternalScopeString, origin: string) {\n this.#subscriptions.forEach((subscriptionEntry) => {\n if (\n subscriptionEntry.scope === scope &&\n subscriptionEntry.origin === origin\n ) {\n this.#unsubscribe(subscriptionEntry);\n }\n });\n }\n\n unsubscribeByOriginAndTabId(origin: string, tabId?: number) {\n this.#subscriptions.forEach((subscriptionEntry) => {\n if (\n subscriptionEntry.origin === origin &&\n subscriptionEntry.tabId === tabId\n ) {\n this.#unsubscribe(subscriptionEntry);\n }\n });\n }\n}\n"]}
1
+ {"version":3,"file":"MultichainSubscriptionManager.mjs","sourceRoot":"","sources":["../../src/middlewares/MultichainSubscriptionManager.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AACA,OAAO,EAAE,KAAK,EAAE,mCAAmC;AACnD,OAAO,0BAAyB,8DAA2D;;AAE3F,OAAO,gBAAgB,qCAAqC;AAE5D,OAAO,EAAE,gBAAgB,EAAE,wBAAwB;AAEnD,OAAO,EAAE,0BAA0B,EAAE,8BAA0B;AAgC/D;;;GAGG;AACH,MAAM,OAAO,6BAA8B,SAAQ,gBAAgB;IAOjE;;;;;;OAMG;IACH,YAAY,OAA6C;QACvD,KAAK,EAAE,CAAC;;QAdD,8EAAiF;QAEjF,sEAAiE;QAE1E,uDAAsC,EAAE,EAAC;QAWvC,uBAAA,IAAI,+DAAiC,OAAO,CAAC,4BAA4B,MAAA,CAAC;QAC1E,uBAAA,IAAI,uDAAyB,OAAO,CAAC,oBAAoB,MAAA,CAAC;IAC5D,CAAC;IAED,MAAM,CACJ,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAmB,EACzC,EAAE,MAAM,EAAE,MAAM,EAAiC;QAEjD,IAAI,CAAC,IAAI,CAAC,cAAc,EAAE,MAAM,EAAE,KAAK,EAAE;YACvC,MAAM,EAAE,0BAA0B,CAAC,YAAY;YAC/C,MAAM,EAAE;gBACN,KAAK;gBACL,YAAY,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE;aACjC;SACF,CAAC,CAAC;IACL,CAAC;IA0BD,SAAS,CAAC,eAAgC;QACxC,MAAM,iBAAiB,GAAG,uBAAA,IAAI,qGAAsB,MAA1B,IAAI,E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K,KAAK,KAAK,CAClC,CAAC;IACJ,CAAC,CAAC,MAAA,CAAC;AACL,CAAC,mGAuCY,iBAAoC;IAC/C,iBAAiB,CAAC,mBAAmB,CAAC,OAAO,EAAE,EAAE,CAAC;IAElD,uBAAA,IAAI,wGAAyB,MAA7B,IAAI,EAA0B,iBAAiB,CAAC,CAAC;AACnD,CAAC","sourcesContent":["import type { ExternalScopeString } from '@metamask/chain-agnostic-permission';\nimport { toHex } from '@metamask/controller-utils';\nimport createSubscriptionManager from '@metamask/eth-json-rpc-filters/subscriptionManager';\nimport type { NetworkController } from '@metamask/network-controller';\nimport SafeEventEmitter from '@metamask/safe-event-emitter';\nimport type { CaipChainId, Hex } from '@metamask/utils';\nimport { parseCaipChainId } from '@metamask/utils';\n\nimport { MultichainApiNotifications } from '../handlers/types';\nimport type { ExtendedJsonRpcMiddleware } from './MultichainMiddlewareManager';\n\nexport type SubscriptionManager = {\n events: SafeEventEmitter;\n destroy?: () => void;\n middleware: ExtendedJsonRpcMiddleware;\n};\n\ntype SubscriptionNotificationEvent = {\n jsonrpc: '2.0';\n method: 'eth_subscription';\n params: {\n subscription: Hex;\n result: unknown;\n };\n};\n\ntype SubscriptionKey = {\n scope: ExternalScopeString;\n origin: string;\n tabId?: number;\n};\ntype SubscriptionEntry = SubscriptionKey & {\n subscriptionManager: SubscriptionManager;\n};\n\ntype MultichainSubscriptionManagerOptions = {\n findNetworkClientIdByChainId: NetworkController['findNetworkClientIdByChainId'];\n getNetworkClientById: NetworkController['getNetworkClientById'];\n};\n\n/**\n * A helper that facilates the lifecycle of a SubscriptionManager instance that\n * is meant to handle subscriptons for only one specific scope, origin, and tabId combination.\n */\nexport class MultichainSubscriptionManager extends SafeEventEmitter {\n readonly #findNetworkClientIdByChainId: NetworkController['findNetworkClientIdByChainId'];\n\n readonly #getNetworkClientById: NetworkController['getNetworkClientById'];\n\n #subscriptions: SubscriptionEntry[] = [];\n\n /**\n * Construct a MultichainSubscriptionManager.\n *\n * @param options - The controller options.\n * @param options.findNetworkClientIdByChainId - The hook to get the networkClientId from a chainId.\n * @param options.getNetworkClientById - The hook to get the network client instance by its networkClientId.\n */\n constructor(options: MultichainSubscriptionManagerOptions) {\n super();\n this.#findNetworkClientIdByChainId = options.findNetworkClientIdByChainId;\n this.#getNetworkClientById = options.getNetworkClientById;\n }\n\n notify(\n { scope, origin, tabId }: SubscriptionKey,\n { method, params }: SubscriptionNotificationEvent,\n ) {\n this.emit('notification', origin, tabId, {\n method: MultichainApiNotifications.walletNotify,\n params: {\n scope,\n notification: { method, params },\n },\n });\n }\n\n #getSubscriptionEntry({\n scope,\n origin,\n tabId,\n }: SubscriptionKey): SubscriptionEntry | undefined {\n return this.#subscriptions.find((subscriptionEntry) => {\n return (\n subscriptionEntry.scope === scope &&\n subscriptionEntry.origin === origin &&\n subscriptionEntry.tabId === tabId\n );\n });\n }\n\n #removeSubscriptionEntry({ scope, origin, tabId }: SubscriptionEntry) {\n this.#subscriptions = this.#subscriptions.filter((subscriptionEntry) => {\n return (\n subscriptionEntry.scope !== scope ||\n subscriptionEntry.origin !== origin ||\n subscriptionEntry.tabId !== tabId\n );\n });\n }\n\n subscribe(subscriptionKey: SubscriptionKey) {\n const subscriptionEntry = this.#getSubscriptionEntry(subscriptionKey);\n if (subscriptionEntry) {\n return subscriptionEntry.subscriptionManager;\n }\n\n const networkClientId = this.#findNetworkClientIdByChainId(\n toHex(parseCaipChainId(subscriptionKey.scope as CaipChainId).reference),\n );\n const networkClient = this.#getNetworkClientById(networkClientId);\n const subscriptionManager = createSubscriptionManager({\n blockTracker: networkClient.blockTracker,\n provider: networkClient.provider,\n });\n\n subscriptionManager.events.on(\n 'notification',\n (message: SubscriptionNotificationEvent) => {\n this.notify(subscriptionKey, message);\n },\n );\n\n const newSubscriptionManagerEntry = {\n ...subscriptionKey,\n subscriptionManager,\n };\n subscriptionManager.destroy = subscriptionManager.middleware.destroy;\n subscriptionManager.middleware.destroy = this.#unsubscribe.bind(\n this,\n newSubscriptionManagerEntry,\n );\n\n this.#subscriptions.push(newSubscriptionManagerEntry);\n\n return subscriptionManager;\n }\n\n #unsubscribe(subscriptionEntry: SubscriptionEntry) {\n subscriptionEntry.subscriptionManager.destroy?.();\n\n this.#removeSubscriptionEntry(subscriptionEntry);\n }\n\n unsubscribeByScope(scope: ExternalScopeString) {\n this.#subscriptions.forEach((subscriptionEntry) => {\n if (subscriptionEntry.scope === scope) {\n this.#unsubscribe(subscriptionEntry);\n }\n });\n }\n\n unsubscribeByScopeAndOrigin(scope: ExternalScopeString, origin: string) {\n this.#subscriptions.forEach((subscriptionEntry) => {\n if (\n subscriptionEntry.scope === scope &&\n subscriptionEntry.origin === origin\n ) {\n this.#unsubscribe(subscriptionEntry);\n }\n });\n }\n\n unsubscribeByOriginAndTabId(origin: string, tabId?: number) {\n this.#subscriptions.forEach((subscriptionEntry) => {\n if (\n subscriptionEntry.origin === origin &&\n subscriptionEntry.tabId === tabId\n ) {\n this.#unsubscribe(subscriptionEntry);\n }\n });\n }\n}\n"]}
package/package.json CHANGED
@@ -1,21 +1,26 @@
1
1
  {
2
2
  "name": "@metamask/multichain-api-middleware",
3
- "version": "2.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "JSON-RPC methods and middleware to support the MetaMask Multichain API",
5
5
  "keywords": [
6
- "MetaMask",
7
- "Ethereum"
6
+ "Ethereum",
7
+ "MetaMask"
8
8
  ],
9
9
  "homepage": "https://github.com/MetaMask/core/tree/main/packages/multichain-api-middleware#readme",
10
10
  "bugs": {
11
11
  "url": "https://github.com/MetaMask/core/issues"
12
12
  },
13
+ "license": "MIT",
13
14
  "repository": {
14
15
  "type": "git",
15
16
  "url": "https://github.com/MetaMask/core.git"
16
17
  },
17
- "license": "MIT",
18
+ "files": [
19
+ "dist/"
20
+ ],
18
21
  "sideEffects": false,
22
+ "main": "./dist/index.cjs",
23
+ "types": "./dist/index.d.cts",
19
24
  "exports": {
20
25
  ".": {
21
26
  "import": {
@@ -29,11 +34,10 @@
29
34
  },
30
35
  "./package.json": "./package.json"
31
36
  },
32
- "main": "./dist/index.cjs",
33
- "types": "./dist/index.d.cts",
34
- "files": [
35
- "dist/"
36
- ],
37
+ "publishConfig": {
38
+ "access": "public",
39
+ "registry": "https://registry.npmjs.org/"
40
+ },
37
41
  "scripts": {
38
42
  "build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references",
39
43
  "build:all": "ts-bridge --project tsconfig.build.json --verbose --clean",
@@ -47,22 +51,24 @@
47
51
  "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
48
52
  },
49
53
  "dependencies": {
54
+ "@metamask/accounts-controller": "^38.0.0",
50
55
  "@metamask/api-specs": "^0.14.0",
51
- "@metamask/chain-agnostic-permission": "^1.5.0",
52
- "@metamask/controller-utils": "^11.19.0",
53
- "@metamask/json-rpc-engine": "^10.2.3",
54
- "@metamask/network-controller": "^30.0.0",
55
- "@metamask/permission-controller": "^12.2.1",
56
+ "@metamask/chain-agnostic-permission": "^1.6.0",
57
+ "@metamask/controller-utils": "^11.20.0",
58
+ "@metamask/json-rpc-engine": "^10.4.0",
59
+ "@metamask/network-controller": "^30.1.0",
60
+ "@metamask/permission-controller": "^13.1.0",
56
61
  "@metamask/rpc-errors": "^7.0.2",
62
+ "@metamask/snaps-controllers": "^19.0.0",
57
63
  "@metamask/utils": "^11.9.0",
58
64
  "@open-rpc/meta-schema": "^1.14.6",
59
65
  "@open-rpc/schema-utils-js": "^2.0.5",
60
66
  "jsonschema": "^1.4.1"
61
67
  },
62
68
  "devDependencies": {
63
- "@metamask/auto-changelog": "^3.4.4",
69
+ "@metamask/auto-changelog": "^6.1.0",
64
70
  "@metamask/eth-json-rpc-filters": "^9.0.0",
65
- "@metamask/multichain-transactions-controller": "^7.0.2",
71
+ "@metamask/multichain-transactions-controller": "^7.1.0",
66
72
  "@metamask/safe-event-emitter": "^3.0.0",
67
73
  "@ts-bridge/cli": "^0.6.4",
68
74
  "@types/jest": "^29.5.14",
@@ -75,9 +81,5 @@
75
81
  },
76
82
  "engines": {
77
83
  "node": "^18.18 || >=20"
78
- },
79
- "publishConfig": {
80
- "access": "public",
81
- "registry": "https://registry.npmjs.org/"
82
84
  }
83
85
  }