From 599ab7543256d014dee8dca55aedd625163b53e2 Mon Sep 17 00:00:00 2001 From: Skye <46286597+Skye-31@users.noreply.github.com> Date: Sun, 18 Sep 2022 17:46:46 +0100 Subject: [PATCH] Log all accessible ports on remote mode dev --- .changeset/forty-files-smoke.md | 5 +++++ packages/wrangler/src/proxy.ts | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 .changeset/forty-files-smoke.md diff --git a/.changeset/forty-files-smoke.md b/.changeset/forty-files-smoke.md new file mode 100644 index 000000000000..9ee8eb36cdd6 --- /dev/null +++ b/.changeset/forty-files-smoke.md @@ -0,0 +1,5 @@ +--- +"wrangler": patch +--- + +Chore: correctly log all listening ports on remote mode (closes #1652) diff --git a/packages/wrangler/src/proxy.ts b/packages/wrangler/src/proxy.ts index 32fbb8abde59..3cc1cda955f2 100644 --- a/packages/wrangler/src/proxy.ts +++ b/packages/wrangler/src/proxy.ts @@ -1,6 +1,7 @@ import { createServer as createHttpServer } from "node:http"; import { connect } from "node:http2"; import { createServer as createHttpsServer } from "node:https"; +import { networkInterfaces } from "node:os"; import WebSocket from "faye-websocket"; import { createHttpTerminator } from "http-terminator"; import { useEffect, useRef, useState } from "react"; @@ -328,6 +329,10 @@ export function usePreviewServer({ .then(() => { proxy.server.on("listening", () => { logger.log(`⬣ Listening at ${localProtocol}://${ip}:${port}`); + const accessibleHosts = ip !== "0.0.0.0" ? [ip] : getAccessibleHosts(); + for (const accessibleHost of accessibleHosts) { + logger.log(`- ${localProtocol}://${accessibleHost}:${port}`); + } }); proxy.server.listen(port, ip); }) @@ -484,3 +489,14 @@ export async function waitForPortToBeAvailable( } }); } + +function getAccessibleHosts(): string[] { + const hosts: string[] = []; + Object.values(networkInterfaces()).forEach((net) => { + net?.forEach(({ family, address }) => { + // @ts-expect-error the `family` property is numeric as of Node.js 18.0.0 + if (family === "IPv4" || family === 4) hosts.push(address); + }); + }); + return hosts; +}