From 6ff5a0308b8f65f0422719ede3a2a4863311d3d9 Mon Sep 17 00:00:00 2001 From: Skye <46286597+Skye-31@users.noreply.github.com> Date: Mon, 26 Sep 2022 13:49:23 +0100 Subject: [PATCH] Log all accessible ports on remote mode dev (#1881) * Log all accessible ports on remote mode dev * Fix: port 0 --- .changeset/forty-files-smoke.md | 5 +++++ packages/wrangler/src/proxy.ts | 22 +++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) 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 00000000000..9ee8eb36cdd --- /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 32fbb8abde5..bc71aaa7856 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"; @@ -327,7 +328,15 @@ export function usePreviewServer({ }) .then(() => { proxy.server.on("listening", () => { - logger.log(`⬣ Listening at ${localProtocol}://${ip}:${port}`); + const address = proxy.server.address(); + const usedPort = + address && typeof address === "object" ? address.port : port; + logger.log(`⬣ Listening at ${localProtocol}://${ip}:${usedPort}`); + const accessibleHosts = + ip !== "0.0.0.0" ? [ip] : getAccessibleHosts(); + for (const accessibleHost of accessibleHosts) { + logger.log(`- ${localProtocol}://${accessibleHost}:${usedPort}`); + } }); proxy.server.listen(port, ip); }) @@ -484,3 +493,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; +}