@sveltejs/kit 3.0.0-next.1 → 3.0.0-next.11

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 (144) hide show
  1. package/package.json +33 -21
  2. package/src/constants.js +2 -0
  3. package/src/core/adapt/builder.js +32 -49
  4. package/src/core/adapt/index.js +1 -4
  5. package/src/core/config/index.js +137 -71
  6. package/src/core/config/options.js +236 -244
  7. package/src/core/config/types.d.ts +1 -1
  8. package/src/core/env.js +121 -5
  9. package/src/core/generate_manifest/index.js +12 -15
  10. package/src/core/postbuild/analyse.js +7 -19
  11. package/src/core/postbuild/crawl.js +22 -6
  12. package/src/core/postbuild/fallback.js +2 -1
  13. package/src/core/postbuild/prerender.js +114 -48
  14. package/src/core/postbuild/queue.js +2 -1
  15. package/src/core/sync/create_manifest_data/index.js +53 -50
  16. package/src/core/sync/sync.js +0 -2
  17. package/src/core/sync/write_client_manifest.js +8 -15
  18. package/src/core/sync/write_env.js +2 -1
  19. package/src/core/sync/write_non_ambient.js +12 -9
  20. package/src/core/sync/write_server.js +14 -15
  21. package/src/core/sync/write_tsconfig.js +16 -8
  22. package/src/core/sync/write_tsconfig_test/package.json +7 -0
  23. package/src/core/sync/write_types/index.js +28 -24
  24. package/src/core/utils.js +30 -5
  25. package/src/exports/env/index.js +77 -0
  26. package/src/exports/hooks/index.js +3 -9
  27. package/src/exports/hooks/sequence.js +3 -2
  28. package/src/exports/index.js +35 -13
  29. package/src/exports/internal/client.js +5 -0
  30. package/src/exports/internal/env.js +8 -5
  31. package/src/exports/internal/index.js +1 -90
  32. package/src/exports/internal/{event.js → server/event.js} +1 -2
  33. package/src/exports/internal/server/index.js +33 -0
  34. package/src/exports/internal/shared.js +89 -0
  35. package/src/exports/node/index.js +62 -15
  36. package/src/exports/params.js +68 -0
  37. package/src/exports/public.d.ts +363 -189
  38. package/src/exports/url.js +86 -0
  39. package/src/exports/vite/build/build_server.js +53 -59
  40. package/src/exports/vite/build/remote.js +18 -11
  41. package/src/exports/vite/build/utils.js +0 -8
  42. package/src/exports/vite/dev/index.js +92 -50
  43. package/src/exports/vite/index.js +903 -681
  44. package/src/exports/vite/module_ids.js +0 -2
  45. package/src/exports/vite/preview/index.js +32 -24
  46. package/src/exports/vite/static_analysis/index.js +2 -4
  47. package/src/exports/vite/static_analysis/types.d.ts +14 -0
  48. package/src/exports/vite/utils.js +40 -35
  49. package/src/runtime/app/env/internal.js +4 -4
  50. package/src/runtime/app/env/types.d.ts +1 -1
  51. package/src/runtime/app/environment/index.js +3 -3
  52. package/src/runtime/app/forms.js +24 -7
  53. package/src/runtime/app/paths/client.js +4 -10
  54. package/src/runtime/app/paths/internal/client.js +4 -2
  55. package/src/runtime/app/paths/internal/server.js +2 -23
  56. package/src/runtime/app/paths/public.d.ts +0 -28
  57. package/src/runtime/app/paths/server.js +10 -6
  58. package/src/runtime/app/server/index.js +1 -1
  59. package/src/runtime/app/server/remote/command.js +0 -3
  60. package/src/runtime/app/server/remote/form.js +27 -15
  61. package/src/runtime/app/server/remote/prerender.js +29 -36
  62. package/src/runtime/app/server/remote/query.js +101 -99
  63. package/src/runtime/app/server/remote/requested.js +22 -14
  64. package/src/runtime/app/server/remote/shared.js +30 -26
  65. package/src/runtime/app/state/client.js +1 -2
  66. package/src/runtime/app/stores.js +13 -76
  67. package/src/runtime/client/bundle.js +1 -1
  68. package/src/runtime/client/client-entry.js +3 -0
  69. package/src/runtime/client/client.js +411 -318
  70. package/src/runtime/client/entry.js +24 -3
  71. package/src/runtime/client/fetcher.js +3 -2
  72. package/src/runtime/client/ndjson.js +6 -33
  73. package/src/runtime/client/payload.js +17 -0
  74. package/src/runtime/client/remote-functions/cache.svelte.js +3 -1
  75. package/src/runtime/client/remote-functions/command.svelte.js +7 -32
  76. package/src/runtime/client/remote-functions/form.svelte.js +183 -112
  77. package/src/runtime/client/remote-functions/prerender.svelte.js +29 -8
  78. package/src/runtime/client/remote-functions/query/index.js +7 -14
  79. package/src/runtime/client/remote-functions/query/instance.svelte.js +63 -18
  80. package/src/runtime/client/remote-functions/query/proxy.js +3 -3
  81. package/src/runtime/client/remote-functions/query-batch.svelte.js +60 -68
  82. package/src/runtime/client/remote-functions/query-live/instance.svelte.js +60 -18
  83. package/src/runtime/client/remote-functions/query-live/iterator.js +36 -55
  84. package/src/runtime/client/remote-functions/shared.svelte.js +88 -62
  85. package/src/runtime/client/sse.js +32 -0
  86. package/src/runtime/client/state.svelte.js +65 -49
  87. package/src/runtime/client/stream.js +39 -0
  88. package/src/runtime/client/types.d.ts +11 -7
  89. package/src/runtime/client/utils.js +0 -96
  90. package/src/runtime/components/root.svelte +66 -0
  91. package/src/runtime/env/dynamic/private.js +7 -0
  92. package/src/runtime/env/dynamic/public.js +7 -0
  93. package/src/runtime/env/static/private.js +6 -0
  94. package/src/runtime/env/static/public.js +6 -0
  95. package/src/runtime/form-utils.js +100 -22
  96. package/src/runtime/server/cookie.js +69 -52
  97. package/src/runtime/server/csrf.js +65 -0
  98. package/src/runtime/server/data/index.js +15 -19
  99. package/src/runtime/server/env_module.js +0 -5
  100. package/src/runtime/server/errors.js +135 -0
  101. package/src/runtime/server/fetch.js +1 -1
  102. package/src/runtime/server/index.js +22 -12
  103. package/src/runtime/server/internal.js +25 -0
  104. package/src/runtime/server/page/actions.js +43 -27
  105. package/src/runtime/server/page/data_serializer.js +10 -10
  106. package/src/runtime/server/page/index.js +12 -17
  107. package/src/runtime/server/page/load_data.js +2 -2
  108. package/src/runtime/server/page/render.js +114 -193
  109. package/src/runtime/server/page/respond_with_error.js +11 -11
  110. package/src/runtime/server/page/server_routing.js +27 -18
  111. package/src/runtime/server/remote-functions.js +596 -0
  112. package/src/runtime/server/respond.js +100 -68
  113. package/src/runtime/server/sourcemaps.js +183 -0
  114. package/src/runtime/server/utils.js +2 -134
  115. package/src/runtime/shared.js +83 -13
  116. package/src/runtime/telemetry/otel.js +1 -1
  117. package/src/runtime/types.d.ts +8 -0
  118. package/src/types/ambient-private.d.ts +2 -0
  119. package/src/types/ambient.d.ts +10 -5
  120. package/src/types/global-private.d.ts +13 -25
  121. package/src/types/internal.d.ts +95 -80
  122. package/src/types/private.d.ts +41 -1
  123. package/src/utils/error.js +24 -4
  124. package/src/utils/escape.js +9 -25
  125. package/src/utils/features.js +1 -1
  126. package/src/utils/fork.js +7 -2
  127. package/src/utils/import.js +6 -1
  128. package/src/utils/imports.js +83 -0
  129. package/src/utils/mime.js +9 -0
  130. package/src/utils/page_nodes.js +3 -5
  131. package/src/utils/params.js +66 -0
  132. package/src/utils/routing.js +90 -44
  133. package/src/utils/shared-iterator.js +5 -0
  134. package/src/utils/streaming.js +14 -4
  135. package/src/utils/url.js +20 -2
  136. package/src/version.js +1 -1
  137. package/types/index.d.ts +537 -557
  138. package/types/index.d.ts.map +24 -39
  139. package/src/core/sync/write_root.js +0 -148
  140. package/src/exports/internal/server.js +0 -22
  141. package/src/runtime/server/remote.js +0 -457
  142. package/src/runtime/shared-server.js +0 -7
  143. package/src/types/synthetic/$lib.md +0 -5
  144. /package/src/exports/internal/{remote-functions.js → server/remote-functions.js} +0 -0
