@sveltejs/kit 1.0.0-next.44 → 1.0.0-next.440

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 (153) hide show
  1. package/README.md +12 -9
  2. package/package.json +94 -63
  3. package/src/cli.js +112 -0
  4. package/src/core/adapt/builder.js +223 -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 +78 -0
  12. package/src/core/prerender/crawl.js +194 -0
  13. package/src/core/prerender/prerender.js +380 -0
  14. package/src/core/prerender/queue.js +80 -0
  15. package/src/core/sync/create_manifest_data/index.js +450 -0
  16. package/src/core/sync/create_manifest_data/types.d.ts +37 -0
  17. package/src/core/sync/sync.js +59 -0
  18. package/src/core/sync/utils.js +33 -0
  19. package/src/core/sync/write_ambient.js +27 -0
  20. package/src/core/sync/write_client_manifest.js +92 -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/index.js +577 -0
  25. package/src/core/sync/write_types/test/layout/+layout.js +5 -0
  26. package/src/core/sync/write_types/test/layout/+layout.server.js +5 -0
  27. package/src/core/sync/write_types/test/layout/+layout.svelte +0 -0
  28. package/src/core/sync/write_types/test/layout/+page.js +5 -0
  29. package/src/core/sync/write_types/test/layout/+page.server.js +5 -0
  30. package/src/core/sync/write_types/test/layout/+page.svelte +0 -0
  31. package/src/core/sync/write_types/test/layout/_expected/$types.d.ts +67 -0
  32. package/src/core/sync/write_types/test/layout-advanced/(main)/+layout.server.js +5 -0
  33. package/src/core/sync/write_types/test/layout-advanced/(main)/+layout.svelte +0 -0
  34. package/src/core/sync/write_types/test/layout-advanced/(main)/+page.js +5 -0
  35. package/src/core/sync/write_types/test/layout-advanced/(main)/+page@.svelte +0 -0
  36. package/src/core/sync/write_types/test/layout-advanced/(main)/sub/+page.js +5 -0
  37. package/src/core/sync/write_types/test/layout-advanced/(main)/sub/+page.svelte +0 -0
  38. package/src/core/sync/write_types/test/layout-advanced/+layout.js +5 -0
  39. package/src/core/sync/write_types/test/layout-advanced/+layout.svelte +0 -0
  40. package/src/core/sync/write_types/test/layout-advanced/_expected/$types.d.ts +32 -0
  41. package/src/core/sync/write_types/test/layout-advanced/_expected/(main)/$types.d.ts +42 -0
  42. package/src/core/sync/write_types/test/layout-advanced/_expected/(main)/sub/$types.d.ts +32 -0
  43. package/src/core/sync/write_types/test/simple-page-server-and-shared/+page.js +5 -0
  44. package/src/core/sync/write_types/test/simple-page-server-and-shared/+page.server.js +5 -0
  45. package/src/core/sync/write_types/test/simple-page-server-and-shared/+page.svelte +0 -0
  46. package/src/core/sync/write_types/test/simple-page-server-and-shared/_expected/$types.d.ts +44 -0
  47. package/src/core/sync/write_types/test/simple-page-server-only/+page.server.js +5 -0
  48. package/src/core/sync/write_types/test/simple-page-server-only/+page.svelte +0 -0
  49. package/src/core/sync/write_types/test/simple-page-server-only/_expected/$types.d.ts +30 -0
  50. package/src/core/sync/write_types/test/simple-page-shared-only/+page.js +5 -0
  51. package/src/core/sync/write_types/test/simple-page-shared-only/+page.svelte +0 -0
  52. package/src/core/sync/write_types/test/simple-page-shared-only/_expected/$types.d.ts +33 -0
  53. package/src/core/sync/write_types/test/slugs/+layout.js +1 -0
  54. package/src/core/sync/write_types/test/slugs/+layout.svelte +1 -0
  55. package/src/core/sync/write_types/test/slugs/[...rest]/+page.js +3 -0
  56. package/src/core/sync/write_types/test/slugs/[...rest]/+page.svelte +0 -0
  57. package/src/core/sync/write_types/test/slugs/[slug]/+page.js +3 -0
  58. package/src/core/sync/write_types/test/slugs/[slug]/+page.svelte +0 -0
  59. package/src/core/sync/write_types/test/slugs/_expected/$types.d.ts +32 -0
  60. package/src/core/sync/write_types/test/slugs/_expected/[...rest]/$types.d.ts +29 -0
  61. package/src/core/sync/write_types/test/slugs/_expected/[slug]/$types.d.ts +29 -0
  62. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/+layout.js +1 -0
  63. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/+layout.svelte +1 -0
  64. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/_expected/$types.d.ts +30 -0
  65. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/_expected/nested/$types.d.ts +32 -0
  66. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/_expected/nested/[...rest]/$types.d.ts +36 -0
  67. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/_expected/nested/[slug]/$types.d.ts +17 -0
  68. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/nested/+layout.js +1 -0
  69. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/nested/+layout.svelte +1 -0
  70. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/nested/[...rest]/+page.js +3 -0
  71. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/nested/[...rest]/+page.svelte +0 -0
  72. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/nested/[slug]/+page@.svelte +0 -0
  73. package/src/core/utils.js +70 -0
  74. package/src/hooks.js +26 -0
  75. package/src/index/index.js +45 -0
  76. package/src/index/private.js +33 -0
  77. package/src/node/index.js +145 -0
  78. package/src/node/polyfills.js +40 -0
  79. package/src/runtime/app/env.js +11 -0
  80. package/src/runtime/app/navigation.js +22 -0
  81. package/src/runtime/app/paths.js +1 -0
  82. package/src/runtime/app/stores.js +102 -0
  83. package/src/runtime/client/ambient.d.ts +18 -0
  84. package/src/runtime/client/client.js +1362 -0
  85. package/src/runtime/client/fetcher.js +60 -0
  86. package/src/runtime/client/parse.js +41 -0
  87. package/src/runtime/client/singletons.js +21 -0
  88. package/src/runtime/client/start.js +46 -0
  89. package/src/runtime/client/types.d.ts +86 -0
  90. package/src/runtime/client/utils.js +113 -0
  91. package/src/runtime/components/error.svelte +16 -0
  92. package/{assets → src/runtime}/components/layout.svelte +0 -0
  93. package/src/runtime/env/dynamic/private.js +1 -0
  94. package/src/runtime/env/dynamic/public.js +1 -0
  95. package/src/runtime/env-private.js +7 -0
  96. package/src/runtime/env-public.js +7 -0
  97. package/src/runtime/env.js +6 -0
  98. package/src/runtime/hash.js +16 -0
  99. package/src/runtime/paths.js +11 -0
  100. package/src/runtime/server/endpoint.js +50 -0
  101. package/src/runtime/server/index.js +489 -0
  102. package/src/runtime/server/page/cookie.js +25 -0
  103. package/src/runtime/server/page/crypto.js +239 -0
  104. package/src/runtime/server/page/csp.js +249 -0
  105. package/src/runtime/server/page/fetch.js +266 -0
  106. package/src/runtime/server/page/index.js +408 -0
  107. package/src/runtime/server/page/load_data.js +168 -0
  108. package/src/runtime/server/page/render.js +361 -0
  109. package/src/runtime/server/page/respond_with_error.js +95 -0
  110. package/src/runtime/server/page/types.d.ts +44 -0
  111. package/src/runtime/server/utils.js +116 -0
  112. package/src/utils/array.js +9 -0
  113. package/src/utils/error.js +22 -0
  114. package/src/utils/escape.js +104 -0
  115. package/src/utils/filesystem.js +108 -0
  116. package/src/utils/functions.js +16 -0
  117. package/src/utils/http.js +55 -0
  118. package/src/utils/misc.js +1 -0
  119. package/src/utils/routing.js +146 -0
  120. package/src/utils/url.js +142 -0
  121. package/src/vite/build/build_server.js +348 -0
  122. package/src/vite/build/build_service_worker.js +90 -0
  123. package/src/vite/build/utils.js +160 -0
  124. package/src/vite/dev/index.js +543 -0
  125. package/src/vite/index.js +578 -0
  126. package/src/vite/preview/index.js +186 -0
  127. package/src/vite/types.d.ts +3 -0
  128. package/src/vite/utils.js +345 -0
  129. package/svelte-kit.js +1 -1
  130. package/types/ambient.d.ts +366 -0
  131. package/types/index.d.ts +345 -0
  132. package/types/internal.d.ts +374 -0
  133. package/types/private.d.ts +209 -0
  134. package/CHANGELOG.md +0 -437
  135. package/assets/components/error.svelte +0 -13
  136. package/assets/runtime/app/env.js +0 -5
  137. package/assets/runtime/app/navigation.js +0 -41
  138. package/assets/runtime/app/paths.js +0 -1
  139. package/assets/runtime/app/stores.js +0 -93
  140. package/assets/runtime/chunks/utils.js +0 -19
  141. package/assets/runtime/internal/singletons.js +0 -23
  142. package/assets/runtime/internal/start.js +0 -770
  143. package/assets/runtime/paths.js +0 -12
  144. package/dist/chunks/index.js +0 -3521
  145. package/dist/chunks/index2.js +0 -587
  146. package/dist/chunks/index3.js +0 -246
  147. package/dist/chunks/index4.js +0 -545
  148. package/dist/chunks/index5.js +0 -761
  149. package/dist/chunks/index6.js +0 -322
  150. package/dist/chunks/standard.js +0 -99
  151. package/dist/chunks/utils.js +0 -83
  152. package/dist/cli.js +0 -546
  153. package/dist/ssr.js +0 -2580
