Skip to content

Commit

Permalink
Log all accessible ports on remote mode dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Skye-31 committed Sep 18, 2022
1 parent d8fe95d commit 599ab75
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/forty-files-smoke.md
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Chore: correctly log all listening ports on remote mode (closes #1652)
16 changes: 16 additions & 0 deletions 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";
Expand Down Expand Up @@ -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);
})
Expand Down Expand Up @@ -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;
}

0 comments on commit 599ab75

Please sign in to comment.