@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,5 +1,5 @@
1
1
  /** @import { RemoteFormInput, RemoteForm, InvalidField } from '@sveltejs/kit' */
2
- /** @import { InternalRemoteFormIssue, MaybePromise, RemoteFormInternals } from 'types' */
2
+ /** @import { InternalRemoteFormIssue, MaybePromise, HasNonOptionalBoolean, RemoteFormInternals } from 'types' */
3
3
  /** @import { StandardSchemaV1 } from '@standard-schema/spec' */
4
4
  import { get_request_store } from '@sveltejs/kit/internal/server';
5
5
  import {
@@ -9,7 +9,7 @@ import {
9
9
  normalize_issue,
10
10
  flatten_issues
11
11
  } from '../../../form-utils.js';
12
- import { get_cache, run_remote_function } from './shared.js';
12
+ import { get_cache, get_implicit_lookup, run_remote_function } from './shared.js';
13
13
  import { ValidationError } from '@sveltejs/kit/internal';
14
14
 
15
15
  /**
@@ -44,7 +44,7 @@ import { ValidationError } from '@sveltejs/kit/internal';
44
44
  * @template {StandardSchemaV1<RemoteFormInput, Record<string, any>>} Schema
45
45
  * @template Output
46
46
  * @overload
47
- * @param {Schema} validate
47
+ * @param {true extends HasNonOptionalBoolean<StandardSchemaV1.InferInput<Schema>> ? 'Error: All booleans in form schemas must be optional (e.g. `v.optional(v.boolean(), false)`) because checkbox inputs do not send a false value when unchecked.' : Schema} validate
48
48
  * @param {(data: StandardSchemaV1.InferOutput<Schema>, issue: InvalidField<StandardSchemaV1.InferInput<Schema>>) => MaybePromise<Output>} fn
49
49
  * @returns {RemoteForm<StandardSchemaV1.InferInput<Schema>, Output>}
50
50
  * @since 2.27
@@ -108,9 +108,6 @@ export function form(validate_or_fn, maybe_fn) {
108
108
  data = validated.value;
109
109
  }
110
110
 
111
- state.remote.refreshes ??= new Map();
112
- state.remote.reconnects ??= new Map();
113
-
114
111
  const issue = create_issues();
115
112
 
116
113
  try {
@@ -133,7 +130,12 @@ export function form(validate_or_fn, maybe_fn) {
133
130
  // We don't need to care about args or deduplicating calls, because uneval results are only relevant in full page reloads
134
131
  // where only one form submission is active at the same time
135
132
  if (!event.isRemoteRequest) {
136
- get_cache(__, state)[''] ??= { serialize: true, data: output };
133
+ const cache = get_cache(__, state);
134
+ cache[''] ??= output;
135
+
136
+ // register under the client-side action id so the output is serialized
137
+ // into the page, allowing the hydrated client to restore `result`/`issues`/`input`
138
+ get_implicit_lookup(__, state)[__.action_id ?? __.id] = () => cache[''];
137
139
  }
138
140
 
139
141
  return output;
@@ -149,28 +151,33 @@ export function form(validate_or_fn, maybe_fn) {
149
151
 
150
152
  Object.defineProperty(instance, 'fields', {
151
153
  get() {
154
+ // the form instance is created once per module and shared across requests,
155
+ // so the current request's state has to be resolved at access time
152
156
  return create_field_proxy(
153
157
  {},
154
- () => get_cache(__)?.['']?.data?.input ?? {},
158
+ () => get_cache(__, get_request_store().state)?.['']?.input ?? {},
155
159
  (path, value) => {
156
- const cache = get_cache(__);
160
+ const cache = get_cache(__, get_request_store().state);
157
161
  const entry = cache[''];
158
162
 
159
- if (entry?.data?.submission) {
163
+ if (entry?.submission) {
160
164
  // don't override a submission
161
165
  return;
162
166
  }
163
167
 
164
168
  if (path.length === 0) {
165
- (cache[''] ??= { serialize: true, data: {} }).data.input = value;
169
+ (cache[''] ??= {}).input = value;
166
170
  return;
167
171
  }
168
172
 
169
- const input = entry?.data?.input ?? {};
173
+ const input = entry?.input ?? {};
170
174
  deep_set(input, path.map(String), value);
171
- (cache[''] ??= { serialize: true, data: {} }).data.input = input;
175
+ (cache[''] ??= {}).input = input;
172
176
  },
173
- () => flatten_issues(get_cache(__)?.['']?.data?.issues ?? [])
177
+ () => flatten_issues(get_cache(__, get_request_store().state)?.['']?.issues ?? []),
178
+ () => ({}),
179
+ () => ({}),
180
+ []
174
181
  );
175
182
  }
176
183
  });
@@ -178,7 +185,7 @@ export function form(validate_or_fn, maybe_fn) {
178
185
  Object.defineProperty(instance, 'result', {
179
186
  get() {
180
187
  try {
181
- return get_cache(__)?.['']?.data?.result;
188
+ return get_cache(__, get_request_store().state)?.['']?.result;
182
189
  } catch {
183
190
  return undefined;
184
191
  }
@@ -190,6 +197,10 @@ export function form(validate_or_fn, maybe_fn) {
190
197
  get: () => 0
191
198
  });
192
199
 
200
+ Object.defineProperty(instance, 'submitted', {
201
+ get: () => false
202
+ });
203
+
193
204
  Object.defineProperty(instance, 'preflight', {
194
205
  // preflight is a noop on the server
195
206
  value: () => instance
@@ -222,6 +233,7 @@ export function form(validate_or_fn, maybe_fn) {
222
233
  if (!instance) {
223
234
  instance = create_instance(key);
224
235
  instance.__.id = `${__.id}/${encodeURIComponent(JSON.stringify(key))}`;
236
+ instance.__.action_id = `${__.id}/${JSON.stringify(key)}`;
225
237
  instance.__.name = __.name;
226
238
 
227
239
  state.remote.forms.set(cache_key, instance);
@@ -1,15 +1,13 @@
1
1
  /** @import { RemoteResource, RemotePrerenderFunction } from '@sveltejs/kit' */
2
2
  /** @import { RemotePrerenderInputsGenerator, RemotePrerenderInternals, MaybePromise } from 'types' */
3
3
  /** @import { StandardSchemaV1 } from '@standard-schema/spec' */
4
- import { error, json } from '@sveltejs/kit';
5
- import { DEV } from 'esm-env';
4
+ import { json, error } from '@sveltejs/kit';
6
5
  import { get_request_store } from '@sveltejs/kit/internal/server';
7
6
  import { stringify, stringify_remote_arg } from '../../../shared.js';
8
7
  import { noop } from '../../../../utils/functions.js';
9
8
  import { app_dir, base } from '$app/paths/internal/server';
10
9
  import {
11
10
  create_validator,
12
- get_cache,
13
11
  get_response,
14
12
  parse_remote_response,
15
13
  run_remote_function
@@ -89,51 +87,46 @@ export function prerender(validate_or_fn, fn_or_options, maybe_options) {
89
87
 
90
88
  /** @type {RemotePrerenderFunction<Input, Output> & { __: RemotePrerenderInternals }} */
91
89
  const wrapper = (arg) => {
90
+ const { event, state } = get_request_store();
91
+ const payload = stringify_remote_arg(arg, state.transport);
92
+
93
+ // `get_response` (as opposed to bare `get_cache`) also registers the call in the
94
+ // implicit lookup, so that the result is inlined into the page payload (`data.p`)
95
+ // and the client doesn't need to fetch it again upon hydration
92
96
  /** @type {Promise<Output> & Partial<RemoteResource<Output>>} */
93
- const promise = (async () => {
94
- const { event, state } = get_request_store();
95
- const payload = stringify_remote_arg(arg, state.transport);
97
+ const promise = get_response(__, payload, state, async () => {
96
98
  const id = __.id;
97
99
  const url = `${base}/${app_dir}/remote/${id}${payload ? `/${payload}` : ''}`;
98
100
 
99
- if (!state.prerendering && !DEV && !event.isRemoteRequest) {
101
+ if (!state.prerendering && !__SVELTEKIT_DEV__ && !event.isRemoteRequest) {
100
102
  try {
101
- return await get_response(__, payload, state, async () => {
102
- const cache = get_cache(__, state);
103
-
104
- // TODO adapters can provide prerendered data more efficiently than
105
- // fetching from the public internet
106
- const promise = (cache[payload] ??= {
107
- serialize: true,
108
- data: fetch(new URL(url, event.url.origin).href).then(async (response) => {
109
- if (!response.ok) {
110
- throw new Error('Prerendered response not found');
111
- }
112
-
113
- const prerendered = await response.json();
114
-
115
- if (prerendered.type === 'error') {
116
- error(prerendered.status, prerendered.error);
117
- }
118
-
119
- return prerendered.result;
120
- })
121
- }).data;
122
-
123
- return parse_remote_response(await promise, state.transport);
124
- });
103
+ // TODO adapters can provide prerendered data more efficiently than
104
+ // fetching from the public internet
105
+ const response = await fetch(new URL(url, event.url.origin).href);
106
+
107
+ if (!response.ok) {
108
+ throw new Error('Prerendered response not found');
109
+ }
110
+
111
+ const prerendered = /** @type {RemoteFunctionResponse} */ await response.json();
112
+
113
+ if (prerendered.type === 'error') {
114
+ error(prerendered.status, prerendered.error);
115
+ }
116
+
117
+ return parse_remote_response(prerendered.data, state.transport)._;
125
118
  } catch {
126
119
  // not available prerendered, fallback to normal function
127
120
  }
128
121
  }
129
122
 
123
+ // during a prerender run, the same function might be invoked while rendering
124
+ // multiple pages — share the result across the entire run
130
125
  if (state.prerendering?.remote_responses.has(url)) {
131
126
  return /** @type {Promise<any>} */ (state.prerendering.remote_responses.get(url));
132
127
  }
133
128
 
134
- const promise = get_response(__, payload, state, () =>
135
- run_remote_function(event, state, false, () => validate(arg), fn)
136
- );
129
+ const promise = run_remote_function(event, state, false, () => validate(arg), fn);
137
130
 
138
131
  if (state.prerendering) {
139
132
  state.prerendering.remote_responses.set(url, promise);
@@ -142,7 +135,7 @@ export function prerender(validate_or_fn, fn_or_options, maybe_options) {
142
135
  const result = await promise;
143
136
 
144
137
  if (state.prerendering) {
145
- const body = { type: 'result', result: stringify(result, state.transport) };
138
+ const body = { type: 'result', data: stringify({ _: result }, state.transport) };
146
139
  state.prerendering.dependencies.set(url, {
147
140
  body: JSON.stringify(body),
148
141
  response: json(body)
@@ -151,7 +144,7 @@ export function prerender(validate_or_fn, fn_or_options, maybe_options) {
151
144
 
152
145
  // TODO this is missing error/loading/current/status
153
146
  return result;
154
- })();
147
+ });
155
148
 
156
149
  promise.catch(noop);
157
150
 
@@ -1,8 +1,8 @@
1
- /** @import { RemoteLiveQuery, RemoteLiveQueryFunction, RemoteQuery, RemoteQueryFunction } from '@sveltejs/kit' */
1
+ /** @import { RemoteLiveQuery, RemoteLiveQueryFunction, RemoteQuery, RemoteQueryFunction, RequestEvent } from '@sveltejs/kit' */
2
2
  /** @import { RemoteInternals, MaybePromise, RequestState, RemoteQueryLiveInternals, RemoteQueryBatchInternals, RemoteQueryInternals, RemoteLiveQueryUserFunctionReturnType } from 'types' */
3
3
  /** @import { StandardSchemaV1 } from '@standard-schema/spec' */
4
4
  import { get_request_store } from '@sveltejs/kit/internal/server';
5
- import { create_remote_key, stringify, stringify_remote_arg } from '../../../shared.js';
5
+ import { create_remote_key, stringify_remote_arg } from '../../../shared.js';
6
6
  import { prerendering } from '$app/env/internal';
7
7
  import {
8
8
  create_validator,
@@ -11,10 +11,9 @@ import {
11
11
  run_remote_function,
12
12
  run_remote_generator
13
13
  } from './shared.js';
14
- import { handle_error_and_jsonify } from '../../../server/utils.js';
15
- import { HttpError, SvelteKitError } from '@sveltejs/kit/internal';
16
14
  import { noop } from '../../../../utils/functions.js';
17
15
  import { SharedIterator } from '../../../../utils/shared-iterator.js';
16
+ import { handle_error_and_jsonify } from '../../../server/errors.js';
18
17
 
19
18
  /**
20
19
  * Creates a remote query. When called from the browser, the function will be invoked on the server via a `fetch` call.
@@ -78,8 +77,14 @@ export function query(validate_or_fn, maybe_fn) {
78
77
  bind(payload, validated_arg) {
79
78
  const { event, state } = get_request_store();
80
79
 
81
- return create_query_resource(__, payload, state, () =>
82
- run_remote_function(event, state, false, () => validated_arg, fn)
80
+ return create_query_resource(__, payload, event, state, () =>
81
+ run_remote_function(
82
+ event,
83
+ { ...state, is_in_remote_query: true },
84
+ false,
85
+ () => validated_arg,
86
+ fn
87
+ )
83
88
  );
84
89
  }
85
90
  };
@@ -95,8 +100,14 @@ export function query(validate_or_fn, maybe_fn) {
95
100
  const { event, state } = get_request_store();
96
101
  const payload = stringify_remote_arg(arg, state.transport);
97
102
 
98
- return create_query_resource(__, payload, state, () =>
99
- run_remote_function(event, state, false, () => validate(arg), fn)
103
+ return create_query_resource(__, payload, event, state, () =>
104
+ run_remote_function(
105
+ event,
106
+ { ...state, is_in_remote_query: true },
107
+ false,
108
+ () => validate(arg),
109
+ fn
110
+ )
100
111
  );
101
112
  };
102
113
 
@@ -152,7 +163,14 @@ function live(validate_or_fn, maybe_fn) {
152
163
  * @param {any} get_input
153
164
  */
154
165
  const run = (event, state, get_input) =>
155
- run_remote_generator(event, state, false, get_input, fn, __.name);
166
+ run_remote_generator(
167
+ event,
168
+ { ...state, is_in_remote_query: true },
169
+ false,
170
+ get_input,
171
+ fn,
172
+ __.name
173
+ );
156
174
 
157
175
  /** @type {RemoteQueryLiveInternals} */
158
176
  const __ = {
@@ -164,7 +182,7 @@ function live(validate_or_fn, maybe_fn) {
164
182
  bind(payload, validated_arg) {
165
183
  const { event, state } = get_request_store();
166
184
 
167
- return create_live_query_resource(__, payload, state, event.request.signal, () =>
185
+ return create_live_query_resource(__, payload, event, state, () =>
168
186
  run(event, state, () => validated_arg)
169
187
  );
170
188
  }
@@ -181,7 +199,7 @@ function live(validate_or_fn, maybe_fn) {
181
199
  const { event, state } = get_request_store();
182
200
  const payload = stringify_remote_arg(arg, state.transport);
183
201
 
184
- return create_live_query_resource(__, payload, state, event.request.signal, () =>
202
+ return create_live_query_resource(__, payload, event, state, () =>
185
203
  run(event, state, () => validate(arg))
186
204
  );
187
205
  };
@@ -273,7 +291,7 @@ function batch(validate_or_fn, maybe_fn) {
273
291
  try {
274
292
  return await run_remote_function(
275
293
  event,
276
- state,
294
+ { ...state, is_in_remote_query: true },
277
295
  false,
278
296
  async () => Promise.all(entries.map((entry) => entry.get_validated())),
279
297
  async (input) => {
@@ -316,7 +334,7 @@ function batch(validate_or_fn, maybe_fn) {
316
334
 
317
335
  return run_remote_function(
318
336
  event,
319
- state,
337
+ { ...state, is_in_remote_query: true },
320
338
  false,
321
339
  async () => Promise.all(args.map(validate)),
322
340
  async (/** @type {any[]} */ input) => {
@@ -326,15 +344,13 @@ function batch(validate_or_fn, maybe_fn) {
326
344
  input.map(async (arg, i) => {
327
345
  try {
328
346
  const data = get_result(arg, i);
329
- return { type: 'result', data: stringify(data, state.transport) };
347
+ return { type: 'result', data };
330
348
  } catch (error) {
349
+ const transformed = await handle_error_and_jsonify(event, state, options, error);
350
+
331
351
  return {
332
352
  type: 'error',
333
- error: await handle_error_and_jsonify(event, state, options, error),
334
- status:
335
- error instanceof HttpError || error instanceof SvelteKitError
336
- ? error.status
337
- : 500
353
+ error: transformed
338
354
  };
339
355
  }
340
356
  })
@@ -343,9 +359,11 @@ function batch(validate_or_fn, maybe_fn) {
343
359
  );
344
360
  },
345
361
  bind(payload, validated_arg) {
346
- const { state } = get_request_store();
362
+ const { event, state } = get_request_store();
347
363
 
348
- return create_query_resource(__, payload, state, () => enqueue(payload, () => validated_arg));
364
+ return create_query_resource(__, payload, event, state, () =>
365
+ enqueue(payload, () => validated_arg)
366
+ );
349
367
  }
350
368
  };
351
369
 
@@ -357,10 +375,10 @@ function batch(validate_or_fn, maybe_fn) {
357
375
  );
358
376
  }
359
377
 
360
- const { state } = get_request_store();
378
+ const { event, state } = get_request_store();
361
379
  const payload = stringify_remote_arg(arg, state.transport);
362
380
 
363
- return create_query_resource(__, payload, state, () =>
381
+ return create_query_resource(__, payload, event, state, () =>
364
382
  // Collect all the calls to the same query in the same macrotask,
365
383
  // then execute them as one backend request.
366
384
  enqueue(payload, () => validate(arg))
@@ -372,14 +390,47 @@ function batch(validate_or_fn, maybe_fn) {
372
390
  return wrapper;
373
391
  }
374
392
 
393
+ /**
394
+ * Include this value in the returned payload...
395
+ * @param {RequestEvent} event
396
+ * @param {RequestState} state
397
+ * @param {RemoteInternals} internals
398
+ * @param {string} payload
399
+ * @param {() => Promise<any>} fn
400
+ */
401
+ export function refresh(event, state, internals, payload, fn) {
402
+ if (!internals.id) {
403
+ // unless this is a non-exported (i.e. private) query...
404
+ return;
405
+ }
406
+
407
+ if (!event.isRemoteRequest) {
408
+ // or this is a no-JS form submission
409
+ return;
410
+ }
411
+
412
+ const key = create_remote_key(internals.id, payload);
413
+
414
+ // `fn` is stored rather than invoked eagerly. The query is run at the end of
415
+ // the command/form (in `collect_remote_data`), so that it observes any state
416
+ // mutations that happen after `refresh()` is called. If the developer re-awaits
417
+ // the query before the command finishes, the cache entry created by that await
418
+ // is reused instead of re-running the query.
419
+ (state.remote.explicit ??= new Map()).set(key, {
420
+ internals,
421
+ fn
422
+ });
423
+ }
424
+
375
425
  /**
376
426
  * @param {RemoteInternals} __
377
427
  * @param {string} payload — the stringified raw argument (i.e. the cache key the client will use)
428
+ * @param {RequestEvent} event
378
429
  * @param {RequestState} state
379
430
  * @param {() => Promise<any>} fn
380
431
  * @returns {RemoteQuery<any>}
381
432
  */
382
- function create_query_resource(__, payload, state, fn) {
433
+ function create_query_resource(__, payload, event, state, fn) {
383
434
  /** @type {Promise<any> | null} */
384
435
  let promise = null;
385
436
 
@@ -391,7 +442,11 @@ function create_query_resource(__, payload, state, fn) {
391
442
  // accessing data properties needs to kick off the work
392
443
  // so that it gets seeded in the hydration cache
393
444
  // and becomes available on the client
394
- void (__.id && state.is_in_render && get_promise());
445
+ if (__.id && state.is_in_render) {
446
+ // swallow rejections so they don't crash the server — the error is
447
+ // serialized into the response and surfaced on the client instead
448
+ get_promise().catch(noop);
449
+ }
395
450
  };
396
451
 
397
452
  return {
@@ -420,20 +475,19 @@ function create_query_resource(__, payload, state, fn) {
420
475
  return false;
421
476
  },
422
477
  refresh() {
423
- const { event } = get_request_store();
424
- if (!event.isRemoteRequest) {
425
- // If the form submission is not a remote request, refreshing the data is
426
- // useless, because it can't be returned to the client.
427
- return Promise.resolve();
428
- }
429
- const refresh_context = get_refresh_context(__, 'refresh', payload);
430
- const is_immediate_refresh = !refresh_context.cache[refresh_context.payload];
431
- const value = is_immediate_refresh ? get_promise() : fn();
432
- return update_refresh_value(refresh_context, value, is_immediate_refresh);
478
+ promise = null;
479
+ delete get_cache(__, state)[payload];
480
+
481
+ refresh(event, state, __, payload, get_promise);
482
+
483
+ return Promise.resolve();
433
484
  },
434
485
  /** @param {any} value */
435
486
  set(value) {
436
- return update_refresh_value(get_refresh_context(__, 'set', payload), value);
487
+ const p = (promise = Promise.resolve(value));
488
+ get_cache(__, state)[payload] = p;
489
+
490
+ refresh(event, state, __, payload, () => p);
437
491
  },
438
492
  /** @type {Promise<any>['then']} */
439
493
  then(onfulfilled, onrejected) {
@@ -451,12 +505,12 @@ function create_query_resource(__, payload, state, fn) {
451
505
  /**
452
506
  * @param {RemoteQueryLiveInternals} __
453
507
  * @param {string} payload — the stringified raw argument (i.e. the cache key the client will use)
508
+ * @param {RequestEvent} event
454
509
  * @param {RequestState} state
455
- * @param {AbortSignal} signal — the request signal; aborts in-flight iteration when the client disconnects
456
510
  * @param {() => AsyncGenerator<any, void, void>} get_generator
457
511
  * @returns {RemoteLiveQuery<any>}
458
512
  */
459
- function create_live_query_resource(__, payload, state, signal, get_generator) {
513
+ function create_live_query_resource(__, payload, event, state, get_generator) {
460
514
  /** @type {Promise<any> | null} */
461
515
  let promise = null;
462
516
 
@@ -472,7 +526,11 @@ function create_live_query_resource(__, payload, state, signal, get_generator) {
472
526
  };
473
527
 
474
528
  const populate_hydratable = () => {
475
- void (__.id && state.is_in_render && get_promise());
529
+ if (__.id && state.is_in_render) {
530
+ // swallow rejections so they don't crash the server — the error is
531
+ // serialized into the response and surfaced on the client instead
532
+ get_promise().catch(noop);
533
+ }
476
534
  };
477
535
 
478
536
  return {
@@ -509,15 +567,11 @@ function create_live_query_resource(__, payload, state, signal, get_generator) {
509
567
  return false;
510
568
  },
511
569
  reconnect() {
512
- const reconnects = state.remote.reconnects;
570
+ promise = null;
571
+ delete get_cache(__, state)[payload];
513
572
 
514
- if (!reconnects) {
515
- throw new Error(
516
- `Cannot call reconnect on query.live '${__.name}' because it is not executed in the context of a command/form remote function`
517
- );
518
- }
573
+ refresh(event, state, __, payload, get_promise);
519
574
 
520
- reconnects.set(create_remote_key(__.id, payload), get_promise());
521
575
  return Promise.resolve();
522
576
  },
523
577
  /** @ts-expect-error This method no longer exists */
@@ -535,7 +589,7 @@ function create_live_query_resource(__, payload, state, signal, get_generator) {
535
589
  const cache = (state.remote.live_iterators ??= new Map());
536
590
  let cached = cache.get(key);
537
591
  if (!cached) {
538
- cached = create_shared_live_iterator(signal, get_generator);
592
+ cached = create_shared_live_iterator(event.request.signal, get_generator);
539
593
  cache.set(key, cached);
540
594
  }
541
595
  return cached.subscribe();
@@ -613,55 +667,3 @@ function create_shared_live_iterator(signal, get_generator) {
613
667
  // Add batch as a property to the query function
614
668
  Object.defineProperty(query, 'batch', { value: batch, enumerable: true });
615
669
  Object.defineProperty(query, 'live', { value: live, enumerable: true });
616
-
617
- /**
618
- * @param {RemoteInternals} __
619
- * @param {'set' | 'refresh'} action
620
- * @param {string} payload — the stringified raw argument (i.e. the cache key the client will use)
621
- * @returns {{ __: RemoteInternals; state: any; refreshes: Map<string, Promise<any>>; cache: Record<string, { serialize: boolean; data: any }>; refreshes_key: string; payload: string }}
622
- */
623
- function get_refresh_context(__, action, payload) {
624
- const { state } = get_request_store();
625
- const { refreshes } = state.remote;
626
-
627
- if (!refreshes) {
628
- const name = __.type === 'query_batch' ? `query.batch '${__.name}'` : `query '${__.name}'`;
629
- throw new Error(
630
- `Cannot call ${action} on ${name} because it is not executed in the context of a command/form remote function`
631
- );
632
- }
633
-
634
- const cache = get_cache(__, state);
635
- const refreshes_key = create_remote_key(__.id, payload);
636
-
637
- return { __, state, refreshes, refreshes_key, cache, payload };
638
- }
639
-
640
- /**
641
- * @param {{ __: RemoteInternals; refreshes: Map<string, Promise<any>>; cache: Record<string, { serialize: boolean; data: any }>; refreshes_key: string; payload: string }} context
642
- * @param {any} value
643
- * @param {boolean} [is_immediate_refresh=false]
644
- * @returns {Promise<void>}
645
- */
646
- function update_refresh_value(
647
- { __, refreshes, refreshes_key, cache, payload },
648
- value,
649
- is_immediate_refresh = false
650
- ) {
651
- const promise = Promise.resolve(value);
652
-
653
- if (!is_immediate_refresh) {
654
- cache[payload] = { serialize: true, data: promise };
655
- }
656
-
657
- if (__.id) {
658
- refreshes.set(refreshes_key, promise);
659
- }
660
-
661
- promise.catch(noop);
662
-
663
- // we return an immediately-resolving promise so that the `refresh()` signature is consistent,
664
- // but it doesn't delay anything if awaited inside a command. this way, people aren't
665
- // penalised if they do `await q1.refresh(); await q2.refresh()`
666
- return Promise.resolve();
667
- }