@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.
Files changed (236) hide show
  1. package/LICENSE +16 -0
  2. package/README.md +1468 -2
  3. package/crate/Cargo.lock +1347 -0
  4. package/crate/Cargo.toml +72 -0
  5. package/crate/README.md +322 -0
  6. package/crate/rust-toolchain.toml +3 -0
  7. package/crate/src/build/emit.rs +103 -0
  8. package/crate/src/build.rs +367 -0
  9. package/crate/src/ctx.rs +186 -0
  10. package/crate/src/envelope.rs +163 -0
  11. package/crate/src/functions.rs +173 -0
  12. package/crate/src/lib.rs +118 -0
  13. package/crate/src/native.rs +212 -0
  14. package/crate/src/otel.rs +62 -0
  15. package/crate/src/server.rs +203 -0
  16. package/package.json +123 -3
  17. package/src/app.mjs +136 -0
  18. package/src/asset-receipt.d.mts +13 -0
  19. package/src/asset-receipt.mjs +61 -0
  20. package/src/auth/better-auth.ts +280 -0
  21. package/src/auth/drizzle.ts +17 -0
  22. package/src/auth/emails.ts +44 -0
  23. package/src/auth.ts +62 -0
  24. package/src/boot.ts +57 -0
  25. package/src/builtin.tsx +19 -0
  26. package/src/bus/postgres.ts +104 -0
  27. package/src/bus.ts +51 -0
  28. package/src/cache.ts +4 -0
  29. package/src/capture.ts +50 -0
  30. package/src/changes-sql.d.mts +20 -0
  31. package/src/changes-sql.mjs +115 -0
  32. package/src/changes.ts +352 -0
  33. package/src/channel/connections.ts +216 -0
  34. package/src/channel/socket.ts +396 -0
  35. package/src/channel-fake.ts +70 -0
  36. package/src/channel.ts +250 -0
  37. package/src/client.tsx +27 -0
  38. package/src/contract/describe.mjs +237 -0
  39. package/src/contract.mjs +299 -0
  40. package/src/cron.ts +74 -0
  41. package/src/data/after.ts +57 -0
  42. package/src/data/cache.ts +354 -0
  43. package/src/data/definitions.ts +94 -0
  44. package/src/data/draft.ts +104 -0
  45. package/src/data/foundation.ts +95 -0
  46. package/src/data/live-api.ts +22 -0
  47. package/src/data/mutation.ts +138 -0
  48. package/src/data/primitives.ts +135 -0
  49. package/src/data/query.ts +106 -0
  50. package/src/data/transport.ts +123 -0
  51. package/src/data/types.ts +333 -0
  52. package/src/data.ts +125 -0
  53. package/src/db/bun-sqlite.d.ts +13 -0
  54. package/src/db/changes-mysql.ts +126 -0
  55. package/src/db/changes-postgres.ts +238 -0
  56. package/src/db/changes-sqlite.ts +183 -0
  57. package/src/db/core.ts +95 -0
  58. package/src/db/index.ts +43 -0
  59. package/src/db/mysql.ts +84 -0
  60. package/src/db/postgres.ts +102 -0
  61. package/src/db/sqlite.ts +166 -0
  62. package/src/derive.d.mts +19 -0
  63. package/src/derive.mjs +237 -0
  64. package/src/dev-runtime.mjs +46 -0
  65. package/src/email.ts +146 -0
  66. package/src/exec/errors.ts +87 -0
  67. package/src/exec/impl.ts +21 -0
  68. package/src/exec/landing.ts +66 -0
  69. package/src/exec/limit.ts +25 -0
  70. package/src/exec/once.ts +24 -0
  71. package/src/exec/scope.ts +328 -0
  72. package/src/exec/touch.ts +80 -0
  73. package/src/exec/validation.ts +176 -0
  74. package/src/exec/warm.ts +50 -0
  75. package/src/exec.ts +400 -0
  76. package/src/extension.ts +48 -0
  77. package/src/first-paint.ts +74 -0
  78. package/src/form/value.ts +15 -0
  79. package/src/form.tsx +223 -0
  80. package/src/gate.css +6 -0
  81. package/src/hub/channels.ts +102 -0
  82. package/src/hub/connections.ts +147 -0
  83. package/src/hub/delivery.ts +62 -0
  84. package/src/hub/feed.ts +168 -0
  85. package/src/hub/presence.ts +64 -0
  86. package/src/hub/route.ts +159 -0
  87. package/src/hub/subscriptions.ts +213 -0
  88. package/src/hub/topic.ts +98 -0
  89. package/src/hub/tracked.ts +12 -0
  90. package/src/hub/types.ts +190 -0
  91. package/src/hub/view.ts +147 -0
  92. package/src/hub.ts +196 -0
  93. package/src/index.ts +184 -0
  94. package/src/inline.mjs +359 -0
  95. package/src/inspect/compare.mjs +162 -0
  96. package/src/inspect/routes.mjs +154 -0
  97. package/src/inspect/samples.mjs +59 -0
  98. package/src/inspect/url.mjs +2 -0
  99. package/src/inspect.mjs +239 -0
  100. package/src/jobs.ts +223 -0
  101. package/src/link/election.ts +178 -0
  102. package/src/link/lines.ts +27 -0
  103. package/src/link.ts +385 -0
  104. package/src/list-drizzle.ts +136 -0
  105. package/src/list.ts +315 -0
  106. package/src/listen.ts +54 -0
  107. package/src/live/api.ts +18 -0
  108. package/src/live/backoff.ts +6 -0
  109. package/src/live/frames.ts +42 -0
  110. package/src/live/read.ts +77 -0
  111. package/src/live/sub-types.ts +24 -0
  112. package/src/live/transport.ts +86 -0
  113. package/src/live/types.ts +52 -0
  114. package/src/live-fake.ts +57 -0
  115. package/src/live-target.mjs +231 -0
  116. package/src/live.ts +390 -0
  117. package/src/media.ts +50 -0
  118. package/src/migrations.mjs +27 -0
  119. package/src/mux-session.ts +6 -0
  120. package/src/mux.ts +232 -0
  121. package/src/og/DefaultCard.tsx +91 -0
  122. package/src/og/fonts.mjs +72 -0
  123. package/src/og/images.mjs +124 -0
  124. package/src/og/index.mjs +222 -0
  125. package/src/og/native.mjs +36 -0
  126. package/src/range.mjs +31 -0
  127. package/src/router.ts +199 -0
  128. package/src/routes.mjs +193 -0
  129. package/src/run/admission.ts +67 -0
  130. package/src/run/boot.tsx +115 -0
  131. package/src/run/cache-presentation.ts +67 -0
  132. package/src/run/cache.ts +254 -0
  133. package/src/run/helpers.ts +197 -0
  134. package/src/run/listeners.ts +166 -0
  135. package/src/run/navigation.ts +385 -0
  136. package/src/run/page-skeleton.tsx +35 -0
  137. package/src/run/prefetch.ts +129 -0
  138. package/src/run/retain-view.ts +18 -0
  139. package/src/run/shape-preparation.ts +97 -0
  140. package/src/run/shape-styles.ts +72 -0
  141. package/src/run/source-context/pin.ts +124 -0
  142. package/src/run/source-context.ts +311 -0
  143. package/src/run.tsx +74 -0
  144. package/src/rust/common.ts +163 -0
  145. package/src/rust/native.ts +282 -0
  146. package/src/rust.ts +207 -0
  147. package/src/server/background.ts +34 -0
  148. package/src/server/card.tsx +65 -0
  149. package/src/server/compression.ts +115 -0
  150. package/src/server/core.ts +70 -0
  151. package/src/server/cron.ts +49 -0
  152. package/src/server/dispatch.ts +73 -0
  153. package/src/server/form.ts +92 -0
  154. package/src/server/functions.ts +133 -0
  155. package/src/server/handler.ts +57 -0
  156. package/src/server/head.ts +96 -0
  157. package/src/server/health.ts +36 -0
  158. package/src/server/jobs.ts +112 -0
  159. package/src/server/lifecycle.ts +48 -0
  160. package/src/server/log.ts +42 -0
  161. package/src/server/media.ts +61 -0
  162. package/src/server/navigation-cache.ts +75 -0
  163. package/src/server/navigation.tsx +86 -0
  164. package/src/server/page-cold.ts +96 -0
  165. package/src/server/page-stream.tsx +181 -0
  166. package/src/server/page-types.ts +51 -0
  167. package/src/server/page-warm.ts +131 -0
  168. package/src/server/page.ts +69 -0
  169. package/src/server/payload.ts +89 -0
  170. package/src/server/prerender.ts +64 -0
  171. package/src/server/preview.tsx +67 -0
  172. package/src/server/render.ts +197 -0
  173. package/src/server/request.ts +192 -0
  174. package/src/server/resolver.ts +228 -0
  175. package/src/server/routes.ts +150 -0
  176. package/src/server/security.ts +50 -0
  177. package/src/server/services.ts +69 -0
  178. package/src/server/shutdown.ts +23 -0
  179. package/src/server/text.ts +29 -0
  180. package/src/server/types.ts +170 -0
  181. package/src/server.tsx +138 -0
  182. package/src/shape.d.mts +3 -0
  183. package/src/shape.mjs +97 -0
  184. package/src/share/display.ts +50 -0
  185. package/src/share/network.ts +119 -0
  186. package/src/share/receive.ts +70 -0
  187. package/src/share/state.ts +30 -0
  188. package/src/share/tab-queue.ts +28 -0
  189. package/src/share-server.ts +293 -0
  190. package/src/share.ts +397 -0
  191. package/src/sheets.mjs +87 -0
  192. package/src/sidecar/build.mjs +170 -0
  193. package/src/sidecar/cargo.mjs +164 -0
  194. package/src/sidecar/common.mjs +17 -0
  195. package/src/sidecar/dev-build.mjs +91 -0
  196. package/src/sidecar/dev-watch.mjs +88 -0
  197. package/src/sidecar/manifest.mjs +19 -0
  198. package/src/sidecar/process.mjs +58 -0
  199. package/src/sidecar/refresh.mjs +19 -0
  200. package/src/sidecar.mjs +162 -0
  201. package/src/socket/check.ts +50 -0
  202. package/src/socket/deliver.ts +16 -0
  203. package/src/socket/publish.ts +56 -0
  204. package/src/socket/rooms.ts +41 -0
  205. package/src/socket/route.ts +79 -0
  206. package/src/socket/shared.ts +26 -0
  207. package/src/socket/types.ts +48 -0
  208. package/src/socket.ts +400 -0
  209. package/src/stream.ts +22 -0
  210. package/src/verify/app.ts +39 -0
  211. package/src/verify/cleanup.ts +27 -0
  212. package/src/verify/database.ts +11 -0
  213. package/src/verify/live.ts +63 -0
  214. package/src/verify/notify.ts +35 -0
  215. package/src/verify/skip.ts +19 -0
  216. package/src/verify/support.ts +149 -0
  217. package/src/verify/timing.ts +15 -0
  218. package/src/verify/trigger.ts +14 -0
  219. package/src/verify.ts +152 -0
  220. package/src/view.tsx +132 -0
  221. package/src/vite/asset-bytes.mjs +95 -0
  222. package/src/vite/assets.mjs +117 -0
  223. package/src/vite/config-hooks.mjs +166 -0
  224. package/src/vite/dev-hooks.mjs +103 -0
  225. package/src/vite/dev.mjs +102 -0
  226. package/src/vite/entries.mjs +149 -0
  227. package/src/vite/module-hooks.mjs +290 -0
  228. package/src/vite/route-modules.mjs +289 -0
  229. package/src/vite/server-functions.mjs +222 -0
  230. package/src/vite/shape-entry-css.mjs +187 -0
  231. package/src/vite/shape-override.mjs +226 -0
  232. package/src/vite/shared.mjs +55 -0
  233. package/src/vite/sheets.mjs +92 -0
  234. package/src/vite/state.mjs +89 -0
  235. package/src/vite/syntax.mjs +139 -0
  236. package/src/vite.mjs +202 -0
