svelte-effect-runtime 4.0.0 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/.dist/chunks/client-DbwVAMWm.js +530 -0
  2. package/.dist/chunks/client-DbwVAMWm.js.map +1 -0
  3. package/.dist/chunks/compiler-Dpyu4Owd.js +412 -0
  4. package/.dist/chunks/compiler-Dpyu4Owd.js.map +1 -0
  5. package/.dist/chunks/descriptors-q909VWyR.js +216 -0
  6. package/.dist/chunks/descriptors-q909VWyR.js.map +1 -0
  7. package/.dist/chunks/diagnostics-DLwwrBWb.js +359 -0
  8. package/.dist/chunks/diagnostics-DLwwrBWb.js.map +1 -0
  9. package/.dist/chunks/dispatcher-CTWINW5I.js +534 -0
  10. package/.dist/chunks/dispatcher-CTWINW5I.js.map +1 -0
  11. package/.dist/chunks/dispatcher-pZPzarul.js +23 -0
  12. package/.dist/chunks/dispatcher-pZPzarul.js.map +1 -0
  13. package/.dist/chunks/{errors-IJjsUwrE.js → errors-qhcbUH6o.js} +56 -44
  14. package/.dist/chunks/errors-qhcbUH6o.js.map +1 -0
  15. package/.dist/chunks/live-BW7xZKjb.js +146 -0
  16. package/.dist/chunks/live-BW7xZKjb.js.map +1 -0
  17. package/.dist/chunks/live-CHK0RrZu.js +10 -0
  18. package/.dist/chunks/live-CHK0RrZu.js.map +1 -0
  19. package/.dist/chunks/{remote-client-CVI4t66c.js → remote-client-mkQAfP1G.js} +29 -23
  20. package/.dist/chunks/remote-client-mkQAfP1G.js.map +1 -0
  21. package/.dist/chunks/{runtime-GBQz32DZ.js → runtime-CPosEhGl.js} +3 -27
  22. package/.dist/chunks/runtime-CPosEhGl.js.map +1 -0
  23. package/.dist/chunks/{server-D1LcDnZE.js → server-yQrNoiCR.js} +15 -84
  24. package/.dist/chunks/server-yQrNoiCR.js.map +1 -0
  25. package/.dist/chunks/source-scan-cSawCvsg.js +1059 -0
  26. package/.dist/chunks/source-scan-cSawCvsg.js.map +1 -0
  27. package/.dist/chunks/{transform-jNUr65K9.js → transform-D7TTlPpK.js} +245 -371
  28. package/.dist/chunks/transform-D7TTlPpK.js.map +1 -0
  29. package/.dist/compiler/markup-identifiers.d.ts +2 -0
  30. package/.dist/compiler/remote-client.d.ts +1 -19
  31. package/.dist/compiler/source-scan.d.ts +11 -108
  32. package/.dist/compiler/sveltekit-remote-bridge.d.ts +3 -0
  33. package/.dist/compiler.d.ts +6 -0
  34. package/.dist/compiler.js +1 -1
  35. package/.dist/detect.d.ts +0 -31
  36. package/.dist/detect.js +0 -39
  37. package/.dist/detect.js.map +1 -1
  38. package/.dist/diagnostics.d.ts +0 -16
  39. package/.dist/dispatcher/deps.d.ts +3 -14
  40. package/.dist/dispatcher/fibers.d.ts +5 -46
  41. package/.dist/dispatcher/index.d.ts +42 -135
  42. package/.dist/dispatcher/scope.d.ts +36 -0
  43. package/.dist/dispatcher/types.d.ts +0 -151
  44. package/.dist/dispatcher.d.ts +2 -1
  45. package/.dist/dispatcher.js +2 -2
  46. package/.dist/errors.d.ts +44 -42
  47. package/.dist/generated/dispatcher.d.ts +6 -51
  48. package/.dist/generators.d.ts +2 -12
  49. package/.dist/internal/descriptors.d.ts +0 -16
  50. package/.dist/internal/generators.js +14 -9
  51. package/.dist/internal/generators.js.map +1 -1
  52. package/.dist/internal/live.d.ts +9 -0
  53. package/.dist/internal/remote-client.js +2 -2
  54. package/.dist/internal/remote-server.js +2 -2
  55. package/.dist/internal/schema.d.ts +0 -54
  56. package/.dist/live.d.ts +9 -26
  57. package/.dist/markup/event-attributes.d.ts +0 -32
  58. package/.dist/markup/promise.d.ts +1 -23
  59. package/.dist/markup/promise.js +2 -18
  60. package/.dist/markup/promise.js.map +1 -1
  61. package/.dist/markup/run.d.ts +1 -10
  62. package/.dist/markup/run.js +2 -11
  63. package/.dist/markup/run.js.map +1 -1
  64. package/.dist/markup/transform/apply.d.ts +5 -4
  65. package/.dist/markup/transform/classify.d.ts +2 -11
  66. package/.dist/markup/transform/constants.d.ts +2 -1
  67. package/.dist/markup/transform/effect-bindings.d.ts +2 -26
  68. package/.dist/markup/transform/effect-callbacks.d.ts +0 -20
  69. package/.dist/markup/transform/emit.d.ts +2 -11
  70. package/.dist/markup/transform/expressions.d.ts +0 -35
  71. package/.dist/markup/transform/scan.d.ts +3 -22
  72. package/.dist/markup/transform/types.d.ts +15 -127
  73. package/.dist/markup/transform.js +1 -1
  74. package/.dist/markup/value.d.ts +1 -18
  75. package/.dist/markup/value.js +2 -19
  76. package/.dist/markup/value.js.map +1 -1
  77. package/.dist/mod.d.ts +3 -8
  78. package/.dist/mod.js +5 -5
  79. package/.dist/mod.js.map +1 -1
  80. package/.dist/remote/cause-codec.d.ts +3 -37
  81. package/.dist/remote/client/command.d.ts +3 -17
  82. package/.dist/remote/client/form-data.d.ts +0 -7
  83. package/.dist/remote/client/form-enhance.d.ts +0 -7
  84. package/.dist/remote/client/form-transport.d.ts +14 -20
  85. package/.dist/remote/client/form.d.ts +4 -24
  86. package/.dist/remote/client/index.d.ts +2 -1
  87. package/.dist/remote/client/prerender.d.ts +6 -0
  88. package/.dist/remote/client/query-result.d.ts +2 -14
  89. package/.dist/remote/client/query.d.ts +8 -48
  90. package/.dist/remote/client/responses.d.ts +3 -27
  91. package/.dist/remote/client/types.d.ts +16 -110
  92. package/.dist/remote/client/utils.d.ts +0 -8
  93. package/.dist/remote/client.d.ts +2 -2
  94. package/.dist/remote/client.js +2 -2
  95. package/.dist/remote/effect.d.ts +6 -0
  96. package/.dist/remote/failures.d.ts +4 -0
  97. package/.dist/remote/query-update.d.ts +2 -0
  98. package/.dist/remote/resource.d.ts +25 -0
  99. package/.dist/remote/server.d.ts +5 -50
  100. package/.dist/remote/server.js +2 -2
  101. package/.dist/remote/shared.d.ts +6 -145
  102. package/.dist/remote/shared.js +6 -127
  103. package/.dist/remote/shared.js.map +1 -1
  104. package/.dist/runtime/transform.js +26 -31
  105. package/.dist/runtime/transform.js.map +1 -1
  106. package/.dist/script-transform/ast.d.ts +0 -46
  107. package/.dist/script-transform/imports.d.ts +3 -39
  108. package/.dist/script-transform/index.d.ts +2 -0
  109. package/.dist/script-transform/lower.d.ts +0 -9
  110. package/.dist/script-transform/runes.d.ts +0 -10
  111. package/.dist/script-transform/runtime-block.d.ts +0 -17
  112. package/.dist/script-transform/source.d.ts +0 -24
  113. package/.dist/script-transform/types.d.ts +9 -126
  114. package/.dist/server/effects.d.ts +4 -53
  115. package/.dist/server/factories.d.ts +7 -7
  116. package/.dist/server/index.d.ts +1 -1
  117. package/.dist/server/invalid.d.ts +0 -7
  118. package/.dist/server/live-snapshot.d.ts +5 -0
  119. package/.dist/server/remote-handler-context.d.ts +5 -0
  120. package/.dist/server/runtime.d.ts +6 -24
  121. package/.dist/server/schema.d.ts +0 -14
  122. package/.dist/server/transport.d.ts +2 -0
  123. package/.dist/server/types.d.ts +49 -26
  124. package/.dist/server/wrappers.d.ts +3 -40
  125. package/.dist/server.d.ts +1 -1
  126. package/.dist/server.js +200 -215
  127. package/.dist/server.js.map +1 -1
  128. package/.dist/yieldable.d.ts +1 -35
  129. package/package.json +2 -2
  130. package/.dist/chunks/client-DcZUV4rR.js +0 -653
  131. package/.dist/chunks/client-DcZUV4rR.js.map +0 -1
  132. package/.dist/chunks/compiler-BBPNyXmQ.js +0 -460
  133. package/.dist/chunks/compiler-BBPNyXmQ.js.map +0 -1
  134. package/.dist/chunks/dispatcher-DOx8iC6r.js +0 -505
  135. package/.dist/chunks/dispatcher-DOx8iC6r.js.map +0 -1
  136. package/.dist/chunks/dispatcher-H-Ia0Os4.js +0 -47
  137. package/.dist/chunks/dispatcher-H-Ia0Os4.js.map +0 -1
  138. package/.dist/chunks/errors-IJjsUwrE.js.map +0 -1
  139. package/.dist/chunks/live-BXAKre_4.js +0 -104
  140. package/.dist/chunks/live-BXAKre_4.js.map +0 -1
  141. package/.dist/chunks/remote-client-CVI4t66c.js.map +0 -1
  142. package/.dist/chunks/runtime-GBQz32DZ.js.map +0 -1
  143. package/.dist/chunks/schema-D9pct6Y5.js +0 -89
  144. package/.dist/chunks/schema-D9pct6Y5.js.map +0 -1
  145. package/.dist/chunks/server-D1LcDnZE.js.map +0 -1
  146. package/.dist/chunks/source-scan-C4PsggID.js +0 -341
  147. package/.dist/chunks/source-scan-C4PsggID.js.map +0 -1
  148. package/.dist/chunks/transform-jNUr65K9.js.map +0 -1
  149. package/.dist/remote/client/effect.d.ts +0 -29
  150. package/.dist/remote/client/failures.d.ts +0 -42
