svelte-effect-runtime 3.4.11 → 4.0.1

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 (147) hide show
  1. package/.dist/chunks/client-BrZka6DW.js +530 -0
  2. package/.dist/chunks/client-BrZka6DW.js.map +1 -0
  3. package/.dist/chunks/compiler-COVzgLYo.js +412 -0
  4. package/.dist/chunks/compiler-COVzgLYo.js.map +1 -0
  5. package/.dist/chunks/descriptors-B4otDlm1.js +216 -0
  6. package/.dist/chunks/descriptors-B4otDlm1.js.map +1 -0
  7. package/.dist/chunks/diagnostics-BDrky-Di.js +359 -0
  8. package/.dist/chunks/diagnostics-BDrky-Di.js.map +1 -0
  9. package/.dist/chunks/dispatcher-BA0PLQhA.js +16 -0
  10. package/.dist/chunks/dispatcher-BA0PLQhA.js.map +1 -0
  11. package/.dist/chunks/{dispatcher-pHH4JcFR.js → dispatcher-_x0MQZBS.js} +84 -258
  12. package/.dist/chunks/dispatcher-_x0MQZBS.js.map +1 -0
  13. package/.dist/chunks/{errors-Dcf0MVbq.js → errors-CLsbGjIa.js} +57 -106
  14. package/.dist/chunks/errors-CLsbGjIa.js.map +1 -0
  15. package/.dist/chunks/live-BundLSoY.js +146 -0
  16. package/.dist/chunks/live-BundLSoY.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-Bj1pZXgq.js → remote-client-mkQAfP1G.js} +30 -24
  20. package/.dist/chunks/remote-client-mkQAfP1G.js.map +1 -0
  21. package/.dist/chunks/{runtime-ekkMQ-wL.js → runtime-HDTlEnzp.js} +3 -27
  22. package/.dist/chunks/runtime-HDTlEnzp.js.map +1 -0
  23. package/.dist/chunks/server-BNrA1I-u.js +144 -0
  24. package/.dist/chunks/server-BNrA1I-u.js.map +1 -0
  25. package/.dist/chunks/source-scan-9pZxnfct.js +1059 -0
  26. package/.dist/chunks/source-scan-9pZxnfct.js.map +1 -0
  27. package/.dist/chunks/{transform-CtQDGS9c.js → transform-BXyYUAez.js} +308 -484
  28. package/.dist/chunks/transform-BXyYUAez.js.map +1 -0
  29. package/.dist/compiler/markup-identifiers.d.ts +2 -0
  30. package/.dist/compiler/remote-client.d.ts +6 -0
  31. package/.dist/compiler/source-scan.d.ts +101 -0
  32. package/.dist/compiler/sveltekit-remote-bridge.d.ts +3 -0
  33. package/.dist/{vite.d.ts → compiler.d.ts} +7 -1
  34. package/.dist/{vite.js → 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 +1 -143
  42. package/.dist/dispatcher/types.d.ts +0 -151
  43. package/.dist/dispatcher.js +1 -1
  44. package/.dist/errors.d.ts +44 -86
  45. package/.dist/generated/dispatcher.d.ts +0 -50
  46. package/.dist/generators.d.ts +3 -12
  47. package/.dist/internal/descriptors.d.ts +0 -16
  48. package/.dist/internal/generators.js +25 -3
  49. package/.dist/internal/generators.js.map +1 -0
  50. package/.dist/internal/live.d.ts +9 -0
  51. package/.dist/internal/remote-client.js +2 -2
  52. package/.dist/internal/remote-server.js +2 -2
  53. package/.dist/internal/schema.d.ts +13 -0
  54. package/.dist/live.d.ts +106 -0
  55. package/.dist/markup/event-attributes.d.ts +5 -28
  56. package/.dist/markup/promise.d.ts +1 -23
  57. package/.dist/markup/promise.js +2 -18
  58. package/.dist/markup/promise.js.map +1 -1
  59. package/.dist/markup/run.d.ts +1 -10
  60. package/.dist/markup/run.js +2 -11
  61. package/.dist/markup/run.js.map +1 -1
  62. package/.dist/markup/transform/apply.d.ts +4 -4
  63. package/.dist/markup/transform/classify.d.ts +2 -11
  64. package/.dist/markup/transform/constants.d.ts +2 -1
  65. package/.dist/markup/transform/effect-bindings.d.ts +2 -26
  66. package/.dist/markup/transform/effect-callbacks.d.ts +0 -20
  67. package/.dist/markup/transform/emit.d.ts +2 -11
  68. package/.dist/markup/transform/expressions.d.ts +0 -35
  69. package/.dist/markup/transform/scan.d.ts +3 -22
  70. package/.dist/markup/transform/types.d.ts +2 -129
  71. package/.dist/markup/transform.js +1 -1
  72. package/.dist/markup/value.d.ts +1 -18
  73. package/.dist/markup/value.js +2 -19
  74. package/.dist/markup/value.js.map +1 -1
  75. package/.dist/mod.d.ts +6 -8
  76. package/.dist/mod.js +5 -4
  77. package/.dist/mod.js.map +1 -1
  78. package/.dist/remote/cause-codec.d.ts +19 -0
  79. package/.dist/remote/client/command.d.ts +3 -17
  80. package/.dist/remote/client/form-data.d.ts +0 -7
  81. package/.dist/remote/client/form-enhance.d.ts +0 -7
  82. package/.dist/remote/client/form-transport.d.ts +15 -18
  83. package/.dist/remote/client/form.d.ts +9 -20
  84. package/.dist/remote/client/index.d.ts +2 -1
  85. package/.dist/remote/client/prerender.d.ts +6 -0
  86. package/.dist/remote/client/query-result.d.ts +2 -14
  87. package/.dist/remote/client/query.d.ts +9 -59
  88. package/.dist/remote/client/responses.d.ts +3 -27
  89. package/.dist/remote/client/types.d.ts +26 -101
  90. package/.dist/remote/client/utils.d.ts +0 -8
  91. package/.dist/remote/client.d.ts +2 -2
  92. package/.dist/remote/client.js +2 -2
  93. package/.dist/remote/effect.d.ts +6 -0
  94. package/.dist/remote/failures.d.ts +4 -0
  95. package/.dist/remote/query-update.d.ts +2 -0
  96. package/.dist/remote/resource.d.ts +25 -0
  97. package/.dist/remote/server.d.ts +5 -60
  98. package/.dist/remote/server.js +2 -179
  99. package/.dist/remote/shared.d.ts +6 -145
  100. package/.dist/remote/shared.js +6 -127
  101. package/.dist/remote/shared.js.map +1 -1
  102. package/.dist/runtime/transform.js +83 -87
  103. package/.dist/runtime/transform.js.map +1 -1
  104. package/.dist/script-transform/ast.d.ts +0 -46
  105. package/.dist/script-transform/imports.d.ts +2 -38
  106. package/.dist/script-transform/lower.d.ts +0 -9
  107. package/.dist/script-transform/runes.d.ts +0 -10
  108. package/.dist/script-transform/runtime-block.d.ts +0 -17
  109. package/.dist/script-transform/source.d.ts +0 -24
  110. package/.dist/script-transform/types.d.ts +9 -126
  111. package/.dist/server/effects.d.ts +8 -57
  112. package/.dist/server/factories.d.ts +7 -7
  113. package/.dist/server/handler.d.ts +37 -0
  114. package/.dist/server/index.d.ts +4 -1
  115. package/.dist/server/invalid.d.ts +0 -7
  116. package/.dist/server/live-snapshot.d.ts +5 -0
  117. package/.dist/server/remote-handler-context.d.ts +5 -0
  118. package/.dist/server/runtime.d.ts +6 -24
  119. package/.dist/server/schema.d.ts +2 -40
  120. package/.dist/server/transport.d.ts +2 -0
  121. package/.dist/server/types.d.ts +88 -71
  122. package/.dist/server/wrappers.d.ts +4 -41
  123. package/.dist/server.d.ts +2 -2
  124. package/.dist/server.js +255 -279
  125. package/.dist/server.js.map +1 -1
  126. package/.dist/yieldable.d.ts +7 -0
  127. package/README.md +1 -1
  128. package/package.json +5 -5
  129. package/.dist/chunks/client-CsVu54pU.js +0 -627
  130. package/.dist/chunks/client-CsVu54pU.js.map +0 -1
  131. package/.dist/chunks/descriptors-cOe9uRJz.js +0 -36
  132. package/.dist/chunks/descriptors-cOe9uRJz.js.map +0 -1
  133. package/.dist/chunks/dispatcher-LtvFKYUp.js +0 -47
  134. package/.dist/chunks/dispatcher-LtvFKYUp.js.map +0 -1
  135. package/.dist/chunks/dispatcher-pHH4JcFR.js.map +0 -1
  136. package/.dist/chunks/errors-Dcf0MVbq.js.map +0 -1
  137. package/.dist/chunks/event-attributes-gHzP2ycp.js +0 -39
  138. package/.dist/chunks/event-attributes-gHzP2ycp.js.map +0 -1
  139. package/.dist/chunks/remote-client-Bj1pZXgq.js.map +0 -1
  140. package/.dist/chunks/runtime-ekkMQ-wL.js.map +0 -1
  141. package/.dist/chunks/transform-CtQDGS9c.js.map +0 -1
  142. package/.dist/chunks/vite-WGlbSnso.js +0 -486
  143. package/.dist/chunks/vite-WGlbSnso.js.map +0 -1
  144. package/.dist/remote/client/effect.d.ts +0 -29
  145. package/.dist/remote/client/failures.d.ts +0 -42
  146. package/.dist/remote/server.js.map +0 -1
  147. package/.dist/vite/remote-client.d.ts +0 -24
package/.dist/server.js CHANGED
@@ -1,145 +1,108 @@
1
- import { C as UncheckedLiveQueryHandlerMissingError, S as UncheckedFormHandlerMissingError, T as UncheckedQueryHandlerMissingError, c as InvalidLiveQueryReturnError, i as BatchQueryHandlerMissingError, w as UncheckedPrerenderHandlerMissingError, x as UncheckedCommandHandlerMissingError } from "./chunks/errors-Dcf0MVbq.js";
2
- import { create_form_error } from "./remote/shared.js";
3
- import { t as copy_property_descriptors } from "./chunks/descriptors-cOe9uRJz.js";
4
- import { normalize_remote_helper_error, run_remote_effect } from "./remote/server.js";
5
- import { i as get_server_runtime_or_throw, n as ServerRuntime, t as RequestEvent } from "./chunks/runtime-ekkMQ-wL.js";
6
- import { Effect, Schema, Stream } from "effect";
1
+ import { D as UncheckedPrerenderHandlerMissingError, E as UncheckedLiveQueryHandlerMissingError, O as UncheckedQueryHandlerMissingError, T as UncheckedFormHandlerMissingError, i as BatchQueryHandlerMissingError, l as InvalidLiveQueryReturnError, w as UncheckedCommandHandlerMissingError } from "./chunks/errors-CLsbGjIa.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-BundLSoY.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-B4otDlm1.js";
5
+ import { i as throw_remote_cause, n as run_remote_effect, t as normalize_remote_helper_error } from "./chunks/server-BNrA1I-u.js";
6
+ import { i as get_server_runtime_or_throw, n as ServerRuntime, t as RequestEvent } from "./chunks/runtime-HDTlEnzp.js";
7
+ import { Effect, Result, Stream } from "effect";
7
8
  import { error, invalid, redirect } from "@sveltejs/kit";
9
+ import { stringify } from "devalue";
8
10
  import { command, form, getRequestEvent, prerender, query } from "$app/server";
9
- //#region src/server/schema.ts
10
- /**
11
- * Checks whether a value is the string sentinel for unchecked remotes.
12
- *
13
- * @since 2.0.0
14
- * @param value - Value to inspect.
15
- * @returns Whether the value is `"unchecked"`.
16
- */
17
- function is_unchecked(value) {
18
- return value === "unchecked";
19
- }
20
- /**
21
- * Checks whether a value is a remote handler function.
22
- *
23
- * @since 2.0.0
24
- * @param value - Value to inspect.
25
- * @returns Whether the value is callable as a handler.
26
- */
27
- function is_handler(value) {
28
- return typeof value === "function";
29
- }
30
- /**
31
- * Checks whether a value implements the Standard Schema contract.
32
- *
33
- * @since 2.0.0
34
- * @param value - Value to inspect.
35
- * @returns Whether the value is a Standard Schema.
36
- */
37
- function is_standard_schema(value) {
38
- return typeof value === "object" && value !== null && "~standard" in value;
39
- }
40
- /**
41
- * Checks whether a value looks like an Effect Schema.
42
- *
43
- * @since 2.0.0
44
- * @param value - Value to inspect.
45
- * @returns Whether the value should be converted to Standard Schema.
46
- */
47
- function is_effect_schema(value) {
48
- return typeof value === "object" && value !== null && "ast" in value && "make" in value;
49
- }
50
- /**
51
- * Converts Effect Schema inputs to Standard Schema for SvelteKit.
52
- *
53
- * @since 2.0.0
54
- * @param value - Schema or handler input.
55
- * @returns Original value or Standard Schema view of an Effect Schema.
56
- */
57
- function normalize_validator(value) {
58
- if (is_standard_schema(value) || !is_effect_schema(value)) return value;
59
- return Schema.toStandardSchemaV1(value);
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
+ }
60
41
  }
