/
index.js
232 lines (193 loc) · 5.63 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import fs from 'fs';
import http from 'http';
import https from 'https';
import { join } from 'path';
import sirv from 'sirv';
import { pathToFileURL } from 'url';
import { getRequest, setResponse } from '../../node.js';
import { installFetch } from '../../install-fetch.js';
import { SVELTE_KIT_ASSETS } from '../constants.js';
import { normalize_path } from '../../utils/url.js';
/** @typedef {import('http').IncomingMessage} Req */
/** @typedef {import('http').ServerResponse} Res */
/** @typedef {(req: Req, res: Res, next: () => void) => void} Handler */
/**
* @param {string} dir
* @returns {Handler}
*/
const mutable = (dir) =>
fs.existsSync(dir)
? sirv(dir, {
etag: true,
maxAge: 0
})
: (req, res, next) => next();
/**
* @param {{
* port: number;
* host?: string;
* config: import('types').ValidatedConfig;
* https?: boolean;
* cwd?: string;
* }} opts
*/
export async function preview({ port, host, config, https: use_https = false }) {
installFetch();
const { paths } = config.kit;
const base = paths.base;
const assets = paths.assets ? SVELTE_KIT_ASSETS : paths.base;
const etag = `"${Date.now()}"`;
const index_file = join(config.kit.outDir, 'output/server/index.js');
const manifest_file = join(config.kit.outDir, 'output/server/manifest.js');
/** @type {import('types').ServerModule} */
const { Server, override } = await import(pathToFileURL(index_file).href);
const { manifest } = await import(pathToFileURL(manifest_file).href);
override({
paths: { base, assets },
prerendering: false,
protocol: use_https ? 'https' : 'http',
read: (file) => fs.readFileSync(join(config.kit.files.assets, file))
});
const server = new Server(manifest);
const handle = compose([
// files in `static`
scoped(assets, mutable(config.kit.files.assets)),
// immutable generated client assets
scoped(
assets,
sirv(join(config.kit.outDir, 'output/client'), {
maxAge: 31536000,
immutable: true
})
),
// prerendered dependencies
scoped(base, mutable(join(config.kit.outDir, 'output/prerendered/dependencies'))),
// prerendered pages (we can't just use sirv because we need to
// preserve the correct trailingSlash behaviour)
scoped(base, (req, res, next) => {
let if_none_match_value = req.headers['if-none-match'];
if (if_none_match_value?.startsWith('W/"')) {
if_none_match_value = if_none_match_value.substring(2);
}
if (if_none_match_value === etag) {
res.statusCode = 304;
res.end();
return;
}
const { pathname, search } = new URL(/** @type {string} */ (req.url), 'http://dummy');
const normalized = normalize_path(pathname, config.kit.trailingSlash);
if (normalized !== pathname) {
res.writeHead(307, {
location: base + normalized + search
});
res.end();
return;
}
// only treat this as a page if it doesn't include an extension
if (pathname === '/' || /\/[^./]+\/?$/.test(pathname)) {
const file = join(
config.kit.outDir,
'output/prerendered/pages' + pathname + (pathname.endsWith('/') ? 'index.html' : '.html')
);
if (fs.existsSync(file)) {
res.writeHead(200, {
'content-type': 'text/html',
etag
});
fs.createReadStream(file).pipe(res);
return;
}
}
next();
}),
// SSR
async (req, res) => {
const protocol = use_https ? 'https' : 'http';
const host = req.headers['host'];
let request;
try {
request = await getRequest(`${protocol}://${host}`, req);
} catch (/** @type {any} */ err) {
res.statusCode = err.status || 400;
return res.end(err.reason || 'Invalid request body');
}
setResponse(res, await server.respond(request));
}
]);
const vite_config = (config.kit.vite && (await config.kit.vite())) || {};
const http_server = await get_server(use_https, vite_config, (req, res) => {
if (req.url == null) {
throw new Error('Invalid request url');
}
handle(req, res);
});
return new Promise((fulfil) => {
http_server.listen(port, host || '0.0.0.0', () => {
fulfil(http_server);
});
});
}
/**
* @param {boolean} use_https
* @param {import('vite').UserConfig} user_config
* @param {(req: http.IncomingMessage, res: http.ServerResponse) => void} handler
* @returns {Promise<import('net').Server>}
*/
async function get_server(use_https, user_config, handler) {
/** @type {https.ServerOptions} */
const https_options = {};
if (use_https) {
const secure_opts = user_config.server
? /** @type {import('tls').SecureContextOptions} */ (user_config.server.https)
: {};
if (secure_opts.key && secure_opts.cert) {
https_options.key = secure_opts.key.toString();
https_options.cert = secure_opts.cert.toString();
} else {
https_options.key = https_options.cert = (await import('./cert')).createCertificate();
}
}
return use_https
? https.createServer(/** @type {https.ServerOptions} */ (https_options), handler)
: http.createServer(handler);
}
/** @param {Handler[]} handlers */
function compose(handlers) {
/**
* @param {Req} req
* @param {Res} res
*/
return (req, res) => {
/** @param {number} i */
function next(i) {
const handler = handlers[i];
if (handler) {
handler(req, res, () => next(i + 1));
} else {
res.statusCode = 404;
res.end('Not found');
}
}
next(0);
};
}
/**
* @param {string} scope
* @param {Handler} handler
* @returns {Handler}
*/
function scoped(scope, handler) {
if (scope === '') return handler;
return (req, res, next) => {
if (req.url?.startsWith(scope)) {
const original_url = req.url;
req.url = req.url.slice(scope.length);
handler(req, res, () => {
req.url = original_url;
next();
});
} else {
next();
}
};
}