Something went wrong. Try again.
Monorepo for the nibling.run platform
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116// runtime.onListening / runtime.onUnlistening: apps choose their own port, and the embedder// learns it from the runtime instead of guessing.import { test, expect } from "@playwright/test";import "./guest";
const SERVER = `const http = require("node:http");const a = http.createServer((req, res) => res.end("a"));a.listen(4100, () => { const b = http.createServer((req, res) => res.end("b")); b.listen(4200, () => setTimeout(() => a.close(() => b.close()), 50));});`;
test("listen and close events arrive in order, with the port's URL", async ({ page }) => { await page.goto("/demo/fixture.html"); await page.waitForFunction(() => window.__runtime !== undefined, undefined, { timeout: 20_000 }); const events = await page.evaluate(async (src) => { const runtime = window.__runtime!; const log: string[] = []; runtime.onListening((port, url) => log.push(`listen ${port} ${url}`)); runtime.onUnlistening((port) => log.push(`close ${port}`)); runtime.writeTree({ "/app/ports.cjs": src }); const code = await runtime.exec("/app/ports.cjs").exited; log.push(`exit ${code}`); return log; }, SERVER); const host = /\/~\/@([0-9a-f]{16})\//.exec(events[0] ?? "")?.[1]; expect(events).toEqual([ `listen 4100 /~/@${host}/http/4100/`, `listen 4200 /~/@${host}/http/4200/`, "close 4100", "close 4200", "exit 0", ]);});
test("a late subscriber is told about ports already listening, and can unsubscribe from its first call", async ({ page }) => { await page.goto("/demo/fixture.html"); await page.waitForFunction(() => window.__runtime !== undefined, undefined, { timeout: 20_000 }); const seen = await page.evaluate(async () => { const runtime = window.__runtime!; runtime.writeTree({ "/app/late.cjs": 'require("node:http").createServer((q, s) => s.end()).listen(4300);' }); runtime.exec("/app/late.cjs"); await runtime.waitForPort(4300); const calls: number[] = []; await new Promise<void>((resolve) => { const off = runtime.onListening((port) => { calls.push(port); off(); resolve(); }); }); await new Promise((r) => setTimeout(r, 50)); return calls; }); expect(seen).toEqual([4300]);});
// Node's `listen(options[, callback])` overload (net.Server.prototype.listen): an// options object in place of the bare port, which get-port-please and several// framework dev servers (e.g. @nuxt/cli's bindServer) pass. Found booting the Nuxt// dev server fixture, which failed with "options.port should be >= 0 and < 65536.// Received [object Object]" — listen() only destructured a bare numeric port.const OPTIONS_SERVER = `const http = require("node:http");const s = http.createServer((req, res) => res.end("ok"));s.listen({ port: 4400, host: "localhost", exclusive: true }, () => s.close());`;
test("listen() accepts an options object, as net.Server.prototype.listen does", async ({ page }) => { await page.goto("/demo/fixture.html"); await page.waitForFunction(() => window.__runtime !== undefined, undefined, { timeout: 20_000 }); const { port, exitCode } = await page.evaluate(async (src) => { const runtime = window.__runtime!; const got = new Promise<number>((resolve) => { const off = runtime.onListening((p) => { off(); resolve(p); }); }); runtime.writeTree({ "/app/listen-options.cjs": src }); const proc = runtime.exec("/app/listen-options.cjs"); const [port, exitCode] = await Promise.all([got, proc.exited]); return { port, exitCode }; }, OPTIONS_SERVER); expect(port).toBe(4400); expect(exitCode).toBe(0);});
test("listen(0) picks a free ephemeral port, as Vite's own port-0 handling (and @angular/build's checkPort) relies on", async ({ page }) => { await page.goto("/demo/fixture.html"); await page.waitForFunction(() => window.__runtime !== undefined, undefined, { timeout: 20_000 }); const result = await page.evaluate(async () => { const runtime = window.__runtime!; runtime.writeTree({ "/app/ephemeral.cjs": ` const http = require("node:http"); const server = http.createServer((req, res) => res.end("ok")); server.listen(0, () => { const { port } = server.address(); console.log("port", port, "nonzero", port !== 0, "ephemeral", port >= 49152 && port <= 65535); server.close(); }); `, }); const proc = runtime.exec("/app/ephemeral.cjs"); const dec = new TextDecoder(); let out = ""; await proc.stdout.pipeTo(new WritableStream({ write: (c) => { out += dec.decode(c); } })); return { code: await proc.exited, out: out.trim() }; }); expect(result.code).toBe(0); expect(result.out).toMatch(/^port \d+ nonzero true ephemeral true$/);});