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.
- package/dist/ChildProcess.js +32 -20
- package/dist/Commander.js +377 -293
- package/dist/ContentNegotiation.js +424 -318
- package/dist/Cookies.js +340 -271
- package/dist/Development.js +85 -49
- package/dist/Effectify.js +22 -14
- package/dist/Entity.js +260 -195
- package/dist/Fetch.js +192 -0
- package/dist/FilePathPattern.js +88 -77
- package/dist/FileRouter.js +192 -136
- package/dist/FileRouterCodegen.js +262 -191
- package/dist/FileSystem.js +126 -64
- package/dist/Http.js +96 -77
- package/dist/PathPattern.js +311 -273
- package/dist/PlatformError.js +36 -21
- package/dist/PlatformRuntime.js +65 -40
- package/dist/Route.js +122 -79
- package/dist/RouteBody.js +83 -58
- package/dist/RouteError.js +46 -25
- package/dist/RouteHook.js +58 -34
- package/dist/RouteHttp.js +346 -237
- package/dist/RouteHttpTracer.js +86 -58
- package/dist/RouteMount.js +81 -58
- package/dist/RouteSchema.js +253 -170
- package/dist/RouteSse.js +87 -72
- package/dist/RouteTree.js +99 -73
- package/dist/RouteTrie.js +160 -133
- package/dist/SchemaExtra.js +87 -62
- package/dist/Socket.js +32 -21
- package/dist/SqlIntrospect.js +317 -268
- package/dist/Start.js +55 -25
- package/dist/StartApp.js +3 -21
- package/dist/StreamExtra.js +109 -74
- package/dist/System.js +37 -21
- package/dist/TuplePathPattern.js +64 -58
- package/dist/Unique.js +159 -120
- package/dist/Values.js +48 -32
- package/dist/bun/BunBundle.js +158 -109
- package/dist/bun/BunChildProcessSpawner.js +121 -82
- package/dist/bun/BunImportTrackerPlugin.js +85 -63
- package/dist/bun/BunRoute.js +135 -99
- package/dist/bun/BunRuntime.js +39 -29
- package/dist/bun/BunServer.js +268 -140
- package/dist/bun/BunVirtualFilesPlugin.js +47 -33
- package/dist/bun/_BunEnhancedResolve.js +107 -82
- package/dist/bun/index.js +5 -5
- package/dist/bundler/Bundle.js +82 -38
- package/dist/bundler/BundleFiles.js +140 -82
- package/dist/bundler/BundleRoute.js +49 -38
- package/dist/client/Overlay.js +29 -28
- package/dist/client/ScrollState.js +94 -82
- package/dist/client/index.js +79 -61
- package/dist/console/Console.js +40 -24
- package/dist/console/ConsoleErrors.js +189 -178
- package/dist/console/ConsoleLogger.js +52 -43
- package/dist/console/ConsoleMetrics.js +69 -58
- package/dist/console/ConsoleProcess.js +57 -47
- package/dist/console/ConsoleStore.js +56 -45
- package/dist/console/ConsoleTracer.js +101 -88
- package/dist/console/Simulation.js +714 -563
- package/dist/console/index.js +3 -3
- package/dist/console/routes/tree.js +29 -28
- package/dist/datastar/actions/fetch.js +514 -381
- package/dist/datastar/actions/peek.js +12 -12
- package/dist/datastar/actions/setAll.js +18 -11
- package/dist/datastar/actions/toggleAll.js +18 -11
- package/dist/datastar/attributes/attr.js +48 -47
- package/dist/datastar/attributes/bind.js +186 -167
- package/dist/datastar/attributes/class.js +51 -44
- package/dist/datastar/attributes/computed.js +23 -24
- package/dist/datastar/attributes/effect.js +9 -8
- package/dist/datastar/attributes/indicator.js +32 -29
- package/dist/datastar/attributes/init.js +26 -25
- package/dist/datastar/attributes/jsonSignals.js +32 -29
- package/dist/datastar/attributes/on.js +76 -73
- package/dist/datastar/attributes/onIntersect.js +51 -51
- package/dist/datastar/attributes/onInterval.js +30 -29
- package/dist/datastar/attributes/onSignalPatch.js +49 -40
- package/dist/datastar/attributes/ref.js +10 -9
- package/dist/datastar/attributes/show.js +31 -30
- package/dist/datastar/attributes/signals.js +17 -16
- package/dist/datastar/attributes/style.js +56 -49
- package/dist/datastar/attributes/text.js +28 -25
- package/dist/datastar/engine.js +1079 -933
- package/dist/datastar/index.js +25 -24
- package/dist/datastar/utils.js +203 -155
- package/dist/datastar/watchers/patchElements.js +459 -372
- package/dist/datastar/watchers/patchSignals.js +13 -13
- package/dist/experimental/EncryptedCookies.js +305 -189
- package/dist/experimental/index.js +1 -1
- package/dist/hyper/Hyper.js +22 -17
- package/dist/hyper/HyperHtml.js +138 -123
- package/dist/hyper/HyperNode.js +11 -9
- package/dist/hyper/HyperRoute.js +41 -28
- package/dist/hyper/html.js +27 -27
- package/dist/hyper/index.js +5 -5
- package/dist/hyper/jsx-runtime.js +11 -5
- package/dist/index.js +8 -8
- package/dist/node/NodeFileSystem.js +606 -341
- package/dist/node/NodeUtils.js +21 -18
- package/dist/sql/Sql.js +8 -0
- package/dist/sql/bun/index.js +134 -67
- package/dist/sql/index.js +1 -0
- package/dist/sql/libsql/index.js +156 -0
- package/dist/sql/mssql/docker.js +103 -60
- package/dist/sql/mssql/index.js +182 -101
- package/dist/testing/TestLogger.js +39 -29
- package/dist/testing/index.js +2 -2
- package/dist/testing/utils.js +45 -23
- package/dist/x/cloudflare/CloudflareTunnel.js +61 -28
- package/dist/x/cloudflare/index.js +1 -1
- package/dist/x/tailscale/TailscaleTunnel.js +86 -60
- package/dist/x/tailscale/index.js +1 -1
- package/dist/x/tailwind/TailwindPlugin.js +280 -205
- package/dist/x/tailwind/compile.js +185 -129
- package/dist/x/tailwind/plugin.js +13 -11
- package/package.json +1 -1
- package/src/Development.ts +3 -1
- package/src/Entity.ts +17 -0
- package/src/Fetch.ts +271 -0
- package/src/SqlIntrospect.ts +64 -70
- package/src/Start.ts +24 -22
- package/src/StartApp.ts +11 -0
- package/src/bun/BunServer.ts +89 -16
- package/src/hyper/HyperHtml.ts +0 -1
- package/src/sql/bun/index.ts +1 -1
- package/src/sql/index.ts +1 -0
- package/src/sql/libsql/index.ts +173 -0
- package/src/sql/libsql/libsql.d.ts +39 -0
- package/src/sql/mssql/index.ts +1 -1
- package/src/x/tailscale/TailscaleTunnel.ts +7 -5
- package/dist/BlobStore.d.ts +0 -80
- package/dist/BlobStore.js +0 -19
- package/dist/ChildProcess.d.ts +0 -60
- package/dist/Commander.d.ts +0 -100
- package/dist/ContentNegotiation.d.ts +0 -12
- package/dist/Cookies.d.ts +0 -47
- package/dist/Development.d.ts +0 -39
- package/dist/Effectify.d.ts +0 -209
- package/dist/Entity.d.ts +0 -47
- package/dist/FilePathPattern.d.ts +0 -29
- package/dist/FileRouter.d.ts +0 -56
- package/dist/FileRouterCodegen.d.ts +0 -18
- package/dist/FileRouterPattern.d.ts +0 -9
- package/dist/FileRouterPattern.js +0 -35
- package/dist/FileSystem.d.ts +0 -158
- package/dist/Http.d.ts +0 -37
- package/dist/HttpAppExtra.d.ts +0 -7
- package/dist/HttpAppExtra.js +0 -320
- package/dist/HttpUtils.d.ts +0 -3
- package/dist/HttpUtils.js +0 -11
- package/dist/PathPattern.d.ts +0 -134
- package/dist/PlatformError.d.ts +0 -38
- package/dist/PlatformRuntime.d.ts +0 -27
- package/dist/Route.d.ts +0 -97
- package/dist/RouteBody.d.ts +0 -47
- package/dist/RouteError.d.ts +0 -98
- package/dist/RouteHook.d.ts +0 -12
- package/dist/RouteHttp.d.ts +0 -21
- package/dist/RouteHttpTracer.d.ts +0 -10
- package/dist/RouteMount.d.ts +0 -86
- package/dist/RouteSchema.d.ts +0 -86
- package/dist/RouteSse.d.ts +0 -21
- package/dist/RouteTree.d.ts +0 -57
- package/dist/RouteTrie.d.ts +0 -20
- package/dist/RouterPattern.d.ts +0 -118
- package/dist/RouterPattern.js +0 -269
- package/dist/SchemaExtra.d.ts +0 -7
- package/dist/Socket.d.ts +0 -27
- package/dist/Sql.d.ts +0 -34
- package/dist/Sql.js +0 -5
- package/dist/SqlIntrospect.d.ts +0 -91
- package/dist/Start.d.ts +0 -44
- package/dist/StartApp.d.ts +0 -19
- package/dist/StreamExtra.d.ts +0 -28
- package/dist/System.d.ts +0 -7
- package/dist/TuplePathPattern.d.ts +0 -9
- package/dist/Unique.d.ts +0 -50
- package/dist/Values.d.ts +0 -27
- package/dist/bun/BunBlobStoreDisk.d.ts +0 -6
- package/dist/bun/BunBlobStoreDisk.js +0 -116
- package/dist/bun/BunBlobStoreS3.d.ts +0 -11
- package/dist/bun/BunBlobStoreS3.js +0 -89
- package/dist/bun/BunBlobWatcherDisk.d.ts +0 -6
- package/dist/bun/BunBlobWatcherDisk.js +0 -60
- package/dist/bun/BunBlobWatcherQueue.d.ts +0 -6
- package/dist/bun/BunBlobWatcherQueue.js +0 -17
- package/dist/bun/BunBundle.d.ts +0 -11
- package/dist/bun/BunChildProcessSpawner.d.ts +0 -3
- package/dist/bun/BunHttpServer.d.ts +0 -44
- package/dist/bun/BunHttpServer.js +0 -186
- package/dist/bun/BunHttpServer_web.d.ts +0 -60
- package/dist/bun/BunHttpServer_web.js +0 -252
- package/dist/bun/BunImportTrackerPlugin.d.ts +0 -13
- package/dist/bun/BunPlatformHttpServer.d.ts +0 -10
- package/dist/bun/BunPlatformHttpServer.js +0 -53
- package/dist/bun/BunRoute.d.ts +0 -48
- package/dist/bun/BunRuntime.d.ts +0 -2
- package/dist/bun/BunServer.d.ts +0 -40
- package/dist/bun/BunServerRequest.d.ts +0 -60
- package/dist/bun/BunServerRequest.js +0 -252
- package/dist/bun/BunSql.d.ts +0 -4
- package/dist/bun/BunSql.js +0 -81
- package/dist/bun/BunVirtualFilesPlugin.d.ts +0 -4
- package/dist/bun/_BunEnhancedResolve.d.ts +0 -45
- package/dist/bun/index.d.ts +0 -5
- package/dist/bundler/Bundle.d.ts +0 -61
- package/dist/bundler/BundleFiles.d.ts +0 -13
- package/dist/bundler/BundleHttp.d.ts +0 -45
- package/dist/bundler/BundleHttp.js +0 -176
- package/dist/bundler/BundleRoute.d.ts +0 -27
- package/dist/client/Overlay.d.ts +0 -2
- package/dist/client/ScrollState.d.ts +0 -6
- package/dist/client/index.d.ts +0 -6
- package/dist/console/Console.d.ts +0 -6
- package/dist/console/ConsoleErrors.d.ts +0 -3
- package/dist/console/ConsoleLogger.d.ts +0 -3
- package/dist/console/ConsoleMetrics.d.ts +0 -3
- package/dist/console/ConsoleProcess.d.ts +0 -3
- package/dist/console/ConsoleStore.d.ts +0 -144
- package/dist/console/ConsoleTracer.d.ts +0 -3
- package/dist/console/Simulation.d.ts +0 -2
- package/dist/console/index.d.ts +0 -3
- package/dist/console/routes/errors/route.d.ts +0 -10
- package/dist/console/routes/errors/route.js +0 -47
- package/dist/console/routes/fiberDetail.d.ts +0 -16
- package/dist/console/routes/fiberDetail.js +0 -38
- package/dist/console/routes/fibers/route.d.ts +0 -10
- package/dist/console/routes/fibers/route.js +0 -19
- package/dist/console/routes/git/route.d.ts +0 -11
- package/dist/console/routes/git/route.js +0 -33
- package/dist/console/routes/layout.d.ts +0 -9
- package/dist/console/routes/layout.js +0 -3
- package/dist/console/routes/logs/route.d.ts +0 -10
- package/dist/console/routes/logs/route.js +0 -32
- package/dist/console/routes/metrics/route.d.ts +0 -10
- package/dist/console/routes/metrics/route.js +0 -17
- package/dist/console/routes/route.d.ts +0 -6
- package/dist/console/routes/route.js +0 -5
- package/dist/console/routes/routes/route.d.ts +0 -6
- package/dist/console/routes/routes/route.js +0 -20
- package/dist/console/routes/services/route.d.ts +0 -6
- package/dist/console/routes/services/route.js +0 -12
- package/dist/console/routes/system/route.d.ts +0 -10
- package/dist/console/routes/system/route.js +0 -18
- package/dist/console/routes/traceDetail.d.ts +0 -16
- package/dist/console/routes/traceDetail.js +0 -14
- package/dist/console/routes/traces/route.d.ts +0 -10
- package/dist/console/routes/traces/route.js +0 -39
- package/dist/console/routes/tree.d.ts +0 -153
- package/dist/console/ui/Errors.d.ts +0 -4
- package/dist/console/ui/Errors.js +0 -15
- package/dist/console/ui/Fibers.d.ts +0 -24
- package/dist/console/ui/Fibers.js +0 -121
- package/dist/console/ui/Git.d.ts +0 -20
- package/dist/console/ui/Git.js +0 -95
- package/dist/console/ui/Logs.d.ts +0 -4
- package/dist/console/ui/Logs.js +0 -25
- package/dist/console/ui/Metrics.d.ts +0 -4
- package/dist/console/ui/Metrics.js +0 -26
- package/dist/console/ui/Routes.d.ts +0 -8
- package/dist/console/ui/Routes.js +0 -70
- package/dist/console/ui/Services.d.ts +0 -10
- package/dist/console/ui/Services.js +0 -246
- package/dist/console/ui/Shell.d.ts +0 -10
- package/dist/console/ui/Shell.js +0 -7
- package/dist/console/ui/System.d.ts +0 -4
- package/dist/console/ui/System.js +0 -35
- package/dist/console/ui/Traces.d.ts +0 -12
- package/dist/console/ui/Traces.js +0 -179
- package/dist/datastar/actions/fetch.d.ts +0 -30
- package/dist/datastar/actions/peek.d.ts +0 -1
- package/dist/datastar/actions/setAll.d.ts +0 -1
- package/dist/datastar/actions/toggleAll.d.ts +0 -1
- package/dist/datastar/attributes/attr.d.ts +0 -1
- package/dist/datastar/attributes/bind.d.ts +0 -1
- package/dist/datastar/attributes/class.d.ts +0 -1
- package/dist/datastar/attributes/computed.d.ts +0 -1
- package/dist/datastar/attributes/effect.d.ts +0 -1
- package/dist/datastar/attributes/indicator.d.ts +0 -1
- package/dist/datastar/attributes/init.d.ts +0 -1
- package/dist/datastar/attributes/jsonSignals.d.ts +0 -1
- package/dist/datastar/attributes/on.d.ts +0 -1
- package/dist/datastar/attributes/onIntersect.d.ts +0 -1
- package/dist/datastar/attributes/onInterval.d.ts +0 -1
- package/dist/datastar/attributes/onSignalPatch.d.ts +0 -1
- package/dist/datastar/attributes/ref.d.ts +0 -1
- package/dist/datastar/attributes/show.d.ts +0 -1
- package/dist/datastar/attributes/signals.d.ts +0 -1
- package/dist/datastar/attributes/style.d.ts +0 -1
- package/dist/datastar/attributes/text.d.ts +0 -1
- package/dist/datastar/engine.d.ts +0 -162
- package/dist/datastar/happydom.d.ts +0 -1
- package/dist/datastar/happydom.js +0 -8
- package/dist/datastar/index.d.ts +0 -24
- package/dist/datastar/load.d.ts +0 -24
- package/dist/datastar/load.js +0 -24
- package/dist/datastar/utils.d.ts +0 -51
- package/dist/datastar/watchers/patchElements.d.ts +0 -1
- package/dist/datastar/watchers/patchSignals.d.ts +0 -1
- package/dist/experimental/EncryptedCookies.d.ts +0 -48
- package/dist/experimental/SseHttpResponse.d.ts +0 -7
- package/dist/experimental/SseHttpResponse.js +0 -28
- package/dist/experimental/index.d.ts +0 -1
- package/dist/hyper/Hyper.d.ts +0 -25
- package/dist/hyper/HyperHtml.d.ts +0 -23
- package/dist/hyper/HyperHtml.test.d.ts +0 -1
- package/dist/hyper/HyperHtml.test.js +0 -197
- package/dist/hyper/HyperNode.d.ts +0 -14
- package/dist/hyper/HyperRoute.d.ts +0 -8
- package/dist/hyper/HyperRoute.test.d.ts +0 -1
- package/dist/hyper/HyperRoute.test.js +0 -83
- package/dist/hyper/html.d.ts +0 -11
- package/dist/hyper/index.d.ts +0 -6
- package/dist/hyper/jsx-runtime.d.ts +0 -7
- package/dist/index.d.ts +0 -8
- package/dist/inference_check.d.ts +0 -1
- package/dist/inference_check.js +0 -15
- package/dist/lint/plugin.d.ts +0 -86
- package/dist/lint/plugin.js +0 -341
- package/dist/middlewares/BasicAuthMiddleware.d.ts +0 -8
- package/dist/middlewares/BasicAuthMiddleware.js +0 -22
- package/dist/middlewares/index.d.ts +0 -1
- package/dist/middlewares/index.js +0 -1
- package/dist/node/Effectify.d.ts +0 -209
- package/dist/node/Effectify.js +0 -19
- package/dist/node/FileSystem.d.ts +0 -7
- package/dist/node/FileSystem.js +0 -420
- package/dist/node/NodeFileSystem.d.ts +0 -7
- package/dist/node/NodeUtils.d.ts +0 -2
- package/dist/node/PlatformError.d.ts +0 -46
- package/dist/node/PlatformError.js +0 -43
- package/dist/node/Utils.d.ts +0 -1
- package/dist/node/Utils.js +0 -19
- package/dist/repro_fail.d.ts +0 -1
- package/dist/repro_fail.js +0 -14
- package/dist/sql/bun/index.d.ts +0 -3
- package/dist/sql/mssql/docker.d.ts +0 -2
- package/dist/sql/mssql/index.d.ts +0 -21
- package/dist/testing/TestHttpClient.d.ts +0 -13
- package/dist/testing/TestHttpClient.js +0 -68
- package/dist/testing/TestLogger.d.ts +0 -13
- package/dist/testing/index.d.ts +0 -2
- package/dist/testing/utils.d.ts +0 -9
- package/dist/x/cloudflare/CloudflareTunnel.d.ts +0 -10
- package/dist/x/cloudflare/index.d.ts +0 -1
- package/dist/x/datastar/Datastar.d.ts +0 -6
- package/dist/x/datastar/Datastar.js +0 -47
- package/dist/x/datastar/index.d.ts +0 -1
- package/dist/x/datastar/index.js +0 -1
- package/dist/x/tailscale/TailscaleTunnel.d.ts +0 -15
- package/dist/x/tailscale/index.d.ts +0 -1
- package/dist/x/tailwind/TailwindPlugin.d.ts +0 -23
- package/dist/x/tailwind/compile.d.ts +0 -19
- package/dist/x/tailwind/plugin.d.ts +0 -2
- /package/src/{Sql.ts → sql/Sql.ts} +0 -0
|
@@ -1,403 +1,536 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
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 (
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
-
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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
|
-
|
|
132
|
-
|
|
133
|
-
|
|
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
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
-
|
|
145
|
-
createHttpMethod("
|
|
146
|
-
createHttpMethod("
|
|
147
|
-
createHttpMethod("
|
|
148
|
-
createHttpMethod("
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
export const
|
|
152
|
-
export const
|
|
153
|
-
export const
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
const
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
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
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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
|
-
|
|
178
|
-
|
|
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
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
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 (
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
408
|
+
if (v) argsRaw[n] = v
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
dispatchFetch(dispatchType, el, argsRaw)
|
|
412
|
+
dispose()
|
|
413
|
+
resolve()
|
|
209
414
|
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
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
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
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
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
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
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
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
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
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
|
-
|
|
402
|
-
|
|
403
|
-
}
|
|
526
|
+
} catch (innerErr) {
|
|
527
|
+
dispose()
|
|
528
|
+
reject(innerErr)
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
create()
|
|
535
|
+
})
|
|
536
|
+
}
|