package/.dist/server.js CHANGED
@@ -1,135 +1,108 @@
1
- import { C as UncheckedFormHandlerMissingError, E as UncheckedQueryHandlerMissingError, S as UncheckedCommandHandlerMissingError, T as UncheckedPrerenderHandlerMissingError, i as BatchQueryHandlerMissingError, l as InvalidLiveQueryReturnError, w as UncheckedLiveQueryHandlerMissingError } from "./chunks/errors-IJjsUwrE.js";
2
- import { n as make_remote_live_stream, t as Live } from "./chunks/live-BXAKre_4.js";
3
- import { create_form_error, create_remote_transport_error, create_serialized_remote_failure_envelope } from "./remote/shared.js";
4
- import { n as normalize_validator, r as copy_property_descriptors } from "./chunks/schema-D9pct6Y5.js";
5
- import { i as encode_remote_failure, n as run_remote_effect, t as normalize_remote_helper_error } from "./chunks/server-D1LcDnZE.js";
6
- import { i as get_server_runtime_or_throw, n as ServerRuntime, t as RequestEvent } from "./chunks/runtime-GBQz32DZ.js";
7
- import { Cause, Effect, Stream } from "effect";
1
+ import { D as UncheckedLiveQueryHandlerMissingError, E as UncheckedFormHandlerMissingError, O as UncheckedPrerenderHandlerMissingError, T as UncheckedCommandHandlerMissingError, i as BatchQueryHandlerMissingError, k as UncheckedQueryHandlerMissingError, l as InvalidLiveQueryReturnError } from "./chunks/errors-qhcbUH6o.js";
2
+ import { create_form_error, create_remote_transport_error } from "./remote/shared.js";
3
+ import { i as make_remote_live_stream, r as make_failed_remote_live_stream, t as Live } from "./chunks/live-BW7xZKjb.js";
4
+ import { a as normalize_validator, c as attach_remote_resource_getters, d as MakeEffectFromPromise, f as MakeEffectFromSync, l as is_remote_resource, n as attach_native_remote_query_update, o as attach_failed_remote_query_resource, r as resolve_native_remote_query_updates, s as attach_failed_remote_resource_getters, t as copy_property_descriptors, u as FailWithRemoteError } from "./chunks/descriptors-q909VWyR.js";
5
+ import { i as throw_remote_cause, n as run_remote_effect, t as normalize_remote_helper_error } from "./chunks/server-yQrNoiCR.js";
6
+ import { i as get_server_runtime_or_throw, n as ServerRuntime, t as RequestEvent } from "./chunks/runtime-CPosEhGl.js";
7
+ import { Effect, Result, Stream } from "effect";
8
8
  import { error, invalid, redirect } from "@sveltejs/kit";
