From 3e48ce2e21c58324676a6df5dc67281cb243dc97 Mon Sep 17 00:00:00 2001 From: Miles Richardson Date: Mon, 29 Aug 2022 19:32:54 +0100 Subject: [PATCH] fix(PreviewServer): serialize IPv6 host correctly in URL * Ensure IPv6 host is surrounded by square brackets when serialized into a URL * Use the Node-provided `family` property of the address returned by `.listen()` to check whether the address is IPv6, instead of relying on guesswork of looking at the IP --- src/server/PreviewServer.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/server/PreviewServer.ts b/src/server/PreviewServer.ts index b6db1da..45c24d6 100644 --- a/src/server/PreviewServer.ts +++ b/src/server/PreviewServer.ts @@ -109,8 +109,12 @@ export class PreviewServer { this.log('establishing server connection...') const connection = this.app.listen(port, host, () => { - const { address, port } = connection.address() as AddressInfo - const url = `http://${address}:${port}` + const { address, port, family } = connection.address() as AddressInfo + // IPv6 host requires surrounding square brackets when serialized to URL + // note: IPv6 host can take many forms, e.g. `::` and `::1` are both ok + const url = `http://${ + family === 'IPv6' ? `[${address}]` : address + }:${port}` this.connectionInfo = { port, host: address,