@sveltejs/kit 1.0.0-next.52 → 1.0.0-next.520

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 (128) hide show
  1. package/README.md +6 -3
  2. package/package.json +93 -67
  3. package/postinstall.js +47 -0
  4. package/scripts/special-types/$env+dynamic+private.md +10 -0
  5. package/scripts/special-types/$env+dynamic+public.md +8 -0
  6. package/scripts/special-types/$env+static+private.md +19 -0
  7. package/scripts/special-types/$env+static+public.md +7 -0
  8. package/scripts/special-types/$lib.md +5 -0
  9. package/src/cli.js +108 -0
  10. package/src/constants.js +7 -0
  11. package/src/core/adapt/builder.js +215 -0
  12. package/src/core/adapt/index.js +31 -0
  13. package/src/core/config/default-error.html +56 -0
  14. package/src/core/config/index.js +110 -0
  15. package/src/core/config/options.js +504 -0
  16. package/src/core/config/types.d.ts +1 -0
  17. package/src/core/env.js +121 -0
  18. package/src/core/generate_manifest/index.js +94 -0
  19. package/src/core/prerender/crawl.js +198 -0
  20. package/src/core/prerender/entities.js +2252 -0
  21. package/src/core/prerender/prerender.js +458 -0
  22. package/src/core/prerender/queue.js +80 -0
  23. package/src/core/sync/create_manifest_data/conflict.js +0 -0
  24. package/src/core/sync/create_manifest_data/index.js +470 -0
  25. package/src/core/sync/create_manifest_data/sort.js +163 -0
  26. package/src/core/sync/create_manifest_data/types.d.ts +37 -0
  27. package/src/core/sync/sync.js +78 -0
  28. package/src/core/sync/utils.js +33 -0
  29. package/src/core/sync/write_ambient.js +53 -0
  30. package/src/core/sync/write_client_manifest.js +106 -0
  31. package/src/core/sync/write_matchers.js +25 -0
  32. package/src/core/sync/write_root.js +91 -0
  33. package/src/core/sync/write_tsconfig.js +195 -0
  34. package/src/core/sync/write_types/index.js +783 -0
  35. package/src/core/utils.js +70 -0
  36. package/src/exports/hooks/index.js +1 -0
  37. package/src/exports/hooks/sequence.js +44 -0
  38. package/src/exports/index.js +45 -0
  39. package/src/exports/node/index.js +161 -0
  40. package/src/exports/node/polyfills.js +28 -0
  41. package/src/exports/vite/build/build_server.js +378 -0
  42. package/src/exports/vite/build/build_service_worker.js +91 -0
  43. package/src/exports/vite/build/utils.js +181 -0
  44. package/src/exports/vite/dev/index.js +581 -0
  45. package/src/exports/vite/graph_analysis/index.js +277 -0
  46. package/src/exports/vite/graph_analysis/types.d.ts +5 -0
  47. package/src/exports/vite/graph_analysis/utils.js +30 -0
  48. package/src/exports/vite/index.js +603 -0
  49. package/src/exports/vite/preview/index.js +189 -0
  50. package/src/exports/vite/types.d.ts +3 -0
  51. package/src/exports/vite/utils.js +157 -0
  52. package/src/runtime/app/env.js +1 -0
  53. package/src/runtime/app/environment.js +11 -0
  54. package/src/runtime/app/forms.js +123 -0
  55. package/src/runtime/app/navigation.js +23 -0
  56. package/src/runtime/app/paths.js +1 -0
  57. package/src/runtime/app/stores.js +102 -0
  58. package/src/runtime/client/ambient.d.ts +30 -0
  59. package/src/runtime/client/client.js +1595 -0
  60. package/src/runtime/client/fetcher.js +107 -0
  61. package/src/runtime/client/parse.js +60 -0
  62. package/src/runtime/client/singletons.js +21 -0
  63. package/src/runtime/client/start.js +37 -0
  64. package/src/runtime/client/types.d.ts +84 -0
  65. package/src/runtime/client/utils.js +159 -0
  66. package/src/runtime/components/error.svelte +16 -0
  67. package/{assets → src/runtime}/components/layout.svelte +0 -0
  68. package/src/runtime/control.js +98 -0
  69. package/src/runtime/env/dynamic/private.js +1 -0
  70. package/src/runtime/env/dynamic/public.js +1 -0
  71. package/src/runtime/env-private.js +6 -0
  72. package/src/runtime/env-public.js +6 -0
  73. package/src/runtime/env.js +6 -0
  74. package/src/runtime/hash.js +20 -0
  75. package/src/runtime/paths.js +11 -0
  76. package/src/runtime/server/cookie.js +166 -0
  77. package/src/runtime/server/data/index.js +131 -0
  78. package/src/runtime/server/endpoint.js +92 -0
  79. package/src/runtime/server/fetch.js +174 -0
  80. package/src/runtime/server/index.js +355 -0
  81. package/src/runtime/server/page/actions.js +256 -0
  82. package/src/runtime/server/page/crypto.js +239 -0
  83. package/src/runtime/server/page/csp.js +250 -0
  84. package/src/runtime/server/page/index.js +304 -0
  85. package/src/runtime/server/page/load_data.js +215 -0
  86. package/src/runtime/server/page/render.js +346 -0
  87. package/src/runtime/server/page/respond_with_error.js +102 -0
  88. package/src/runtime/server/page/serialize_data.js +87 -0
  89. package/src/runtime/server/page/types.d.ts +35 -0
  90. package/src/runtime/server/utils.js +181 -0
  91. package/src/utils/array.js +9 -0
  92. package/src/utils/error.js +22 -0
  93. package/src/utils/escape.js +46 -0
  94. package/src/utils/filesystem.js +142 -0
  95. package/src/utils/functions.js +16 -0
  96. package/src/utils/http.js +72 -0
  97. package/src/utils/misc.js +1 -0
  98. package/src/utils/promises.js +17 -0
  99. package/src/utils/routing.js +130 -0
  100. package/src/utils/unit_test.js +11 -0
  101. package/src/utils/url.js +144 -0
  102. package/svelte-kit.js +1 -1
  103. package/types/ambient.d.ts +431 -0
  104. package/types/index.d.ts +477 -0
  105. package/types/internal.d.ts +380 -0
  106. package/types/private.d.ts +224 -0
  107. package/CHANGELOG.md +0 -496
  108. package/assets/components/error.svelte +0 -13
  109. package/assets/runtime/app/env.js +0 -5
  110. package/assets/runtime/app/navigation.js +0 -44
  111. package/assets/runtime/app/paths.js +0 -1
  112. package/assets/runtime/app/stores.js +0 -93
  113. package/assets/runtime/chunks/utils.js +0 -22
  114. package/assets/runtime/internal/singletons.js +0 -23
  115. package/assets/runtime/internal/start.js +0 -776
  116. package/assets/runtime/paths.js +0 -12
  117. package/dist/chunks/index.js +0 -3537
  118. package/dist/chunks/index2.js +0 -587
  119. package/dist/chunks/index3.js +0 -246
  120. package/dist/chunks/index4.js +0 -568
  121. package/dist/chunks/index5.js +0 -763
  122. package/dist/chunks/index6.js +0 -323
  123. package/dist/chunks/standard.js +0 -99
  124. package/dist/chunks/utils.js +0 -83
  125. package/dist/cli.js +0 -555
  126. package/dist/ssr.js +0 -2604
  127. package/types.d.ts +0 -73
  128. package/types.internal.d.ts +0 -222
