-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathhandler.js
More file actions
117 lines (101 loc) · 2.86 KB
/
handler.js
File metadata and controls
117 lines (101 loc) · 2.86 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import './shims';
import fs from 'fs';
import path from 'path';
import sirv from 'sirv';
import { fileURLToPath } from 'url';
import { getRequest, setResponse } from '@sveltejs/kit/node';
import { Server } from 'SERVER';
import { manifest } from 'MANIFEST';
/* global ORIGIN, ADDRESS_HEADER, PROTOCOL_HEADER, HOST_HEADER, X_FORWARDED_FOR_INDEX */
const server = new Server(manifest);
const origin = ORIGIN;
const address_header = ADDRESS_HEADER && (process.env[ADDRESS_HEADER] || '').toLowerCase();
const protocol_header = PROTOCOL_HEADER && process.env[PROTOCOL_HEADER];
const host_header = (HOST_HEADER && process.env[HOST_HEADER]) || 'host';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
/**
* @param {string} path
* @param {number} max_age
* @param {boolean} immutable
*/
function serve(path, max_age, immutable = false) {
return (
fs.existsSync(path) &&
sirv(path, {
etag: true,
maxAge: max_age,
immutable,
gzip: true,
brotli: true
})
);
}
/** @type {import('polka').Middleware} */
const ssr = async (req, res) => {
let request;
try {
request = await getRequest(origin || get_origin(req.headers), req);
} catch (err) {
res.statusCode = err.status || 400;
return res.end(err.reason || 'Invalid request body');
}
if (address_header && !(address_header in req.headers)) {
throw new Error(
`Address header was specified with ${ADDRESS_HEADER}=${process.env[ADDRESS_HEADER]} but is absent from request`
);
}
setResponse(
res,
await server.respond(request, {
getClientAddress: () => {
if (address_header) {
const value = /** @type {string} */ (req.headers[address_header]) || '';
if (address_header === 'x-forwarded-for') {
const addresses = value.split(',');
return addresses[(addresses.length + X_FORWARDED_FOR_INDEX) % addresses.length].trim();
}
return value;
}
return (
req.connection?.remoteAddress ||
// @ts-expect-error
req.connection?.socket?.remoteAddress ||
req.socket?.remoteAddress ||
// @ts-expect-error
req.info?.remoteAddress
);
}
})
);
};
/** @param {import('polka').Middleware[]} handlers */
function sequence(handlers) {
/** @type {import('polka').Middleware} */
return (req, res, next) => {
/** @param {number} i */
function handle(i) {
handlers[i](req, res, () => {
if (i < handlers.length) handle(i + 1);
else next();
});
}
handle(0);
};
}
/**
* @param {import('http').IncomingHttpHeaders} headers
* @returns
*/
function get_origin(headers) {
const protocol = (protocol_header && headers[protocol_header]) || 'https';
const host = headers[host_header];
return `${protocol}://${host}`;
}
export const handler = sequence(
[
serve(path.join(__dirname, '/client'), 31536000, true),
serve(path.join(__dirname, '/static'), 0),
serve(path.join(__dirname, '/prerendered'), 0),
ssr
].filter(Boolean)
);