105 lines
2.4 KiB
JavaScript
105 lines
2.4 KiB
JavaScript
'use strict';
|
|
|
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
|
require('./shims.js');
|
|
var _0SERVER = require('./server/index.js');
|
|
require('node:http');
|
|
require('node:https');
|
|
require('node:zlib');
|
|
require('node:stream');
|
|
require('node:util');
|
|
require('node:url');
|
|
require('net');
|
|
|
|
/**
|
|
* Splits headers into two categories: single value and multi value
|
|
* @param {Headers} headers
|
|
* @returns {{
|
|
* headers: Record<string, string>,
|
|
* multiValueHeaders: Record<string, string[]>
|
|
* }}
|
|
*/
|
|
function split_headers(headers) {
|
|
/** @type {Record<string, string>} */
|
|
const h = {};
|
|
|
|
/** @type {Record<string, string[]>} */
|
|
const m = {};
|
|
|
|
headers.forEach((value, key) => {
|
|
if (key === 'set-cookie') {
|
|
// @ts-expect-error (headers.raw() is non-standard)
|
|
m[key] = headers.raw()[key];
|
|
} else {
|
|
h[key] = value;
|
|
}
|
|
});
|
|
|
|
return {
|
|
headers: h,
|
|
multiValueHeaders: m
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @param {import('@sveltejs/kit').SSRManifest} manifest
|
|
* @returns {import('@netlify/functions').Handler}
|
|
*/
|
|
function init(manifest) {
|
|
const server = new _0SERVER.Server(manifest);
|
|
|
|
return async (event, context) => {
|
|
const rendered = await server.respond(to_request(event), {
|
|
platform: { context },
|
|
getClientAddress() {
|
|
return event.headers['x-nf-client-connection-ip'];
|
|
}
|
|
});
|
|
|
|
const partial_response = {
|
|
statusCode: rendered.status,
|
|
...split_headers(rendered.headers)
|
|
};
|
|
|
|
// TODO this is probably wrong now?
|
|
if (rendered.body instanceof Uint8Array) {
|
|
// Function responses should be strings (or undefined), and responses with binary
|
|
// content should be base64 encoded and set isBase64Encoded to true.
|
|
// https://github.com/netlify/functions/blob/main/src/function/response.ts
|
|
return {
|
|
...partial_response,
|
|
isBase64Encoded: true,
|
|
body: Buffer.from(rendered.body).toString('base64')
|
|
};
|
|
}
|
|
|
|
return {
|
|
...partial_response,
|
|
body: await rendered.text()
|
|
};
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @param {import('@netlify/functions').HandlerEvent} event
|
|
* @returns {Request}
|
|
*/
|
|
function to_request(event) {
|
|
const { httpMethod, headers, rawUrl, body, isBase64Encoded } = event;
|
|
|
|
/** @type {RequestInit} */
|
|
const init = {
|
|
method: httpMethod,
|
|
headers: new Headers(headers)
|
|
};
|
|
|
|
if (httpMethod !== 'GET' && httpMethod !== 'HEAD') {
|
|
const encoding = isBase64Encoded ? 'base64' : 'utf-8';
|
|
init.body = typeof body === 'string' ? Buffer.from(body, encoding) : body;
|
|
}
|
|
|
|
return new Request(rawUrl, init);
|
|
}
|
|
|
|
exports.init = init;
|