61
42
  //#endregion
62
43
  //#region src/server/effects.ts
63
- /**
64
- * Checks whether a value is an Effect generator return object.
65
- *
66
- * @since 2.0.0
67
- * @param value - Value to inspect.
68
- * @returns Whether the value should be wrapped with `Effect.gen`.
69
- */
70
44
  function is_generator_result(value) {
71
45
  return typeof value === "object" && value !== null && typeof value.next === "function";
72
46
  }
73
- /**
74
- * Normalizes an Effect-like handler return value into an Effect.
75
- *
76
- * @since 2.0.0
77
- * @param value - Effect or generator return value.
78
- * @returns Normalized Effect.
79
- */
80
47
  function ToEffect(value) {
81
48
  if (is_generator_result(value)) return Effect.gen(() => value);
82
49
  return value;
83
50
  }
84
- /**
85
- * Checks whether a value is a live query source SvelteKit can consume.
86
- *
87
- * @since 2.0.0
88
- * @param value - Value to inspect.
89
- * @returns Whether the value is an Effect Stream or native iterable source.
90
- */
91
51
  function is_live_source(value) {
92
- if (Stream.isStream(value)) return true;
93
- if (typeof value !== "object" || value === null) return false;
94
- const source = value;
95
- return typeof source.next === "function" || typeof source[Symbol.asyncIterator] === "function" || typeof source[Symbol.iterator] === "function";
52
+ return Stream.isStream(value);
96
53
  }
