From c876223f356a4e0835f0fda4acb8917e42ca70a0 Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Thu, 9 Feb 2023 12:23:07 -0330 Subject: [PATCH] Remove `node-fetch`. The package `node-fetch` has been removed, instead requiring that `fetch` be provided as an option for the fetch middleware. This might reduce our bundle size (depending on whether our bundler is smart enough to omit this package already; I'm not sure). It also eliminates a dependency that has been a source of security advisories in the past. We will probably want to add it back as a `devDependency` when we add tests for this middleware, but so far this middleware is not tested. --- package.json | 1 - src/fetch.ts | 35 +++++++++++++++++++++++------------ yarn.lock | 39 --------------------------------------- 3 files changed, 23 insertions(+), 52 deletions(-) diff --git a/package.json b/package.json index 0341d3b8..01f3eeec 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,6 @@ "eth-rpc-errors": "^4.0.3", "json-rpc-engine": "^6.1.0", "json-stable-stringify": "^1.0.1", - "node-fetch": "^2.6.7", "pify": "^3.0.0" }, "devDependencies": { diff --git a/src/fetch.ts b/src/fetch.ts index 3622b5d8..e2b6aaa9 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -7,7 +7,6 @@ import { EthereumRpcError, ethErrors } from 'eth-rpc-errors'; import type { Block } from './types'; /* eslint-disable @typescript-eslint/no-require-imports,@typescript-eslint/no-shadow */ -const fetch = global.fetch || require('node-fetch'); const btoa = global.btoa || require('btoa'); /* eslint-enable @typescript-eslint/no-require-imports,@typescript-eslint/no-shadow */ @@ -34,19 +33,27 @@ interface FetchConfig { fetchUrl: string; fetchParams: Request; } -interface FetchMiddlewareOptions { - rpcUrl: string; - originHttpHeaderKey?: string; -} - -interface FetchMiddlewareFromReqOptions extends FetchMiddlewareOptions { - req: PayloadWithOrigin; -} +/** + * Create middleware for sending a JSON-RPC request to the given RPC URL. + * + * @param options - Options + * @param options.fetch - The `fetch` function; expected to be equivalent to `window.fetch`. + * @param options.rpcUrl - The URL to send the request to. + * @param options.originHttpHeaderKey - If provider, the origin field for each JSON-RPC request + * will be attached to each outgoing fetch request under this header. + * @returns The fetch middleware. + */ export function createFetchMiddleware({ + // eslint-disable-next-line @typescript-eslint/no-shadow + fetch, rpcUrl, originHttpHeaderKey, -}: FetchMiddlewareOptions): JsonRpcMiddleware { +}: { + fetch: typeof global.fetch; + rpcUrl: string; + originHttpHeaderKey?: string; +}): JsonRpcMiddleware { return createAsyncMiddleware(async (req, res, _next) => { const { fetchUrl, fetchParams } = createFetchConfigFromReq({ req, @@ -59,7 +66,7 @@ export function createFetchMiddleware({ const retryInterval = 1000; for (let attempt = 0; attempt < maxAttempts; attempt++) { try { - const fetchRes: Response = await fetch(fetchUrl, fetchParams); + const fetchRes = await fetch(fetchUrl, fetchParams); // check for http errrors checkForHttpErrors(fetchRes); // parse response body @@ -133,7 +140,11 @@ export function createFetchConfigFromReq({ req, rpcUrl, originHttpHeaderKey, -}: FetchMiddlewareFromReqOptions): FetchConfig { +}: { + rpcUrl: string; + originHttpHeaderKey?: string; + req: PayloadWithOrigin; +}): FetchConfig { const parsedUrl: URL = new URL(rpcUrl); const fetchUrl: string = normalizeUrlFromParsed(parsedUrl); diff --git a/yarn.lock b/yarn.lock index 38ee4488..c8b49666 100644 --- a/yarn.lock +++ b/yarn.lock @@ -880,7 +880,6 @@ __metadata: jest: ^27.5.1 json-rpc-engine: ^6.1.0 json-stable-stringify: ^1.0.1 - node-fetch: ^2.6.7 pify: ^3.0.0 prettier: ^2.2.1 prettier-plugin-packagejson: ^2.2.11 @@ -4851,20 +4850,6 @@ __metadata: languageName: node linkType: hard -"node-fetch@npm:^2.6.7": - version: 2.6.7 - resolution: "node-fetch@npm:2.6.7" - dependencies: - whatwg-url: ^5.0.0 - peerDependencies: - encoding: ^0.1.0 - peerDependenciesMeta: - encoding: - optional: true - checksum: 8d816ffd1ee22cab8301c7756ef04f3437f18dace86a1dae22cf81db8ef29c0bf6655f3215cb0cdb22b420b6fe141e64b26905e7f33f9377a7fa59135ea3e10b - languageName: node - linkType: hard - "node-gyp@npm:^7.1.0": version: 7.1.2 resolution: "node-gyp@npm:7.1.2" @@ -6108,13 +6093,6 @@ __metadata: languageName: node linkType: hard -"tr46@npm:~0.0.3": - version: 0.0.3 - resolution: "tr46@npm:0.0.3" - checksum: 726321c5eaf41b5002e17ffbd1fb7245999a073e8979085dacd47c4b4e8068ff5777142fc6726d6ca1fd2ff16921b48788b87225cbc57c72636f6efa8efbffe3 - languageName: node - linkType: hard - "ts-jest@npm:^27.1.4": version: 27.1.5 resolution: "ts-jest@npm:27.1.5" @@ -6463,13 +6441,6 @@ __metadata: languageName: node linkType: hard -"webidl-conversions@npm:^3.0.0": - version: 3.0.1 - resolution: "webidl-conversions@npm:3.0.1" - checksum: c92a0a6ab95314bde9c32e1d0a6dfac83b578f8fa5f21e675bc2706ed6981bc26b7eb7e6a1fab158e5ce4adf9caa4a0aee49a52505d4d13c7be545f15021b17c - languageName: node - linkType: hard - "webidl-conversions@npm:^5.0.0": version: 5.0.0 resolution: "webidl-conversions@npm:5.0.0" @@ -6500,16 +6471,6 @@ __metadata: languageName: node linkType: hard -"whatwg-url@npm:^5.0.0": - version: 5.0.0 - resolution: "whatwg-url@npm:5.0.0" - dependencies: - tr46: ~0.0.3 - webidl-conversions: ^3.0.0 - checksum: b8daed4ad3356cc4899048a15b2c143a9aed0dfae1f611ebd55073310c7b910f522ad75d727346ad64203d7e6c79ef25eafd465f4d12775ca44b90fa82ed9e2c - languageName: node - linkType: hard - "whatwg-url@npm:^8.0.0, whatwg-url@npm:^8.5.0": version: 8.7.0 resolution: "whatwg-url@npm:8.7.0"