2022-10-12 21:25:51 +00:00
|
|
|
import type { NodeApp } from 'astro/app/node';
|
2023-07-18 00:17:59 +00:00
|
|
|
import type { IncomingMessage, ServerResponse } from 'node:http';
|
2022-10-12 21:25:51 +00:00
|
|
|
import type { Readable } from 'stream';
|
2023-06-06 15:11:47 +00:00
|
|
|
import { createOutgoingHttpHeaders } from './createOutgoingHttpHeaders';
|
2022-11-16 21:55:15 +00:00
|
|
|
import { responseIterator } from './response-iterator';
|
2023-01-10 16:59:20 +00:00
|
|
|
import type { Options } from './types';
|
2022-10-12 21:25:51 +00:00
|
|
|
|
2023-08-01 14:52:16 +00:00
|
|
|
// 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']) {
|
2022-10-12 21:27:56 +00:00
|
|
|
return async function (
|
|
|
|
req: IncomingMessage,
|
|
|
|
res: ServerResponse,
|
2023-06-21 12:07:16 +00:00
|
|
|
next?: (err?: unknown) => void,
|
|
|
|
locals?: object
|
2022-10-12 21:27:56 +00:00
|
|
|
) {
|
2022-10-12 21:25:51 +00:00
|
|
|
try {
|
2023-08-01 14:52:16 +00:00
|
|
|
const route = app.match(req);
|
2022-10-12 21:25:51 +00:00
|
|
|
if (route) {
|
|
|
|
try {
|
2023-06-21 12:07:16 +00:00
|
|
|
const response = await app.render(req, route, locals);
|
2022-10-12 21:25:51 +00:00
|
|
|
await writeWebResponse(app, res, response);
|
|
|
|
} catch (err: unknown) {
|
|
|
|
if (next) {
|
|
|
|
next(err);
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (next) {
|
|
|
|
return next();
|
|
|
|
} else {
|
2023-08-01 14:52:16 +00:00
|
|
|
const response = await app.render(req);
|
|
|
|
await writeWebResponse(app, res, response);
|
2022-10-12 21:25:51 +00:00
|
|
|
}
|
|
|
|
} catch (err: unknown) {
|
|
|
|
if (!res.headersSent) {
|
|
|
|
res.writeHead(500, `Server error`);
|
|
|
|
res.end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function writeWebResponse(app: NodeApp, res: ServerResponse, webResponse: Response) {
|
2022-11-16 21:55:15 +00:00
|
|
|
const { status, headers } = webResponse;
|
2022-10-12 21:25:51 +00:00
|
|
|
|
|
|
|
if (app.setCookieHeaders) {
|
|
|
|
const setCookieHeaders: Array<string> = Array.from(app.setCookieHeaders(webResponse));
|
2023-06-06 15:11:47 +00:00
|
|
|
|
2022-10-12 21:25:51 +00:00
|
|
|
if (setCookieHeaders.length) {
|
2023-06-06 15:11:47 +00:00
|
|
|
for (const setCookieHeader of setCookieHeaders) {
|
|
|
|
webResponse.headers.append('set-cookie', setCookieHeader);
|
|
|
|
}
|
2022-10-12 21:25:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-06 15:11:47 +00:00
|
|
|
const nodeHeaders = createOutgoingHttpHeaders(headers);
|
|
|
|
res.writeHead(status, nodeHeaders);
|
2022-11-16 21:55:15 +00:00
|
|
|
if (webResponse.body) {
|
2023-05-01 14:08:18 +00:00
|
|
|
try {
|
|
|
|
for await (const chunk of responseIterator(webResponse) as unknown as Readable) {
|
|
|
|
res.write(chunk);
|
|
|
|
}
|
2023-05-01 14:11:30 +00:00
|
|
|
} catch (err: any) {
|
|
|
|
console.error(err?.stack || err?.message || String(err));
|
2023-05-01 14:08:18 +00:00
|
|
|
res.write('Internal server error');
|
2022-10-12 21:25:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
res.end();
|
|
|
|
}
|