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

Add missing fetch imports #593

Merged
merged 3 commits into from
Jul 3, 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
1 change: 1 addition & 0 deletions packages/replay/src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fetch from "node-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.

This has been "broken"* for months from what I can tell but it seemed like a good idea to fix it now (I run an audit through all of the files in the repo to see if fetch is imported by all them). I decided to use node-fetch (over undici) as that's what is used more by this package (the old CLI)

*didn't always work in node 18 or earlier

import { spawn } from "child_process";
import { createHash } from "crypto";
import dbg from "./debug";
Expand Down
21 changes: 13 additions & 8 deletions packages/shared/src/cachedFetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import assert from "assert";
import { cachedFetch, resetCache } from "./cachedFetch";
import type {
cachedFetch as cachedFetchStatic,
resetCache as resetCacheStatic,
} from "./cachedFetch";

class Response {
public status: number;
Expand All @@ -21,20 +24,22 @@ const failedResponse = new Response(500, "error");
const successResponse = new Response(200, "ok");

describe("cachedFetch", () => {
let globalFetch: typeof fetch;
let cachedFetch: typeof cachedFetchStatic;
let resetCache: typeof resetCacheStatic;
let mockFetch: jest.Mock;

beforeEach(() => {
globalFetch = global.fetch;
mockFetch = global.fetch = jest.fn(async (url: string) => {
return successResponse;
}) as jest.Mock;
jest.mock("undici");

mockFetch = require("undici").fetch;
mockFetch.mockReturnValue(successResponse);

({ cachedFetch, resetCache } = require("./cachedFetch"));
});

afterEach(() => {
global.fetch = globalFetch;

resetCache();
mockFetch.mockClear();
});

it("should return a successful response", async () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/src/cachedFetch.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { fetch } from "undici";
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems very easy to forget to do. Can we add a Lint rule to disallow using the global fetch? Else I can see us reintroducing this problem in the future.

Copy link
Member Author

@Andarist Andarist Jul 3, 2024

Choose a reason for hiding this comment

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

PRO-755 , it should be a super quick ticket but I'm wrapping up for today and I don't have time to switch branches and test it out. I'm also unsure what's the current state of linting in this repo

Copy link
Contributor

Choose a reason for hiding this comment

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

🫡


type CacheEntry = { json: any | null; status: number; statusText: string };

export const cache: Map<string, CacheEntry> = new Map();
Expand Down