effect-start 0.18.0 → 0.20.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 (203) hide show
  1. package/README.md +3 -3
  2. package/dist/Development.d.ts +8 -3
  3. package/dist/Development.js +14 -7
  4. package/dist/Effectify.d.ts +212 -0
  5. package/dist/Effectify.js +19 -0
  6. package/dist/FilePathPattern.d.ts +29 -0
  7. package/dist/FilePathPattern.js +86 -0
  8. package/dist/FileRouter.d.ts +39 -41
  9. package/dist/FileRouter.js +104 -158
  10. package/dist/FileRouterCodegen.d.ts +7 -8
  11. package/dist/FileRouterCodegen.js +97 -66
  12. package/dist/PlatformError.d.ts +46 -0
  13. package/dist/PlatformError.js +43 -0
  14. package/dist/PlatformRuntime.d.ts +27 -0
  15. package/dist/PlatformRuntime.js +51 -0
  16. package/dist/Route.d.ts +6 -2
  17. package/dist/Route.js +22 -0
  18. package/dist/RouteBody.d.ts +1 -1
  19. package/dist/RouteHttp.d.ts +1 -1
  20. package/dist/RouteHttp.js +12 -19
  21. package/dist/RouteMount.d.ts +2 -1
  22. package/dist/Start.d.ts +33 -6
  23. package/dist/Start.js +31 -13
  24. package/dist/Unique.d.ts +50 -0
  25. package/dist/Unique.js +187 -0
  26. package/dist/bun/BunHttpServer.js +5 -6
  27. package/dist/bun/BunPlatformHttpServer.d.ts +10 -0
  28. package/dist/bun/BunPlatformHttpServer.js +53 -0
  29. package/dist/bun/BunRoute.d.ts +4 -6
  30. package/dist/bun/BunRoute.js +10 -18
  31. package/dist/bun/BunRuntime.d.ts +2 -1
  32. package/dist/bun/BunRuntime.js +10 -5
  33. package/dist/bun/BunServer.d.ts +33 -0
  34. package/dist/bun/BunServer.js +133 -0
  35. package/dist/bun/BunServerRequest.d.ts +60 -0
  36. package/dist/bun/BunServerRequest.js +252 -0
  37. package/dist/bun/index.d.ts +1 -1
  38. package/dist/bun/index.js +1 -1
  39. package/dist/datastar/actions/fetch.d.ts +30 -0
  40. package/dist/datastar/actions/fetch.js +411 -0
  41. package/dist/datastar/actions/peek.d.ts +1 -0
  42. package/dist/datastar/actions/peek.js +14 -0
  43. package/dist/datastar/actions/setAll.d.ts +1 -0
  44. package/dist/datastar/actions/setAll.js +13 -0
  45. package/dist/datastar/actions/toggleAll.d.ts +1 -0
  46. package/dist/datastar/actions/toggleAll.js +13 -0
  47. package/dist/datastar/attributes/attr.d.ts +1 -0
  48. package/dist/datastar/attributes/attr.js +49 -0
  49. package/dist/datastar/attributes/bind.d.ts +1 -0
  50. package/dist/datastar/attributes/bind.js +183 -0
  51. package/dist/datastar/attributes/class.d.ts +1 -0
  52. package/dist/datastar/attributes/class.js +50 -0
  53. package/dist/datastar/attributes/computed.d.ts +1 -0
  54. package/dist/datastar/attributes/computed.js +27 -0
  55. package/dist/datastar/attributes/effect.d.ts +1 -0
  56. package/dist/datastar/attributes/effect.js +10 -0
  57. package/dist/datastar/attributes/indicator.d.ts +1 -0
  58. package/dist/datastar/attributes/indicator.js +32 -0
  59. package/dist/datastar/attributes/init.d.ts +1 -0
  60. package/dist/datastar/attributes/init.js +27 -0
  61. package/dist/datastar/attributes/jsonSignals.d.ts +1 -0
  62. package/dist/datastar/attributes/jsonSignals.js +31 -0
  63. package/dist/datastar/attributes/on.d.ts +1 -0
  64. package/dist/datastar/attributes/on.js +59 -0
  65. package/dist/datastar/attributes/onIntersect.d.ts +1 -0
  66. package/dist/datastar/attributes/onIntersect.js +54 -0
  67. package/dist/datastar/attributes/onInterval.d.ts +1 -0
  68. package/dist/datastar/attributes/onInterval.js +31 -0
  69. package/dist/datastar/attributes/onSignalPatch.d.ts +1 -0
  70. package/dist/datastar/attributes/onSignalPatch.js +44 -0
  71. package/dist/datastar/attributes/ref.d.ts +1 -0
  72. package/dist/datastar/attributes/ref.js +11 -0
  73. package/dist/datastar/attributes/show.d.ts +1 -0
  74. package/dist/datastar/attributes/show.js +32 -0
  75. package/dist/datastar/attributes/signals.d.ts +1 -0
  76. package/dist/datastar/attributes/signals.js +18 -0
  77. package/dist/datastar/attributes/style.d.ts +1 -0
  78. package/dist/datastar/attributes/style.js +56 -0
  79. package/dist/datastar/attributes/text.d.ts +1 -0
  80. package/dist/datastar/attributes/text.js +27 -0
  81. package/dist/datastar/engine.d.ts +156 -0
  82. package/dist/datastar/engine.js +971 -0
  83. package/dist/datastar/index.d.ts +24 -0
  84. package/dist/datastar/index.js +24 -0
  85. package/dist/datastar/load.d.ts +24 -0
  86. package/dist/datastar/load.js +24 -0
  87. package/dist/datastar/utils.d.ts +51 -0
  88. package/dist/datastar/utils.js +205 -0
  89. package/dist/datastar/watchers/patchElements.d.ts +1 -0
  90. package/dist/datastar/watchers/patchElements.js +420 -0
  91. package/dist/datastar/watchers/patchSignals.d.ts +1 -0
  92. package/dist/datastar/watchers/patchSignals.js +15 -0
  93. package/dist/index.d.ts +1 -0
  94. package/dist/index.js +1 -0
  95. package/dist/node/Effectify.d.ts +209 -0
  96. package/dist/node/Effectify.js +19 -0
  97. package/dist/node/FileSystem.d.ts +3 -5
  98. package/dist/node/FileSystem.js +42 -62
  99. package/dist/node/NodeFileSystem.d.ts +7 -0
  100. package/dist/node/NodeFileSystem.js +420 -0
  101. package/dist/node/NodeUtils.d.ts +2 -0
  102. package/dist/node/NodeUtils.js +20 -0
  103. package/dist/node/PlatformError.d.ts +46 -0
  104. package/dist/node/PlatformError.js +43 -0
  105. package/dist/testing/TestLogger.js +1 -1
  106. package/dist/x/tailwind/plugin.js +1 -1
  107. package/package.json +18 -7
  108. package/src/Development.ts +36 -40
  109. package/src/Effectify.ts +269 -0
  110. package/src/FilePathPattern.ts +115 -0
  111. package/src/FileRouter.ts +178 -255
  112. package/src/FileRouterCodegen.ts +135 -92
  113. package/src/PlatformError.ts +117 -0
  114. package/src/PlatformRuntime.ts +108 -0
  115. package/src/Route.ts +31 -2
  116. package/src/RouteBody.ts +1 -1
  117. package/src/RouteHttp.ts +15 -29
  118. package/src/RouteMount.ts +1 -1
  119. package/src/Start.ts +61 -27
  120. package/src/Unique.ts +232 -0
  121. package/src/bun/BunPlatformHttpServer.ts +88 -0
  122. package/src/bun/BunRoute.ts +14 -24
  123. package/src/bun/BunRuntime.ts +21 -5
  124. package/src/bun/BunServer.ts +228 -0
  125. package/src/bun/index.ts +1 -1
  126. package/src/datastar/README.md +18 -0
  127. package/src/datastar/actions/fetch.ts +609 -0
  128. package/src/datastar/actions/peek.ts +17 -0
  129. package/src/datastar/actions/setAll.ts +20 -0
  130. package/src/datastar/actions/toggleAll.ts +20 -0
  131. package/src/datastar/attributes/attr.ts +50 -0
  132. package/src/datastar/attributes/bind.ts +220 -0
  133. package/src/datastar/attributes/class.ts +57 -0
  134. package/src/datastar/attributes/computed.ts +33 -0
  135. package/src/datastar/attributes/effect.ts +11 -0
  136. package/src/datastar/attributes/indicator.ts +39 -0
  137. package/src/datastar/attributes/init.ts +35 -0
  138. package/src/datastar/attributes/jsonSignals.ts +38 -0
  139. package/src/datastar/attributes/on.ts +71 -0
  140. package/src/datastar/attributes/onIntersect.ts +65 -0
  141. package/src/datastar/attributes/onInterval.ts +39 -0
  142. package/src/datastar/attributes/onSignalPatch.ts +63 -0
  143. package/src/datastar/attributes/ref.ts +12 -0
  144. package/src/datastar/attributes/show.ts +33 -0
  145. package/src/datastar/attributes/signals.ts +22 -0
  146. package/src/datastar/attributes/style.ts +63 -0
  147. package/src/datastar/attributes/text.ts +30 -0
  148. package/src/datastar/engine.ts +1341 -0
  149. package/src/datastar/index.ts +25 -0
  150. package/src/datastar/utils.ts +286 -0
  151. package/src/datastar/watchers/patchElements.ts +554 -0
  152. package/src/datastar/watchers/patchSignals.ts +15 -0
  153. package/src/index.ts +1 -0
  154. package/src/node/{FileSystem.ts → NodeFileSystem.ts} +59 -97
  155. package/src/node/{Utils.ts → NodeUtils.ts} +2 -0
  156. package/src/testing/TestLogger.ts +1 -1
  157. package/src/x/tailwind/plugin.ts +1 -1
  158. package/dist/Random.d.ts +0 -5
  159. package/dist/Random.js +0 -49
  160. package/src/Commander.test.ts +0 -1639
  161. package/src/ContentNegotiation.test.ts +0 -603
  162. package/src/Development.test.ts +0 -119
  163. package/src/Entity.test.ts +0 -592
  164. package/src/FileRouterCodegen.todo.ts +0 -1133
  165. package/src/FileRouterPattern.test.ts +0 -147
  166. package/src/FileRouterPattern.ts +0 -59
  167. package/src/FileRouter_files.test.ts +0 -64
  168. package/src/FileRouter_path.test.ts +0 -145
  169. package/src/FileRouter_tree.test.ts +0 -132
  170. package/src/Http.test.ts +0 -319
  171. package/src/HttpAppExtra.test.ts +0 -103
  172. package/src/HttpUtils.test.ts +0 -85
  173. package/src/PathPattern.test.ts +0 -648
  174. package/src/Random.ts +0 -59
  175. package/src/RouteBody.test.ts +0 -232
  176. package/src/RouteHook.test.ts +0 -40
  177. package/src/RouteHttp.test.ts +0 -2909
  178. package/src/RouteMount.test.ts +0 -481
  179. package/src/RouteSchema.test.ts +0 -427
  180. package/src/RouteSse.test.ts +0 -249
  181. package/src/RouteTree.test.ts +0 -494
  182. package/src/RouteTrie.test.ts +0 -322
  183. package/src/RouterPattern.test.ts +0 -676
  184. package/src/RouterPattern.ts +0 -416
  185. package/src/StartApp.ts +0 -47
  186. package/src/Values.test.ts +0 -263
  187. package/src/bun/BunBundle.test.ts +0 -268
  188. package/src/bun/BunBundle_imports.test.ts +0 -48
  189. package/src/bun/BunHttpServer.test.ts +0 -251
  190. package/src/bun/BunHttpServer.ts +0 -306
  191. package/src/bun/BunImportTrackerPlugin.test.ts +0 -77
  192. package/src/bun/BunRoute.test.ts +0 -162
  193. package/src/bundler/BundleHttp.test.ts +0 -132
  194. package/src/effect/HttpRouter.test.ts +0 -548
  195. package/src/experimental/EncryptedCookies.test.ts +0 -488
  196. package/src/hyper/HyperHtml.test.ts +0 -209
  197. package/src/hyper/HyperRoute.test.tsx +0 -197
  198. package/src/middlewares/BasicAuthMiddleware.test.ts +0 -84
  199. package/src/testing/TestHttpClient.test.ts +0 -83
  200. package/src/testing/TestLogger.test.ts +0 -51
  201. package/src/x/datastar/Datastar.test.ts +0 -266
  202. package/src/x/tailwind/TailwindPlugin.test.ts +0 -333
  203. /package/src/bun/{BunHttpServer_web.ts → BunServerRequest.ts} +0 -0
