@sveltejs/kit 1.0.0-next.42 → 1.0.0-next.422

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 (126) 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 +482 -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 +91 -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 +88 -0
  23. package/src/core/sync/write_tsconfig.js +189 -0
  24. package/src/core/sync/write_types.js +769 -0
  25. package/src/core/utils.js +58 -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 +91 -0
  35. package/src/runtime/client/ambient.d.ts +17 -0
  36. package/src/runtime/client/client.js +1258 -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 +11 -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 +127 -0
  60. package/src/runtime/server/page/render.js +359 -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 +107 -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 +157 -0
  74. package/src/vite/dev/index.js +568 -0
  75. package/src/vite/index.js +566 -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 +334 -0
  79. package/svelte-kit.js +1 -1
  80. package/types/ambient.d.ts +355 -0
  81. package/types/index.d.ts +342 -0
  82. package/types/internal.d.ts +308 -0
  83. package/types/private.d.ts +209 -0
  84. package/CHANGELOG.md +0 -425
  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/api.js +0 -28
  95. package/dist/api.js.map +0 -1
  96. package/dist/chunks/index.js +0 -3521
  97. package/dist/chunks/index2.js +0 -587
  98. package/dist/chunks/index3.js +0 -246
  99. package/dist/chunks/index4.js +0 -530
  100. package/dist/chunks/index5.js +0 -761
  101. package/dist/chunks/index6.js +0 -322
  102. package/dist/chunks/standard.js +0 -99
  103. package/dist/chunks/utils.js +0 -83
  104. package/dist/cli.js +0 -546
  105. package/dist/cli.js.map +0 -1
  106. package/dist/create_app.js +0 -592
  107. package/dist/create_app.js.map +0 -1
  108. package/dist/index.js +0 -392
  109. package/dist/index.js.map +0 -1
  110. package/dist/index2.js +0 -3519
  111. package/dist/index2.js.map +0 -1
  112. package/dist/index3.js +0 -320
  113. package/dist/index3.js.map +0 -1
  114. package/dist/index4.js +0 -323
  115. package/dist/index4.js.map +0 -1
  116. package/dist/index5.js +0 -247
  117. package/dist/index5.js.map +0 -1
  118. package/dist/index6.js +0 -761
  119. package/dist/index6.js.map +0 -1
  120. package/dist/renderer.js +0 -2499
  121. package/dist/renderer.js.map +0 -1
  122. package/dist/ssr.js +0 -2581
  123. package/dist/standard.js +0 -100
  124. package/dist/standard.js.map +0 -1
  125. package/dist/utils.js +0 -84
  126. package/dist/utils.js.map +0 -1
