@sveltejs/kit 1.30.3 → 2.0.0

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 (57) hide show
  1. package/package.json +24 -24
  2. package/src/core/adapt/builder.js +8 -1
  3. package/src/core/config/index.js +9 -1
  4. package/src/core/config/options.js +1 -12
  5. package/src/core/postbuild/analyse.js +98 -80
  6. package/src/core/postbuild/prerender.js +11 -9
  7. package/src/core/sync/sync.js +2 -0
  8. package/src/core/sync/write_non_ambient.js +42 -0
  9. package/src/core/sync/write_server.js +3 -3
  10. package/src/core/sync/write_tsconfig.js +27 -78
  11. package/src/core/sync/write_types/index.js +1 -1
  12. package/src/exports/hooks/sequence.js +1 -1
  13. package/src/exports/index.js +88 -71
  14. package/src/exports/node/index.js +21 -24
  15. package/src/exports/node/polyfills.js +5 -34
  16. package/src/exports/public.d.ts +82 -61
  17. package/src/exports/vite/dev/index.js +11 -19
  18. package/src/exports/vite/graph_analysis/index.js +2 -4
  19. package/src/exports/vite/index.js +73 -14
  20. package/src/exports/vite/module_ids.js +7 -0
  21. package/src/exports/vite/preview/index.js +56 -130
  22. package/src/runtime/app/forms.js +2 -35
  23. package/src/runtime/app/navigation.js +28 -15
  24. package/src/runtime/app/paths.js +2 -29
  25. package/src/runtime/client/client.js +449 -199
  26. package/src/runtime/client/constants.js +5 -1
  27. package/src/runtime/client/session-storage.js +7 -5
  28. package/src/runtime/client/singletons.js +7 -1
  29. package/src/runtime/client/types.d.ts +6 -2
  30. package/src/runtime/client/utils.js +12 -10
  31. package/src/runtime/control.js +16 -8
  32. package/src/runtime/server/cookie.js +38 -61
  33. package/src/runtime/server/data/index.js +6 -4
  34. package/src/runtime/server/env_module.js +29 -0
  35. package/src/runtime/server/fetch.js +7 -6
  36. package/src/runtime/server/index.js +23 -20
  37. package/src/runtime/server/page/actions.js +24 -15
  38. package/src/runtime/server/page/index.js +6 -8
  39. package/src/runtime/server/page/load_data.js +58 -40
  40. package/src/runtime/server/page/render.js +12 -7
  41. package/src/runtime/server/page/respond_with_error.js +4 -4
  42. package/src/runtime/server/page/types.d.ts +1 -1
  43. package/src/runtime/server/respond.js +14 -12
  44. package/src/runtime/server/utils.js +11 -8
  45. package/src/runtime/shared-server.js +19 -2
  46. package/src/types/ambient.d.ts +7 -1
  47. package/src/types/internal.d.ts +4 -1
  48. package/src/types/synthetic/$env+dynamic+private.md +2 -0
  49. package/src/types/synthetic/$env+dynamic+public.md +2 -0
  50. package/src/utils/error.js +17 -1
  51. package/src/utils/routing.js +47 -1
  52. package/src/utils/url.js +45 -27
  53. package/src/version.js +1 -1
  54. package/types/index.d.ts +166 -115
  55. package/types/index.d.ts.map +9 -5
  56. package/src/utils/platform.js +0 -1
  57. package/src/utils/promises.js +0 -61
@@ -3,7 +3,6 @@ import path from 'node:path';
3
3
  import colors from 'kleur';
4
4
  import { posixify } from '../../utils/filesystem.js';
5
5
  import { write_if_changed } from './utils.js';
6
- import { ts } from './ts.js';
7
6
 