@@ -0,0 +1,411 @@
1
+ import { DATASTAR_FETCH_EVENT } from "../engine.js";
2
+ import { action } from "../engine.js";
3
+ import { filtered, startPeeking, stopPeeking, } from "../engine.js";
4
+ import { kebab } from "../utils.js";
5
+ const createHttpMethod = (name, method, openWhenHiddenDefault = true) => action({
6
+ name,
7
+ apply: async ({ el, evt, error, cleanups }, url, { selector, headers: userHeaders, contentType = "json", filterSignals: { include = /.*/, exclude = /(^|\.)_/ } = {}, openWhenHidden = openWhenHiddenDefault, payload, requestCancellation = "auto", retry = "auto", retryInterval = 1_000, retryScaler = 2, retryMaxWaitMs = 30_000, retryMaxCount = 10, } = {}) => {
8
+ const controller = requestCancellation instanceof AbortController
9
+ ? requestCancellation
10
+ : new AbortController();
11
+ if (requestCancellation === "auto") {
12
+ cleanups.get(`@${name}`)?.();
13
+ cleanups.set(`@${name}`, async () => {
14
+ controller.abort();
15
+ await Promise.resolve();
16
+ });
17
+ }
18
+ let cleanupFn = null;
19
+ try {
20
+ if (!url?.length) {
21
+ throw error("FetchNoUrlProvided", { action });
22
+ }
23
+ const initialHeaders = {
24
+ Accept: "text/event-stream, text/html, application/json",
25
+ "Datastar-Request": true,
26
+ };
27
+ if (contentType === "json") {
28
+ initialHeaders["Content-Type"] = "application/json";
29
+ }
30
+ const headers = Object.assign({}, initialHeaders, userHeaders);
31
+ const req = {
32
+ method,
33
+ headers,
34
+ openWhenHidden,
35
+ retry,
36
+ retryInterval,
37
+ retryScaler,
38
+ retryMaxWaitMs,
39
+ retryMaxCount,
40
+ signal: controller.signal,
41
+ onopen: async (response) => {
42
+ if (response.status >= 400) {
43
+ dispatchFetch(ERROR, el, { status: response.status.toString() });
44
+ }
45
+ },
46
+ onmessage: (evt) => {
47
+ if (!evt.event.startsWith("datastar"))
48
+ return;
49
+ const type = evt.event;
50
+ const argsRawLines = {};
51
+ for (const line of evt.data.split("\n")) {
52
+ const i = line.indexOf(" ");
53
+ const k = line.slice(0, i);
54
+ const v = line.slice(i + 1);
55
+ (argsRawLines[k] ||= []).push(v);
56
+ }
57
+ const argsRaw = Object.fromEntries(Object.entries(argsRawLines).map(([k, v]) => [k, v.join("\n")]));
58
+ dispatchFetch(type, el, argsRaw);
59
+ },
60
+ onerror: (error) => {
61
+ if (isWrongContent(error)) {
62
+ throw error("FetchExpectedTextEventStream", { url });
63
+ }
64
+ if (error) {
65
+ console.error(error.message);
66
+ dispatchFetch(RETRYING, el, { message: error.message });
67
+ }
68
+ },
69
+ };
70
+ const urlInstance = new URL(url, document.baseURI);
71
+ const queryParams = new URLSearchParams(urlInstance.search);
72
+ if (contentType === "json") {
73
+ startPeeking();
74
+ payload = payload !== undefined
75
+ ? payload
76
+ : filtered({ include, exclude });
77
+ stopPeeking();
78
+ const body = JSON.stringify(payload);
79
+ if (method === "GET") {
80
+ queryParams.set("datastar", body);
81
+ }
82
+ else {
83
+ req.body = body;
84
+ }
85
+ }
86
+ else if (contentType === "form") {
87
+ const formEl = (selector ? document.querySelector(selector) : el.closest("form"));
88
+ if (!formEl) {
89
+ throw error("FetchFormNotFound", { action, selector });
90
+ }
91
+ if (!formEl.noValidate && !formEl.checkValidity()) {
92
+ formEl.reportValidity();
93
+ return;
94
+ }
95
+ const formData = new FormData(formEl);
96
+ let submitter = el;
97
+ if (el === formEl && evt instanceof SubmitEvent) {
98
+ submitter = evt.submitter;
99
+ }
100
+ else {
101
+ const preventDefault = (evt) => evt.preventDefault();
102
+ formEl.addEventListener("submit", preventDefault);
103
+ cleanupFn = () => {
104
+ formEl.removeEventListener("submit", preventDefault);
105
+ };
106
+ }
107
+ if (submitter instanceof HTMLButtonElement) {
108
+ const name = submitter.getAttribute("name");
109
+ if (name)
110
+ formData.append(name, submitter.value);
111
+ }
112
+ const multipart = formEl
113
+ .getAttribute("enctype") === "multipart/form-data";
114
+ if (!multipart) {
115
+ headers["Content-Type"] = "application/x-www-form-urlencoded";
116
+ }
117
+ const formParams = new URLSearchParams(formData);
118
+ if (method === "GET") {
119
+ for (const [key, value] of formParams) {
120
+ queryParams.append(key, value);
121
+ }
122
+ }
123
+ else if (multipart) {
124
+ req.body = formData;
125
+ }
126
+ else {
127
+ req.body = formParams;
128
+ }
129
+ }
130
+ else {
131
+ throw error("FetchInvalidContentType", { action, contentType });
132
+ }
133
+ dispatchFetch(STARTED, el, {});
134
+ urlInstance.search = queryParams.toString();
135
+ try {
136
+ await fetchEventSource(urlInstance.toString(), el, req);
137
+ }
138
+ catch (e) {
139
+ if (!isWrongContent(e)) {
140
+ throw error("FetchFailed", { method, url, error: e.message });
141
+ }
142
+ }
143
+ }
144
+ finally {
145
+ dispatchFetch(FINISHED, el, {});
146
+ cleanupFn?.();
147
+ cleanups.delete(`@${name}`);
148
+ }
149
+ },
150
+ });
151
+ createHttpMethod("get", "GET", false);
152
+ createHttpMethod("patch", "PATCH");
153
+ createHttpMethod("post", "POST");
154
+ createHttpMethod("put", "PUT");
155
+ createHttpMethod("delete", "DELETE");
156
+ export const STARTED = "started";
157
+ export const FINISHED = "finished";
158
+ export const ERROR = "error";
159
+ export const RETRYING = "retrying";
160
+ export const RETRIES_FAILED = "retries-failed";
161
+ const dispatchFetch = (type, el, argsRaw) => document.dispatchEvent(new CustomEvent(DATASTAR_FETCH_EVENT, {
162
+ detail: { type, el, argsRaw },
163
+ }));
164
+ const isWrongContent = (err) => `${err}`.includes("text/event-stream");
165
+ const getBytes = async (stream, onChunk) => {
166
+ const reader = stream.getReader();
167
+ let result = await reader.read();
168
+ while (!result.done) {
169
+ onChunk(result.value);
170
+ result = await reader.read();
171
+ }
172
+ };
173
+ const getLines = (onLine) => {
174
+ let buffer;
175
+ let position;
176
+ let fieldLength;
177
+ let discardTrailingNewline = false;
178
+ return (arr) => {
179
+ if (!buffer) {
180
+ buffer = arr;
181
+ position = 0;
182
+ fieldLength = -1;
183
+ }
184
+ else {
185
+ buffer = concat(buffer, arr);
186
+ }
187
+ const bufLength = buffer.length;
188
+ let lineStart = 0;
189
+ while (position < bufLength) {
190
+ if (discardTrailingNewline) {
191
+ if (buffer[position] === 10)
192
+ lineStart = ++position;
193
+ discardTrailingNewline = false;
194
+ }
195
+ let lineEnd = -1;
196
+ for (; position < bufLength && lineEnd === -1; ++position) {
197
+ switch (buffer[position]) {
198
+ case 58:
199
+ if (fieldLength === -1) {
200
+ fieldLength = position - lineStart;
201
+ }
202
+ break;
203
+ // @ts-expect-error:7029
204
+ // biome-ignore lint/suspicious/noFallthroughSwitchClause: intentional fallthrough for CR to LF
205
+ case 13:
206
+ discardTrailingNewline = true;
207
+ case 10:
208
+ lineEnd = position;
209
+ break;
210
+ }
211
+ }
212
+ if (lineEnd === -1)
213
+ break;
214
+ onLine(buffer.subarray(lineStart, lineEnd), fieldLength);
215
+ lineStart = position;
216
+ fieldLength = -1;
217
+ }
218
+ if (lineStart === bufLength)
219
+ buffer = undefined;
220
+ else if (lineStart) {
221
+ buffer = buffer.subarray(lineStart);
222
+ position -= lineStart;
223
+ }
224
+ };
225
+ };
226
+ const getMessages = (onId, onRetry, onMessage) => {
227
+ let message = newMessage();
228
+ const decoder = new TextDecoder();
229
+ return (line, fieldLength) => {
230
+ if (!line.length) {
231
+ onMessage?.(message);
232
+ message = newMessage();
233
+ }
234
+ else if (fieldLength > 0) {
235
+ const field = decoder.decode(line.subarray(0, fieldLength));
236
+ const valueOffset = fieldLength + (line[fieldLength + 1] === 32 ? 2 : 1);
237
+ const value = decoder.decode(line.subarray(valueOffset));
238
+ switch (field) {
239
+ case "data":
240
+ message.data = message.data ? `${message.data}\n${value}` : value;
241
+ break;
242
+ case "event":
243
+ message.event = value;
244
+ break;
245
+ case "id":
246
+ onId(message.id = value);
247
+ break;
248
+ case "retry": {
249
+ const retry = +value;
250
+ if (!Number.isNaN(retry)) {
251
+ onRetry(message.retry = retry);
252
+ }
253
+ break;
254
+ }
255
+ }
256
+ }
257
+ };
258
+ };
259
+ const concat = (a, b) => {
260
+ const res = new Uint8Array(a.length + b.length);
261
+ res.set(a);
262
+ res.set(b, a.length);
263
+ return res;
264
+ };
265
+ const newMessage = () => ({
266
+ data: "",
267
+ event: "",
268
+ id: "",
269
+ retry: undefined,
270
+ });
271
+ const fetchEventSource = (input, el, { signal: inputSignal, headers: inputHeaders, onopen: inputOnOpen, onmessage, onclose, onerror, openWhenHidden, fetch: inputFetch, retry = "auto", retryInterval = 1_000, retryScaler = 2, retryMaxWaitMs = 30_000, retryMaxCount = 10, responseOverrides, ...rest }) => {
272
+ return new Promise((resolve, reject) => {
273
+ const headers = {
274
+ ...inputHeaders,
275
+ };
276
+ let curRequestController;
277
+ const onVisibilityChange = () => {
278
+ curRequestController.abort();
279
+ if (!document.hidden)
280
+ create();
281
+ };
282
+ if (!openWhenHidden) {
283
+ document.addEventListener("visibilitychange", onVisibilityChange);
284
+ }
285
+ let retryTimer;
286
+ const dispose = () => {
287
+ document.removeEventListener("visibilitychange", onVisibilityChange);
288
+ clearTimeout(retryTimer);
289
+ curRequestController.abort();
290
+ };
291
+ inputSignal?.addEventListener("abort", () => {
292
+ dispose();
293
+ resolve();
294
+ });
295
+ const fetch = inputFetch || window.fetch;
296
+ const onopen = inputOnOpen || (() => { });
297
+ let retries = 0;
298
+ let baseRetryInterval = retryInterval;
299
+ const create = async () => {
300
+ curRequestController = new AbortController();
301
+ const curRequestSignal = curRequestController.signal;
302
+ try {
303
+ const response = await fetch(input, {
304
+ ...rest,
305
+ headers,
306
+ signal: curRequestSignal,
307
+ });
308
+ await onopen(response);
309
+ const dispatchNonSSE = async (dispatchType, response, name, responseOverrides, ...argNames) => {
310
+ const argsRaw = {
311
+ [name]: await response.text(),
312
+ };
313
+ for (const n of argNames) {
314
+ let v = response.headers.get(`datastar-${kebab(n)}`);
315
+ if (responseOverrides) {
316
+ const o = responseOverrides[n];
317
+ if (o)
318
+ v = typeof o === "string" ? o : JSON.stringify(o);
319
+ }
320
+ if (v)
321
+ argsRaw[n] = v;
322
+ }
323
+ dispatchFetch(dispatchType, el, argsRaw);
324
+ dispose();
325
+ resolve();
326
+ };
327
+ const status = response.status;
328
+ const isNoContentStatus = status === 204;
329
+ const isRedirectStatus = status >= 300 && status < 400;
330
+ const isErrorStatus = status >= 400 && status < 600;
331
+ if (status !== 200) {
332
+ onclose?.();
333
+ if (retry !== "never"
334
+ && !isNoContentStatus
335
+ && !isRedirectStatus
336
+ && (retry === "always" || (retry === "error" && isErrorStatus))) {
337
+ clearTimeout(retryTimer);
338
+ retryTimer = setTimeout(create, retryInterval);
339
+ return;
340
+ }
341
+ dispose();
342
+ resolve();
343
+ return;
344
+ }
345
+ retries = 0;
346
+ retryInterval = baseRetryInterval;
347
+ const ct = response.headers.get("Content-Type");
348
+ if (ct?.includes("text/html")) {
349
+ return await dispatchNonSSE("datastar-patch-elements", response, "elements", responseOverrides, "selector", "mode", "namespace", "useViewTransition", "namespace");
350
+ }
351
+ if (ct?.includes("application/json")) {
352
+ return await dispatchNonSSE("datastar-patch-signals", response, "signals", responseOverrides, "onlyIfMissing");
353
+ }
354
+ if (ct?.includes("text/javascript")) {
355
+ const script = document.createElement("script");
356
+ const scriptAttributesHeader = response.headers.get("datastar-script-attributes");
357
+ if (scriptAttributesHeader) {
358
+ for (const [name, value] of Object.entries(JSON.parse(scriptAttributesHeader))) {
359
+ script.setAttribute(name, value);
360
+ }
361
+ }
362
+ script.textContent = await response.text();
363
+ document.head.appendChild(script);
364
+ dispose();
365
+ return;
366
+ }
367
+ await getBytes(response.body, getLines(getMessages((id) => {
368
+ if (id) {
369
+ headers["last-event-id"] = id;
370
+ }
371
+ else {
372
+ delete headers["last-event-id"];
373
+ }
374
+ }, (retry) => {
375
+ baseRetryInterval = retryInterval = retry;
376
+ }, onmessage)));
377
+ onclose?.();
378
+ if (retry === "always" && !isRedirectStatus) {
379
+ clearTimeout(retryTimer);
380
+ retryTimer = setTimeout(create, retryInterval);
381
+ return;
382
+ }
383
+ dispose();
384
+ resolve();
385
+ }
386
+ catch (err) {
387
+ if (!curRequestSignal.aborted) {
388
+ try {
389
+ const interval = onerror?.(err) || retryInterval;
390
+ clearTimeout(retryTimer);
391
+ retryTimer = setTimeout(create, interval);
392
+ retryInterval = Math.min(retryInterval * retryScaler, retryMaxWaitMs);
393
+ if (++retries >= retryMaxCount) {
394
+ dispatchFetch(RETRIES_FAILED, el, {});
395
+ dispose();
396
+ reject("Max retries reached.");
397
+ }
398
+ else {
399
+ console.error(`Datastar failed to reach ${input.toString()} retrying in ${interval}ms.`);
400
+ }
401
+ }
402
+ catch (innerErr) {
403
+ dispose();
404
+ reject(innerErr);
405
+ }
406
+ }
407
+ }
408
+ };
409
+ create();
410
+ });
411
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,14 @@
1
+ import { action } from "../engine.js";
2
+ import { startPeeking, stopPeeking, } from "../engine.js";
3
+ action({
4
+ name: "peek",
5
+ apply(_, fn) {
6
+ startPeeking();
7
+ try {
8
+ return fn();
9
+ }
10
+ finally {
11
+ stopPeeking();
12
+ }
13
+ },
14
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ import { action } from "../engine.js";
2
+ import { filtered, mergePatch, startPeeking, stopPeeking, } from "../engine.js";
3
+ import { updateLeaves } from "../utils.js";
4
+ action({
5
+ name: "setAll",
6
+ apply(_, value, filter) {
7
+ startPeeking();
8
+ const masked = filtered(filter);
9
+ updateLeaves(masked, () => value);
10
+ mergePatch(masked);
11
+ stopPeeking();
12
+ },
13
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ import { action } from "../engine.js";
2
+ import { filtered, mergePatch, startPeeking, stopPeeking, } from "../engine.js";
3
+ import { updateLeaves } from "../utils.js";
4
+ action({
5
+ name: "toggleAll",
6
+ apply(_, filter) {
7
+ startPeeking();
8
+ const masked = filtered(filter);
9
+ updateLeaves(masked, (oldValue) => !oldValue);
10
+ mergePatch(masked);
11
+ stopPeeking();
12
+ },
13
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,49 @@
1
+ import { attribute } from "../engine.js";
2
+ import { effect } from "../engine.js";
3
+ attribute({
4
+ name: "attr",
5
+ requirement: { value: "must" },
6
+ returnsValue: true,
7
+ apply({ el, key, rx }) {
8
+ const syncAttr = (key, val) => {
9
+ if (val === "" || val === true) {
10
+ el.setAttribute(key, "");
11
+ }
12
+ else if (val === false || val == null) {
13
+ el.removeAttribute(key);
14
+ }
15
+ else if (typeof val === "string") {
16
+ el.setAttribute(key, val);
17
+ }
18
+ else {
19
+ el.setAttribute(key, JSON.stringify(val));
20
+ }
21
+ };
22
+ const update = key
23
+ ? () => {
24
+ observer.disconnect();
25
+ const val = rx();
26
+ syncAttr(key, val);
27
+ observer.observe(el, {
28
+ attributeFilter: [key],
29
+ });
30
+ }
31
+ : () => {
32
+ observer.disconnect();
33
+ const obj = rx();
34
+ const attributeFilter = Object.keys(obj);
35
+ for (const key of attributeFilter) {
36
+ syncAttr(key, obj[key]);
37
+ }
38
+ observer.observe(el, {
39
+ attributeFilter,
40
+ });
41
+ };
42
+ const observer = new MutationObserver(update);
43
+ const cleanup = effect(update);
44
+ return () => {
45
+ observer.disconnect();
46
+ cleanup();
47
+ };
48
+ },
49
+ });
@@ -0,0 +1 @@
1
+ export {};