97
- /**
98
- * Runs and normalizes a live query handler result with request services
99
- * available to Effect Streams.
100
- *
101
- * @since 2.0.0
102
- * @param value - Live query source or Effect that resolves to one.
103
- * @param event - SvelteKit request event for this remote call.
104
- * @returns Promise resolving with a source SvelteKit can stream.
105
- */
106
- function run_live_handler_source(value, event) {
107
- const runtime = get_server_runtime_or_throw();
108
- 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);
109
61
  }
110
- function ToLiveSourceEffect(value) {
111
- if (Stream.isStream(value)) return Stream.toAsyncIterableEffect(value);
112
- if (is_native_live_source(value)) return Effect.succeed(value);
113
- if (!Effect.isEffect(value)) return Effect.fail(new InvalidLiveQueryReturnError());
114
- return Effect.flatMap(value, ToLiveSourceEffect);
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);
65
+ }
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) {
69
+ return { [Symbol.asyncIterator]() {
70
+ const iterator = source[Symbol.asyncIterator]();
71
+ const throw_iterator = iterator.throw?.bind(iterator);
72
+ return {
73
+ next() {
74
+ return RunLiveIteratorCall(event, () => iterator.next());
75
+ },
76
+ return(value) {
77
+ if (iterator.return) return RunLiveIteratorCall(event, () => iterator.return?.(value));
78
+ return Promise.resolve({
79
+ done: true,
80
+ value: void 0
81
+ });
82
+ },
83
+ ...throw_iterator === void 0 ? {} : { throw(error) {
84
+ return RunLiveIteratorCall(event, () => throw_iterator(error));
85
+ } }
86
+ };
87
+ } };
115
88
  }
