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

fix: use node-fetch only as fallback #2688

Merged
merged 1 commit into from
Sep 19, 2023
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
6 changes: 6 additions & 0 deletions .changeset/sweet-dingos-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rnx-kit/metro-service": patch
"@rnx-kit/cli": patch
---

Use `node-fetch` only as fallback when current Node version doesn't implement Fetch API
7 changes: 5 additions & 2 deletions packages/cli/src/serve/keyboard.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { info } from "@rnx-kit/console";
import type { MetroTerminal } from "@rnx-kit/metro-service";
import fetch from "node-fetch";
import nodeFetch from "node-fetch";
import qrcode from "qrcode";
import readline from "readline";
import type { DevServerMiddleware } from "./types";
Expand Down Expand Up @@ -43,7 +43,10 @@ export function attachKeyHandlers({
break;

case "j": {
fetch(devServerUrl + "/open-debugger", { method: "POST" });
info("Opening debugger...");
// TODO: Remove `node-fetch` when we drop support for Node 16
const ftch = "fetch" in globalThis ? fetch : nodeFetch;
ftch(devServerUrl + "/open-debugger", { method: "POST" });
break;
}

Expand Down
6 changes: 4 additions & 2 deletions packages/metro-service/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { runServer } from "metro";
import net from "net";
import fetch from "node-fetch";
import nodeFetch from "node-fetch";
import { ensureBabelConfig } from "./babel";

type ServerStatus = "not_running" | "already_running" | "in_use" | "unknown";
Expand Down Expand Up @@ -48,8 +48,10 @@ export async function isDevServerRunning(
return "not_running";
}

// TODO: Remove `node-fetch` when we drop support for Node 16
const ftch = "fetch" in globalThis ? fetch : nodeFetch;
const statusUrl = `${scheme}://${host || "localhost"}:${port}/status`;
const statusResponse = await fetch(statusUrl);
const statusResponse = await ftch(statusUrl);
const body = await statusResponse.text();

return body === "packager-status:running" &&
Expand Down