@sveltejs/kit 1.0.0-next.40 → 1.0.0-next.402

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 (48) hide show
  1. package/README.md +12 -9
  2. package/assets/app/env.js +13 -0
  3. package/assets/app/navigation.js +24 -0
  4. package/assets/app/paths.js +1 -0
  5. package/assets/{runtime/app → app}/stores.js +33 -29
  6. package/assets/client/singletons.js +13 -0
  7. package/assets/client/start.js +1845 -0
  8. package/assets/components/error.svelte +18 -2
  9. package/assets/env/dynamic/private.js +1 -0
  10. package/assets/env/dynamic/public.js +1 -0
  11. package/assets/env-private.js +9 -0
  12. package/assets/env-public.js +9 -0
  13. package/assets/env.js +8 -0
  14. package/assets/{runtime/chunks/paths.js → paths.js} +4 -3
  15. package/assets/server/index.js +3579 -0
  16. package/dist/chunks/error.js +12 -0
  17. package/dist/chunks/filesystem.js +110 -0
  18. package/dist/chunks/index.js +541 -3385
  19. package/dist/chunks/index2.js +15631 -473
  20. package/dist/chunks/index3.js +189 -217
  21. package/dist/chunks/multipart-parser.js +458 -0
  22. package/dist/chunks/sync.js +1366 -0
  23. package/dist/chunks/utils.js +40 -57
  24. package/dist/chunks/write_tsconfig.js +273 -0
  25. package/dist/cli.js +85 -513
  26. package/dist/hooks.js +28 -0
  27. package/dist/node/polyfills.js +17778 -0
  28. package/dist/node.js +348 -0
  29. package/dist/prerender.js +788 -0
  30. package/dist/vite.js +2520 -0
  31. package/package.json +98 -64
  32. package/svelte-kit.js +10 -1
  33. package/types/ambient.d.ts +375 -0
  34. package/types/index.d.ts +298 -0
  35. package/types/internal.d.ts +335 -0
  36. package/types/private.d.ts +235 -0
  37. package/CHANGELOG.md +0 -411
  38. package/assets/runtime/app/env.js +0 -5
  39. package/assets/runtime/app/navigation.js +0 -41
  40. package/assets/runtime/app/paths.js +0 -1
  41. package/assets/runtime/chunks/utils.js +0 -19
  42. package/assets/runtime/internal/singletons.js +0 -23
  43. package/assets/runtime/internal/start.js +0 -770
  44. package/dist/chunks/index4.js +0 -526
  45. package/dist/chunks/index5.js +0 -761
  46. package/dist/chunks/index6.js +0 -322
  47. package/dist/chunks/standard.js +0 -99
  48. package/dist/ssr.js +0 -2523