8
7
  /**
9
8
  * @param {string} cwd
@@ -42,51 +41,22 @@ export function write_tsconfig(kit, cwd = process.cwd()) {
42
41
  const out = path.join(kit.outDir, 'tsconfig.json');
43
42
 
44
43
  const user_config = load_user_tsconfig(cwd);
45
- if (user_config) validate_user_config(kit, cwd, out, user_config);
46
-
47
- // only specify baseUrl if a) the user doesn't specify their own baseUrl
48
- // and b) they have non-relative paths. this causes problems with auto-imports,
49
- // so we print a suggestion that they use relative paths instead
50
- // TODO(v2): never include base URL, and skip the check below
51
- let include_base_url = false;
52
-
53
- if (user_config && !user_config.options.compilerOptions?.baseUrl) {
54
- const non_relative_paths = new Set();
55
- for (const paths of Object.values(user_config?.options.compilerOptions?.paths || {})) {
56
- for (const path of paths) {
57
- if (!path.startsWith('.')) non_relative_paths.add(path);
58
- }
59
- }
60
-
61
- if (non_relative_paths.size) {
62
- include_base_url = true;
63
-
64
- console.log(colors.bold().yellow('Please replace non-relative compilerOptions.paths:\n'));
44
+ if (user_config) validate_user_config(cwd, out, user_config);
65
45
 
66
- for (const path of non_relative_paths) {
67
- console.log(` - "${path}" -> "./${path}"`);
68
- }
69
-
70
- console.log(
71
- '\nDoing so allows us to omit "baseUrl" — which causes problems with imports — from the generated tsconfig.json. See https://github.com/sveltejs/kit/pull/8437 for more information.'
72
- );
73
- }
74
- }
75
-
76
- write_if_changed(out, JSON.stringify(get_tsconfig(kit, include_base_url), null, '\t'));
46
+ write_if_changed(out, JSON.stringify(get_tsconfig(kit), null, '\t'));
77
47
  }
78
48
 
79
49
  /**
80
50
  * Generates the tsconfig that the user's tsconfig inherits from.
81
51
  * @param {import('types').ValidatedKitConfig} kit
82
- * @param {boolean} include_base_url
83
52
  */
