From ec0de5ffe778bfef9e5673ce23c8019beaaf74f4 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Mon, 18 Sep 2023 15:01:05 +0200 Subject: [PATCH] fix: use `node-fetch` only as fallback --- .changeset/sweet-dingos-boil.md | 6 ++++++ packages/cli/src/serve/keyboard.ts | 7 +++++-- packages/metro-service/src/server.ts | 6 ++++-- 3 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 .changeset/sweet-dingos-boil.md diff --git a/.changeset/sweet-dingos-boil.md b/.changeset/sweet-dingos-boil.md new file mode 100644 index 000000000..8183e2afb --- /dev/null +++ b/.changeset/sweet-dingos-boil.md @@ -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 diff --git a/packages/cli/src/serve/keyboard.ts b/packages/cli/src/serve/keyboard.ts index 7d4d3c631..28cd0f646 100644 --- a/packages/cli/src/serve/keyboard.ts +++ b/packages/cli/src/serve/keyboard.ts @@ -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"; @@ -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; } diff --git a/packages/metro-service/src/server.ts b/packages/metro-service/src/server.ts index 4d3fb7440..be4424739 100644 --- a/packages/metro-service/src/server.ts +++ b/packages/metro-service/src/server.ts @@ -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"; @@ -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" &&