diff options
author | 2022-10-12 17:25:51 -0400 | |
---|---|---|
committer | 2022-10-12 17:25:51 -0400 | |
commit | e55af8a23233b6335f45b7a04b9d026990fb616c (patch) | |
tree | 62f47ae6e1fa56c04c045318c3a0d34674cb4a63 /packages/integrations/node/src/standalone.ts | |
parent | 2b7fb848bbe18942960c17a135c5a3769780512b (diff) | |
download | astro-e55af8a23233b6335f45b7a04b9d026990fb616c.tar.gz astro-e55af8a23233b6335f45b7a04b9d026990fb616c.tar.zst astro-e55af8a23233b6335f45b7a04b9d026990fb616c.zip |
Node.js standalone mode + support for astro preview (#5056)
* wip
* Deprecate buildConfig and move to config.build
* Implement the standalone server
* Stay backwards compat
* Add changesets
* correctly merge URLs
* Get config earlier
* update node tests
* Return the preview server
* update remaining tests
* swap usage and config ordering
* Update packages/astro/src/@types/astro.ts
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* Update .changeset/metal-pumas-walk.md
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* Update .changeset/metal-pumas-walk.md
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* Update .changeset/stupid-points-refuse.md
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* Update .changeset/stupid-points-refuse.md
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* Link to build.server config
Co-authored-by: Fred K. Schott <fkschott@gmail.com>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Diffstat (limited to 'packages/integrations/node/src/standalone.ts')
-rw-r--r-- | packages/integrations/node/src/standalone.ts | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/packages/integrations/node/src/standalone.ts b/packages/integrations/node/src/standalone.ts new file mode 100644 index 000000000..8fef96ed5 --- /dev/null +++ b/packages/integrations/node/src/standalone.ts @@ -0,0 +1,53 @@ +import type { NodeApp } from 'astro/app/node'; +import type { Options } from './types'; +import path from 'path'; +import { fileURLToPath } from 'url'; +import middleware from './middleware.js'; +import { createServer } from './http-server.js'; + +function resolvePaths(options: Options) { + const clientURLRaw = new URL(options.client); + const serverURLRaw = new URL(options.server); + const rel = path.relative(fileURLToPath(serverURLRaw), fileURLToPath(clientURLRaw)); + + const serverEntryURL = new URL(import.meta.url); + const clientURL = new URL(appendForwardSlash(rel), serverEntryURL); + + return { + client: clientURL, + }; +} + +function appendForwardSlash(pth: string) { + return pth.endsWith('/') ? pth : pth + '/'; +} + +export function getResolvedHostForHttpServer(host: string | boolean) { + if (host === false) { + // Use a secure default + return '127.0.0.1'; + } else if (host === true) { + // If passed --host in the CLI without arguments + return undefined; // undefined typically means 0.0.0.0 or :: (listen on all IPs) + } else { + return host; + } +} + +export default function startServer(app: NodeApp, options: Options) { + const port = process.env.PORT ? Number(process.env.port) : (options.port ?? 8080); + const { client } = resolvePaths(options); + const handler = middleware(app); + + const host = getResolvedHostForHttpServer(options.host); + const server = createServer({ + client, + port, + host, + }, handler); + + // eslint-disable-next-line no-console + console.log(`Server listening on http://${host}:${port}`); + + return server.closed(); +} |