@@ -1,246 +1,218 @@
1
- function read_only_form_data() {
2
- /** @type {Map<string, string[]>} */
3
- const map = new Map();
1
+ import { $ } from './index.js';
2
+ import { r as rimraf, m as mkdirp, c as copy } from './filesystem.js';
3
+ import { g as generate_manifest } from '../vite.js';
4
+ import 'fs';
5
+ import 'path';
6
+ import 'url';
7
+ import 'node:child_process';
8
+ import 'node:fs';
9
+ import 'node:path';
10
+ import '@sveltejs/vite-plugin-svelte';
11
+ import 'vite';
12
+ import './sync.js';
13
+ import './utils.js';
14
+ import './write_tsconfig.js';
15
+ import 'querystring';
16
+ import '../node.js';
17
+ import '../node/polyfills.js';
18
+ import 'assert';
19
+ import 'net';
20
+ import 'http';
21
+ import 'stream';
22
+ import 'buffer';
23
+ import 'util';
24
+ import 'stream/web';
25
+ import 'perf_hooks';
26
+ import 'util/types';
27
+ import 'events';
28
+ import 'tls';
29
+ import 'async_hooks';
30
+ import 'console';
31
+ import 'zlib';
32
+ import 'node:http';
33
+ import 'node:https';
34
+ import 'node:zlib';
35
+ import 'node:stream';
36
+ import 'node:buffer';
37
+ import 'node:util';
38
+ import 'node:url';
39
+ import 'node:net';
40
+ import 'crypto';
41
+ import './error.js';
4
42
 
5
- return {
6
- /**
7
- * @param {string} key
8
- * @param {string} value
9
- */
10
- append(key, value) {
11
- if (map.has(key)) {
12
- map.get(key).push(value);
13
- } else {
14
- map.set(key, [value]);
15
- }
16
- },
17
-
18
- data: new ReadOnlyFormData(map)
19
- };
20
- }
21
-
22
- class ReadOnlyFormData {
23
- /** @type {Map<string, string[]>} */
24
- #map;
25
-
26
- /** @param {Map<string, string[]>} map */
27
- constructor(map) {
28
- this.#map = map;
29
- }
30
-
31
- /** @param {string} key */
32
- get(key) {
33
- const value = this.#map.get(key);
34
- return value && value[0];
35
- }
36
-
37
- /** @param {string} key */
38
- getAll(key) {
39
- return this.#map.get(key);
40
- }
41
-
42
- /** @param {string} key */
43
- has(key) {
44
- return this.#map.has(key);
45
- }
46
-
47
- *[Symbol.iterator]() {
48
- for (const [key, value] of this.#map) {
49
- for (let i = 0; i < value.length; i += 1) {
50
- yield [key, value[i]];
51
- }
43
+ /**
44
+ * Creates the Builder which is passed to adapters for building the application.
45
+ * @param {{
46
+ * config: import('types').ValidatedConfig;
47
+ * build_data: import('types').BuildData;
48
+ * prerendered: import('types').Prerendered;
49
+ * log: import('types').Logger;
50
+ * }} opts
51
+ * @returns {import('types').Builder}
52
+ */
53
+ function create_builder({ config, build_data, prerendered, log }) {
54
+ /** @type {Set<string>} */
55
+ const prerendered_paths = new Set(prerendered.paths);
56
+
57
+ /** @param {import('types').RouteData} route */
58
+ // TODO routes should come pre-filtered
59
+ function not_prerendered(route) {
60
+ if (route.type === 'page' && route.path) {
61
+ return !prerendered_paths.has(route.path) && !prerendered_paths.has(route.path + '/');
52
62
  }
53
- }
54
63
 
55
- *entries() {
56
- for (const [key, value] of this.#map) {
57
- for (let i = 0; i < value.length; i += 1) {
58
- yield [key, value[i]];
59
- }
60
- }
64
+ return true;
61
65
  }
62
66
 
63
- *keys() {
64
- for (const [key, value] of this.#map) {
65
- for (let i = 0; i < value.length; i += 1) {
66
- yield key;
67
- }
68
- }
69
- }
67
+ return {
68
+ log,
69
+ rimraf,
70
+ mkdirp,
71
+ copy,
72
+
73
+ config,
74
+ prerendered,
75
+
76
+ async createEntries(fn) {
77
+ const { routes } = build_data.manifest_data;
78
+
79
+ /** @type {import('types').RouteDefinition[]} */
80
+ const facades = routes.map((route) => ({
81
+ id: route.id,
82
+ type: route.type,
83
+ segments: route.id.split('/').map((segment) => ({
84
+ dynamic: segment.includes('['),
85
+ rest: segment.includes('[...'),
86
+ content: segment
87
+ })),
88
+ pattern: route.pattern,
89
+ methods: route.type === 'page' ? ['GET'] : build_data.server.methods[route.file]
90
+ }));
91
+
92
+ const seen = new Set();
93
+
94
+ for (let i = 0; i < routes.length; i += 1) {
95
+ const route = routes[i];
96
+ const { id, filter, complete } = fn(facades[i]);
97
+
98
+ if (seen.has(id)) continue;
99
+ seen.add(id);
100
+
101
+ const group = [route];
102
+
103
+ // figure out which lower priority routes should be considered fallbacks
104
+ for (let j = i + 1; j < routes.length; j += 1) {
105
+ if (filter(facades[j])) {
106
+ group.push(routes[j]);
107
+ }
108
+ }
70
109
 
71
- *values() {
72
- for (const [, value] of this.#map) {
73
- for (let i = 0; i < value.length; i += 1) {
74
- yield value;
110
+ const filtered = new Set(group.filter(not_prerendered));
111
+
112
+ // heuristic: if /foo/[bar] is included, /foo/[bar].json should
113
+ // also be included, since the page likely needs the endpoint
114
+ filtered.forEach((route) => {
115
+ if (route.type === 'page') {
116
+ const endpoint = routes.find((candidate) => candidate.id === route.id + '.json');
117
+
118
+ if (endpoint) {
119
+ filtered.add(endpoint);
120
+ }
121
+ }
122
+ });
123
+
124
+ if (filtered.size > 0) {
125
+ await complete({
126
+ generateManifest: ({ relativePath, format }) =>
127
+ generate_manifest({
128
+ build_data,
129
+ relative_path: relativePath,
130
+ routes: Array.from(filtered),
131
+ format
132
+ })
133
+ });
134
+ }
75
135
  }
76
- }
77
- }
78
- }
79
-
80
- /** @param {import('http').IncomingMessage} req */
81
- function get_body(req) {
82
- const headers = req.headers;
83
- const has_body =
84
- headers['content-type'] !== undefined &&
85
- // https://github.com/jshttp/type-is/blob/c1f4388c71c8a01f79934e68f630ca4a15fffcd6/index.js#L81-L95
86
- (headers['transfer-encoding'] !== undefined || !isNaN(Number(headers['content-length'])));
87
-
88
- if (!has_body) return Promise.resolve(undefined);
89
-
90
- const [type, ...directives] = headers['content-type'].split(/;\s*/);
91
-
92
- switch (type) {
93
- case 'application/octet-stream':
94
- return get_buffer(req);
95
-
96
- case 'text/plain':
97
- return get_text(req);
98
-
99
- case 'application/json':
100
- return get_json(req);
101
-
102
- case 'application/x-www-form-urlencoded':
103
- return get_urlencoded(req);
104
-
105
- case 'multipart/form-data': {
106
- const boundary = directives.find((directive) => directive.startsWith('boundary='));
107
- if (!boundary) throw new Error('Missing boundary');
108
- return get_multipart(req, boundary.slice('boundary='.length));
109
- }
110
- default:
111
- throw new Error(`Invalid Content-Type ${type}`);
112
- }
113
- }
114
-
115
- /** @param {import('http').IncomingMessage} req */
116
- async function get_json(req) {
117
- return JSON.parse(await get_text(req));
118
- }
119
-
120
- /** @param {import('http').IncomingMessage} req */
121
- async function get_urlencoded(req) {
122
- const text = await get_text(req);
123
-
124
- const { data, append } = read_only_form_data();
125
-
126
- text
127
- .replace(/\+/g, ' ')
128
- .split('&')
129
- .forEach((str) => {
130
- const [key, value] = str.split('=');
131
- append(decodeURIComponent(key), decodeURIComponent(value));
132
- });
133
-
134
- return data;
135
- }
136
-
137
- /**
138
- * @param {import('http').IncomingMessage} req
139
- * @param {string} boundary
140
- */
141
- async function get_multipart(req, boundary) {
142
- const text = await get_text(req);
143
- const parts = text.split(`--${boundary}`);
144
-
145
- const nope = () => {
146
- throw new Error('Malformed form data');
147
- };
148
-
149
- if (parts[0] !== '' || parts[parts.length - 1].trim() !== '--') {
150
- nope();
151
- }
136
+ },
152
137
 
153
- const { data, append } = read_only_form_data();
138
+ generateManifest: ({ relativePath, format }) => {
139
+ return generate_manifest({
140
+ build_data,
141
+ relative_path: relativePath,
142
+ routes: build_data.manifest_data.routes.filter(not_prerendered),
143
+ format
144
+ });
145
+ },
154
146
 
155
- parts.slice(1, -1).forEach((part) => {
156
- const match = /\s*([\s\S]+?)\r\n\r\n([\s\S]*)\s*/.exec(part);
157
- const raw_headers = match[1];
158
- const body = match[2].trim();
147
+ getBuildDirectory(name) {
148
+ return `${config.kit.outDir}/${name}`;
149
+ },
159
150
 
160
- let key;
161
- raw_headers.split('\r\n').forEach((str) => {
162
- const [raw_header, ...raw_directives] = str.split('; ');
163
- let [name, value] = raw_header.split(': ');
151
+ getClientDirectory() {
152
+ return `${config.kit.outDir}/output/client`;
153
+ },
164
154
 
165
- name = name.toLowerCase();
155
+ getServerDirectory() {
156
+ return `${config.kit.outDir}/output/server`;
157
+ },
166
158
 
167
- /** @type {Record<string, string>} */
168
- const directives = {};
169
- raw_directives.forEach((raw_directive) => {
170
- const [name, value] = raw_directive.split('=');
171
- directives[name] = JSON.parse(value); // TODO is this right?
172
- });
159
+ getStaticDirectory() {
160
+ return config.kit.files.assets;
161
+ },
173
162
 
174
- if (name === 'content-disposition') {
175
- if (value !== 'form-data') nope();
163
+ writeClient(dest) {
164
+ return [...copy(`${config.kit.outDir}/output/client`, dest)];
165
+ },
176
166
 
177
- if (directives.filename) {
178
- // TODO we probably don't want to do this automatically
179
- throw new Error('File upload is not yet implemented');
180
- }
167
+ writePrerendered(dest, { fallback } = {}) {
168
+ const source = `${config.kit.outDir}/output/prerendered`;
169
+ const files = [...copy(`${source}/pages`, dest), ...copy(`${source}/dependencies`, dest)];
181
170
 
182
- if (directives.name) {
183
- key = directives.name;
184
- }
171
+ if (fallback) {
172
+ files.push(fallback);
173
+ copy(`${source}/fallback.html`, `${dest}/${fallback}`);
185
174
  }
186
- });
187
-
188
- if (!key) nope();
189
175
 
190
- append(key, body);
191
- });
192
-
193
- return data;
194
- }
195
-
196
- /**
197
- * @param {import('http').IncomingMessage} req
198
- * @returns {Promise<string>}
199
- */
200
- function get_text(req) {
201
- return new Promise((fulfil, reject) => {
202
- let data = '';
176
+ return files;
177
+ },
203
178
 
204
- req.on('error', reject);
179
+ writeServer(dest) {
180
+ return copy(`${config.kit.outDir}/output/server`, dest);
181
+ },
205
182
 
206
- req.on('data', (chunk) => {
207
- data += chunk;
208
- });
183
+ // TODO remove these methods for 1.0
184
+ // @ts-expect-error
185
+ writeStatic() {
186
+ throw new Error(
187
+ `writeStatic has been removed. Please ensure you are using the latest version of ${
188
+ config.kit.adapter.name || 'your adapter'
189
+ }`
190
+ );
191
+ },
209
192
 
210
- req.on('end', () => {
211
- fulfil(data);
212
- });
213
- });
193
+ async prerender() {
194
+ throw new Error(
195
+ 'builder.prerender() has been removed. Prerendering now takes place in the build phase — see builder.prerender and builder.writePrerendered'
196
+ );
197
+ }
198
+ };
214
199
  }
215
200
 
216
201
  /**
217
- * @param {import('http').IncomingMessage} req
218
- * @returns {Promise<ArrayBuffer>}
202
+ * @param {import('types').ValidatedConfig} config
203
+ * @param {import('types').BuildData} build_data
204
+ * @param {import('types').Prerendered} prerendered
205
+ * @param {{ log: import('types').Logger }} opts
219
206
  */
220
- function get_buffer(req) {
221
- return new Promise((fulfil, reject) => {
222
- let data = new Uint8Array(0);
207
+ async function adapt(config, build_data, prerendered, { log }) {
208
+ const { name, adapt } = config.kit.adapter;
223
209
 
224
- req.on('error', reject);
225
-
226
- req.on('data', (chunk) => {
227
- const new_data = new Uint8Array(data.length + chunk.length);
228
-
229
- for (let i = 0; i < data.length; i += 1) {
230
- new_data[i] = data[i];
231
- }
232
-
233
- for (let i = 0; i < chunk.length; i += 1) {
234
- new_data[i + data.length] = chunk[i];
235
- }
210
+ console.log($.bold().cyan(`\n> Using ${name}`));
236
211
 
237
- data = new_data;
238
- });
212
+ const builder = create_builder({ config, build_data, prerendered, log });
213
+ await adapt(builder);
239
214
 
240
- req.on('end', () => {
241
- fulfil(data.buffer);
242
- });
243
- });
215
+ log.success('done');
244
216
  }
245
217
 
246
- export { get_body as g };
218
+ export { adapt };