Skip to content

Commit

Permalink
Log all accessible ports on remote mode dev (#1881)
Browse files Browse the repository at this point in the history
* Log all accessible ports on remote mode dev

* Fix: port 0
  • Loading branch information
Skye-31 committed Sep 26, 2022
1 parent b829c9c commit 6ff5a03
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
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)
22 changes: 21 additions & 1 deletion 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 @@ -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);
})
Expand Down Expand Up @@ -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;
}

0 comments on commit 6ff5a03

Please sign in to comment.