2022-06-06 16:49:53 +00:00
|
|
|
import { polyfill } from '@astrojs/webapi';
|
2022-03-25 16:08:51 +00:00
|
|
|
import type { Handler } from '@netlify/functions';
|
2022-06-06 16:49:53 +00:00
|
|
|
import { SSRManifest } from 'astro';
|
2022-03-25 16:08:02 +00:00
|
|
|
import { App } from 'astro/app';
|
|
|
|
|
|
|
|
polyfill(globalThis, {
|
|
|
|
exclude: 'window document',
|
|
|
|
});
|
|
|
|
|
2022-06-15 19:49:09 +00:00
|
|
|
export interface Args {
|
|
|
|
binaryMediaTypes?: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseContentType(header?: string) {
|
|
|
|
return header?.split(';')[0] ?? '';
|
|
|
|
}
|
2022-03-25 16:08:02 +00:00
|
|
|
|
2022-07-19 20:10:15 +00:00
|
|
|
const clientAddressSymbol = Symbol.for('astro.clientAddress');
|
|
|
|
|
2022-03-25 16:08:02 +00:00
|
|
|
export const createExports = (manifest: SSRManifest, args: Args) => {
|
|
|
|
const app = new App(manifest);
|
|
|
|
|
2022-06-15 19:49:09 +00:00
|
|
|
const binaryMediaTypes = args.binaryMediaTypes ?? [];
|
|
|
|
const knownBinaryMediaTypes = new Set([
|
|
|
|
'audio/3gpp',
|
|
|
|
'audio/3gpp2',
|
|
|
|
'audio/aac',
|
|
|
|
'audio/midi',
|
|
|
|
'audio/mpeg',
|
|
|
|
'audio/ogg',
|
|
|
|
'audio/opus',
|
|
|
|
'audio/wav',
|
|
|
|
'audio/webm',
|
|
|
|
'audio/x-midi',
|
|
|
|
'image/avif',
|
|
|
|
'image/bmp',
|
|
|
|
'image/gif',
|
|
|
|
'image/vnd.microsoft.icon',
|
|
|
|
'image/jpeg',
|
|
|
|
'image/png',
|
|
|
|
'image/svg+xml',
|
|
|
|
'image/tiff',
|
|
|
|
'image/webp',
|
|
|
|
'video/3gpp',
|
|
|
|
'video/3gpp2',
|
|
|
|
'video/mp2t',
|
|
|
|
'video/mp4',
|
|
|
|
'video/mpeg',
|
|
|
|
'video/ogg',
|
|
|
|
'video/x-msvideo',
|
|
|
|
'video/webm',
|
|
|
|
...binaryMediaTypes,
|
|
|
|
]);
|
|
|
|
|
2022-03-25 16:08:02 +00:00
|
|
|
const handler: Handler = async (event) => {
|
2022-04-10 21:34:49 +00:00
|
|
|
const { httpMethod, headers, rawUrl, body: requestBody, isBase64Encoded } = event;
|
|
|
|
const init: RequestInit = {
|
|
|
|
method: httpMethod,
|
|
|
|
headers: new Headers(headers as any),
|
|
|
|
};
|
|
|
|
// Attach the event body the the request, with proper encoding.
|
|
|
|
if (httpMethod !== 'GET' && httpMethod !== 'HEAD') {
|
|
|
|
const encoding = isBase64Encoded ? 'base64' : 'utf-8';
|
|
|
|
init.body =
|
|
|
|
typeof requestBody === 'string' ? Buffer.from(requestBody, encoding) : requestBody;
|
|
|
|
}
|
|
|
|
const request = new Request(rawUrl, init);
|
2022-03-25 16:08:02 +00:00
|
|
|
|
2022-07-22 20:30:17 +00:00
|
|
|
let routeData = app.match(request, { matchNotFound: true });
|
|
|
|
|
|
|
|
if (!routeData) {
|
2022-03-25 16:08:02 +00:00
|
|
|
return {
|
|
|
|
statusCode: 404,
|
2022-03-25 16:08:51 +00:00
|
|
|
body: 'Not found',
|
2022-03-25 16:08:02 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-19 20:10:15 +00:00
|
|
|
const ip = headers['x-nf-client-connection-ip'];
|
|
|
|
Reflect.set(request, clientAddressSymbol, ip);
|
|
|
|
|
2022-07-22 20:30:17 +00:00
|
|
|
const response: Response = await app.render(request, routeData);
|
2022-04-12 20:50:10 +00:00
|
|
|
const responseHeaders = Object.fromEntries(response.headers.entries());
|
2022-06-15 19:50:36 +00:00
|
|
|
|
2022-06-15 19:49:09 +00:00
|
|
|
const responseContentType = parseContentType(responseHeaders['content-type']);
|
|
|
|
const responseIsBase64Encoded = knownBinaryMediaTypes.has(responseContentType);
|
|
|
|
|
|
|
|
const responseBody = responseIsBase64Encoded
|
|
|
|
? Buffer.from(await response.text(), 'binary').toString('base64')
|
|
|
|
: await response.text();
|
2022-06-15 19:50:36 +00:00
|
|
|
|
2022-04-12 20:50:10 +00:00
|
|
|
const fnResponse: any = {
|
|
|
|
statusCode: response.status,
|
|
|
|
headers: responseHeaders,
|
2022-04-10 21:34:49 +00:00
|
|
|
body: responseBody,
|
2022-06-15 19:49:09 +00:00
|
|
|
isBase64Encoded: responseIsBase64Encoded,
|
2022-03-25 16:08:02 +00:00
|
|
|
};
|
2022-04-12 20:50:10 +00:00
|
|
|
|
|
|
|
// Special-case set-cookie which has to be set an different way :/
|
|
|
|
// The fetch API does not have a way to get multiples of a single header, but instead concatenates
|
|
|
|
// them. There are non-standard ways to do it, and node-fetch gives us headers.raw()
|
|
|
|
// See https://github.com/whatwg/fetch/issues/973 for discussion
|
|
|
|
if (response.headers.has('set-cookie') && 'raw' in response.headers) {
|
|
|
|
// Node fetch allows you to get the raw headers, which includes multiples of the same type.
|
|
|
|
// This is needed because Set-Cookie *must* be called for each cookie, and can't be
|
|
|
|
// concatenated together.
|
|
|
|
type HeadersWithRaw = Headers & {
|
|
|
|
raw: () => Record<string, string[]>;
|
|
|
|
};
|
|
|
|
|
|
|
|
const rawPacked = (response.headers as HeadersWithRaw).raw();
|
2022-04-12 20:50:59 +00:00
|
|
|
if ('set-cookie' in rawPacked) {
|
2022-04-12 20:50:10 +00:00
|
|
|
fnResponse.multiValueHeaders = {
|
2022-04-12 20:50:59 +00:00
|
|
|
'set-cookie': rawPacked['set-cookie'],
|
|
|
|
};
|
2022-04-12 20:50:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fnResponse;
|
2022-03-25 16:08:51 +00:00
|
|
|
};
|
2022-03-25 16:08:02 +00:00
|
|
|
|
|
|
|
return { handler };
|
|
|
|
};
|