effect-start 0.23.0 → 0.25.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 (356) hide show
  1. package/dist/ChildProcess.js +32 -20
  2. package/dist/Commander.js +377 -293
  3. package/dist/ContentNegotiation.js +424 -318
  4. package/dist/Cookies.js +340 -271
  5. package/dist/Development.js +85 -49
  6. package/dist/Effectify.js +22 -14
  7. package/dist/Entity.js +260 -195
  8. package/dist/Fetch.js +192 -0
  9. package/dist/FilePathPattern.js +88 -77
  10. package/dist/FileRouter.js +192 -136
  11. package/dist/FileRouterCodegen.js +262 -191
  12. package/dist/FileSystem.js +126 -64
  13. package/dist/Http.js +96 -77
  14. package/dist/PathPattern.js +311 -273
  15. package/dist/PlatformError.js +36 -21
  16. package/dist/PlatformRuntime.js +65 -40
  17. package/dist/Route.js +122 -79
  18. package/dist/RouteBody.js +83 -58
  19. package/dist/RouteError.js +46 -25
  20. package/dist/RouteHook.js +58 -34
  21. package/dist/RouteHttp.js +346 -237
  22. package/dist/RouteHttpTracer.js +86 -58
  23. package/dist/RouteMount.js +81 -58
  24. package/dist/RouteSchema.js +253 -170
  25. package/dist/RouteSse.js +87 -72
  26. package/dist/RouteTree.js +99 -73
  27. package/dist/RouteTrie.js +160 -133
  28. package/dist/SchemaExtra.js +87 -62
  29. package/dist/Socket.js +32 -21
  30. package/dist/SqlIntrospect.js +317 -268
  31. package/dist/Start.js +55 -25
  32. package/dist/StartApp.js +3 -21
  33. package/dist/StreamExtra.js +109 -74
  34. package/dist/System.js +37 -21
  35. package/dist/TuplePathPattern.js +64 -58
  36. package/dist/Unique.js +159 -120
  37. package/dist/Values.js +48 -32
  38. package/dist/bun/BunBundle.js +158 -109
  39. package/dist/bun/BunChildProcessSpawner.js +121 -82
  40. package/dist/bun/BunImportTrackerPlugin.js +85 -63
  41. package/dist/bun/BunRoute.js +135 -99
  42. package/dist/bun/BunRuntime.js +39 -29
  43. package/dist/bun/BunServer.js +268 -140
  44. package/dist/bun/BunVirtualFilesPlugin.js +47 -33
  45. package/dist/bun/_BunEnhancedResolve.js +107 -82
  46. package/dist/bun/index.js +5 -5
  47. package/dist/bundler/Bundle.js +82 -38
  48. package/dist/bundler/BundleFiles.js +140 -82
  49. package/dist/bundler/BundleRoute.js +49 -38
  50. package/dist/client/Overlay.js +29 -28
  51. package/dist/client/ScrollState.js +94 -82
  52. package/dist/client/index.js +79 -61
  53. package/dist/console/Console.js +40 -24
  54. package/dist/console/ConsoleErrors.js +189 -178
  55. package/dist/console/ConsoleLogger.js +52 -43
  56. package/dist/console/ConsoleMetrics.js +69 -58
  57. package/dist/console/ConsoleProcess.js +57 -47
  58. package/dist/console/ConsoleStore.js +56 -45
  59. package/dist/console/ConsoleTracer.js +101 -88
  60. package/dist/console/Simulation.js +714 -563
  61. package/dist/console/index.js +3 -3
  62. package/dist/console/routes/tree.js +29 -28
  63. package/dist/datastar/actions/fetch.js +514 -381
  64. package/dist/datastar/actions/peek.js +12 -12
  65. package/dist/datastar/actions/setAll.js +18 -11
  66. package/dist/datastar/actions/toggleAll.js +18 -11
  67. package/dist/datastar/attributes/attr.js +48 -47
  68. package/dist/datastar/attributes/bind.js +186 -167
  69. package/dist/datastar/attributes/class.js +51 -44
  70. package/dist/datastar/attributes/computed.js +23 -24
  71. package/dist/datastar/attributes/effect.js +9 -8
  72. package/dist/datastar/attributes/indicator.js +32 -29
  73. package/dist/datastar/attributes/init.js +26 -25
  74. package/dist/datastar/attributes/jsonSignals.js +32 -29
  75. package/dist/datastar/attributes/on.js +76 -73
  76. package/dist/datastar/attributes/onIntersect.js +51 -51
  77. package/dist/datastar/attributes/onInterval.js +30 -29
  78. package/dist/datastar/attributes/onSignalPatch.js +49 -40
  79. package/dist/datastar/attributes/ref.js +10 -9
  80. package/dist/datastar/attributes/show.js +31 -30
  81. package/dist/datastar/attributes/signals.js +17 -16
  82. package/dist/datastar/attributes/style.js +56 -49
  83. package/dist/datastar/attributes/text.js +28 -25
  84. package/dist/datastar/engine.js +1079 -933
  85. package/dist/datastar/index.js +25 -24
  86. package/dist/datastar/utils.js +203 -155
  87. package/dist/datastar/watchers/patchElements.js +459 -372
  88. package/dist/datastar/watchers/patchSignals.js +13 -13
  89. package/dist/experimental/EncryptedCookies.js +305 -189
  90. package/dist/experimental/index.js +1 -1
  91. package/dist/hyper/Hyper.js +22 -17
  92. package/dist/hyper/HyperHtml.js +138 -123
  93. package/dist/hyper/HyperNode.js +11 -9
  94. package/dist/hyper/HyperRoute.js +41 -28
  95. package/dist/hyper/html.js +27 -27
  96. package/dist/hyper/index.js +5 -5
  97. package/dist/hyper/jsx-runtime.js +11 -5
  98. package/dist/index.js +8 -8
  99. package/dist/node/NodeFileSystem.js +606 -341
  100. package/dist/node/NodeUtils.js +21 -18
  101. package/dist/sql/Sql.js +8 -0
  102. package/dist/sql/bun/index.js +134 -67
  103. package/dist/sql/index.js +1 -0
  104. package/dist/sql/libsql/index.js +156 -0
  105. package/dist/sql/mssql/docker.js +103 -60
  106. package/dist/sql/mssql/index.js +182 -101
  107. package/dist/testing/TestLogger.js +39 -29
  108. package/dist/testing/index.js +2 -2
  109. package/dist/testing/utils.js +45 -23
  110. package/dist/x/cloudflare/CloudflareTunnel.js +61 -28
  111. package/dist/x/cloudflare/index.js +1 -1
  112. package/dist/x/tailscale/TailscaleTunnel.js +86 -60
  113. package/dist/x/tailscale/index.js +1 -1
  114. package/dist/x/tailwind/TailwindPlugin.js +280 -205
  115. package/dist/x/tailwind/compile.js +185 -129
  116. package/dist/x/tailwind/plugin.js +13 -11
  117. package/package.json +1 -1
  118. package/src/Development.ts +3 -1
  119. package/src/Entity.ts +17 -0
  120. package/src/Fetch.ts +271 -0
  121. package/src/SqlIntrospect.ts +64 -70
  122. package/src/Start.ts +24 -22
  123. package/src/StartApp.ts +11 -0
  124. package/src/bun/BunServer.ts +89 -16
  125. package/src/hyper/HyperHtml.ts +0 -1
  126. package/src/sql/bun/index.ts +1 -1
  127. package/src/sql/index.ts +1 -0
  128. package/src/sql/libsql/index.ts +173 -0
  129. package/src/sql/libsql/libsql.d.ts +39 -0
  130. package/src/sql/mssql/index.ts +1 -1
  131. package/src/x/tailscale/TailscaleTunnel.ts +7 -5
  132. package/dist/BlobStore.d.ts +0 -80
  133. package/dist/BlobStore.js +0 -19
  134. package/dist/ChildProcess.d.ts +0 -60
  135. package/dist/Commander.d.ts +0 -100
  136. package/dist/ContentNegotiation.d.ts +0 -12
  137. package/dist/Cookies.d.ts +0 -47
  138. package/dist/Development.d.ts +0 -39
  139. package/dist/Effectify.d.ts +0 -209
  140. package/dist/Entity.d.ts +0 -47
  141. package/dist/FilePathPattern.d.ts +0 -29
  142. package/dist/FileRouter.d.ts +0 -56
  143. package/dist/FileRouterCodegen.d.ts +0 -18
  144. package/dist/FileRouterPattern.d.ts +0 -9
  145. package/dist/FileRouterPattern.js +0 -35
  146. package/dist/FileSystem.d.ts +0 -158
  147. package/dist/Http.d.ts +0 -37
  148. package/dist/HttpAppExtra.d.ts +0 -7
  149. package/dist/HttpAppExtra.js +0 -320
  150. package/dist/HttpUtils.d.ts +0 -3
  151. package/dist/HttpUtils.js +0 -11
  152. package/dist/PathPattern.d.ts +0 -134
  153. package/dist/PlatformError.d.ts +0 -38
  154. package/dist/PlatformRuntime.d.ts +0 -27
  155. package/dist/Route.d.ts +0 -97
  156. package/dist/RouteBody.d.ts +0 -47
  157. package/dist/RouteError.d.ts +0 -98
  158. package/dist/RouteHook.d.ts +0 -12
  159. package/dist/RouteHttp.d.ts +0 -21
  160. package/dist/RouteHttpTracer.d.ts +0 -10
  161. package/dist/RouteMount.d.ts +0 -86
  162. package/dist/RouteSchema.d.ts +0 -86
  163. package/dist/RouteSse.d.ts +0 -21
  164. package/dist/RouteTree.d.ts +0 -57
  165. package/dist/RouteTrie.d.ts +0 -20
  166. package/dist/RouterPattern.d.ts +0 -118
  167. package/dist/RouterPattern.js +0 -269
  168. package/dist/SchemaExtra.d.ts +0 -7
  169. package/dist/Socket.d.ts +0 -27
  170. package/dist/Sql.d.ts +0 -34
  171. package/dist/Sql.js +0 -5
  172. package/dist/SqlIntrospect.d.ts +0 -91
  173. package/dist/Start.d.ts +0 -44
  174. package/dist/StartApp.d.ts +0 -19
  175. package/dist/StreamExtra.d.ts +0 -28
  176. package/dist/System.d.ts +0 -7
  177. package/dist/TuplePathPattern.d.ts +0 -9
  178. package/dist/Unique.d.ts +0 -50
  179. package/dist/Values.d.ts +0 -27
  180. package/dist/bun/BunBlobStoreDisk.d.ts +0 -6
  181. package/dist/bun/BunBlobStoreDisk.js +0 -116
  182. package/dist/bun/BunBlobStoreS3.d.ts +0 -11
  183. package/dist/bun/BunBlobStoreS3.js +0 -89
  184. package/dist/bun/BunBlobWatcherDisk.d.ts +0 -6
  185. package/dist/bun/BunBlobWatcherDisk.js +0 -60
  186. package/dist/bun/BunBlobWatcherQueue.d.ts +0 -6
  187. package/dist/bun/BunBlobWatcherQueue.js +0 -17
  188. package/dist/bun/BunBundle.d.ts +0 -11
  189. package/dist/bun/BunChildProcessSpawner.d.ts +0 -3
  190. package/dist/bun/BunHttpServer.d.ts +0 -44
  191. package/dist/bun/BunHttpServer.js +0 -186
  192. package/dist/bun/BunHttpServer_web.d.ts +0 -60
  193. package/dist/bun/BunHttpServer_web.js +0 -252
  194. package/dist/bun/BunImportTrackerPlugin.d.ts +0 -13
  195. package/dist/bun/BunPlatformHttpServer.d.ts +0 -10
  196. package/dist/bun/BunPlatformHttpServer.js +0 -53
  197. package/dist/bun/BunRoute.d.ts +0 -48
  198. package/dist/bun/BunRuntime.d.ts +0 -2
  199. package/dist/bun/BunServer.d.ts +0 -40
  200. package/dist/bun/BunServerRequest.d.ts +0 -60
  201. package/dist/bun/BunServerRequest.js +0 -252
  202. package/dist/bun/BunSql.d.ts +0 -4
  203. package/dist/bun/BunSql.js +0 -81
  204. package/dist/bun/BunVirtualFilesPlugin.d.ts +0 -4
  205. package/dist/bun/_BunEnhancedResolve.d.ts +0 -45
  206. package/dist/bun/index.d.ts +0 -5
  207. package/dist/bundler/Bundle.d.ts +0 -61
  208. package/dist/bundler/BundleFiles.d.ts +0 -13
  209. package/dist/bundler/BundleHttp.d.ts +0 -45
  210. package/dist/bundler/BundleHttp.js +0 -176
  211. package/dist/bundler/BundleRoute.d.ts +0 -27
  212. package/dist/client/Overlay.d.ts +0 -2
  213. package/dist/client/ScrollState.d.ts +0 -6
  214. package/dist/client/index.d.ts +0 -6
  215. package/dist/console/Console.d.ts +0 -6
  216. package/dist/console/ConsoleErrors.d.ts +0 -3
  217. package/dist/console/ConsoleLogger.d.ts +0 -3
  218. package/dist/console/ConsoleMetrics.d.ts +0 -3
  219. package/dist/console/ConsoleProcess.d.ts +0 -3
  220. package/dist/console/ConsoleStore.d.ts +0 -144
  221. package/dist/console/ConsoleTracer.d.ts +0 -3
  222. package/dist/console/Simulation.d.ts +0 -2
  223. package/dist/console/index.d.ts +0 -3
  224. package/dist/console/routes/errors/route.d.ts +0 -10
  225. package/dist/console/routes/errors/route.js +0 -47
  226. package/dist/console/routes/fiberDetail.d.ts +0 -16
  227. package/dist/console/routes/fiberDetail.js +0 -38
  228. package/dist/console/routes/fibers/route.d.ts +0 -10
  229. package/dist/console/routes/fibers/route.js +0 -19
  230. package/dist/console/routes/git/route.d.ts +0 -11
  231. package/dist/console/routes/git/route.js +0 -33
  232. package/dist/console/routes/layout.d.ts +0 -9
  233. package/dist/console/routes/layout.js +0 -3
  234. package/dist/console/routes/logs/route.d.ts +0 -10
  235. package/dist/console/routes/logs/route.js +0 -32
  236. package/dist/console/routes/metrics/route.d.ts +0 -10
  237. package/dist/console/routes/metrics/route.js +0 -17
  238. package/dist/console/routes/route.d.ts +0 -6
  239. package/dist/console/routes/route.js +0 -5
  240. package/dist/console/routes/routes/route.d.ts +0 -6
  241. package/dist/console/routes/routes/route.js +0 -20
  242. package/dist/console/routes/services/route.d.ts +0 -6
  243. package/dist/console/routes/services/route.js +0 -12
  244. package/dist/console/routes/system/route.d.ts +0 -10
  245. package/dist/console/routes/system/route.js +0 -18
  246. package/dist/console/routes/traceDetail.d.ts +0 -16
  247. package/dist/console/routes/traceDetail.js +0 -14
  248. package/dist/console/routes/traces/route.d.ts +0 -10
  249. package/dist/console/routes/traces/route.js +0 -39
  250. package/dist/console/routes/tree.d.ts +0 -153
  251. package/dist/console/ui/Errors.d.ts +0 -4
  252. package/dist/console/ui/Errors.js +0 -15
  253. package/dist/console/ui/Fibers.d.ts +0 -24
  254. package/dist/console/ui/Fibers.js +0 -121
  255. package/dist/console/ui/Git.d.ts +0 -20
  256. package/dist/console/ui/Git.js +0 -95
  257. package/dist/console/ui/Logs.d.ts +0 -4
  258. package/dist/console/ui/Logs.js +0 -25
  259. package/dist/console/ui/Metrics.d.ts +0 -4
  260. package/dist/console/ui/Metrics.js +0 -26
  261. package/dist/console/ui/Routes.d.ts +0 -8
  262. package/dist/console/ui/Routes.js +0 -70
  263. package/dist/console/ui/Services.d.ts +0 -10
  264. package/dist/console/ui/Services.js +0 -246
  265. package/dist/console/ui/Shell.d.ts +0 -10
  266. package/dist/console/ui/Shell.js +0 -7
  267. package/dist/console/ui/System.d.ts +0 -4
  268. package/dist/console/ui/System.js +0 -35
  269. package/dist/console/ui/Traces.d.ts +0 -12
  270. package/dist/console/ui/Traces.js +0 -179
  271. package/dist/datastar/actions/fetch.d.ts +0 -30
  272. package/dist/datastar/actions/peek.d.ts +0 -1
  273. package/dist/datastar/actions/setAll.d.ts +0 -1
  274. package/dist/datastar/actions/toggleAll.d.ts +0 -1
  275. package/dist/datastar/attributes/attr.d.ts +0 -1
  276. package/dist/datastar/attributes/bind.d.ts +0 -1
  277. package/dist/datastar/attributes/class.d.ts +0 -1
  278. package/dist/datastar/attributes/computed.d.ts +0 -1
  279. package/dist/datastar/attributes/effect.d.ts +0 -1
  280. package/dist/datastar/attributes/indicator.d.ts +0 -1
  281. package/dist/datastar/attributes/init.d.ts +0 -1
  282. package/dist/datastar/attributes/jsonSignals.d.ts +0 -1
  283. package/dist/datastar/attributes/on.d.ts +0 -1
  284. package/dist/datastar/attributes/onIntersect.d.ts +0 -1
  285. package/dist/datastar/attributes/onInterval.d.ts +0 -1
  286. package/dist/datastar/attributes/onSignalPatch.d.ts +0 -1
  287. package/dist/datastar/attributes/ref.d.ts +0 -1
  288. package/dist/datastar/attributes/show.d.ts +0 -1
  289. package/dist/datastar/attributes/signals.d.ts +0 -1
  290. package/dist/datastar/attributes/style.d.ts +0 -1
  291. package/dist/datastar/attributes/text.d.ts +0 -1
  292. package/dist/datastar/engine.d.ts +0 -162
  293. package/dist/datastar/happydom.d.ts +0 -1
  294. package/dist/datastar/happydom.js +0 -8
  295. package/dist/datastar/index.d.ts +0 -24
  296. package/dist/datastar/load.d.ts +0 -24
  297. package/dist/datastar/load.js +0 -24
  298. package/dist/datastar/utils.d.ts +0 -51
  299. package/dist/datastar/watchers/patchElements.d.ts +0 -1
  300. package/dist/datastar/watchers/patchSignals.d.ts +0 -1
  301. package/dist/experimental/EncryptedCookies.d.ts +0 -48
  302. package/dist/experimental/SseHttpResponse.d.ts +0 -7
  303. package/dist/experimental/SseHttpResponse.js +0 -28
  304. package/dist/experimental/index.d.ts +0 -1
  305. package/dist/hyper/Hyper.d.ts +0 -25
  306. package/dist/hyper/HyperHtml.d.ts +0 -23
  307. package/dist/hyper/HyperHtml.test.d.ts +0 -1
  308. package/dist/hyper/HyperHtml.test.js +0 -197
  309. package/dist/hyper/HyperNode.d.ts +0 -14
  310. package/dist/hyper/HyperRoute.d.ts +0 -8
  311. package/dist/hyper/HyperRoute.test.d.ts +0 -1
  312. package/dist/hyper/HyperRoute.test.js +0 -83
  313. package/dist/hyper/html.d.ts +0 -11
  314. package/dist/hyper/index.d.ts +0 -6
  315. package/dist/hyper/jsx-runtime.d.ts +0 -7
  316. package/dist/index.d.ts +0 -8
  317. package/dist/inference_check.d.ts +0 -1
  318. package/dist/inference_check.js +0 -15
  319. package/dist/lint/plugin.d.ts +0 -86
  320. package/dist/lint/plugin.js +0 -341
  321. package/dist/middlewares/BasicAuthMiddleware.d.ts +0 -8
  322. package/dist/middlewares/BasicAuthMiddleware.js +0 -22
  323. package/dist/middlewares/index.d.ts +0 -1
  324. package/dist/middlewares/index.js +0 -1
  325. package/dist/node/Effectify.d.ts +0 -209
  326. package/dist/node/Effectify.js +0 -19
  327. package/dist/node/FileSystem.d.ts +0 -7
  328. package/dist/node/FileSystem.js +0 -420
  329. package/dist/node/NodeFileSystem.d.ts +0 -7
  330. package/dist/node/NodeUtils.d.ts +0 -2
  331. package/dist/node/PlatformError.d.ts +0 -46
  332. package/dist/node/PlatformError.js +0 -43
  333. package/dist/node/Utils.d.ts +0 -1
  334. package/dist/node/Utils.js +0 -19
  335. package/dist/repro_fail.d.ts +0 -1
  336. package/dist/repro_fail.js +0 -14
  337. package/dist/sql/bun/index.d.ts +0 -3
  338. package/dist/sql/mssql/docker.d.ts +0 -2
  339. package/dist/sql/mssql/index.d.ts +0 -21
  340. package/dist/testing/TestHttpClient.d.ts +0 -13
  341. package/dist/testing/TestHttpClient.js +0 -68
  342. package/dist/testing/TestLogger.d.ts +0 -13
  343. package/dist/testing/index.d.ts +0 -2
  344. package/dist/testing/utils.d.ts +0 -9
  345. package/dist/x/cloudflare/CloudflareTunnel.d.ts +0 -10
  346. package/dist/x/cloudflare/index.d.ts +0 -1
  347. package/dist/x/datastar/Datastar.d.ts +0 -6
  348. package/dist/x/datastar/Datastar.js +0 -47
  349. package/dist/x/datastar/index.d.ts +0 -1
  350. package/dist/x/datastar/index.js +0 -1
  351. package/dist/x/tailscale/TailscaleTunnel.d.ts +0 -15
  352. package/dist/x/tailscale/index.d.ts +0 -1
  353. package/dist/x/tailwind/TailwindPlugin.d.ts +0 -23
  354. package/dist/x/tailwind/compile.d.ts +0 -19
  355. package/dist/x/tailwind/plugin.d.ts +0 -2
  356. /package/src/{Sql.ts → sql/Sql.ts} +0 -0