@@ -0,0 +1,58 @@
1
+ import path from 'path';
2
+ import colors from 'kleur';
3
+ import { fileURLToPath } from 'url';
4
+ import { posixify } from '../utils/filesystem.js';
5
+
6
+ /**
7
+ * Resolved path of the `runtime` directory
8
+ *
9
+ * TODO Windows issue:
10
+ * Vite or sth else somehow sets the driver letter inconsistently to lower or upper case depending on the run environment.
11
+ * In playwright debug mode run through VS Code this a root-to-lowercase conversion is needed in order for the tests to run.
12
+ * If we do this conversion in other cases it has the opposite effect though and fails.
13
+ */
14
+ export const runtime_directory = posixify(fileURLToPath(new URL('../runtime', import.meta.url)));
15
+
16
+ /** Prefix for the `runtime` directory, for use with import declarations */
17
+ export const runtime_prefix = posixify_path(runtime_directory);
18
+
19
+ /** @param {string} str */
20
+ function posixify_path(str) {
21
+ const parsed = path.parse(str);
22
+ return `/${parsed.dir.slice(parsed.root.length).split(path.sep).join('/')}/${parsed.base}`;
23
+ }
24
+
25
+ function noop() {}
26
+
27
+ /** @param {{ verbose: boolean }} opts */
28
+ export function logger({ verbose }) {
29
+ /** @type {import('types').Logger} */
30
+ const log = (msg) => console.log(msg.replace(/^/gm, ' '));
31
+
32
+ /** @param {string} msg */
33
+ const err = (msg) => console.error(msg.replace(/^/gm, ' '));
34
+
35
+ log.success = (msg) => log(colors.green(`✔ ${msg}`));
36
+ log.error = (msg) => err(colors.bold().red(msg));
37
+ log.warn = (msg) => log(colors.bold().yellow(msg));
38
+
39
+ log.minor = verbose ? (msg) => log(colors.grey(msg)) : noop;
40
+ log.info = verbose ? log : noop;
41
+
42
+ return log;
43
+ }
44
+
45
+ /** @param {import('types').ManifestData} manifest_data */
46
+ export function get_mime_lookup(manifest_data) {
47
+ /** @type {Record<string, string>} */
48
+ const mime = {};
49
+
50
+ manifest_data.assets.forEach((asset) => {
51
+ if (asset.type) {
52
+ const ext = path.extname(asset.file);
53
+ mime[ext] = asset.type;
54
+ }
55
+ });
56
+
57
+ return mime;
58
+ }
package/src/hooks.js ADDED
@@ -0,0 +1,26 @@
1
+ /**
2
+ * @param {...import('types').Handle} handlers
3
+ * @returns {import('types').Handle}
4
+ */
5
+ export function sequence(...handlers) {
6
+ const length = handlers.length;
7
+ if (!length) return ({ event, resolve }) => resolve(event);
8
+
9
+ return ({ event, resolve }) => {
10
+ return apply_handle(0, event);
11
+
12
+ /**
13
+ * @param {number} i
14
+ * @param {import('types').RequestEvent} event
15
+ * @returns {import('types').MaybePromise<Response>}
16
+ */
17
+ function apply_handle(i, event) {
18
+ const handle = handlers[i];
19
+
20
+ return handle({
21
+ event,
22
+ resolve: i < length - 1 ? (event) => apply_handle(i + 1, event) : resolve
23
+ });
24
+ }
25
+ };
26
+ }
@@ -0,0 +1,45 @@
1
+ import { HttpError, Redirect } from './private.js';
2
+
3
+ /**
4
+ * Creates an `HttpError` object with an HTTP status code and an optional message.
5
+ * This object, if thrown during request handling, will cause SvelteKit to
6
+ * return an error response without invoking `handleError`
7
+ * @param {number} status
8
+ * @param {string | undefined} [message]
9
+ */
10
+ export function error(status, message) {
11
+ return new HttpError(status, message);
12
+ }
13
+
14
+ /**
15
+ * Creates a `Redirect` object. If thrown during request handling, SvelteKit will
16
+ * return a redirect response.
17
+ * @param {number} status
18
+ * @param {string} location
19
+ */
20
+ export function redirect(status, location) {
21
+ if (isNaN(status) || status < 300 || status > 399) {
22
+ throw new Error('Invalid status code');
23
+ }
24
+
25
+ return new Redirect(status, location);
26
+ }
27
+
28
+ /**
29
+ * Generates a JSON `Response` object from the supplied data.
30
+ * @param {any} data
31
+ * @param {ResponseInit} [init]
32
+ */
33
+ export function json(data, init) {
34
+ // TODO deprecate this in favour of `Response.json` when it's
35
+ // more widely supported
36
+ const headers = new Headers(init?.headers);
37
+ if (!headers.has('content-type')) {
38
+ headers.set('content-type', 'application/json');
39
+ }
40
+
41
+ return new Response(JSON.stringify(data), {
42
+ ...init,
43
+ headers
44
+ });
45
+ }
@@ -0,0 +1,33 @@
1
+ export class HttpError {
2
+ // without these, things like `$page.error.stack` will error. we don't want to
3
+ // include a stack for these sorts of errors, but we also don't want red
4
+ // squigglies everywhere, so this feels like a not-terribile compromise
5
+ name = 'HttpError';
6
+
7
+ /** @type {void} */
8
+ stack = undefined;
9
+
10
+ /**
11
+ * @param {number} status
12
+ * @param {string | undefined} message
13
+ */
14
+ constructor(status, message) {
15
+ this.status = status;
16
+ this.message = message ?? `Error: ${status}`;
17
+ }
18
+
19
+ toString() {
20
+ return this.message;
21
+ }
22
+ }
23
+
24
+ export class Redirect {
25
+ /**
26
+ * @param {number} status
27
+ * @param {string} location
28
+ */
29
+ constructor(status, location) {
30
+ this.status = status;
31
+ this.location = location;
32
+ }
33
+ }
@@ -0,0 +1,145 @@
1
+ import * as set_cookie_parser from 'set-cookie-parser';
2
+
3
+ /** @param {import('http').IncomingMessage} req */
4
+ function get_raw_body(req) {
5
+ const h = req.headers;
6
+
7
+ if (!h['content-type']) {
8
+ return null;
9
+ }
10
+
11
+ const length = Number(h['content-length']);
12
+
13
+ // check if no request body
14
+ // https://github.com/jshttp/type-is/blob/c1f4388c71c8a01f79934e68f630ca4a15fffcd6/index.js#L81-L95
15
+ if (isNaN(length) && h['transfer-encoding'] == null) {
16
+ return null;
17
+ }
18
+
19
+ if (req.destroyed) {
20
+ const readable = new ReadableStream();
21
+ readable.cancel();
22
+ return readable;
23
+ }
24
+
25
+ let size = 0;
26
+ let cancelled = false;
27
+
28
+ return new ReadableStream({
29
+ start(controller) {
30
+ req.on('error', (error) => {
31
+ controller.error(error);
32
+ });
33
+
34
+ req.on('end', () => {
35
+ if (cancelled) return;
36
+ controller.close();
37
+ });
38
+
39
+ req.on('data', (chunk) => {
40
+ if (cancelled) return;
41
+
42
+ size += chunk.length;
43
+ if (size > length) {
44
+ controller.error(new Error('content-length exceeded'));
45
+ return;
46
+ }
47
+
48
+ controller.enqueue(chunk);
49
+
50
+ if (controller.desiredSize === null || controller.desiredSize <= 0) {
51
+ req.pause();
52
+ }
53
+ });
54
+ },
55
+
56
+ pull() {
57
+ req.resume();
58
+ },
59
+
60
+ cancel(reason) {
61
+ cancelled = true;
62
+ req.destroy(reason);
63
+ }
64
+ });
65
+ }
66
+
67
+ /** @type {import('@sveltejs/kit/node').getRequest} */
68
+ export async function getRequest(base, req) {
69
+ let headers = /** @type {Record<string, string>} */ (req.headers);
70
+ if (req.httpVersionMajor === 2) {
71
+ // we need to strip out the HTTP/2 pseudo-headers because node-fetch's
72
+ // Request implementation doesn't like them
73
+ // TODO is this still true with Node 18
74
+ headers = Object.assign({}, headers);
75
+ delete headers[':method'];
76
+ delete headers[':path'];
77
+ delete headers[':authority'];
78
+ delete headers[':scheme'];
79
+ }
80
+
81
+ return new Request(base + req.url, {
82
+ method: req.method,
83
+ headers,
84
+ body: get_raw_body(req)
85
+ });
86
+ }
87
+
88
+ /** @type {import('@sveltejs/kit/node').setResponse} */
89
+ export async function setResponse(res, response) {
90
+ const headers = Object.fromEntries(response.headers);
91
+
92
+ if (response.headers.has('set-cookie')) {
93
+ const header = /** @type {string} */ (response.headers.get('set-cookie'));
94
+ const split = set_cookie_parser.splitCookiesString(header);
95
+
96
+ // @ts-expect-error
97
+ headers['set-cookie'] = split;
98
+ }
99
+
100
+ res.writeHead(response.status, headers);
101
+
102
+ if (!response.body) {
103
+ res.end();
104
+ return;
105
+ }
106
+
107
+ const reader = response.body.getReader();
108
+
109
+ if (res.destroyed) {
110
+ reader.cancel();
111
+ return;
112
+ }
113
+
114
+ const cancel = (/** @type {Error|undefined} */ error) => {
115
+ res.off('close', cancel);
116
+ res.off('error', cancel);
117
+
118
+ // If the reader has already been interrupted with an error earlier,
119
+ // then it will appear here, it is useless, but it needs to be catch.
120
+ reader.cancel(error).catch(() => {});
121
+ if (error) res.destroy(error);
122
+ };
123
+
124
+ res.on('close', cancel);
125
+ res.on('error', cancel);
126
+
127
+ next();
128
+ async function next() {
129
+ try {
130
+ for (;;) {
131
+ const { done, value } = await reader.read();
132
+
133
+ if (done) break;
134
+
135
+ if (!res.write(value)) {
136
+ res.once('drain', next);
137
+ return;
138
+ }
139
+ }
140
+ res.end();
141
+ } catch (error) {
142
+ cancel(error instanceof Error ? error : new Error(String(error)));
143
+ }
144
+ }
145
+ }
@@ -0,0 +1,40 @@
1
+ import { fetch, Response, Request, Headers } from 'undici';
2
+ import { ReadableStream, TransformStream, WritableStream } from 'stream/web';
3
+ import { Readable } from 'stream';
4
+ import { Request as NodeFetchRequest } from 'node-fetch';
5
+ import { webcrypto as crypto } from 'crypto';
6
+
7
+ /** @type {Record<string, any>} */
8
+ const globals = {
9
+ crypto,
10
+ fetch,
11
+ Response,
12
+ // TODO remove the superclass as soon as Undici supports formData
13
+ // https://github.com/nodejs/undici/issues/974
14
+ Request: class extends Request {
15
+ // @ts-expect-error
16
+ formData() {
17
+ return new NodeFetchRequest(this.url, {
18
+ method: this.method,
19
+ headers: this.headers,
20
+ body: this.body && Readable.from(this.body)
21
+ }).formData();
22
+ }
23
+ },
24
+ Headers,
25
+ ReadableStream,
26
+ TransformStream,
27
+ WritableStream
28
+ };
29
+
30
+ // exported for dev/preview and node environments
31
+ export function installPolyfills() {
32
+ for (const name in globals) {
33
+ Object.defineProperty(globalThis, name, {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: globals[name]
38
+ });
39
+ }
40
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @type {import('$app/env').browser}
3
+ */
4
+ export const browser = !import.meta.env.SSR;
5
+
6
+ /**
7
+ * @type {import('$app/env').dev}
8
+ */
9
+ export const dev = __SVELTEKIT_DEV__;
10
+
11
+ export { prerendering } from '../env.js';
@@ -0,0 +1,22 @@
1
+ import { client } from '../client/singletons.js';
2
+
3
+ /**
4
+ * @param {string} name
5
+ */
6
+ function guard(name) {
7
+ return () => {
8
+ throw new Error(`Cannot call ${name}(...) on the server`);
9
+ };
10
+ }
11
+
12
+ const ssr = import.meta.env.SSR;
13
+
14
+ export const disableScrollHandling = ssr
15
+ ? guard('disableScrollHandling')
16
+ : client.disable_scroll_handling;
17
+ export const goto = ssr ? guard('goto') : client.goto;
18
+ export const invalidate = ssr ? guard('invalidate') : client.invalidate;
19
+ export const prefetch = ssr ? guard('prefetch') : client.prefetch;
20
+ export const prefetchRoutes = ssr ? guard('prefetchRoutes') : client.prefetch_routes;
21
+ export const beforeNavigate = ssr ? () => {} : client.before_navigate;
22
+ export const afterNavigate = ssr ? () => {} : client.after_navigate;
@@ -0,0 +1 @@
1
+ export { base, assets } from '../paths.js';
@@ -0,0 +1,91 @@
1
+ import { getContext } from 'svelte';
2
+ import { browser } from './env.js';
3
+
4
+ // TODO remove this (for 1.0? after 1.0?)
5
+ let warned = false;
6
+ export function stores() {
7
+ if (!warned) {
8
+ console.error('stores() is deprecated; use getStores() instead');
9
+ warned = true;
10
+ }
11
+ return getStores();
12
+ }
13
+
14
+ /**
15
+ * @type {import('$app/stores').getStores}
16
+ */
17
+ export const getStores = () => {
18
+ const stores = getContext('__svelte__');
19
+
20
+ return {
21
+ page: {
22
+ subscribe: stores.page.subscribe
23
+ },
24
+ navigating: {
25
+ subscribe: stores.navigating.subscribe
26
+ },
27
+ // TODO remove this (for 1.0? after 1.0?)
28
+ // @ts-expect-error - deprecated, not part of type definitions, but still callable
29
+ get preloading() {
30
+ console.error('stores.preloading is deprecated; use stores.navigating instead');
31
+ return {
32
+ subscribe: stores.navigating.subscribe
33
+ };
34
+ },
35
+ get session() {
36
+ removed_session();
37
+ return {};
38
+ },
39
+ updated: stores.updated
40
+ };
41
+ };
42
+
43
+ /** @type {typeof import('$app/stores').page} */
44
+ export const page = {
45
+ /** @param {(value: any) => void} fn */
46
+ subscribe(fn) {
47
+ const store = getStores().page;
48
+ return store.subscribe(fn);
49
+ }
50
+ };
51
+
52
+ /** @type {typeof import('$app/stores').navigating} */
53
+ export const navigating = {
54
+ subscribe(fn) {
55
+ const store = getStores().navigating;
56
+ return store.subscribe(fn);
57
+ }
58
+ };
59
+
60
+ function removed_session() {
61
+ // TODO remove for 1.0
62
+ throw new Error(
63
+ 'stores.session is no longer available. See https://github.com/sveltejs/kit/discussions/5883'
64
+ );
65
+ }
66
+
67
+ export const session = {
68
+ subscribe: removed_session,
69
+ set: removed_session,
70
+ update: removed_session
71
+ };
72
+
73
+ /** @type {typeof import('$app/stores').updated} */
74
+ export const updated = {
75
+ subscribe(fn) {
76
+ const store = getStores().updated;
77
+
78
+ if (browser) {
79
+ updated.check = store.check;
80
+ }
81
+
82
+ return store.subscribe(fn);
83
+ },
84
+ check: () => {
85
+ throw new Error(
86
+ browser
87
+ ? `Cannot check updated store before subscribing`
88
+ : `Can only check updated store in browser`
89
+ );
90
+ }
91
+ };
@@ -0,0 +1,17 @@
1
+ declare module '__GENERATED__/client-manifest.js' {
2
+ import { CSRPageNodeLoader, ParamMatcher } from 'types';
3
+
4
+ /**
5
+ * A list of all the error/layout/page nodes used in the app
6
+ */
7
+ export const nodes: CSRPageNodeLoader[];
8
+
9
+ /**
10
+ * A map of `[routeId: string]: [errors, layouts, page]` tuples, which
11
+ * is parsed into an array of routes on startup. The numbers refer to the
12
+ * indices in `nodes`.
13
+ */
14
+ export const dictionary: Record<string, [number[], number[], number]>;
15
+
16
+ export const matchers: Record<string, ParamMatcher>;
17
+ }