116
- function is_native_live_source(value) {
117
- return is_live_source(value) && !Stream.isStream(value);
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));
118
96
  }
119
- /**
120
- * Runs a remote handler Effect with the current request event provided.
121
- *
122
- * @since 2.0.0
123
- * @param value - Handler return value.
124
- * @param event - SvelteKit request event for this remote call.
125
- * @returns Promise resolving with the handler output.
126
- */
127
97
  function run_handler_effect(value, event) {
128
98
  const runtime = get_server_runtime_or_throw();
129
- 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);
130
100
  }
131
101
  const svelte_remote_error = (status, body) => {
132
102
  error(status, body);
133
103
  };
134
104
  //#endregion
135
105
  //#region src/server/invalid.ts
136
- /**
137
- * Creates a path-aware proxy for remote form validation failures.
138
- *
139
- * @since 2.0.0
140
- * @param path - Current nested form path.
141
- * @returns Callable invalid proxy for this path.
142
- */
143
106
  function make_invalid_proxy(path = []) {
144
107
  const invalid_at_path = (message) => Effect.fail(create_form_error([{
145
108
  message,
@@ -147,37 +110,33 @@ function make_invalid_proxy(path = []) {
147
110
  }]));
148
111
  return new Proxy(invalid_at_path, { get(_target, property) {
149
112
  if (typeof property === "symbol") return;
150
- 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]);
151
115
  } });
