@sveltejs/kit 1.0.0-next.43 → 1.0.0-next.430

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 (104) hide show
  1. package/README.md +12 -9
  2. package/package.json +95 -63
  3. package/src/cli.js +112 -0
  4. package/src/core/adapt/builder.js +207 -0
  5. package/src/core/adapt/index.js +19 -0
  6. package/src/core/config/index.js +86 -0
  7. package/src/core/config/options.js +488 -0
  8. package/src/core/config/types.d.ts +1 -0
  9. package/src/core/constants.js +5 -0
  10. package/src/core/env.js +97 -0
  11. package/src/core/generate_manifest/index.js +99 -0
  12. package/src/core/prerender/crawl.js +194 -0
  13. package/src/core/prerender/prerender.js +378 -0
  14. package/src/core/prerender/queue.js +80 -0
  15. package/src/core/sync/create_manifest_data/index.js +506 -0
  16. package/src/core/sync/create_manifest_data/types.d.ts +40 -0
  17. package/src/core/sync/sync.js +59 -0
  18. package/src/core/sync/utils.js +44 -0
  19. package/src/core/sync/write_ambient.js +27 -0
  20. package/src/core/sync/write_client_manifest.js +82 -0
  21. package/src/core/sync/write_matchers.js +25 -0
  22. package/src/core/sync/write_root.js +91 -0
  23. package/src/core/sync/write_tsconfig.js +195 -0
  24. package/src/core/sync/write_types.js +775 -0
  25. package/src/core/utils.js +70 -0
  26. package/src/hooks.js +26 -0
  27. package/src/index/index.js +45 -0
  28. package/src/index/private.js +33 -0
  29. package/src/node/index.js +145 -0
  30. package/src/node/polyfills.js +40 -0
  31. package/src/runtime/app/env.js +11 -0
  32. package/src/runtime/app/navigation.js +22 -0
  33. package/src/runtime/app/paths.js +1 -0
  34. package/src/runtime/app/stores.js +102 -0
  35. package/src/runtime/client/ambient.d.ts +17 -0
  36. package/src/runtime/client/client.js +1289 -0
  37. package/src/runtime/client/fetcher.js +60 -0
  38. package/src/runtime/client/parse.js +36 -0
  39. package/src/runtime/client/singletons.js +21 -0
  40. package/src/runtime/client/start.js +46 -0
  41. package/src/runtime/client/types.d.ts +105 -0
  42. package/src/runtime/client/utils.js +113 -0
  43. package/src/runtime/components/error.svelte +16 -0
  44. package/{assets → src/runtime}/components/layout.svelte +0 -0
  45. package/src/runtime/env/dynamic/private.js +1 -0
  46. package/src/runtime/env/dynamic/public.js +1 -0
  47. package/src/runtime/env-private.js +7 -0
  48. package/src/runtime/env-public.js +7 -0
  49. package/src/runtime/env.js +6 -0
  50. package/src/runtime/hash.js +16 -0
  51. package/src/runtime/paths.js +11 -0
  52. package/src/runtime/server/endpoint.js +58 -0
  53. package/src/runtime/server/index.js +448 -0
  54. package/src/runtime/server/page/cookie.js +25 -0
  55. package/src/runtime/server/page/crypto.js +239 -0
  56. package/src/runtime/server/page/csp.js +249 -0
  57. package/src/runtime/server/page/fetch.js +266 -0
  58. package/src/runtime/server/page/index.js +416 -0
  59. package/src/runtime/server/page/load_data.js +135 -0
  60. package/src/runtime/server/page/render.js +362 -0
  61. package/src/runtime/server/page/respond_with_error.js +94 -0
  62. package/src/runtime/server/page/types.d.ts +44 -0
  63. package/src/runtime/server/utils.js +116 -0
  64. package/src/utils/error.js +22 -0
  65. package/src/utils/escape.js +104 -0
  66. package/src/utils/filesystem.js +108 -0
  67. package/src/utils/http.js +55 -0
  68. package/src/utils/misc.js +1 -0
  69. package/src/utils/routing.js +108 -0
  70. package/src/utils/url.js +97 -0
  71. package/src/vite/build/build_server.js +337 -0
  72. package/src/vite/build/build_service_worker.js +90 -0
  73. package/src/vite/build/utils.js +160 -0
  74. package/src/vite/dev/index.js +551 -0
  75. package/src/vite/index.js +574 -0
  76. package/src/vite/preview/index.js +186 -0
  77. package/src/vite/types.d.ts +3 -0
  78. package/src/vite/utils.js +345 -0
  79. package/svelte-kit.js +1 -1
  80. package/types/ambient.d.ts +357 -0
  81. package/types/index.d.ts +343 -0
  82. package/types/internal.d.ts +308 -0
  83. package/types/private.d.ts +209 -0
  84. package/CHANGELOG.md +0 -431
  85. package/assets/components/error.svelte +0 -13
  86. package/assets/runtime/app/env.js +0 -5
  87. package/assets/runtime/app/navigation.js +0 -41
  88. package/assets/runtime/app/paths.js +0 -1
  89. package/assets/runtime/app/stores.js +0 -93
  90. package/assets/runtime/chunks/utils.js +0 -19
  91. package/assets/runtime/internal/singletons.js +0 -23
  92. package/assets/runtime/internal/start.js +0 -770
  93. package/assets/runtime/paths.js +0 -12
  94. package/dist/.DS_Store +0 -0
  95. package/dist/chunks/index.js +0 -3521
  96. package/dist/chunks/index2.js +0 -587
  97. package/dist/chunks/index3.js +0 -246
  98. package/dist/chunks/index4.js +0 -538
  99. package/dist/chunks/index5.js +0 -761
  100. package/dist/chunks/index6.js +0 -322
  101. package/dist/chunks/standard.js +0 -99
  102. package/dist/chunks/utils.js +0 -83
  103. package/dist/cli.js +0 -546
  104. package/dist/ssr.js +0 -2581
