Skip to content

Commit 15574eb

Browse files
feat(p2-shim): add support for retrieving HTTP server address
This commit adds support for retrieving the port of a created `HTTPServer` that is listening. This is helpful for servers that should be started on port 0 (w/ OS support for random port selection), so that users can get the resolved address out later, if needed.
1 parent 2830898 commit 15574eb

File tree

5 files changed

+30
-0
lines changed

5 files changed

+30
-0
lines changed

packages/preview2-shim/lib/io/calls.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export const HTTP_SERVER_STOP = ++call_id << CALL_SHIFT;
6262
export const HTTP_SERVER_INCOMING_HANDLER = ++call_id << CALL_SHIFT;
6363
export const HTTP_SERVER_SET_OUTGOING_RESPONSE = ++call_id << CALL_SHIFT;
6464
export const HTTP_SERVER_CLEAR_OUTGOING_RESPONSE = ++call_id << CALL_SHIFT;
65+
export const HTTP_SERVER_GET_ADDRESS = ++call_id << CALL_SHIFT;
6566
export const HTTP_OUTGOING_BODY_DISPOSE = ++call_id << CALL_SHIFT;
6667

6768
// Clocks

packages/preview2-shim/lib/io/worker-http.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export function clearOutgoingResponse(id) {
2727
responses.delete(id);
2828
}
2929

30+
export async function getHttpServerAddress(id) {
31+
return servers.get(id).address();
32+
}
33+
3034
export async function setOutgoingResponse(
3135
id,
3236
{ statusCode, headers, streamId }

packages/preview2-shim/lib/io/worker-thread.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
setOutgoingResponse,
99
startHttpServer,
1010
stopHttpServer,
11+
getHttpServerAddress,
1112
} from './worker-http.js';
1213
import { Readable } from 'node:stream';
1314
import { read } from 'node:fs';
@@ -29,6 +30,7 @@ import {
2930
HTTP_SERVER_SET_OUTGOING_RESPONSE,
3031
HTTP_SERVER_START,
3132
HTTP_SERVER_STOP,
33+
HTTP_SERVER_GET_ADDRESS,
3234
INPUT_STREAM_BLOCKING_READ,
3335
INPUT_STREAM_BLOCKING_SKIP,
3436
INPUT_STREAM_CREATE,
@@ -417,6 +419,8 @@ function handle(call, id, payload) {
417419
return setOutgoingResponse(id, payload);
418420
case HTTP_SERVER_CLEAR_OUTGOING_RESPONSE:
419421
return clearOutgoingResponse(id);
422+
case HTTP_SERVER_GET_ADDRESS:
423+
return getHttpServerAddress(id);
420424

421425
// Sockets name resolution
422426
case SOCKET_RESOLVE_ADDRESS_CREATE_REQUEST:

packages/preview2-shim/lib/nodejs/http.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
HTTP_SERVER_SET_OUTGOING_RESPONSE,
1010
HTTP_SERVER_START,
1111
HTTP_SERVER_STOP,
12+
HTTP_SERVER_GET_ADDRESS,
1213
OUTPUT_STREAM_CREATE,
1314
OUTPUT_STREAM_DISPOSE,
1415
} from '../io/calls.js';
@@ -776,6 +777,9 @@ export class HTTPServer {
776777
this.#liveEventLoopInterval = setInterval(() => {}, 10_000);
777778
ioCall(HTTP_SERVER_START, this.#id, { port, host });
778779
}
780+
address() {
781+
return ioCall(HTTP_SERVER_GET_ADDRESS, this.#id);
782+
}
779783
stop() {
780784
if (this.#stopped) {
781785
return;

packages/preview2-shim/test/test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,23 @@ suite("Node.js Preview2", () => {
500500
});
501501
});
502502

503+
suite("HTTPServer", () => {
504+
test(
505+
"HTTPServer: can retrieve randomized server address",
506+
testWithGCWrap(async () => {
507+
const { HTTPServer } = await import("@bytecodealliance/preview2-shim/http");
508+
const server = new HTTPServer({
509+
handle() {
510+
throw new Error("never called");
511+
}
512+
});
513+
server.listen(0);
514+
const address = server.address();
515+
assert(Number.isSafeInteger(address?.port) && address?.port != 0, "a random port was assigned and retrieved");
516+
}),
517+
);
518+
});
519+
503520
suite("Instantiation", () => {
504521
test("WASIShim export (random)", async () => {
505522
const { random } = await import("@bytecodealliance/preview2-shim");

0 commit comments

Comments
 (0)