152
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
+ }
153
128
  //#endregion
154
129
  //#region src/server/wrappers.ts
155
- let running_remote_effect_handlers = 0;
156
- /**
157
- * Reports whether SER is currently executing user remote handler code.
158
- *
159
- * @example
160
- * ```ts
161
- * if (is_running_remote_effect_handler()) {
162
- * return query();
163
- * }
164
- * ```
165
- *
166
- * @since 2.0.0
167
- * @returns Whether a remote handler wrapper is currently active.
168
- * @internal
169
- */
170
- function is_running_remote_effect_handler() {
171
- 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
+ };
172
139
  }
173
- /**
174
- * Builds the wrapper passed to native query, command, and prerender helpers.
175
- *
176
- * @since 2.0.0
177
- * @param handler - Effect handler or already-built Effect-like value.
178
- * @param helper_name - Remote helper name for error normalization.
179
- * @returns Native SvelteKit handler wrapper.
180
- */
181
140
  function make_remote_wrapper(handler, helper_name) {
182
141
  return async (input) => {
183
142
  let event;
@@ -186,23 +145,11 @@ function make_remote_wrapper(handler, helper_name) {
186
145
  } catch (error) {
187
146
  throw normalize_remote_helper_error(error, helper_name);
188
147
  }
189
- return await run_inside_remote_effect_handler(() => {
190
- try {
191
- return run_handler_effect(is_handler(handler) ? handler(input) : handler, event);
192
- } catch (error) {
193
- throw normalize_remote_helper_error(error, helper_name);
194
- }
195
- });
148
+ return await run_handler_effect(Effect.suspend(() => {
149
+ return ToEffect(is_handler(handler) ? handler(input) : handler);
150
+ }), event);
196
151
  };
197
152
  }
