Skip to content

Commit

Permalink
fix: use same host for websocket server as for http server when previ…
Browse files Browse the repository at this point in the history
…ewing locally (#1309)
  • Loading branch information
f3ng3r authored Oct 26, 2023
1 parent 1510e47 commit 73d96a2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-insects-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@redocly/cli': patch
---

The `--host/-h` argument in the `preview-docs` command is now also used by the WebSocket server for hot reloading.
21 changes: 19 additions & 2 deletions packages/cli/src/commands/preview-docs/preview-server/hot.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
(function run() {
const Socket = window.SimpleWebsocket;
const port = window.__OPENAPI_CLI_WS_PORT;
const host = window.__OPENAPI_CLI_WS_HOST;

let socket;

reconnect();

function getFormattedHost() {
// Use localhost when bound to all interfaces
if (host === '::' || host === '0.0.0.0') {
return 'localhost';
}

// Other IPv6 addresses must be wrapped in brackets
if (host.includes('::')) {
return `[${host}]`;
}

// Otherwise return as-is
return host;
}

function reconnect() {
socket = new Socket(`ws://127.0.0.1:${port}`);
socket = new Socket(`ws://${getFormattedHost()}:${port}`);
socket.on('connect', () => {
socket.send('{"type": "ping"}');
});
Expand All @@ -29,13 +45,14 @@

socket.on('close', () => {
socket.destroy();
console.log('Connection lost, trying to reconnect in 4s');
console.log('[hot] Connection lost, trying to reconnect in 4s');
setTimeout(() => {
reconnect();
}, 4000);
});

socket.on('error', () => {
console.log('[hot] Error connecting to hot reloading server');
socket.destroy();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ function getPageHTML(
htmlTemplate: string,
redocOptions: object = {},
useRedocPro: boolean,
wsPort: number
wsPort: number,
host: string
) {
let templateSrc = readFileSync(htmlTemplate, 'utf-8');

Expand All @@ -28,6 +29,7 @@ function getPageHTML(
<script>
window.__REDOC_EXPORT = '${useRedocPro ? 'RedoclyReferenceDocs' : 'Redoc'}';
window.__OPENAPI_CLI_WS_PORT = ${wsPort};
window.__OPENAPI_CLI_WS_HOST = "${host}";
</script>
<script src="/simplewebsocket.min.js"></script>
<script src="/hot.js"></script>
Expand Down Expand Up @@ -67,7 +69,7 @@ export default async function startPreviewServer(

if (request.url?.endsWith('/') || path.extname(request.url!) === '') {
respondWithGzip(
getPageHTML(htmlTemplate || defaultTemplate, getOptions(), useRedocPro, wsPort),
getPageHTML(htmlTemplate || defaultTemplate, getOptions(), useRedocPro, wsPort, host),
request,
response,
{
Expand Down Expand Up @@ -143,7 +145,7 @@ export default async function startPreviewServer(
console.timeEnd(colorette.dim(`GET ${request.url}`));
};

const wsPort = await getPort({ portRange: [32201, 32301] });
const wsPort = await getPort({ port: 32201, portRange: [32201, 32301], host });

const server = startHttpServer(port, host, handler);
server.on('listening', () => {
Expand All @@ -152,5 +154,5 @@ export default async function startPreviewServer(
);
});

return startWsServer(wsPort);
return startWsServer(wsPort, host);
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export function startHttpServer(port: number, host: string, handler: http.Reques
return http.createServer(handler).listen(port, host);
}

export function startWsServer(port: number) {
const socketServer = new SocketServer({ port, clientTracking: true });
export function startWsServer(port: number, host: string) {
const socketServer = new SocketServer({ port, host, clientTracking: true });

socketServer.on('connection', (socket: any) => {
socket.on('data', (data: string) => {
Expand Down

1 comment on commit 73d96a2

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements 76.08% 4045/5317
🟡 Branches 65.9% 2145/3255
🟡 Functions 68.26% 656/961
🟡 Lines 76.27% 3793/4973

Test suite run success

645 tests passing in 93 suites.

Report generated by 🧪jest coverage report action from 73d96a2

Please sign in to comment.