-
Notifications
You must be signed in to change notification settings - Fork 34
/
handler.js
52 lines (46 loc) · 1.48 KB
/
handler.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
// TODO: hardcoding the relative location makes this brittle
// @ts-expect-error
import {init, render} from '../output/server/app.js';
init();
/**
* Firebase Cloud Function handler for SvelteKit
*
* This function converts the Firebase Cloud Function (Express.js) Request object
* into a format consumable to the SvelteKit render() function
*
* Relevant documentation - https://firebase.google.com/docs/functions/http-events#read_values_from_the_request
*
* @param {import('firebase-functions').https.Request} request
* @param {import('express').Response} response
* @returns {Promise<void>}
*/
async function svelteKit(request, response) {
const rendered = await render(toSvelteKitRequest(request));
return rendered ?
response.writeHead(rendered.status, rendered.headers).end(rendered.body) :
response.writeHead(404, 'Not Found').end();
}
/**
* @param {import('firebase-functions').https.Request} request
* @return {import('@sveltejs/kit/types/internal').Incoming}
*/
function toSvelteKitRequest(request) {
const host = `${request.headers['x-forwarded-proto']}://${request.headers.host}`;
const {pathname, searchParams: searchParameters} = new URL(
request.url || '',
host
);
return {
// Incoming
method: request.method,
headers: request.headers,
rawBody: new Uint8Array(request.rawBody),
// Body: request.body, Why does SvelteKit not use body?
// Location
host,
path: pathname,
params: request.params,
query: searchParameters
};
}
export default svelteKit;