198
- /**
199
- * Builds the wrapper passed to native live query helpers.
200
- *
201
- * @since 2.0.0
202
- * @param handler - Effect-aware live source handler.
203
- * @param helper_name - Remote helper name for error normalization.
204
- * @returns Native SvelteKit live query wrapper.
205
- */
206
153
  function make_remote_live_wrapper(handler, helper_name) {
207
154
  return async (input) => {
208
155
  let event;
@@ -211,23 +158,9 @@ function make_remote_live_wrapper(handler, helper_name) {
211
158
  } catch (error) {
212
159
  throw normalize_remote_helper_error(error, helper_name);
213
160
  }
214
- return await run_inside_remote_effect_handler(() => {
215
- try {
216
- return run_live_handler_source(typeof handler === "function" ? handler(input) : handler, event);
217
- } catch (error) {
218
- throw normalize_remote_helper_error(error, helper_name);
219
- }
220
- });
161
+ return await run_live_handler(() => typeof handler === "function" ? handler(input) : handler, event);
221
162
  };
222
163
  }
223
- /**
224
- * Builds the wrapper passed to native form helpers.
225
- *
226
- * @since 2.0.0
227
- * @param handler - Effect-aware form handler.
228
- * @param helper_name - Remote helper name for error normalization.
229
- * @returns Native SvelteKit form handler wrapper.
230
- */
231
164
  function make_remote_form_wrapper(handler, helper_name) {
232
165
  return async (data, issue) => {
233
166
  let event;
@@ -236,50 +169,102 @@ function make_remote_form_wrapper(handler, helper_name) {
236
169
  } catch (error) {
237
170
  throw normalize_remote_helper_error(error, helper_name);
238
171
  }
239
- return await run_inside_remote_effect_handler(() => {
240
- const invalid_proxy = make_invalid_proxy();
241
- try {
242
- return run_handler_effect(handler({
243
- data,
244
- invalid: invalid_proxy,
245
- issue
246
- }), event);
247
- } catch (error) {
248
- throw normalize_remote_helper_error(error, helper_name);
249
- }
250
- });
172
+ return await run_handler_effect(Effect.suspend(() => {
173
+ return ToEffect(handler({
174
+ data,
175
+ invalid: make_invalid_proxy(),
176
+ issue
177
+ }));
178
+ }), event);
251
179
  };
252
180
  }
253
- async function run_inside_remote_effect_handler(run) {
254
- running_remote_effect_handlers += 1;
255
- try {
256
- return await run();
257
- } finally {
258
- running_remote_effect_handlers -= 1;
259
- }
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);
260
196
  }
261
197
  //#endregion
262
198
  //#region src/server/factories.ts
263
199
  const request_event_context_error_start = "Can only read the current request event inside functions invoked during `handle`";
264
200
  const request_store_context_error = "Could not get the request store.";
265
- 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) {
266
208
  const wrapped = ((input) => {
267
209
  if (is_current_remote_request()) return native(input);
268
- const resource = native(input);
269
- const effect = Effect.tryPromise({
270
- try: () => Promise.resolve(resource),
271
- 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;
272
222
  });
273
- attach_query_resource_methods(resource, effect);
274
- 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;
275
254
  });
276
255
  copy_property_descriptors(native, wrapped);
256
+ attach_native_remote_query_update(wrapped, native);
277
257
  return wrapped;
278
258
  }
259
+ function begin_batch_resource(resource) {
260
+ const result = Promise.resolve(resource);
261
+ result.catch(() => {});
262
+ return result;
263
+ }
279
264
  function is_current_remote_request() {
280
- if (is_running_remote_effect_handler()) return false;
281
265
  const detection = detect_current_remote_request();
282
266
  if (detection._tag === "NoCurrentRemoteRequest") return false;
267
+ if (is_running_remote_effect_handler(detection.event)) return false;
283
268
  return detection.event.isRemoteRequest === true;
284
269
  }
