-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathserverless.js
94 lines (80 loc) · 2.47 KB
/
serverless.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import './shims';
import { Server } from '0SERVER';
import { split_headers } from './headers.js';
import { createReadableStream } from '@sveltejs/kit/node';
import process from 'node:process';
/**
* @param {import('@sveltejs/kit').SSRManifest} manifest
* @returns {import('@netlify/functions').Handler}
*/
export function init(manifest) {
const server = new Server(manifest);
let init_promise = server.init({
env: process.env,
read: (file) => createReadableStream(`.netlify/server/${file}`)
});
return async (event, context) => {
if (init_promise !== null) {
await init_promise;
init_promise = null;
}
const response = await server.respond(to_request(event), {
platform: { context },
getClientAddress() {
return event.headers['x-nf-client-connection-ip'];
}
});
const partial_response = {
statusCode: response.status,
...split_headers(response.headers)
};
if (!is_text(response.headers.get('content-type'))) {
// 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(await response.arrayBuffer()).toString('base64')
};
}
return {
...partial_response,
body: await response.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);
}
const text_types = new Set([
'application/xml',
'application/json',
'application/x-www-form-urlencoded',
'multipart/form-data'
]);
/**
* Decides how the body should be parsed based on its mime type
*
* @param {string | undefined | null} content_type The `content-type` header of a request/response.
* @returns {boolean}
*/
function is_text(content_type) {
if (!content_type) return true; // defaults to json
const type = content_type.split(';')[0].toLowerCase(); // get the mime type
return type.startsWith('text/') || type.endsWith('+xml') || text_types.has(type);
}