@@ -1,403 +1,536 @@
1
- import { action, DATASTAR_FETCH_EVENT, filtered, startPeeking, stopPeeking, } from "../engine.js";
2
- import { kebab } from "../utils.js";
3
- const createHttpMethod = (name, method, openWhenHiddenDefault = true) => action({
1
+ import {
2
+ action,
3
+ DATASTAR_FETCH_EVENT,
4
+ filtered,
5
+ startPeeking,
6
+ stopPeeking,
7
+ } from "../engine.js"
8
+ import { kebab } from "../utils.js"
9
+
10
+ const createHttpMethod = (
11
+ name,
12
+ method,
13
+ openWhenHiddenDefault = true,
14
+ ) =>
15
+ action({
4
16
  name,
5
- 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, } = {}) => {
6
- const controller = requestCancellation instanceof AbortController ? requestCancellation : new AbortController();
7
- if (requestCancellation === "auto") {
8
- cleanups.get(`@${name}`)?.();
9
- cleanups.set(`@${name}`, async () => {
10
- controller.abort();
11
- await Promise.resolve();
12
- });
17
+ apply: async (
18
+ { el, evt, error, cleanups },
19
+ url,
20
+ {
21
+ selector,
22
+ headers: userHeaders,
23
+ contentType = "json",
24
+ filterSignals: { include = /.*/, exclude = /(^|\.)_/ } = {},
25
+ openWhenHidden = openWhenHiddenDefault,
26
+ payload,
27
+ requestCancellation = "auto",
28
+ retry = "auto",
29
+ retryInterval = 1_000,
30
+ retryScaler = 2,
31
+ retryMaxWaitMs = 30_000,
32
+ retryMaxCount = 10,
33
+ } = {},
34
+ ) => {
35
+ const controller =
36
+ requestCancellation instanceof AbortController ? requestCancellation : new AbortController()
37
+ if (requestCancellation === "auto") {
38
+ cleanups.get(`@${name}`)?.()
39
+ cleanups.set(`@${name}`, async () => {
40
+ controller.abort()
41
+ await Promise.resolve()
42
+ })
43
+ }
44
+
45
+ let cleanupFn = null
46
+
47
+ try {
48
+ if (!url?.length) {
49
+ throw error("FetchNoUrlProvided", { action })
13
50
  }
14
- let cleanupFn = null;
15
- try {
16
- if (!url?.length) {
17
- throw error("FetchNoUrlProvided", { action });
51
+
52
+ const initialHeaders = {
53
+ Accept: "text/event-stream, text/html, application/json",
54
+ "Datastar-Request": true,
55
+ }
56
+ if (contentType === "json") {
57
+ initialHeaders["Content-Type"] = "application/json"
58
+ }
59
+ const headers = Object.assign({}, initialHeaders, userHeaders)
60
+
61
+ const req = {
62
+ method,
63
+ headers,
64
+ openWhenHidden,
65
+ retry,
66
+ retryInterval,
67
+ retryScaler,
68
+ retryMaxWaitMs,
69
+ retryMaxCount,
70
+ signal: controller.signal,
71
+ onopen: async (response) => {
72
+ if (response.status >= 400) {
73
+ dispatchFetch(ERROR, el, { status: response.status.toString() })
18
74
  }
19
- const initialHeaders = {
20
- Accept: "text/event-stream, text/html, application/json",
21
- "Datastar-Request": true,
22
- };
23
- if (contentType === "json") {
24
- initialHeaders["Content-Type"] = "application/json";
75
+ },
76
+ onmessage: (evt) => {
77
+ if (!evt.event.startsWith("datastar")) return
78
+ const type = evt.event
79
+ const argsRawLines = {}
80
+
81
+ for (const line of evt.data.split("\n")) {
82
+ const i = line.indexOf(" ")
83
+ const k = line.slice(0, i)
84
+ const v = line.slice(i + 1)
85
+ ;(argsRawLines[k] ||= []).push(v)
25
86
  }
26
- const headers = Object.assign({}, initialHeaders, userHeaders);
27
- const req = {
28
- method,
29
- headers,
30
- openWhenHidden,
31
- retry,
32
- retryInterval,
33
- retryScaler,
34
- retryMaxWaitMs,
35
- retryMaxCount,
36
- signal: controller.signal,
37
- onopen: async (response) => {
38
- if (response.status >= 400) {
39
- dispatchFetch(ERROR, el, { status: response.status.toString() });
40
- }
41
- },
42
- onmessage: (evt) => {
43
- if (!evt.event.startsWith("datastar"))
44
- return;
45
- const type = evt.event;
46
- const argsRawLines = {};
47
- for (const line of evt.data.split("\n")) {
48
- const i = line.indexOf(" ");
49
- const k = line.slice(0, i);
50
- const v = line.slice(i + 1);
51
- (argsRawLines[k] ||= []).push(v);
52
- }
53
- const argsRaw = Object.fromEntries(Object.entries(argsRawLines).map(([k, v]) => [k, v.join("\n")]));
54
- dispatchFetch(type, el, argsRaw);
55
- },
56
- onerror: (error) => {
57
- if (isWrongContent(error)) {
58
- throw error("FetchExpectedTextEventStream", { url });
59
- }
60
- if (error) {
61
- console.error(error.message);
62
- dispatchFetch(RETRYING, el, { message: error.message });
63
- }
64
- },
65
- };
66
- const urlInstance = new URL(url, document.baseURI);
67
- const queryParams = new URLSearchParams(urlInstance.search);
68
- if (contentType === "json") {
69
- startPeeking();
70
- payload = payload !== undefined ? payload : filtered({ include, exclude });
71
- stopPeeking();
72
- const body = JSON.stringify(payload);
73
- if (method === "GET") {
74
- queryParams.set("datastar", body);
75
- }
76
- else {
77
- req.body = body;
78
- }
87
+
88
+ const argsRaw = Object.fromEntries(
89
+ Object.entries(argsRawLines).map(([k, v]) => [k, v.join("\n")]),
90
+ )
91
+
92
+ dispatchFetch(type, el, argsRaw)
93
+ },
94
+ onerror: (error) => {
95
+ if (isWrongContent(error)) {
96
+ throw error("FetchExpectedTextEventStream", { url })
79
97
  }
80
- else if (contentType === "form") {
81
- const formEl = (selector ? document.querySelector(selector) : el.closest("form"));
82
- if (!formEl) {
83
- throw error("FetchFormNotFound", { action, selector });
84
- }
85
- if (!formEl.noValidate && !formEl.checkValidity()) {
86
- formEl.reportValidity();
87
- return;
88
- }
89
- const formData = new FormData(formEl);
90
- let submitter = el;
91
- if (el === formEl && evt instanceof SubmitEvent) {
92
- submitter = evt.submitter;
93
- }
94
- else {
95
- const preventDefault = (evt) => evt.preventDefault();
96
- formEl.addEventListener("submit", preventDefault);
97
- cleanupFn = () => {
98
- formEl.removeEventListener("submit", preventDefault);
99
- };
100
- }
101
- if (submitter instanceof HTMLButtonElement) {
102
- const name = submitter.getAttribute("name");
103
- if (name)
104
- formData.append(name, submitter.value);
105
- }
106
- const multipart = formEl.getAttribute("enctype") === "multipart/form-data";
107
- if (!multipart) {
108
- headers["Content-Type"] = "application/x-www-form-urlencoded";
109
- }
110
- const formParams = new URLSearchParams(formData);
111
- if (method === "GET") {
112
- for (const [key, value] of formParams) {
113
- queryParams.append(key, value);
114
- }
115
- }
116
- else if (multipart) {
117
- req.body = formData;
118
- }
119
- else {
120
- req.body = formParams;
121
- }
122
- }
123
- else {
124
- throw error("FetchInvalidContentType", { action, contentType });
98
+ if (error) {
99
+ console.error(error.message)
100
+ dispatchFetch(RETRYING, el, { message: error.message })
125
101
  }
126
- dispatchFetch(STARTED, el, {});
127
- urlInstance.search = queryParams.toString();
128
- try {
129
- await fetchEventSource(urlInstance.toString(), el, req);
102
+ },
103
+ }
104
+
105
+ const urlInstance = new URL(url, document.baseURI)
106
+ const queryParams = new URLSearchParams(urlInstance.search)
107
+
108
+ if (contentType === "json") {
109
+ startPeeking()
110
+ payload = payload !== undefined ? payload : filtered({ include, exclude })
111
+ stopPeeking()
112
+ const body = JSON.stringify(payload)
113
+ if (method === "GET") {
114
+ queryParams.set("datastar", body)
115
+ } else {
116
+ req.body = body
117
+ }
118
+ } else if (contentType === "form") {
119
+ const formEl = (
120
+ selector ? document.querySelector(selector) : el.closest("form")
121
+ )
122
+ if (!formEl) {
123
+ throw error("FetchFormNotFound", { action, selector })
124
+ }
125
+
126
+ if (!formEl.noValidate && !formEl.checkValidity()) {
127
+ formEl.reportValidity()
128
+ return
129
+ }
130
+
131
+ const formData = new FormData(formEl)
132
+ let submitter = el
133
+
134
+ if (el === formEl && evt instanceof SubmitEvent) {
135
+ submitter = evt.submitter
136
+ } else {
137
+ const preventDefault = (evt) => evt.preventDefault()
138
+ formEl.addEventListener("submit", preventDefault)
139
+ cleanupFn = () => {
140
+ formEl.removeEventListener("submit", preventDefault)
130
141
  }
131
- catch (e) {
132
- if (!isWrongContent(e)) {
133
- throw error("FetchFailed", { method, url, error: e.message });
134
- }
142
+ }
143
+
144
+ if (submitter instanceof HTMLButtonElement) {
145
+ const name = submitter.getAttribute("name")
146
+ if (name) formData.append(name, submitter.value)
147
+ }
148
+
149
+ const multipart = formEl.getAttribute("enctype") === "multipart/form-data"
150
+ if (!multipart) {
151
+ headers["Content-Type"] = "application/x-www-form-urlencoded"
152
+ }
153
+
154
+ const formParams = new URLSearchParams(formData)
155
+ if (method === "GET") {
156
+ for (const [key, value] of formParams) {
157
+ queryParams.append(key, value)
135
158
  }
159
+ } else if (multipart) {
160
+ req.body = formData
161
+ } else {
162
+ req.body = formParams
163
+ }
164
+ } else {
165
+ throw error("FetchInvalidContentType", { action, contentType })
136
166
  }
137
- finally {
138
- dispatchFetch(FINISHED, el, {});
139
- cleanupFn?.();
140
- cleanups.delete(`@${name}`);
167
+
168
+ dispatchFetch(STARTED, el, {})
169
+ urlInstance.search = queryParams.toString()
170
+
171
+ try {
172
+ await fetchEventSource(urlInstance.toString(), el, req)
173
+ } catch (e) {
174
+ if (!isWrongContent(e)) {
175
+ throw error("FetchFailed", { method, url, error: e.message })
176
+ }
141
177
  }
178
+ } finally {
179
+ dispatchFetch(FINISHED, el, {})
180
+ cleanupFn?.()
181
+ cleanups.delete(`@${name}`)
182
+ }
142
183
  },
143
- });
144
- createHttpMethod("get", "GET", false);
145
- createHttpMethod("patch", "PATCH");
146
- createHttpMethod("post", "POST");
147
- createHttpMethod("put", "PUT");
148
- createHttpMethod("delete", "DELETE");
149
- export const STARTED = "started";
150
- export const FINISHED = "finished";
151
- export const ERROR = "error";
152
- export const RETRYING = "retrying";
153
- export const RETRIES_FAILED = "retries-failed";
154
- const dispatchFetch = (type, el, argsRaw) => document.dispatchEvent(new CustomEvent(DATASTAR_FETCH_EVENT, {
155
- detail: { type, el, argsRaw },
156
- }));
157
- const isWrongContent = (err) => `${err}`.includes("text/event-stream");
158
- const getBytes = async (stream, onChunk) => {
159
- const reader = stream.getReader();
160
- let result = await reader.read();
161
- while (!result.done) {
162
- onChunk(result.value);
163
- result = await reader.read();
164
- }
165
- };
184
+ })
185
+
186
+ createHttpMethod("get", "GET", false)
187
+ createHttpMethod("patch", "PATCH")
188
+ createHttpMethod("post", "POST")
189
+ createHttpMethod("put", "PUT")
190
+ createHttpMethod("delete", "DELETE")
191
+
192
+ export const STARTED = "started"
193
+ export const FINISHED = "finished"
194
+ export const ERROR = "error"
195
+ export const RETRYING = "retrying"
196
+ export const RETRIES_FAILED = "retries-failed"
197
+
198
+ const dispatchFetch = (type, el, argsRaw) =>
199
+ document.dispatchEvent(
200
+ new CustomEvent(DATASTAR_FETCH_EVENT, {
201
+ detail: { type, el, argsRaw },
202
+ }),
203
+ )
204
+
205
+ const isWrongContent = (err) => `${err}`.includes("text/event-stream")
206
+
207
+ const getBytes = async (
208
+ stream,
209
+ onChunk,
210
+ ) => {
211
+ const reader = stream.getReader()
212
+ let result = await reader.read()
213
+ while (!result.done) {
214
+ onChunk(result.value)
215
+ result = await reader.read()
216
+ }
217
+ }
218
+
166
219
  const getLines = (onLine) => {
167
- let buffer;
168
- let position;
169
- let fieldLength;
170
- let discardTrailingNewline = false;
171
- return (arr) => {
172
- if (!buffer) {
173
- buffer = arr;
174
- position = 0;
175
- fieldLength = -1;
220
+ let buffer
221
+ let position
222
+ let fieldLength
223
+ let discardTrailingNewline = false
224
+
225
+ return (arr) => {
226
+ if (!buffer) {
227
+ buffer = arr
228
+ position = 0
229
+ fieldLength = -1
230
+ } else {
231
+ buffer = concat(buffer, arr)
232
+ }
233
+
234
+ const bufLength = buffer.length
235
+ let lineStart = 0
236
+ while (position < bufLength) {
237
+ if (discardTrailingNewline) {
238
+ if (buffer[position] === 10) lineStart = ++position
239
+ discardTrailingNewline = false
240
+ }
241
+
242
+ let lineEnd = -1
243
+ for (; position < bufLength && lineEnd === -1; ++position) {
244
+ switch (buffer[position]) {
245
+ case 58:
246
+ if (fieldLength === -1) {
247
+ fieldLength = position - lineStart
248
+ }
249
+ break
250
+ // @ts-expect-error:7029
251
+ case 13:
252
+ discardTrailingNewline = true
253
+ case 10:
254
+ lineEnd = position
255
+ break
176
256
  }
177
- else {
178
- buffer = concat(buffer, arr);
257
+ }
258
+
259
+ if (lineEnd === -1) break
260
+
261
+ onLine(buffer.subarray(lineStart, lineEnd), fieldLength)
262
+ lineStart = position
263
+ fieldLength = -1
264
+ }
265
+
266
+ if (lineStart === bufLength) buffer = undefined
267
+ else if (lineStart) {
268
+ buffer = buffer.subarray(lineStart)
269
+ position -= lineStart
270
+ }
271
+ }
272
+ }
273
+
274
+ const getMessages = (
275
+ onId,
276
+ onRetry,
277
+ onMessage,
278
+ ) => {
279
+ let message = newMessage()
280
+ const decoder = new TextDecoder()
281
+
282
+ return (line, fieldLength) => {
283
+ if (!line.length) {
284
+ onMessage?.(message)
285
+ message = newMessage()
286
+ } else if (fieldLength > 0) {
287
+ const field = decoder.decode(line.subarray(0, fieldLength))
288
+ const valueOffset = fieldLength + (line[fieldLength + 1] === 32 ? 2 : 1)
289
+ const value = decoder.decode(line.subarray(valueOffset))
290
+
291
+ switch (field) {
292
+ case "data":
293
+ message.data = message.data ? `${message.data}\n${value}` : value
294
+ break
295
+ case "event":
296
+ message.event = value
297
+ break
298
+ case "id":
299
+ onId((message.id = value))
300
+ break
301
+ case "retry": {
302
+ const retry = +value
303
+ if (!Number.isNaN(retry)) {
304
+ onRetry((message.retry = retry))
305
+ }
306
+ break
179
307
  }
180
- const bufLength = buffer.length;
181
- let lineStart = 0;
182
- while (position < bufLength) {
183
- if (discardTrailingNewline) {
184
- if (buffer[position] === 10)
185
- lineStart = ++position;
186
- discardTrailingNewline = false;
187
- }
188
- let lineEnd = -1;
189
- for (; position < bufLength && lineEnd === -1; ++position) {
190
- switch (buffer[position]) {
191
- case 58:
192
- if (fieldLength === -1) {
193
- fieldLength = position - lineStart;
194
- }
195
- break;
196
- // @ts-expect-error:7029
197
- case 13:
198
- discardTrailingNewline = true;
199
- case 10:
200
- lineEnd = position;
201
- break;
202
- }
308
+ }
309
+ }
310
+ }
311
+ }
312
+
313
+ const concat = (a, b) => {
314
+ const res = new Uint8Array(a.length + b.length)
315
+ res.set(a)
316
+ res.set(b, a.length)
317
+ return res
318
+ }
319
+
320
+ const newMessage = () => ({
321
+ data: "",
322
+ event: "",
323
+ id: "",
324
+ retry: undefined,
325
+ })
326
+
327
+ const fetchEventSource = (
328
+ input,
329
+ el,
330
+ {
331
+ signal: inputSignal,
332
+ headers: inputHeaders,
333
+ onopen: inputOnOpen,
334
+ onmessage,
335
+ onclose,
336
+ onerror,
337
+ openWhenHidden,
338
+ fetch: inputFetch,
339
+ retry = "auto",
340
+ retryInterval = 1_000,
341
+ retryScaler = 2,
342
+ retryMaxWaitMs = 30_000,
343
+ retryMaxCount = 10,
344
+ responseOverrides,
345
+ ...rest
346
+ },
347
+ ) => {
348
+ return new Promise((resolve, reject) => {
349
+ const headers = {
350
+ ...inputHeaders,
351
+ }
352
+
353
+ let curRequestController
354
+ const onVisibilityChange = () => {
355
+ curRequestController.abort()
356
+ if (!document.hidden) create()
357
+ }
358
+
359
+ if (!openWhenHidden) {
360
+ document.addEventListener("visibilitychange", onVisibilityChange)
361
+ }
362
+
363
+ let retryTimer
364
+ const dispose = () => {
365
+ document.removeEventListener("visibilitychange", onVisibilityChange)
366
+ clearTimeout(retryTimer)
367
+ curRequestController.abort()
368
+ }
369
+
370
+ inputSignal?.addEventListener("abort", () => {
371
+ dispose()
372
+ resolve()
373
+ })
374
+
375
+ const fetch = inputFetch || window.fetch
376
+ const onopen = inputOnOpen || (() => {})
377
+
378
+ let retries = 0
379
+ let baseRetryInterval = retryInterval
380
+ const create = async () => {
381
+ curRequestController = new AbortController()
382
+ const curRequestSignal = curRequestController.signal
383
+ try {
384
+ const response = await fetch(input, {
385
+ ...rest,
386
+ headers,
387
+ signal: curRequestSignal,
388
+ })
389
+
390
+ await onopen(response)
391
+
392
+ const dispatchNonSSE = async (
393
+ dispatchType,
394
+ response,
395
+ name,
396
+ responseOverrides,
397
+ ...argNames
398
+ ) => {
399
+ const argsRaw = {
400
+ [name]: await response.text(),
401
+ }
402
+ for (const n of argNames) {
403
+ let v = response.headers.get(`datastar-${kebab(n)}`)
404
+ if (responseOverrides) {
405
+ const o = (responseOverrides)[n]
406
+ if (o) v = typeof o === "string" ? o : JSON.stringify(o)
203
407
  }
204
- if (lineEnd === -1)
205
- break;
206
- onLine(buffer.subarray(lineStart, lineEnd), fieldLength);
207
- lineStart = position;
208
- fieldLength = -1;
408
+ if (v) argsRaw[n] = v
409
+ }
410
+
411
+ dispatchFetch(dispatchType, el, argsRaw)
412
+ dispose()
413
+ resolve()
209
414
  }
210
- if (lineStart === bufLength)
211
- buffer = undefined;
212
- else if (lineStart) {
213
- buffer = buffer.subarray(lineStart);
214
- position -= lineStart;
415
+
416
+ const status = response.status
417
+ const isNoContentStatus = status === 204
418
+ const isRedirectStatus = status >= 300 && status < 400
419
+ const isErrorStatus = status >= 400 && status < 600
420
+
421
+ if (status !== 200) {
422
+ onclose?.()
423
+ if (
424
+ retry !== "never" &&
425
+ !isNoContentStatus &&
426
+ !isRedirectStatus &&
427
+ (retry === "always" || (retry === "error" && isErrorStatus))
428
+ ) {
429
+ clearTimeout(retryTimer)
430
+ retryTimer = setTimeout(create, retryInterval)
431
+ return
432
+ }
433
+ dispose()
434
+ resolve()
435
+ return
215
436
  }
216
- };
217
- };
218
- const getMessages = (onId, onRetry, onMessage) => {
219
- let message = newMessage();
220
- const decoder = new TextDecoder();
221
- return (line, fieldLength) => {
222
- if (!line.length) {
223
- onMessage?.(message);
224
- message = newMessage();
437
+
438
+ retries = 0
439
+ retryInterval = baseRetryInterval
440
+
441
+ const ct = response.headers.get("Content-Type")
442
+ if (ct?.includes("text/html")) {
443
+ return await dispatchNonSSE(
444
+ "datastar-patch-elements",
445
+ response,
446
+ "elements",
447
+ responseOverrides,
448
+ "selector",
449
+ "mode",
450
+ "namespace",
451
+ "useViewTransition",
452
+ "namespace",
453
+ )
225
454
  }
226
- else if (fieldLength > 0) {
227
- const field = decoder.decode(line.subarray(0, fieldLength));
228
- const valueOffset = fieldLength + (line[fieldLength + 1] === 32 ? 2 : 1);
229
- const value = decoder.decode(line.subarray(valueOffset));
230
- switch (field) {
231
- case "data":
232
- message.data = message.data ? `${message.data}\n${value}` : value;
233
- break;
234
- case "event":
235
- message.event = value;
236
- break;
237
- case "id":
238
- onId((message.id = value));
239
- break;
240
- case "retry": {
241
- const retry = +value;
242
- if (!Number.isNaN(retry)) {
243
- onRetry((message.retry = retry));
244
- }
245
- break;
246
- }
247
- }
455
+
456
+ if (ct?.includes("application/json")) {
457
+ return await dispatchNonSSE(
458
+ "datastar-patch-signals",
459
+ response,
460
+ "signals",
461
+ responseOverrides,
462
+ "onlyIfMissing",
463
+ )
248
464
  }
249
- };
250
- };
251
- const concat = (a, b) => {
252
- const res = new Uint8Array(a.length + b.length);
253
- res.set(a);
254
- res.set(b, a.length);
255
- return res;
256
- };
257
- const newMessage = () => ({
258
- data: "",
259
- event: "",
260
- id: "",
261
- retry: undefined,
262
- });
263
- 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 }) => {
264
- return new Promise((resolve, reject) => {
265
- const headers = {
266
- ...inputHeaders,
267
- };
268
- let curRequestController;
269
- const onVisibilityChange = () => {
270
- curRequestController.abort();
271
- if (!document.hidden)
272
- create();
273
- };
274
- if (!openWhenHidden) {
275
- document.addEventListener("visibilitychange", onVisibilityChange);
276
- }
277
- let retryTimer;
278
- const dispose = () => {
279
- document.removeEventListener("visibilitychange", onVisibilityChange);
280
- clearTimeout(retryTimer);
281
- curRequestController.abort();
282
- };
283
- inputSignal?.addEventListener("abort", () => {
284
- dispose();
285
- resolve();
286
- });
287
- const fetch = inputFetch || window.fetch;
288
- const onopen = inputOnOpen || (() => { });
289
- let retries = 0;
290
- let baseRetryInterval = retryInterval;
291
- const create = async () => {
292
- curRequestController = new AbortController();
293
- const curRequestSignal = curRequestController.signal;
294
- try {
295
- const response = await fetch(input, {
296
- ...rest,
297
- headers,
298
- signal: curRequestSignal,
299
- });
300
- await onopen(response);
301
- const dispatchNonSSE = async (dispatchType, response, name, responseOverrides, ...argNames) => {
302
- const argsRaw = {
303
- [name]: await response.text(),
304
- };
305
- for (const n of argNames) {
306
- let v = response.headers.get(`datastar-${kebab(n)}`);
307
- if (responseOverrides) {
308
- const o = responseOverrides[n];
309
- if (o)
310
- v = typeof o === "string" ? o : JSON.stringify(o);
311
- }
312
- if (v)
313
- argsRaw[n] = v;
314
- }
315
- dispatchFetch(dispatchType, el, argsRaw);
316
- dispose();
317
- resolve();
318
- };
319
- const status = response.status;
320
- const isNoContentStatus = status === 204;
321
- const isRedirectStatus = status >= 300 && status < 400;
322
- const isErrorStatus = status >= 400 && status < 600;
323
- if (status !== 200) {
324
- onclose?.();
325
- if (retry !== "never" &&
326
- !isNoContentStatus &&
327
- !isRedirectStatus &&
328
- (retry === "always" || (retry === "error" && isErrorStatus))) {
329
- clearTimeout(retryTimer);
330
- retryTimer = setTimeout(create, retryInterval);
331
- return;
332
- }
333
- dispose();
334
- resolve();
335
- return;
336
- }
337
- retries = 0;
338
- retryInterval = baseRetryInterval;
339
- const ct = response.headers.get("Content-Type");
340
- if (ct?.includes("text/html")) {
341
- return await dispatchNonSSE("datastar-patch-elements", response, "elements", responseOverrides, "selector", "mode", "namespace", "useViewTransition", "namespace");
342
- }
343
- if (ct?.includes("application/json")) {
344
- return await dispatchNonSSE("datastar-patch-signals", response, "signals", responseOverrides, "onlyIfMissing");
345
- }
346
- if (ct?.includes("text/javascript")) {
347
- const script = document.createElement("script");
348
- const scriptAttributesHeader = response.headers.get("datastar-script-attributes");
349
- if (scriptAttributesHeader) {
350
- for (const [name, value] of Object.entries(JSON.parse(scriptAttributesHeader))) {
351
- script.setAttribute(name, value);
352
- }
353
- }
354
- script.textContent = await response.text();
355
- document.head.appendChild(script);
356
- dispose();
357
- return;
358
- }
359
- await getBytes(response.body, getLines(getMessages((id) => {
360
- if (id) {
361
- headers["last-event-id"] = id;
362
- }
363
- else {
364
- delete headers["last-event-id"];
365
- }
366
- }, (retry) => {
367
- baseRetryInterval = retryInterval = retry;
368
- }, onmessage)));
369
- onclose?.();
370
- if (retry === "always" && !isRedirectStatus) {
371
- clearTimeout(retryTimer);
372
- retryTimer = setTimeout(create, retryInterval);
373
- return;
374
- }
375
- dispose();
376
- resolve();
465
+
466
+ if (ct?.includes("text/javascript")) {
467
+ const script = document.createElement("script")
468
+ const scriptAttributesHeader = response.headers.get("datastar-script-attributes")
469
+
470
+ if (scriptAttributesHeader) {
471
+ for (const [name, value] of Object.entries(JSON.parse(scriptAttributesHeader))) {
472
+ script.setAttribute(name, value)
377
473
  }
378
- catch (err) {
379
- if (!curRequestSignal.aborted) {
380
- try {
381
- const interval = onerror?.(err) || retryInterval;
382
- clearTimeout(retryTimer);
383
- retryTimer = setTimeout(create, interval);
384
- retryInterval = Math.min(retryInterval * retryScaler, retryMaxWaitMs);
385
- if (++retries >= retryMaxCount) {
386
- dispatchFetch(RETRIES_FAILED, el, {});
387
- dispose();
388
- reject("Max retries reached.");
389
- }
390
- else {
391
- console.error(`Datastar failed to reach ${input.toString()} retrying in ${interval}ms.`);
392
- }
393
- }
394
- catch (innerErr) {
395
- dispose();
396
- reject(innerErr);
397
- }
474
+ }
475
+ script.textContent = await response.text()
476
+ document.head.appendChild(script)
477
+ dispose()
478
+ return
479
+ }
480
+
481
+ await getBytes(
482
+ response.body,
483
+ getLines(
484
+ getMessages(
485
+ (id) => {
486
+ if (id) {
487
+ headers["last-event-id"] = id
488
+ } else {
489
+ delete headers["last-event-id"]
398
490
  }
491
+ },
492
+ (retry) => {
493
+ baseRetryInterval = retryInterval = retry
494
+ },
495
+ onmessage,
496
+ ),
497
+ ),
498
+ )
499
+
500
+ onclose?.()
501
+
502
+ if (retry === "always" && !isRedirectStatus) {
503
+ clearTimeout(retryTimer)
504
+ retryTimer = setTimeout(create, retryInterval)
505
+ return
506
+ }
507
+
508
+ dispose()
509
+ resolve()
510
+ } catch (err) {
511
+ if (!curRequestSignal.aborted) {
512
+ try {
513
+ const interval = onerror?.(err) || retryInterval
514
+ clearTimeout(retryTimer)
515
+ retryTimer = setTimeout(create, interval)
516
+ retryInterval = Math.min(retryInterval * retryScaler, retryMaxWaitMs)
517
+ if (++retries >= retryMaxCount) {
518
+ dispatchFetch(RETRIES_FAILED, el, {})
519
+ dispose()
520
+ reject("Max retries reached.")
521
+ } else {
522
+ console.error(
523
+ `Datastar failed to reach ${input.toString()} retrying in ${interval}ms.`,
524
+ )
399
525
  }
400
- };
401
- create();
402
- });
403
- };
526
+ } catch (innerErr) {
527
+ dispose()
528
+ reject(innerErr)
529
+ }
530
+ }
531
+ }
532
+ }
533
+
534
+ create()
535
+ })
536
+ }