@sveltejs/kit 1.0.0-next.345 → 1.0.0-next.348
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/client/start.js +6 -0
- package/assets/server/index.js +22 -17
- package/dist/chunks/cert.js +136 -15
- package/dist/chunks/constants.js +1 -1
- package/dist/chunks/index.js +1265 -457
- package/dist/chunks/index2.js +88 -1361
- package/dist/chunks/index3.js +165 -99
- package/dist/chunks/index4.js +232 -148
- package/dist/chunks/index5.js +15690 -202
- package/dist/chunks/multipart-parser.js +2 -8
- package/dist/chunks/plugin.js +557 -0
- package/dist/chunks/sync.js +7 -6
- package/dist/chunks/write_tsconfig.js +12 -14
- package/dist/cli.js +89 -63
- package/dist/node/polyfills.js +211 -71
- package/package.json +24 -9
- package/types/ambient.d.ts +1 -1
- package/types/index.d.ts +61 -59
- package/types/internal.d.ts +3 -0
- package/types/private.d.ts +2 -2
- package/dist/chunks/index6.js +0 -15749
package/dist/chunks/index4.js
CHANGED
|
@@ -1,183 +1,267 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import '
|
|
5
|
-
import '
|
|
6
|
-
import '
|
|
7
|
-
import '
|
|
8
|
-
import '
|
|
1
|
+
import fs__default from 'fs';
|
|
2
|
+
import http from 'http';
|
|
3
|
+
import https from 'https';
|
|
4
|
+
import { join } from 'path';
|
|
5
|
+
import { S as SVELTE_KIT_ASSETS, s as sirv } from './constants.js';
|
|
6
|
+
import { pathToFileURL } from 'url';
|
|
7
|
+
import { getRequest, setResponse } from '../node.js';
|
|
8
|
+
import { installPolyfills } from '../node/polyfills.js';
|
|
9
|
+
import { d as load_config } from '../cli.js';
|
|
10
|
+
import 'querystring';
|
|
11
|
+
import 'stream';
|
|
12
|
+
import 'node:http';
|
|
13
|
+
import 'node:https';
|
|
14
|
+
import 'node:zlib';
|
|
15
|
+
import 'node:stream';
|
|
16
|
+
import 'node:buffer';
|
|
17
|
+
import 'node:util';
|
|
18
|
+
import 'node:url';
|
|
19
|
+
import 'node:net';
|
|
20
|
+
import 'node:fs';
|
|
21
|
+
import 'node:path';
|
|
22
|
+
import 'crypto';
|
|
9
23
|
import 'chokidar';
|
|
10
|
-
import '
|
|
24
|
+
import 'net';
|
|
25
|
+
import 'child_process';
|
|
26
|
+
import 'sade';
|
|
27
|
+
import 'vite';
|
|
11
28
|
import 'os';
|
|
12
|
-
|
|
29
|
+
|
|
30
|
+
/** @typedef {import('http').IncomingMessage} Req */
|
|
31
|
+
/** @typedef {import('http').ServerResponse} Res */
|
|
32
|
+
/** @typedef {(req: Req, res: Res, next: () => void) => void} Handler */
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @param {string} dir
|
|
36
|
+
* @returns {Handler}
|
|
37
|
+
*/
|
|
38
|
+
const mutable = (dir) =>
|
|
39
|
+
fs__default.existsSync(dir)
|
|
40
|
+
? sirv(dir, {
|
|
41
|
+
etag: true,
|
|
42
|
+
maxAge: 0
|
|
43
|
+
})
|
|
44
|
+
: (req, res, next) => next();
|
|
13
45
|
|
|
14
46
|
/**
|
|
15
47
|
* @param {{
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
48
|
+
* port: number;
|
|
49
|
+
* host?: string;
|
|
50
|
+
* https?: boolean;
|
|
51
|
+
* cwd?: string;
|
|
20
52
|
* }} opts
|
|
21
|
-
* @returns {import('types').Builder}
|
|
22
53
|
*/
|
|
23
|
-
function
|
|
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
|
-
const seen = new Set();
|
|
63
|
-
|
|
64
|
-
for (let i = 0; i < routes.length; i += 1) {
|
|
65
|
-
const route = routes[i];
|
|
66
|
-
const { id, filter, complete } = fn(facades[i]);
|
|
67
|
-
|
|
68
|
-
if (seen.has(id)) continue;
|
|
69
|
-
seen.add(id);
|
|
70
|
-
|
|
71
|
-
const group = [route];
|
|
72
|
-
|
|
73
|
-
// figure out which lower priority routes should be considered fallbacks
|
|
74
|
-
for (let j = i + 1; j < routes.length; j += 1) {
|
|
75
|
-
if (filter(facades[j])) {
|
|
76
|
-
group.push(routes[j]);
|
|
54
|
+
async function preview({ port, host, https: use_https = false }) {
|
|
55
|
+
installPolyfills();
|
|
56
|
+
|
|
57
|
+
const config = await load_config();
|
|
58
|
+
const { paths } = config.kit;
|
|
59
|
+
const base = paths.base;
|
|
60
|
+
const assets = paths.assets ? SVELTE_KIT_ASSETS : paths.base;
|
|
61
|
+
|
|
62
|
+
const etag = `"${Date.now()}"`;
|
|
63
|
+
|
|
64
|
+
const index_file = join(config.kit.outDir, 'output/server/index.js');
|
|
65
|
+
const manifest_file = join(config.kit.outDir, 'output/server/manifest.js');
|
|
66
|
+
|
|
67
|
+
/** @type {import('types').ServerModule} */
|
|
68
|
+
const { Server, override } = await import(pathToFileURL(index_file).href);
|
|
69
|
+
const { manifest } = await import(pathToFileURL(manifest_file).href);
|
|
70
|
+
|
|
71
|
+
override({
|
|
72
|
+
paths: { base, assets },
|
|
73
|
+
prerendering: false,
|
|
74
|
+
protocol: use_https ? 'https' : 'http',
|
|
75
|
+
read: (file) => fs__default.readFileSync(join(config.kit.files.assets, file))
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const server = new Server(manifest);
|
|
79
|
+
|
|
80
|
+
const handle = compose([
|
|
81
|
+
// files in `static`
|
|
82
|
+
scoped(assets, mutable(config.kit.files.assets)),
|
|
83
|
+
|
|
84
|
+
// immutable generated client assets
|
|
85
|
+
scoped(
|
|
86
|
+
assets,
|
|
87
|
+
sirv(join(config.kit.outDir, 'output/client'), {
|
|
88
|
+
setHeaders: (res, pathname) => {
|
|
89
|
+
// only apply to build directory, not e.g. version.json
|
|
90
|
+
if (pathname.startsWith(`/${config.kit.appDir}/immutable`)) {
|
|
91
|
+
res.setHeader('cache-control', 'public,max-age=31536000,immutable');
|
|
77
92
|
}
|
|
78
93
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
if (filtered.size > 0) {
|
|
95
|
-
await complete({
|
|
96
|
-
generateManifest: ({ relativePath, format }) =>
|
|
97
|
-
generate_manifest({
|
|
98
|
-
build_data,
|
|
99
|
-
relative_path: relativePath,
|
|
100
|
-
routes: Array.from(filtered),
|
|
101
|
-
format
|
|
102
|
-
})
|
|
103
|
-
});
|
|
104
|
-
}
|
|
94
|
+
})
|
|
95
|
+
),
|
|
96
|
+
|
|
97
|
+
(req, res, next) => {
|
|
98
|
+
const original_url = /** @type {string} */ (req.url);
|
|
99
|
+
const { pathname } = new URL(original_url, 'http://dummy');
|
|
100
|
+
|
|
101
|
+
if (pathname.startsWith(base)) {
|
|
102
|
+
next();
|
|
103
|
+
} else {
|
|
104
|
+
res.statusCode = 404;
|
|
105
|
+
res.end(`Not found (did you mean ${base + pathname}?)`);
|
|
105
106
|
}
|
|
106
107
|
},
|
|
107
108
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
build_data,
|
|
111
|
-
relative_path: relativePath,
|
|
112
|
-
routes: build_data.manifest_data.routes.filter(not_prerendered),
|
|
113
|
-
format
|
|
114
|
-
});
|
|
115
|
-
},
|
|
109
|
+
// prerendered dependencies
|
|
110
|
+
scoped(base, mutable(join(config.kit.outDir, 'output/prerendered/dependencies'))),
|
|
116
111
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
112
|
+
// prerendered pages (we can't just use sirv because we need to
|
|
113
|
+
// preserve the correct trailingSlash behaviour)
|
|
114
|
+
scoped(base, (req, res, next) => {
|
|
115
|
+
let if_none_match_value = req.headers['if-none-match'];
|
|
120
116
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
117
|
+
if (if_none_match_value?.startsWith('W/"')) {
|
|
118
|
+
if_none_match_value = if_none_match_value.substring(2);
|
|
119
|
+
}
|
|
124
120
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
121
|
+
if (if_none_match_value === etag) {
|
|
122
|
+
res.statusCode = 304;
|
|
123
|
+
res.end();
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
128
126
|
|
|
129
|
-
|
|
130
|
-
return config.kit.files.assets;
|
|
131
|
-
},
|
|
127
|
+
const { pathname } = new URL(/** @type {string} */ (req.url), 'http://dummy');
|
|
132
128
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
129
|
+
// only treat this as a page if it doesn't include an extension
|
|
130
|
+
if (pathname === '/' || /\/[^./]+\/?$/.test(pathname)) {
|
|
131
|
+
const file = join(
|
|
132
|
+
config.kit.outDir,
|
|
133
|
+
'output/prerendered/pages' + pathname + (pathname.endsWith('/') ? 'index.html' : '.html')
|
|
134
|
+
);
|
|
136
135
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
136
|
+
if (fs__default.existsSync(file)) {
|
|
137
|
+
res.writeHead(200, {
|
|
138
|
+
'content-type': 'text/html',
|
|
139
|
+
etag
|
|
140
|
+
});
|
|
140
141
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
142
|
+
fs__default.createReadStream(file).pipe(res);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
144
145
|
}
|
|
145
146
|
|
|
146
|
-
|
|
147
|
-
},
|
|
147
|
+
next();
|
|
148
|
+
}),
|
|
148
149
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
150
|
+
// SSR
|
|
151
|
+
async (req, res) => {
|
|
152
|
+
const protocol = use_https ? 'https' : 'http';
|
|
153
|
+
const host = req.headers['host'];
|
|
152
154
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
155
|
+
let request;
|
|
156
|
+
|
|
157
|
+
try {
|
|
158
|
+
request = await getRequest(`${protocol}://${host}`, req);
|
|
159
|
+
} catch (/** @type {any} */ err) {
|
|
160
|
+
res.statusCode = err.status || 400;
|
|
161
|
+
return res.end(err.reason || 'Invalid request body');
|
|
162
|
+
}
|
|
156
163
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
164
|
+
setResponse(
|
|
165
|
+
res,
|
|
166
|
+
await server.respond(request, {
|
|
167
|
+
getClientAddress: () => {
|
|
168
|
+
const { remoteAddress } = req.socket;
|
|
169
|
+
if (remoteAddress) return remoteAddress;
|
|
170
|
+
throw new Error('Could not determine clientAddress');
|
|
171
|
+
}
|
|
172
|
+
})
|
|
161
173
|
);
|
|
162
174
|
}
|
|
163
|
-
|
|
175
|
+
]);
|
|
176
|
+
|
|
177
|
+
const vite_config = (config.kit.vite && (await config.kit.vite())) || {};
|
|
178
|
+
|
|
179
|
+
const http_server = await get_server(use_https, vite_config, (req, res) => {
|
|
180
|
+
if (req.url == null) {
|
|
181
|
+
throw new Error('Invalid request url');
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
handle(req, res);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
return new Promise((fulfil) => {
|
|
188
|
+
http_server.listen(port, host, () => {
|
|
189
|
+
fulfil({ server: http_server, config });
|
|
190
|
+
});
|
|
191
|
+
});
|
|
164
192
|
}
|
|
165
193
|
|
|
166
194
|
/**
|
|
167
|
-
* @param {
|
|
168
|
-
* @param {import('
|
|
169
|
-
* @param {
|
|
170
|
-
* @
|
|
195
|
+
* @param {boolean} use_https
|
|
196
|
+
* @param {import('vite').UserConfig} user_config
|
|
197
|
+
* @param {(req: http.IncomingMessage, res: http.ServerResponse) => void} handler
|
|
198
|
+
* @returns {Promise<import('net').Server>}
|
|
171
199
|
*/
|
|
172
|
-
async function
|
|
173
|
-
|
|
200
|
+
async function get_server(use_https, user_config, handler) {
|
|
201
|
+
/** @type {https.ServerOptions} */
|
|
202
|
+
const https_options = {};
|
|
203
|
+
|
|
204
|
+
if (use_https) {
|
|
205
|
+
const secure_opts = user_config.server
|
|
206
|
+
? /** @type {import('tls').SecureContextOptions} */ (user_config.server.https)
|
|
207
|
+
: {};
|
|
208
|
+
|
|
209
|
+
if (secure_opts.key && secure_opts.cert) {
|
|
210
|
+
https_options.key = secure_opts.key.toString();
|
|
211
|
+
https_options.cert = secure_opts.cert.toString();
|
|
212
|
+
} else {
|
|
213
|
+
https_options.key = https_options.cert = (await import('./cert.js')).createCertificate();
|
|
214
|
+
}
|
|
215
|
+
}
|
|
174
216
|
|
|
175
|
-
|
|
217
|
+
return use_https
|
|
218
|
+
? https.createServer(/** @type {https.ServerOptions} */ (https_options), handler)
|
|
219
|
+
: http.createServer(handler);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** @param {Handler[]} handlers */
|
|
223
|
+
function compose(handlers) {
|
|
224
|
+
/**
|
|
225
|
+
* @param {Req} req
|
|
226
|
+
* @param {Res} res
|
|
227
|
+
*/
|
|
228
|
+
return (req, res) => {
|
|
229
|
+
/** @param {number} i */
|
|
230
|
+
function next(i) {
|
|
231
|
+
const handler = handlers[i];
|
|
232
|
+
|
|
233
|
+
if (handler) {
|
|
234
|
+
handler(req, res, () => next(i + 1));
|
|
235
|
+
} else {
|
|
236
|
+
res.statusCode = 404;
|
|
237
|
+
res.end('Not found');
|
|
238
|
+
}
|
|
239
|
+
}
|
|
176
240
|
|
|
177
|
-
|
|
178
|
-
|
|
241
|
+
next(0);
|
|
242
|
+
};
|
|
243
|
+
}
|
|
179
244
|
|
|
180
|
-
|
|
245
|
+
/**
|
|
246
|
+
* @param {string} scope
|
|
247
|
+
* @param {Handler} handler
|
|
248
|
+
* @returns {Handler}
|
|
249
|
+
*/
|
|
250
|
+
function scoped(scope, handler) {
|
|
251
|
+
if (scope === '') return handler;
|
|
252
|
+
|
|
253
|
+
return (req, res, next) => {
|
|
254
|
+
if (req.url?.startsWith(scope)) {
|
|
255
|
+
const original_url = req.url;
|
|
256
|
+
req.url = req.url.slice(scope.length);
|
|
257
|
+
handler(req, res, () => {
|
|
258
|
+
req.url = original_url;
|
|
259
|
+
next();
|
|
260
|
+
});
|
|
261
|
+
} else {
|
|
262
|
+
next();
|
|
263
|
+
}
|
|
264
|
+
};
|
|
181
265
|
}
|
|
182
266
|
|
|
183
|
-
export {
|
|
267
|
+
export { preview };
|