Skip to content

Commit

Permalink
Remove node-fetch.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Gudahtt committed Feb 10, 2023
1 parent 9c1dba0 commit dd40dfb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 52 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
35 changes: 23 additions & 12 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand All @@ -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<unknown, unknown> {
}: {
fetch: typeof global.fetch;
rpcUrl: string;
originHttpHeaderKey?: string;
}): JsonRpcMiddleware<unknown, unknown> {
return createAsyncMiddleware(async (req, res, _next) => {
const { fetchUrl, fetchParams } = createFetchConfigFromReq({
req,
Expand All @@ -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
Expand Down Expand Up @@ -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);

Expand Down
39 changes: 0 additions & 39 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit dd40dfb

Please sign in to comment.