@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,12 @@
|
|
|
1
|
+
/** A message with its cursor: `yield tracked(row.id, row)`, and a resumed `run` gets it as `lastEventId`. */
|
|
2
|
+
export class Tracked<M> {
|
|
3
|
+
constructor(
|
|
4
|
+
public id: string,
|
|
5
|
+
public value: M,
|
|
6
|
+
) {}
|
|
7
|
+
}
|
|
8
|
+
/** A message with an id, so a stream can resume after it. */
|
|
9
|
+
export const tracked = <M>(id: string, value: M) => new Tracked(id, value)
|
|
10
|
+
|
|
11
|
+
/** Thrown out of `run` when its cursor can't resume: the stream restarts without one. */
|
|
12
|
+
export const RESET = new Error('kerf.reset')
|
package/src/hub/types.ts
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import type { Bus } from '../bus'
|
|
2
|
+
import type { Source } from '../changes'
|
|
3
|
+
import type { Fn, LiveMode, StartError } from '../data'
|
|
4
|
+
import type { Scope, ScopeOptions, Store } from '../exec'
|
|
5
|
+
import type { ShareRule } from '../share-server'
|
|
6
|
+
|
|
7
|
+
/** What one connection may cost; every number has a default in `LIMITS`. */
|
|
8
|
+
export interface Limits {
|
|
9
|
+
subscriptions: number
|
|
10
|
+
/** Per signed-in user. */
|
|
11
|
+
connections: number
|
|
12
|
+
/** Bytes of one request (client→server). */
|
|
13
|
+
body: number
|
|
14
|
+
/** Bytes of one message (server→client). */
|
|
15
|
+
message: number
|
|
16
|
+
/** Bytes one connection may hold before it's closed. */
|
|
17
|
+
queue: number
|
|
18
|
+
/** Events a ring keeps. */
|
|
19
|
+
ring: number
|
|
20
|
+
/** Ms a ring keeps them. */
|
|
21
|
+
window: number
|
|
22
|
+
/** Ms between `: ping`s. */
|
|
23
|
+
heartbeat: number
|
|
24
|
+
/** Ms between session re-checks of `auth` subscriptions and channel joins. */
|
|
25
|
+
session: number
|
|
26
|
+
/** Channel messages a client may send per second, per socket. */
|
|
27
|
+
perSecond: number
|
|
28
|
+
/** The burst `perSecond` allows. */
|
|
29
|
+
burst: number
|
|
30
|
+
/** Keys one socket may `share`. They cost no joins: a 200-row list is one. */
|
|
31
|
+
shareKeys: number
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** `realtime()`: the options of an app's `src/realtime.server.ts`. */
|
|
35
|
+
export interface RealtimeOptions {
|
|
36
|
+
/** Default: `memoryBus()`, one instance only (R002). */
|
|
37
|
+
bus?: Bus
|
|
38
|
+
/** Origins allowed besides the app's own, exact: scheme, host and port. */
|
|
39
|
+
origins?: string[]
|
|
40
|
+
limits?: Partial<Limits>
|
|
41
|
+
/** Seconds the platform lets a response live: streams end 10 s earlier, and the client resumes. */
|
|
42
|
+
maxDuration?: number
|
|
43
|
+
/** Writes from outside the app: `changes: dbChanges(db, [todos])`, or a source of your own. */
|
|
44
|
+
changes?: Source
|
|
45
|
+
/**
|
|
46
|
+
* Who may share or see a key of `interaction({ share })` with other clients. Left out, nobody: tabs still work. One
|
|
47
|
+
* key at a time, `({ ctx, key }) => boolean`, or `{ keys: ({ ctx, keys }) => allowed }` to answer a whole join at once.
|
|
48
|
+
*/
|
|
49
|
+
share?: ShareRule | undefined
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** What the app gets from `configure`, beside `src/realtime.server.ts`. */
|
|
53
|
+
export interface HubOptions extends RealtimeOptions {
|
|
54
|
+
build?: string | undefined
|
|
55
|
+
dev?: boolean | undefined
|
|
56
|
+
/** Queries marked `live: { required: true }`: without a real feed the server refuses to run (R015). */
|
|
57
|
+
required?: string[] | undefined
|
|
58
|
+
store?: Store | undefined
|
|
59
|
+
onDiagnostic?: ScopeOptions['onDiagnostic'] | undefined
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** What `.live` reports, and a health route reads. */
|
|
63
|
+
export interface LiveState {
|
|
64
|
+
mode: LiveMode
|
|
65
|
+
via: string
|
|
66
|
+
why?: string | undefined
|
|
67
|
+
fix?: string | undefined
|
|
68
|
+
/** The feed works in general and is out right now. */
|
|
69
|
+
down?: string | undefined
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Where a connection's frames go. `data` is JSON. */
|
|
73
|
+
export interface Sink {
|
|
74
|
+
write(event: string, data: string): void
|
|
75
|
+
ping?(): void
|
|
76
|
+
/** Bytes written and not yet taken by the client. */
|
|
77
|
+
backlog(): number
|
|
78
|
+
close(): void
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** What the client sends (research 30 §4): `[n, kind, id, input, cursor, buffer]`. */
|
|
82
|
+
export type SubEntry = [
|
|
83
|
+
n: number,
|
|
84
|
+
kind: 'q' | 's',
|
|
85
|
+
id: string,
|
|
86
|
+
input?: unknown,
|
|
87
|
+
cursor?: string,
|
|
88
|
+
buffer?: number,
|
|
89
|
+
]
|
|
90
|
+
/** The body of `/_kerf/rt`: the cursor, subscriptions to add, and subscriptions to drop. */
|
|
91
|
+
export interface Body {
|
|
92
|
+
k?: string
|
|
93
|
+
c?: string | undefined
|
|
94
|
+
s?: SubEntry[]
|
|
95
|
+
d?: number[]
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** One message in a channel's ring. */
|
|
99
|
+
export interface Ev {
|
|
100
|
+
seq: number
|
|
101
|
+
at: number
|
|
102
|
+
raw: string
|
|
103
|
+
/** Parsed once, for every socket of a room. */ p?: unknown
|
|
104
|
+
}
|
|
105
|
+
/** One hub channel: its replay ring and who's listening on this instance. */
|
|
106
|
+
export interface Channel {
|
|
107
|
+
name: string
|
|
108
|
+
ring: Ev[]
|
|
109
|
+
floor: number
|
|
110
|
+
listeners: Set<(ev: Ev | null) => void>
|
|
111
|
+
/** Take a message as if the bus had brought it: a change feed reaches this instance's connections only. */
|
|
112
|
+
got: (raw: string) => void
|
|
113
|
+
ready: Promise<void>
|
|
114
|
+
stop?: () => void
|
|
115
|
+
idle?: ReturnType<typeof setTimeout> | undefined
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export interface Sub {
|
|
119
|
+
n: number
|
|
120
|
+
kind: 'q' | 's'
|
|
121
|
+
fn: Fn
|
|
122
|
+
input: unknown
|
|
123
|
+
tags: string[]
|
|
124
|
+
buffer: number
|
|
125
|
+
q: { raw: string; size: number }[]
|
|
126
|
+
ac: AbortController
|
|
127
|
+
request: Request
|
|
128
|
+
options: ScopeOptions
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** One client's realtime connection. */
|
|
132
|
+
export interface Conn {
|
|
133
|
+
k: string
|
|
134
|
+
sink: Sink
|
|
135
|
+
user?: string | undefined
|
|
136
|
+
subs: Map<number, Sub>
|
|
137
|
+
tags: Map<string, number>
|
|
138
|
+
held: Set<Sub>
|
|
139
|
+
pokes?: { t: Set<string>; c: string } | undefined
|
|
140
|
+
bytes: number
|
|
141
|
+
timers: ReturnType<typeof setTimeout>[]
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** The realtime server of this process: channels, connections and the change feed. */
|
|
145
|
+
export interface Hub {
|
|
146
|
+
readonly bus: Bus
|
|
147
|
+
readonly limits: Limits
|
|
148
|
+
/** `realtime({ share })`. */
|
|
149
|
+
readonly share?: ShareRule | undefined
|
|
150
|
+
poke(tags: string[], origin?: string | null): Promise<void>
|
|
151
|
+
cursor(): string | undefined
|
|
152
|
+
/** Listening for pokes, or 250 ms: a page waits this long for a cursor, and goes without one. */
|
|
153
|
+
ready(): Promise<unknown>
|
|
154
|
+
cursorOf(seq: number): string
|
|
155
|
+
channel(name: string): Channel
|
|
156
|
+
replay(ch: Channel, after: string): Ev[] | undefined
|
|
157
|
+
unlisten(ch: Channel, on: (ev: Ev | null) => void): void
|
|
158
|
+
/** `line`: one tab on a shared socket (mux.ts), which `limits.connections` counted already. */
|
|
159
|
+
connect(request: Request, scope: Scope, body: Body, sink: Sink, line?: boolean): Promise<Conn>
|
|
160
|
+
change(c: Conn, request: Request, options: ScopeOptions, body: Body): Promise<void>
|
|
161
|
+
drain(c: Conn): void
|
|
162
|
+
close(c: Conn): void
|
|
163
|
+
find(k: string): Conn | undefined
|
|
164
|
+
connections(): Conn[]
|
|
165
|
+
/** @kerf-js/core/test: a message straight to every reader of a stream. */
|
|
166
|
+
push(id: string, msg: unknown): number
|
|
167
|
+
/** Server-side counts for tests and `kerf browser`. */
|
|
168
|
+
stats(): { connections: number; subscriptions: number; channels: number }
|
|
169
|
+
/** The live mode this server settled on: `.liveMode` on the client, and health routes. */
|
|
170
|
+
live(): LiveState
|
|
171
|
+
required: boolean
|
|
172
|
+
/** Settled once the source has been probed. */
|
|
173
|
+
probed: Promise<unknown>
|
|
174
|
+
/** R015: required live queries, and no feed. Requests get this instead of stale data. */
|
|
175
|
+
refused?: StartError | undefined
|
|
176
|
+
shutdown(): Promise<void>
|
|
177
|
+
/** The position a new listener resumes from. */
|
|
178
|
+
position(): string
|
|
179
|
+
/** Every poked tag, on this instance: socket.ts's `session:<user>` re-check. */
|
|
180
|
+
watch(fn: (tags: string[]) => void): void
|
|
181
|
+
/** Run at shutdown: socket.ts closes its sockets. */
|
|
182
|
+
closing: Set<() => void>
|
|
183
|
+
/** `room.publish` checks its messages (C002). */
|
|
184
|
+
dev?: boolean | undefined
|
|
185
|
+
/** The instance's name in a shared value's stamp. */
|
|
186
|
+
epoch: string
|
|
187
|
+
/** Seconds a connection may live (`RealtimeOptions.maxDuration`). */
|
|
188
|
+
maxDuration?: number | undefined
|
|
189
|
+
build?: string | undefined
|
|
190
|
+
}
|
package/src/hub/view.ts
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import type { Bus } from '../bus'
|
|
2
|
+
import type { createChannels } from './channels'
|
|
3
|
+
import type { createConnections } from './connections'
|
|
4
|
+
import type { createDelivery } from './delivery'
|
|
5
|
+
import type { createFeedState } from './feed'
|
|
6
|
+
import type { createSubscriptions } from './subscriptions'
|
|
7
|
+
import type { Channel, Conn, Hub, HubOptions, Limits } from './types'
|
|
8
|
+
|
|
9
|
+
const unref = <T>(t: T) => ((t as { unref?(): void } | null | undefined)?.unref?.(), t)
|
|
10
|
+
interface View {
|
|
11
|
+
bus: Bus
|
|
12
|
+
limits: Limits
|
|
13
|
+
epoch: string
|
|
14
|
+
options: HubOptions
|
|
15
|
+
channels: Map<string, Channel>
|
|
16
|
+
conns: Map<string, Conn>
|
|
17
|
+
watchers: Set<(tags: string[]) => void>
|
|
18
|
+
closing: Set<() => void>
|
|
19
|
+
cursorOf: (n: number) => string
|
|
20
|
+
channel: ReturnType<typeof createChannels>['channel']
|
|
21
|
+
replay: ReturnType<typeof createChannels>['replay']
|
|
22
|
+
unlisten: ReturnType<typeof createChannels>['unlisten']
|
|
23
|
+
seq: () => number
|
|
24
|
+
pokes: () => Channel
|
|
25
|
+
want: ReturnType<typeof createFeedState>['want']
|
|
26
|
+
rest: ReturnType<typeof createFeedState>['rest']
|
|
27
|
+
feedState: ReturnType<typeof createFeedState>
|
|
28
|
+
feed: ReturnType<typeof createFeedState>['feed']
|
|
29
|
+
probed: Promise<unknown>
|
|
30
|
+
deliver: ReturnType<typeof createDelivery>['deliver']
|
|
31
|
+
drain: ReturnType<typeof createDelivery>['drain']
|
|
32
|
+
connect: ReturnType<typeof createConnections>['connect']
|
|
33
|
+
close: ReturnType<typeof createConnections>['close']
|
|
34
|
+
change: ReturnType<typeof createSubscriptions>['change']
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function createHubView(deps: View): Hub {
|
|
38
|
+
const {
|
|
39
|
+
bus,
|
|
40
|
+
limits,
|
|
41
|
+
epoch,
|
|
42
|
+
options,
|
|
43
|
+
channels,
|
|
44
|
+
conns,
|
|
45
|
+
watchers,
|
|
46
|
+
closing,
|
|
47
|
+
cursorOf,
|
|
48
|
+
channel,
|
|
49
|
+
replay,
|
|
50
|
+
unlisten,
|
|
51
|
+
seq,
|
|
52
|
+
pokes,
|
|
53
|
+
feedState,
|
|
54
|
+
probed,
|
|
55
|
+
drain,
|
|
56
|
+
connect,
|
|
57
|
+
close,
|
|
58
|
+
change,
|
|
59
|
+
} = deps
|
|
60
|
+
const { ready, poke, push, shutdown } = createOperations(deps)
|
|
61
|
+
return {
|
|
62
|
+
bus,
|
|
63
|
+
limits,
|
|
64
|
+
cursorOf,
|
|
65
|
+
channel,
|
|
66
|
+
replay,
|
|
67
|
+
unlisten,
|
|
68
|
+
connect,
|
|
69
|
+
change,
|
|
70
|
+
drain,
|
|
71
|
+
close,
|
|
72
|
+
closing,
|
|
73
|
+
epoch,
|
|
74
|
+
dev: options.dev,
|
|
75
|
+
maxDuration: options.maxDuration,
|
|
76
|
+
build: options.build,
|
|
77
|
+
share: options.share,
|
|
78
|
+
position: () => cursorOf(seq()),
|
|
79
|
+
watch(fn) {
|
|
80
|
+
watchers.add(fn)
|
|
81
|
+
void pokes()
|
|
82
|
+
},
|
|
83
|
+
find: (k) => conns.get(k),
|
|
84
|
+
connections: () => [...conns.values()],
|
|
85
|
+
cursor: () => (pokes().stop ? cursorOf(seq()) : undefined),
|
|
86
|
+
ready,
|
|
87
|
+
poke,
|
|
88
|
+
push,
|
|
89
|
+
stats: () => ({
|
|
90
|
+
connections: conns.size,
|
|
91
|
+
subscriptions: [...conns.values()].reduce((n, c) => n + c.subs.size, 0),
|
|
92
|
+
channels: channels.size,
|
|
93
|
+
}),
|
|
94
|
+
live: feedState.live,
|
|
95
|
+
probed,
|
|
96
|
+
required: !!options.required?.length,
|
|
97
|
+
get refused() {
|
|
98
|
+
return feedState.refused()
|
|
99
|
+
},
|
|
100
|
+
shutdown,
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function createOperations(deps: View) {
|
|
105
|
+
const { want, rest, pokes, feedState, feed, bus, conns, deliver, closing, close, channels } = deps
|
|
106
|
+
return {
|
|
107
|
+
ready: () => {
|
|
108
|
+
const on = Promise.resolve(want()).then(rest)
|
|
109
|
+
return Promise.race([
|
|
110
|
+
Promise.all([pokes().ready.catch(() => {}), on]),
|
|
111
|
+
new Promise((ok) => {
|
|
112
|
+
unref(setTimeout(ok, 250))
|
|
113
|
+
}),
|
|
114
|
+
])
|
|
115
|
+
},
|
|
116
|
+
async poke(this: void, tags: string[], origin?: string | null) {
|
|
117
|
+
if (!tags.length) return
|
|
118
|
+
// In this order: a client's refetch must start after what the barrier claims is covered.
|
|
119
|
+
if (feedState.live().mode === 'feed' || feedState.live().mode === 'external')
|
|
120
|
+
await feed?.settle(tags)
|
|
121
|
+
await bus.publish('kerf:poke', JSON.stringify({ t: tags, ...(origin && { o: origin }) }))
|
|
122
|
+
},
|
|
123
|
+
push(this: void, id: string, msg: unknown) {
|
|
124
|
+
let hit = 0
|
|
125
|
+
for (const c of conns.values()) {
|
|
126
|
+
for (const s of c.subs.values()) {
|
|
127
|
+
if (s.fn.$id !== id) continue
|
|
128
|
+
hit++
|
|
129
|
+
deliver(c, s, JSON.stringify([s.n, null, msg]))
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return hit
|
|
133
|
+
},
|
|
134
|
+
async shutdown(this: void) {
|
|
135
|
+
feedState.clear()
|
|
136
|
+
await feed?.stop()
|
|
137
|
+
for (const fn of closing) fn()
|
|
138
|
+
for (const c of conns.values()) close(c)
|
|
139
|
+
for (const ch of channels.values()) {
|
|
140
|
+
clearTimeout(ch.idle)
|
|
141
|
+
ch.stop?.()
|
|
142
|
+
}
|
|
143
|
+
channels.clear()
|
|
144
|
+
await bus.close?.()
|
|
145
|
+
},
|
|
146
|
+
}
|
|
147
|
+
}
|
package/src/hub.ts
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
// The server's realtime hub (research 30 §5). No runtime imports, so one hub runs behind `/_kerf/rt` and behind
|
|
2
|
+
// @kerf-js/core/test's fake transport: both hand it a sink, and only the HTTP sink knows about bytes.
|
|
3
|
+
//
|
|
4
|
+
// rings one for pokes, always listening, so a rendered page's cursor can replay the gap before it connects;
|
|
5
|
+
// one per topic channel, alive while someone listens and one window longer. Cursors are
|
|
6
|
+
// `<epoch>:<seq>`: another epoch, or one older than a ring's floor, is out of the window.
|
|
7
|
+
// connections one per tab, changed in place (`k`), so adding a subscription never restarts another's `run`.
|
|
8
|
+
// backpressure past the high-water mark a subscription holds its last message, or up to `buffer`; pokes merge by tag.
|
|
9
|
+
// A buffer that overflows resets that stream; a connection over `queue` bytes is closed, and resumes.
|
|
10
|
+
|
|
11
|
+
import { memoryBus } from './bus'
|
|
12
|
+
import { StartError, type StandardSchemaV1 } from './data'
|
|
13
|
+
import type { Scope } from './exec'
|
|
14
|
+
import { createChannels } from './hub/channels'
|
|
15
|
+
import { createConnections } from './hub/connections'
|
|
16
|
+
import { createDelivery } from './hub/delivery'
|
|
17
|
+
import { createFeedState } from './hub/feed'
|
|
18
|
+
import { createPresence } from './hub/presence'
|
|
19
|
+
import { handleRoute } from './hub/route'
|
|
20
|
+
import { createSubscriptions } from './hub/subscriptions'
|
|
21
|
+
import { makeTopic, type Topic } from './hub/topic'
|
|
22
|
+
import type { Conn, Hub, HubOptions, Limits, LiveState, RealtimeOptions } from './hub/types'
|
|
23
|
+
import { createHubView } from './hub/view'
|
|
24
|
+
|
|
25
|
+
export { validBody } from './hub/route'
|
|
26
|
+
export { Tracked, tracked } from './hub/tracked'
|
|
27
|
+
export type {
|
|
28
|
+
Limits,
|
|
29
|
+
RealtimeOptions,
|
|
30
|
+
HubOptions,
|
|
31
|
+
LiveState,
|
|
32
|
+
Sink,
|
|
33
|
+
SubEntry,
|
|
34
|
+
Body,
|
|
35
|
+
Ev,
|
|
36
|
+
Channel,
|
|
37
|
+
Conn,
|
|
38
|
+
Hub,
|
|
39
|
+
} from './hub/types'
|
|
40
|
+
|
|
41
|
+
const LIMITS: Limits = {
|
|
42
|
+
subscriptions: 100,
|
|
43
|
+
connections: 10,
|
|
44
|
+
body: 64 * 1024,
|
|
45
|
+
message: 1 << 20,
|
|
46
|
+
queue: 1 << 20,
|
|
47
|
+
ring: 256,
|
|
48
|
+
window: 60_000,
|
|
49
|
+
heartbeat: 20_000,
|
|
50
|
+
session: 300_000,
|
|
51
|
+
perSecond: 20,
|
|
52
|
+
burst: 40,
|
|
53
|
+
shareKeys: 1000,
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** `src/realtime.server.ts`'s default export. */
|
|
57
|
+
export function realtime(options: RealtimeOptions): RealtimeOptions {
|
|
58
|
+
checkOrigins(options.origins)
|
|
59
|
+
return options
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** R004. `realtime()` is a wrapper an app can leave out, so the upgrade calls this too. */
|
|
63
|
+
export function checkOrigins(origins: string[] = []) {
|
|
64
|
+
if (origins.some((o) => o.includes('*')))
|
|
65
|
+
throw new StartError(
|
|
66
|
+
'R004',
|
|
67
|
+
"`realtime({ origins })` has a wildcard: any site could read this app's streams and join its channels with a visitor's cookies",
|
|
68
|
+
'list the origins, scheme and port included',
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** A hub on these options; `hub()` is the usual way to reach the one a server made. */
|
|
73
|
+
export function createHub(options: HubOptions): Hub {
|
|
74
|
+
const bus = options.bus ?? memoryBus()
|
|
75
|
+
const limits = { ...LIMITS, ...options.limits }
|
|
76
|
+
const epoch = crypto.randomUUID().slice(0, 8)
|
|
77
|
+
const { channels, channel, unlisten, replay, seq } = createChannels(bus, limits, epoch)
|
|
78
|
+
const conns = new Map<string, Conn>()
|
|
79
|
+
const watchers = new Set<(tags: string[]) => void>()
|
|
80
|
+
const closing = new Set<() => void>()
|
|
81
|
+
const cursorOf = (n: number) => `${epoch}:${n}`
|
|
82
|
+
const { poke, deliver, drain } = createDelivery(limits, cursorOf, (c, retry) =>
|
|
83
|
+
connections.bye(c, retry),
|
|
84
|
+
)
|
|
85
|
+
const source = options.changes
|
|
86
|
+
const { fail, change } = createSubscriptions({ limits, source, deliver })
|
|
87
|
+
const presence = createPresence({
|
|
88
|
+
channels,
|
|
89
|
+
channel,
|
|
90
|
+
conns,
|
|
91
|
+
watchers,
|
|
92
|
+
cursorOf,
|
|
93
|
+
seq,
|
|
94
|
+
poke,
|
|
95
|
+
replay,
|
|
96
|
+
heartbeat: () => feedState.heartbeat(),
|
|
97
|
+
})
|
|
98
|
+
const { pokes, replayPokes } = presence
|
|
99
|
+
const feedState = createFeedState(options, limits, conns, pokes)
|
|
100
|
+
const { want, rest, wire, probed, feed } = feedState
|
|
101
|
+
const connections = createConnections({
|
|
102
|
+
conns,
|
|
103
|
+
limits,
|
|
104
|
+
epoch,
|
|
105
|
+
options,
|
|
106
|
+
source,
|
|
107
|
+
pokes,
|
|
108
|
+
want,
|
|
109
|
+
rest,
|
|
110
|
+
cursorOf,
|
|
111
|
+
seq,
|
|
112
|
+
wire,
|
|
113
|
+
change,
|
|
114
|
+
replayPokes,
|
|
115
|
+
fail,
|
|
116
|
+
})
|
|
117
|
+
const { connect, close } = connections
|
|
118
|
+
|
|
119
|
+
return createHubView({
|
|
120
|
+
bus,
|
|
121
|
+
limits,
|
|
122
|
+
epoch,
|
|
123
|
+
options,
|
|
124
|
+
channels,
|
|
125
|
+
conns,
|
|
126
|
+
watchers,
|
|
127
|
+
closing,
|
|
128
|
+
cursorOf,
|
|
129
|
+
channel,
|
|
130
|
+
replay,
|
|
131
|
+
unlisten,
|
|
132
|
+
seq,
|
|
133
|
+
pokes,
|
|
134
|
+
want,
|
|
135
|
+
rest,
|
|
136
|
+
feedState,
|
|
137
|
+
feed,
|
|
138
|
+
probed,
|
|
139
|
+
deliver,
|
|
140
|
+
drain,
|
|
141
|
+
connect,
|
|
142
|
+
close,
|
|
143
|
+
change,
|
|
144
|
+
})
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
let current: Hub | undefined
|
|
148
|
+
/** The process's hub: the first caller makes the default one (memory bus). */
|
|
149
|
+
export const hub = (): Hub => (current ??= createHub({}))
|
|
150
|
+
/** Set by `createHandler`, from `src/realtime.server.ts`. */
|
|
151
|
+
export const configure = (options: HubOptions) => {
|
|
152
|
+
void current?.shutdown() // a dev server's reload makes a new handler
|
|
153
|
+
return (current = createHub(options))
|
|
154
|
+
}
|
|
155
|
+
/** This instance's connections, subscriptions and listened channels: for a health route. */
|
|
156
|
+
export const realtimeStats = () => hub().stats()
|
|
157
|
+
/**
|
|
158
|
+
* For a health route: `ok` is false when required live queries have no working feed, now or at startup. A server that
|
|
159
|
+
* started fine and lost its database mid-flight keeps running, and says so here: restarting it wouldn't bring the
|
|
160
|
+
* database back, and it recovers by itself.
|
|
161
|
+
*/
|
|
162
|
+
export function liveHealth(): LiveState & { ok: boolean; required: boolean } {
|
|
163
|
+
const h = hub(),
|
|
164
|
+
s = h.live(),
|
|
165
|
+
required = h.required
|
|
166
|
+
return {
|
|
167
|
+
...s,
|
|
168
|
+
required,
|
|
169
|
+
ok: !required || ((s.mode === 'feed' || s.mode === 'external') && !s.down),
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/** @kerf-js/core/test, after each test. */
|
|
173
|
+
export const resetHub = () => {
|
|
174
|
+
const old = current
|
|
175
|
+
current = undefined
|
|
176
|
+
return old?.shutdown()
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export type { Topic } from './hub/topic'
|
|
180
|
+
type Out<S> = S extends StandardSchemaV1<unknown, infer O> ? O : never
|
|
181
|
+
/** A typed server channel: mutations, jobs and webhooks publish, streams read. */
|
|
182
|
+
export function topic<
|
|
183
|
+
K extends StandardSchemaV1<unknown, string | number>,
|
|
184
|
+
M extends StandardSchemaV1,
|
|
185
|
+
>(name: string, def: { key?: K; message: M }): Topic<Out<K>, Out<M>> {
|
|
186
|
+
return makeTopic(name, def, hub)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** `/_kerf/rt` opens a connection or changes one. */
|
|
190
|
+
export async function rtRoute(
|
|
191
|
+
request: Request,
|
|
192
|
+
scope: Scope,
|
|
193
|
+
origins: string[] = [],
|
|
194
|
+
): Promise<Response> {
|
|
195
|
+
return handleRoute(request, scope, origins, hub)
|
|
196
|
+
}
|