2022-09-28 20:55:27 +00:00
|
|
|
import type { App } from 'astro/app';
|
2022-09-28 20:57:35 +00:00
|
|
|
import type { IncomingMessage, ServerResponse } from 'node:http';
|
2022-12-02 23:38:40 +00:00
|
|
|
import { splitCookiesString } from 'set-cookie-parser';
|
2022-03-30 02:01:37 +00:00
|
|
|
|
2023-03-09 16:27:16 +00:00
|
|
|
const clientAddressSymbol = Symbol.for('astro.clientAddress');
|
|
|
|
|
2022-03-30 02:01:37 +00:00
|
|
|
/*
|
|
|
|
Credits to the SvelteKit team
|
2023-03-01 19:00:52 +00:00
|
|
|
https://github.com/sveltejs/kit/blob/8d1ba04825a540324bc003e85f36559a594aadc2/packages/kit/src/exports/node/index.js
|
2022-03-30 02:01:37 +00:00
|
|
|
*/
|
|
|
|
|
2022-12-02 23:38:40 +00:00
|
|
|
function get_raw_body(req: IncomingMessage, body_size_limit?: number): ReadableStream | null {
|
|
|
|
const h = req.headers;
|
2022-03-30 02:01:37 +00:00
|
|
|
|
2022-12-02 23:38:40 +00:00
|
|
|
if (!h['content-type']) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-03-30 02:01:37 +00:00
|
|
|
|
2022-12-02 23:38:40 +00:00
|
|
|
const content_length = Number(h['content-length']);
|
|
|
|
|
|
|
|
// check if no request body
|
|
|
|
if (
|
|
|
|
(req.httpVersionMajor === 1 && isNaN(content_length) && h['transfer-encoding'] == null) ||
|
|
|
|
content_length === 0
|
|
|
|
) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-03-30 02:01:37 +00:00
|
|
|
|
2022-12-02 23:38:40 +00:00
|
|
|
let length = content_length;
|
2022-03-30 02:01:37 +00:00
|
|
|
|
2022-12-02 23:38:40 +00:00
|
|
|
if (body_size_limit) {
|
|
|
|
if (!length) {
|
|
|
|
length = body_size_limit;
|
|
|
|
} else if (length > body_size_limit) {
|
2023-03-01 19:00:52 +00:00
|
|
|
throw new HTTPError(
|
|
|
|
413,
|
2022-12-02 23:38:40 +00:00
|
|
|
`Received content-length of ${length}, but only accept up to ${body_size_limit} bytes.`
|
|
|
|
);
|
2022-03-30 02:01:37 +00:00
|
|
|
}
|
2022-12-02 23:38:40 +00:00
|
|
|
}
|
2022-03-30 02:01:37 +00:00
|
|
|
|
2022-12-02 23:38:40 +00:00
|
|
|
if (req.destroyed) {
|
|
|
|
const readable = new ReadableStream();
|
|
|
|
readable.cancel();
|
|
|
|
return readable;
|
|
|
|
}
|
2022-03-30 02:01:37 +00:00
|
|
|
|
2022-12-02 23:38:40 +00:00
|
|
|
let size = 0;
|
|
|
|
let cancelled = false;
|
2022-03-30 02:01:37 +00:00
|
|
|
|
2022-12-02 23:38:40 +00:00
|
|
|
return new ReadableStream({
|
|
|
|
start(controller) {
|
|
|
|
req.on('error', (error) => {
|
|
|
|
cancelled = true;
|
|
|
|
controller.error(error);
|
|
|
|
});
|
2022-03-30 02:01:37 +00:00
|
|
|
|
2022-12-02 23:38:40 +00:00
|
|
|
req.on('end', () => {
|
|
|
|
if (cancelled) return;
|
|
|
|
controller.close();
|
2022-03-30 02:01:37 +00:00
|
|
|
});
|
2022-12-02 23:38:40 +00:00
|
|
|
|
2022-03-30 02:01:37 +00:00
|
|
|
req.on('data', (chunk) => {
|
2022-12-02 23:38:40 +00:00
|
|
|
if (cancelled) return;
|
|
|
|
|
|
|
|
size += chunk.length;
|
|
|
|
if (size > length) {
|
|
|
|
cancelled = true;
|
|
|
|
controller.error(
|
2023-03-01 19:00:52 +00:00
|
|
|
new HTTPError(
|
|
|
|
413,
|
2022-12-02 23:38:40 +00:00
|
|
|
`request body size exceeded ${
|
|
|
|
content_length ? "'content-length'" : 'BODY_SIZE_LIMIT'
|
|
|
|
} of ${length}`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
controller.enqueue(chunk);
|
|
|
|
|
|
|
|
if (controller.desiredSize === null || controller.desiredSize <= 0) {
|
|
|
|
req.pause();
|
|
|
|
}
|
2022-03-30 02:01:37 +00:00
|
|
|
});
|
2022-12-02 23:38:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
pull() {
|
|
|
|
req.resume();
|
|
|
|
},
|
2022-03-30 02:01:37 +00:00
|
|
|
|
2022-12-02 23:38:40 +00:00
|
|
|
cancel(reason) {
|
|
|
|
cancelled = true;
|
|
|
|
req.destroy(reason);
|
|
|
|
},
|
2022-03-30 02:01:37 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-02 23:38:40 +00:00
|
|
|
export async function getRequest(
|
|
|
|
base: string,
|
|
|
|
req: IncomingMessage,
|
|
|
|
bodySizeLimit?: number
|
|
|
|
): Promise<Request> {
|
2023-03-09 16:27:16 +00:00
|
|
|
let headers = req.headers as Record<string, string>;
|
|
|
|
let request = new Request(base + req.url, {
|
2023-03-01 19:00:52 +00:00
|
|
|
// @ts-expect-error
|
|
|
|
duplex: 'half',
|
2022-03-30 02:01:37 +00:00
|
|
|
method: req.method,
|
2023-03-09 16:27:16 +00:00
|
|
|
headers,
|
2022-12-02 23:38:40 +00:00
|
|
|
body: get_raw_body(req, bodySizeLimit),
|
2022-03-30 02:01:37 +00:00
|
|
|
});
|
2023-03-09 16:27:16 +00:00
|
|
|
Reflect.set(request, clientAddressSymbol, headers['x-forwarded-for']);
|
|
|
|
return request;
|
2022-03-30 02:01:37 +00:00
|
|
|
}
|
|
|
|
|
2023-03-01 19:00:52 +00:00
|
|
|
export async function setResponse(
|
|
|
|
app: App,
|
|
|
|
res: ServerResponse,
|
|
|
|
response: Response
|
|
|
|
): Promise<void> {
|
2022-03-30 02:01:37 +00:00
|
|
|
const headers = Object.fromEntries(response.headers);
|
2022-12-02 23:38:40 +00:00
|
|
|
let cookies: string[] = [];
|
2022-03-30 02:01:37 +00:00
|
|
|
|
|
|
|
if (response.headers.has('set-cookie')) {
|
2022-12-02 23:38:40 +00:00
|
|
|
const header = response.headers.get('set-cookie')!;
|
|
|
|
const split = splitCookiesString(header);
|
|
|
|
cookies = split;
|
2022-03-30 02:01:37 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 20:57:35 +00:00
|
|
|
if (app.setCookieHeaders) {
|
2023-03-01 19:00:52 +00:00
|
|
|
for (const setCookieHeader of app.setCookieHeaders(response)) {
|
|
|
|
cookies.push(setCookieHeader);
|
|
|
|
}
|
2022-09-28 20:55:27 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 23:38:40 +00:00
|
|
|
res.writeHead(response.status, { ...headers, 'set-cookie': cookies });
|
2022-03-30 02:01:37 +00:00
|
|
|
|
2022-12-02 23:38:40 +00:00
|
|
|
if (!response.body) {
|
|
|
|
res.end();
|
|
|
|
return;
|
|
|
|
}
|
2022-03-30 02:01:37 +00:00
|
|
|
|
2022-12-02 23:38:40 +00:00
|
|
|
if (response.body.locked) {
|
|
|
|
res.write(
|
|
|
|
'Fatal error: Response body is locked. ' +
|
|
|
|
`This can happen when the response was already read (for example through 'response.json()' or 'response.text()').`
|
|
|
|
);
|
2022-03-30 02:01:37 +00:00
|
|
|
res.end();
|
2022-12-02 23:38:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const reader = response.body.getReader();
|
|
|
|
|
|
|
|
if (res.destroyed) {
|
|
|
|
reader.cancel();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const cancel = (error?: Error) => {
|
|
|
|
res.off('close', cancel);
|
|
|
|
res.off('error', cancel);
|
|
|
|
|
|
|
|
// If the reader has already been interrupted with an error earlier,
|
|
|
|
// then it will appear here, it is useless, but it needs to be catch.
|
|
|
|
reader.cancel(error).catch(() => {});
|
|
|
|
if (error) res.destroy(error);
|
|
|
|
};
|
|
|
|
|
|
|
|
res.on('close', cancel);
|
|
|
|
res.on('error', cancel);
|
|
|
|
|
|
|
|
next();
|
|
|
|
async function next() {
|
|
|
|
try {
|
|
|
|
for (;;) {
|
|
|
|
const { done, value } = await reader.read();
|
|
|
|
|
|
|
|
if (done) break;
|
|
|
|
|
|
|
|
if (!res.write(value)) {
|
|
|
|
res.once('drain', next);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.end();
|
|
|
|
} catch (error) {
|
|
|
|
cancel(error instanceof Error ? error : new Error(String(error)));
|
|
|
|
}
|
2022-03-30 02:01:37 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-01 19:00:52 +00:00
|
|
|
|
|
|
|
class HTTPError extends Error {
|
|
|
|
status: number;
|
|
|
|
|
|
|
|
constructor(status: number, reason: string) {
|
|
|
|
super(reason);
|
|
|
|
this.status = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
get reason() {
|
|
|
|
return super.message;
|
|
|
|
}
|
|
|
|
}
|