2022-10-12 21:25:51 +00:00
|
|
|
import type { NodeApp } from 'astro/app/node';
|
2023-08-01 22:10:29 +00:00
|
|
|
import type { 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-08-01 22:10:29 +00:00
|
|
|
import type { ErrorHandlerParams, Options, RequestHandlerParams } 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
|
2023-08-21 16:29:38 +00:00
|
|
|
export default function (app: NodeApp, mode: Options['mode']) {
|
2023-08-01 22:10:29 +00:00
|
|
|
return async function (...args: RequestHandlerParams | ErrorHandlerParams) {
|
|
|
|
let error = null;
|
2023-08-21 16:29:38 +00:00
|
|
|
let locals;
|
|
|
|
let [req, res, next] = args as RequestHandlerParams;
|
|
|
|
if (mode === 'middleware') {
|
|
|
|
let { [3]: _locals } = args;
|
|
|
|
locals = _locals;
|
|
|
|
}
|
2023-08-01 22:10:29 +00:00
|
|
|
|
|
|
|
if (args[0] instanceof Error) {
|
2023-08-21 16:29:38 +00:00
|
|
|
[error, req, res, next] = args as ErrorHandlerParams;
|
|
|
|
if (mode === 'middleware') {
|
|
|
|
let { [4]: _locals } = args as ErrorHandlerParams;
|
|
|
|
locals = _locals;
|
|
|
|
}
|
2023-08-01 22:10:29 +00:00
|
|
|
if (error) {
|
|
|
|
if (next) {
|
|
|
|
return next(error);
|
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|