285
270
  function detect_current_remote_request() {
@@ -299,43 +284,20 @@ function is_request_event_context_error(error) {
299
284
  }
300
285
  function to_effect_live_query(native) {
301
286
  const wrapped = ((input) => {
302
- return Effect.try({
303
- try: () => {
304
- return make_live_resource(native(input));
305
- },
306
- catch: (error) => error
307
- });
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;
308
294
  });
309
295
  copy_property_descriptors(native, wrapped);
296
+ attach_native_remote_query_update(wrapped, native);
310
297
  return wrapped;
311
298
  }
312
- function is_query_resource(resource) {
313
- const resource_type = typeof resource;
314
- return resource_type === "object" && resource !== null || resource_type === "function";
315
- }
316
- function is_live_resource(resource) {
317
- const resource_type = typeof resource;
318
- return resource_type === "object" && resource !== null || resource_type === "function";
319
- }
320
- function attach_remote_resource_getters(resource, effect) {
321
- const methods = is_query_resource(resource) ? resource : void 0;
322
- const keys = [
323
- "current",
324
- "error",
325
- "loading",
326
- "ready"
327
- ];
328
- if (!methods) return;
329
- for (const key of keys) {
330
- if (!(key in methods)) continue;
331
- Object.defineProperty(effect, key, {
332
- configurable: true,
333
- get: () => methods[key]
334
- });
335
- }
336
- }
337
299
  function attach_query_resource_methods(resource, effect) {
338
- const methods = is_query_resource(resource) ? resource : void 0;
300
+ const methods = is_remote_resource(resource) ? resource : void 0;
339
301
  const refresh = methods?.refresh;
340
302
  const set = methods?.set;
341
303
  const with_override = methods?.withOverride;
@@ -343,10 +305,7 @@ function attach_query_resource_methods(resource, effect) {
343
305
  if (!methods) return;
344
306
  if (typeof refresh === "function") Object.defineProperty(effect, "refresh", {
345
307
  configurable: true,
346
- value: () => Effect.tryPromise({
347
- try: () => Promise.resolve(refresh.call(resource)),
348
- catch: (error) => error
349
- })
308
+ value: () => MakeEffectFromPromise(() => Promise.resolve(refresh.call(resource)))
350
309
  });
351
310
  if (typeof set === "function") Object.defineProperty(effect, "set", {
352
311
  configurable: true,
@@ -357,42 +316,12 @@ function attach_query_resource_methods(resource, effect) {
357
316
  value: (update) => with_override.call(resource, update)
358
317
  });
359
318
  }
360
- function attach_live_resource_methods(resource, effect) {
361
- const methods = is_live_resource(resource) ? resource : void 0;
362
- const reconnect = methods?.reconnect;
363
- const async_iterator = methods?.[Symbol.asyncIterator];
364
- const keys = [
365
- "connected",
366
- "current",
367
- "done",
368
- "error",
369
- "loading",
370
- "ready"
371
- ];
372
- if (!methods) return;
373
- for (const key of keys) {
374
- if (!(key in methods)) continue;
375
- Object.defineProperty(effect, key, {
376
- configurable: true,
377
- get: () => methods[key]
378
- });
379
- }
380
- if (typeof reconnect === "function") Object.defineProperty(effect, "reconnect", {
381
- configurable: true,
382
- value: () => Effect.tryPromise({
383
- try: () => Promise.resolve(reconnect.call(resource)),
384
- catch: (error) => error
385
- })
386
- });
387
- if (typeof async_iterator === "function") Object.defineProperty(effect, Symbol.asyncIterator, {
388
- configurable: true,
389
- value: () => async_iterator.call(resource)
390
- });
391
- }
392
- function make_live_resource(resource) {
393
- const live_resource = {};
394
- attach_live_resource_methods(resource, live_resource);
395
- return live_resource;
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
+ };
396
325
  }
397
326
  function QueryRoot(validate_or_handler, maybe_handler) {
398
327
  try {
@@ -406,7 +335,7 @@ function QueryRoot(validate_or_handler, maybe_handler) {
406
335
  function QueryBatch(validate_or_handler, maybe_handler) {
407
336
  try {
408
337
  if (!maybe_handler) throw new BatchQueryHandlerMissingError();
409
- 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");
410
339
  } catch (error) {
411
340
  throw normalize_remote_helper_error(error, "Query.batch");
412
341
  }
@@ -442,9 +371,9 @@ const Query = Object.assign(QueryRoot, {
442
371
  });
443
372
  function Command(validate_or_handler, maybe_handler) {
444
373
  try {
445
- 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")));
446
375
  if (is_unchecked(validate_or_handler)) throw new UncheckedCommandHandlerMissingError();
447
- return command(make_remote_wrapper(validate_or_handler, "Command"));
376
+ return to_effect_command(command(make_remote_wrapper(validate_or_handler, "Command")));
448
377
  } catch (error) {
449
378
  throw normalize_remote_helper_error(error, "Command");
450
379
  }
@@ -466,16 +395,63 @@ function Form(validate_or_handler, maybe_handler) {
466
395
  throw normalize_remote_helper_error(error, "Form");
467
396
  }
468
397
  }
469
- 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) {
470
399
  try {
471
- 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)));
472
401
  if (is_unchecked(validate_or_handler)) throw new UncheckedPrerenderHandlerMissingError();
473
- 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)));
474
403
  } catch (error) {
475
404
  throw normalize_remote_helper_error(error, "Prerender");
476
405
  }
