@sveltejs/kit 3.0.0-next.2 → 3.0.0-next.21

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 (195) hide show
  1. package/package.json +62 -36
  2. package/src/cli.js +15 -12
  3. package/src/constants.js +6 -1
  4. package/src/core/adapt/builder.js +66 -72
  5. package/src/core/adapt/index.js +17 -6
  6. package/src/core/config/index.js +134 -80
  7. package/src/core/config/options.js +295 -273
  8. package/src/core/config/types.d.ts +1 -1
  9. package/src/core/env.js +146 -16
  10. package/src/core/generate_manifest/index.js +18 -15
  11. package/src/core/postbuild/analyse.js +8 -20
  12. package/src/core/postbuild/crawl.js +22 -6
  13. package/src/core/postbuild/entities.js +8 -2
  14. package/src/core/postbuild/fallback.js +4 -2
  15. package/src/core/postbuild/prerender.js +222 -70
  16. package/src/core/postbuild/queue.js +2 -1
  17. package/src/core/sync/create_manifest_data/conflict.js +1 -1
  18. package/src/core/sync/create_manifest_data/index.js +82 -62
  19. package/src/core/sync/sync.js +40 -31
  20. package/src/core/sync/ts.js +1 -1
  21. package/src/core/sync/utils.js +22 -2
  22. package/src/core/sync/{write_non_ambient.js → write_app_types.js} +107 -58
  23. package/src/core/sync/write_client_manifest.js +14 -29
  24. package/src/core/sync/write_env.js +7 -5
  25. package/src/core/sync/write_server.js +37 -45
  26. package/src/core/sync/write_tsconfig/index.js +274 -0
  27. package/src/core/sync/write_tsconfig/test-app/package.json +7 -0
  28. package/src/core/sync/write_tsconfig/utils.js +77 -0
  29. package/src/core/sync/write_tsconfig/validate.js +128 -0
  30. package/src/core/sync/write_types/index.js +127 -123
  31. package/src/core/utils.js +30 -5
  32. package/src/exports/env/index.js +77 -0
  33. package/src/exports/env/public.d.ts +55 -0
  34. package/src/exports/hooks/index.js +3 -9
  35. package/src/exports/hooks/public.d.ts +195 -0
  36. package/src/exports/hooks/sequence.js +9 -6
  37. package/src/exports/index.js +52 -29
  38. package/src/exports/internal/client.js +5 -0
  39. package/src/exports/internal/env.js +8 -5
  40. package/src/exports/internal/index.js +1 -90
  41. package/src/exports/internal/{event.js → server/event.js} +2 -3
  42. package/src/exports/internal/server/index.js +37 -0
  43. package/src/exports/internal/server/telemetry.js +95 -0
  44. package/src/exports/internal/shared.js +90 -0
  45. package/src/exports/node/index.js +64 -22
  46. package/src/exports/params/index.js +70 -0
  47. package/src/exports/params/public.d.ts +63 -0
  48. package/src/exports/public.d.ts +50 -1733
  49. package/src/exports/remote/index.js +11 -0
  50. package/src/exports/remote/public.d.ts +519 -0
  51. package/src/exports/url.js +86 -0
  52. package/src/exports/vite/build/build_server.js +54 -65
  53. package/src/exports/vite/build/remote.js +24 -19
  54. package/src/exports/vite/build/utils.js +0 -8
  55. package/src/exports/vite/dev/index.js +218 -146
  56. package/src/exports/vite/index.js +1590 -858
  57. package/src/exports/vite/module_ids.js +2 -2
  58. package/src/exports/vite/preview/index.js +40 -32
  59. package/src/exports/vite/public.d.ts +588 -0
  60. package/src/exports/vite/utils.js +84 -46
  61. package/src/pathname.js +55 -0
  62. package/src/runner.js +15 -0
  63. package/src/runtime/app/env/internal.js +4 -4
  64. package/src/runtime/app/env/types.d.ts +1 -1
  65. package/src/runtime/app/environment/index.js +3 -3
  66. package/src/runtime/app/{forms.js → forms/index.js} +73 -42
  67. package/src/runtime/app/forms/public.d.ts +2 -0
  68. package/src/runtime/app/forms/types.d.ts +56 -0
  69. package/src/runtime/app/internal/transport.js +53 -0
  70. package/src/runtime/app/manifest/index.js +1 -0
  71. package/src/runtime/app/{navigation.js → navigation/index.js} +2 -1
  72. package/src/runtime/app/navigation/public.d.ts +237 -0
  73. package/src/runtime/app/paths/client.js +37 -37
  74. package/src/runtime/app/paths/index.js +1 -1
  75. package/src/runtime/app/paths/internal/client.js +34 -2
  76. package/src/runtime/app/paths/internal/server.js +6 -23
  77. package/src/runtime/app/paths/internal.d.ts +3 -0
  78. package/src/runtime/app/paths/public.d.ts +1 -29
  79. package/src/runtime/app/paths/server.js +36 -17
  80. package/src/runtime/app/paths/types.d.ts +11 -19
  81. package/src/runtime/app/server/index.js +2 -2
  82. package/src/runtime/app/server/public.d.ts +201 -0
  83. package/src/runtime/app/server/remote/command.js +13 -11
  84. package/src/runtime/app/server/remote/form.js +61 -39
  85. package/src/runtime/app/server/remote/prerender.js +45 -45
  86. package/src/runtime/app/server/remote/query.js +106 -108
  87. package/src/runtime/app/server/remote/requested.js +27 -19
  88. package/src/runtime/app/server/remote/shared.js +76 -76
  89. package/src/runtime/app/service-worker/index.js +24 -0
  90. package/src/runtime/app/state/client.js +4 -2
  91. package/src/runtime/app/state/index.js +7 -5
  92. package/src/runtime/app/state/public.d.ts +72 -0
  93. package/src/runtime/app/state/server.js +3 -0
  94. package/src/runtime/app/stores.js +15 -78
  95. package/src/runtime/client/bundle.js +1 -1
  96. package/src/runtime/client/client-entry.js +3 -0
  97. package/src/runtime/client/client.js +1336 -648
  98. package/src/runtime/client/constants.js +3 -6
  99. package/src/runtime/client/entry.js +24 -3
  100. package/src/runtime/client/fetcher.js +25 -25
  101. package/src/runtime/client/ndjson.js +1 -1
  102. package/src/runtime/client/parse.js +1 -1
  103. package/src/runtime/client/payload.js +17 -0
  104. package/src/runtime/client/remote-functions/cache.svelte.js +3 -1
  105. package/src/runtime/client/remote-functions/command.svelte.js +37 -61
  106. package/src/runtime/client/remote-functions/form.svelte.js +267 -178
  107. package/src/runtime/client/remote-functions/prerender.svelte.js +32 -11
  108. package/src/runtime/client/remote-functions/query/index.js +10 -17
  109. package/src/runtime/client/remote-functions/query/instance.svelte.js +64 -18
  110. package/src/runtime/client/remote-functions/query/proxy.js +5 -5
  111. package/src/runtime/client/remote-functions/query-batch.svelte.js +61 -70
  112. package/src/runtime/client/remote-functions/query-live/index.js +1 -1
  113. package/src/runtime/client/remote-functions/query-live/instance.svelte.js +63 -21
  114. package/src/runtime/client/remote-functions/query-live/iterator.js +15 -12
  115. package/src/runtime/client/remote-functions/query-live/proxy.js +2 -12
  116. package/src/runtime/client/remote-functions/shared.svelte.js +90 -65
  117. package/src/runtime/client/snapshots.js +147 -0
  118. package/src/runtime/client/state.svelte.js +94 -55
  119. package/src/runtime/client/stream.js +3 -2
  120. package/src/runtime/client/types.d.ts +13 -9
  121. package/src/runtime/client/utils.js +22 -110
  122. package/src/runtime/components/root.svelte +56 -0
  123. package/src/runtime/env/dynamic/private.js +7 -0
  124. package/src/runtime/env/dynamic/public.js +7 -0
  125. package/src/runtime/env/static/private.js +6 -0
  126. package/src/runtime/env/static/public.js +6 -0
  127. package/src/runtime/form-utils.js +172 -61
  128. package/src/runtime/pathname.js +20 -32
  129. package/src/runtime/props.svelte.js +72 -0
  130. package/src/runtime/server/constants.js +0 -3
  131. package/src/runtime/server/cookie.js +72 -55
  132. package/src/runtime/server/csrf.js +65 -0
  133. package/src/runtime/server/data/index.js +49 -53
  134. package/src/runtime/server/dev.js +22 -0
  135. package/src/runtime/server/endpoint.js +6 -7
  136. package/src/runtime/server/env_module.js +0 -5
  137. package/src/runtime/server/errors.js +160 -0
  138. package/src/runtime/server/fetch.js +32 -39
  139. package/src/runtime/server/index.js +90 -64
  140. package/src/runtime/server/internal.js +71 -0
  141. package/src/runtime/server/page/actions.js +85 -64
  142. package/src/runtime/server/page/crypto.js +2 -2
  143. package/src/runtime/server/page/csp.js +88 -104
  144. package/src/runtime/server/page/data_serializer.js +24 -25
  145. package/src/runtime/server/page/index.js +36 -53
  146. package/src/runtime/server/page/load_data.js +50 -57
  147. package/src/runtime/server/page/render.js +173 -239
  148. package/src/runtime/server/page/respond_with_error.js +17 -31
  149. package/src/runtime/server/page/serialize_data.js +2 -13
  150. package/src/runtime/server/page/server_routing.js +85 -26
  151. package/src/runtime/server/remote-functions.js +640 -0
  152. package/src/runtime/server/respond.js +190 -131
  153. package/src/runtime/server/sourcemaps.js +183 -0
  154. package/src/runtime/server/state.js +53 -0
  155. package/src/runtime/server/utils.js +13 -155
  156. package/src/runtime/shared.js +20 -40
  157. package/src/runtime/utils.js +3 -0
  158. package/src/types/ambient-private.d.ts +11 -1
  159. package/src/types/ambient.d.ts +87 -36
  160. package/src/types/global-private.d.ts +25 -25
  161. package/src/types/internal.d.ts +163 -157
  162. package/src/types/private.d.ts +41 -1
  163. package/src/utils/error.js +28 -4
  164. package/src/utils/escape.js +9 -25
  165. package/src/utils/features.js +1 -1
  166. package/src/utils/filesystem.js +1 -23
  167. package/src/utils/fork.js +7 -2
  168. package/src/utils/hash.js +21 -0
  169. package/src/utils/http.js +8 -7
  170. package/src/utils/import.js +9 -2
  171. package/src/utils/imports.js +83 -0
  172. package/src/utils/mime.js +9 -0
  173. package/src/utils/page_nodes.js +6 -7
  174. package/src/utils/params.js +67 -0
  175. package/src/utils/regex.js +9 -0
  176. package/src/utils/routing.js +145 -73
  177. package/src/utils/shared-iterator.js +5 -0
  178. package/src/utils/streaming.js +14 -4
  179. package/src/utils/url.js +33 -2
  180. package/src/version.js +1 -1
  181. package/types/index.d.ts +2616 -2433
  182. package/types/index.d.ts.map +120 -106
  183. package/src/core/sync/write_ambient.js +0 -18
  184. package/src/core/sync/write_root.js +0 -148
  185. package/src/core/sync/write_tsconfig.js +0 -250
  186. package/src/exports/internal/server.js +0 -22
  187. package/src/exports/node/polyfills.js +0 -30
  188. package/src/runtime/server/app.js +0 -9
  189. package/src/runtime/server/remote.js +0 -457
  190. package/src/runtime/shared-server.js +0 -7
  191. package/src/runtime/telemetry/otel.js +0 -21
  192. package/src/runtime/telemetry/record_span.js +0 -65
  193. package/src/types/synthetic/$lib.md +0 -5
  194. /package/src/exports/internal/{remote-functions.js → server/remote-functions.js} +0 -0
  195. /package/src/{runtime/telemetry/noop.js → telemetry.js} +0 -0