@@ -0,0 +1,70 @@
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
+ /**
20
+ * This allows us to import SvelteKit internals that aren't exposed via `pkg.exports` in a
21
+ * way that works whether `@sveltejs/kit` is installed inside the project's `node_modules`
22
+ * or in a workspace root
23
+ */
24
+ export const runtime_base = runtime_directory.startsWith(process.cwd())
25
+ ? `/${path.relative('.', runtime_directory)}`
26
+ : `/@fs${
27
+ // Windows/Linux separation - Windows starts with a drive letter, we need a / in front there
28
+ runtime_directory.startsWith('/') ? '' : '/'
29
+ }${runtime_directory}`;
30
+
31
+ /** @param {string} str */
32
+ function posixify_path(str) {
33
+ const parsed = path.parse(str);
34
+ return `/${parsed.dir.slice(parsed.root.length).split(path.sep).join('/')}/${parsed.base}`;
35
+ }
36
+
37
+ function noop() {}
38
+
39
+ /** @param {{ verbose: boolean }} opts */
40
+ export function logger({ verbose }) {
41
+ /** @type {import('types').Logger} */
42
+ const log = (msg) => console.log(msg.replace(/^/gm, ' '));
43
+
44
+ /** @param {string} msg */
45
+ const err = (msg) => console.error(msg.replace(/^/gm, ' '));
46
+
47
+ log.success = (msg) => log(colors.green(`✔ ${msg}`));
48
+ log.error = (msg) => err(colors.bold().red(msg));
49
+ log.warn = (msg) => log(colors.bold().yellow(msg));
50
+
51
+ log.minor = verbose ? (msg) => log(colors.grey(msg)) : noop;
52
+ log.info = verbose ? log : noop;
53
+
54
+ return log;
55
+ }
56
+
57
+ /** @param {import('types').ManifestData} manifest_data */
58
+ export function get_mime_lookup(manifest_data) {
59
+ /** @type {Record<string, string>} */
60
+ const mime = {};
61
+
62
+ manifest_data.assets.forEach((asset) => {
63
+ if (asset.type) {
64
+ const ext = path.extname(asset.file);
65
+ mime[ext] = asset.type;
66
+ }
67
+ });
68
+
69
+ return mime;
70
+ }
@@ -0,0 +1 @@
1
+ export { sequence } from './sequence.js';
@@ -0,0 +1,44 @@
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
+ * @param {import('types').ResolveOptions | undefined} parent_options
16
+ * @returns {import('types').MaybePromise<Response>}
17
+ */
18
+ function apply_handle(i, event, parent_options) {
19
+ const handle = handlers[i];
20
+
21
+ return handle({
22
+ event,
23
+ resolve: (event, options) => {
24
+ /** @param {{ html: string, done: boolean }} opts */
25
+ const transformPageChunk = async ({ html, done }) => {
26
+ if (options?.transformPageChunk) {
27
+ html = (await options.transformPageChunk({ html, done })) ?? '';
28
+ }
29
+
30
+ if (parent_options?.transformPageChunk) {
31
+ html = (await parent_options.transformPageChunk({ html, done })) ?? '';
32
+ }
33
+
34
+ return html;
35
+ };
36
+
37
+ return i < length - 1
38
+ ? apply_handle(i + 1, event, { transformPageChunk })
39
+ : resolve(event, { transformPageChunk });
40
+ }
41
+ });
42
+ }
43
+ };
44
+ }
@@ -0,0 +1,45 @@
1
+ import { HttpError, Redirect, ValidationError } from '../runtime/control.js';
2
+
3
+ // For some reason we need to type the params as well here,
4
+ // JSdoc doesn't seem to like @type with function overloads
5
+ /**
6
+ * @type {import('@sveltejs/kit').error}
7
+ * @param {number} status
8
+ * @param {any} message
9
+ */
10
+ export function error(status, message) {
11
+ return new HttpError(status, message);
12
+ }
13
+
14
+ /** @type {import('@sveltejs/kit').redirect} */
15
+ export function redirect(status, location) {
16
+ if (isNaN(status) || status < 300 || status > 399) {
17
+ throw new Error('Invalid status code');
18
+ }
19
+
20
+ return new Redirect(status, location);
21
+ }
22
+
23
+ /** @type {import('@sveltejs/kit').json} */
24
+ export function json(data, init) {
25
+ // TODO deprecate this in favour of `Response.json` when it's
26
+ // more widely supported
27
+ const headers = new Headers(init?.headers);
28
+ if (!headers.has('content-type')) {
29
+ headers.set('content-type', 'application/json');
30
+ }
31
+
32
+ return new Response(JSON.stringify(data), {
33
+ ...init,
34
+ headers
35
+ });
36
+ }
37
+
38
+ /**
39
+ * Generates a `ValidationError` object.
40
+ * @param {number} status
41
+ * @param {Record<string, any> | undefined} [data]
42
+ */
43
+ export function invalid(status, data) {
44
+ return new ValidationError(status, data);
45
+ }
@@ -0,0 +1,161 @@
1
+ import * as set_cookie_parser from 'set-cookie-parser';
2
+ import { error } from '../index.js';
3
+
4
+ /**
5
+ * @param {import('http').IncomingMessage} req
6
+ * @param {number} [body_size_limit]
7
+ */
8
+ function get_raw_body(req, body_size_limit) {
9
+ const h = req.headers;
10
+
11
+ if (!h['content-type']) {
12
+ return null;
13
+ }
14
+
15
+ const content_length = Number(h['content-length']);
16
+
17
+ // check if no request body
18
+ if (
19
+ (req.httpVersionMajor === 1 && isNaN(content_length) && h['transfer-encoding'] == null) ||
20
+ content_length === 0
21
+ ) {
22
+ return null;
23
+ }
24
+
25
+ let length = content_length;
26
+
27
+ if (body_size_limit) {
28
+ if (!length) {
29
+ length = body_size_limit;
30
+ } else if (length > body_size_limit) {
31
+ throw error(
32
+ 413,
33
+ `Received content-length of ${length}, but only accept up to ${body_size_limit} bytes.`
34
+ );
35
+ }
36
+ }
37
+
38
+ if (req.destroyed) {
39
+ const readable = new ReadableStream();
40
+ readable.cancel();
41
+ return readable;
42
+ }
43
+
44
+ let size = 0;
45
+ let cancelled = false;
46
+
47
+ return new ReadableStream({
48
+ start(controller) {
49
+ req.on('error', (error) => {
50
+ cancelled = true;
51
+ controller.error(error);
52
+ });
53
+
54
+ req.on('end', () => {
55
+ if (cancelled) return;
56
+ controller.close();
57
+ });
58
+
59
+ req.on('data', (chunk) => {
60
+ if (cancelled) return;
61
+
62
+ size += chunk.length;
63
+ if (size > length) {
64
+ cancelled = true;
65
+ controller.error(
66
+ error(
67
+ 413,
68
+ `request body size exceeded ${
69
+ content_length ? "'content-length'" : 'BODY_SIZE_LIMIT'
70
+ } of ${length}`
71
+ )
72
+ );
73
+ return;
74
+ }
75
+
76
+ controller.enqueue(chunk);
77
+
78
+ if (controller.desiredSize === null || controller.desiredSize <= 0) {
79
+ req.pause();
80
+ }
81
+ });
82
+ },
83
+
84
+ pull() {
85
+ req.resume();
86
+ },
87
+
88
+ cancel(reason) {
89
+ cancelled = true;
90
+ req.destroy(reason);
91
+ }
92
+ });
93
+ }
94
+
95
+ /** @type {import('@sveltejs/kit/node').getRequest} */
96
+ export async function getRequest({ request, base, bodySizeLimit }) {
97
+ return new Request(base + request.url, {
98
+ method: request.method,
99
+ headers: /** @type {Record<string, string>} */ (request.headers),
100
+ body: get_raw_body(request, bodySizeLimit)
101
+ });
102
+ }
103
+
104
+ /** @type {import('@sveltejs/kit/node').setResponse} */
105
+ export async function setResponse(res, response) {
106
+ const headers = Object.fromEntries(response.headers);
107
+
108
+ if (response.headers.has('set-cookie')) {
109
+ const header = /** @type {string} */ (response.headers.get('set-cookie'));
110
+ const split = set_cookie_parser.splitCookiesString(header);
111
+
112
+ // @ts-expect-error
113
+ headers['set-cookie'] = split;
114
+ }
115
+
116
+ res.writeHead(response.status, headers);
117
+
118
+ if (!response.body) {
119
+ res.end();
120
+ return;
121
+ }
122
+
123
+ const reader = response.body.getReader();
124
+
125
+ if (res.destroyed) {
126
+ reader.cancel();
127
+ return;
128
+ }
129
+
130
+ const cancel = (/** @type {Error|undefined} */ error) => {
131
+ res.off('close', cancel);
132
+ res.off('error', cancel);
133
+
134
+ // If the reader has already been interrupted with an error earlier,
135
+ // then it will appear here, it is useless, but it needs to be catch.
136
+ reader.cancel(error).catch(() => {});
137
+ if (error) res.destroy(error);
138
+ };
139
+
140
+ res.on('close', cancel);
141
+ res.on('error', cancel);
142
+
143
+ next();
144
+ async function next() {
145
+ try {
146
+ for (;;) {
147
+ const { done, value } = await reader.read();
148
+
149
+ if (done) break;
150
+
151
+ if (!res.write(value)) {
152
+ res.once('drain', next);
153
+ return;
154
+ }
155
+ }
156
+ res.end();
157
+ } catch (error) {
158
+ cancel(error instanceof Error ? error : new Error(String(error)));
159
+ }
160
+ }
161
+ }
@@ -0,0 +1,28 @@
1
+ import { fetch, Response, Request, Headers, FormData } from 'undici';
2
+ import { ReadableStream, TransformStream, WritableStream } from 'stream/web';
3
+ import { webcrypto as crypto } from 'crypto';
4
+
5
+ /** @type {Record<string, any>} */
6
+ const globals = {
7
+ crypto,
8
+ fetch,
9
+ Response,
10
+ Request,
11
+ Headers,
12
+ ReadableStream,
13
+ TransformStream,
14
+ WritableStream,
15
+ FormData
16
+ };
17
+
18
+ // exported for dev/preview and node environments
19
+ export function installPolyfills() {
20
+ for (const name in globals) {
21
+ Object.defineProperty(globalThis, name, {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: globals[name]
26
+ });
27
+ }
28
+ }