@sveltejs/kit 1.0.0-next.47 → 1.0.0-next.472

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 (117) hide show
  1. package/README.md +12 -9
  2. package/package.json +93 -64
  3. package/scripts/special-types/$env+dynamic+private.md +8 -0
  4. package/scripts/special-types/$env+dynamic+public.md +8 -0
  5. package/scripts/special-types/$env+static+private.md +19 -0
  6. package/scripts/special-types/$env+static+public.md +7 -0
  7. package/scripts/special-types/$lib.md +1 -0
  8. package/src/cli.js +112 -0
  9. package/src/constants.js +7 -0
  10. package/src/core/adapt/builder.js +207 -0
  11. package/src/core/adapt/index.js +31 -0
  12. package/src/core/config/default-error.html +56 -0
  13. package/src/core/config/index.js +105 -0
  14. package/src/core/config/options.js +492 -0
  15. package/src/core/config/types.d.ts +1 -0
  16. package/src/core/env.js +121 -0
  17. package/src/core/generate_manifest/index.js +92 -0
  18. package/src/core/prerender/crawl.js +194 -0
  19. package/src/core/prerender/prerender.js +431 -0
  20. package/src/core/prerender/queue.js +80 -0
  21. package/src/core/sync/create_manifest_data/index.js +472 -0
  22. package/src/core/sync/create_manifest_data/types.d.ts +37 -0
  23. package/src/core/sync/sync.js +59 -0
  24. package/src/core/sync/utils.js +33 -0
  25. package/src/core/sync/write_ambient.js +53 -0
  26. package/src/core/sync/write_client_manifest.js +94 -0
  27. package/src/core/sync/write_matchers.js +25 -0
  28. package/src/core/sync/write_root.js +91 -0
  29. package/src/core/sync/write_tsconfig.js +195 -0
  30. package/src/core/sync/write_types/index.js +673 -0
  31. package/src/core/utils.js +70 -0
  32. package/src/exports/hooks/index.js +1 -0
  33. package/src/exports/hooks/sequence.js +44 -0
  34. package/src/exports/index.js +54 -0
  35. package/src/exports/node/index.js +145 -0
  36. package/src/exports/node/polyfills.js +41 -0
  37. package/src/exports/vite/build/build_server.js +357 -0
  38. package/src/exports/vite/build/build_service_worker.js +90 -0
  39. package/src/exports/vite/build/utils.js +162 -0
  40. package/src/exports/vite/dev/index.js +554 -0
  41. package/src/exports/vite/index.js +591 -0
  42. package/src/exports/vite/preview/index.js +186 -0
  43. package/src/exports/vite/types.d.ts +3 -0
  44. package/src/exports/vite/utils.js +361 -0
  45. package/src/runtime/app/env.js +1 -0
  46. package/src/runtime/app/environment.js +11 -0
  47. package/src/runtime/app/forms.js +65 -0
  48. package/src/runtime/app/navigation.js +23 -0
  49. package/src/runtime/app/paths.js +1 -0
  50. package/src/runtime/app/stores.js +102 -0
  51. package/src/runtime/client/ambient.d.ts +24 -0
  52. package/src/runtime/client/client.js +1558 -0
  53. package/src/runtime/client/fetcher.js +107 -0
  54. package/src/runtime/client/parse.js +60 -0
  55. package/src/runtime/client/singletons.js +21 -0
  56. package/src/runtime/client/start.js +37 -0
  57. package/src/runtime/client/types.d.ts +87 -0
  58. package/src/runtime/client/utils.js +159 -0
  59. package/src/runtime/components/error.svelte +16 -0
  60. package/{assets → src/runtime}/components/layout.svelte +0 -0
  61. package/src/runtime/control.js +100 -0
  62. package/src/runtime/env/dynamic/private.js +1 -0
  63. package/src/runtime/env/dynamic/public.js +1 -0
  64. package/src/runtime/env-private.js +6 -0
  65. package/src/runtime/env-public.js +6 -0
  66. package/src/runtime/env.js +6 -0
  67. package/src/runtime/hash.js +16 -0
  68. package/src/runtime/paths.js +11 -0
  69. package/src/runtime/server/cookie.js +76 -0
  70. package/src/runtime/server/data/index.js +146 -0
  71. package/src/runtime/server/endpoint.js +66 -0
  72. package/src/runtime/server/index.js +339 -0
  73. package/src/runtime/server/page/actions.js +225 -0
  74. package/src/runtime/server/page/crypto.js +239 -0
  75. package/src/runtime/server/page/csp.js +249 -0
  76. package/src/runtime/server/page/fetch.js +286 -0
  77. package/src/runtime/server/page/index.js +308 -0
  78. package/src/runtime/server/page/load_data.js +124 -0
  79. package/src/runtime/server/page/render.js +359 -0
  80. package/src/runtime/server/page/respond_with_error.js +92 -0
  81. package/src/runtime/server/page/serialize_data.js +87 -0
  82. package/src/runtime/server/page/types.d.ts +41 -0
  83. package/src/runtime/server/utils.js +220 -0
  84. package/src/utils/array.js +9 -0
  85. package/src/utils/error.js +22 -0
  86. package/src/utils/escape.js +46 -0
  87. package/src/utils/filesystem.js +108 -0
  88. package/src/utils/functions.js +16 -0
  89. package/src/utils/http.js +55 -0
  90. package/src/utils/misc.js +1 -0
  91. package/src/utils/routing.js +117 -0
  92. package/src/utils/url.js +142 -0
  93. package/svelte-kit.js +1 -1
  94. package/types/ambient.d.ts +404 -0
  95. package/types/index.d.ts +396 -0
  96. package/types/internal.d.ts +377 -0
  97. package/types/private.d.ts +210 -0
  98. package/CHANGELOG.md +0 -463
  99. package/assets/components/error.svelte +0 -13
  100. package/assets/runtime/app/env.js +0 -5
  101. package/assets/runtime/app/navigation.js +0 -44
  102. package/assets/runtime/app/paths.js +0 -1
  103. package/assets/runtime/app/stores.js +0 -93
  104. package/assets/runtime/chunks/utils.js +0 -22
  105. package/assets/runtime/internal/singletons.js +0 -23
  106. package/assets/runtime/internal/start.js +0 -773
  107. package/assets/runtime/paths.js +0 -12
  108. package/dist/chunks/index.js +0 -3517
  109. package/dist/chunks/index2.js +0 -587
  110. package/dist/chunks/index3.js +0 -246
  111. package/dist/chunks/index4.js +0 -530
  112. package/dist/chunks/index5.js +0 -763
  113. package/dist/chunks/index6.js +0 -322
  114. package/dist/chunks/standard.js +0 -99
  115. package/dist/chunks/utils.js +0 -83
  116. package/dist/cli.js +0 -553
  117. package/dist/ssr.js +0 -2581
@@ -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,54 @@
1
+ import { HttpError, Redirect, ValidationError } from '../runtime/control.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
+ }
46
+
47
+ /**
48
+ * Generates a `ValidationError` object.
49
+ * @param {number} status
50
+ * @param {Record<string, any> | undefined} [data]
51
+ */
52
+ export function invalid(status, data) {
53
+ return new ValidationError(status, data);
54
+ }
@@ -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,41 @@
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, FormData } 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
+ FormData
29
+ };
30
+
31
+ // exported for dev/preview and node environments
32
+ export function installPolyfills() {
33
+ for (const name in globals) {
34
+ Object.defineProperty(globalThis, name, {
35
+ enumerable: true,
36
+ configurable: true,
37
+ writable: true,
38
+ value: globals[name]
39
+ });
40
+ }
41
+ }