@@ -1,12 +1,15 @@
1
1
  /** @import { RemoteLiveQuery, RemoteLiveQueryFunction, RemoteQuery, RemoteQueryFunction, RequestedResult, QueryRequestedResult, LiveQueryRequestedResult } from '@sveltejs/kit' */
2
2
  /** @import { MaybePromise, RemoteAnyQueryInternals } from 'types' */
3
+ import { HttpError } from '@sveltejs/kit/internal';
3
4
  import { get_request_store } from '@sveltejs/kit/internal/server';
4
- import { create_remote_key, parse_remote_arg } from '../../../shared.js';
5
+ import { parse_remote_arg } from '../../../shared.js';
5
6
  import { noop } from '../../../../utils/functions.js';
7
+ import { get_cache } from './shared.js';
8
+ import { refresh } from './query.js';
6
9
 
7
10
  /**
8
- * In the context of a remote `command` or `form` request, returns an iterable
9
- * of `{ arg, query }` entries for the refreshes requested by the client, up to
11
+ * Inside a remote `command` or `form` callback, returns an iterable
12
+ * of `{ arg, query }` entries for the query instances the client asked to refresh, up to
10
13
  * the supplied `limit`. Each `query` is a `RemoteQuery` bound to the original
11
14
  * client-side cache key, so `refresh()` / `set()` propagate correctly even when
12
15
  * the query's schema transforms the input. `arg` is the *validated* argument,
@@ -15,6 +18,8 @@ import { noop } from '../../../../utils/functions.js';
15
18
  *
16
19
  * Arguments that fail validation or exceed `limit` are recorded as failures in
17
20
  * the response to the client.
21
+ * See [Client-requested refreshes](https://svelte.dev/docs/kit/remote-functions#Single-flight-mutations-Client-requested-refreshes)
22
+ * for usage in a remote `command` or `form`.
18
23
  *
19
24
  * @example
20
25
  * ```ts
@@ -51,14 +56,16 @@ import { noop } from '../../../../utils/functions.js';
51
56
  * @returns {QueryRequestedResult<Validated, Output>}
52
57
  */