@@ -0,0 +1,249 @@
1
+ import { escape_html_attr } from '../../../utils/escape.js';
2
+ import { sha256, base64 } from './crypto.js';
3
+
4
+ const array = new Uint8Array(16);
5
+
6
+ function generate_nonce() {
7
+ crypto.getRandomValues(array);
8
+ return base64(array);
9
+ }
10
+
11
+ const quoted = new Set([
12
+ 'self',
13
+ 'unsafe-eval',
14
+ 'unsafe-hashes',
15
+ 'unsafe-inline',
16
+ 'none',
17
+ 'strict-dynamic',
18
+ 'report-sample'
19
+ ]);
20
+
21
+ const crypto_pattern = /^(nonce|sha\d\d\d)-/;
22
+
23
+ // CSP and CSP Report Only are extremely similar with a few caveats
24
+ // the easiest/DRYest way to express this is with some private encapsulation
25
+ class BaseProvider {
26
+ /** @type {boolean} */
27
+ #use_hashes;
28
+
29
+ /** @type {boolean} */
30
+ #script_needs_csp;
31
+
32
+ /** @type {boolean} */
33
+ #style_needs_csp;
34
+
35
+ /** @type {import('types').CspDirectives} */
36
+ #directives;
37
+
38
+ /** @type {import('types').Csp.Source[]} */
39
+ #script_src;
40
+
41
+ /** @type {import('types').Csp.Source[]} */
42
+ #style_src;
43
+
44
+ /** @type {string} */
45
+ #nonce;
46
+
47
+ /**
48
+ * @param {boolean} use_hashes
49
+ * @param {import('types').CspDirectives} directives
50
+ * @param {string} nonce
51
+ * @param {boolean} dev
52
+ */
53
+ constructor(use_hashes, directives, nonce, dev) {
54
+ this.#use_hashes = use_hashes;
55
+ this.#directives = dev ? { ...directives } : directives; // clone in dev so we can safely mutate
56
+
57
+ const d = this.#directives;
58
+
59
+ if (dev) {
60
+ // remove strict-dynamic in dev...
61
+ // TODO reinstate this if we can figure out how to make strict-dynamic work
62
+ // if (d['default-src']) {
63
+ // d['default-src'] = d['default-src'].filter((name) => name !== 'strict-dynamic');
64
+ // if (d['default-src'].length === 0) delete d['default-src'];
65
+ // }
66
+
67
+ // if (d['script-src']) {
68
+ // d['script-src'] = d['script-src'].filter((name) => name !== 'strict-dynamic');
69
+ // if (d['script-src'].length === 0) delete d['script-src'];
70
+ // }
71
+
72
+ const effective_style_src = d['style-src'] || d['default-src'];
73
+
74
+ // ...and add unsafe-inline so we can inject <style> elements
75
+ if (effective_style_src && !effective_style_src.includes('unsafe-inline')) {
76
+ d['style-src'] = [...effective_style_src, 'unsafe-inline'];
77
+ }
78
+ }
79
+
80
+ this.#script_src = [];
81
+ this.#style_src = [];
82
+
83
+ const effective_script_src = d['script-src'] || d['default-src'];
84
+ const effective_style_src = d['style-src'] || d['default-src'];
85
+
86
+ this.#script_needs_csp =
87
+ !!effective_script_src &&
88
+ effective_script_src.filter((value) => value !== 'unsafe-inline').length > 0;
89
+
90
+ this.#style_needs_csp =
91
+ !dev &&
92
+ !!effective_style_src &&
93
+ effective_style_src.filter((value) => value !== 'unsafe-inline').length > 0;
94
+
95
+ this.script_needs_nonce = this.#script_needs_csp && !this.#use_hashes;
96
+ this.style_needs_nonce = this.#style_needs_csp && !this.#use_hashes;
97
+ this.#nonce = nonce;
98
+ }
99
+
100
+ /** @param {string} content */
101
+ add_script(content) {
102
+ if (this.#script_needs_csp) {
103
+ if (this.#use_hashes) {
104
+ this.#script_src.push(`sha256-${sha256(content)}`);
105
+ } else if (this.#script_src.length === 0) {
106
+ this.#script_src.push(`nonce-${this.#nonce}`);
107
+ }
108
+ }
109
+ }
110
+
111
+ /** @param {string} content */
112
+ add_style(content) {
113
+ if (this.#style_needs_csp) {
114
+ if (this.#use_hashes) {
115
+ this.#style_src.push(`sha256-${sha256(content)}`);
116
+ } else if (this.#style_src.length === 0) {
117
+ this.#style_src.push(`nonce-${this.#nonce}`);
118
+ }
119
+ }
120
+ }
121
+
122
+ /**
123
+ * @param {boolean} [is_meta]
124
+ */
125
+ get_header(is_meta = false) {
126
+ const header = [];
127
+
128
+ // due to browser inconsistencies, we can't append sources to default-src
129
+ // (specifically, Firefox appears to not ignore nonce-{nonce} directives
130
+ // on default-src), so we ensure that script-src and style-src exist
131
+
132
+ const directives = { ...this.#directives };
133
+
134
+ if (this.#style_src.length > 0) {
135
+ directives['style-src'] = [
136
+ ...(directives['style-src'] || directives['default-src'] || []),
137
+ ...this.#style_src
138
+ ];
139
+ }
140
+
141
+ if (this.#script_src.length > 0) {
142
+ directives['script-src'] = [
143
+ ...(directives['script-src'] || directives['default-src'] || []),
144
+ ...this.#script_src
145
+ ];
146
+ }
147
+
148
+ for (const key in directives) {
149
+ if (is_meta && (key === 'frame-ancestors' || key === 'report-uri' || key === 'sandbox')) {
150
+ // these values cannot be used with a <meta> tag
151
+ // TODO warn?
152
+ continue;
153
+ }
154
+
155
+ // @ts-expect-error gimme a break typescript, `key` is obviously a member of internal_directives
156
+ const value = /** @type {string[] | true} */ (directives[key]);
157
+
158
+ if (!value) continue;
159
+
160
+ const directive = [key];
161
+ if (Array.isArray(value)) {
162
+ value.forEach((value) => {
163
+ if (quoted.has(value) || crypto_pattern.test(value)) {
164
+ directive.push(`'${value}'`);
165
+ } else {
166
+ directive.push(value);
167
+ }
168
+ });
169
+ }
170
+
171
+ header.push(directive.join(' '));
172
+ }
173
+
174
+ return header.join('; ');
175
+ }
176
+ }
177
+
178
+ class CspProvider extends BaseProvider {
179
+ get_meta() {
180
+ const content = escape_html_attr(this.get_header(true));
181
+ return `<meta http-equiv="content-security-policy" content=${content}>`;
182
+ }
183
+ }
184
+
185
+ class CspReportOnlyProvider extends BaseProvider {
186
+ /**
187
+ * @param {boolean} use_hashes
188
+ * @param {import('types').CspDirectives} directives
189
+ * @param {string} nonce
190
+ * @param {boolean} dev
191
+ */
192
+ constructor(use_hashes, directives, nonce, dev) {
193
+ super(use_hashes, directives, nonce, dev);
194
+
195
+ if (Object.values(directives).filter((v) => !!v).length > 0) {
196
+ // If we're generating content-security-policy-report-only,
197
+ // if there are any directives, we need a report-uri or report-to (or both)
198
+ // else it's just an expensive noop.
199
+ const has_report_to = directives['report-to']?.length ?? 0 > 0;
200
+ const has_report_uri = directives['report-uri']?.length ?? 0 > 0;
201
+ if (!has_report_to && !has_report_uri) {
202
+ throw Error(
203
+ '`content-security-policy-report-only` must be specified with either the `report-to` or `report-uri` directives, or both'
204
+ );
205
+ }
206
+ }
207
+ }
208
+ }
209
+
210
+ export class Csp {
211
+ /** @readonly */
212
+ nonce = generate_nonce();
213
+
214
+ /** @type {CspProvider} */
215
+ csp_provider;
216
+
217
+ /** @type {CspReportOnlyProvider} */
218
+ report_only_provider;
219
+
220
+ /**
221
+ * @param {import('./types').CspConfig} config
222
+ * @param {import('./types').CspOpts} opts
223
+ */
224
+ constructor({ mode, directives, reportOnly }, { prerender, dev }) {
225
+ const use_hashes = mode === 'hash' || (mode === 'auto' && prerender);
226
+ this.csp_provider = new CspProvider(use_hashes, directives, this.nonce, dev);
227
+ this.report_only_provider = new CspReportOnlyProvider(use_hashes, reportOnly, this.nonce, dev);
228
+ }
229
+
230
+ get script_needs_nonce() {
231
+ return this.csp_provider.script_needs_nonce || this.report_only_provider.script_needs_nonce;
232
+ }
233
+
234
+ get style_needs_nonce() {
235
+ return this.csp_provider.style_needs_nonce || this.report_only_provider.style_needs_nonce;
236
+ }
237
+
238
+ /** @param {string} content */
239
+ add_script(content) {
240
+ this.csp_provider.add_script(content);
241
+ this.report_only_provider.add_script(content);
242
+ }
243
+
244
+ /** @param {string} content */
245
+ add_style(content) {
246
+ this.csp_provider.add_style(content);
247
+ this.report_only_provider.add_style(content);
248
+ }
249
+ }
@@ -0,0 +1,266 @@
1
+ import * as cookie from 'cookie';
2
+ import * as set_cookie_parser from 'set-cookie-parser';
3
+ import { respond } from '../index.js';
4
+ import { is_root_relative, resolve } from '../../../utils/url.js';
5
+ import { domain_matches, path_matches } from './cookie.js';
6
+
7
+ /**
8
+ * @param {{
9
+ * event: import('types').RequestEvent;
10
+ * options: import('types').SSROptions;
11
+ * state: import('types').SSRState;
12
+ * route: import('types').SSRPage | import('types').SSRErrorPage;
13
+ * }} opts
14
+ */
15
+ export function create_fetch({ event, options, state, route }) {
16
+ /** @type {import('./types').Fetched[]} */
17
+ const fetched = [];
18
+
19
+ const initial_cookies = cookie.parse(event.request.headers.get('cookie') || '');
20
+
21
+ /** @type {import('set-cookie-parser').Cookie[]} */
22
+ const cookies = [];
23
+
24
+ /** @type {typeof fetch} */
25
+ const fetcher = async (resource, opts = {}) => {
26
+ /** @type {string} */
27
+ let requested;
28
+
29
+ if (typeof resource === 'string' || resource instanceof URL) {
30
+ requested = resource.toString();
31
+ } else {
32
+ requested = resource.url;
33
+
34
+ opts = {
35
+ method: resource.method,
36
+ headers: resource.headers,
37
+ body: resource.body,
38
+ mode: resource.mode,
39
+ credentials: resource.credentials,
40
+ cache: resource.cache,
41
+ redirect: resource.redirect,
42
+ referrer: resource.referrer,
43
+ integrity: resource.integrity,
44
+ ...opts
45
+ };
46
+ }
47
+
48
+ opts.headers = new Headers(opts.headers);
49
+
50
+ // merge headers from request
51
+ for (const [key, value] of event.request.headers) {
52
+ if (
53
+ key !== 'authorization' &&
54
+ key !== 'connection' &&
55
+ key !== 'content-length' &&
56
+ key !== 'cookie' &&
57
+ key !== 'host' &&
58
+ key !== 'if-none-match' &&
59
+ !opts.headers.has(key)
60
+ ) {
61
+ opts.headers.set(key, value);
62
+ }
63
+ }
64
+
65
+ const resolved = resolve(event.url.pathname, requested.split('?')[0]);
66
+
67
+ /** @type {Response} */
68
+ let response;
69
+
70
+ /** @type {import('types').PrerenderDependency} */
71
+ let dependency;
72
+
73
+ // handle fetch requests for static assets. e.g. prebaked data, etc.
74
+ // we need to support everything the browser's fetch supports
75
+ const prefix = options.paths.assets || options.paths.base;
76
+ const filename = decodeURIComponent(
77
+ resolved.startsWith(prefix) ? resolved.slice(prefix.length) : resolved
78
+ ).slice(1);
79
+ const filename_html = `${filename}/index.html`; // path may also match path/index.html
80
+
81
+ const is_asset = options.manifest.assets.has(filename);
82
+ const is_asset_html = options.manifest.assets.has(filename_html);
83
+
84
+ if (is_asset || is_asset_html) {
85
+ const file = is_asset ? filename : filename_html;
86
+
87
+ if (options.read) {
88
+ const type = is_asset
89
+ ? options.manifest.mimeTypes[filename.slice(filename.lastIndexOf('.'))]
90
+ : 'text/html';
91
+
92
+ response = new Response(options.read(file), {
93
+ headers: type ? { 'content-type': type } : {}
94
+ });
95
+ } else {
96
+ response = await fetch(`${event.url.origin}/${file}`, /** @type {RequestInit} */ (opts));
97
+ }
98
+ } else if (is_root_relative(resolved)) {
99
+ if (opts.credentials !== 'omit') {
100
+ const authorization = event.request.headers.get('authorization');
101
+
102
+ // combine cookies from the initiating request with any that were
103
+ // added via set-cookie
104
+ const combined_cookies = { ...initial_cookies };
105
+
106
+ for (const cookie of cookies) {
107
+ if (!domain_matches(event.url.hostname, cookie.domain)) continue;
108
+ if (!path_matches(resolved, cookie.path)) continue;
109
+
110
+ combined_cookies[cookie.name] = cookie.value;
111
+ }
112
+
113
+ const cookie = Object.entries(combined_cookies)
114
+ .map(([name, value]) => `${name}=${value}`)
115
+ .join('; ');
116
+
117
+ if (cookie) {
118
+ opts.headers.set('cookie', cookie);
119
+ }
120
+
121
+ if (authorization && !opts.headers.has('authorization')) {
122
+ opts.headers.set('authorization', authorization);
123
+ }
124
+ }
125
+
126
+ if (opts.body && typeof opts.body !== 'string') {
127
+ // per https://developer.mozilla.org/en-US/docs/Web/API/Request/Request, this can be a
128
+ // Blob, BufferSource, FormData, URLSearchParams, USVString, or ReadableStream object.
129
+ // non-string bodies are irksome to deal with, but luckily aren't particularly useful
130
+ // in this context anyway, so we take the easy route and ban them
131
+ throw new Error('Request body must be a string');
132
+ }
133
+
134
+ response = await respond(
135
+ new Request(new URL(requested, event.url).href, { ...opts }),
136
+ options,
137
+ {
138
+ ...state,
139
+ initiator: route
140
+ }
141
+ );
142
+
143
+ if (state.prerendering) {
144
+ dependency = { response, body: null };
145
+ state.prerendering.dependencies.set(resolved, dependency);
146
+ }
147
+ } else {
148
+ // external
149
+ if (resolved.startsWith('//')) {
150
+ requested = event.url.protocol + requested;
151
+ }
152
+
153
+ // external fetch
154
+ // allow cookie passthrough for "same-origin"
155
+ // if SvelteKit is serving my.domain.com:
156
+ // - domain.com WILL NOT receive cookies
157
+ // - my.domain.com WILL receive cookies
158
+ // - api.domain.dom WILL NOT receive cookies
159
+ // - sub.my.domain.com WILL receive cookies
160
+ // ports do not affect the resolution
161
+ // leading dot prevents mydomain.com matching domain.com
162
+ if (
163
+ `.${new URL(requested).hostname}`.endsWith(`.${event.url.hostname}`) &&
164
+ opts.credentials !== 'omit'
165
+ ) {
166
+ const cookie = event.request.headers.get('cookie');
167
+ if (cookie) opts.headers.set('cookie', cookie);
168
+ }
169
+
170
+ // we need to delete the connection header, as explained here:
171
+ // https://github.com/nodejs/undici/issues/1470#issuecomment-1140798467
172
+ // TODO this may be a case for being selective about which headers we let through
173
+ opts.headers.delete('connection');
174
+
175
+ const external_request = new Request(requested, /** @type {RequestInit} */ (opts));
176
+ response = await options.hooks.externalFetch.call(null, external_request);
177
+ }
178
+
179
+ const set_cookie = response.headers.get('set-cookie');
180
+ if (set_cookie) {
181
+ cookies.push(
182
+ ...set_cookie_parser
183
+ .splitCookiesString(set_cookie)
184
+ .map((str) => set_cookie_parser.parseString(str))
185
+ );
186
+ }
187
+
188
+ const proxy = new Proxy(response, {
189
+ get(response, key, _receiver) {
190
+ async function text() {
191
+ const body = await response.text();
192
+
193
+ /** @type {import('types').ResponseHeaders} */
194
+ const headers = {};
195
+ for (const [key, value] of response.headers) {
196
+ // TODO skip others besides set-cookie and etag?
197
+ if (key !== 'set-cookie' && key !== 'etag') {
198
+ headers[key] = value;
199
+ }
200
+ }
201
+
202
+ if (!opts.body || typeof opts.body === 'string') {
203
+ const status_number = Number(response.status);
204
+ if (isNaN(status_number)) {
205
+ throw new Error(
206
+ `response.status is not a number. value: "${
207
+ response.status
208
+ }" type: ${typeof response.status}`
209
+ );
210
+ }
211
+
212
+ fetched.push({
213
+ url: requested,
214
+ body: opts.body,
215
+ response: {
216
+ status: status_number,
217
+ statusText: response.statusText,
218
+ headers,
219
+ body
220
+ }
221
+ });
222
+ }
223
+
224
+ if (dependency) {
225
+ dependency.body = body;
226
+ }
227
+
228
+ return body;
229
+ }
230
+
231
+ if (key === 'arrayBuffer') {
232
+ return async () => {
233
+ const buffer = await response.arrayBuffer();
234
+
235
+ if (dependency) {
236
+ dependency.body = new Uint8Array(buffer);
237
+ }
238
+
239
+ // TODO should buffer be inlined into the page (albeit base64'd)?
240
+ // any conditions in which it shouldn't be?
241
+
242
+ return buffer;
243
+ };
244
+ }
245
+
246
+ if (key === 'text') {
247
+ return text;
248
+ }
249
+
250
+ if (key === 'json') {
251
+ return async () => {
252
+ return JSON.parse(await text());
253
+ };
254
+ }
255
+
256
+ // TODO arrayBuffer?
257
+
258
+ return Reflect.get(response, key, response);
259
+ }
260
+ });
261
+
262
+ return proxy;
263
+ };
264
+
265
+ return { fetcher, fetched, cookies };
266
+ }