84
- export function get_tsconfig(kit, include_base_url) {
53
+ export function get_tsconfig(kit) {
85
54
  /** @param {string} file */
86
55
  const config_relative = (file) => posixify(path.relative(kit.outDir, file));
87
56
 
88
57
  const include = new Set([
89
58
  'ambient.d.ts',
59
+ 'non-ambient.d.ts',
90
60
  './types/**/$types.d.ts',
91
61
  config_relative('vite.config.js'),
92
62
  config_relative('vite.config.ts')
@@ -110,7 +80,7 @@ export function get_tsconfig(kit, include_base_url) {
110
80
  include.add(config_relative(`${test_folder}/**/*.ts`));
111
81
  include.add(config_relative(`${test_folder}/**/*.svelte`));
112
82
 
113
- const exclude = [config_relative('node_modules/**'), './[!ambient.d.ts]**'];
83
+ const exclude = [config_relative('node_modules/**')];
114
84
  if (path.extname(kit.files.serviceWorker)) {
115
85
  exclude.push(config_relative(kit.files.serviceWorker));
116
86
  } else {
@@ -122,30 +92,25 @@ export function get_tsconfig(kit, include_base_url) {
122
92
  const config = {
123
93
  compilerOptions: {
124
94
  // generated options
125
- baseUrl: include_base_url ? config_relative('.') : undefined,
126
- paths: get_tsconfig_paths(kit, include_base_url),
95
+ paths: get_tsconfig_paths(kit),
127
96
  rootDirs: [config_relative('.'), './types'],
128
97
 
129
98
  // essential options
130
99
  // svelte-preprocess cannot figure out whether you have a value or a type, so tell TypeScript
131
100
  // to enforce using \`import type\` instead of \`import\` for Types.
132
- importsNotUsedAsValues: 'error',
101
+ // Also, TypeScript doesn't know about import usages in the template because it only sees the
102
+ // script of a Svelte file. Therefore preserve all value imports.
103
+ verbatimModuleSyntax: true,
133
104
  // Vite compiles modules one at a time
134
105
  isolatedModules: true,
135
- // TypeScript doesn't know about import usages in the template because it only sees the
136
- // script of a Svelte file. Therefore preserve all value imports. Requires TS 4.5 or higher.
137
- preserveValueImports: true,
138
106
 
139
107
  // This is required for svelte-package to work as expected
140
108
  // Can be overwritten
141
109
  lib: ['esnext', 'DOM', 'DOM.Iterable'],
142
- moduleResolution: 'node', // TODO change to "bundler" in SvelteKit v2
110
+ moduleResolution: 'bundler',
143
111
  module: 'esnext',
144
112
  noEmit: true, // prevent tsconfig error "overwriting input files" - Vite handles the build and ignores this
145
- target: 'esnext',
146
-
147
- // TODO(v2): use the new flag verbatimModuleSyntax instead (requires support by Vite/Esbuild)
148
- ignoreDeprecations: ts && Number(ts.version.split('.')[0]) >= 5 ? '5.0' : undefined
113
+ target: 'esnext'
149
114
  },
150
115
  include: [...include],
151
116
  exclude
@@ -170,47 +135,34 @@ function load_user_tsconfig(cwd) {
170
135
  }
171
136
 
172
137
  /**
173
- * @param {import('types').ValidatedKitConfig} kit
174
138
  * @param {string} cwd
175
139
  * @param {string} out
176
140
  * @param {{ kind: string, options: any }} config
177
141
  */
178
- function validate_user_config(kit, cwd, out, config) {
142
+ function validate_user_config(cwd, out, config) {
179
143
  // we need to check that the user's tsconfig extends the framework config
180
144
  const extend = config.options.extends;
181
145
  const extends_framework_config =
182
146
  typeof extend === 'string'
183
147
  ? path.resolve(cwd, extend) === out
184
148
  : Array.isArray(extend)
185
- ? extend.some((e) => path.resolve(cwd, e) === out)
186
- : false;
149
+ ? extend.some((e) => path.resolve(cwd, e) === out)
150
+ : false;
187
151
 
188
152
  const options = config.options.compilerOptions || {};
189
153
 
190
154
  if (extends_framework_config) {
191
- const { paths: user_paths } = options;
192
-
193
- if (user_paths && fs.existsSync(kit.files.lib)) {
194
- /** @type {string[]} */
195
- const lib = user_paths['$lib'] || [];
196
- /** @type {string[]} */
197
- const lib_ = user_paths['$lib/*'] || [];
198
-
199
- // TODO(v2): check needs to be adjusted when we remove the base path
200
- const missing_lib_paths =
201
- !lib.some((relative) => path.resolve(cwd, relative) === kit.files.lib) ||
202
- !lib_.some((relative) => path.resolve(cwd, relative) === path.join(kit.files.lib, '/*'));
203
-
204
- if (missing_lib_paths) {
205
- console.warn(
206
- colors
207
- .bold()
208
- .yellow(`Your compilerOptions.paths in ${config.kind} should include the following:`)
209
- );
210
- let relative = posixify(path.relative('.', kit.files.lib));
211
- if (!relative.startsWith('.')) relative = `./${relative}`;
212
- console.warn(`{\n "$lib":["${relative}"],\n "$lib/*":["${relative}/*"]\n}`);
213
- }
155
+ const { paths, baseUrl } = options;
156
+
157
+ if (baseUrl || paths) {
158
+ console.warn(
159
+ colors
160
+ .bold()
161
+ .yellow(
162
+ `You have specified a baseUrl and/or paths in your ${config.kind} which interferes with SvelteKit's auto-generated tsconfig.json. ` +
163
+ 'Remove it to avoid problems with intellisense. For path aliases, use `kit.alias` instead: https://kit.svelte.dev/docs/configuration#alias'
164
+ )
165
+ );
214
166
  }
215
167
  } else {
216
168
  let relative = posixify(path.relative('.', out));
@@ -235,9 +187,8 @@ const value_regex = /^(.*?)((\/\*)|(\.\w+))?$/;
235
187
  * Related to vite alias creation.
236
188
  *
237
189
  * @param {import('types').ValidatedKitConfig} config
238
- * @param {boolean} include_base_url
239
190
  */
240
- function get_tsconfig_paths(config, include_base_url) {
191
+ function get_tsconfig_paths(config) {
241
192
  /** @param {string} file */
242
193
  const config_relative = (file) => posixify(path.relative(config.outDir, file));
243
194
 
@@ -256,9 +207,7 @@ function get_tsconfig_paths(config, include_base_url) {
256
207
  const value_match = value_regex.exec(value);
257
208
  if (!value_match) throw new Error(`Invalid alias value: ${value}`);
258
209
 
259
- const rel_path = (include_base_url ? project_relative : config_relative)(
260
- remove_trailing_slashstar(value)
261
- );
210
+ const rel_path = config_relative(remove_trailing_slashstar(value));
262
211
  const slashstar = key_match[2];
263
212
 
264
213
  if (slashstar) {
@@ -480,7 +480,7 @@ function process_node(node, outdir, is_page, proxies, all_pages_have_load = true
480
480
  const from = proxy.modified
481
481
  ? `./proxy${replace_ext_with_js(path.basename(file_path))}`
482
482
  : path_to_original(outdir, file_path);
483
- const type = `Kit.AwaitedProperties<Awaited<ReturnType<typeof import('${from}').load>>>`;
483
+ const type = `Kit.LoadProperties<Awaited<ReturnType<typeof import('${from}').load>>>`;
484
484
  return expand ? `Expand<OptionalUnion<EnsureDefined<${type}>>>` : type;
485
485
  } else {
486
486
  return fallback;
@@ -111,7 +111,7 @@ export function sequence(...handlers) {
111
111
  transformPageChunk,
112
112
  filterSerializedResponseHeaders,
113
113
  preload
114
- })
114
+ })
115
115
  : resolve(event, { transformPageChunk, filterSerializedResponseHeaders, preload });
116
116
  }
117
117
  });
@@ -1,51 +1,106 @@
1
1
  import { HttpError, Redirect, ActionFailure } from '../runtime/control.js';
2
2
  import { BROWSER, DEV } from 'esm-env';
3
- import { get_route_segments } from '../utils/routing.js';
4
3
 
5
4
  export { VERSION } from '../version.js';
6
5
 
7
6
  /**
7
+ * @template {number} TNumber
8
+ * @template {any[]} [TArray=[]]
9
+ * @typedef {TNumber extends TArray['length'] ? TArray[number] : LessThan<TNumber, [...TArray, TArray['length']]>} LessThan
10
+ */
11
+
12
+ /**
13
+ * @template {number} TStart
14
+ * @template {number} TEnd
15
+ * @typedef {Exclude<TEnd | LessThan<TEnd>, LessThan<TStart>>} NumericRange
16
+ */
17
+
18
+ // we have to repeat the JSDoc because the display for function overloads is broken
19
+ // see https://github.com/microsoft/TypeScript/issues/55056
20
+
21
+ /**
22
+ * Throws an error with a HTTP status code and an optional message.
23
+ * When called during request handling, this will cause SvelteKit to
24
+ * return an error response without invoking `handleError`.
25
+ * Make sure you're not catching the thrown error, which would prevent SvelteKit from handling it.
26
+ * @param {NumericRange<400, 599>} status The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses). Must be in the range 400-599.
27
+ * @param {App.Error} body An object that conforms to the App.Error type. If a string is passed, it will be used as the message property.
8
28
  * @overload
9
- * @param {number} status
29
+ * @param {NumericRange<400, 599>} status
10
30
  * @param {App.Error} body
11
- * @return {HttpError}
31
+ * @return {never}
32
+ * @throws {HttpError} This error instructs SvelteKit to initiate HTTP error handling.
33
+ * @throws {Error} If the provided status is invalid (not between 400 and 599).
12
34
  */
13
-
14
35
  /**
36
+ * Throws an error with a HTTP status code and an optional message.
37
+ * When called during request handling, this will cause SvelteKit to
38
+ * return an error response without invoking `handleError`.
39
+ * Make sure you're not catching the thrown error, which would prevent SvelteKit from handling it.
40
+ * @param {NumericRange<400, 599>} status The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses). Must be in the range 400-599.
41
+ * @param {{ message: string } extends App.Error ? App.Error | string | undefined : never} [body] An object that conforms to the App.Error type. If a string is passed, it will be used as the message property.
15
42
  * @overload
16
- * @param {number} status
43
+ * @param {NumericRange<400, 599>} status
17
44
  * @param {{ message: string } extends App.Error ? App.Error | string | undefined : never} [body]
18
- * @return {HttpError}
45
+ * @return {never}
46
+ * @throws {HttpError} This error instructs SvelteKit to initiate HTTP error handling.
47
+ * @throws {Error} If the provided status is invalid (not between 400 and 599).
19
48
  */
20
-
21
49
  /**
22
- * Creates an `HttpError` object with an HTTP status code and an optional message.
23
- * This object, if thrown during request handling, will cause SvelteKit to
50
+ * Throws an error with a HTTP status code and an optional message.
51
+ * When called during request handling, this will cause SvelteKit to
24
52
  * return an error response without invoking `handleError`.
25
53
  * Make sure you're not catching the thrown error, which would prevent SvelteKit from handling it.
26
- * @param {number} status The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses). Must be in the range 400-599.
54
+ * @param {NumericRange<400, 599>} status The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses). Must be in the range 400-599.
27
55
  * @param {{ message: string } extends App.Error ? App.Error | string | undefined : never} body An object that conforms to the App.Error type. If a string is passed, it will be used as the message property.
56
+ * @return {never}
57
+ * @throws {HttpError} This error instructs SvelteKit to initiate HTTP error handling.
58
+ * @throws {Error} If the provided status is invalid (not between 400 and 599).
28
59
  */
29
60
  export function error(status, body) {
30
61
  if ((!BROWSER || DEV) && (isNaN(status) || status < 400 || status > 599)) {
31
62
  throw new Error(`HTTP error status codes must be between 400 and 599 — ${status} is invalid`);
32
63
  }
33
64
 
34
- return new HttpError(status, body);
65
+ throw new HttpError(status, body);
35
66
  }
36
67
 
37
68
  /**
38
- * Create a `Redirect` object. If thrown during request handling, SvelteKit will return a redirect response.
69
+ * Checks whether this is an error thrown by {@link error}.
70
+ * @template {number} T
71
+ * @param {unknown} e
72
+ * @param {T} [status] The status to filter for.
73
+ * @return {e is (HttpError & { status: T extends undefined ? never : T })}
74
+ */
75
+ export function isHttpError(e, status) {
76
+ if (!(e instanceof HttpError)) return false;
77
+ return !status || e.status === status;
78
+ }
79
+
80
+ /**
81
+ * Redirect a request. When called during request handling, SvelteKit will return a redirect response.
39
82
  * Make sure you're not catching the thrown redirect, which would prevent SvelteKit from handling it.
40
- * @param {300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308} status The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#redirection_messages). Must be in the range 300-308.
83
+ * @param {NumericRange<300, 308>} status The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#redirection_messages). Must be in the range 300-308.
41
84
  * @param {string | URL} location The location to redirect to.
85
+ * @throws {Redirect} This error instructs SvelteKit to redirect to the specified location.
86
+ * @throws {Error} If the provided status is invalid.
87
+ * @return {never}
42
88
  */
43
89
  export function redirect(status, location) {
44
90
  if ((!BROWSER || DEV) && (isNaN(status) || status < 300 || status > 308)) {
45
91
  throw new Error('Invalid status code');
46
92
  }
47
93
 
48
- return new Redirect(status, location.toString());
94
+ throw new Redirect(status, location.toString());
95
+ }
96
+
97
+ /**
98
+ * Checks whether this is a redirect thrown by {@link redirect}.
99
+ * @param {unknown} e The object to check.
100
+ * @return {e is Redirect}
101
+ */
102
+ export function isRedirect(e) {
103
+ return e instanceof Redirect;
49
104
  }
50
105
 
51
106
  /**
@@ -100,68 +155,30 @@ export function text(body, init) {
100
155
  });
101
156
  }
102
157
 
158
+ /**
159
+ * Create an `ActionFailure` object.
160
+ * @param {number} status The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses). Must be in the range 400-599.
161
+ * @overload
162
+ * @param {number} status
163
+ * @returns {import('./public.js').ActionFailure<undefined>}
164
+ */
103
165
  /**
104
166
  * Create an `ActionFailure` object.
105
167
  * @template {Record<string, unknown> | undefined} [T=undefined]
106
168
  * @param {number} status The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses). Must be in the range 400-599.
107
- * @param {T} [data] Data associated with the failure (e.g. validation errors)
108
- * @returns {ActionFailure<T>}
169
+ * @param {T} data Data associated with the failure (e.g. validation errors)
170
+ * @overload
171
+ * @param {number} status
172
+ * @param {T} data
173
+ * @returns {import('./public.js').ActionFailure<T>}
109
174
  */
110
- export function fail(status, data) {
111
- return new ActionFailure(status, data);
112
- }
113
-
114
- const basic_param_pattern = /\[(\[)?(\.\.\.)?(\w+?)(?:=(\w+))?\]\]?/g;
115
-
116
- let warned = false;
117
-
118
175
  /**
119
- * @deprecated Use `resolveRoute` from `$app/paths` instead.
120
- *
121
- * Populate a route ID with params to resolve a pathname.
122
- * @example
123
- * ```js
124
- * resolvePath(
125
- * `/blog/[slug]/[...somethingElse]`,
126
- * {
127
- * slug: 'hello-world',
128
- * somethingElse: 'something/else'
129
- * }
130
- * ); // `/blog/hello-world/something/else`
131
- * ```
132
- * @param {string} id
133
- * @param {Record<string, string | undefined>} params
134
- * @returns {string}
176
+ * Create an `ActionFailure` object.
177
+ * @param {number} status
178
+ * @param {any} [data]
179
+ * @returns {import('./public.js').ActionFailure<any>}
135
180
  */
136
- export function resolvePath(id, params) {
137
- if (!warned) {
138
- console.warn('`resolvePath` is deprecated. Use `resolveRoute` from `$app/paths` instead.');
139
- warned = true;
140
- }
141
-
142
- const segments = get_route_segments(id);
143
- return (
144
- '/' +
145
- segments
146
- .map((segment) =>
147
- segment.replace(basic_param_pattern, (_, optional, rest, name) => {
148
- const param_value = params[name];
149
-
150
- // This is nested so TS correctly narrows the type
151
- if (!param_value) {
152
- if (optional) return '';
153
- if (rest && param_value !== undefined) return '';
154
- throw new Error(`Missing parameter '${name}' in route ${id}`);
155
- }
156
-
157
- if (param_value.startsWith('/') || param_value.endsWith('/'))
158
- throw new Error(
159
- `Parameter '${name}' in route ${id} cannot start or end with a slash -- this would cause an invalid route like foo//bar`
160
- );
161
- return param_value;
162
- })
163
- )
164
- .filter(Boolean)
165
- .join('/')
166
- );
181
+ export function fail(status, data) {
182
+ // @ts-expect-error unique symbol missing
183
+ return new ActionFailure(status, data);
167
184
  }
@@ -1,5 +1,5 @@
1
1
  import * as set_cookie_parser from 'set-cookie-parser';
2
- import { error } from '../index.js';
2
+ import { SvelteKitError } from '../../runtime/control.js';
3
3
 
4
4
  /**
5
5
  * @param {import('http').IncomingMessage} req
@@ -22,19 +22,6 @@ function get_raw_body(req, body_size_limit) {
22
22
  return null;
23
23
  }
24
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
25
  if (req.destroyed) {
39
26
  const readable = new ReadableStream();
40
27
  readable.cancel();
@@ -46,6 +33,17 @@ function get_raw_body(req, body_size_limit) {
46
33
 
47
34
  return new ReadableStream({
48
35
  start(controller) {
36
+ if (body_size_limit !== undefined && content_length > body_size_limit) {
37
+ const error = new SvelteKitError(
38
+ 413,
39
+ 'Payload Too Large',
40
+ `Content-length of ${content_length} exceeds limit of ${body_size_limit} bytes.`
41
+ );
42
+
43
+ controller.error(error);
44
+ return;
45
+ }
46
+
49
47
  req.on('error', (error) => {
50
48
  cancelled = true;
51
49
  controller.error(error);
@@ -60,16 +58,15 @@ function get_raw_body(req, body_size_limit) {
60
58
  if (cancelled) return;
61
59
 
62
60
  size += chunk.length;
63
- if (size > length) {
61
+ if (size > content_length) {
64
62
  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
- );
63
+
64
+ const constraint = content_length ? 'content-length' : 'BODY_SIZE_LIMIT';
65
+ const message = `request body size exceeded ${constraint} of ${content_length}`;
66
+
67
+ const error = new SvelteKitError(413, 'Payload Too Large', message);
68
+ controller.error(error);
69
+
73
70
  return;
74
71
  }
75
72
 
@@ -124,7 +121,7 @@ export async function setResponse(res, response) {
124
121
  ? set_cookie_parser.splitCookiesString(
125
122
  // This is absurd but necessary, TODO: investigate why
126
123
  /** @type {string}*/ (response.headers.get(key))
127
- )
124
+ )
128
125
  : value
129
126
  );
130
127
  } catch (error) {
@@ -1,54 +1,25 @@
1
- import { ReadableStream, TransformStream, WritableStream } from 'node:stream/web';
2
1
  import buffer from 'node:buffer';
3
2
  import { webcrypto as crypto } from 'node:crypto';
4
- import { fetch, Response, Request, Headers, FormData, File as UndiciFile } from 'undici';
5
3
 
6
4
  // `buffer.File` was added in Node 18.13.0 while the `File` global was added in Node 20.0.0
7
- const File = /** @type {import('node:buffer') & { File?: File}} */ (buffer).File ?? UndiciFile;
5
+ const File = /** @type {import('node:buffer') & { File?: File}} */ (buffer).File;
8
6
 
9
7
  /** @type {Record<string, any>} */
10
- const globals_post_node_18_11 = {
8
+ const globals = {
11
9
  crypto,
12
10
  File
13
11
  };
14
12
 
15
- /** @type {Record<string, any>} */
16
- // TODO: remove this once we only support Node 18.11+ (the version multipart/form-data was added)
17
- const globals_pre_node_18_11 = {
18
- crypto,
19
- fetch,
20
- Response,
21
- Request,
22
- Headers,
23
- ReadableStream,
24
- TransformStream,
25
- WritableStream,
26
- FormData,
27
- File
28
- };
29
-
30
13
  // exported for dev/preview and node environments
31
14
  /**
32
15
  * Make various web APIs available as globals:
33
16
  * - `crypto`
34
- * - `fetch` (only in node < 18.11)
35
- * - `Headers` (only in node < 18.11)
36
- * - `Request` (only in node < 18.11)
37
- * - `Response` (only in node < 18.11)
17
+ * - `File`
38
18
  */
39
19
  export function installPolyfills() {
40
- // Be defensive (we don't know in which environments this is called) and always apply if something goes wrong
41
- let globals = globals_pre_node_18_11;
42
- try {
43
- const version = process.versions.node.split('.').map((n) => parseInt(n, 10));
44
- if ((version[0] === 18 && version[1] >= 11) || version[0] > 18) {
45
- globals = globals_post_node_18_11;
46
- }
47
- } catch (e) {
48
- // ignore
49
- }
50
-
51
20
  for (const name in globals) {
21
+ if (name in globalThis) continue;
22
+
52
23
  Object.defineProperty(globalThis, name, {
53
24
  enumerable: true,
54
25
  configurable: true,