Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow createFetchMiddleware to take an RPC service #357

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mcmire
Copy link
Contributor

@mcmire mcmire commented Jan 28, 2025

@metamask/network-controller now contains an RpcService class. Using this class not only allows us to remove a lot of code from this package, as it incorporates the vast majority of logic contained in createFetchMiddleware, including the retry logic, but it also allows us to use the circuit breaker pattern and the exponential backoff pattern to prevent too many retries if the network is unreliable, and then automatically cut over to a failover node when the network truly goes down.

Closes #356.
Closes #207.
Closes #209.
Closes #211.
Closes #166.

Copy link

socket-security bot commented Jan 28, 2025

New and updated dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@jest/[email protected] None 0 6.07 kB simenb
npm/@metamask/[email protected] None 0 348 kB metamaskbot
npm/@metamask/[email protected] network 0 322 kB metamaskbot
npm/@metamask/[email protected] network +2 874 kB lgbot
npm/@metamask/[email protected] None 0 0 B
npm/@metamask/[email protected] 🔁 npm/@metamask/[email protected] None +1 82.1 kB danfinlay, gudahtt, kumavis, ...6 more
npm/@metamask/[email protected] None 0 10.8 kB mcmire
npm/@metamask/[email protected] None 0 321 kB lgbot
npm/@metamask/[email protected] 🔁 npm/@metamask/[email protected] None 0 178 kB danfinlay, gudahtt, kumavis, ...6 more
npm/@metamask/[email protected] environment 0 812 kB metamaskbot
npm/@metamask/[email protected] None 0 292 kB lgbot
npm/@metamask/[email protected] None 0 37.1 kB metamaskbot
npm/@noble/[email protected] 🔁 npm/@noble/[email protected] None 0 1.08 MB paulmillr
npm/@sinclair/[email protected] None 0 442 kB sinclair
npm/@spruceid/[email protected] None 0 36.3 kB sbihel
npm/@tsd/[email protected] None 0 46.8 MB samverschueren
npm/@types/[email protected] None 0 165 kB types
npm/@types/[email protected] None 0 25.8 kB types
npm/@types/[email protected] 🔁 npm/@types/[email protected] None 0 31.7 kB types
npm/@types/[email protected] None 0 6.27 kB types
npm/@types/[email protected] None 0 5.81 kB types
npm/[email protected] filesystem 0 1.46 MB ldthomas
npm/[email protected] None 0 2.34 kB sindresorhus
npm/[email protected] None 0 63 kB dirtyhairy
npm/[email protected] None 0 351 kB mikemcl
npm/[email protected] None +1 16.3 kB sindresorhus
npm/[email protected] None 0 465 kB connor.peet
npm/[email protected] None +1 6.55 kB sindresorhus
npm/[email protected] None 0 2.94 kB sindresorhus
npm/[email protected] environment 0 8.82 kB sindresorhus
npm/[email protected] None 0 200 kB stefanbuck
npm/[email protected] None 0 254 kB danfinlay
npm/[email protected] None 0 5.14 kB sindresorhus
npm/[email protected] 🔁 npm/[email protected] None 0 8.77 kB ljharb
npm/[email protected] None +1 232 kB 1api
npm/[email protected] environment 0 872 kB mweststrate
npm/[email protected] None 0 6.92 kB sindresorhus
npm/[email protected] 🔁 npm/[email protected] None 0 33.5 kB ljharb
npm/[email protected] None 0 9.44 kB silentcicero
npm/[email protected] None 0 3.54 kB sindresorhus
npm/[email protected] None 0 81.8 kB emn178
npm/[email protected] None 0 22.8 kB doowb
npm/[email protected] None 0 4.58 kB sindresorhus
npm/[email protected] None 0 86.2 kB pimterry
npm/[email protected] None 0 9.49 kB sindresorhus
npm/[email protected] None +4 195 kB sindresorhus
npm/[email protected] None 0 2.97 kB thejameskyle
npm/[email protected] None +1 11.1 kB vdemedes
npm/[email protected] None 0 5 kB sindresorhus
npm/[email protected] None 0 7.47 kB sindresorhus
npm/[email protected] Transitive: filesystem +5 97.5 kB sindresorhus
npm/[email protected] filesystem +4 151 kB sindresorhus
npm/[email protected] None 0 3.6 kB sindresorhus
npm/[email protected] None 0 646 kB acemarke
npm/[email protected] 🔁 npm/[email protected] None 0 95.8 kB npm-cli-ops
npm/[email protected] None 0 9.66 kB silentcicero
npm/[email protected] None 0 3.31 kB sindresorhus
npm/[email protected] None 0 3.85 kB sindresorhus
npm/[email protected] Transitive: environment +6 322 kB sindresorhus
npm/[email protected] None 0 90.4 kB typescript-bot
npm/[email protected] None 0 17.2 kB odysseas
npm/[email protected] None 0 6.46 kB raynos

