Skip to content

Commit

Permalink
fix: use node-fetch only as fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 committed Sep 18, 2023
1 parent 1bc6fa8 commit ec0de5f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
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

0 comments on commit ec0de5f

Please sign in to comment.