4dd6c7900c
* fix: res.writeHead is not a function * fix: handler params type added * fix: handler function params error * Update packages/integrations/node/src/nodeMiddleware.ts --------- Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
81 lines
2.2 KiB
TypeScript
81 lines
2.2 KiB
TypeScript
import type { NodeApp } from 'astro/app/node';
|
|
import type { ServerResponse } from 'node:http';
|
|
import type { Readable } from 'stream';
|
|
import { createOutgoingHttpHeaders } from './createOutgoingHttpHeaders';
|
|
import { responseIterator } from './response-iterator';
|
|
import type { ErrorHandlerParams, Options, RequestHandlerParams } from './types';
|
|
|
|
// Disable no-unused-vars to avoid breaking signature change
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
export default function (app: NodeApp, _mode: Options['mode']) {
|
|
return async function (...args: RequestHandlerParams | ErrorHandlerParams) {
|
|
let error = null;
|
|
let [req, res, next, locals] = args as RequestHandlerParams;
|
|
|
|
if (args[0] instanceof Error) {
|
|
[error, req, res, next, locals] = args as ErrorHandlerParams;
|
|
|
|
if (error) {
|
|
if (next) {
|
|
return next(error);
|
|
} else {
|
|
throw error;
|
|
}
|
|
}
|
|
}
|
|
|
|
try {
|
|
const route = app.match(req);
|
|
if (route) {
|
|
try {
|
|
const response = await app.render(req, route, locals);
|
|
await writeWebResponse(app, res, response);
|
|
} catch (err: unknown) {
|
|
if (next) {
|
|
next(err);
|
|
} else {
|
|
throw err;
|
|
}
|
|
}
|
|
} else if (next) {
|
|
return next();
|
|
} else {
|
|
const response = await app.render(req);
|
|
await writeWebResponse(app, res, response);
|
|
}
|
|
} catch (err: unknown) {
|
|
if (!res.headersSent) {
|
|
res.writeHead(500, `Server error`);
|
|
res.end();
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
async function writeWebResponse(app: NodeApp, res: ServerResponse, webResponse: Response) {
|
|
const { status, headers } = webResponse;
|
|
|
|
if (app.setCookieHeaders) {
|
|
const setCookieHeaders: Array<string> = Array.from(app.setCookieHeaders(webResponse));
|
|
|
|
if (setCookieHeaders.length) {
|
|
for (const setCookieHeader of setCookieHeaders) {
|
|
webResponse.headers.append('set-cookie', setCookieHeader);
|
|
}
|
|
}
|
|
}
|
|
|
|
const nodeHeaders = createOutgoingHttpHeaders(headers);
|
|
res.writeHead(status, nodeHeaders);
|
|
if (webResponse.body) {
|
|
try {
|
|
for await (const chunk of responseIterator(webResponse) as unknown as Readable) {
|
|
res.write(chunk);
|
|
}
|
|
} catch (err: any) {
|
|
console.error(err?.stack || err?.message || String(err));
|
|
res.write('Internal server error');
|
|
}
|
|
}
|
|
res.end();
|
|
}
|