53
58
  /**
54
- * In the context of a remote `command` or `form` request, returns an iterable
55
- * of `{ arg, query }` entries for the reconnects requested by the client, up to
59
+ * Inside a remote `command` or `form` callback, returns an iterable
60
+ * of `{ arg, query }` entries for the live query instances the client asked to reconnect, up to
56
61
  * the supplied `limit`. Each `query` is a `RemoteLiveQuery` bound to the original
57
62
  * client-side cache key, so `reconnect()` propagates correctly even when
58
63
  * the query's schema transforms the input. `arg` is the *validated* argument.
59
64
  *
60
65
  * Arguments that fail validation or exceed `limit` are recorded as failures in
61
66
  * the response to the client.
67
+ * See [Client-requested refreshes](https://svelte.dev/docs/kit/remote-functions#Single-flight-mutations-Client-requested-refreshes)
68
+ * for usage in a remote `command` or `form`.
62
69
  *
63
70
  * @example
64
71
  * ```ts
@@ -95,7 +102,7 @@ import { noop } from '../../../../utils/functions.js';
95
102
  * @returns {RequestedResult<Validated, Output>}
96
103
  */
97
104
  export function requested(query, limit) {
98
- const { state } = get_request_store();
105
+ const { event, state } = get_request_store();
99
106
  const internals = /** @type {RemoteAnyQueryInternals | undefined} */ (
100
107
  /** @type {any} */ (query).__
101
108
  );
@@ -115,15 +122,12 @@ export function requested(query, limit) {
115
122
 
116
123
  const requested = state.remote.requested;
117
124
  const payloads = requested?.get(__.id) ?? [];
125
+
118
126
  // note: don't initialize these maps here -- they will be initialized by the
119
127
  // command/form wrapper when we enter them, and if we initialize them here
120
128
  // we will enable requested(...) in contexts where it shouldn't be allowed,
121
129
  // such as load functions or other server functions
122
- const refreshes = state.remote.refreshes;
123
- const reconnects = state.remote.reconnects;
124
- const store = __.type === 'query_live' ? reconnects : refreshes;
125
-
126
- if (!store) {
130
+ if (!state.is_in_remote_form_or_command) {
127
131
  throw new Error(
128
132
  'requested(...) can only be called in the context of a command/form remote function'
129
133
  );
@@ -131,6 +135,9 @@ export function requested(query, limit) {
131
135
  const [selected, skipped] = split_limit(payloads, limit);
132
136
 
133
137
  /**
138
+ * Registers the failure exactly like `.set()` registers a value: the error record
139
+ * is serialized to the client (putting the query there into a failed state), and
140
+ * subsequent server-side calls of the query with the same argument reject with it.
134
141
  * @param {string} payload
135
142
  * @param {unknown} error
136
143
  */
@@ -138,14 +145,15 @@ export function requested(query, limit) {
138
145
  const promise = Promise.reject(error);
139
146
  promise.catch(noop);
140
147
 
141
- const key = create_remote_key(__.id, payload);
142
- store.set(key, promise);
148
+ get_cache(__, state)[payload] = promise;
149
+ refresh(event, state, __, payload, () => promise);
143
150
  };
144
151
 
145
152
  for (const payload of skipped) {
146
153
  record_failure(
147
154
  payload,
148
- new Error(
155
+ new HttpError(
156
+ 400,
149
157
  `Requested refresh was rejected because it exceeded requested(${__.name}, ${limit}) limit`
150
158
  )
151
159
  );
@@ -2,10 +2,7 @@
2
2
  /** @import { ServerHooks, MaybePromise, RequestState, RemoteInternals, RequestStore, RemoteLiveQueryUserFunctionReturnType } from 'types' */
3
3
  import { parse } from 'devalue';
4
4
  import { error } from '@sveltejs/kit';
5
- import { hydratable } from 'svelte';
6
5
  import { with_request_store, get_request_store } from '@sveltejs/kit/internal/server';
7
- import { create_remote_key, stringify } from '../../../shared.js';
8
- import { noop } from '../../../../utils/functions.js';
9
6
 
10
7
  /**
11
8
  * @param {any} validate_or_fn
@@ -38,13 +35,11 @@ export function create_validator(validate_or_fn, maybe_fn) {
38
35
 
39
36
  // if the `issues` field exists, the validation failed
40
37
  if (result.issues) {
41
- error(
42
- 400,
43
- await state.handleValidationError({
44
- issues: result.issues,
45
- event
46
- })
47
- );
38
+ const body = await state.handleValidationError({
39
+ issues: result.issues,
40
+ event
41
+ });
42
+ error(body.status ?? 400, body);
48
43
  }
49
44
 
50
45
  return result.value;
@@ -76,24 +71,13 @@ export async function get_response(internals, payload, state, get_result) {
76
71
  await 0;
77
72
 
78
73
  const cache = get_cache(internals, state);
79
- const entry = (cache[payload] ??= {
80
- serialize: false,
81
- data: get_result()
82
- });
83
74
 
84
- entry.serialize ||= !!state.is_in_universal_load;
85
-
86
- if (state.is_in_render && internals.id) {
87
- const remote_key = create_remote_key(internals.id, payload);
88
-
89
- void Promise.resolve(entry.data)
90
- .then((value) => {
91
- void hydratable(remote_key, () => stringify(value, state.transport));
92
- })
93
- .catch(noop);
75
+ if (!state.is_in_remote_query) {
76
+ // if this is a top-level (not nested) `await myQuery()`, include it in the serialized response
77
+ get_implicit_lookup(internals, state)[payload] = get_result;
94
78
  }
95
79
 
96
- return entry.data;
80
+ return (cache[payload] ??= get_result());
97
81
  }
98
82
 
99
83
  /**
@@ -239,10 +223,15 @@ function to_iterator(source, name) {
239
223
  }
240
224
 
241
225
  /**
226
+ * Note that `state` is deliberately not optional: resources that capture the request
227
+ * state at creation must pass it explicitly, because reading it from the request store
228
+ * at call time is only equivalent on runtimes with `AsyncLocalStorage` support.
229
+ * Callers without a captured state (such as the module-level `form` instance getters)
230
+ * should pass `get_request_store().state` themselves.
242
231
  * @param {RemoteInternals} internals
243
232
  * @param {RequestState} state
244
233
  */
245
- export function get_cache(internals, state = get_request_store().state) {
234
+ export function get_cache(internals, state) {
246
235
  let cache = state.remote.data?.get(internals);
247
236
 
248
237
  if (cache === undefined) {
@@ -252,3 +241,18 @@ export function get_cache(internals, state = get_request_store().state) {
252
241
 
253
242
  return cache;
254
243
  }
244
+
245
+ /**
246
+ * @param {RemoteInternals} internals
247
+ * @param {RequestState} state
248
+ */
249
+ export function get_implicit_lookup(internals, state) {
250
+ let cache = state.remote.implicit?.get(internals);
251
+
252
+ if (cache === undefined) {
253
+ cache = {};
254
+ (state.remote.implicit ??= new Map()).set(internals, cache);
255
+ }
256
+
257
+ return cache;
258
+ }
@@ -3,7 +3,6 @@ import {
3
3
  navigating as _navigating,
4
4
  updated as _updated
5
5
  } from '../../client/state.svelte.js';
6
- import { stores } from '../../client/client.js';
7
6
 
8
7
  export const page = {
9
8
  get data() {
@@ -57,5 +56,5 @@ export const updated = {
57
56
  get current() {
58
57
  return _updated.current;
59
58
  },
60
- check: stores.updated.check
59
+ check: _updated.check
61
60
  };
@@ -1,101 +1,38 @@
1
- import { getContext } from 'svelte';
2
- import { BROWSER, DEV } from 'esm-env';
3
- import { stores as browser_stores } from '../client/client.js';
1
+ // TODO: remove this file when enough people have migrated to Kit 3
2
+
3
+ /** @returns {never} */
4
+ function removed() {
5
+ throw new Error(
6
+ '`$app/stores` has been removed in favour of `$app/state`. See https://svelte.dev/docs/kit/migrating-to-sveltekit-2#SvelteKit-2.12:-$app-stores-deprecated'
7
+ );
8
+ }
4
9
 
5
10
  /**
6
- * A function that returns all of the contextual stores. On the server, this must be called during component initialization.
7
- * Only use this if you need to defer store subscription until after the component has mounted, for some reason.
8
- *
9
11
  * @deprecated Use `$app/state` instead (requires Svelte 5, [see docs for more info](https://svelte.dev/docs/kit/migrating-to-sveltekit-2#SvelteKit-2.12:-$app-stores-deprecated))
10
12
  */
11
- export const getStores = () => {
12
- const stores = BROWSER ? browser_stores : getContext('__svelte__');
13
-
14
- return {
15
- /** @type {typeof page} */
16
- page: {
17
- subscribe: stores.page.subscribe
18
- },
19
- /** @type {typeof navigating} */
20
- navigating: {
21
- subscribe: stores.navigating.subscribe
22
- },
23
- /** @type {typeof updated} */
24
- updated: stores.updated
25
- };
26
- };
13
+ export const getStores = removed;
27
14
 
28
15
  /**
29
- * A readable store whose value contains page data.
30
- *
31
- * On the server, this store can only be subscribed to during component initialization. In the browser, it can be subscribed to at any time.
32
- *
33
16
  * @deprecated Use `page` from `$app/state` instead (requires Svelte 5, [see docs for more info](https://svelte.dev/docs/kit/migrating-to-sveltekit-2#SvelteKit-2.12:-$app-stores-deprecated))
34
17
  * @type {import('svelte/store').Readable<import('@sveltejs/kit').Page>}
35
18
  */
36
19
  export const page = {
37
- subscribe(fn) {
38
- const store = DEV ? get_store('page') : getStores().page;
39
- return store.subscribe(fn);
40
- }
20
+ subscribe: removed
41
21
  };
42
22
 
43
23
  /**
44
- * A readable store.
45
- * When navigating starts, its value is a `Navigation` object with `from`, `to`, `type` and (if `type === 'popstate'`) `delta` properties.
46
- * When navigating finishes, its value reverts to `null`.
47
- *
48
- * On the server, this store can only be subscribed to during component initialization. In the browser, it can be subscribed to at any time.
49
- *
50
24
  * @deprecated Use `navigating` from `$app/state` instead (requires Svelte 5, [see docs for more info](https://svelte.dev/docs/kit/migrating-to-sveltekit-2#SvelteKit-2.12:-$app-stores-deprecated))
51
25
  * @type {import('svelte/store').Readable<import('@sveltejs/kit').Navigation | null>}
52
26
  */
53
27
  export const navigating = {
54
- subscribe(fn) {
55
- const store = DEV ? get_store('navigating') : getStores().navigating;
56
- return store.subscribe(fn);
57
- }
28
+ subscribe: removed
58
29
  };
59
30
 
60
31
  /**
61
- * A readable store whose initial value is `false`. If [`version.pollInterval`](https://svelte.dev/docs/kit/configuration#version) is a non-zero value, SvelteKit will poll for new versions of the app and update the store value to `true` when it detects one. `updated.check()` will force an immediate check, regardless of polling.
62
- *
63
- * On the server, this store can only be subscribed to during component initialization. In the browser, it can be subscribed to at any time.
64
- *
65
32
  * @deprecated Use `updated` from `$app/state` instead (requires Svelte 5, [see docs for more info](https://svelte.dev/docs/kit/migrating-to-sveltekit-2#SvelteKit-2.12:-$app-stores-deprecated))
66
33
  * @type {import('svelte/store').Readable<boolean> & { check(): Promise<boolean> }}
67
34
  */
68
35
  export const updated = {
69
- subscribe(fn) {
70
- const store = DEV ? get_store('updated') : getStores().updated;
71
-
72
- if (BROWSER) {
73
- updated.check = store.check;
74
- }
75
-
76
- return store.subscribe(fn);
77
- },
78
- check: () => {
79
- throw new Error(
80
- BROWSER
81
- ? 'Cannot check updated store before subscribing'
82
- : 'Can only check updated store in browser'
83
- );
84
- }
36
+ subscribe: removed,
37
+ check: removed
85
38
  };
86
-
87
- /**
88
- * @template {keyof ReturnType<typeof getStores>} Name
89
- * @param {Name} name
90
- * @returns {ReturnType<typeof getStores>[Name]}
91
- */
92
- function get_store(name) {
93
- try {
94
- return getStores()[name];
95
- } catch {
96
- throw new Error(
97
- `Cannot subscribe to '${name}' store on the server outside of a Svelte component, as it is bound to the current request via component context. This prevents state from leaking between users.` +
98
- 'For more information, see https://svelte.dev/docs/kit/state-management#avoid-shared-state-on-the-server'
99
- );
100
- }
101
- }
@@ -1,6 +1,6 @@
1
1
  /* if `bundleStrategy` is 'single' or 'inline', this file is used as the entry point */
2
2
 
3
- import * as kit from './entry.js';
3
+ import * as kit from './client-entry.js';
4
4
 
5
5
  // @ts-expect-error
6
6
  import * as app from '__sveltekit/manifest';
@@ -0,0 +1,3 @@
1
+ // we expose this as a separate entry point (rather than treating client.js as the entry point)
2
+ // so that everything other than `start`/`load_css` can be treeshaken
3
+ export { start, load_css } from './client.js';