9
+ import { stringify } from "devalue";
9
10
  import { command, form, getRequestEvent, prerender, query } from "$app/server";
10
- //#region src/server/schema.ts
11
- /**
12
- * Checks whether a value is the string sentinel for unchecked remotes.
13
- *
14
- * @since 2.0.0
15
- * @param value - Value to inspect.
16
- * @returns Whether the value is `"unchecked"`.
17
- */
18
- function is_unchecked(value) {
19
- return value === "unchecked";
20
- }
21
- /**
22
- * Checks whether a value is a remote handler function.
23
- *
24
- * @since 2.0.0
25
- * @param value - Value to inspect.
26
- * @returns Whether the value is callable as a handler.
27
- */
28
- function is_handler(value) {
29
- return typeof value === "function";
11
+ import * as SvelteKitInternalServer from "@sveltejs/kit/internal/server";
12
+ //#region src/server/remote-handler-context.ts
13
+ const active_remote_handler_counts = /* @__PURE__ */ new WeakMap();
14
+ /** Tracks ownership per request event so concurrent requests remain isolated. */
15
+ function is_running_remote_effect_handler(event) {
16
+ const request_event = event ?? get_current_request_event();
17
+ if (!request_event) return false;
18
+ return (active_remote_handler_counts.get(request_event) ?? 0) > 0;
19
+ }
20
+ /** Marks one request as handler-owned for the lifetime of the supplied Effect. */
21
+ const RunInsideRemoteEffectHandler = (event, effect) => Effect.acquireUseRelease(AcquireRemoteHandlerOwnership(event), () => effect, () => ReleaseRemoteHandlerOwnership(event));
22
+ const AcquireRemoteHandlerOwnership = (event) => Effect.sync(() => {
23
+ const active_count = active_remote_handler_counts.get(event) ?? 0;
24
+ active_remote_handler_counts.set(event, active_count + 1);
25
+ });
26
+ const ReleaseRemoteHandlerOwnership = (event) => Effect.sync(() => {
27
+ const remaining_count = (active_remote_handler_counts.get(event) ?? 1) - 1;
28
+ if (remaining_count === 0) {
29
+ active_remote_handler_counts.delete(event);
30
+ return;
31
+ }
32
+ active_remote_handler_counts.set(event, remaining_count);
33
+ });
34
+ function get_current_request_event() {
35
+ try {
36
+ const event = getRequestEvent();
37
+ return typeof event === "object" && event !== null ? event : void 0;
38
+ } catch {
39
+ return;
40
+ }
30
41
  }
31
42
  //#endregion
32
43
  //#region src/server/effects.ts
33
- /**
34
- * Checks whether a value is an Effect generator return object.
35
- *
36
- * @since 2.0.0
37
- * @param value - Value to inspect.
38
- * @returns Whether the value should be wrapped with `Effect.gen`.
39
- */
40
44
  function is_generator_result(value) {
41
45
  return typeof value === "object" && value !== null && typeof value.next === "function";
42
46
  }
43
- /**
44
- * Normalizes an Effect-like handler return value into an Effect.
45
- *
46
- * @since 2.0.0
47
- * @param value - Effect or generator return value.
48
- * @returns Normalized Effect.
49
- */
50
47
  function ToEffect(value) {
51
48
  if (is_generator_result(value)) return Effect.gen(() => value);
52
49
  return value;
53
50
  }
54
- /**
55
- * Checks whether a value is an Effect Stream live source.
56
- *
57
- * @since 2.0.0
58
- * @param value - Value to inspect.
59
- * @returns Whether the value is an Effect Stream.
60
- */
61
51
  function is_live_source(value) {
62
52
  return Stream.isStream(value);
63
53
  }
64
- /**
65
- * Runs and normalizes a live query handler result with request services
66
- * available to Effect Streams.
67
- *
68
- * @since 2.0.0
69
- * @param value - Live query source or Effect that resolves to one.
70
- * @param event - SvelteKit request event for this remote call.
71
- * @returns Promise resolving with a source SvelteKit can stream.
72
- */
73
- function run_live_handler_source(value, event) {
74
- if (!is_live_source(value)) throw new InvalidLiveQueryReturnError();
75
- const runtime = get_server_runtime_or_throw();
76
- return run_remote_effect(Effect.provideService(ToLiveSourceEffect(value), RequestEvent, event), runtime, invalid, svelte_remote_error);
54
+ /** Runs a live handler inside the request-local ownership scope. */
55
+ function run_live_handler(handler, event) {
56
+ return run_live_source_effect(RunInsideRemoteEffectHandler(event, Effect.suspend(() => {
57
+ const value = handler();
58
+ if (!is_live_source(value)) return Effect.die(new InvalidLiveQueryReturnError());
59
+ return ToLiveSourceEffect(value, event);
60
+ })), event);
77
61
  }
78
- function ToLiveSourceEffect(value) {
79
- return Stream.toAsyncIterableEffect(value).pipe(Effect.map(wrap_live_source_errors));
62
+ function run_live_source_effect(effect, event) {
63
+ const runtime = get_server_runtime_or_throw();
64
+ return run_remote_effect(Effect.provideService(effect, RequestEvent, event), runtime, invalid, svelte_remote_error);
80
65
  }
81
- function wrap_live_source_errors(source) {
66
+ const ToLiveSourceEffect = (value, event) => Stream.toAsyncIterableEffect(preserve_live_source_cause(value)).pipe(Effect.map((source) => wrap_live_source_errors(source, event)));
67
+ const preserve_live_source_cause = (value) => value.pipe(Stream.catchCause((cause) => Stream.fromEffect(Effect.sync(() => throw_remote_cause(cause, invalid, svelte_remote_error)))));
68
+ function wrap_live_source_errors(source, event) {
82
69
  return { [Symbol.asyncIterator]() {
83
70
  const iterator = source[Symbol.asyncIterator]();
71
+ const throw_iterator = iterator.throw?.bind(iterator);
84
72
  return {
85
- async next() {
86
- try {
87
- return await iterator.next();
88
- } catch (error) {
89
- throw_live_source_error(error);
90
- }
73
+ next() {
74
+ return RunLiveIteratorCall(event, () => iterator.next());
91
75
  },
92
- async return(value) {
93
- if (iterator.return) return await iterator.return(value);
94
- return {
76
+ return(value) {
77
+ if (iterator.return) return RunLiveIteratorCall(event, () => iterator.return?.(value));
78
+ return Promise.resolve({
95
79
  done: true,
96
80
  value: void 0
97
- };
81
+ });
98
82
  },
99
- async throw(error) {
100
- if (iterator.throw) return await iterator.throw(error);
101
- throw_live_source_error(error);
102
- }
83
+ ...throw_iterator === void 0 ? {} : { throw(error) {
84
+ return RunLiveIteratorCall(event, () => throw_iterator(error));
85
+ } }
103
86
  };
104
87
  } };
105
88
  }
106
- function throw_live_source_error(error$2) {
107
- error(500, create_serialized_remote_failure_envelope(encode_remote_failure(Cause.fail(error$2))));
89
+ function RunLiveIteratorCall(event, run) {
90
+ const runtime = get_server_runtime_or_throw();
91
+ const IteratorEffect = Effect.tryPromise({
92
+ try: run,
93
+ catch: (error) => error
94
+ });
95
+ return runtime.runPromise(RunInsideRemoteEffectHandler(event, IteratorEffect));
108
96
  }
109
- /**
110
- * Runs a remote handler Effect with the current request event provided.
111
- *
112
- * @since 2.0.0
113
- * @param value - Handler return value.
114
- * @param event - SvelteKit request event for this remote call.
115
- * @returns Promise resolving with the handler output.
116
- */
117
97
  function run_handler_effect(value, event) {
118
98
  const runtime = get_server_runtime_or_throw();
119
- return run_remote_effect(Effect.provideService(ToEffect(value), RequestEvent, event), runtime, invalid, svelte_remote_error);
99
+ return run_remote_effect(Effect.provideService(RunInsideRemoteEffectHandler(event, ToEffect(value)), RequestEvent, event), runtime, invalid, svelte_remote_error);
120
100
  }
121
101
  const svelte_remote_error = (status, body) => {
122
102
  error(status, body);
123
103
  };
124
104
  //#endregion
125
105
  //#region src/server/invalid.ts
126
- /**
127
- * Creates a path-aware proxy for remote form validation failures.
128
- *
129
- * @since 2.0.0
130
- * @param path - Current nested form path.
131
- * @returns Callable invalid proxy for this path.
132
- */
133
106
  function make_invalid_proxy(path = []) {
134
107
  const invalid_at_path = (message) => Effect.fail(create_form_error([{
135
108
  message,
@@ -137,37 +110,33 @@ function make_invalid_proxy(path = []) {
137
110
  }]));
138
111
  return new Proxy(invalid_at_path, { get(_target, property) {
139
112
  if (typeof property === "symbol") return;
140
- return make_invalid_proxy([...path, property]);
113
+ const segment = path.length === 0 ? property : normalize_nested_path_segment(property);
114
+ return make_invalid_proxy([...path, segment]);
141
115
  } });
142
116
  }
117
+ function normalize_nested_path_segment(property) {
118
+ return /^(0|[1-9]\d*)$/.test(property) ? Number(property) : property;
119
+ }
120
+ //#endregion
121
+ //#region src/server/schema.ts
122
+ function is_unchecked(value) {
123
+ return value === "unchecked";
124
+ }
125
+ function is_handler(value) {
126
+ return typeof value === "function";
127
+ }
143
128
  //#endregion
144
129
  //#region src/server/wrappers.ts
145
- let running_remote_effect_handlers = 0;
146
- /**
147
- * Reports whether SER is currently executing user remote handler code.
148
- *
149
- * @example
150
- * ```ts
151
- * if (is_running_remote_effect_handler()) {
152
- * return query();
153
- * }
154
- * ```
155
- *
156
- * @since 2.0.0
157
- * @returns Whether a remote handler wrapper is currently active.
158
- * @internal
159
- */
160
- function is_running_remote_effect_handler() {
161
- return running_remote_effect_handlers > 0;
130
+ function make_prerender_inputs_wrapper(generate_inputs) {
131
+ return () => {
132
+ const runtime = get_server_runtime_or_throw();
133
+ const CollectInputs = Effect.gen(function* () {
134
+ const inputs = yield* Effect.suspend(() => ToEffect(generate_inputs()));
135
+ return Array.from(inputs);
136
+ });
137
+ return runtime.runPromise(CollectInputs);
138
+ };
162
139
  }
163
- /**
164
- * Builds the wrapper passed to native query, command, and prerender helpers.
165
- *
166
- * @since 2.0.0
167
- * @param handler - Effect handler or already-built Effect-like value.
168
- * @param helper_name - Remote helper name for error normalization.
169
- * @returns Native SvelteKit handler wrapper.
170
- */
171
140
  function make_remote_wrapper(handler, helper_name) {
172
141
  return async (input) => {
173
142
  let event;
@@ -176,23 +145,11 @@ function make_remote_wrapper(handler, helper_name) {
176
145
  } catch (error) {
177
146
  throw normalize_remote_helper_error(error, helper_name);
178
147
  }
179
- return await run_inside_remote_effect_handler(() => {
180
- try {
181
- return run_handler_effect(is_handler(handler) ? handler(input) : handler, event);
182
- } catch (error) {
183
- throw normalize_remote_helper_error(error, helper_name);
184
- }
185
- });
148
+ return await run_handler_effect(Effect.suspend(() => {
149
+ return ToEffect(is_handler(handler) ? handler(input) : handler);
150
+ }), event);
186
151
  };
187
152
  }
188
- /**
189
- * Builds the wrapper passed to native live query helpers.
190
- *
191
- * @since 2.0.0
192
- * @param handler - Effect-aware live source handler.
193
- * @param helper_name - Remote helper name for error normalization.
194
- * @returns Native SvelteKit live query wrapper.
195
- */
196
153
  function make_remote_live_wrapper(handler, helper_name) {
197
154
  return async (input) => {
198
155
  let event;
@@ -201,23 +158,9 @@ function make_remote_live_wrapper(handler, helper_name) {
201
158
  } catch (error) {
202
159
  throw normalize_remote_helper_error(error, helper_name);
203
160
  }
204
- return await run_inside_remote_effect_handler(() => {
205
- try {
206
- return run_live_handler_source(typeof handler === "function" ? handler(input) : handler, event);
207
- } catch (error) {
208
- throw normalize_remote_helper_error(error, helper_name);
209
- }
210
- });
161
+ return await run_live_handler(() => typeof handler === "function" ? handler(input) : handler, event);
211
162
  };
212
163
  }
213
- /**
214
- * Builds the wrapper passed to native form helpers.
215
- *
216
- * @since 2.0.0
217
- * @param handler - Effect-aware form handler.
218
- * @param helper_name - Remote helper name for error normalization.
219
- * @returns Native SvelteKit form handler wrapper.
220
- */
221
164
  function make_remote_form_wrapper(handler, helper_name) {
222
165
  return async (data, issue) => {
223
166
  let event;
@@ -226,50 +169,102 @@ function make_remote_form_wrapper(handler, helper_name) {
226
169
  } catch (error) {
227
170
  throw normalize_remote_helper_error(error, helper_name);
228
171
  }
229
- return await run_inside_remote_effect_handler(() => {
230
- const invalid_proxy = make_invalid_proxy();
231
- try {
232
- return run_handler_effect(handler({
233
- data,
234
- invalid: invalid_proxy,
235
- issue
236
- }), event);
237
- } catch (error) {
238
- throw normalize_remote_helper_error(error, helper_name);
239
- }
240
- });
172
+ return await run_handler_effect(Effect.suspend(() => {
173
+ return ToEffect(handler({
174
+ data,
175
+ invalid: make_invalid_proxy(),
176
+ issue
177
+ }));
178
+ }), event);
241
179
  };
242
180
  }
243
- async function run_inside_remote_effect_handler(run) {
244
- running_remote_effect_handlers += 1;
245
- try {
246
- return await run();
247
- } finally {
248
- running_remote_effect_handlers -= 1;
249
- }
181
+ //#endregion
182
+ //#region src/server/live-snapshot.ts
183
+ /** Converts SvelteKit transport hooks into a devalue live snapshot encoder. */
184
+ function make_remote_live_snapshot_encoder(transport) {
185
+ const encoders = Object.fromEntries(Object.entries(transport).map(([key, transformer]) => [key, transformer.encode]));
186
+ return (value) => stringify(value, encoders);
187
+ }
188
+ //#endregion
189
+ //#region src/server/transport.ts
190
+ /** Creates the current SvelteKit request's transport-aware live snapshot encoder. */
191
+ function get_remote_live_snapshot_encoder() {
192
+ const try_get_request_store = SvelteKitInternalServer.try_get_request_store;
193
+ const request_store = try_get_request_store?.();
194
+ if (!request_store) return;
195
+ return make_remote_live_snapshot_encoder(request_store.state.transport);
250
196
  }
251
197
  //#endregion
252
198
  //#region src/server/factories.ts
253
199
  const request_event_context_error_start = "Can only read the current request event inside functions invoked during `handle`";
254
200
  const request_store_context_error = "Could not get the request store.";
255
- function to_effect_query(native) {
201
+ function to_effect_query(native, mode = "standard") {
202
+ return to_effect_remote_resource(native, attach_query_resource_methods, attach_failed_remote_query_resource, mode);
203
+ }
204
+ function to_effect_prerender(native) {
205
+ return to_effect_remote_resource(native, attach_remote_resource_getters, attach_failed_remote_resource_getters);
206
+ }
207
+ function to_effect_command(native) {
256
208
  const wrapped = ((input) => {
257
209
  if (is_current_remote_request()) return native(input);
258
- const resource = native(input);
259
- const effect = Effect.tryPromise({
260
- try: () => Promise.resolve(resource),
261
- catch: (error) => error
210
+ return MakeServerCommandEffect(native, input);
211
+ });
212
+ copy_property_descriptors(native, wrapped);
213
+ return wrapped;
214
+ }
215
+ const MakeServerCommandEffect = (native, input) => {
216
+ let updates_args;
217
+ const CommandEffect = Effect.gen(function* () {
218
+ const invocation = yield* MakeEffectFromSync(() => {
219
+ const result = native(input);
220
+ if (updates_args && has_command_updates(result)) return result.updates(...resolve_native_remote_query_updates(updates_args));
221
+ return result;
262
222
  });
263
- attach_query_resource_methods(resource, effect);
264
- return effect;
223
+ return yield* MakeEffectFromPromise(() => Promise.resolve(invocation));
224
+ });
225
+ Object.defineProperty(CommandEffect, "updates", {
226
+ configurable: true,
227
+ enumerable: false,
228
+ value: (...args) => {
229
+ updates_args ??= args;
230
+ return CommandEffect;
231
+ }
232
+ });
233
+ return CommandEffect;
234
+ };
235
+ function has_command_updates(value) {
236
+ const value_type = typeof value;
237
+ return (value_type === "object" && value !== null || value_type === "function") && typeof value.updates === "function";
238
+ }
239
+ function to_effect_remote_resource(native, attach_resource, attach_failed_resource, mode = "standard") {
240
+ const wrapped = ((input) => {
241
+ if (is_current_remote_request()) return native(input);
242
+ const resource_attempt = Result.try(() => native(input));
243
+ if (Result.isFailure(resource_attempt)) {
244
+ const ResourceEffect = FailWithRemoteError(resource_attempt.failure);
245
+ attach_failed_resource(resource_attempt.failure, ResourceEffect);
246
+ return ResourceEffect;
247
+ }
248
+ const resource = resource_attempt.success;
249
+ const started_result = mode === "batch" ? begin_batch_resource(resource) : void 0;
250
+ const ResourceEffect = MakeEffectFromPromise(() => started_result ?? Promise.resolve(resource));
251
+ attach_native_remote_query_update(ResourceEffect, resource);
252
+ attach_resource(resource, ResourceEffect);
253
+ return ResourceEffect;
265
254
  });
266
255
  copy_property_descriptors(native, wrapped);
256
+ attach_native_remote_query_update(wrapped, native);
267
257
  return wrapped;
268
258
  }
259
+ function begin_batch_resource(resource) {
260
+ const result = Promise.resolve(resource);
261
+ result.catch(() => {});
262
+ return result;
263
+ }
269
264
  function is_current_remote_request() {
270
- if (is_running_remote_effect_handler()) return false;
271
265
  const detection = detect_current_remote_request();
272
266
  if (detection._tag === "NoCurrentRemoteRequest") return false;
267
+ if (is_running_remote_effect_handler(detection.event)) return false;
273
268
  return detection.event.isRemoteRequest === true;
274
269
  }
275
270
  function detect_current_remote_request() {
@@ -289,34 +284,20 @@ function is_request_event_context_error(error) {
289
284
  }
290
285
  function to_effect_live_query(native) {
291
286
  const wrapped = ((input) => {
292
- return make_remote_live_stream(native(input), create_remote_transport_error);
287
+ const snapshot_encoder = get_remote_live_snapshot_encoder();
288
+ const resource_attempt = Result.try(() => native(input));
289
+ if (Result.isFailure(resource_attempt)) return make_failed_remote_live_stream(resource_attempt.failure, create_remote_transport_error);
290
+ const resource = resource_attempt.success;
291
+ const stream = make_remote_live_stream(resource, create_remote_transport_error, snapshot_encoder);
292
+ attach_native_remote_query_update(stream, resource);
293
+ return stream;
293
294
  });
294
295
  copy_property_descriptors(native, wrapped);
296
+ attach_native_remote_query_update(wrapped, native);
295
297
  return wrapped;
296
298
  }
297
- function is_query_resource(resource) {
298
- const resource_type = typeof resource;
299
- return resource_type === "object" && resource !== null || resource_type === "function";
300
- }
301
- function attach_remote_resource_getters(resource, effect) {
302
- const methods = is_query_resource(resource) ? resource : void 0;
303
- const keys = [
304
- "current",
305
- "error",
306
- "loading",
307
- "ready"
308
- ];
309
- if (!methods) return;
310
- for (const key of keys) {
311
- if (!(key in methods)) continue;
312
- Object.defineProperty(effect, key, {
313
- configurable: true,
314
- get: () => methods[key]
315
- });
316
- }
317
- }
318
299
  function attach_query_resource_methods(resource, effect) {
319
- const methods = is_query_resource(resource) ? resource : void 0;
300
+ const methods = is_remote_resource(resource) ? resource : void 0;
320
301
  const refresh = methods?.refresh;
321
302
  const set = methods?.set;
322
303
  const with_override = methods?.withOverride;
@@ -324,10 +305,7 @@ function attach_query_resource_methods(resource, effect) {
324
305
  if (!methods) return;
325
306
  if (typeof refresh === "function") Object.defineProperty(effect, "refresh", {
326
307
  configurable: true,
327
- value: () => Effect.tryPromise({
328
- try: () => Promise.resolve(refresh.call(resource)),
329
- catch: (error) => error
330
- })
308
+ value: () => MakeEffectFromPromise(() => Promise.resolve(refresh.call(resource)))
331
309
  });
332
310
  if (typeof set === "function") Object.defineProperty(effect, "set", {
333
311
  configurable: true,
@@ -338,6 +316,13 @@ function attach_query_resource_methods(resource, effect) {
338
316
  value: (update) => with_override.call(resource, update)
339
317
  });
340
318
  }
319
+ function normalize_prerender_options(options) {
320
+ if (!options) return;
321
+ return {
322
+ dynamic: options.dynamic,
323
+ inputs: options.inputs ? make_prerender_inputs_wrapper(options.inputs) : void 0
324
+ };
325
+ }
341
326
  function QueryRoot(validate_or_handler, maybe_handler) {
342
327
  try {
343
328
  if (maybe_handler) return to_effect_query(query(normalize_validator(validate_or_handler), make_remote_wrapper(maybe_handler, "Query")));
@@ -350,7 +335,7 @@ function QueryRoot(validate_or_handler, maybe_handler) {
350
335
  function QueryBatch(validate_or_handler, maybe_handler) {
351
336
  try {
352
337
  if (!maybe_handler) throw new BatchQueryHandlerMissingError();
353
- return to_effect_query(query.batch(normalize_validator(validate_or_handler), make_remote_wrapper(maybe_handler, "Query.batch")));
338
+ return to_effect_query(query.batch(normalize_validator(validate_or_handler), make_remote_wrapper(maybe_handler, "Query.batch")), "batch");
354
339
  } catch (error) {
355
340
  throw normalize_remote_helper_error(error, "Query.batch");
356
341
  }
@@ -386,9 +371,9 @@ const Query = Object.assign(QueryRoot, {
386
371
  });
387
372
  function Command(validate_or_handler, maybe_handler) {
388
373
  try {
389
- if (maybe_handler) return command(normalize_validator(validate_or_handler), make_remote_wrapper(maybe_handler, "Command"));
374
+ if (maybe_handler) return to_effect_command(command(normalize_validator(validate_or_handler), make_remote_wrapper(maybe_handler, "Command")));
390
375
  if (is_unchecked(validate_or_handler)) throw new UncheckedCommandHandlerMissingError();
391
- return command(make_remote_wrapper(validate_or_handler, "Command"));
376
+ return to_effect_command(command(make_remote_wrapper(validate_or_handler, "Command")));
392
377
  } catch (error) {
393
378
  throw normalize_remote_helper_error(error, "Command");
394
379
  }
@@ -410,11 +395,11 @@ function Form(validate_or_handler, maybe_handler) {
410
395
  throw normalize_remote_helper_error(error, "Form");
411
396
  }
412
397
  }
413
- function Prerender(validate_or_handler, maybe_handler_or_options, maybe_options) {
398
+ function Prerender(validate_or_handler, maybe_handler_or_options, maybe_options, native_factory = prerender) {
414
399
  try {
415
- if (is_handler(maybe_handler_or_options)) return prerender(normalize_validator(validate_or_handler), make_remote_wrapper(maybe_handler_or_options, "Prerender"), maybe_options);
400
+ if (is_handler(maybe_handler_or_options)) return to_effect_prerender(native_factory(normalize_validator(validate_or_handler), make_remote_wrapper(maybe_handler_or_options, "Prerender"), normalize_prerender_options(maybe_options)));
416
401
  if (is_unchecked(validate_or_handler)) throw new UncheckedPrerenderHandlerMissingError();
417
- return prerender(make_remote_wrapper(validate_or_handler, "Prerender"), maybe_handler_or_options);
402
+ return to_effect_prerender(native_factory(make_remote_wrapper(validate_or_handler, "Prerender"), normalize_prerender_options(maybe_handler_or_options)));
418
403
  } catch (error) {
419
404
  throw normalize_remote_helper_error(error, "Prerender");
420
405
  }
@@ -460,9 +445,9 @@ function Handler(handler) {
460
445
  const native_handler = async (...arguments_) => {
461
446
  const event = getRequestEvent();
462
447
  const runtime = get_server_runtime_or_throw();
463
- const value = handler(...arguments_);
464
- const EffectWithRequestEvent = Effect.provideService(ToEffect(value), RequestEvent, event);
465
- return await runtime.runPromise(EffectWithRequestEvent);
448
+ const HandlerOwnedEffect = RunInsideRemoteEffectHandler(event, Effect.suspend(() => ToEffect(handler(...arguments_))));
449
+ const EffectWithRequestEvent = Effect.provideService(HandlerOwnedEffect, RequestEvent, event);
450
+ return await runtime.runPromise(EffectWithRequestEvent, { signal: event.request.signal });
466
451
  };
467
452
  return native_handler;
468
453
  }