summaryrefslogtreecommitdiff
path: root/packages/integrations/node/src/server.ts
diff options
context:
space:
mode:
authorGravatar Matthew Phillips <matthew@skypack.dev> 2022-10-12 17:25:51 -0400
committerGravatar GitHub <noreply@github.com> 2022-10-12 17:25:51 -0400
commite55af8a23233b6335f45b7a04b9d026990fb616c (patch)
tree62f47ae6e1fa56c04c045318c3a0d34674cb4a63 /packages/integrations/node/src/server.ts
parent2b7fb848bbe18942960c17a135c5a3769780512b (diff)
downloadastro-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/server.ts')
-rw-r--r--packages/integrations/node/src/server.ts53
1 files changed, 10 insertions, 43 deletions
diff --git a/packages/integrations/node/src/server.ts b/packages/integrations/node/src/server.ts
index 6ecd14931..202e66b7e 100644
--- a/packages/integrations/node/src/server.ts
+++ b/packages/integrations/node/src/server.ts
@@ -1,8 +1,9 @@
-import { polyfill } from '@astrojs/webapi';
import type { SSRManifest } from 'astro';
+import type { Options } from './types';
+import { polyfill } from '@astrojs/webapi';
import { NodeApp } from 'astro/app/node';
-import type { IncomingMessage, ServerResponse } from 'http';
-import type { Readable } from 'stream';
+import middleware from './middleware.js';
+import startServer from './standalone.js';
polyfill(globalThis, {
exclude: 'window document',
@@ -11,49 +12,15 @@ polyfill(globalThis, {
export function createExports(manifest: SSRManifest) {
const app = new NodeApp(manifest);
return {
- async handler(req: IncomingMessage, res: ServerResponse, next?: (err?: unknown) => void) {
- try {
- const route = app.match(req);
-
- if (route) {
- try {
- const response = await app.render(req);
- await writeWebResponse(app, res, response);
- } catch (err: unknown) {
- if (next) {
- next(err);
- } else {
- throw err;
- }
- }
- } else if (next) {
- return next();
- }
- } catch (err: unknown) {
- if (!res.headersSent) {
- res.writeHead(500, `Server error`);
- res.end();
- }
- }
- },
+ handler: middleware(app)
};
}
-async function writeWebResponse(app: NodeApp, res: ServerResponse, webResponse: Response) {
- const { status, headers, body } = webResponse;
-
- if (app.setCookieHeaders) {
- const setCookieHeaders: Array<string> = Array.from(app.setCookieHeaders(webResponse));
- if (setCookieHeaders.length) {
- res.setHeader('Set-Cookie', setCookieHeaders);
- }
+export function start(manifest: SSRManifest, options: Options) {
+ if(options.mode !== 'standalone' || process.env.ASTRO_NODE_AUTOSTART === 'disabled') {
+ return;
}
- res.writeHead(status, Object.fromEntries(headers.entries()));
- if (body) {
- for await (const chunk of body as unknown as Readable) {
- res.write(chunk);
- }
- }
- res.end();
+ const app = new NodeApp(manifest);
+ startServer(app, options);
}