Skip to content

Commit

Permalink
chore: add eslint rule to restrict global fetch (#6500)
Browse files Browse the repository at this point in the history
* chore: add eslint rule to restrict global fetch

* Add comment to eslint disable

* Rephrase comment
  • Loading branch information
nflaig authored Feb 29, 2024
1 parent 6675739 commit 6ad9740
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ module.exports = {
"no-console": "error",
"no-loss-of-precision": "error",
"no-prototype-builtins": 0,
"no-restricted-globals": [
"error",
{
name: "fetch",
message: "Please use 'fetch' from '@lodestar/api' instead.",
},
],
"no-restricted-imports": [
"error",
{
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/utils/client/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
async function wrappedFetch(url: string | URL, init?: RequestInit): Promise<Response> {
try {
// This function wraps global `fetch` which should only be directly called here
// eslint-disable-next-line no-restricted-globals
return await fetch(url, init);
} catch (e) {
throw new FetchError(url, e);
Expand Down
10 changes: 5 additions & 5 deletions packages/api/test/unit/client/httpClientFallback.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {describe, it, beforeEach, afterEach, expect, vi} from "vitest";
import {HttpClient} from "../../../src/utils/client/index.js";
import {HttpClient, fetch} from "../../../src/utils/client/index.js";

describe("httpClient fallback", () => {
const testRoute = {url: "/test-route", method: "GET" as const};
const DEBUG_LOGS = Boolean(process.env.DEBUG);

// Using fetchSub instead of actually setting up servers because there are some strange
// race conditions, where the server stub doesn't count the call in time before the test is over.
const fetchStub = vi.fn();
const fetchStub = vi.fn<Parameters<typeof fetch>, ReturnType<typeof fetch>>();

let httpClient: HttpClient;

Expand All @@ -20,7 +20,7 @@ describe("httpClient fallback", () => {
const serverErrors = new Map<number, boolean>();

// With baseURLs above find the server index associated with that URL
function getServerIndex(url: URL): number {
function getServerIndex(url: URL | string): number {
const i = baseUrls.findIndex((baseUrl) => url.toString().startsWith(baseUrl));
if (i < 0) {
throw Error(`fetch called with unknown url ${url.toString()}`);
Expand All @@ -33,7 +33,7 @@ describe("httpClient fallback", () => {
httpClient = new HttpClient({
baseUrl: "",
urls: baseUrls.map((baseUrl) => ({baseUrl})),
fetch: fetchStub as typeof fetch,
fetch: fetchStub,
});

fetchStub.mockImplementation(async (url) => {
Expand All @@ -57,7 +57,7 @@ describe("httpClient fallback", () => {
const callCounts: number[] = [];
for (let i = 0; i < serverCount; i++) callCounts[i] = 0;
for (const call of fetchStub.mock.calls) {
callCounts[getServerIndex(call)]++;
callCounts[getServerIndex(call[0])]++;
}

expect(callCounts.join(",")).toBe(expectedCallCounts.join(","));
Expand Down

0 comments on commit 6ad9740

Please sign in to comment.