@@ -0,0 +1,166 @@
1
+ import { drain, wake } from '@kerf-js/core/dom'
2
+ import type { Held } from '../boot'
3
+ import { client, peers } from '../router'
4
+ import { admitLink, routeClicks } from './admission'
5
+ import type { Navigation } from './navigation'
6
+
7
+ type Hot = { on(event: string, on: (data: { queries: boolean }) => unknown): void }
8
+
9
+ export function listeners(nav: Navigation, hot: () => Hot | undefined) {
10
+ addEventListener('online', nav.owing)
11
+ addEventListener('focus', nav.owing)
12
+ addEventListener('pagehide', nav.dispose)
13
+ // A page restored from the back/forward cache was disposed on the way out: let it prefetch again.
14
+ addEventListener('pageshow', (e) => e.persisted && nav.prefetch.resume())
15
+ // A cross-document transition's pagereveal and pageswap are settled from the head (page-stream.tsx `REVEAL`): the page
16
+ // reveals before this runs. One still going when it does is settled here too.
17
+ settle((document as Document & { activeViewTransition?: ViewTransition }).activeViewTransition)
18
+ if (hot()) hot()!.on('kerf:refresh', (changed) => void nav.reloaded(changed))
19
+ client.invalidated = nav.invalidated
20
+ client.sessionChanged = nav.dispose
21
+ clicks(nav)
22
+ preload(nav)
23
+ history(nav)
24
+ peers?.addEventListener('message', (m) => {
25
+ if (!m.data) {
26
+ nav.dispose()
27
+ location.reload()
28
+ }
29
+ })
30
+ const q = (window as Window & { __wq?: Held }).__wq
31
+ if (q) {
32
+ q.w = wake
33
+ q.s = 1
34
+ if (q.l) wake(q.l)
35
+ drain()
36
+ }
37
+ }
38
+
39
+ // A fade can intercept a click; replay it on the element underneath once the picture is gone.
40
+ function clicks(nav: Navigation) {
41
+ const network = (
42
+ navigator as Navigator & { connection?: { saveData?: boolean; effectiveType?: string } }
43
+ ).connection
44
+ routeClicks(
45
+ document,
46
+ nav.routes,
47
+ (href) => nav.go(href),
48
+ (url) => {
49
+ if (!document.hidden) nav.prefetch.start(url, 'press', network, navigator.onLine)
50
+ },
51
+ )
52
+ let ended: Promise<unknown> | undefined
53
+ document.addEventListener(
54
+ 'pointerdown',
55
+ () => {
56
+ ended = nav.fading?.finished.catch(() => {})
57
+ nav.fading?.skipTransition()
58
+ nav.fading = undefined
59
+ },
60
+ true,
61
+ )
62
+ document.addEventListener(
63
+ 'click',
64
+ (e) => {
65
+ const gone = ended
66
+ ended = undefined
67
+ if (!gone || e.target !== document.documentElement) return
68
+ void gone.then(() => {
69
+ const under = document.elementFromPoint(e.clientX, e.clientY)
70
+ if (under !== document.documentElement) under?.dispatchEvent(new MouseEvent('click', e))
71
+ })
72
+ },
73
+ true,
74
+ )
75
+ }
76
+
77
+ // Only known-cost intent can start a bounded data request; no page module runs here.
78
+ export function preload(nav: Navigation) {
79
+ let pending: ReturnType<typeof setTimeout> | undefined
80
+ const network = (
81
+ navigator as Navigator & {
82
+ connection?: {
83
+ saveData?: boolean
84
+ effectiveType?: string
85
+ addEventListener?: (type: string, fn: () => void) => void
86
+ removeEventListener?: (type: string, fn: () => void) => void
87
+ }
88
+ }
89
+ ).connection
90
+ const cancel = () => {
91
+ clearTimeout(pending)
92
+ nav.prefetch.cancel()
93
+ }
94
+ const intent = (e: Event) => {
95
+ clearTimeout(pending)
96
+ const target = e.target as Element | null
97
+ const admitted = admitLink(e, nav.routes)
98
+ if (
99
+ !admitted ||
100
+ admitted.noop ||
101
+ !target ||
102
+ target.closest('[data-preload="off"]') ||
103
+ target.closest('input,textarea,select,[contenteditable]:not([contenteditable="false"])')
104
+ )
105
+ return
106
+ pending = setTimeout(
107
+ () => {
108
+ if (document.hidden) return
109
+ const version = nav.cache.version
110
+ nav.cache.setBuild(nav.routes.build)
111
+ if (version !== nav.cache.version) nav.prefetch.cancel(true)
112
+ nav.prefetch.start(
113
+ admitted.url,
114
+ e.type === 'focusin' ? 'focus' : 'hover',
115
+ network,
116
+ navigator.onLine,
117
+ )
118
+ },
119
+ e.type === 'focusin' ? 0 : 80,
120
+ )
121
+ }
122
+ document.addEventListener('pointerover', intent)
123
+ document.addEventListener('focusin', intent)
124
+ document.addEventListener('pointerout', cancel)
125
+ document.addEventListener('focusout', cancel)
126
+ const visibility = () => {
127
+ if (document.hidden) cancel()
128
+ }
129
+ document.addEventListener('visibilitychange', visibility)
130
+ addEventListener('offline', cancel)
131
+ addEventListener('pagehide', cancel)
132
+ network?.addEventListener?.('change', cancel)
133
+ return () => {
134
+ cancel()
135
+ document.removeEventListener('pointerover', intent)
136
+ document.removeEventListener('focusin', intent)
137
+ document.removeEventListener('pointerout', cancel)
138
+ document.removeEventListener('focusout', cancel)
139
+ document.removeEventListener('visibilitychange', visibility)
140
+ removeEventListener('offline', cancel)
141
+ removeEventListener('pagehide', cancel)
142
+ network?.removeEventListener?.('change', cancel)
143
+ }
144
+ }
145
+
146
+ // An in-page anchor fires popstate too; only a changed path needs a navigation.
147
+ function history(nav: Navigation) {
148
+ nav.at = location.pathname + location.search
149
+ addEventListener('popstate', () => {
150
+ if (location.pathname + location.search !== nav.at)
151
+ return void nav.go(location.href, { pop: true })
152
+ if (nav.current) {
153
+ nav.current.abort()
154
+ nav.settle()
155
+ }
156
+ })
157
+ }
158
+
159
+ /** Mark a transition's outcome as seen: a skipped one rejects all three of its promises. */
160
+ function settle(t: ViewTransition | null | undefined) {
161
+ if (!t) return
162
+ const seen = () => {}
163
+ t.ready.catch(seen)
164
+ t.finished.catch(seen)
165
+ t.updateCallbackDone.catch(seen)
166
+ }
@@ -0,0 +1,385 @@
1
+ import { flush } from '@kerf-js/core'
2
+ import { setHead } from '@kerf-js/core/head'
3
+ import {
4
+ applyMatches,
5
+ client,
6
+ type Level,
7
+ type NavigateOptions,
8
+ type RoutePayload,
9
+ } from '../router'
10
+ import type { ClientRoutes } from '../run'
11
+ import { streamed, type mount } from './boot'
12
+ import { matchingRouteAssets } from '../asset-receipt.mjs'
13
+ import { ask, changedRouteParams, crossfade, current_, guess, revive } from './helpers'
14
+ import { presentShape, type PreparedRouteShape } from './shape-preparation'
15
+ import { retainSourceContext, type SourceContext } from './source-context'
16
+ import { RouteCache, type Read } from './cache'
17
+ import { Prefetch } from './prefetch'
18
+ import { CachePresentation, retryDelay } from './cache-presentation'
19
+
20
+ const RETRY = 1000,
21
+ RETRIES = 30_000
22
+
23
+ type Options = NavigateOptions & { pop?: boolean; hops?: number }
24
+ type Page = ReturnType<typeof mount>
25
+
26
+ export class Navigation {
27
+ route: Page['route']
28
+ chunk: Page['chunk']
29
+ island: Page['island']
30
+ setBag: Page['setBag']
31
+ ready: Page['ready']
32
+ tags: Page['initialTags']
33
+ current: AbortController | undefined
34
+ refreshing: { ctrl: AbortController; tags: string[] } | undefined
35
+ /** Tags invalidated while a navigation was out: its route may read them, and that's only known when it lands. */
36
+ owed: string[] = []
37
+ again: ReturnType<typeof setTimeout> | undefined
38
+ tries = 0
39
+ prefetch: Prefetch
40
+ cache: RouteCache
41
+ presentation = new CachePresentation()
42
+ // A fade pictures the whole page twice, and the picture holds up every frame. Where that's slow, the page just changes.
43
+ fades = !(navigator.hardwareConcurrency <= 4)
44
+ fading: ViewTransition | undefined
45
+ painted = false
46
+ at!: string
47
+ shape: { outlet: Level; leases: PreparedRouteShape[] } | undefined
48
+ source: SourceContext | undefined
49
+ committedURL = location.href
50
+ committedBase = document.baseURI
51
+
52
+ constructor(
53
+ readonly routes: ClientRoutes,
54
+ page: Page,
55
+ ) {
56
+ this.cache = new RouteCache(routes.build)
57
+ this.prefetch = new Prefetch(this.cache, this.now, () => !!client.seed)
58
+ this.ready = page.ready
59
+ this.route = page.route
60
+ this.chunk = page.chunk
61
+ this.island = page.island
62
+ this.setBag = page.setBag
63
+ this.tags = page.initialTags
64
+ // Two frames in, the page has painted; a transition before then has no old picture to hold.
65
+ requestAnimationFrame(() => requestAnimationFrame(() => (this.painted = true)))
66
+ }
67
+
68
+ now = () => client.now?.() ?? 0
69
+ needs = (next: RoutePayload) =>
70
+ Promise.all([...next.m.map((m) => this.chunk(m.id)), ...(next.i ?? []).map(this.island)])
71
+ reads = (t: string[]) => this.tags.some((l) => l.some((x) => t.includes(x)))
72
+
73
+ /** The page moved on: drop old chunks and build held Boundaries from this data. */
74
+ release() {
75
+ const h = streamed.$wh || {}
76
+ for (const k of Object.keys(h)) {
77
+ const build = h[k]!
78
+ h[k] = () => {}
79
+ build()
80
+ }
81
+ }
82
+
83
+ apply(next: RoutePayload, url: URL, path: string, since: number, scroll: boolean) {
84
+ const owner = this.current
85
+ const current = () => this.current === owner && !owner?.signal.aborted
86
+ return () => {
87
+ if (!current()) return
88
+ this.at = path
89
+ if (next.c) client.seed ? client.seed(next.c, since) : (client.seeds ??= []).push(...next.c)
90
+ if (!current()) return
91
+ this.tags = next.m.map((m) => m.t ?? [])
92
+ this.route.assetBytes = matchingRouteAssets(next.a, this.routes.build)
93
+ this.setBag(next.b)
94
+ if (!current()) return
95
+ const source = this.source
96
+ this.source = undefined
97
+ source?.stop()
98
+ const old = this.detachShape()
99
+ const previousURL = this.committedURL
100
+ const previousBase = this.committedBase
101
+ this.committedURL = url.href
102
+ this.committedBase = document.baseURI
103
+ try {
104
+ applyMatches(this.route, next.m.map(revive), path, next.s)
105
+ if (!current()) return
106
+ if (next.m.some((m) => m.s)) current_(url.pathname)
107
+ if (!current()) return
108
+ if (next.h) setHead(next.h, url.pathname)
109
+ if (!current()) return
110
+ this.release()
111
+ if (!current()) return
112
+ flush()
113
+ } catch (error) {
114
+ if (current()) {
115
+ this.committedURL = previousURL
116
+ this.committedBase = previousBase
117
+ }
118
+ throw error
119
+ } finally {
120
+ old?.forEach((lease) => lease.release())
121
+ source?.release()
122
+ }
123
+ if (!current()) return
124
+ if (scroll) {
125
+ const target = url.hash && document.getElementById(decodeURIComponent(url.hash.slice(1)))
126
+ // a new page arrives where it starts, at once: veneer makes the root scroll smooth for in-page anchors, and a
127
+ // route change that glided down from the old page's offset read as the old page scrolling away
128
+ if (target) target.scrollIntoView({ behavior: 'instant' })
129
+ else window.scrollTo({ top: 0, left: 0, behavior: 'instant' })
130
+ }
131
+ }
132
+ }
133
+
134
+ /** Find the first layout containing a changed parameter, or the leaf for a changed query. */
135
+ changedParams(ids: string[], url: URL) {
136
+ return changedRouteParams(this.routes, this.committedURL, ids, url)
137
+ }
138
+
139
+ /** Prepare all changed levels before replacing the current outlet. */
140
+ present(ids: string[], url: URL, force = false) {
141
+ return presentShape(this, ids, url, force)
142
+ }
143
+
144
+ detachShape() {
145
+ const shape = this.shape
146
+ if (!shape) return
147
+ this.shape = undefined
148
+ shape.outlet.shape.value = undefined
149
+ return shape.leases
150
+ }
151
+
152
+ clearShape() {
153
+ const leases = this.detachShape()
154
+ if (leases) {
155
+ try {
156
+ flush()
157
+ } finally {
158
+ leases.forEach((lease) => lease.release())
159
+ }
160
+ }
161
+ }
162
+
163
+ /** No navigation is out any more: what it left owed is for the page now showing. */
164
+ settle() {
165
+ // Every way a navigation ends comes through here, so its cached-presentation timer stops here too.
166
+ if (this.current) this.presentation.stop(this.current)
167
+ this.clearShape()
168
+ if (this.source && this.current?.signal.aborted) {
169
+ history.replaceState(history.state, '', this.committedURL)
170
+ const url = new URL(this.committedURL)
171
+ this.at = url.pathname + url.search
172
+ this.route.path.value = this.at
173
+ current_(url.pathname)
174
+ flush()
175
+ }
176
+ this.source?.release()
177
+ this.source = undefined
178
+ this.current = undefined
179
+ const t = this.owed
180
+ this.owed = []
181
+ if (this.reads(t)) void this.refresh(t)
182
+ }
183
+
184
+ dispose = () => {
185
+ this.cache.clear()
186
+ this.prefetch.stop()
187
+ if (this.current) this.presentation.stop(this.current)
188
+ this.current?.abort()
189
+ this.current = undefined
190
+ this.refreshing?.ctrl.abort()
191
+ this.refreshing = undefined
192
+ clearTimeout(this.again)
193
+ this.owed = []
194
+ this.clearShape()
195
+ this.source?.release()
196
+ this.source = undefined
197
+ }
198
+
199
+ go = async (href: string, opts: Options = {}) => {
200
+ const url = new URL(href, location.href)
201
+ if (url.origin !== location.origin) return location.assign(url.href)
202
+ if (!opts.pop && url.href === location.href) return
203
+ const buildVersion = this.cache.version
204
+ this.cache.setBuild(this.routes.build)
205
+ if (buildVersion !== this.cache.version) this.prefetch.cancel(true)
206
+ if (this.current) this.presentation.stop(this.current)
207
+ this.current?.abort()
208
+ if (this.refreshing) {
209
+ this.refreshing.ctrl.abort()
210
+ this.owed.push(...this.refreshing.tags)
211
+ this.refreshing = undefined
212
+ }
213
+ const ctrl = (this.current = new AbortController())
214
+ const path = url.pathname + url.search
215
+ try {
216
+ this.source ??=
217
+ retainSourceContext(document, this.committedURL, this.committedBase) ?? undefined
218
+ if (!this.source) return opts.pop ? location.replace(url.href) : location.assign(url.href)
219
+ if (!opts.pop && url.href !== location.href)
220
+ history[opts.replace ? 'replaceState' : 'pushState'](null, '', url.href)
221
+ this.at = path
222
+ this.route.path.value = path
223
+ current_(url.pathname)
224
+ flush()
225
+ const speculative = this.prefetch.claim(url, ctrl.signal)
226
+ this.prefetch.cancel()
227
+ const cached = opts.pop ? this.cache.peek(url, !!client.seed) : undefined
228
+ const shown = this.presentation.show(this, ctrl, url, cached)
229
+ if (ctrl.signal.aborted || this.current !== ctrl) return
230
+ if (!shown) this.present(guess(this.routes, url.pathname), url)
231
+ await this.travel(url, ctrl, opts, shown, speculative)
232
+ } finally {
233
+ this.presentation.stop(ctrl)
234
+ if (this.current === ctrl) this.settle()
235
+ }
236
+ }
237
+
238
+ async travel(
239
+ url: URL,
240
+ ctrl: AbortController,
241
+ opts: Options,
242
+ shown = false,
243
+ speculative?: Promise<Read | undefined>,
244
+ ) {
245
+ const path = url.pathname + url.search
246
+ for (const id of guess(this.routes, url.pathname)) void this.chunk(id).catch(() => {})
247
+ let next: RoutePayload
248
+ let since: number
249
+ let version: number
250
+ let retries = 0
251
+ for (;;) {
252
+ try {
253
+ const shared = speculative ? await speculative : undefined
254
+ speculative = undefined
255
+ const read =
256
+ shared && (!shared.next.c?.length || client.seed)
257
+ ? shared
258
+ : await this.cache.load({
259
+ url,
260
+ signal: ctrl.signal,
261
+ fetchRoute: ask,
262
+ now: this.now,
263
+ canSeed: () => !!client.seed,
264
+ onForget: shown ? () => this.presentation.check(this) : undefined,
265
+ })
266
+ this.cache.setBuild(this.routes.build)
267
+ if (ctrl.signal.aborted) return
268
+ if (read.version !== this.cache.version) continue
269
+ next = read.next
270
+ if (next.s === 401 || next.s === 403) this.prefetch.cancel(true)
271
+ since = read.since
272
+ version = read.version
273
+ if (next.r) {
274
+ const hops = (opts.hops ?? 0) + 1
275
+ return hops > 5 ? location.replace(next.r) : this.go(next.r, { replace: true, hops })
276
+ }
277
+ if (shown && (next.s === 401 || next.s === 403)) {
278
+ if (this.ready?.(next)) {
279
+ this.apply(next, url, path, since, false)()
280
+ return
281
+ }
282
+ this.presentation.check(this)
283
+ if (ctrl.signal.aborted) return
284
+ }
285
+ await this.needs(next)
286
+ this.cache.setBuild(this.routes.build)
287
+ if (ctrl.signal.aborted) return
288
+ if (read.version !== this.cache.version) continue
289
+ if (shown && read.notModified) return
290
+ break
291
+ } catch {
292
+ if (ctrl.signal.aborted) return
293
+ if (!shown) return location.replace(url.href)
294
+ if (!this.cache.peek(url, !!client.seed)) {
295
+ this.current = undefined
296
+ ctrl.abort()
297
+ return location.replace(url.href)
298
+ }
299
+ await retryDelay(ctrl.signal, Math.min(RETRIES, RETRY * 2 ** retries++))
300
+ continue
301
+ }
302
+ }
303
+ let swapped = false
304
+ const swap = () => {
305
+ swapped = true
306
+ this.cache.setBuild(this.routes.build)
307
+ if (ctrl.signal.aborted || this.current !== ctrl || this.cache.version !== version) return
308
+ this.apply(next, url, path, since, opts.scroll !== false && !opts.pop)()
309
+ }
310
+ await crossfade(this, swap, () => swapped)
311
+ if (!ctrl.signal.aborted && this.current === ctrl && this.cache.version !== version)
312
+ await this.travel(url, ctrl, opts, shown)
313
+ }
314
+
315
+ /** Fetch the page's data again in place, retrying a failed refresh. */
316
+ async refresh(t: string[]) {
317
+ t = [...new Set([...t, ...(this.refreshing?.tags ?? [])])]
318
+ this.refreshing?.ctrl.abort()
319
+ const self = (this.refreshing = { ctrl: new AbortController(), tags: t })
320
+ const url = new URL(location.href),
321
+ since = this.now()
322
+ try {
323
+ const next: RoutePayload = await ask(url, self.ctrl.signal).then((r) => r.json())
324
+ if (self.ctrl.signal.aborted) return
325
+ if (next.s === 401 || next.s === 403) {
326
+ this.cache.clear()
327
+ this.prefetch.cancel(true)
328
+ }
329
+ if (next.r && !self.ctrl.signal.aborted) {
330
+ this.refreshing = undefined
331
+ return void this.go(next.r, { replace: true })
332
+ }
333
+ await this.needs(next)
334
+ if (self.ctrl.signal.aborted) return
335
+ this.apply(next, url, url.pathname + url.search, since, false)()
336
+ this.tries = 0
337
+ } catch {
338
+ if (this.refreshing === self) {
339
+ this.owed.push(...t)
340
+ clearTimeout(this.again)
341
+ this.again = setTimeout(this.owing, Math.min(RETRIES, RETRY * 2 ** this.tries++))
342
+ }
343
+ }
344
+ if (this.refreshing === self) this.refreshing = undefined
345
+ }
346
+
347
+ owing = () => {
348
+ if (!this.current && !this.refreshing) this.settle()
349
+ }
350
+
351
+ reloaded = async ({ queries }: { queries: boolean }) => {
352
+ const owner = this.current
353
+ this.cache.clear()
354
+ this.prefetch.cancel(true)
355
+ const url = new URL(location.href),
356
+ since = this.now()
357
+ let version = this.cache.version
358
+ const res = await ask(url)
359
+ if (version !== this.cache.version || !res.ok) return
360
+ const next: RoutePayload = await res.clone().json()
361
+ this.cache.setBuild(this.routes.build)
362
+ if (version !== this.cache.version || next.s >= 500) return
363
+ if (next.s === 401 || next.s === 403) {
364
+ this.cache.clear()
365
+ version = this.cache.version
366
+ }
367
+ if (queries) client.refreshed?.()
368
+ this.cache.setBuild(this.routes.build)
369
+ if (version !== this.cache.version || this.current !== owner) return
370
+ if (next.r) return await this.go(next.r, { replace: true })
371
+ if (this.current) return await this.go(url.href, { pop: true })
372
+ await this.needs(next)
373
+ this.cache.setBuild(this.routes.build)
374
+ if (version !== this.cache.version || this.current !== owner) return
375
+ this.apply(next, url, url.pathname + url.search, since, false)()
376
+ }
377
+
378
+ invalidated = (t: string[]) => {
379
+ this.cache.invalidate(t)
380
+ this.presentation.check(this)
381
+ this.prefetch.cancel(true)
382
+ if (this.current) this.owed.push(...t)
383
+ else if (this.reads(t)) void this.refresh(t)
384
+ }
385
+ }
@@ -0,0 +1,35 @@
1
+ // What navigation shows for a level whose generated shape has no structure of its own (`$kerfShapeThin`): a route
2
+ // whose markup is one component the compiler can't see into. A page gets a page's outline, a layout stands aside for
3
+ // its page's. A route's own `export const skeleton` is what fits exactly; this keeps the outlet from going blank.
4
+
5
+ /** One placeholder bar. The colour is the fallback without Veneer; Veneer's skeleton styles paint and shimmer it. */
6
+ const bar = (height: string, width: string, gap = '0.75em') =>
7
+ `display:block;block-size:${height};inline-size:${width};margin-block-end:${gap};border-radius:0.375em;background:color-mix(in srgb,currentColor 9%,transparent)`
8
+
9
+ /** A heading, a lede and a block of text, then a panel: the outline most pages share. */
10
+ export function PageSkeleton() {
11
+ return (
12
+ <div
13
+ data-scope="skeleton-scope"
14
+ aria-busy="true"
15
+ role="status"
16
+ aria-label="Loading"
17
+ inert
18
+ data-kerf-page-skeleton=""
19
+ style="display:block;min-inline-size:0;padding-block:clamp(1.5rem,4vw,2.5rem)"
20
+ >
21
+ <span data-skeleton style={bar('2.25em', 'min(22ch,60%)', '1.25em')}></span>
22
+ <span data-skeleton style={bar('1em', 'min(60ch,92%)')}></span>
23
+ <span data-skeleton style={bar('1em', 'min(48ch,74%)', '2em')}></span>
24
+ <span data-skeleton style={bar('0.9em', '100%')}></span>
25
+ <span data-skeleton style={bar('0.9em', '96%')}></span>
26
+ <span data-skeleton style={bar('0.9em', '88%', '2em')}></span>
27
+ <span data-skeleton style={bar('14em', '100%', '0')}></span>
28
+ </div>
29
+ )
30
+ }
31
+
32
+ /** A layout with nothing to show of its own: its page's skeleton, in place. */
33
+ export function PassThroughSkeleton(props: { children?: unknown }) {
34
+ return <>{props.children}</>
35
+ }