@sveltejs/kit 1.0.0-next.34 → 1.0.0-next.340

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.
Files changed (73) hide show
  1. package/README.md +12 -9
  2. package/assets/app/env.js +16 -0
  3. package/assets/app/navigation.js +24 -0
  4. package/assets/app/paths.js +1 -0
  5. package/assets/app/stores.js +97 -0
  6. package/assets/client/singletons.js +13 -0
  7. package/assets/client/start.js +1768 -0
  8. package/assets/components/error.svelte +18 -2
  9. package/assets/env.js +8 -0
  10. package/assets/paths.js +13 -0
  11. package/assets/server/index.js +3384 -0
  12. package/dist/chunks/cert.js +28154 -0
  13. package/dist/chunks/constants.js +663 -0
  14. package/dist/chunks/filesystem.js +110 -0
  15. package/dist/chunks/index.js +544 -0
  16. package/dist/chunks/index2.js +1384 -0
  17. package/dist/chunks/index3.js +118 -0
  18. package/dist/chunks/index4.js +183 -0
  19. package/dist/chunks/index5.js +252 -0
  20. package/dist/chunks/index6.js +15749 -0
  21. package/dist/chunks/misc.js +78 -0
  22. package/dist/chunks/multipart-parser.js +449 -0
  23. package/dist/chunks/object.js +83 -0
  24. package/dist/chunks/sync.js +858 -0
  25. package/dist/chunks/write_tsconfig.js +160 -0
  26. package/dist/cli.js +1101 -64
  27. package/dist/hooks.js +28 -0
  28. package/dist/install-fetch.js +6518 -0
  29. package/dist/node.js +94 -0
  30. package/package.json +80 -62
  31. package/svelte-kit.js +0 -1
  32. package/types/ambient.d.ts +302 -0
  33. package/types/index.d.ts +292 -0
  34. package/types/internal.d.ts +321 -0
  35. package/types/private.d.ts +235 -0
  36. package/CHANGELOG.md +0 -362
  37. package/assets/runtime/app/navigation.js +0 -23
  38. package/assets/runtime/app/navigation.js.map +0 -1
  39. package/assets/runtime/app/paths.js +0 -2
  40. package/assets/runtime/app/paths.js.map +0 -1
  41. package/assets/runtime/app/stores.js +0 -78
  42. package/assets/runtime/app/stores.js.map +0 -1
  43. package/assets/runtime/internal/singletons.js +0 -15
  44. package/assets/runtime/internal/singletons.js.map +0 -1
  45. package/assets/runtime/internal/start.js +0 -591
  46. package/assets/runtime/internal/start.js.map +0 -1
  47. package/assets/runtime/utils-85ebcc60.js +0 -18
  48. package/assets/runtime/utils-85ebcc60.js.map +0 -1
  49. package/dist/api.js +0 -32
  50. package/dist/api.js.map +0 -1
  51. package/dist/cli.js.map +0 -1
  52. package/dist/create_app.js +0 -577
  53. package/dist/create_app.js.map +0 -1
  54. package/dist/index.js +0 -329
  55. package/dist/index.js.map +0 -1
  56. package/dist/index2.js +0 -14368
  57. package/dist/index2.js.map +0 -1
  58. package/dist/index3.js +0 -545
  59. package/dist/index3.js.map +0 -1
  60. package/dist/index4.js +0 -71
  61. package/dist/index4.js.map +0 -1
  62. package/dist/index5.js +0 -465
  63. package/dist/index5.js.map +0 -1
  64. package/dist/index6.js +0 -729
  65. package/dist/index6.js.map +0 -1
  66. package/dist/renderer.js +0 -2413
  67. package/dist/renderer.js.map +0 -1
  68. package/dist/standard.js +0 -100
  69. package/dist/standard.js.map +0 -1
  70. package/dist/utils.js +0 -57
  71. package/dist/utils.js.map +0 -1
  72. package/dist/vite.js +0 -317
  73. package/dist/vite.js.map +0 -1
@@ -0,0 +1,118 @@
1
+ import { s, p as parse_route_id } from './misc.js';
2
+ import { a as get_mime_lookup } from '../cli.js';
3
+
4
+ /**
5
+ * @param {{
6
+ * build_data: import('types').BuildData;
7
+ * relative_path: string;
8
+ * routes: import('types').RouteData[];
9
+ * format?: 'esm' | 'cjs'
10
+ * }} opts
11
+ */
12
+ function generate_manifest({ build_data, relative_path, routes, format = 'esm' }) {
13
+ /** @typedef {{ index: number, path: string }} LookupEntry */
14
+ /** @type {Map<string, LookupEntry>} */
15
+ const bundled_nodes = new Map();
16
+
17
+ // 0 and 1 are special, they correspond to the root layout and root error nodes
18
+ bundled_nodes.set(build_data.manifest_data.components[0], {
19
+ path: `${relative_path}/nodes/0.js`,
20
+ index: 0
21
+ });
22
+
23
+ bundled_nodes.set(build_data.manifest_data.components[1], {
24
+ path: `${relative_path}/nodes/1.js`,
25
+ index: 1
26
+ });
27
+
28
+ routes.forEach((route) => {
29
+ if (route.type === 'page') {
30
+ [...route.a, ...route.b].forEach((component) => {
31
+ if (component && !bundled_nodes.has(component)) {
32
+ const i = build_data.manifest_data.components.indexOf(component);
33
+
34
+ bundled_nodes.set(component, {
35
+ path: `${relative_path}/nodes/${i}.js`,
36
+ index: bundled_nodes.size
37
+ });
38
+ }
39
+ });
40
+ }
41
+ });
42
+
43
+ /** @type {(path: string) => string} */
44
+ const load =
45
+ format === 'esm'
46
+ ? (path) => `import('${path}')`
47
+ : (path) => `Promise.resolve().then(() => require('${path}'))`;
48
+
49
+ /** @type {(path: string) => string} */
50
+ const loader = (path) => `() => ${load(path)}`;
51
+
52
+ const assets = build_data.manifest_data.assets.map((asset) => asset.file);
53
+ if (build_data.service_worker) {
54
+ assets.push(build_data.service_worker);
55
+ }
56
+
57
+ /** @param {string | undefined} id */
58
+ const get_index = (id) => id && /** @type {LookupEntry} */ (bundled_nodes.get(id)).index;
59
+
60
+ const matchers = new Set();
61
+
62
+ // prettier-ignore
63
+ return `{
64
+ appDir: ${s(build_data.app_dir)},
65
+ assets: new Set(${s(assets)}),
66
+ mimeTypes: ${s(get_mime_lookup(build_data.manifest_data))},
67
+ _: {
68
+ entry: ${s(build_data.client.entry)},
69
+ nodes: [
70
+ ${Array.from(bundled_nodes.values()).map(node => loader(node.path)).join(',\n\t\t\t\t')}
71
+ ],
72
+ routes: [
73
+ ${routes.map(route => {
74
+ const { pattern, names, types } = parse_route_id(route.id);
75
+
76
+ types.forEach(type => {
77
+ if (type) matchers.add(type);
78
+ });
79
+
80
+ if (route.type === 'page') {
81
+ return `{
82
+ type: 'page',
83
+ id: ${s(route.id)},
84
+ pattern: ${pattern},
85
+ names: ${s(names)},
86
+ types: ${s(types)},
87
+ path: ${route.path ? s(route.path) : null},
88
+ shadow: ${route.shadow ? loader(`${relative_path}/${build_data.server.vite_manifest[route.shadow].file}`) : null},
89
+ a: ${s(route.a.map(get_index))},
90
+ b: ${s(route.b.map(get_index))}
91
+ }`.replace(/^\t\t/gm, '');
92
+ } else {
93
+ if (!build_data.server.vite_manifest[route.file]) {
94
+ // this is necessary in cases where a .css file snuck in —
95
+ // perhaps it would be better to disallow these (and others?)
96
+ return null;
97
+ }
98
+
99
+ return `{
100
+ type: 'endpoint',
101
+ id: ${s(route.id)},
102
+ pattern: ${pattern},
103
+ names: ${s(names)},
104
+ types: ${s(types)},
105
+ load: ${loader(`${relative_path}/${build_data.server.vite_manifest[route.file].file}`)}
106
+ }`.replace(/^\t\t/gm, '');
107
+ }
108
+ }).filter(Boolean).join(',\n\t\t\t\t')}
109
+ ],
110
+ matchers: async () => {
111
+ ${Array.from(matchers).map(type => `const { match: ${type} } = await ${load(`${relative_path}/entries/matchers/${type}.js`)}`).join('\n\t\t\t\t')}
112
+ return { ${Array.from(matchers).join(', ')} };
113
+ }
114
+ }
115
+ }`.replace(/^\t/gm, '');
116
+ }
117
+
118
+ export { generate_manifest as g };
@@ -0,0 +1,183 @@
1
+ import { $ } from '../cli.js';
2
+ import { r as rimraf, m as mkdirp, c as copy } from './filesystem.js';
3
+ import { g as generate_manifest } from './index3.js';
4
+ import 'sade';
5
+ import 'fs';
6
+ import 'path';
7
+ import 'child_process';
8
+ import 'net';
9
+ import 'chokidar';
10
+ import 'url';
11
+ import 'os';
12
+ import './misc.js';
13
+
14
+ /**
15
+ * @param {{
16
+ * config: import('types').ValidatedConfig;
17
+ * build_data: import('types').BuildData;
18
+ * prerendered: import('types').Prerendered;
19
+ * log: import('types').Logger;
20
+ * }} opts
21
+ * @returns {import('types').Builder}
22
+ */
23
+ function create_builder({ config, build_data, prerendered, log }) {
24
+ /** @type {Set<string>} */
25
+ const prerendered_paths = new Set(prerendered.paths);
26
+
27
+ /** @param {import('types').RouteData} route */
28
+ // TODO routes should come pre-filtered
29
+ function not_prerendered(route) {
30
+ if (route.type === 'page' && route.path) {
31
+ return !prerendered_paths.has(route.path);
32
+ }
33
+
34
+ return true;
35
+ }
36
+
37
+ return {
38
+ log,
39
+ rimraf,
40
+ mkdirp,
41
+ copy,
42
+
43
+ config,
44
+ prerendered,
45
+
46
+ async createEntries(fn) {
47
+ const { routes } = build_data.manifest_data;
48
+
49
+ /** @type {import('types').RouteDefinition[]} */
50
+ const facades = routes.map((route) => ({
51
+ id: route.id,
52
+ type: route.type,
53
+ segments: route.id.split('/').map((segment) => ({
54
+ dynamic: segment.includes('['),
55
+ rest: segment.includes('[...'),
56
+ content: segment
57
+ })),
58
+ pattern: route.pattern,
59
+ methods: route.type === 'page' ? ['get'] : build_data.server.methods[route.file]
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]);
77
+ }
78
+ }
79
+
80
+ const filtered = new Set(group.filter(not_prerendered));
81
+
82
+ // heuristic: if /foo/[bar] is included, /foo/[bar].json should
83
+ // also be included, since the page likely needs the endpoint
84
+ filtered.forEach((route) => {
85
+ if (route.type === 'page') {
86
+ const endpoint = routes.find((candidate) => candidate.id === route.id + '.json');
87
+
88
+ if (endpoint) {
89
+ filtered.add(endpoint);
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
+ }
105
+ }
106
+ },
107
+
108
+ generateManifest: ({ relativePath, format }) => {
109
+ return generate_manifest({
110
+ build_data,
111
+ relative_path: relativePath,
112
+ routes: build_data.manifest_data.routes.filter(not_prerendered),
113
+ format
114
+ });
115
+ },
116
+
117
+ getBuildDirectory(name) {
118
+ return `${config.kit.outDir}/${name}`;
119
+ },
120
+
121
+ getClientDirectory() {
122
+ return `${config.kit.outDir}/output/client`;
123
+ },
124
+
125
+ getServerDirectory() {
126
+ return `${config.kit.outDir}/output/server`;
127
+ },
128
+
129
+ getStaticDirectory() {
130
+ return config.kit.files.assets;
131
+ },
132
+
133
+ writeClient(dest) {
134
+ return copy(`${config.kit.outDir}/output/client`, dest);
135
+ },
136
+
137
+ writePrerendered(dest, { fallback } = {}) {
138
+ const source = `${config.kit.outDir}/output/prerendered`;
139
+ const files = [...copy(`${source}/pages`, dest), ...copy(`${source}/dependencies`, dest)];
140
+
141
+ if (fallback) {
142
+ files.push(fallback);
143
+ copy(`${source}/fallback.html`, `${dest}/${fallback}`);
144
+ }
145
+
146
+ return files;
147
+ },
148
+
149
+ writeServer(dest) {
150
+ return copy(`${config.kit.outDir}/output/server`, dest);
151
+ },
152
+
153
+ writeStatic(dest) {
154
+ return copy(config.kit.files.assets, dest);
155
+ },
156
+
157
+ // @ts-expect-error
158
+ async prerender() {
159
+ throw new Error(
160
+ 'builder.prerender() has been removed. Prerendering now takes place in the build phase — see builder.prerender and builder.writePrerendered'
161
+ );
162
+ }
163
+ };
164
+ }
165
+
166
+ /**
167
+ * @param {import('types').ValidatedConfig} config
168
+ * @param {import('types').BuildData} build_data
169
+ * @param {import('types').Prerendered} prerendered
170
+ * @param {{ log: import('types').Logger }} opts
171
+ */
172
+ async function adapt(config, build_data, prerendered, { log }) {
173
+ const { name, adapt } = config.kit.adapter;
174
+
175
+ console.log($.bold().cyan(`\n> Using ${name}`));
176
+
177
+ const builder = create_builder({ config, build_data, prerendered, log });
178
+ await adapt(builder);
179
+
180
+ log.success('done');
181
+ }
182
+
183
+ export { adapt };
@@ -0,0 +1,252 @@
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 { installFetch } from '../install-fetch.js';
9
+ import 'querystring';
10
+ import 'stream';
11
+ import 'node:http';
12
+ import 'node:https';
13
+ import 'node:zlib';
14
+ import 'node:stream';
15
+ import 'node:util';
16
+ import 'node:url';
17
+ import 'net';
18
+
19
+ /** @typedef {import('http').IncomingMessage} Req */
20
+ /** @typedef {import('http').ServerResponse} Res */
21
+ /** @typedef {(req: Req, res: Res, next: () => void) => void} Handler */
22
+
23
+ /**
24
+ * @param {string} dir
25
+ * @returns {Handler}
26
+ */
27
+ const mutable = (dir) =>
28
+ fs__default.existsSync(dir)
29
+ ? sirv(dir, {
30
+ etag: true,
31
+ maxAge: 0
32
+ })
33
+ : (req, res, next) => next();
34
+
35
+ /**
36
+ * @param {{
37
+ * port: number;
38
+ * host?: string;
39
+ * config: import('types').ValidatedConfig;
40
+ * https?: boolean;
41
+ * cwd?: string;
42
+ * }} opts
43
+ */
44
+ async function preview({ port, host, config, https: use_https = false }) {
45
+ installFetch();
46
+
47
+ const { paths } = config.kit;
48
+ const base = paths.base;
49
+ const assets = paths.assets ? SVELTE_KIT_ASSETS : paths.base;
50
+
51
+ const etag = `"${Date.now()}"`;
52
+
53
+ const index_file = join(config.kit.outDir, 'output/server/index.js');
54
+ const manifest_file = join(config.kit.outDir, 'output/server/manifest.js');
55
+
56
+ /** @type {import('types').ServerModule} */
57
+ const { Server, override } = await import(pathToFileURL(index_file).href);
58
+ const { manifest } = await import(pathToFileURL(manifest_file).href);
59
+
60
+ override({
61
+ paths: { base, assets },
62
+ prerendering: false,
63
+ protocol: use_https ? 'https' : 'http',
64
+ read: (file) => fs__default.readFileSync(join(config.kit.files.assets, file))
65
+ });
66
+
67
+ const server = new Server(manifest);
68
+
69
+ const handle = compose([
70
+ // files in `static`
71
+ scoped(assets, mutable(config.kit.files.assets)),
72
+
73
+ // immutable generated client assets
74
+ scoped(
75
+ assets,
76
+ sirv(join(config.kit.outDir, 'output/client'), {
77
+ maxAge: 31536000,
78
+ immutable: true
79
+ })
80
+ ),
81
+
82
+ (req, res, next) => {
83
+ const original_url = /** @type {string} */ (req.url);
84
+ const { pathname } = new URL(original_url, 'http://dummy');
85
+
86
+ if (pathname.startsWith(base)) {
87
+ next();
88
+ } else {
89
+ res.statusCode = 404;
90
+ res.end(`Not found (did you mean ${base + pathname}?)`);
91
+ }
92
+ },
93
+
94
+ // prerendered dependencies
95
+ scoped(base, mutable(join(config.kit.outDir, 'output/prerendered/dependencies'))),
96
+
97
+ // prerendered pages (we can't just use sirv because we need to
98
+ // preserve the correct trailingSlash behaviour)
99
+ scoped(base, (req, res, next) => {
100
+ let if_none_match_value = req.headers['if-none-match'];
101
+
102
+ if (if_none_match_value?.startsWith('W/"')) {
103
+ if_none_match_value = if_none_match_value.substring(2);
104
+ }
105
+
106
+ if (if_none_match_value === etag) {
107
+ res.statusCode = 304;
108
+ res.end();
109
+ return;
110
+ }
111
+
112
+ const { pathname } = new URL(/** @type {string} */ (req.url), 'http://dummy');
113
+
114
+ // only treat this as a page if it doesn't include an extension
115
+ if (pathname === '/' || /\/[^./]+\/?$/.test(pathname)) {
116
+ const file = join(
117
+ config.kit.outDir,
118
+ 'output/prerendered/pages' + pathname + (pathname.endsWith('/') ? 'index.html' : '.html')
119
+ );
120
+
121
+ if (fs__default.existsSync(file)) {
122
+ res.writeHead(200, {
123
+ 'content-type': 'text/html',
124
+ etag
125
+ });
126
+
127
+ fs__default.createReadStream(file).pipe(res);
128
+ return;
129
+ }
130
+ }
131
+
132
+ next();
133
+ }),
134
+
135
+ // SSR
136
+ async (req, res) => {
137
+ const protocol = use_https ? 'https' : 'http';
138
+ const host = req.headers['host'];
139
+
140
+ let request;
141
+
142
+ try {
143
+ request = await getRequest(`${protocol}://${host}`, req);
144
+ } catch (/** @type {any} */ err) {
145
+ res.statusCode = err.status || 400;
146
+ return res.end(err.reason || 'Invalid request body');
147
+ }
148
+
149
+ setResponse(
150
+ res,
151
+ await server.respond(request, {
152
+ getClientAddress: () => {
153
+ const { remoteAddress } = req.socket;
154
+ if (remoteAddress) return remoteAddress;
155
+ throw new Error('Could not determine clientAddress');
156
+ }
157
+ })
158
+ );
159
+ }
160
+ ]);
161
+
162
+ const vite_config = (config.kit.vite && (await config.kit.vite())) || {};
163
+
164
+ const http_server = await get_server(use_https, vite_config, (req, res) => {
165
+ if (req.url == null) {
166
+ throw new Error('Invalid request url');
167
+ }
168
+
169
+ handle(req, res);
170
+ });
171
+
172
+ return new Promise((fulfil) => {
173
+ http_server.listen(port, host, () => {
174
+ fulfil(http_server);
175
+ });
176
+ });
177
+ }
178
+
179
+ /**
180
+ * @param {boolean} use_https
181
+ * @param {import('vite').UserConfig} user_config
182
+ * @param {(req: http.IncomingMessage, res: http.ServerResponse) => void} handler
183
+ * @returns {Promise<import('net').Server>}
184
+ */
185
+ async function get_server(use_https, user_config, handler) {
186
+ /** @type {https.ServerOptions} */
187
+ const https_options = {};
188
+
189
+ if (use_https) {
190
+ const secure_opts = user_config.server
191
+ ? /** @type {import('tls').SecureContextOptions} */ (user_config.server.https)
192
+ : {};
193
+
194
+ if (secure_opts.key && secure_opts.cert) {
195
+ https_options.key = secure_opts.key.toString();
196
+ https_options.cert = secure_opts.cert.toString();
197
+ } else {
198
+ https_options.key = https_options.cert = (await import('./cert.js')).createCertificate();
199
+ }
200
+ }
201
+
202
+ return use_https
203
+ ? https.createServer(/** @type {https.ServerOptions} */ (https_options), handler)
204
+ : http.createServer(handler);
205
+ }
206
+
207
+ /** @param {Handler[]} handlers */
208
+ function compose(handlers) {
209
+ /**
210
+ * @param {Req} req
211
+ * @param {Res} res
212
+ */
213
+ return (req, res) => {
214
+ /** @param {number} i */
215
+ function next(i) {
216
+ const handler = handlers[i];
217
+
218
+ if (handler) {
219
+ handler(req, res, () => next(i + 1));
220
+ } else {
221
+ res.statusCode = 404;
222
+ res.end('Not found');
223
+ }
224
+ }
225
+
226
+ next(0);
227
+ };
228
+ }
229
+
230
+ /**
231
+ * @param {string} scope
232
+ * @param {Handler} handler
233
+ * @returns {Handler}
234
+ */
235
+ function scoped(scope, handler) {
236
+ if (scope === '') return handler;
237
+
238
+ return (req, res, next) => {
239
+ if (req.url?.startsWith(scope)) {
240
+ const original_url = req.url;
241
+ req.url = req.url.slice(scope.length);
242
+ handler(req, res, () => {
243
+ req.url = original_url;
244
+ next();
245
+ });
246
+ } else {
247
+ next();
248
+ }
249
+ };
250
+ }
251
+
252
+ export { preview };