@teyik0/furin 0.5.0-alpha.2 → 0.7.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapter/runtime-build.d.ts +3 -1
- package/dist/build/client.d.ts +4 -1
- package/dist/build/entry-template.d.ts +5 -0
- package/dist/build/entry-template.ts +9 -0
- package/dist/build/hydrate.d.ts +1 -1
- package/dist/build/index.js +9589 -6685
- package/dist/build/preload-manifest.d.ts +17 -0
- package/dist/build/request-keys.d.ts +4 -0
- package/dist/build/types.d.ts +6 -0
- package/dist/cli/index.js +9539 -6644
- package/dist/client/browser-events-runtime.d.ts +2 -1
- package/dist/client/client-module.d.ts +26 -0
- package/dist/client/define-route.d.ts +9 -1
- package/dist/client/dev-error-overlay.ts +1 -0
- package/dist/client/document.d.ts +3 -0
- package/dist/client/hmr.d.ts +1 -1
- package/dist/client/internal/runtime-types.d.ts +2 -0
- package/dist/client/link.js +1011 -157
- package/dist/client/mutation.d.ts +38 -0
- package/dist/client/query-store.d.ts +85 -0
- package/dist/client/query.d.ts +14 -0
- package/dist/client/route-api.d.ts +11 -0
- package/dist/client/router/context.d.ts +3 -2
- package/dist/client/router/index.d.ts +1 -1
- package/dist/client/router/link-utils.d.ts +8 -1
- package/dist/client/router/navigation.d.ts +2 -9
- package/dist/client/router/optimistic.d.ts +41 -0
- package/dist/client/router/search/index.d.ts +3 -3
- package/dist/client/router/search-store.d.ts +1 -0
- package/dist/client/router/sync-catch-up.d.ts +3 -0
- package/dist/client/router/types.d.ts +49 -7
- package/dist/client/sync.d.ts +39 -22
- package/dist/client.d.ts +11 -33
- package/dist/client.js +1222 -57
- package/dist/config.d.ts +7 -0
- package/dist/config.js +1 -0
- package/dist/define-route.d.ts +231 -123
- package/dist/env.d.ts +2 -2
- package/dist/furin.d.ts +4 -2
- package/dist/furin.js +9717 -6796
- package/dist/plugin/hmr-dependencies.d.ts +2 -0
- package/dist/plugin/index.js +811 -145
- package/dist/plugin/transform-client-module.d.ts +16 -0
- package/dist/plugin/transform-client.d.ts +2 -0
- package/dist/plugin/transform-dev-page.d.ts +7 -0
- package/dist/plugin/transform-isomorphic.d.ts +1 -1
- package/dist/routes.d.ts +6 -0
- package/dist/rsc-client.js +87 -16
- package/dist/rsc.js +87 -16
- package/dist/server/auto-invalidate/registry.d.ts +5 -2
- package/dist/server/auto-invalidate/types.d.ts +2 -0
- package/dist/server/browser-events/plugin.d.ts +3 -0
- package/dist/server/cache/isr.d.ts +2 -4
- package/dist/server/cache/route-cache.d.ts +5 -0
- package/dist/server/dev/graph.d.ts +1 -0
- package/dist/server/dev/runtime.production.d.ts +1 -0
- package/dist/server/dev-page-plugin.d.ts +6 -19
- package/dist/server/internal.d.ts +5 -0
- package/dist/server/internal.js +105 -57
- package/dist/server/production-server.js +104 -56
- package/dist/server/render/assemble.d.ts +6 -6
- package/dist/server/render/document.d.ts +1 -1
- package/dist/server/render/element.d.ts +1 -1
- package/dist/server/render/loaders.d.ts +15 -5
- package/dist/server/render/mixed-cache.d.ts +6 -0
- package/dist/server/render/react-stream.d.ts +3 -0
- package/dist/server/render/ssr.d.ts +5 -4
- package/dist/server/render/template.d.ts +4 -0
- package/dist/server/router/defined-route.d.ts +2 -0
- package/dist/server/router/discovery.d.ts +1 -0
- package/dist/server/router/hmr.d.ts +1 -1
- package/dist/server/router/patterns.d.ts +3 -0
- package/dist/server/router/plugin.d.ts +3 -0
- package/dist/server/router/types.d.ts +4 -0
- package/dist/server/security/csp.d.ts +12 -0
- package/dist/server/sync/adapter.d.ts +25 -4
- package/dist/server/sync/atomic.d.ts +25 -0
- package/dist/server/sync/drizzle/index.d.ts +45 -0
- package/dist/server/sync/drizzle/index.js +540 -0
- package/dist/server/sync/execution-error.d.ts +2 -0
- package/dist/server/sync/index.d.ts +2 -1
- package/dist/server/sync/index.js +580 -124
- package/dist/server/sync/mutation-handler.d.ts +4 -0
- package/dist/server/sync/plugin.d.ts +34 -11
- package/dist/server/sync/postgres/execute.d.ts +3 -0
- package/dist/server/sync/postgres/index.d.ts +12 -1
- package/dist/server/sync/postgres/index.js +42 -13
- package/dist/server/sync/postgres/sql.d.ts +8 -0
- package/dist/server/sync/prisma/index.d.ts +24 -0
- package/dist/server/sync/prisma/index.js +340 -0
- package/dist/server/sync/queries.d.ts +21 -0
- package/dist/server/sync/query-context.d.ts +5 -0
- package/dist/server/sync/redis/index.js +3 -3
- package/dist/server/sync/redis/scripts.d.ts +1 -1
- package/dist/server/sync/response.d.ts +1 -0
- package/dist/server/sync/sqlite/index.d.ts +3 -0
- package/dist/server/sync/sqlite/index.js +10 -4
- package/dist/server/sync/stream.d.ts +2 -5
- package/dist/server/sync/validation.d.ts +4 -0
- package/dist/shared/defer.d.ts +31 -0
- package/dist/shared/error.d.ts +2 -2
- package/dist/shared/page-key.d.ts +7 -0
- package/dist/shared/query-bindings.d.ts +12 -0
- package/dist/shared/route-frame.d.ts +1 -0
- package/dist/shared/sync-query.d.ts +40 -0
- package/package.json +40 -16
- package/src/adapter/README.md +3 -0
- package/src/adapter/bun.ts +3 -0
- package/src/adapter/package.ts +16 -2
- package/src/adapter/runtime-build.ts +99 -14
- package/src/adapter/static.ts +17 -6
- package/src/adapter/vercel.ts +33 -23
- package/src/build/client.ts +23 -5
- package/src/build/entry-template.ts +9 -0
- package/src/build/hydrate.ts +18 -3
- package/src/build/preload-manifest.ts +100 -0
- package/src/build/production-instrumentation.ts +6 -3
- package/src/build/request-keys.ts +93 -0
- package/src/build/ssg-cache.ts +9 -4
- package/src/build/types.ts +4 -0
- package/src/cli/index.ts +1 -0
- package/src/client/boundaries.tsx +1 -1
- package/src/client/browser-events-runtime.ts +73 -16
- package/src/client/client-module.ts +57 -0
- package/src/client/define-route.ts +11 -2
- package/src/client/dev-error-overlay.ts +1 -0
- package/src/client/document.tsx +47 -20
- package/src/client/hmr.ts +4 -2
- package/src/client/internal/runtime-types.ts +2 -0
- package/src/client/link.tsx +8 -8
- package/src/client/mutation.ts +136 -0
- package/src/client/query-store.ts +418 -0
- package/src/client/query.tsx +67 -0
- package/src/client/route-api.tsx +75 -0
- package/src/client/router/boundary-tree.tsx +10 -6
- package/src/client/router/context.ts +57 -4
- package/src/client/router/index.ts +3 -0
- package/src/client/router/link-utils.ts +29 -1
- package/src/client/router/navigation.ts +3 -50
- package/src/client/router/optimistic.ts +233 -0
- package/src/client/router/provider.tsx +404 -96
- package/src/client/router/search/index.ts +37 -17
- package/src/client/router/search-store.ts +4 -0
- package/src/client/router/sync-catch-up.ts +7 -0
- package/src/client/router/types.ts +71 -9
- package/src/client/sync.ts +438 -65
- package/src/client.ts +20 -60
- package/src/config.ts +5 -0
- package/src/define-route.ts +442 -155
- package/src/env.d.ts +2 -2
- package/src/furin.ts +213 -38
- package/src/plugin/hmr-dependencies.ts +58 -0
- package/src/plugin/index.ts +38 -2
- package/src/plugin/route-config-autofix.ts +2 -2
- package/src/plugin/routes.ts +19 -8
- package/src/plugin/transform-client-module.ts +123 -0
- package/src/plugin/transform-client.ts +293 -13
- package/src/plugin/transform-dev-page.ts +231 -0
- package/src/plugin/transform-isomorphic.ts +11 -5
- package/src/routes.ts +8 -0
- package/src/rsc/shared.tsx +104 -18
- package/src/rsc/version.ts +5 -0
- package/src/server/auto-invalidate/registry.ts +56 -24
- package/src/server/auto-invalidate/runtime.ts +17 -0
- package/src/server/auto-invalidate/types.ts +6 -0
- package/src/server/browser-events/plugin.ts +112 -1
- package/src/server/cache/dev-loader.ts +1 -1
- package/src/server/cache/invalidation.ts +2 -0
- package/src/server/cache/isr-ssg.ts +1 -1
- package/src/server/cache/isr.ts +6 -34
- package/src/server/cache/route-cache.ts +40 -4
- package/src/server/cache/ssg.ts +1 -1
- package/src/server/dev/diagnostics.ts +4 -2
- package/src/server/dev/error.ts +2 -1
- package/src/server/dev/graph.ts +70 -7
- package/src/server/dev/runtime.production.ts +4 -0
- package/src/server/dev/symbolicate.ts +2 -0
- package/src/server/dev-page-plugin.ts +160 -63
- package/src/server/evlog.ts +1 -1
- package/src/server/internal.ts +5 -0
- package/src/server/render/assemble.ts +15 -11
- package/src/server/render/document.tsx +13 -2
- package/src/server/render/element.tsx +11 -6
- package/src/server/render/isr.ts +71 -33
- package/src/server/render/loaders.ts +328 -70
- package/src/server/render/mixed-cache.ts +327 -0
- package/src/server/render/not-found.ts +14 -11
- package/src/server/render/ppr-document.ts +47 -11
- package/src/server/render/ppr-route.ts +54 -20
- package/src/server/render/react-stream.ts +17 -0
- package/src/server/render/route-frame-transport.ts +3 -0
- package/src/server/render/ssg.ts +53 -19
- package/src/server/render/ssr.ts +105 -36
- package/src/server/render/static-params.ts +1 -1
- package/src/server/render/template.ts +23 -0
- package/src/server/router/defined-route.ts +3 -0
- package/src/server/router/discovery.ts +14 -0
- package/src/server/router/hmr.ts +109 -18
- package/src/server/router/patterns.ts +27 -0
- package/src/server/router/plugin.ts +94 -19
- package/src/server/router/source-version.ts +20 -2
- package/src/server/router/types.ts +4 -0
- package/src/server/security/csp.ts +44 -0
- package/src/server/sync/adapter.ts +41 -4
- package/src/server/sync/atomic.ts +303 -0
- package/src/server/sync/drizzle/index.ts +145 -0
- package/src/server/sync/execution-error.ts +1 -0
- package/src/server/sync/fingerprint.ts +2 -6
- package/src/server/sync/index.ts +6 -0
- package/src/server/sync/mutation-handler.ts +69 -0
- package/src/server/sync/plugin.ts +177 -60
- package/src/server/sync/postgres/execute.ts +20 -0
- package/src/server/sync/postgres/index.ts +60 -16
- package/src/server/sync/postgres/sql.ts +20 -0
- package/src/server/sync/prisma/index.ts +70 -0
- package/src/server/sync/queries.ts +75 -0
- package/src/server/sync/query-context.ts +85 -0
- package/src/server/sync/redis/index.ts +7 -3
- package/src/server/sync/redis/scripts.ts +1 -1
- package/src/server/sync/response.ts +21 -0
- package/src/server/sync/sqlite/index.ts +19 -8
- package/src/server/sync/stream.ts +14 -22
- package/src/server/sync/validation.ts +34 -0
- package/src/shared/defer.ts +48 -0
- package/src/shared/error.ts +2 -2
- package/src/shared/page-key.ts +36 -0
- package/src/shared/query-bindings.ts +138 -0
- package/src/shared/route-frame.ts +13 -1
- package/src/shared/route-map.ts +12 -2
- package/src/shared/sync-query.ts +138 -0
package/dist/client.js
CHANGED
|
@@ -1,7 +1,33 @@
|
|
|
1
1
|
// @bun
|
|
2
|
+
// src/client/client-module.ts
|
|
3
|
+
import { preloadModule } from "react-dom";
|
|
4
|
+
var resolveModuleHrefs = () => [];
|
|
5
|
+
var resolveNonce = () => {
|
|
6
|
+
return;
|
|
7
|
+
};
|
|
8
|
+
function clientModule(load, build) {
|
|
9
|
+
return {
|
|
10
|
+
get hrefs() {
|
|
11
|
+
if (build === undefined) {
|
|
12
|
+
return [];
|
|
13
|
+
}
|
|
14
|
+
return typeof build === "string" ? resolveModuleHrefs(build) : build;
|
|
15
|
+
},
|
|
16
|
+
load
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function preloadClientModule(mod) {
|
|
20
|
+
const nonce = resolveNonce();
|
|
21
|
+
for (const href of mod.hrefs) {
|
|
22
|
+
preloadModule(href, { as: "script", nonce });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
2
25
|
// src/client/define-route.ts
|
|
3
|
-
function
|
|
26
|
+
function createRouteBuilder(remountDeps) {
|
|
4
27
|
return {
|
|
28
|
+
config(options) {
|
|
29
|
+
return createRouteBuilder(options.remountDeps);
|
|
30
|
+
},
|
|
5
31
|
layout(component) {
|
|
6
32
|
const runtimeComponent = component;
|
|
7
33
|
return {
|
|
@@ -15,16 +41,21 @@ function defineRoute() {
|
|
|
15
41
|
return {
|
|
16
42
|
__type: "FURIN_ROUTE",
|
|
17
43
|
component: runtimeComponent,
|
|
18
|
-
page: runtimeComponent
|
|
44
|
+
page: runtimeComponent,
|
|
45
|
+
remountDeps
|
|
19
46
|
};
|
|
20
47
|
}
|
|
21
48
|
};
|
|
22
49
|
}
|
|
50
|
+
function defineRoute() {
|
|
51
|
+
return createRouteBuilder(undefined);
|
|
52
|
+
}
|
|
23
53
|
function defineRootRoute() {
|
|
24
54
|
return defineRoute();
|
|
25
55
|
}
|
|
26
56
|
// src/client/document.tsx
|
|
27
57
|
import { createContext, createElement, useContext } from "react";
|
|
58
|
+
import { preloadModule as preloadModule2 } from "react-dom";
|
|
28
59
|
import { jsxDEV, Fragment } from "react/jsx-dev-runtime";
|
|
29
60
|
var DocumentContext = createContext(null);
|
|
30
61
|
function serializeJson(value) {
|
|
@@ -39,34 +70,45 @@ function DocumentProvider({
|
|
|
39
70
|
children
|
|
40
71
|
}, undefined, false, undefined, this);
|
|
41
72
|
}
|
|
42
|
-
function renderMeta(meta,
|
|
73
|
+
function renderMeta(meta, key) {
|
|
43
74
|
if ("title" in meta) {
|
|
44
75
|
return /* @__PURE__ */ jsxDEV("title", {
|
|
45
76
|
children: meta.title
|
|
46
|
-
},
|
|
77
|
+
}, key, false, undefined, this);
|
|
47
78
|
}
|
|
48
79
|
if ("script:ld+json" in meta) {
|
|
49
80
|
return /* @__PURE__ */ jsxDEV("script", {
|
|
50
81
|
dangerouslySetInnerHTML: { __html: serializeJson(meta["script:ld+json"]) },
|
|
51
82
|
type: "application/ld+json"
|
|
52
|
-
},
|
|
83
|
+
}, key, false, undefined, this);
|
|
53
84
|
}
|
|
54
85
|
if ("tagName" in meta) {
|
|
55
86
|
const { tagName, ...attributes } = meta;
|
|
56
|
-
return createElement(tagName, { ...attributes, key
|
|
87
|
+
return createElement(tagName, { ...attributes, key });
|
|
57
88
|
}
|
|
58
|
-
return createElement("meta", { ...meta, key
|
|
89
|
+
return createElement("meta", { ...meta, key });
|
|
59
90
|
}
|
|
60
91
|
function HeadContent() {
|
|
61
92
|
const state = useContext(DocumentContext);
|
|
62
93
|
if (state === null) {
|
|
63
94
|
throw new Error("[furin] <HeadContent /> must be rendered inside the root layout.");
|
|
64
95
|
}
|
|
96
|
+
for (const href of state.assets.modulePreloads ?? []) {
|
|
97
|
+
preloadModule2(href, { as: "script", nonce: state.nonce });
|
|
98
|
+
}
|
|
99
|
+
const occurrences = new Map;
|
|
100
|
+
const descriptorKey = (descriptor) => {
|
|
101
|
+
const identity = JSON.stringify(descriptor);
|
|
102
|
+
const occurrence = occurrences.get(identity) ?? 0;
|
|
103
|
+
occurrences.set(identity, occurrence + 1);
|
|
104
|
+
return `${identity}:${occurrence}`;
|
|
105
|
+
};
|
|
65
106
|
return /* @__PURE__ */ jsxDEV(Fragment, {
|
|
66
107
|
children: [
|
|
67
108
|
state.assets.extensionErrorFilterScript === undefined ? null : /* @__PURE__ */ jsxDEV("script", {
|
|
68
109
|
dangerouslySetInnerHTML: { __html: state.assets.extensionErrorFilterScript },
|
|
69
|
-
"data-furin-extension-error-filter": ""
|
|
110
|
+
"data-furin-extension-error-filter": "",
|
|
111
|
+
nonce: state.nonce
|
|
70
112
|
}, undefined, false, undefined, this),
|
|
71
113
|
/* @__PURE__ */ jsxDEV("meta", {
|
|
72
114
|
charSet: "utf-8",
|
|
@@ -93,18 +135,20 @@ function HeadContent() {
|
|
|
93
135
|
href,
|
|
94
136
|
rel: "stylesheet"
|
|
95
137
|
}, href, false, undefined, this)),
|
|
96
|
-
state.head?.meta?.map(renderMeta),
|
|
97
|
-
state.head?.links?.map((link
|
|
138
|
+
state.head?.meta?.map((meta) => renderMeta(meta, descriptorKey(meta))),
|
|
139
|
+
state.head?.links?.map((link) => /* @__PURE__ */ jsxDEV("link", {
|
|
98
140
|
...link
|
|
99
|
-
},
|
|
100
|
-
state.head?.scripts?.map(({ children, ...attributes }
|
|
141
|
+
}, descriptorKey(link), false, undefined, this)),
|
|
142
|
+
state.head?.scripts?.map(({ children, ...attributes }) => /* @__PURE__ */ jsxDEV("script", {
|
|
101
143
|
...attributes,
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
144
|
+
nonce: state.nonce ?? attributes.nonce,
|
|
145
|
+
children
|
|
146
|
+
}, descriptorKey({ children, ...attributes }), false, undefined, this)),
|
|
147
|
+
state.head?.styles?.map(({ children, type }) => /* @__PURE__ */ jsxDEV("style", {
|
|
105
148
|
dangerouslySetInnerHTML: { __html: children },
|
|
149
|
+
nonce: state.nonce,
|
|
106
150
|
type
|
|
107
|
-
},
|
|
151
|
+
}, descriptorKey({ children, type }), false, undefined, this))
|
|
108
152
|
]
|
|
109
153
|
}, undefined, true, undefined, this);
|
|
110
154
|
}
|
|
@@ -141,12 +185,14 @@ function Scripts() {
|
|
|
141
185
|
state.assets.frameworkModules.map((src) => /* @__PURE__ */ jsxDEV("script", {
|
|
142
186
|
crossOrigin: "",
|
|
143
187
|
"data-furin-framework-module": "",
|
|
188
|
+
nonce: state.nonce,
|
|
144
189
|
src,
|
|
145
190
|
type: "module"
|
|
146
191
|
}, src, false, undefined, this)),
|
|
147
192
|
state.assets.entryModule === undefined ? null : /* @__PURE__ */ jsxDEV("script", {
|
|
148
193
|
crossOrigin: "",
|
|
149
194
|
"data-furin-entry": "",
|
|
195
|
+
nonce: state.nonce,
|
|
150
196
|
src: state.assets.entryModule,
|
|
151
197
|
type: "module"
|
|
152
198
|
}, undefined, false, undefined, this)
|
|
@@ -154,7 +200,7 @@ function Scripts() {
|
|
|
154
200
|
}, undefined, true, undefined, this);
|
|
155
201
|
}
|
|
156
202
|
// src/client/hmr.ts
|
|
157
|
-
import {
|
|
203
|
+
import { jsx } from "react/jsx-runtime";
|
|
158
204
|
var HOT_COMPONENT_SIGNATURE = Symbol.for("furin.hmr.hook-signature");
|
|
159
205
|
function readHookSignature(component) {
|
|
160
206
|
const signed = component;
|
|
@@ -189,57 +235,1171 @@ function updateHotComponent(registry, key, component) {
|
|
|
189
235
|
boundary: (props) => slot.current(props),
|
|
190
236
|
current: component,
|
|
191
237
|
signature: readHookSignature(component),
|
|
192
|
-
stable: (props) =>
|
|
238
|
+
stable: (props) => jsx(slot.boundary, { ...props })
|
|
193
239
|
};
|
|
194
240
|
registry.set(key, slot);
|
|
195
241
|
return slot.stable;
|
|
196
242
|
}
|
|
243
|
+
// ../../node_modules/.bun/@elysia+eden@2.0.0-beta.5+211b202b1e8e6104/node_modules/@elysia/eden/dist/chunk-BCTQASJ2.mjs
|
|
244
|
+
var d = class extends Error {
|
|
245
|
+
constructor(e, s) {
|
|
246
|
+
super(typeof s == "object" ? JSON.stringify(s) : s + "");
|
|
247
|
+
this.status = e;
|
|
248
|
+
this.value = s;
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
var a = /(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/;
|
|
252
|
+
var o = /(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)\s(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s\d{2}\s\d{4}\s\d{2}:\d{2}:\d{2}\sGMT(?:\+|-)\d{4}\s\([^)]+\)/;
|
|
253
|
+
var c = /^(?:(?:(?:(?:0?[1-9]|[12][0-9]|3[01])[/\s-](?:0?[1-9]|1[0-2])[/\s-](?:19|20)\d{2})|(?:(?:19|20)\d{2}[/\s-](?:0?[1-9]|1[0-2])[/\s-](?:0?[1-9]|[12][0-9]|3[01]))))(?:\s(?:1[012]|0?[1-9]):[0-5][0-9](?::[0-5][0-9])?(?:\s[AP]M)?)?$/;
|
|
254
|
+
var u = (t) => t.trim().length !== 0 && !Number.isNaN(Number(t));
|
|
255
|
+
var i = (t, r) => {
|
|
256
|
+
if (typeof t != "string" || r?.parseDate === false)
|
|
257
|
+
return null;
|
|
258
|
+
let e = t.replace(/"/g, "");
|
|
259
|
+
if (a.test(e) || o.test(e) || c.test(e)) {
|
|
260
|
+
let s = new Date(e);
|
|
261
|
+
if (!Number.isNaN(s.getTime()))
|
|
262
|
+
return s;
|
|
263
|
+
}
|
|
264
|
+
return null;
|
|
265
|
+
};
|
|
266
|
+
var p = (t) => {
|
|
267
|
+
let r = t.charCodeAt(0), e = t.charCodeAt(t.length - 1);
|
|
268
|
+
return r === 123 && e === 125 || r === 91 && e === 93;
|
|
269
|
+
};
|
|
270
|
+
var f = (t, r) => JSON.parse(t, (e, s) => {
|
|
271
|
+
let n = i(s, r);
|
|
272
|
+
return n || s;
|
|
273
|
+
});
|
|
274
|
+
var g = (t, r) => {
|
|
275
|
+
if (!t)
|
|
276
|
+
return t;
|
|
277
|
+
if (u(t))
|
|
278
|
+
return +t;
|
|
279
|
+
if (t === "true")
|
|
280
|
+
return true;
|
|
281
|
+
if (t === "false")
|
|
282
|
+
return false;
|
|
283
|
+
if (r?.parseDate !== false) {
|
|
284
|
+
let e = i(t, r);
|
|
285
|
+
if (e)
|
|
286
|
+
return e;
|
|
287
|
+
}
|
|
288
|
+
if (p(t))
|
|
289
|
+
try {
|
|
290
|
+
return f(t, r);
|
|
291
|
+
} catch {}
|
|
292
|
+
return t;
|
|
293
|
+
};
|
|
294
|
+
var b = (t, r) => {
|
|
295
|
+
let e = t.data.toString();
|
|
296
|
+
return e === "null" ? null : g(e, r);
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
// ../../node_modules/.bun/@elysia+eden@2.0.0-beta.5+211b202b1e8e6104/node_modules/@elysia/eden/dist/chunk-LU6DO2U6.mjs
|
|
300
|
+
var M = class {
|
|
301
|
+
constructor(t) {
|
|
302
|
+
this.url = t;
|
|
303
|
+
this.ws = new WebSocket(t);
|
|
304
|
+
}
|
|
305
|
+
ws;
|
|
306
|
+
send(t) {
|
|
307
|
+
return Array.isArray(t) ? (t.forEach((n) => this.send(n)), this) : (this.ws.send(typeof t == "object" ? JSON.stringify(t) : t.toString()), this);
|
|
308
|
+
}
|
|
309
|
+
on(t, n, r) {
|
|
310
|
+
return this.addEventListener(t, n, r);
|
|
311
|
+
}
|
|
312
|
+
off(t, n, r) {
|
|
313
|
+
return this.ws.removeEventListener(t, n, r), this;
|
|
314
|
+
}
|
|
315
|
+
subscribe(t, n) {
|
|
316
|
+
return this.addEventListener("message", t, n);
|
|
317
|
+
}
|
|
318
|
+
addEventListener(t, n, r) {
|
|
319
|
+
return this.ws.addEventListener(t, (a) => {
|
|
320
|
+
if (t === "message") {
|
|
321
|
+
let i = b(a);
|
|
322
|
+
n({ ...a, data: i });
|
|
323
|
+
} else
|
|
324
|
+
n(a);
|
|
325
|
+
}, r), this;
|
|
326
|
+
}
|
|
327
|
+
removeEventListener(t, n, r) {
|
|
328
|
+
return this.off(t, n, r), this;
|
|
329
|
+
}
|
|
330
|
+
close() {
|
|
331
|
+
return this.ws.close(), this;
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
var ee = ["get", "post", "put", "delete", "patch", "options", "head", "connect", "subscribe"];
|
|
335
|
+
var ce = [...ee, "use", "~path", "then", "catch", "finally"];
|
|
336
|
+
var le = ["post", "put", "patch", "delete"];
|
|
337
|
+
var fe = (e, t) => {
|
|
338
|
+
if (!e?.name || !e.verbs && !e.before && !e.after)
|
|
339
|
+
throw new Error('Eden Treaty plugin must have a "name" and a "verbs" record or a call callback');
|
|
340
|
+
let n = { verbs: { ...t?.verbs }, beforeCall: t ? [...t.beforeCall] : [], afterCall: t ? [...t.afterCall] : [] };
|
|
341
|
+
for (let r of Object.keys(e.verbs ?? {})) {
|
|
342
|
+
if (ce.includes(r))
|
|
343
|
+
throw new Error(`Eden Treaty plugin "${e.name}" cannot register verb "${r}" because it is reserved by Eden Treaty`);
|
|
344
|
+
if (Object.hasOwn(n.verbs, r))
|
|
345
|
+
throw new Error(`Eden Treaty plugin "${e.name}" cannot register verb "${r}" because it is already registered by plugin "${n.verbs[r].plugin}"`);
|
|
346
|
+
n.verbs[r] = { plugin: e.name, handler: e.verbs[r] };
|
|
347
|
+
}
|
|
348
|
+
return e.before && n.beforeCall.push(e.before), e.after && n.afterCall.push(e.after), n;
|
|
349
|
+
};
|
|
350
|
+
function X(e, t) {
|
|
351
|
+
return typeof t == "function" ? t(e) : t === true;
|
|
352
|
+
}
|
|
353
|
+
var te = ["localhost", "127.0.0.1", "0.0.0.0"];
|
|
354
|
+
var V = typeof FileList > "u";
|
|
355
|
+
var N = (e) => V ? e instanceof Blob : e instanceof FileList || e instanceof File;
|
|
356
|
+
var ue = (e) => {
|
|
357
|
+
if (!e)
|
|
358
|
+
return false;
|
|
359
|
+
for (let t in e)
|
|
360
|
+
if (N(e[t]) || Array.isArray(e[t]) && e[t].find(N))
|
|
361
|
+
return true;
|
|
362
|
+
return false;
|
|
363
|
+
};
|
|
364
|
+
var B = (e) => V ? e : new Promise((t) => {
|
|
365
|
+
let n = new FileReader;
|
|
366
|
+
n.onload = () => {
|
|
367
|
+
let r = new File([n.result], e.name, { lastModified: e.lastModified, type: e.type });
|
|
368
|
+
t(r);
|
|
369
|
+
}, n.readAsArrayBuffer(e);
|
|
370
|
+
});
|
|
371
|
+
var O = async (e, t, n = {}, r = {}) => {
|
|
372
|
+
if (Array.isArray(e)) {
|
|
373
|
+
for (let a of e)
|
|
374
|
+
if (!Array.isArray(a))
|
|
375
|
+
r = await O(a, t, n, r);
|
|
376
|
+
else {
|
|
377
|
+
let i = a[0];
|
|
378
|
+
if (typeof i == "string")
|
|
379
|
+
r[i.toLowerCase()] = a[1];
|
|
380
|
+
else
|
|
381
|
+
for (let [u, g] of i)
|
|
382
|
+
r[u.toLowerCase()] = g;
|
|
383
|
+
}
|
|
384
|
+
return r;
|
|
385
|
+
}
|
|
386
|
+
if (!e)
|
|
387
|
+
return r;
|
|
388
|
+
switch (typeof e) {
|
|
389
|
+
case "function":
|
|
390
|
+
if (e instanceof Headers)
|
|
391
|
+
return O(e, t, n, r);
|
|
392
|
+
let a = await e(t, n);
|
|
393
|
+
return a ? O(a, t, n, r) : r;
|
|
394
|
+
case "object":
|
|
395
|
+
if (e instanceof Headers)
|
|
396
|
+
return e.forEach((i, u) => {
|
|
397
|
+
r[u.toLowerCase()] = i;
|
|
398
|
+
}), r;
|
|
399
|
+
for (let [i, u] of Object.entries(e))
|
|
400
|
+
r[i.toLowerCase()] = u;
|
|
401
|
+
return r;
|
|
402
|
+
default:
|
|
403
|
+
return r;
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
function ne(e, t) {
|
|
407
|
+
let n = e.split(`
|
|
408
|
+
`), r = {};
|
|
409
|
+
for (let a of n) {
|
|
410
|
+
if (!a || a.startsWith(":"))
|
|
411
|
+
continue;
|
|
412
|
+
let i = a.indexOf(":");
|
|
413
|
+
if (i > 0) {
|
|
414
|
+
let u = a.slice(0, i).trim(), g2 = a.slice(i + 1).replace(/^ /, "");
|
|
415
|
+
r[u] = g2 && g(g2, t);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
return Object.keys(r).length > 0 ? r : null;
|
|
419
|
+
}
|
|
420
|
+
function* Y(e, t) {
|
|
421
|
+
let n;
|
|
422
|
+
for (;(n = e.value.indexOf(`
|
|
423
|
+
|
|
424
|
+
`)) !== -1; ) {
|
|
425
|
+
let r = e.value.slice(0, n);
|
|
426
|
+
if (e.value = e.value.slice(n + 2), r.trim()) {
|
|
427
|
+
let a = ne(r, t);
|
|
428
|
+
a && (yield a);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
async function* Z(e, t) {
|
|
433
|
+
let n = e.body;
|
|
434
|
+
if (!n)
|
|
435
|
+
return;
|
|
436
|
+
let r = n.getReader(), a = new TextDecoder("utf-8");
|
|
437
|
+
if (e.headers.get("Content-Type")?.startsWith("text/event-stream")) {
|
|
438
|
+
let i = { value: "" };
|
|
439
|
+
try {
|
|
440
|
+
for (;; ) {
|
|
441
|
+
let { done: g, value: v } = await r.read();
|
|
442
|
+
if (g)
|
|
443
|
+
break;
|
|
444
|
+
let R = typeof v == "string" ? v : a.decode(v, { stream: true });
|
|
445
|
+
i.value += R, yield* Y(i, t);
|
|
446
|
+
}
|
|
447
|
+
let u = a.decode();
|
|
448
|
+
if (u && (i.value += u), yield* Y(i, t), i.value.trim()) {
|
|
449
|
+
let g = ne(i.value, t);
|
|
450
|
+
g && (yield g);
|
|
451
|
+
}
|
|
452
|
+
} finally {
|
|
453
|
+
r.releaseLock();
|
|
454
|
+
}
|
|
455
|
+
} else
|
|
456
|
+
try {
|
|
457
|
+
for (;; ) {
|
|
458
|
+
let { done: i, value: u } = await r.read();
|
|
459
|
+
if (i)
|
|
460
|
+
break;
|
|
461
|
+
yield g(typeof u == "string" ? u : a.decode(u, { stream: true }), { parseDate: t?.parseDate });
|
|
462
|
+
}
|
|
463
|
+
} finally {
|
|
464
|
+
r.releaseLock();
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
var F = (e, t, n = [], r, a, i2 = false, u = []) => new Proxy(() => {}, { get(g, v) {
|
|
468
|
+
if (v === "~path")
|
|
469
|
+
return "/" + n.join("/");
|
|
470
|
+
if (n.length === 0) {
|
|
471
|
+
if (v === "use")
|
|
472
|
+
return (R) => F(e, t, n, r, fe(R, a));
|
|
473
|
+
if (v === "then" || v === "catch" || v === "finally")
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
return F(e, t, [...n, v], r, a, false, u);
|
|
477
|
+
}, apply(g2, v, R) {
|
|
478
|
+
if (a && n.length > 0 && !i2) {
|
|
479
|
+
let T = n[n.length - 1];
|
|
480
|
+
if (Object.hasOwn(a.verbs, T))
|
|
481
|
+
return a.verbs[T].handler({ path: "/" + n.slice(0, -1).join("/"), paths: n.slice(0, -1).map(String), domain: e, config: t, elysia: r }, ...R);
|
|
482
|
+
}
|
|
483
|
+
let [d2, k] = R;
|
|
484
|
+
if (!d2 || k || typeof d2 == "object" && Object.keys(d2).length !== 1 || ee.includes(n.at(-1))) {
|
|
485
|
+
let T = [...n], S = T.pop(), E = "/" + T.map((s, w) => u.includes(w) ? encodeURIComponent(s) : s).join("/"), { fetcher: re = fetch, headers: W, onRequest: m, onResponse: D, fetch: se } = t, x = S === "get" || S === "head" || S === "subscribe", $ = x ? d2?.query : k?.query, H = "";
|
|
486
|
+
if ($) {
|
|
487
|
+
let s = (w, y) => {
|
|
488
|
+
y != null && (y instanceof Date && (y = y.toISOString()), H += (H ? "&" : "?") + `${encodeURIComponent(w)}=${encodeURIComponent(typeof y == "object" ? JSON.stringify(y) : y + "")}`);
|
|
489
|
+
};
|
|
490
|
+
for (let [w, y] of Object.entries($)) {
|
|
491
|
+
if (Array.isArray(y)) {
|
|
492
|
+
for (let I of y)
|
|
493
|
+
s(w, I);
|
|
494
|
+
continue;
|
|
495
|
+
}
|
|
496
|
+
s(w, y);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
if (S === "subscribe") {
|
|
500
|
+
let s = e.replace(/^([^]+):\/\//, e.startsWith("https://") ? "wss://" : e.startsWith("http://") || te.find((w) => e.includes(w)) ? "ws://" : "wss://") + E + H;
|
|
501
|
+
return new M(s);
|
|
502
|
+
}
|
|
503
|
+
let J = !!a && le.includes(S), K = J ? { paths: T.map(String), method: S, options: k, config: t, domain: e } : undefined, U = false, L = (s) => {
|
|
504
|
+
if (U = true, K && a)
|
|
505
|
+
for (let w of a.afterCall)
|
|
506
|
+
try {
|
|
507
|
+
w(s, K);
|
|
508
|
+
} catch (y) {
|
|
509
|
+
console.error(y);
|
|
510
|
+
}
|
|
511
|
+
return s;
|
|
512
|
+
}, ae = (s) => {
|
|
513
|
+
U || L({ data: null, error: new d(0, s), response: undefined, status: 0, headers: undefined });
|
|
514
|
+
}, _ = (async () => {
|
|
515
|
+
if (K && a)
|
|
516
|
+
for (let l of a.beforeCall)
|
|
517
|
+
l(K);
|
|
518
|
+
W = await O(W, E, k);
|
|
519
|
+
let s = { method: S?.toUpperCase(), body: d2, ...se, headers: W };
|
|
520
|
+
s.headers = { ...W, ...await O(x ? d2?.headers : k?.headers, E, s) };
|
|
521
|
+
let w = x && typeof d2 == "object" ? d2.fetch : k?.fetch, I = (x && typeof d2 == "object" ? d2.throwHttpError : k?.throwHttpError) ?? t.throwHttpError;
|
|
522
|
+
if (s = { ...s, ...w }, x && delete s.body, m) {
|
|
523
|
+
Array.isArray(m) || (m = [m]);
|
|
524
|
+
for (let l of m) {
|
|
525
|
+
let o = await l(E, s);
|
|
526
|
+
typeof o == "object" && (s = { ...s, ...o, headers: { ...s.headers, ...await O(o.headers, E, s) } });
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
if (x && delete s.body, ue(d2)) {
|
|
530
|
+
let l = new FormData, o = (f) => {
|
|
531
|
+
if (typeof f == "string" || N(f))
|
|
532
|
+
return false;
|
|
533
|
+
if (typeof f == "object") {
|
|
534
|
+
if (f !== null)
|
|
535
|
+
return true;
|
|
536
|
+
if (f instanceof Date)
|
|
537
|
+
return false;
|
|
538
|
+
}
|
|
539
|
+
return false;
|
|
540
|
+
}, b = async (f) => f instanceof File ? await B(f) : o(f) ? JSON.stringify(f) : f;
|
|
541
|
+
for (let [f, h] of Object.entries(s.body)) {
|
|
542
|
+
if (Array.isArray(h)) {
|
|
543
|
+
if (h.some((A) => typeof A == "object" && A !== null && !N(A)))
|
|
544
|
+
l.append(f, JSON.stringify(h));
|
|
545
|
+
else
|
|
546
|
+
for (let A = 0;A < h.length; A++) {
|
|
547
|
+
let oe = h[A], ie = await b(oe);
|
|
548
|
+
l.append(f, ie);
|
|
549
|
+
}
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
if (V) {
|
|
553
|
+
if (Array.isArray(h))
|
|
554
|
+
for (let P of h)
|
|
555
|
+
l.append(f, await b(P));
|
|
556
|
+
else
|
|
557
|
+
l.append(f, await b(h));
|
|
558
|
+
continue;
|
|
559
|
+
}
|
|
560
|
+
if (h instanceof File) {
|
|
561
|
+
l.append(f, await B(h));
|
|
562
|
+
continue;
|
|
563
|
+
}
|
|
564
|
+
if (h instanceof FileList) {
|
|
565
|
+
for (let P = 0;P < h.length; P++)
|
|
566
|
+
l.append(f, await B(h[P]));
|
|
567
|
+
continue;
|
|
568
|
+
}
|
|
569
|
+
l.append(f, await b(h));
|
|
570
|
+
}
|
|
571
|
+
s.body = l;
|
|
572
|
+
} else
|
|
573
|
+
typeof d2 == "object" ? (s.headers["content-type"] = "application/json", s.body = JSON.stringify(d2)) : d2 != null && (s.headers["content-type"] = "text/plain");
|
|
574
|
+
if (x && delete s.body, m) {
|
|
575
|
+
Array.isArray(m) || (m = [m]);
|
|
576
|
+
for (let l of m) {
|
|
577
|
+
let o = await l(E, s);
|
|
578
|
+
typeof o == "object" && (s = { ...s, ...o, headers: { ...s.headers, ...await O(o.headers, E, s) } });
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
k?.headers?.["content-type"] && (s.headers["content-type"] = k?.headers["content-type"]);
|
|
582
|
+
let G = e + E + H, c;
|
|
583
|
+
try {
|
|
584
|
+
c = await (r?.handle(new Request(G, s)) ?? re(G, s));
|
|
585
|
+
} catch (l) {
|
|
586
|
+
let o = new d(503, l), b = L({ data: null, error: o, response: undefined, status: 503, headers: undefined });
|
|
587
|
+
if (X(o, I))
|
|
588
|
+
throw o;
|
|
589
|
+
return b;
|
|
590
|
+
}
|
|
591
|
+
let p = null, C = null;
|
|
592
|
+
if (D) {
|
|
593
|
+
Array.isArray(D) || (D = [D]);
|
|
594
|
+
for (let l of D)
|
|
595
|
+
try {
|
|
596
|
+
let o = await l(c.clone());
|
|
597
|
+
if (o != null) {
|
|
598
|
+
p = o;
|
|
599
|
+
break;
|
|
600
|
+
}
|
|
601
|
+
} catch (o) {
|
|
602
|
+
o instanceof d ? C = o : C = new d(422, o);
|
|
603
|
+
break;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
if (p !== null)
|
|
607
|
+
return L({ data: p, error: C, response: c, status: c.status, headers: c.headers });
|
|
608
|
+
switch (c.headers.get("Content-Type")?.split(";")[0]) {
|
|
609
|
+
case "text/event-stream":
|
|
610
|
+
p = Z(c, { parseDate: t.parseDate });
|
|
611
|
+
break;
|
|
612
|
+
case "application/json":
|
|
613
|
+
case "application/problem+json":
|
|
614
|
+
p = JSON.parse(await c.text(), (o, b) => {
|
|
615
|
+
if (typeof b != "string")
|
|
616
|
+
return b;
|
|
617
|
+
let f = i(b, { parseDate: t.parseDate });
|
|
618
|
+
return f || b;
|
|
619
|
+
});
|
|
620
|
+
break;
|
|
621
|
+
case "application/octet-stream":
|
|
622
|
+
p = await c.arrayBuffer();
|
|
623
|
+
break;
|
|
624
|
+
case "multipart/form-data":
|
|
625
|
+
let l = await c.formData();
|
|
626
|
+
p = {}, l.forEach((o, b) => {
|
|
627
|
+
p[b] = o;
|
|
628
|
+
});
|
|
629
|
+
break;
|
|
630
|
+
default:
|
|
631
|
+
c.headers.get("content-type")?.startsWith("text/") && c.headers.get("transfer-encoding") === "chunked" && !c.headers.has("content-length") ? p = Z(c, { parseDate: t.parseDate }) : p = await c.text().then((o) => g(o, { parseDate: t.parseDate }));
|
|
632
|
+
}
|
|
633
|
+
if (c.status >= 300 || c.status < 200) {
|
|
634
|
+
C = new d(c.status, p), p = null;
|
|
635
|
+
let l = L({ data: p, error: C, response: c, status: c.status, headers: c.headers });
|
|
636
|
+
if (X(C, I))
|
|
637
|
+
throw C;
|
|
638
|
+
return l;
|
|
639
|
+
}
|
|
640
|
+
return L({ data: p, error: C, response: c, status: c.status, headers: c.headers });
|
|
641
|
+
})();
|
|
642
|
+
return J ? _.catch((s) => {
|
|
643
|
+
throw ae(s), s;
|
|
644
|
+
}) : _;
|
|
645
|
+
}
|
|
646
|
+
return typeof d2 == "object" ? F(e, t, [...n, Object.values(d2)[0]], r, a, true, [...u, n.length]) : F(e, t, n, undefined, a, i2, u);
|
|
647
|
+
} });
|
|
648
|
+
var ge = (e, t = {}) => typeof e == "string" ? (t.keepDomain || (e.includes("://") || (e = (te.find((n) => e.includes(n)) ? "http://" : "https://") + e), e.endsWith("/") && (e = e.slice(0, -1))), F(e, t)) : (typeof window < "u" && console.warn("Elysia instance server found on client side, this is not recommended for security reason. Use generic type instead."), F("http://e.ly", t, [], e));
|
|
649
|
+
|
|
650
|
+
// src/client/mutation.ts
|
|
651
|
+
import { useCallback, useRef, useState } from "react";
|
|
652
|
+
function isApiError(value) {
|
|
653
|
+
return typeof value === "object" && value !== null && "status" in value && typeof value.status === "number" && "value" in value;
|
|
654
|
+
}
|
|
655
|
+
function normalizeMutationError(cause) {
|
|
656
|
+
const exception = isApiError(cause) && cause.status === 0 && cause.value instanceof Error ? cause.value : cause;
|
|
657
|
+
if (isApiError(exception)) {
|
|
658
|
+
const { value } = exception;
|
|
659
|
+
if (typeof value === "object" && value !== null && "detail" in value && typeof value.detail === "string") {
|
|
660
|
+
return exception;
|
|
661
|
+
}
|
|
662
|
+
const error = new d(exception.status, {
|
|
663
|
+
...typeof value === "object" && value !== null && !Array.isArray(value) ? value : {},
|
|
664
|
+
detail: typeof value === "string" && value ? value : "Mutation failed"
|
|
665
|
+
});
|
|
666
|
+
error.cause = exception;
|
|
667
|
+
return error;
|
|
668
|
+
}
|
|
669
|
+
const error = new d(0, {
|
|
670
|
+
detail: exception instanceof Error ? exception.message : "Mutation failed"
|
|
671
|
+
});
|
|
672
|
+
error.cause = exception;
|
|
673
|
+
return error;
|
|
674
|
+
}
|
|
675
|
+
function useMutation(method, options) {
|
|
676
|
+
const [state, setState] = useState({ data: undefined, error: null, isPending: false });
|
|
677
|
+
const latest = useRef(0);
|
|
678
|
+
const pending = useRef(0);
|
|
679
|
+
const mutateAsync = useCallback(async (...args) => {
|
|
680
|
+
latest.current += 1;
|
|
681
|
+
const invocation = latest.current;
|
|
682
|
+
pending.current += 1;
|
|
683
|
+
setState({ data: undefined, error: null, isPending: true });
|
|
684
|
+
try {
|
|
685
|
+
const result = await Reflect.apply(method, undefined, args);
|
|
686
|
+
if (result.error !== null) {
|
|
687
|
+
if ("response" in result && result.response === undefined && result.error instanceof d && result.error.status === 503 && (result.error.value instanceof Error || typeof result.error.value === "string")) {
|
|
688
|
+
throw result.error.value;
|
|
689
|
+
}
|
|
690
|
+
throw result.error;
|
|
691
|
+
}
|
|
692
|
+
await options?.onSuccess?.(result.data);
|
|
693
|
+
if (latest.current === invocation) {
|
|
694
|
+
setState((previous) => ({ ...previous, data: result.data, error: null }));
|
|
695
|
+
}
|
|
696
|
+
return result.data;
|
|
697
|
+
} catch (cause) {
|
|
698
|
+
const failure = normalizeMutationError(cause);
|
|
699
|
+
if (latest.current === invocation) {
|
|
700
|
+
setState((previous) => ({ ...previous, data: undefined, error: failure }));
|
|
701
|
+
}
|
|
702
|
+
await options?.onError?.(failure);
|
|
703
|
+
throw failure;
|
|
704
|
+
} finally {
|
|
705
|
+
pending.current -= 1;
|
|
706
|
+
setState((previous) => ({ ...previous, isPending: pending.current > 0 }));
|
|
707
|
+
}
|
|
708
|
+
}, [method, options]);
|
|
709
|
+
const mutate = useCallback((...args) => {
|
|
710
|
+
mutateAsync(...args).catch(() => {
|
|
711
|
+
return;
|
|
712
|
+
});
|
|
713
|
+
}, [mutateAsync]);
|
|
714
|
+
const reset = useCallback(() => {
|
|
715
|
+
latest.current += 1;
|
|
716
|
+
setState((previous) => ({ ...previous, data: undefined, error: null }));
|
|
717
|
+
}, []);
|
|
718
|
+
return { ...state, mutate, mutateAsync, reset };
|
|
719
|
+
}
|
|
720
|
+
// src/client/query.tsx
|
|
721
|
+
import {
|
|
722
|
+
createContext as createContext2,
|
|
723
|
+
useCallback as useCallback2,
|
|
724
|
+
useContext as useContext2,
|
|
725
|
+
useEffect,
|
|
726
|
+
useSyncExternalStore
|
|
727
|
+
} from "react";
|
|
728
|
+
|
|
729
|
+
// src/shared/sync-query.ts
|
|
730
|
+
var PREFIX = "__furin.query:";
|
|
731
|
+
function queryTag(identity) {
|
|
732
|
+
for (const [key, value] of Object.entries(identity.scope)) {
|
|
733
|
+
if (value === undefined) {
|
|
734
|
+
throw new Error(`[furin] Query scope "${key}" is undefined for "${identity.id}".`);
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
return PREFIX + JSON.stringify([
|
|
738
|
+
identity.id,
|
|
739
|
+
Object.entries(identity.scope).sort(([a], [b]) => {
|
|
740
|
+
if (a === b) {
|
|
741
|
+
return 0;
|
|
742
|
+
}
|
|
743
|
+
return a < b ? -1 : 1;
|
|
744
|
+
})
|
|
745
|
+
]);
|
|
746
|
+
}
|
|
747
|
+
function queryUrl(url, origin) {
|
|
748
|
+
const parsed = new URL(url, origin);
|
|
749
|
+
parsed.searchParams.sort();
|
|
750
|
+
return (parsed.origin === origin ? "" : parsed.origin) + parsed.pathname + parsed.search;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
// src/client/query-store.ts
|
|
754
|
+
var QUERY_REFERENCE = Symbol.for("furin.query.reference.v1");
|
|
755
|
+
var SERVER_ENVIRONMENT = Symbol.for("furin.query.environment.v1");
|
|
756
|
+
var environmentGlobal = globalThis;
|
|
757
|
+
function currentQueryEnvironment() {
|
|
758
|
+
return environmentGlobal[SERVER_ENVIRONMENT]?.();
|
|
759
|
+
}
|
|
760
|
+
function readReference(method) {
|
|
761
|
+
return method[QUERY_REFERENCE];
|
|
762
|
+
}
|
|
763
|
+
function readUrl(reference, options) {
|
|
764
|
+
const url = new URL(reference.url);
|
|
765
|
+
const query = options?.query;
|
|
766
|
+
const append = (key, value) => {
|
|
767
|
+
if (value === undefined || value === null) {
|
|
768
|
+
return;
|
|
769
|
+
}
|
|
770
|
+
let serialized = String(value);
|
|
771
|
+
if (value instanceof Date) {
|
|
772
|
+
serialized = value.toISOString();
|
|
773
|
+
} else if (typeof value === "object") {
|
|
774
|
+
serialized = JSON.stringify(value);
|
|
775
|
+
}
|
|
776
|
+
url.searchParams.append(key, serialized);
|
|
777
|
+
};
|
|
778
|
+
for (const [key, value] of Object.entries(query ?? {})) {
|
|
779
|
+
if (Array.isArray(value)) {
|
|
780
|
+
for (const item of value) {
|
|
781
|
+
append(key, item);
|
|
782
|
+
}
|
|
783
|
+
} else {
|
|
784
|
+
append(key, value);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
url.searchParams.sort();
|
|
788
|
+
return url.href;
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
class QueryStore {
|
|
792
|
+
entries = new Map;
|
|
793
|
+
listeners = new Set;
|
|
794
|
+
revisionValue = 0;
|
|
795
|
+
projections = new Set;
|
|
796
|
+
session;
|
|
797
|
+
epoch = 0;
|
|
798
|
+
origin;
|
|
799
|
+
constructor(origin) {
|
|
800
|
+
this.origin = origin;
|
|
801
|
+
}
|
|
802
|
+
key(url) {
|
|
803
|
+
const absolute = new URL(url, this.origin).href;
|
|
804
|
+
return queryUrl(absolute, undefined);
|
|
805
|
+
}
|
|
806
|
+
entry(url) {
|
|
807
|
+
const key = this.key(url);
|
|
808
|
+
let entry = this.entries.get(key);
|
|
809
|
+
if (!entry) {
|
|
810
|
+
entry = {
|
|
811
|
+
base: undefined,
|
|
812
|
+
listeners: new Set,
|
|
813
|
+
snapshot: { data: undefined, error: null, isFetching: false },
|
|
814
|
+
stale: true,
|
|
815
|
+
version: 0
|
|
816
|
+
};
|
|
817
|
+
this.entries.set(key, entry);
|
|
818
|
+
}
|
|
819
|
+
return entry;
|
|
820
|
+
}
|
|
821
|
+
publish(entry) {
|
|
822
|
+
let data = entry.base;
|
|
823
|
+
for (const projection of this.projections) {
|
|
824
|
+
const transform = projection.transforms.get(entry);
|
|
825
|
+
if (transform && data !== undefined) {
|
|
826
|
+
data = transform(data);
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
entry.snapshot = { ...entry.snapshot, data };
|
|
830
|
+
for (const listener of entry.listeners) {
|
|
831
|
+
listener();
|
|
832
|
+
}
|
|
833
|
+
this.revisionValue += 1;
|
|
834
|
+
for (const listener of this.listeners) {
|
|
835
|
+
listener();
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
revision = () => this.revisionValue;
|
|
839
|
+
subscribeAll = (listener) => {
|
|
840
|
+
this.listeners.add(listener);
|
|
841
|
+
return () => this.listeners.delete(listener);
|
|
842
|
+
};
|
|
843
|
+
observe(url, result, epoch, version) {
|
|
844
|
+
const existing = this.entries.get(this.key(url));
|
|
845
|
+
if (epoch !== this.epoch || result.error !== null || existing && (this.pending(existing) || version !== undefined && existing.version !== version)) {
|
|
846
|
+
return;
|
|
847
|
+
}
|
|
848
|
+
const header = result.response?.headers.get("x-furin-query");
|
|
849
|
+
const identity = result.identity ?? (header ? JSON.parse(header) : undefined);
|
|
850
|
+
if (identity) {
|
|
851
|
+
this.setSession(identity.session);
|
|
852
|
+
}
|
|
853
|
+
const entry = this.entry(url);
|
|
854
|
+
entry.base = result.data;
|
|
855
|
+
entry.version += 1;
|
|
856
|
+
entry.identity = identity;
|
|
857
|
+
entry.local = result.local ?? result.response?.url === "";
|
|
858
|
+
entry.stale = false;
|
|
859
|
+
entry.snapshot = { data: result.data, error: null, isFetching: false };
|
|
860
|
+
for (const projection of this.projections) {
|
|
861
|
+
if (!projection.pending) {
|
|
862
|
+
projection.transforms.delete(entry);
|
|
863
|
+
if (projection.transforms.size === 0) {
|
|
864
|
+
this.projections.delete(projection);
|
|
865
|
+
projection.onRemove?.();
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
this.publish(entry);
|
|
870
|
+
if (this.entries.size > 100) {
|
|
871
|
+
const oldest = [...this.entries].find(([, item]) => item !== entry && item.listeners.size === 0 && !item.promise && !this.pending(item));
|
|
872
|
+
if (oldest) {
|
|
873
|
+
this.entries.delete(oldest[0]);
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
setSession(session) {
|
|
878
|
+
if (this.session !== undefined && this.session !== session) {
|
|
879
|
+
this.epoch += 1;
|
|
880
|
+
for (const projection of this.projections) {
|
|
881
|
+
projection.onRemove?.();
|
|
882
|
+
}
|
|
883
|
+
this.projections.clear();
|
|
884
|
+
for (const entry of this.entries.values()) {
|
|
885
|
+
entry.base = undefined;
|
|
886
|
+
entry.identity = undefined;
|
|
887
|
+
entry.stale = true;
|
|
888
|
+
entry.version += 1;
|
|
889
|
+
entry.snapshot = { data: undefined, error: null, isFetching: false };
|
|
890
|
+
this.publish(entry);
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
this.session = session;
|
|
894
|
+
}
|
|
895
|
+
generation() {
|
|
896
|
+
return this.epoch;
|
|
897
|
+
}
|
|
898
|
+
version(url) {
|
|
899
|
+
return this.entry(url).version;
|
|
900
|
+
}
|
|
901
|
+
snapshot(url) {
|
|
902
|
+
return this.entry(url).snapshot;
|
|
903
|
+
}
|
|
904
|
+
subscribe(url, listener) {
|
|
905
|
+
const entry = this.entry(url);
|
|
906
|
+
entry.listeners.add(listener);
|
|
907
|
+
return () => entry.listeners.delete(listener);
|
|
908
|
+
}
|
|
909
|
+
bind(url, load, client) {
|
|
910
|
+
const entry = this.entry(url);
|
|
911
|
+
if (client && entry.client && client !== entry.client) {
|
|
912
|
+
entry.base = undefined;
|
|
913
|
+
entry.identity = undefined;
|
|
914
|
+
entry.stale = true;
|
|
915
|
+
entry.version += 1;
|
|
916
|
+
entry.snapshot = { data: undefined, error: null, isFetching: false };
|
|
917
|
+
for (const projection of this.projections) {
|
|
918
|
+
projection.transforms.delete(entry);
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
entry.client = client ?? entry.client;
|
|
922
|
+
entry.load = load;
|
|
923
|
+
}
|
|
924
|
+
pending(entry) {
|
|
925
|
+
return [...this.projections].some((projection) => projection.pending && projection.transforms.has(entry));
|
|
926
|
+
}
|
|
927
|
+
fetch(url) {
|
|
928
|
+
const entry = this.entry(url);
|
|
929
|
+
if (entry.promise) {
|
|
930
|
+
return entry.promise;
|
|
931
|
+
}
|
|
932
|
+
if (!(entry.stale && entry.load) || this.pending(entry)) {
|
|
933
|
+
return Promise.resolve();
|
|
934
|
+
}
|
|
935
|
+
const { version } = entry;
|
|
936
|
+
const { epoch } = this;
|
|
937
|
+
entry.snapshot = { ...entry.snapshot, isFetching: true };
|
|
938
|
+
this.publish(entry);
|
|
939
|
+
entry.promise = Promise.resolve().then(() => entry.load?.()).then((result) => {
|
|
940
|
+
if (!result || epoch !== this.epoch || version !== entry.version || this.pending(entry)) {
|
|
941
|
+
return;
|
|
942
|
+
}
|
|
943
|
+
if (result.error === null) {
|
|
944
|
+
this.observe(url, result, epoch);
|
|
945
|
+
} else {
|
|
946
|
+
entry.snapshot = { ...entry.snapshot, error: result.error };
|
|
947
|
+
}
|
|
948
|
+
}).catch((error) => {
|
|
949
|
+
if (epoch === this.epoch && version === entry.version) {
|
|
950
|
+
entry.snapshot = { ...entry.snapshot, error };
|
|
951
|
+
}
|
|
952
|
+
}).finally(() => {
|
|
953
|
+
entry.promise = undefined;
|
|
954
|
+
entry.snapshot = { ...entry.snapshot, isFetching: false };
|
|
955
|
+
this.publish(entry);
|
|
956
|
+
if (entry.stale && version !== entry.version && entry.listeners.size > 0) {
|
|
957
|
+
this.fetch(url);
|
|
958
|
+
}
|
|
959
|
+
});
|
|
960
|
+
return entry.promise;
|
|
961
|
+
}
|
|
962
|
+
invalidate(identities) {
|
|
963
|
+
const tags = new Set(identities.map(queryTag));
|
|
964
|
+
let affected = false;
|
|
965
|
+
for (const [url, entry] of this.entries) {
|
|
966
|
+
if (entry.identity && tags.has(queryTag(entry.identity))) {
|
|
967
|
+
affected = true;
|
|
968
|
+
this.invalidateEntry(url, entry);
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
return affected;
|
|
972
|
+
}
|
|
973
|
+
invalidateEntry(url, entry) {
|
|
974
|
+
entry.stale = true;
|
|
975
|
+
entry.version += 1;
|
|
976
|
+
if (entry.listeners.size > 0) {
|
|
977
|
+
this.fetch(url);
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
invalidateAll() {
|
|
981
|
+
for (const [url, entry] of this.entries) {
|
|
982
|
+
this.invalidateEntry(url, entry);
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
begin() {
|
|
986
|
+
return { pending: true, transforms: new Map };
|
|
987
|
+
}
|
|
988
|
+
update(projection, url, transform) {
|
|
989
|
+
const entry = this.entries.get(this.key(url));
|
|
990
|
+
if (!entry || entry.base === undefined) {
|
|
991
|
+
return;
|
|
992
|
+
}
|
|
993
|
+
const previous = projection.transforms.get(entry);
|
|
994
|
+
projection.transforms.set(entry, previous ? (data) => transform(previous(data)) : transform);
|
|
995
|
+
entry.version += 1;
|
|
996
|
+
this.projections.add(projection);
|
|
997
|
+
this.publish(entry);
|
|
998
|
+
}
|
|
999
|
+
finish(projection, outcome) {
|
|
1000
|
+
projection.pending = false;
|
|
1001
|
+
const entries = [...projection.transforms.keys()];
|
|
1002
|
+
if (outcome === "error") {
|
|
1003
|
+
this.projections.delete(projection);
|
|
1004
|
+
projection.onRemove?.();
|
|
1005
|
+
}
|
|
1006
|
+
for (const entry of entries) {
|
|
1007
|
+
this.publish(entry);
|
|
1008
|
+
if (outcome !== "error") {
|
|
1009
|
+
entry.stale = true;
|
|
1010
|
+
}
|
|
1011
|
+
const url = [...this.entries].find(([, candidate]) => candidate === entry)?.[0];
|
|
1012
|
+
if (url && entry.stale) {
|
|
1013
|
+
this.fetch(url);
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
dehydrate() {
|
|
1018
|
+
return [...this.entries].flatMap(([url, entry]) => entry.identity && entry.base !== undefined ? [{ url, data: entry.base, identity: entry.identity, local: entry.local }] : []);
|
|
1019
|
+
}
|
|
1020
|
+
seedUrl(seed, localOrigin) {
|
|
1021
|
+
const original = new URL(seed.url, this.origin);
|
|
1022
|
+
return seed.local && localOrigin ? new URL(original.pathname + original.search, localOrigin).href : original.href;
|
|
1023
|
+
}
|
|
1024
|
+
captureHydration() {
|
|
1025
|
+
const { epoch } = this;
|
|
1026
|
+
const versions = new Map([...this.entries].map(([key, entry]) => [key, entry.version]));
|
|
1027
|
+
return (seeds, localOrigin) => {
|
|
1028
|
+
if (epoch === this.epoch) {
|
|
1029
|
+
this.hydrate(seeds.filter((seed) => {
|
|
1030
|
+
const url = this.seedUrl(seed, localOrigin);
|
|
1031
|
+
return this.version(url) === (versions.get(this.key(url)) ?? 0);
|
|
1032
|
+
}), localOrigin);
|
|
1033
|
+
}
|
|
1034
|
+
};
|
|
1035
|
+
}
|
|
1036
|
+
hydrate(seeds, localOrigin) {
|
|
1037
|
+
for (const seed of seeds) {
|
|
1038
|
+
const url = this.seedUrl(seed, localOrigin);
|
|
1039
|
+
this.observe(url, {
|
|
1040
|
+
data: seed.data,
|
|
1041
|
+
error: null,
|
|
1042
|
+
identity: seed.identity,
|
|
1043
|
+
local: seed.local ?? false
|
|
1044
|
+
}, this.epoch);
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
// src/client/query.tsx
|
|
1050
|
+
var QUERY_CONTEXT = Symbol.for("furin.query.context.v1");
|
|
1051
|
+
var contextGlobal = globalThis;
|
|
1052
|
+
var QueryStoreContext = contextGlobal[QUERY_CONTEXT] ??= createContext2(null);
|
|
1053
|
+
function useQuery(method, ...args) {
|
|
1054
|
+
const reference = readReference(method);
|
|
1055
|
+
if (!reference) {
|
|
1056
|
+
throw new Error("[furin] useQuery requires a GET from withSync().");
|
|
1057
|
+
}
|
|
1058
|
+
const context = useContext2(QueryStoreContext);
|
|
1059
|
+
const store = context ?? reference.client;
|
|
1060
|
+
const [options] = args;
|
|
1061
|
+
const select = options?.select;
|
|
1062
|
+
const url = readUrl(reference, options);
|
|
1063
|
+
store.bind(url, () => reference.load(options), reference.client);
|
|
1064
|
+
const subscribe = useCallback2((listener) => store.subscribe(url, listener), [store, url]);
|
|
1065
|
+
const snapshot = useCallback2(() => store.snapshot(url), [store, url]);
|
|
1066
|
+
const state = useSyncExternalStore(subscribe, snapshot, snapshot);
|
|
1067
|
+
useEffect(() => {
|
|
1068
|
+
store.fetch(url);
|
|
1069
|
+
}, [store, url]);
|
|
1070
|
+
let data;
|
|
1071
|
+
if (state.data !== undefined) {
|
|
1072
|
+
data = select ? select(state.data) : state.data;
|
|
1073
|
+
}
|
|
1074
|
+
return {
|
|
1075
|
+
data,
|
|
1076
|
+
error: state.error,
|
|
1077
|
+
isFetching: state.isFetching
|
|
1078
|
+
};
|
|
1079
|
+
}
|
|
1080
|
+
// src/client/route-api.tsx
|
|
1081
|
+
import { createContext as createContext3, createElement as createElement2, useContext as useContext3 } from "react";
|
|
1082
|
+
var RESERVED_RENDER_KEYS = new Set([
|
|
1083
|
+
"catch",
|
|
1084
|
+
"children",
|
|
1085
|
+
"finally",
|
|
1086
|
+
"key",
|
|
1087
|
+
"ref",
|
|
1088
|
+
"then",
|
|
1089
|
+
"toJSON"
|
|
1090
|
+
]);
|
|
1091
|
+
var ROUTE_CONTEXT = Symbol.for("furin.route.context.v1");
|
|
1092
|
+
var contextGlobal2 = globalThis;
|
|
1093
|
+
var RouteContext = contextGlobal2[ROUTE_CONTEXT] ??= createContext3(null);
|
|
1094
|
+
function useRouteSnapshot(pattern) {
|
|
1095
|
+
const snapshot = useContext3(RouteContext);
|
|
1096
|
+
if (snapshot?.pattern !== pattern) {
|
|
1097
|
+
throw new Error(`[furin] getRouteApi("${pattern}") does not match the active route.`);
|
|
1098
|
+
}
|
|
1099
|
+
return snapshot;
|
|
1100
|
+
}
|
|
1101
|
+
function getRouteApi(pattern) {
|
|
1102
|
+
return {
|
|
1103
|
+
useLoaderData: () => useRouteSnapshot(pattern).data,
|
|
1104
|
+
useParams: () => useRouteSnapshot(pattern).params
|
|
1105
|
+
};
|
|
1106
|
+
}
|
|
1107
|
+
// src/client/router/optimistic.ts
|
|
1108
|
+
var SYNC_REQUEST = Symbol.for("furin.sync.request.v1");
|
|
1109
|
+
var RUNTIMES = Symbol.for("furin.optimistic.runtimes.v1");
|
|
1110
|
+
var runtimeGlobal = globalThis;
|
|
1111
|
+
var runtimes = runtimeGlobal[RUNTIMES] ??= new Set;
|
|
1112
|
+
function findOptimisticRuntime(domain) {
|
|
1113
|
+
if (typeof window === "undefined" || new URL(domain, window.location.origin).origin !== window.location.origin) {
|
|
1114
|
+
return;
|
|
1115
|
+
}
|
|
1116
|
+
const path = window.location.pathname;
|
|
1117
|
+
const matches = [...runtimes].filter(({ basePath }) => basePath === "" || path === basePath || path.startsWith(`${basePath}/`)).sort((a, b) => b.basePath.length - a.basePath.length);
|
|
1118
|
+
return matches[0]?.basePath === matches[1]?.basePath ? undefined : matches[0];
|
|
1119
|
+
}
|
|
1120
|
+
|
|
197
1121
|
// src/client/sync.ts
|
|
198
|
-
|
|
1122
|
+
function queryProjectionCache(queries, token, routeCache) {
|
|
1123
|
+
return {
|
|
1124
|
+
update(destination, transform, queryOptions) {
|
|
1125
|
+
if (typeof destination === "function") {
|
|
1126
|
+
const reference = readReference(destination);
|
|
1127
|
+
const url = readUrl(reference, queryOptions);
|
|
1128
|
+
queries.bind(url, () => reference.load(queryOptions), reference.client);
|
|
1129
|
+
queries.update(token, url, transform);
|
|
1130
|
+
} else {
|
|
1131
|
+
Reflect.apply(routeCache.update, routeCache, [destination, transform]);
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
};
|
|
1135
|
+
}
|
|
1136
|
+
var OPERATION = Symbol("furin.sync.operation");
|
|
1137
|
+
var MUTATIONS = new Set(["post", "put", "patch", "delete"]);
|
|
199
1138
|
function createIdempotencyKey() {
|
|
200
1139
|
if (typeof globalThis.crypto?.randomUUID === "function") {
|
|
201
1140
|
return globalThis.crypto.randomUUID();
|
|
202
1141
|
}
|
|
203
1142
|
return `${Date.now()}-${Math.random()}`;
|
|
204
1143
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
1144
|
+
var plugin = {
|
|
1145
|
+
name: "furin-sync",
|
|
1146
|
+
verbs: { __furinQuery: (context) => context },
|
|
1147
|
+
before(context) {
|
|
1148
|
+
const options = context.options;
|
|
1149
|
+
const operation = options?.[OPERATION];
|
|
1150
|
+
if (!(operation && options)) {
|
|
1151
|
+
return;
|
|
1152
|
+
}
|
|
1153
|
+
if (!operation.runtime) {
|
|
1154
|
+
operation.runtime = findOptimisticRuntime(context.domain);
|
|
1155
|
+
}
|
|
1156
|
+
operation.method = context.method;
|
|
1157
|
+
if (operation.runtime) {
|
|
1158
|
+
options.fetch = { ...options.fetch, [SYNC_REQUEST]: true };
|
|
1159
|
+
}
|
|
1160
|
+
},
|
|
1161
|
+
after(result, context) {
|
|
1162
|
+
const operation = context.options?.[OPERATION];
|
|
1163
|
+
if (operation) {
|
|
1164
|
+
operation.result = result;
|
|
1165
|
+
}
|
|
208
1166
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
try {
|
|
221
|
-
result = await mutation(input, {
|
|
222
|
-
headers: { "Idempotency-Key": idempotencyKey }
|
|
1167
|
+
};
|
|
1168
|
+
async function resolveHeaders(source, path, init) {
|
|
1169
|
+
if (typeof source === "function") {
|
|
1170
|
+
return resolveHeaders(await source(path, init), path, init);
|
|
1171
|
+
}
|
|
1172
|
+
if (Array.isArray(source) && !(source.length > 0 && Array.isArray(source[0]) && typeof source[0][0] === "string")) {
|
|
1173
|
+
const headers = new Headers;
|
|
1174
|
+
for (const entry of source) {
|
|
1175
|
+
const resolved = await resolveHeaders(entry, path, init);
|
|
1176
|
+
resolved.forEach((value, name) => {
|
|
1177
|
+
headers.set(name, value);
|
|
223
1178
|
});
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
1179
|
+
}
|
|
1180
|
+
return headers;
|
|
1181
|
+
}
|
|
1182
|
+
return new Headers(source);
|
|
1183
|
+
}
|
|
1184
|
+
function inProgress(result) {
|
|
1185
|
+
const value = result.error?.value;
|
|
1186
|
+
return result.response?.status === 409 && typeof value === "object" && value !== null && "code" in value && value.code === "FURIN_MUTATION_IN_PROGRESS";
|
|
1187
|
+
}
|
|
1188
|
+
function retryDelay(response, attempt) {
|
|
1189
|
+
const header = response?.headers.get("Retry-After");
|
|
1190
|
+
if (header !== null && header !== undefined) {
|
|
1191
|
+
const seconds = Number(header);
|
|
1192
|
+
if (Number.isFinite(seconds) && seconds >= 0) {
|
|
1193
|
+
return seconds * 1000;
|
|
1194
|
+
}
|
|
1195
|
+
const date = Date.parse(header);
|
|
1196
|
+
if (Number.isFinite(date)) {
|
|
1197
|
+
return Math.max(0, date - Date.now());
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
return Math.min(250 * 2 ** attempt, 4000);
|
|
1201
|
+
}
|
|
1202
|
+
function delay(ms, signal) {
|
|
1203
|
+
if (signal?.aborted) {
|
|
1204
|
+
return Promise.resolve(false);
|
|
1205
|
+
}
|
|
1206
|
+
return new Promise((resolve) => {
|
|
1207
|
+
const abort = () => {
|
|
1208
|
+
clearTimeout(timer);
|
|
1209
|
+
resolve(false);
|
|
1210
|
+
};
|
|
1211
|
+
const timer = setTimeout(() => {
|
|
1212
|
+
signal?.removeEventListener("abort", abort);
|
|
1213
|
+
resolve(true);
|
|
1214
|
+
}, ms);
|
|
1215
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
1216
|
+
});
|
|
1217
|
+
}
|
|
1218
|
+
async function runMutation(target, receiver, args, clientOptions, projections, clientQueries) {
|
|
1219
|
+
const supplied = args[1];
|
|
1220
|
+
const retry = supplied?.retry ?? clientOptions?.retry ?? 0;
|
|
1221
|
+
if (!Number.isSafeInteger(retry) || retry < 0) {
|
|
1222
|
+
throw new RangeError("[furin] retry must be a nonnegative integer.");
|
|
1223
|
+
}
|
|
1224
|
+
const operation = {};
|
|
1225
|
+
let key;
|
|
1226
|
+
const headers = async (path, init) => {
|
|
1227
|
+
const resolved = await resolveHeaders(supplied?.headers, path, init);
|
|
1228
|
+
Object.assign(headers, { "content-type": resolved.get("content-type") ?? undefined });
|
|
1229
|
+
const fetchHeaders = new Headers(supplied?.fetch?.headers);
|
|
1230
|
+
key ??= fetchHeaders.get("Idempotency-Key") ?? resolved.get("Idempotency-Key") ?? new Headers(init.headers).get("Idempotency-Key") ?? createIdempotencyKey();
|
|
1231
|
+
resolved.set("Idempotency-Key", key);
|
|
1232
|
+
if (supplied?.fetch?.headers) {
|
|
1233
|
+
fetchHeaders.set("Idempotency-Key", key);
|
|
1234
|
+
options.fetch = { ...options.fetch, headers: fetchHeaders };
|
|
1235
|
+
}
|
|
1236
|
+
if (!operation.projection) {
|
|
1237
|
+
const { runtime } = operation;
|
|
1238
|
+
const queries = currentQueryEnvironment()?.store ?? runtime?.queries ?? clientQueries;
|
|
1239
|
+
const owner = runtime ?? queries;
|
|
1240
|
+
const entries = projections.get(owner) ?? new Map;
|
|
1241
|
+
const identity = JSON.stringify([
|
|
1242
|
+
operation.method,
|
|
1243
|
+
path,
|
|
1244
|
+
key,
|
|
1245
|
+
supplied?.query && Object.entries(supplied.query).sort(([a], [b]) => a.localeCompare(b))
|
|
1246
|
+
]);
|
|
1247
|
+
let projection = entries.get(identity);
|
|
1248
|
+
if (!projection) {
|
|
1249
|
+
const queryToken = queries.begin();
|
|
1250
|
+
const onRemove = () => {
|
|
1251
|
+
entries.delete(identity);
|
|
1252
|
+
if (entries.size === 0) {
|
|
1253
|
+
projections.delete(owner);
|
|
1254
|
+
}
|
|
1255
|
+
};
|
|
1256
|
+
queryToken.onRemove = onRemove;
|
|
1257
|
+
const optimistic = (routeCache) => supplied?.optimistic?.(queryProjectionCache(queries, queryToken, routeCache));
|
|
1258
|
+
projection = {
|
|
1259
|
+
remove: onRemove,
|
|
1260
|
+
pending: 0,
|
|
1261
|
+
outcome: "error",
|
|
1262
|
+
runtime,
|
|
1263
|
+
queries,
|
|
1264
|
+
queryToken,
|
|
1265
|
+
token: runtime?.begin(optimistic, onRemove, () => queryToken.transforms.size > 0)
|
|
1266
|
+
};
|
|
1267
|
+
if (!runtime) {
|
|
1268
|
+
optimistic({ update: () => {
|
|
1269
|
+
return;
|
|
1270
|
+
} });
|
|
1271
|
+
}
|
|
1272
|
+
entries.set(identity, projection);
|
|
1273
|
+
projections.set(owner, entries);
|
|
1274
|
+
}
|
|
1275
|
+
projection.pending += 1;
|
|
1276
|
+
operation.projection = projection;
|
|
1277
|
+
}
|
|
1278
|
+
return resolved;
|
|
1279
|
+
};
|
|
1280
|
+
const options = {
|
|
1281
|
+
...supplied,
|
|
1282
|
+
[OPERATION]: operation,
|
|
1283
|
+
headers
|
|
1284
|
+
};
|
|
1285
|
+
let result;
|
|
1286
|
+
let thrown;
|
|
1287
|
+
try {
|
|
1288
|
+
let attempt = 0;
|
|
1289
|
+
for (;; ) {
|
|
1290
|
+
operation.result = undefined;
|
|
1291
|
+
thrown = undefined;
|
|
1292
|
+
try {
|
|
1293
|
+
result = await Reflect.apply(target, receiver, [args[0], options]);
|
|
1294
|
+
} catch (error) {
|
|
1295
|
+
if (!operation.result) {
|
|
1296
|
+
throw error;
|
|
1297
|
+
}
|
|
1298
|
+
({ result } = operation);
|
|
1299
|
+
thrown = { error };
|
|
1300
|
+
}
|
|
1301
|
+
if (!inProgress(result) || attempt >= retry || options.fetch?.signal?.aborted) {
|
|
1302
|
+
break;
|
|
1303
|
+
}
|
|
1304
|
+
if (!await delay(retryDelay(result.response, attempt), options.fetch?.signal)) {
|
|
1305
|
+
break;
|
|
1306
|
+
}
|
|
1307
|
+
attempt += 1;
|
|
1308
|
+
}
|
|
1309
|
+
} catch (error) {
|
|
1310
|
+
finishOperation(operation, undefined);
|
|
1311
|
+
throw error;
|
|
1312
|
+
}
|
|
1313
|
+
finishOperation(operation, result);
|
|
1314
|
+
if (thrown) {
|
|
1315
|
+
throw thrown.error;
|
|
1316
|
+
}
|
|
1317
|
+
return result;
|
|
1318
|
+
}
|
|
1319
|
+
function finishOperation(operation, result) {
|
|
1320
|
+
const { projection } = operation;
|
|
1321
|
+
if (!projection) {
|
|
1322
|
+
return;
|
|
1323
|
+
}
|
|
1324
|
+
let outcome = "ambiguous";
|
|
1325
|
+
if (result?.error === null) {
|
|
1326
|
+
outcome = "success";
|
|
1327
|
+
} else if (result?.response && result.response.status >= 400 && result.response.status < 500 && !inProgress(result)) {
|
|
1328
|
+
outcome = "error";
|
|
1329
|
+
}
|
|
1330
|
+
if (outcome === "success" || outcome === "ambiguous" && projection.outcome === "error") {
|
|
1331
|
+
projection.outcome = outcome;
|
|
1332
|
+
projection.response = result?.response;
|
|
1333
|
+
}
|
|
1334
|
+
projection.pending -= 1;
|
|
1335
|
+
if (projection.pending === 0) {
|
|
1336
|
+
if (!projection.runtime && projection.queryToken.transforms.size === 0) {
|
|
1337
|
+
projection.remove();
|
|
1338
|
+
}
|
|
1339
|
+
const invalidations = (projection.response ?? result?.response)?.headers.get("x-furin-queries");
|
|
1340
|
+
if (invalidations) {
|
|
1341
|
+
projection.queries.invalidate(JSON.parse(invalidations));
|
|
1342
|
+
}
|
|
1343
|
+
projection.queries.finish(projection.queryToken, projection.outcome);
|
|
1344
|
+
if (projection.runtime && projection.token) {
|
|
1345
|
+
projection.runtime.finish(projection.token, projection.outcome, projection.response ?? result?.response);
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
function wrapClient(client, method, options, projections, queries, reference) {
|
|
1350
|
+
return new Proxy(client, {
|
|
1351
|
+
get(target, property) {
|
|
1352
|
+
if (property === QUERY_REFERENCE && reference) {
|
|
1353
|
+
return reference;
|
|
1354
|
+
}
|
|
1355
|
+
const value = Reflect.get(target, property);
|
|
1356
|
+
if (property === "get" && typeof value === "function") {
|
|
1357
|
+
const context = Reflect.apply(Reflect.get(target, "__furinQuery"), target, []);
|
|
1358
|
+
const url = `${context.domain}/${context.paths.map(encodeURIComponent).join("/")}`;
|
|
1359
|
+
const read = {
|
|
1360
|
+
client: queries,
|
|
1361
|
+
url,
|
|
1362
|
+
load: (readOptions) => Reflect.apply(value, target, [readOptions])
|
|
1363
|
+
};
|
|
1364
|
+
return wrapClient(value, "get", options, projections, queries, read);
|
|
1365
|
+
}
|
|
1366
|
+
return typeof value === "function" ? wrapClient(value, String(property), options, projections, queries) : value;
|
|
1367
|
+
},
|
|
1368
|
+
apply(target, receiver, args) {
|
|
1369
|
+
if (method && MUTATIONS.has(method)) {
|
|
1370
|
+
return runMutation(target, receiver, args, options, projections, queries);
|
|
1371
|
+
}
|
|
1372
|
+
if (reference && method === "get") {
|
|
1373
|
+
const environment = currentQueryEnvironment();
|
|
1374
|
+
if (typeof window === "undefined" && !environment) {
|
|
1375
|
+
return reference.load(args[0]);
|
|
1376
|
+
}
|
|
1377
|
+
const store = environment?.store ?? findOptimisticRuntime(new URL(reference.url).origin)?.queries ?? queries;
|
|
1378
|
+
const url = readUrl(reference, args[0]);
|
|
1379
|
+
const epoch = store.generation();
|
|
1380
|
+
store.bind(url, () => reference.load(args[0]), reference.client);
|
|
1381
|
+
const version = store.version(url);
|
|
1382
|
+
return reference.load(args[0]).then((result) => {
|
|
1383
|
+
store.observe(url, result, epoch, version);
|
|
1384
|
+
environment?.onRead();
|
|
1385
|
+
return result;
|
|
1386
|
+
});
|
|
1387
|
+
}
|
|
1388
|
+
const value = Reflect.apply(target, receiver, args);
|
|
1389
|
+
return typeof value === "function" ? wrapClient(value, undefined, options, projections, queries) : value;
|
|
1390
|
+
}
|
|
1391
|
+
});
|
|
1392
|
+
}
|
|
1393
|
+
function withSync(client, options) {
|
|
1394
|
+
return wrapClient(client.use(plugin), undefined, options, new Map, new QueryStore(typeof window === "undefined" ? undefined : window.location.origin));
|
|
1395
|
+
}
|
|
1396
|
+
function createClient(domain, options) {
|
|
1397
|
+
const { retry, ...eden } = options ?? {};
|
|
1398
|
+
return withSync(ge(domain, eden), { retry });
|
|
239
1399
|
}
|
|
240
1400
|
// src/shared/await.tsx
|
|
241
|
-
import { Component, createContext as
|
|
242
|
-
var AsyncErrorContext =
|
|
1401
|
+
import { Component, createContext as createContext4, createElement as createElement3, use, useMemo } from "react";
|
|
1402
|
+
var AsyncErrorContext = createContext4(undefined);
|
|
243
1403
|
function useAsyncError() {
|
|
244
1404
|
return use(AsyncErrorContext);
|
|
245
1405
|
}
|
|
@@ -250,7 +1410,7 @@ function useAsyncValue() {
|
|
|
250
1410
|
}
|
|
251
1411
|
return use(ctx.promise);
|
|
252
1412
|
}
|
|
253
|
-
var AwaitValueContext =
|
|
1413
|
+
var AwaitValueContext = createContext4(undefined);
|
|
254
1414
|
function isAbortError(error) {
|
|
255
1415
|
return error instanceof Error && error.name === "AbortError";
|
|
256
1416
|
}
|
|
@@ -297,8 +1457,7 @@ function AwaitInner({ resolve, children }) {
|
|
|
297
1457
|
const value = use(resolve);
|
|
298
1458
|
return createElement3(AwaitValueContext.Provider, { value: { promise: resolve } }, children ? children(value) : null);
|
|
299
1459
|
}
|
|
300
|
-
|
|
301
|
-
// src/client.ts
|
|
1460
|
+
// src/shared/defer.ts
|
|
302
1461
|
var DEFERRED_BRAND = Symbol.for("@teyik0/furin/deferred");
|
|
303
1462
|
function defer(data) {
|
|
304
1463
|
if (Object.hasOwn(data, DEFERRED_BRAND)) {
|
|
@@ -314,13 +1473,19 @@ export {
|
|
|
314
1473
|
DocumentProvider,
|
|
315
1474
|
HeadContent,
|
|
316
1475
|
Scripts,
|
|
1476
|
+
clientModule,
|
|
1477
|
+
createClient,
|
|
317
1478
|
defer,
|
|
318
1479
|
defineRootRoute,
|
|
319
1480
|
defineRoute,
|
|
1481
|
+
getRouteApi,
|
|
320
1482
|
isDeferred,
|
|
1483
|
+
preloadClientModule,
|
|
321
1484
|
reconcileHotComponentRegistry,
|
|
322
1485
|
updateHotComponent,
|
|
323
1486
|
useAsyncError,
|
|
324
1487
|
useAsyncValue,
|
|
325
|
-
|
|
1488
|
+
useMutation,
|
|
1489
|
+
useQuery,
|
|
1490
|
+
withSync
|
|
326
1491
|
};
|