@@ -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,102 @@
1
+ import { getContext } from 'svelte';
2
+ import { browser } from './env.js';
3
+ import { stores as browser_stores } from '../client/singletons.js';
4
+
5
+ // TODO remove this (for 1.0? after 1.0?)
6
+ let warned = false;
7
+ export function stores() {
8
+ if (!warned) {
9
+ console.error('stores() is deprecated; use getStores() instead');
10
+ warned = true;
11
+ }
12
+ return getStores();
13
+ }
14
+
15
+ /**
16
+ * @type {import('$app/stores').getStores}
17
+ */
18
+ export const getStores = () => {
19
+ const stores = browser ? browser_stores : getContext('__svelte__');
20
+
21
+ const readonly_stores = {
22
+ page: {
23
+ subscribe: stores.page.subscribe
24
+ },
25
+ navigating: {
26
+ subscribe: stores.navigating.subscribe
27
+ },
28
+ updated: stores.updated
29
+ };
30
+
31
+ // TODO remove this for 1.0
32
+ Object.defineProperties(readonly_stores, {
33
+ preloading: {
34
+ get() {
35
+ console.error('stores.preloading is deprecated; use stores.navigating instead');
36
+ return {
37
+ subscribe: stores.navigating.subscribe
38
+ };
39
+ },
40
+ enumerable: false
41
+ },
42
+ session: {
43
+ get() {
44
+ removed_session();
45
+ return {};
46
+ },
47
+ enumerable: false
48
+ }
49
+ });
50
+
51
+ return readonly_stores;
52
+ };
53
+
54
+ /** @type {typeof import('$app/stores').page} */
55
+ export const page = {
56
+ /** @param {(value: any) => void} fn */
57
+ subscribe(fn) {
58
+ const store = getStores().page;
59
+ return store.subscribe(fn);
60
+ }
61
+ };
62
+
63
+ /** @type {typeof import('$app/stores').navigating} */
64
+ export const navigating = {
65
+ subscribe(fn) {
66
+ const store = getStores().navigating;
67
+ return store.subscribe(fn);
68
+ }
69
+ };
70
+
71
+ function removed_session() {
72
+ // TODO remove for 1.0
73
+ throw new Error(
74
+ 'stores.session is no longer available. See https://github.com/sveltejs/kit/discussions/5883'
75
+ );
76
+ }
77
+
78
+ export const session = {
79
+ subscribe: removed_session,
80
+ set: removed_session,
81
+ update: removed_session
82
+ };
83
+
84
+ /** @type {typeof import('$app/stores').updated} */
85
+ export const updated = {
86
+ subscribe(fn) {
87
+ const store = getStores().updated;
88
+
89
+ if (browser) {
90
+ updated.check = store.check;
91
+ }
92
+
93
+ return store.subscribe(fn);
94
+ },
95
+ check: () => {
96
+ throw new Error(
97
+ browser
98
+ ? `Cannot check updated store before subscribing`
99
+ : `Can only check updated store in browser`
100
+ );
101
+ }
102
+ };
@@ -0,0 +1,18 @@
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]: [leaf, layouts, errors]` tuples, which
11
+ * is parsed into an array of routes on startup. The numbers refer to the
12
+ * indices in `nodes`. The route layout and error nodes are not referenced,
13
+ * they are always number 0 and 1 and always apply.
14
+ */
15
+ export const dictionary: Record<string, [leaf: number, layouts?: number[], errors?: number[]]>;
16
+
17
+ export const matchers: Record<string, ParamMatcher>;
18
+ }