477
406
  }
478
407
  //#endregion
408
+ //#region src/server/handler.ts
409
+ /**
410
+ * Adapts an Effect-producing callback to a native SvelteKit server handler.
411
+ * SvelteKit continues to select the handler by its exported binding name, such
412
+ * as `GET`, `PUT`, or `load`.
413
+ *
414
+ * @example A `+server.ts` request handler:
415
+ * ```ts
416
+ * import { Handler } from "svelte-effect-runtime/server";
417
+ * import type { RequestHandler } from "./$types";
418
+ *
419
+ * export const GET = Handler<RequestHandler>(function* ({ params }) {
420
+ * const post = yield* Posts.get(params.slug);
421
+ *
422
+ * return Response.json(post);
423
+ * });
424
+ * ```
425
+ *
426
+ * @example A `+page.server.ts` load function:
427
+ * ```ts
428
+ * import { Handler } from "svelte-effect-runtime/server";
429
+ * import type { PageServerLoad } from "./$types";
430
+ *
431
+ * export const load = Handler<PageServerLoad>(function* ({ locals }) {
432
+ * const profile = yield* Profiles.get(locals.user.id);
433
+ *
434
+ * return { profile };
435
+ * });
436
+ * ```
437
+ *
438
+ * @since 4.0.0
439
+ * @param handler - Effect-producing callback whose parameters and successful
440
+ * result match the native SvelteKit handler type.
441
+ * @returns A native handler that runs the callback through the configured
442
+ * {@link ServerRuntime} with the current {@link RequestEvent} available.
443
+ */
444
+ function Handler(handler) {
445
+ const native_handler = async (...arguments_) => {
446
+ const event = getRequestEvent();
447
+ const runtime = get_server_runtime_or_throw();
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 });
451
+ };
452
+ return native_handler;
453
+ }
454
+ //#endregion
479
455
  //#region src/server/control-flow.ts
480
456
  const error_status_codes = {
481
457
  ProxyAuthenticationRequired: 407,
@@ -589,6 +565,6 @@ function resolve_redirect_status(status) {
589
565
  return redirect_status_codes[status];
590
566
  }
591
567
  //#endregion
592
- export { Command, Error$1 as Error, Form, Prerender, Query, Redirect, RequestEvent, ServerRuntime, get_server_runtime_or_throw };
568
+ export { Command, Error$1 as Error, Form, Handler, Live, Prerender, Query, Redirect, RequestEvent, ServerRuntime, get_server_runtime_or_throw };
593
569
 
594
570
  //# sourceMappingURL=server.js.map