@kerf-js/start 0.0.0-stage → 0.1.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/LICENSE +16 -0
- package/README.md +1468 -2
- package/crate/Cargo.lock +1347 -0
- package/crate/Cargo.toml +72 -0
- package/crate/README.md +322 -0
- package/crate/rust-toolchain.toml +3 -0
- package/crate/src/build/emit.rs +103 -0
- package/crate/src/build.rs +367 -0
- package/crate/src/ctx.rs +186 -0
- package/crate/src/envelope.rs +163 -0
- package/crate/src/functions.rs +173 -0
- package/crate/src/lib.rs +118 -0
- package/crate/src/native.rs +212 -0
- package/crate/src/otel.rs +62 -0
- package/crate/src/server.rs +203 -0
- package/package.json +123 -3
- package/src/app.mjs +136 -0
- package/src/asset-receipt.d.mts +13 -0
- package/src/asset-receipt.mjs +61 -0
- package/src/auth/better-auth.ts +280 -0
- package/src/auth/drizzle.ts +17 -0
- package/src/auth/emails.ts +44 -0
- package/src/auth.ts +62 -0
- package/src/boot.ts +57 -0
- package/src/builtin.tsx +19 -0
- package/src/bus/postgres.ts +104 -0
- package/src/bus.ts +51 -0
- package/src/cache.ts +4 -0
- package/src/capture.ts +50 -0
- package/src/changes-sql.d.mts +20 -0
- package/src/changes-sql.mjs +115 -0
- package/src/changes.ts +352 -0
- package/src/channel/connections.ts +216 -0
- package/src/channel/socket.ts +396 -0
- package/src/channel-fake.ts +70 -0
- package/src/channel.ts +250 -0
- package/src/client.tsx +27 -0
- package/src/contract/describe.mjs +237 -0
- package/src/contract.mjs +299 -0
- package/src/cron.ts +74 -0
- package/src/data/after.ts +57 -0
- package/src/data/cache.ts +354 -0
- package/src/data/definitions.ts +94 -0
- package/src/data/draft.ts +104 -0
- package/src/data/foundation.ts +95 -0
- package/src/data/live-api.ts +22 -0
- package/src/data/mutation.ts +138 -0
- package/src/data/primitives.ts +135 -0
- package/src/data/query.ts +106 -0
- package/src/data/transport.ts +123 -0
- package/src/data/types.ts +333 -0
- package/src/data.ts +125 -0
- package/src/db/bun-sqlite.d.ts +13 -0
- package/src/db/changes-mysql.ts +126 -0
- package/src/db/changes-postgres.ts +238 -0
- package/src/db/changes-sqlite.ts +183 -0
- package/src/db/core.ts +95 -0
- package/src/db/index.ts +43 -0
- package/src/db/mysql.ts +84 -0
- package/src/db/postgres.ts +102 -0
- package/src/db/sqlite.ts +166 -0
- package/src/derive.d.mts +19 -0
- package/src/derive.mjs +237 -0
- package/src/dev-runtime.mjs +46 -0
- package/src/email.ts +146 -0
- package/src/exec/errors.ts +87 -0
- package/src/exec/impl.ts +21 -0
- package/src/exec/landing.ts +66 -0
- package/src/exec/limit.ts +25 -0
- package/src/exec/once.ts +24 -0
- package/src/exec/scope.ts +328 -0
- package/src/exec/touch.ts +80 -0
- package/src/exec/validation.ts +176 -0
- package/src/exec/warm.ts +50 -0
- package/src/exec.ts +400 -0
- package/src/extension.ts +48 -0
- package/src/first-paint.ts +74 -0
- package/src/form/value.ts +15 -0
- package/src/form.tsx +223 -0
- package/src/gate.css +6 -0
- package/src/hub/channels.ts +102 -0
- package/src/hub/connections.ts +147 -0
- package/src/hub/delivery.ts +62 -0
- package/src/hub/feed.ts +168 -0
- package/src/hub/presence.ts +64 -0
- package/src/hub/route.ts +159 -0
- package/src/hub/subscriptions.ts +213 -0
- package/src/hub/topic.ts +98 -0
- package/src/hub/tracked.ts +12 -0
- package/src/hub/types.ts +190 -0
- package/src/hub/view.ts +147 -0
- package/src/hub.ts +196 -0
- package/src/index.ts +184 -0
- package/src/inline.mjs +359 -0
- package/src/inspect/compare.mjs +162 -0
- package/src/inspect/routes.mjs +154 -0
- package/src/inspect/samples.mjs +59 -0
- package/src/inspect/url.mjs +2 -0
- package/src/inspect.mjs +239 -0
- package/src/jobs.ts +223 -0
- package/src/link/election.ts +178 -0
- package/src/link/lines.ts +27 -0
- package/src/link.ts +385 -0
- package/src/list-drizzle.ts +136 -0
- package/src/list.ts +315 -0
- package/src/listen.ts +54 -0
- package/src/live/api.ts +18 -0
- package/src/live/backoff.ts +6 -0
- package/src/live/frames.ts +42 -0
- package/src/live/read.ts +77 -0
- package/src/live/sub-types.ts +24 -0
- package/src/live/transport.ts +86 -0
- package/src/live/types.ts +52 -0
- package/src/live-fake.ts +57 -0
- package/src/live-target.mjs +231 -0
- package/src/live.ts +390 -0
- package/src/media.ts +50 -0
- package/src/migrations.mjs +27 -0
- package/src/mux-session.ts +6 -0
- package/src/mux.ts +232 -0
- package/src/og/DefaultCard.tsx +91 -0
- package/src/og/fonts.mjs +72 -0
- package/src/og/images.mjs +124 -0
- package/src/og/index.mjs +222 -0
- package/src/og/native.mjs +36 -0
- package/src/range.mjs +31 -0
- package/src/router.ts +199 -0
- package/src/routes.mjs +193 -0
- package/src/run/admission.ts +67 -0
- package/src/run/boot.tsx +115 -0
- package/src/run/cache-presentation.ts +67 -0
- package/src/run/cache.ts +254 -0
- package/src/run/helpers.ts +197 -0
- package/src/run/listeners.ts +166 -0
- package/src/run/navigation.ts +385 -0
- package/src/run/page-skeleton.tsx +35 -0
- package/src/run/prefetch.ts +129 -0
- package/src/run/retain-view.ts +18 -0
- package/src/run/shape-preparation.ts +97 -0
- package/src/run/shape-styles.ts +72 -0
- package/src/run/source-context/pin.ts +124 -0
- package/src/run/source-context.ts +311 -0
- package/src/run.tsx +74 -0
- package/src/rust/common.ts +163 -0
- package/src/rust/native.ts +282 -0
- package/src/rust.ts +207 -0
- package/src/server/background.ts +34 -0
- package/src/server/card.tsx +65 -0
- package/src/server/compression.ts +115 -0
- package/src/server/core.ts +70 -0
- package/src/server/cron.ts +49 -0
- package/src/server/dispatch.ts +73 -0
- package/src/server/form.ts +92 -0
- package/src/server/functions.ts +133 -0
- package/src/server/handler.ts +57 -0
- package/src/server/head.ts +96 -0
- package/src/server/health.ts +36 -0
- package/src/server/jobs.ts +112 -0
- package/src/server/lifecycle.ts +48 -0
- package/src/server/log.ts +42 -0
- package/src/server/media.ts +61 -0
- package/src/server/navigation-cache.ts +75 -0
- package/src/server/navigation.tsx +86 -0
- package/src/server/page-cold.ts +96 -0
- package/src/server/page-stream.tsx +181 -0
- package/src/server/page-types.ts +51 -0
- package/src/server/page-warm.ts +131 -0
- package/src/server/page.ts +69 -0
- package/src/server/payload.ts +89 -0
- package/src/server/prerender.ts +64 -0
- package/src/server/preview.tsx +67 -0
- package/src/server/render.ts +197 -0
- package/src/server/request.ts +192 -0
- package/src/server/resolver.ts +228 -0
- package/src/server/routes.ts +150 -0
- package/src/server/security.ts +50 -0
- package/src/server/services.ts +69 -0
- package/src/server/shutdown.ts +23 -0
- package/src/server/text.ts +29 -0
- package/src/server/types.ts +170 -0
- package/src/server.tsx +138 -0
- package/src/shape.d.mts +3 -0
- package/src/shape.mjs +97 -0
- package/src/share/display.ts +50 -0
- package/src/share/network.ts +119 -0
- package/src/share/receive.ts +70 -0
- package/src/share/state.ts +30 -0
- package/src/share/tab-queue.ts +28 -0
- package/src/share-server.ts +293 -0
- package/src/share.ts +397 -0
- package/src/sheets.mjs +87 -0
- package/src/sidecar/build.mjs +170 -0
- package/src/sidecar/cargo.mjs +164 -0
- package/src/sidecar/common.mjs +17 -0
- package/src/sidecar/dev-build.mjs +91 -0
- package/src/sidecar/dev-watch.mjs +88 -0
- package/src/sidecar/manifest.mjs +19 -0
- package/src/sidecar/process.mjs +58 -0
- package/src/sidecar/refresh.mjs +19 -0
- package/src/sidecar.mjs +162 -0
- package/src/socket/check.ts +50 -0
- package/src/socket/deliver.ts +16 -0
- package/src/socket/publish.ts +56 -0
- package/src/socket/rooms.ts +41 -0
- package/src/socket/route.ts +79 -0
- package/src/socket/shared.ts +26 -0
- package/src/socket/types.ts +48 -0
- package/src/socket.ts +400 -0
- package/src/stream.ts +22 -0
- package/src/verify/app.ts +39 -0
- package/src/verify/cleanup.ts +27 -0
- package/src/verify/database.ts +11 -0
- package/src/verify/live.ts +63 -0
- package/src/verify/notify.ts +35 -0
- package/src/verify/skip.ts +19 -0
- package/src/verify/support.ts +149 -0
- package/src/verify/timing.ts +15 -0
- package/src/verify/trigger.ts +14 -0
- package/src/verify.ts +152 -0
- package/src/view.tsx +132 -0
- package/src/vite/asset-bytes.mjs +95 -0
- package/src/vite/assets.mjs +117 -0
- package/src/vite/config-hooks.mjs +166 -0
- package/src/vite/dev-hooks.mjs +103 -0
- package/src/vite/dev.mjs +102 -0
- package/src/vite/entries.mjs +149 -0
- package/src/vite/module-hooks.mjs +290 -0
- package/src/vite/route-modules.mjs +289 -0
- package/src/vite/server-functions.mjs +222 -0
- package/src/vite/shape-entry-css.mjs +187 -0
- package/src/vite/shape-override.mjs +226 -0
- package/src/vite/shared.mjs +55 -0
- package/src/vite/sheets.mjs +92 -0
- package/src/vite/state.mjs +89 -0
- package/src/vite/syntax.mjs +139 -0
- package/src/vite.mjs +202 -0
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import {
|
|
2
|
+
changedNode,
|
|
3
|
+
ownerOf,
|
|
4
|
+
patchItem,
|
|
5
|
+
pin,
|
|
6
|
+
relevant,
|
|
7
|
+
scan,
|
|
8
|
+
SELECTOR,
|
|
9
|
+
type Retained,
|
|
10
|
+
} from './source-context/pin'
|
|
11
|
+
|
|
12
|
+
export interface SourceContext {
|
|
13
|
+
/** Stop observing before incoming setup; keep controls pinned until the swap. */
|
|
14
|
+
stop(): void
|
|
15
|
+
release(): void
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type Original = (...args: unknown[]) => unknown
|
|
19
|
+
|
|
20
|
+
const active = new WeakSet<Document>()
|
|
21
|
+
const EVENTS = ['pointerdown', 'click', 'auxclick', 'submit']
|
|
22
|
+
const AFTER = ['click', 'auxclick', 'submit']
|
|
23
|
+
|
|
24
|
+
function install(
|
|
25
|
+
c: Retained,
|
|
26
|
+
target: object,
|
|
27
|
+
name: string,
|
|
28
|
+
make: (d: PropertyDescriptor) => PropertyDescriptor,
|
|
29
|
+
required = true,
|
|
30
|
+
) {
|
|
31
|
+
const descriptor = Object.getOwnPropertyDescriptor(target, name)
|
|
32
|
+
if (!descriptor) {
|
|
33
|
+
if (required) throw new Error('Missing control hook')
|
|
34
|
+
return
|
|
35
|
+
}
|
|
36
|
+
if (!descriptor.configurable) throw new Error('Locked control hook')
|
|
37
|
+
const replacement = make(descriptor)
|
|
38
|
+
Object.defineProperty(target, name, replacement)
|
|
39
|
+
c.wrappers.push({ target, name, descriptor, replacement })
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function restore(c: Retained) {
|
|
43
|
+
for (const { target, name, descriptor, replacement } of c.wrappers.reverse()) {
|
|
44
|
+
const now = Object.getOwnPropertyDescriptor(target, name)
|
|
45
|
+
if (
|
|
46
|
+
now &&
|
|
47
|
+
now.get === replacement.get &&
|
|
48
|
+
now.set === replacement.set &&
|
|
49
|
+
now.value === replacement.value &&
|
|
50
|
+
now.writable === replacement.writable &&
|
|
51
|
+
now.enumerable === replacement.enumerable &&
|
|
52
|
+
now.configurable === replacement.configurable
|
|
53
|
+
)
|
|
54
|
+
Object.defineProperty(target, name, descriptor)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function wrapMethods(
|
|
59
|
+
c: Retained,
|
|
60
|
+
target: object,
|
|
61
|
+
names: string[],
|
|
62
|
+
make: (name: string, original: Original) => Original,
|
|
63
|
+
) {
|
|
64
|
+
for (const name of names)
|
|
65
|
+
install(c, target, name, (descriptor) => ({
|
|
66
|
+
...descriptor,
|
|
67
|
+
value: make(name, descriptor.value as Original),
|
|
68
|
+
}))
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function hookAttributes(c: Retained) {
|
|
72
|
+
install(c, c.win.Element.prototype, 'attributes', (descriptor) => ({
|
|
73
|
+
...descriptor,
|
|
74
|
+
get(this: Element) {
|
|
75
|
+
// oxlint-disable-next-line typescript/unbound-method -- the native method is kept to be called with .call()
|
|
76
|
+
const map = Reflect.apply(descriptor.get!, this, []) as NamedNodeMap
|
|
77
|
+
if (!c.stopped) c.owners.set(map, this)
|
|
78
|
+
return map
|
|
79
|
+
},
|
|
80
|
+
}))
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function hookValues(c: Retained) {
|
|
84
|
+
const { win, root } = c
|
|
85
|
+
for (const [prototype, property] of [
|
|
86
|
+
[win.Attr.prototype, 'value'],
|
|
87
|
+
[win.Node.prototype, 'nodeValue'],
|
|
88
|
+
[win.Node.prototype, 'textContent'],
|
|
89
|
+
] as const) {
|
|
90
|
+
install(c, prototype, property, (descriptor) => ({
|
|
91
|
+
...descriptor,
|
|
92
|
+
set(this: Node, value: string | null) {
|
|
93
|
+
if (!c.stopped && this instanceof win.Attr && this.namespaceURI === null) {
|
|
94
|
+
const item = this.ownerElement,
|
|
95
|
+
name = this.localName.toLowerCase()
|
|
96
|
+
if (item && root.contains(item) && relevant(item, name)) {
|
|
97
|
+
// oxlint-disable-next-line typescript/no-unnecessary-type-conversion -- a DOM setter can receive null at runtime
|
|
98
|
+
const raw = property === 'value' ? value : String(value ?? '')
|
|
99
|
+
value = pin(c, item, name, raw, true)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// oxlint-disable-next-line typescript/unbound-method -- the native method is kept to be called with .call()
|
|
103
|
+
Reflect.apply(descriptor.set!, this, [value])
|
|
104
|
+
},
|
|
105
|
+
}))
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function hookNamedNodeMap(c: Retained) {
|
|
110
|
+
const { win } = c
|
|
111
|
+
const names = ['setNamedItem', 'setNamedItemNS', 'removeNamedItem', 'removeNamedItemNS']
|
|
112
|
+
wrapMethods(
|
|
113
|
+
c,
|
|
114
|
+
win.NamedNodeMap.prototype,
|
|
115
|
+
names,
|
|
116
|
+
(method, original) =>
|
|
117
|
+
function (this: NamedNodeMap, ...args: unknown[]) {
|
|
118
|
+
const added = method.startsWith('set')
|
|
119
|
+
const owner = !c.stopped && !added ? ownerOf(c, this) : c.owners.get(this)
|
|
120
|
+
const result = Reflect.apply(original, this, args) as Attr | null
|
|
121
|
+
const attr = added ? args[0] : result
|
|
122
|
+
const item = owner ?? (attr instanceof win.Attr ? attr.ownerElement : null)
|
|
123
|
+
changedNode(c, item, attr, added, result)
|
|
124
|
+
return result
|
|
125
|
+
},
|
|
126
|
+
)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function hookAttributeNodes(c: Retained) {
|
|
130
|
+
const names = ['setAttributeNode', 'setAttributeNodeNS', 'removeAttributeNode']
|
|
131
|
+
wrapMethods(
|
|
132
|
+
c,
|
|
133
|
+
c.win.Element.prototype,
|
|
134
|
+
names,
|
|
135
|
+
(method, original) =>
|
|
136
|
+
function (this: Element, ...args: unknown[]) {
|
|
137
|
+
const result = Reflect.apply(original, this, args) as Attr | null
|
|
138
|
+
const added = method !== 'removeAttributeNode'
|
|
139
|
+
changedNode(c, this, added ? args[0] : result, added, result)
|
|
140
|
+
return result
|
|
141
|
+
},
|
|
142
|
+
)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function hookAttributeMethods(c: Retained) {
|
|
146
|
+
const names = ['setAttribute', 'removeAttribute', 'setAttributeNS', 'removeAttributeNS']
|
|
147
|
+
wrapMethods(
|
|
148
|
+
c,
|
|
149
|
+
c.win.Element.prototype,
|
|
150
|
+
names,
|
|
151
|
+
(name, original) =>
|
|
152
|
+
function (this: Element, ...args: unknown[]) {
|
|
153
|
+
const namespaced = name.endsWith('NS')
|
|
154
|
+
const key = String(args[namespaced ? 1 : 0])
|
|
155
|
+
const attr = key.toLowerCase()
|
|
156
|
+
const local = !namespaced || args[0] === null || args[0] === ''
|
|
157
|
+
if (!c.stopped && local && c.root.contains(this) && relevant(this, attr)) {
|
|
158
|
+
const raw = name.startsWith('remove') ? null : String(args[namespaced ? 2 : 1])
|
|
159
|
+
const value = pin(c, this, attr, raw, true)
|
|
160
|
+
if (value === null) return Reflect.apply(c.nativeRemove, this, [key])
|
|
161
|
+
return Reflect.apply(c.nativeSet, this, [key, value])
|
|
162
|
+
}
|
|
163
|
+
return Reflect.apply(original, this, args)
|
|
164
|
+
},
|
|
165
|
+
)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function hookReflected(c: Retained) {
|
|
169
|
+
const { win } = c
|
|
170
|
+
for (const [prototype, property, attr] of [
|
|
171
|
+
[win.HTMLAnchorElement.prototype, 'href', 'href'],
|
|
172
|
+
[win.HTMLFormElement.prototype, 'action', 'action'],
|
|
173
|
+
[win.HTMLButtonElement.prototype, 'formAction', 'formaction'],
|
|
174
|
+
[win.HTMLInputElement.prototype, 'formAction', 'formaction'],
|
|
175
|
+
] as const) {
|
|
176
|
+
install(
|
|
177
|
+
c,
|
|
178
|
+
prototype,
|
|
179
|
+
property,
|
|
180
|
+
(descriptor) => ({
|
|
181
|
+
...descriptor,
|
|
182
|
+
set(this: Element, value: string) {
|
|
183
|
+
const next = !c.stopped && c.root.contains(this) ? pin(c, this, attr, value, true) : value
|
|
184
|
+
// oxlint-disable-next-line typescript/unbound-method -- the native method is kept to be called with .call()
|
|
185
|
+
Reflect.apply(descriptor.set!, this, [next])
|
|
186
|
+
},
|
|
187
|
+
}),
|
|
188
|
+
false,
|
|
189
|
+
)
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function hookSubmit(c: Retained) {
|
|
194
|
+
wrapMethods(
|
|
195
|
+
c,
|
|
196
|
+
c.win.HTMLFormElement.prototype,
|
|
197
|
+
['submit', 'requestSubmit'],
|
|
198
|
+
(_, original) =>
|
|
199
|
+
function (this: HTMLFormElement, ...args: unknown[]) {
|
|
200
|
+
if (!c.stopped && c.root.contains(this)) {
|
|
201
|
+
patchItem(c, this)
|
|
202
|
+
if (args[0] instanceof Element && c.root.contains(args[0])) patchItem(c, args[0])
|
|
203
|
+
}
|
|
204
|
+
return Reflect.apply(original, this, args)
|
|
205
|
+
},
|
|
206
|
+
)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function observe(c: Retained, doc: Document) {
|
|
210
|
+
const { root } = c
|
|
211
|
+
const activate = (event: Event) => {
|
|
212
|
+
for (const item of event.composedPath())
|
|
213
|
+
if (item instanceof Element && root.contains(item)) patchItem(c, item)
|
|
214
|
+
if ('submitter' in event) {
|
|
215
|
+
const submitter = (event as SubmitEvent).submitter
|
|
216
|
+
if (submitter && root.contains(submitter)) patchItem(c, submitter)
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
for (const type of EVENTS) doc.addEventListener(type, activate, true)
|
|
220
|
+
for (const type of AFTER) doc.addEventListener(type, activate)
|
|
221
|
+
const observer = new MutationObserver((records) => {
|
|
222
|
+
for (const record of records) {
|
|
223
|
+
if (
|
|
224
|
+
record.type === 'attributes' &&
|
|
225
|
+
record.target instanceof Element &&
|
|
226
|
+
root.contains(record.target)
|
|
227
|
+
)
|
|
228
|
+
patchItem(c, record.target)
|
|
229
|
+
for (const node of record.addedNodes) {
|
|
230
|
+
if (!(node instanceof Element) || !root.contains(node)) continue
|
|
231
|
+
if (node.matches(SELECTOR)) patchItem(c, node)
|
|
232
|
+
scan(c, node)
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
})
|
|
236
|
+
observer.observe(root, {
|
|
237
|
+
subtree: true,
|
|
238
|
+
childList: true,
|
|
239
|
+
attributes: true,
|
|
240
|
+
attributeFilter: ['href', 'action', 'formaction'],
|
|
241
|
+
})
|
|
242
|
+
return () => {
|
|
243
|
+
observer.disconnect()
|
|
244
|
+
for (const type of EVENTS) doc.removeEventListener(type, activate, true)
|
|
245
|
+
for (const type of AFTER) doc.removeEventListener(type, activate)
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** Keep retained controls on their committed URL without changing the document base. */
|
|
250
|
+
export function retainSourceContext(
|
|
251
|
+
doc: Document,
|
|
252
|
+
source: string,
|
|
253
|
+
base: string,
|
|
254
|
+
): SourceContext | null {
|
|
255
|
+
const root = doc.getElementById('app') ?? doc.body
|
|
256
|
+
const win = doc.defaultView
|
|
257
|
+
if (!root || !win || active.has(doc)) return null
|
|
258
|
+
const c: Retained = {
|
|
259
|
+
root,
|
|
260
|
+
win,
|
|
261
|
+
source,
|
|
262
|
+
base,
|
|
263
|
+
saved: new Map(),
|
|
264
|
+
owners: new WeakMap(),
|
|
265
|
+
// oxlint-disable-next-line typescript/unbound-method -- the native method is kept to be called with .call()
|
|
266
|
+
nativeSet: win.Element.prototype.setAttribute,
|
|
267
|
+
// oxlint-disable-next-line typescript/unbound-method -- the native method is kept to be called with .call()
|
|
268
|
+
nativeRemove: win.Element.prototype.removeAttribute,
|
|
269
|
+
// oxlint-disable-next-line typescript/unbound-method -- the native setter is kept to be called with .call()
|
|
270
|
+
attrValue: Object.getOwnPropertyDescriptor(win.Attr.prototype, 'value')?.set,
|
|
271
|
+
stopped: false,
|
|
272
|
+
wrappers: [],
|
|
273
|
+
}
|
|
274
|
+
try {
|
|
275
|
+
hookAttributes(c)
|
|
276
|
+
hookValues(c)
|
|
277
|
+
hookNamedNodeMap(c)
|
|
278
|
+
hookAttributeNodes(c)
|
|
279
|
+
hookAttributeMethods(c)
|
|
280
|
+
hookReflected(c)
|
|
281
|
+
hookSubmit(c)
|
|
282
|
+
} catch {
|
|
283
|
+
restore(c)
|
|
284
|
+
return null
|
|
285
|
+
}
|
|
286
|
+
active.add(doc)
|
|
287
|
+
scan(c, root)
|
|
288
|
+
const unobserve = observe(c, doc)
|
|
289
|
+
const stop = () => {
|
|
290
|
+
if (c.stopped) return
|
|
291
|
+
c.stopped = true
|
|
292
|
+
active.delete(doc)
|
|
293
|
+
unobserve()
|
|
294
|
+
restore(c)
|
|
295
|
+
}
|
|
296
|
+
let released = false
|
|
297
|
+
return {
|
|
298
|
+
stop,
|
|
299
|
+
release() {
|
|
300
|
+
if (released) return
|
|
301
|
+
released = true
|
|
302
|
+
stop()
|
|
303
|
+
for (const [item, records] of c.saved)
|
|
304
|
+
for (const [name, action] of records) {
|
|
305
|
+
if (item.getAttribute(name) !== action.installed) continue
|
|
306
|
+
if (action.original === null) item.removeAttribute(name)
|
|
307
|
+
else item.setAttribute(name, action.original)
|
|
308
|
+
}
|
|
309
|
+
},
|
|
310
|
+
}
|
|
311
|
+
}
|
package/src/run.tsx
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// The client (`client.tsx` starts it) and its router (research 25 §3): hydrate the server's page, then navigate without page loads. A navigation
|
|
2
|
+
// fetches `/_kerf/data/<path>` (the server matches, runs the loaders and merges the head, so loaders never ship to
|
|
3
|
+
// the client), loads the route chunks it needs (started in parallel from a guess against the route table), and
|
|
4
|
+
// writes the levels: a level that keeps its route keeps its component and gets new params/data in place.
|
|
5
|
+
// `setHead` writes the server's merged tags for the tab title and history; crawlers only read the server's HTML.
|
|
6
|
+
//
|
|
7
|
+
// Server functions (research 27): the query cache seeds itself from the page's `#kerf-cache` as it loads, and gets
|
|
8
|
+
// each navigation's `c` through `client.seed`, so hydration fetches nothing (and an app without queries doesn't ship
|
|
9
|
+
// the cache). Redirects from loaders and server functions navigate (at most 5 in a row); a
|
|
10
|
+
// mutation that invalidates a tag the current route's loaders read refetches the route data in place.
|
|
11
|
+
|
|
12
|
+
import { client, type Component } from './router'
|
|
13
|
+
import { mount, prepare } from './run/boot'
|
|
14
|
+
import { listeners } from './run/listeners'
|
|
15
|
+
import { Navigation } from './run/navigation'
|
|
16
|
+
import { prepareRouteShape } from './run/shape-preparation'
|
|
17
|
+
|
|
18
|
+
type Hot = ImportMeta & {
|
|
19
|
+
hot?: { on(event: string, on: (data: { queries: boolean }) => unknown): void }
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Synchronous shape metadata from separately compiled modules. CSS and emitted
|
|
24
|
+
* asset bytes are null until known; consumers must not treat them as available
|
|
25
|
+
* or free. cssText holds generated groups and resolved static entry and level
|
|
26
|
+
* styles, including tokens. cssStatus remains partial.
|
|
27
|
+
* Unavailable covers unproven overrides and unsupported route files.
|
|
28
|
+
*/
|
|
29
|
+
export type RouteShape =
|
|
30
|
+
| { kind: 'disabled'; css: string[] | null; bytes: number | null }
|
|
31
|
+
| {
|
|
32
|
+
kind: 'unavailable'
|
|
33
|
+
reason: 'unresolved-export' | 'unsupported-file' | 'missing-shape'
|
|
34
|
+
css: null
|
|
35
|
+
bytes: null
|
|
36
|
+
}
|
|
37
|
+
| {
|
|
38
|
+
kind: 'generated' | 'override'
|
|
39
|
+
component: Component
|
|
40
|
+
css: string[] | null
|
|
41
|
+
cssText: string[]
|
|
42
|
+
cssStatus: 'partial'
|
|
43
|
+
bytes: number | null
|
|
44
|
+
/** A generated shape with no structure of its own (`$kerfShapeThin`): the router stands in for it. */
|
|
45
|
+
thin?: boolean
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Shape entries are keyed by level id, including layouts and error routes. */
|
|
49
|
+
export type RouteShapes = Record<string, RouteShape>
|
|
50
|
+
|
|
51
|
+
/** The client half of `virtual:kerf/routes`: the page routes in match order, and a chunk loader per route id. */
|
|
52
|
+
export interface ClientRoutes {
|
|
53
|
+
build?: string | null
|
|
54
|
+
table: { id: string; pattern: string; layouts: string[] }[]
|
|
55
|
+
load: Record<string, () => Promise<{ default?: Component }>>
|
|
56
|
+
/** Eager shape metadata, keyed by route or layout id. */
|
|
57
|
+
shapes?: RouteShapes
|
|
58
|
+
/** Each island's code, by id: all a static level loads (PLAN §11). */
|
|
59
|
+
islands?: Record<string, () => Promise<{ default: Component }>>
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Hydrate the page in `#app` and take over its links. Resolves once hydrated. */
|
|
63
|
+
export async function start(routes: ClientRoutes, options: { container?: Element } = {}) {
|
|
64
|
+
const prepared = prepare(routes, options)
|
|
65
|
+
await Promise.all(prepared.loads)
|
|
66
|
+
const page = mount(prepared)
|
|
67
|
+
page.route.prepareShape = (id) => prepareRouteShape(routes, id, document)
|
|
68
|
+
const navigation = new Navigation(routes, page)
|
|
69
|
+
client.navigate = navigation.go
|
|
70
|
+
listeners(navigation, () => (import.meta as Hot).hot)
|
|
71
|
+
// The router follows links from here: the browser's prefetching for full loads (capture.ts `speculate`) stops.
|
|
72
|
+
document.getElementById('kerf-speculate')?.remove()
|
|
73
|
+
return page.route
|
|
74
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { raise, StartError, type Context, type Fn } from '../data'
|
|
2
|
+
import type { Claims, RemoteResult } from '../rust'
|
|
3
|
+
type User = Context extends { user: infer U } ? U | null : unknown
|
|
4
|
+
|
|
5
|
+
export const DEFAULT_TIMEOUT = 10_000
|
|
6
|
+
export const encoder = new TextEncoder()
|
|
7
|
+
|
|
8
|
+
// An app's tsc may have no Node types, so what's read of `process` is named here.
|
|
9
|
+
export const nodeHost = globalThis as unknown as {
|
|
10
|
+
process?: {
|
|
11
|
+
env: Record<string, string | undefined>
|
|
12
|
+
platform: string
|
|
13
|
+
arch: string
|
|
14
|
+
getBuiltinModule?(name: string): unknown
|
|
15
|
+
dlopen(mod: object, file: string): void
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export const env = (name: string): string | undefined => nodeHost.process?.env?.[name] || undefined
|
|
19
|
+
export const randomHex = (bytes: number) =>
|
|
20
|
+
Array.from(crypto.getRandomValues(new Uint8Array(bytes)), (x) =>
|
|
21
|
+
x.toString(16).padStart(2, '0'),
|
|
22
|
+
).join('')
|
|
23
|
+
|
|
24
|
+
export const defaultClaims = (user: User): Claims | null => {
|
|
25
|
+
const u = user as { id: unknown; roles?: string[]; role?: string; tenant?: string | null } | null
|
|
26
|
+
return (
|
|
27
|
+
u && {
|
|
28
|
+
id: String(u.id),
|
|
29
|
+
roles: u.roles ?? (u.role ? [u.role] : []),
|
|
30
|
+
...(u.tenant != null && { tenant: u.tenant }),
|
|
31
|
+
}
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const TRACEPARENT = /^00-(?!0{32})[0-9a-f]{32}-(?!0{16})[0-9a-f]{16}-[0-9a-f]{2}$/
|
|
36
|
+
|
|
37
|
+
/** The caller's trace with a new span, or a new trace. */
|
|
38
|
+
function traceparent(request: Request): string {
|
|
39
|
+
const inbound = request.headers.get('traceparent')
|
|
40
|
+
const trace = inbound && TRACEPARENT.test(inbound) ? inbound.split('-')[1] : randomHex(16)
|
|
41
|
+
return `00-${trace}-${randomHex(8)}-01`
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The value as a sidecar reads it: what the schema parsed, encoded back to the wire shape (codecs, defaults filled). */
|
|
45
|
+
export function wire(fn: Fn, input: unknown): unknown {
|
|
46
|
+
const schema = fn.def.input as { encodeAsync?: (value: unknown) => Promise<unknown> } | undefined
|
|
47
|
+
if (typeof schema?.encodeAsync === 'function') return schema.encodeAsync(input)
|
|
48
|
+
return input
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function checkedOutput(fn: Fn, value: unknown, validate: boolean): Promise<unknown> {
|
|
52
|
+
const schema = fn.def.output
|
|
53
|
+
if (!schema || !validate) return value
|
|
54
|
+
const r = await schema['~standard'].validate(value)
|
|
55
|
+
if (!r.issues) return r.value
|
|
56
|
+
const issues = r.issues
|
|
57
|
+
.map(
|
|
58
|
+
(i) =>
|
|
59
|
+
`${i.path?.map((p) => (typeof p === 'object' ? p.key : p)).join('.') ?? ''}: ${i.message}`,
|
|
60
|
+
)
|
|
61
|
+
.join('; ')
|
|
62
|
+
throw new StartError(
|
|
63
|
+
'R103',
|
|
64
|
+
`\`${fn.$id}\`'s sidecar returned a value its \`output\` schema rejects: ${issues}`,
|
|
65
|
+
'the sidecar was built from another contract, or sets a field the schema refuses: rebuild it, or fix the schema',
|
|
66
|
+
{ status: 502 },
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** A declared error: its data checked like any output, with the status a client can act on. */
|
|
71
|
+
async function declared(fn: Fn, error: { code: string; data?: unknown }): Promise<never> {
|
|
72
|
+
const schema = fn.def.errors?.[error.code]
|
|
73
|
+
if (!schema)
|
|
74
|
+
throw new StartError(
|
|
75
|
+
'R103',
|
|
76
|
+
`\`${fn.$id}\`'s sidecar answered with the undeclared error \`${error.code}\``,
|
|
77
|
+
'declare it in `errors`, or rebuild the sidecar from the current contract',
|
|
78
|
+
{ status: 502 },
|
|
79
|
+
)
|
|
80
|
+
const r = await schema['~standard'].validate(error.data ?? null)
|
|
81
|
+
if (r.issues)
|
|
82
|
+
throw new StartError(
|
|
83
|
+
'R103',
|
|
84
|
+
`\`${fn.$id}\`'s sidecar sent \`${error.code}\` with data its schema rejects`,
|
|
85
|
+
'rebuild the sidecar from the current contract',
|
|
86
|
+
{ status: 502 },
|
|
87
|
+
)
|
|
88
|
+
raise(error.code, r.value)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** A shared value goes by no one's claims: the crate's `SharedCtx` has no user to read, and neither has the wire. */
|
|
92
|
+
export function callContext(
|
|
93
|
+
fn: Fn,
|
|
94
|
+
ctx: Context,
|
|
95
|
+
request: Request,
|
|
96
|
+
timeout: number,
|
|
97
|
+
claims: (user: User) => Claims | null,
|
|
98
|
+
) {
|
|
99
|
+
const shared = fn.def.cache && fn.def.cache.scope !== 'user'
|
|
100
|
+
return {
|
|
101
|
+
user: shared ? null : (claims((ctx as { user?: User }).user ?? null) ?? null),
|
|
102
|
+
requestId: randomHex(8),
|
|
103
|
+
deadlineMs: timeout,
|
|
104
|
+
traceparent: traceparent(request),
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** What a reply comes to: the same for a sidecar's body and an addon's string. */
|
|
109
|
+
export async function settled(
|
|
110
|
+
fn: Fn,
|
|
111
|
+
reply: Reply | undefined,
|
|
112
|
+
target: string,
|
|
113
|
+
timeout: number,
|
|
114
|
+
validate: boolean,
|
|
115
|
+
): Promise<RemoteResult> {
|
|
116
|
+
if (reply?.ok === true)
|
|
117
|
+
return {
|
|
118
|
+
value: await checkedOutput(fn, reply.value, validate),
|
|
119
|
+
tags: reply.tags ?? [],
|
|
120
|
+
touched: reply.touched ?? [],
|
|
121
|
+
}
|
|
122
|
+
const error = reply?.error
|
|
123
|
+
if (error?.code === 'Refused')
|
|
124
|
+
throw new StartError(
|
|
125
|
+
'R106',
|
|
126
|
+
`\`${target}\` refused \`${fn.$id}\`'s signature`,
|
|
127
|
+
'`KERF_RUST_SECRET` must be the same on both sides, and the clocks within a minute',
|
|
128
|
+
{ status: 503 },
|
|
129
|
+
)
|
|
130
|
+
if (error?.code === 'Deadline')
|
|
131
|
+
throw new StartError(
|
|
132
|
+
'R107',
|
|
133
|
+
`\`${fn.$id}\` ran past its ${timeout} ms`,
|
|
134
|
+
'raise `timeout`, or make the Rust answer sooner',
|
|
135
|
+
{ status: 504 },
|
|
136
|
+
)
|
|
137
|
+
if (error?.code === 'Drift')
|
|
138
|
+
throw new StartError(
|
|
139
|
+
'R103',
|
|
140
|
+
`\`${fn.$id}\` doesn't match \`${target}\`: ${error.message}`,
|
|
141
|
+
'rebuild it from the current contract',
|
|
142
|
+
{ status: 502 },
|
|
143
|
+
)
|
|
144
|
+
if (error?.code && error.code !== 'Internal')
|
|
145
|
+
await declared(fn, { code: error.code, data: error.data })
|
|
146
|
+
// The detail stays in a log, under this id: a sidecar's own, or this server's for what ran in this process.
|
|
147
|
+
if (error?.detail)
|
|
148
|
+
console.error(`[kerf] \`${fn.$id}\` failed (request ${error.requestId}): ${error.detail}`)
|
|
149
|
+
throw new StartError(
|
|
150
|
+
'R108',
|
|
151
|
+
`\`${fn.$id}\` failed in \`${target}\` (request ${error?.requestId ?? 'unknown'})`,
|
|
152
|
+
"find the request id in the server's or the sidecar's log",
|
|
153
|
+
{ status: 500 },
|
|
154
|
+
)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export interface Reply {
|
|
158
|
+
ok?: boolean
|
|
159
|
+
value?: unknown
|
|
160
|
+
tags?: string[]
|
|
161
|
+
touched?: string[]
|
|
162
|
+
error?: { code?: string; message?: string; detail?: string; requestId?: string; data?: unknown }
|
|
163
|
+
}
|