From b47dcaa25968ec85ba96fce23381c94a94e389f6 Mon Sep 17 00:00:00 2001 From: Satanshu Mishra Date: Fri, 1 Mar 2024 00:32:22 -0800 Subject: [PATCH] fix(node): listen on 0.0.0.0 if server.host is set to true (#10282) Co-authored-by: Arsh <69170106+lilnasy@users.noreply.github.com> Co-authored-by: Kevin Zuniga Cuellar <46791833+kevinzunigacuellar@users.noreply.github.com> Co-authored-by: Florian Lefebvre --- .changeset/smooth-singers-kiss.md | 5 +++++ packages/integrations/node/src/standalone.ts | 12 ++++++++--- .../node/test/server-host.test.js | 21 +++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 .changeset/smooth-singers-kiss.md create mode 100644 packages/integrations/node/test/server-host.test.js diff --git a/.changeset/smooth-singers-kiss.md b/.changeset/smooth-singers-kiss.md new file mode 100644 index 000000000000..56d1ea893af4 --- /dev/null +++ b/.changeset/smooth-singers-kiss.md @@ -0,0 +1,5 @@ +--- +"@astrojs/node": patch +--- + +Fixes the `server.host` option to properly listen on all network interfaces when set to `true` diff --git a/packages/integrations/node/src/standalone.ts b/packages/integrations/node/src/standalone.ts index 35f1ee8d8f96..9567e8ab4c8d 100644 --- a/packages/integrations/node/src/standalone.ts +++ b/packages/integrations/node/src/standalone.ts @@ -9,11 +9,17 @@ import { createAppHandler } from './serve-app.js'; import { createStaticHandler } from './serve-static.js'; import type { Options } from './types.js'; +// Used to get Host Value at Runtime +export const hostOptions = (host: Options["host"]): string => { + if (typeof host === 'boolean') { + return host ? '0.0.0.0' : 'localhost'; + } + return host; +}; + export default function standalone(app: NodeApp, options: Options) { const port = process.env.PORT ? Number(process.env.PORT) : options.port ?? 8080; - // Allow to provide host value at runtime - const hostOptions = typeof options.host === 'boolean' ? 'localhost' : options.host; - const host = process.env.HOST ?? hostOptions; + const host = process.env.HOST ?? hostOptions(options.host); const handler = createStandaloneHandler(app, options); const server = createServer(handler, host, port); server.server.listen(port, host); diff --git a/packages/integrations/node/test/server-host.test.js b/packages/integrations/node/test/server-host.test.js new file mode 100644 index 000000000000..4c987ab23c51 --- /dev/null +++ b/packages/integrations/node/test/server-host.test.js @@ -0,0 +1,21 @@ +import { describe, it } from 'node:test'; +import * as assert from 'node:assert/strict'; +import { hostOptions } from '../dist/standalone.js'; + +describe('host', () => { + it('returns "0.0.0.0" when host is true', () => { + const options = { host: true }; + assert.equal(hostOptions(options.host), '0.0.0.0'); + }); + + it('returns "localhost" when host is false', () => { + const options = { host: false }; + assert.equal(hostOptions(options.host), 'localhost'); + }); + + it('returns the value of host when host is a string', () => { + const host = "1.1.1.1" + const options = { host }; + assert.equal(hostOptions(options.host), host); + }); +});