@@ -0,0 +1,195 @@
1
+ import { StandardSchemaV1 } from '@standard-schema/spec';
2
+ import { NavigationEvent } from '@sveltejs/kit';
3
+ import { RequestEvent } from '$app/server';
4
+ import { MaybePromise } from 'types';
5
+
6
+ export * from './index.js';
7
+
8
+ /**
9
+ * The [`handle`](https://svelte.dev/docs/kit/hooks#handle) hook runs every time the SvelteKit server receives a [request](https://svelte.dev/docs/kit/web-standards#Fetch-APIs-Request) and
10
+ * determines the [response](https://svelte.dev/docs/kit/web-standards#Fetch-APIs-Response).
11
+ * It receives an `event` object representing the request and a function called `resolve`, which renders the route and generates a `Response`.
12
+ * This allows you to modify response headers or bodies, or bypass SvelteKit entirely (for implementing routes programmatically, for example).
13
+ */
14
+ export type Handle = (input: {
15
+ event: RequestEvent;
16
+ resolve: (event: RequestEvent, opts?: ResolveOptions) => Promise<Response>;
17
+ }) => MaybePromise<Response>;
18
+
19
+ type CaughtErrorMap = {
20
+ app: App.Error;
21
+ framework: { status: number; message: string };
22
+ unknown: unknown;
23
+ };
24
+
25
+ type ValidationCaughtError<Issue extends StandardSchemaV1.Issue> = {
26
+ kind: 'validation';
27
+ error: { status: number; message: string };
28
+ issues: Issue[];
29
+ };
30
+
31
+ /**
32
+ * The error passed to the [`handleError`](https://svelte.dev/docs/kit/hooks#handleError) hooks.
33
+ * Use the `kind` discriminant to distinguish errors from your app (thrown with the
34
+ * [`error`](https://svelte.dev/docs/kit/errors#App-errors) helper), errors generated by
35
+ * SvelteKit itself (such as 404s), validation errors, and unknown errors (thrown by your code,
36
+ * or code it calls).
37
+ */
38
+ export type CaughtError<Issue extends StandardSchemaV1.Issue = StandardSchemaV1.Issue> =
39
+ | {
40
+ [Kind in keyof CaughtErrorMap]: {
41
+ /** Identifies the category and origin of the error */
42
+ kind: Kind;
43
+ /** The caught error. Its type depends on `kind` */
44
+ error: CaughtErrorMap[Kind];
45
+ /** Only present for validation errors */
46
+ issues?: undefined;
47
+ };
48
+ }[keyof CaughtErrorMap]
49
+ | ValidationCaughtError<Issue>;
50
+
51
+ /** The error passed to the client-side `handleError` hook. */
52
+ export type ClientCaughtError = Exclude<CaughtError, { kind: 'validation' }>;
53
+
54
+ /**
55
+ * The server-side [`handleError`](https://svelte.dev/docs/kit/hooks#handleError) hook runs for every error thrown while responding to a request, except redirects.
56
+ *
57
+ * The `kind` property discriminates between _app_ errors (thrown with the [`error`](https://svelte.dev/docs/kit/errors#App-errors) helper),
58
+ * _framework_ errors (generated by SvelteKit itself, such as 404s), _validation_ errors (caused by invalid remote function arguments)
59
+ * and _unknown_ errors (thrown by your code, or code it calls).
60
+ *
61
+ * The hook returns an object matching `App.Error`, in which `status` and `message` are optional — return them only to
62
+ * override the defaults. Omitted properties are inherited from the caught error: the body passed to `error(...)` for app errors,
63
+ * the status and safe message for framework and validation errors, and `500`/`'Internal Error'` for unknown errors. Return nothing to
64
+ * keep the defaults entirely (if you augment `App.Error` with required properties, you must return those).
65
+ *
66
+ * Make sure that this function _never_ throws an error.
67
+ */
68
+ export type HandleServerError<Issue extends StandardSchemaV1.Issue = StandardSchemaV1.Issue> = (
69
+ input: CaughtError<Issue> & { event: RequestEvent }
70
+ ) => MaybePromise<AppErrorWithOptionalDefaults | VoidIfNoRequiredAppErrorProperties>;
71
+
72
+ /**
73
+ * The client-side [`handleError`](https://svelte.dev/docs/kit/hooks#handleError) hook runs for every error thrown while navigating, except redirects.
74
+ * Errors that were already transformed by the server-side hook are not passed to it a second time.
75
+ *
76
+ * The `kind` property discriminates between _app_ errors (thrown with the [`error`](https://svelte.dev/docs/kit/errors#App-errors) helper),
77
+ * _framework_ errors (generated by SvelteKit itself, such as 404s) and _unknown_ errors (thrown by your code, or code it calls).
78
+ *
79
+ * The hook returns an object matching `App.Error`, in which `status` and `message` are optional — return them only to
80
+ * override the defaults. Omitted properties are inherited from the caught error: the body passed to `error(...)` for app errors,
81
+ * the status and safe message for framework errors, and `500`/`'Internal Error'` for unknown errors. Return nothing to
82
+ * keep the defaults entirely (if you augment `App.Error` with required properties, you must return those).
83
+ *
84
+ * Make sure that this function _never_ throws an error.
85
+ */
86
+ export type HandleClientError = (
87
+ input: ClientCaughtError & { event: NavigationEvent }
88
+ ) => MaybePromise<AppErrorWithOptionalDefaults | VoidIfNoRequiredAppErrorProperties>;
89
+
90
+ /**
91
+ * The [`handleFetch`](https://svelte.dev/docs/kit/hooks#handleFetch) hook allows you to modify (or replace) the result of an [`event.fetch`](https://svelte.dev/docs/kit/load#Making-fetch-requests) call that runs on the server (or during prerendering) inside an endpoint, `load`, `action`, `handle`, `handleError` or `reroute`.
92
+ */
93
+ export type HandleFetch = (input: {
94
+ event: RequestEvent;
95
+ request: Request;
96
+ fetch: typeof fetch;
97
+ }) => MaybePromise<Response>;
98
+
99
+ /**
100
+ * The [`init`](https://svelte.dev/docs/kit/hooks#init) will be invoked before the server responds to its first request
101
+ * @since 2.10.0
102
+ */
103
+ export type ServerInit = () => MaybePromise<void>;
104
+
105
+ /**
106
+ * The [`init`](https://svelte.dev/docs/kit/hooks#init) will be invoked once the app starts in the browser
107
+ * @since 2.10.0
108
+ */
109
+ export type ClientInit = () => MaybePromise<void>;
110
+
111
+ /**
112
+ * The [`reroute`](https://svelte.dev/docs/kit/hooks#reroute) hook allows you to modify the URL before it is used to determine which route to render.
113
+ * @since 2.3.0
114
+ */
115
+ export type Reroute = (event: { url: URL; fetch: typeof fetch }) => MaybePromise<void | string>;
116
+
117
+ /**
118
+ * The [`transport`](https://svelte.dev/docs/kit/hooks#transport) hook allows you to transport custom types across the server/client boundary.
119
+ *
120
+ * Each transporter has a pair of `encode` and `decode` functions. On the server, `encode` determines whether a value is an instance of the custom type and, if so, returns a non-falsy encoding of the value which can be an object or an array (or `false` otherwise).
121
+ *
122
+ * In the browser, `decode` turns the encoding back into an instance of the custom type.
123
+ *
124
+ * ```ts
125
+ * import type { Transport } from '@sveltejs/kit/hooks';
126
+ *
127
+ * declare class MyCustomType {
128
+ * data: any
129
+ * }
130
+ *
131
+ * // hooks.js
132
+ * export const transport: Transport = {
133
+ * MyCustomType: {
134
+ * encode: (value) => value instanceof MyCustomType && [value.data],
135
+ * decode: ([data]) => new MyCustomType(data)
136
+ * }
137
+ * };
138
+ * ```
139
+ * @since 2.11.0
140
+ */
141
+ export type Transport = Record<string, Transporter>;
142
+
143
+ /**
144
+ * A member of the [`transport`](https://svelte.dev/docs/kit/hooks#transport) hook.
145
+ */
146
+ export interface Transporter<
147
+ T = any,
148
+ U = any /* minus falsy values, but we can't properly express that */
149
+ > {
150
+ encode: (value: T) => false | U;
151
+ decode: (data: U) => T;
152
+ }
153
+
154
+ export interface ResolveOptions {
155
+ /**
156
+ * Applies custom transforms to HTML. If `done` is true, it's the final chunk. Chunks are not guaranteed to be well-formed HTML
157
+ * (they could include an element's opening tag but not its closing tag, for example)
158
+ * but they will always be split at sensible boundaries such as `%sveltekit.head%` or layout/page components.
159
+ * @param input the html chunk and the info if this is the last chunk
160
+ */
161
+ transformPageChunk?: (input: { html: string; done: boolean }) => MaybePromise<string | undefined>;
162
+ /**
163
+ * Determines which headers should be included in serialized responses when a `load` function loads a resource with `fetch`.
164
+ * By default, none will be included.
165
+ * @param name header name
166
+ * @param value header value
167
+ */
168
+ filterSerializedResponseHeaders?: (name: string, value: string) => boolean;
169
+ /**
170
+ * Determines which files should be preloaded. Files are preloaded via `<link>` tags added to the
171
+ * `<head>` tag; if `output.linkHeaderPreload` is enabled, dynamically rendered pages use the
172
+ * [`Link` response header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link) instead.
173
+ * By default, `js` and `css` files will be preloaded.
174
+ * @param input the type of the file and its path
175
+ */
176
+ preload?: (input: { type: 'font' | 'css' | 'js' | 'asset'; path: string }) => boolean;
177
+ }
178
+
179
+ type AppErrorWithOptionalDefaults = Omit<App.Error, 'status' | 'message'> & {
180
+ status?: App.Error['status'];
181
+ message?: App.Error['message'];
182
+ };
183
+
184
+ /**
185
+ * `void` is only a valid `handleError` return when `App.Error` adds no required properties
186
+ * beyond `status` and `message` — both of which are optional in the return, since they default
187
+ * to those of the caught error. If `App.Error` is augmented with required properties, the hook
188
+ * must return them, so returning nothing becomes a type error.
189
+ */
190
+ type VoidIfNoRequiredAppErrorProperties = {
191
+ status: number;
192
+ message: string;
193
+ } extends App.Error
194
+ ? void
195
+ : never;
@@ -1,8 +1,9 @@
1
- /** @import { Handle, RequestEvent, ResolveOptions } from '@sveltejs/kit' */
2
- /** @import { MaybePromise } from 'types' */
1
+ /** @import { RequestEvent } from '$app/server' */
2
+ /** @import { Handle, ResolveOptions } from '@sveltejs/kit/hooks' */
3
3
  import {
4
4
  merge_tracing,
5
5
  get_request_store,
6
+ record_span,
6
7
  with_request_store
7
8
  } from '@sveltejs/kit/internal/server';