View full report↗︎

Copy link

socket-security bot commented Jan 28, 2025

👍 Dependency issues cleared. Learn more about Socket for GitHub ↗︎

This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored.

Ignoring: npm/[email protected], npm/[email protected], npm/@metamask/[email protected], npm/@spruceid/[email protected], npm/@metamask/[email protected], npm/@metamask/[email protected]

View full report↗︎

Next steps

Take a deeper look at the dependency

Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at support [AT] socket [DOT] dev.

Remove the package

If you happen to install a dependency that Socket reports as Known Malware you should immediately remove it and select a different dependency. For other alert types, you may may wish to investigate alternative packages or consider if there are other ways to mitigate the specific risk posed by the dependency.

Mark a package as acceptable risk

To ignore an alert, reply with a comment starting with @SocketSecurity ignore followed by a space separated list of ecosystem/package-name@version specifiers. e.g. @SocketSecurity ignore npm/[email protected] or ignore all packages with @SocketSecurity ignore-all

@mcmire
Copy link
Contributor Author

mcmire commented Jan 28, 2025

@SocketSecurity ignore npm/[email protected]
@SocketSecurity ignore npm/[email protected]
@SocketSecurity ignore npm/@metamask/[email protected]
@SocketSecurity ignore npm/@spruceid/[email protected]
@SocketSecurity ignore npm/@metamask-previews/[email protected]

New author is OK. Either it's us or known members of the NPM community.
Network access is OK. @metamask/controller-utils includes several wrappers around fetch.

@github-advanced-security
Copy link

This pull request sets up GitHub code scanning for this repository. Once the scans have completed and the checks have passed, the analysis results for this pull request branch will appear on this overview. Once you merge this pull request, the 'Security' tab will show more code scanning analysis results (for example, for the default branch). Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results. For more information about GitHub code scanning, check out the documentation.

@mcmire mcmire force-pushed the integrate-rpc-service branch 7 times, most recently from 9777496 to 23479ea Compare February 3, 2025 22:31
@mcmire
Copy link
Contributor Author

mcmire commented Feb 3, 2025

@SocketSecurity ignore npm/@metamask/[email protected]

This is our package.

`@metamask/network-controller` now contains an `RpcService` class. Using
this class not only allows us to remove a lot of code from this package,
as it incorporates the vast majority of logic contained in
`createFetchMiddleware`, including the retry logic, but it also allows
us to use the circuit breaker pattern and the exponential backoff
pattern to prevent too many retries if the network is unreliable, and
then automatically cut over to a failover node when the network truly
goes down.
@mcmire mcmire force-pushed the integrate-rpc-service branch from 23479ea to 5ecddf7 Compare February 4, 2025 23:01
@mcmire
Copy link
Contributor Author

mcmire commented Feb 4, 2025

@SocketSecurity ignore npm/@metamask/[email protected]

This is our package.

* Like a JSON-RPC request, but includes an optional `origin` property.
* This will be included in the request as a header if specified.
*/
export type JsonRpcRequestWithOrigin<Params extends JsonRpcParams> =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Purposefully not reusing PayloadWithOrigin above because 1) it's badly named, 2) it doesn't take any type parameters and 3) we need a type and not an interface going forward.

* @returns The fetch middleware.
*/
function createFetchMiddlewareWithoutRpcService({
btoa: givenBtoa,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btoa and fetch are globals, so we rename them.

* The interface for a service class responsible for making a request to an RPC
* endpoint.
*/
export type AbstractRpcService = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could get this from @metamask/network-controller, but that would create a circular dependency.

@mcmire mcmire marked this pull request as ready for review February 4, 2025 23:06
@mcmire mcmire requested a review from a team as a code owner February 4, 2025 23:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment