@sveltejs/kit 1.0.0-next.30 → 1.0.0-next.300

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