8
9
 
@@ -17,7 +18,7 @@ import {
17
18
  * /// file: src/hooks.server.js
18
19
  * import { sequence } from '@sveltejs/kit/hooks';
19
20
  *
20
- * /// type: import('@sveltejs/kit').Handle
21
+ * /// type: import('@sveltejs/kit/hooks').Handle
21
22
  * async function first({ event, resolve }) {
22
23
  * console.log('first pre-processing');
23
24
  * const result = await resolve(event, {
@@ -36,7 +37,7 @@ import {
36
37
  * return result;
37
38
  * }
38
39
  *
39
- * /// type: import('@sveltejs/kit').Handle
40
+ * /// type: import('@sveltejs/kit/hooks').Handle
40
41
  * async function second({ event, resolve }) {
41
42
  * console.log('second pre-processing');
42
43
  * const result = await resolve(event, {
@@ -74,6 +75,8 @@ import {
74
75
  * first post-processing
75
76
  * ```
76
77
  *
78
+ * Calling `resolve` invokes the next handler in the sequence (or SvelteKit itself, if it is the last one). To pass data between handlers, use `event.locals`.
79
+ *
77
80
  * @param {...Handle} handlers The chain of `handle` functions
78
81
  * @returns {Handle}
79
82
  */
@@ -89,12 +92,12 @@ export function sequence(...handlers) {
89
92
  * @param {number} i
90
93
  * @param {RequestEvent} event
91
94
  * @param {ResolveOptions | undefined} parent_options
92
- * @returns {MaybePromise<Response>}
95
+ * @returns {Promise<Response>}
93
96
  */
94
97
  function apply_handle(i, event, parent_options) {
95
98
  const handle = handlers[i];
96
99
 
97
- return state.tracing.record_span({
100
+ return record_span({
98
101
  name: `sveltekit.handle.sequenced.${handle.name ? handle.name : i}`,
99
102
  attributes: {},
100
103
  fn: async (current) => {
@@ -1,6 +1,5 @@
1
1
  /** @import { StandardSchemaV1 } from '@standard-schema/spec' */
2
-
3
- import { HttpError, Redirect, ActionFailure, ValidationError } from './internal/index.js';
2
+ import { HttpError, Redirect, ActionFailure, ValidationError } from './internal/shared.js';
4
3
  import { BROWSER, DEV } from 'esm-env';
5
4
  import {
6
5
  add_data_suffix,
@@ -9,8 +8,10 @@ import {
9
8
  has_resolution_suffix,
10
9
  strip_data_suffix,
11
10
  strip_resolution_suffix
12
- } from '../runtime/pathname.js';
13
- import { text_encoder } from '../runtime/utils.js';
11
+ } from '../pathname.js';
12
+ import { validate_redirect_location } from './url.js';
13
+
14
+ const text_encoder = new TextEncoder();
14
15
 
15
16
  export { VERSION } from '../version.js';
16
17
 
@@ -23,13 +24,13 @@ export { VERSION } from '../version.js';
23
24
  /**
24
25
  * Throws an error with a HTTP status code and an optional message.
25
26
  * When called during request handling, this will cause SvelteKit to
26
- * return an error response without invoking `handleError`.
27
+ * return an error response; the error will be passed to `handleError` as an _expected_ error.
27
28
  * Make sure you're not catching the thrown error, which would prevent SvelteKit from handling it.
28
29
  * @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.
29
- * @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.
30
+ * @param {string} [message] The error message.
30
31
  * @overload
31
- * @param {number} status
32
- * @param {App.Error} body
32
+ * @param {{ status: number; message: string } extends App.Error ? number : never} status
33
+ * @param {{ status: number; message: string } extends App.Error ? string : never} [message]
33
34
  * @return {never}
34
35
  * @throws {import('./public.js').HttpError} This error instructs SvelteKit to initiate HTTP error handling.
35
36
  * @throws {Error} If the provided status is invalid (not between 400 and 599).
@@ -37,13 +38,15 @@ export { VERSION } from '../version.js';
37
38
  /**
38
39
  * Throws an error with a HTTP status code and an optional message.
39
40
  * When called during request handling, this will cause SvelteKit to
40
- * return an error response without invoking `handleError`.
41
+ * return an error response; the error will be passed to `handleError` as an _expected_ error.
41
42
  * Make sure you're not catching the thrown error, which would prevent SvelteKit from handling it.
42
43
  * @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.
43
- * @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.
44
+ * @param {string} message The error message.
45
+ * @param {keyof Omit<App.Error, 'status' | 'message'> extends never ? never : Omit<App.Error, 'status' | 'message'>} properties Additional properties of the App.Error type.
44
46
  * @overload
45
47
  * @param {number} status
46
- * @param {{ message: string } extends App.Error ? App.Error | string | undefined : never} [body]
48
+ * @param {string} message
49
+ * @param {keyof Omit<App.Error, 'status' | 'message'> extends never ? never : Omit<App.Error, 'status' | 'message'>} properties
47
50
  * @return {never}
48
51
  * @throws {import('./public.js').HttpError} This error instructs SvelteKit to initiate HTTP error handling.
49
52
  * @throws {Error} If the provided status is invalid (not between 400 and 599).
@@ -51,20 +54,46 @@ export { VERSION } from '../version.js';
51
54
  /**
52
55
  * Throws an error with a HTTP status code and an optional message.
53
56
  * When called during request handling, this will cause SvelteKit to
54
- * return an error response without invoking `handleError`.
57
+ * return an error response; the error will be passed to `handleError` as an _expected_ error.
55
58
  * Make sure you're not catching the thrown error, which would prevent SvelteKit from handling it.
59
+ * @deprecated Passing an `App.Error` body as the second argument is deprecated — pass the `message` as the second argument, and any additional properties as the third
56
60
  * @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.
57
- * @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.
61
+ * @param {Omit<App.Error, 'status'> & { status?: App.Error['status'] }} body An object that conforms to the App.Error type. If a string is passed, it will be used as the message property.
62
+ * @overload
63
+ * @param {number} status
64
+ * @param {Omit<App.Error, 'status'> & { status?: App.Error['status'] }} properties
65
+ * @return {never}
66
+ * @throws {import('./public.js').HttpError} This error instructs SvelteKit to initiate HTTP error handling.
67
+ * @throws {Error} If the provided status is invalid (not between 400 and 599).
68
+ */
69
+ /**
70
+ * Throws an error with a HTTP status code and an optional message.
71
+ * When called during request handling, this will cause SvelteKit to
72
+ * return an error response; the error will be passed to `handleError` as an _expected_ error.
73
+ * Make sure you're not catching the thrown error, which would prevent SvelteKit from handling it.
74
+ * @param {any} 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.
75
+ * @param {any} [message] A string, or (deprecated) a partial App.Error object
76
+ * @param {any} [properties] Additional properties of the App.Error type when passing a string message.
58
77
  * @return {never}
59
78
  * @throws {import('./public.js').HttpError} This error instructs SvelteKit to initiate HTTP error handling.
60
79
  * @throws {Error} If the provided status is invalid (not between 400 and 599).
61
80
  */
62
- export function error(status, body) {
81
+ export function error(status, message, properties) {
63
82
  if ((!BROWSER || DEV) && (isNaN(status) || status < 400 || status > 599)) {
64
83
  throw new Error(`HTTP error status codes must be between 400 and 599 — ${status} is invalid`);
65
84
  }
66
85
 
67
- throw new HttpError(status, body);
86
+ if (message !== undefined && typeof message !== 'string') {
87
+ if (DEV) {
88
+ console.warn(
89
+ 'Passing an `App.Error` body as the second argument is deprecated — pass the `message` as the second argument, and any additional properties as the third'
90
+ );
91
+ }
92
+
93
+ ({ message, ...properties } = message);
94
+ }
95
+
96
+ throw new HttpError({ ...properties, status, message: message ?? `Error: ${status}` });
68
97
  }
69
98
 
70
99
  /**
@@ -92,19 +121,23 @@ export function isHttpError(e, status) {
92
121
  *
93
122
  * @param {300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | ({} & number)} status The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#redirection_messages). Must be in the range 300-308.
94
123
  * @param {string | URL} location The location to redirect to.
124
+ * @param {{ external?: boolean | string[] }} [options] To redirect to an external URL, you must pass `{ external: true }` to allow any external URL except `javascript:` URLs, or `{ external: [...] }` with an allowlist of permitted origins.
95
125
  * @throws {import('./public.js').Redirect} This error instructs SvelteKit to redirect to the specified location.
96
- * @throws {Error} If the provided status is invalid or the location cannot be used as a header value.
126
+ * @throws {Error} If the provided status is invalid, the location cannot be used as a header value, or the location is an external URL without permission.
97
127
  * @return {never}
98
128
  */
99
- export function redirect(status, location) {
129
+ export function redirect(status, location, options) {
100
130
  if ((!BROWSER || DEV) && (isNaN(status) || status < 300 || status > 308)) {
101
131
  throw new Error('Invalid status code');
102
132
  }
103
133
 
134
+ const href = location.toString();
135
+ validate_redirect_location(href, options);
136
+
104
137
  throw new Redirect(
105
138
  // @ts-ignore
106
139
  status,
107
- location.toString()
140
+ href
108
141
  );
109
142
  }
110
143
 
@@ -212,7 +245,7 @@ export function isActionFailure(e) {
212
245
  * ```ts
213
246
  * import { invalid } from '@sveltejs/kit';
214
247
  * import { form } from '$app/server';
215
- * import { tryLogin } from '$lib/server/auth';
248
+ * import { tryLogin } from '#lib/server/auth';
216
249
  * import * as v from 'valibot';
217
250
  *
218
251
  * export const login = form(
@@ -237,16 +270,6 @@ export function invalid(...issues) {
237
270
  );
238
271
  }
239
272
 
240
- /**
241
- * Checks whether this is an validation error thrown by {@link invalid}.
242
- * @param {unknown} e The object to check.
243
- * @return {e is import('./public.js').ActionFailure}
244
- * @since 2.47.3
245
- */
246
- export function isValidationError(e) {
247
- return e instanceof ValidationError;
248
- }
249
-
250
273
  /**
251
274
  * Strips possible SvelteKit-internal suffixes and trailing slashes from the URL pathname.
252
275
  * Returns the normalized URL as well as a method for adding the potential suffix back
@@ -0,0 +1,5 @@
1
+ export function get_origin() {
2
+ return window.location.origin;
3
+ }
4
+
5
+ export * from './shared.js';
@@ -1,10 +1,10 @@
1
1
  /** @import { StandardSchemaV1 } from '@standard-schema/spec' */
2
- /** @import { EnvVarConfig } from '@sveltejs/kit' */
2
+ /** @import { EnvVarConfig } from '@sveltejs/kit/env' */
3
3
 
4
4
  import { stackless } from '../../utils/error.js';
5
5
 
6
6
  const MISSING = {
7
- message: `Value is missing. If it is optional, add a Standard Schema validator declaring it as such.`
7
+ message: `Value is missing. If it is optional, add a validator declaring it as such.`
8
8
  };
9
9
 
10
10
  const BAD_VALIDATOR = {
@@ -16,7 +16,7 @@ const ASYNC_VALIDATOR = {
16
16
  };
17
17
 
18
18
  /**
19
- * @param {Record<string, EnvVarConfig<any>>} variables
19
+ * @param {Record<string, EnvVarConfig<any> | undefined>} variables
20
20
  * @param {string | undefined} value
21
21
  * @param {string} name
22
22
  * @param {Record<string, StandardSchemaV1.Issue[]>} issues
@@ -24,10 +24,13 @@ const ASYNC_VALIDATOR = {
24
24
  */
25
25
  export function validate(variables, value, name, issues) {
26
26
  const config = variables[name] ?? {};
27
- const validator = config.schema;
27
+ // `defineEnvVars` normalizes function validators to standard schemas
28
+ const validator = /** @type {StandardSchemaV1<string | undefined, any> | undefined} */ (
29
+ config.schema
30
+ );
28
31
 
29
32
  if (!validator) {
30
- if (!value) issues[name] = [MISSING];
33
+ if (value === undefined) issues[name] = [MISSING];
31
34
  return value;
32
35
  }
33
36
 
@@ -1,90 +1 @@
1
- /** @import { StandardSchemaV1 } from '@standard-schema/spec' */
2
-
3
- export class HttpError {
4
- /**
5
- * @param {number} status
6
- * @param {{message: string} extends App.Error ? (App.Error | string | undefined) : App.Error} body
7
- */
8
- constructor(status, body) {
9
- this.status = status;
10
- if (typeof body === 'string') {
11
- this.body = { message: body };
12
- } else if (body) {
13
- this.body = body;
14
- } else {
15
- this.body = { message: `Error: ${status}` };
16
- }
17
- }
18
-
19
- toString() {
20
- return JSON.stringify(this.body);
21
- }
22
- }
23
-
24
- export class Redirect {
25
- /**
26
- * @param {300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308} status
27
- * @param {string} location
28
- */
29
- constructor(status, location) {
30
- try {
31
- new Headers({ location });
32
- } catch {
33
- throw new Error(
34
- `Invalid redirect location ${JSON.stringify(location)}: ` +
35
- 'this string contains characters that cannot be used in HTTP headers'
36
- );
37
- }
38
-
39
- this.status = status;
40
- this.location = location;
41
- }
42
- }
43
-
44
- /**
45
- * An error that was thrown from within the SvelteKit runtime that is not fatal and doesn't result in a 500, such as a 404.
46
- * `SvelteKitError` goes through `handleError`.
47
- * @extends Error
48
- */
49
- export class SvelteKitError extends Error {
50
- /**
51
- * @param {number} status
52
- * @param {string} text
53
- * @param {string} message
54
- */
55
- constructor(status, text, message) {
56
- super(message);
57
- this.status = status;
58
- this.text = text;
59
- }
60
- }
61
-
62
- /**
63
- * @template [T=undefined]
64
- */
65
- export class ActionFailure {
66
- /**
67
- * @param {number} status
68
- * @param {T} data
69
- */
70
- constructor(status, data) {
71
- this.status = status;
72
- this.data = data;
73
- }
74
- }
75
-
76
- /**
77
- * Error thrown when form validation fails imperatively
78
- */
79
- export class ValidationError extends Error {
80
- /**
81
- * @param {StandardSchemaV1.Issue[]} issues
82
- */
83
- constructor(issues) {
84
- super('Validation failed');
85
- this.name = 'ValidationError';
86
- this.issues = issues;
87
- }
88
- }
89
-
90
- export { init_remote_functions } from './remote-functions.js';
1
+ export * from '#internal';
@@ -1,8 +1,7 @@
1
- /** @import { RequestEvent } from '@sveltejs/kit' */
1
+ /** @import { RequestEvent } from '$app/server' */
2
2
  /** @import { RequestStore } from 'types' */
3
3
  /** @import { AsyncLocalStorage } from 'node:async_hooks' */
4
-
5
- import { IN_WEBCONTAINER } from '../../runtime/server/constants.js';
4
+ import { IN_WEBCONTAINER } from '../../../constants.js';
6
5
 
7
6
  /** @type {RequestStore | null} */
8
7
  let sync_store = null;
@@ -0,0 +1,37 @@
1
+ /** @import { Span } from '@opentelemetry/api' */
2
+ import { try_get_request_store } from './event.js';
3
+
4
+ export function get_origin() {
5
+ // `request.url` rather than `event.url`, which throws inside queries
6
+ const request = try_get_request_store()?.event.request;
7
+ return request && new URL(request.url).origin;
8
+ }
9
+
10
+ /**
11
+ * @template {{ tracing: { enabled: boolean, root: Span, current: Span } }} T
12
+ * @param {T} event_like
13
+ * @param {Span} current
14
+ * @returns {T}
15
+ */
16
+ export function merge_tracing(event_like, current) {
17
+ return {
18
+ ...event_like,
19
+ tracing: {
20
+ ...event_like.tracing,
21
+ current
22
+ }
23
+ };
24
+ }
25
+
26
+ export {
27
+ with_request_store,
28
+ getRequestEvent,
29
+ get_request_store,
30
+ try_get_request_store
31
+ } from './event.js';
32
+
33
+ export { init_remote_functions } from './remote-functions.js';
34
+
35
+ export { init_tracing, otel, record_span } from './telemetry.js';
36
+
37
+ export * from '../shared.js';