MetaMask / MetaMask/metamask-extension
[`MetamaskController` B.2] Port `connection-manager` body from `MetamaskController`
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 13.2k
- Forks
- 5.6k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 451
Description
**Sub-epic:** [#41737 — `MetamaskController` decomposition: extension-specific modules](https://github.com/MetaMask/metamask-extension/issues/41737)
**Files:**
- `app/scripts/lib/connection-manager/index.ts` (new)
- `app/scripts/lib/connection-manager/connection-manager.test.ts` (new)
- `app/scripts/metamask-controller.js`
**Size:** L
## Problem
Connection lifecycle logic lives inline on `MetamaskController`: communication setup (eip1193/caip/trusted/phishing), connection CRUD, notification fan-out, port/phishing/message handlers, `getConnectedSites`, and `getRpcMethodMiddleware` — roughly 16 methods in total. Extract them into `connection-manager/` so the responsibility is named and independently testable, and so `session-manager` (B.1) can call `ConnectionManager:notifyAllConnections` via messenger.
## Solution
1. Create `app/scripts/lib/connection-manager/` with `index.ts` exporting the `ConnectionManager` class.
2. Port all connection-lifecycle methods from `metamask-controller.js` into the class.
3. Hold state in a private `#connections: Map`.
4. Register `ConnectionManager:notifyAllConnections` as a messenger action (consumed by `session-manager`).
5. Add unit tests.
6. Delete the moved method bodies from `metamask-controller.js`.
7. Update `MetamaskController#getApi()` forwards where applicable.
## Acceptance criteria
- [ ] `app/scripts/lib/connection-manager/index.ts` exists with the full set of connection-lifecycle methods
- [ ] State held in a private `#connections` map
- [ ] `notifyAllConnections` exposed as a messenger action
- [ ] Moved method bodies removed from `app/scripts/metamask-controller.js`
- [ ] `getApi()` entries rewritten to call `messenger.call('Module:action', ...)` directly — no forwarding stub remains in `MetamaskController`
- [ ] `yarn test` green, `tsc` green, extension dev build boots
## Dependencies
None. Unblocks B.5.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the connection-lifecycle methods in app/scripts/metamask-controller.js and the related messenger patterns from the session-manager decomposition. Create the new index.ts and connection-manager.test.ts, then run the focused unit tests before yarn test and tsc. Done means the methods and state are moved, notifyAllConnections is exposed through messenger, getApi() has no forwarding stub, and the extension dev build boots.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- api, backend
- Issue type
- Refactor
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 38/100