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

chore: add eslint rule to restrict global fetch #6500

Merged
merged 3 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a comment here which explains why disabling this rule is important here. I believe that's the only place we need to use native fetch.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added a comment but obviously the function can't call itself, so I thought it is kinda self explanatory here

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
Loading