@weft-js/start 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +16 -0
- package/README.md +41 -2
- package/package.json +107 -4
- package/src/app.mjs +93 -0
- package/src/auth/better-auth.ts +58 -0
- package/src/auth/drizzle.ts +17 -0
- package/src/auth.ts +47 -0
- package/src/boot.ts +44 -0
- package/src/builtin.tsx +19 -0
- package/src/bus/postgres.ts +91 -0
- package/src/bus.ts +48 -0
- package/src/capture.d.mts +1 -0
- package/src/capture.mjs +15 -0
- package/src/changes-sql.d.mts +14 -0
- package/src/changes-sql.mjs +99 -0
- package/src/changes.ts +253 -0
- package/src/channel-fake.ts +55 -0
- package/src/channel.ts +622 -0
- package/src/client.tsx +20 -0
- package/src/contract.mjs +312 -0
- package/src/data.ts +929 -0
- package/src/db/bun-sqlite.d.ts +10 -0
- package/src/db/changes-mysql.ts +86 -0
- package/src/db/changes-postgres.ts +159 -0
- package/src/db/changes-sqlite.ts +116 -0
- package/src/db/core.ts +63 -0
- package/src/db/index.ts +27 -0
- package/src/db/mysql.ts +67 -0
- package/src/db/postgres.ts +65 -0
- package/src/db/sqlite.ts +139 -0
- package/src/derive.d.mts +16 -0
- package/src/derive.mjs +140 -0
- package/src/dev-runtime.mjs +42 -0
- package/src/exec.ts +519 -0
- package/src/extension.ts +38 -0
- package/src/first-paint.ts +57 -0
- package/src/form.tsx +134 -0
- package/src/gate.css +6 -0
- package/src/hub.ts +822 -0
- package/src/index.ts +106 -0
- package/src/inline.mjs +2 -0
- package/src/inspect.mjs +280 -0
- package/src/link.ts +461 -0
- package/src/list-drizzle.ts +50 -0
- package/src/list.ts +224 -0
- package/src/listen.ts +28 -0
- package/src/live-fake.ts +42 -0
- package/src/live-target.mjs +147 -0
- package/src/live.ts +499 -0
- package/src/media.ts +43 -0
- package/src/migrations.mjs +19 -0
- package/src/mux-session.ts +6 -0
- package/src/mux.ts +135 -0
- package/src/og/DefaultCard.tsx +60 -0
- package/src/og/fonts.mjs +64 -0
- package/src/og/images.mjs +54 -0
- package/src/og/index.mjs +146 -0
- package/src/og/native.mjs +29 -0
- package/src/range.mjs +19 -0
- package/src/router.ts +149 -0
- package/src/routes.mjs +142 -0
- package/src/run.tsx +202 -0
- package/src/rust.ts +333 -0
- package/src/server.tsx +1021 -0
- package/src/shape.d.mts +3 -0
- package/src/shape.mjs +73 -0
- package/src/share-server.ts +208 -0
- package/src/share.ts +503 -0
- package/src/sheets.mjs +75 -0
- package/src/sidecar.mjs +503 -0
- package/src/socket.ts +389 -0
- package/src/stream.ts +13 -0
- package/src/verify.ts +251 -0
- package/src/view.tsx +47 -0
- package/src/vite.mjs +899 -0
package/src/channel.ts
ADDED
|
@@ -0,0 +1,622 @@
|
|
|
1
|
+
// `channel` and `sharedState` (research 32): definitions on both sides, and the browser's half. One line per tab on the
|
|
2
|
+
// browser's one socket (link.ts), one join per id + params, shared by every reader and held by this module, so a remount within `keep`
|
|
3
|
+
// finds its messages. The socket server is socket.ts, and `room.in(params).publish` reaches it through
|
|
4
|
+
// `runtime.channels`, so nothing of the server ships here.
|
|
5
|
+
|
|
6
|
+
import { Box, DEV, batch, collect as boxes, createRoot, derived, effect, getOwner, onCleanup, state, untrack, type Derived, type State } from '@weft-js/core'
|
|
7
|
+
import { HANDOVER, SOCKETS, openLine, socketDown } from './link'
|
|
8
|
+
import { currentScope, fns, FnError, keyOf, net, rt, runtime, skip, StartError, track, type Context, type Fn, type Skip, type StandardSchemaV1 } from './data'
|
|
9
|
+
|
|
10
|
+
// ---- definitions ----------------------------------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
type In<S> = S extends StandardSchemaV1<infer I, unknown> ? I : void
|
|
13
|
+
type Out<S> = S extends StandardSchemaV1<unknown, infer O> ? O : undefined
|
|
14
|
+
type Schemas = Record<string, StandardSchemaV1>
|
|
15
|
+
type Inputs<T extends Schemas> = { [K in keyof T]: In<T[K]> }
|
|
16
|
+
type Outputs<T extends Schemas> = { [K in keyof T]: Out<T[K]> }
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Where a connection stands; `unauthorized` waits for `rejoin`. `unavailable`: this deploy can't hold a socket (a
|
|
20
|
+
* function host, R024), so nothing connects and nothing is retried.
|
|
21
|
+
*/
|
|
22
|
+
export type ChannelStatus = 'connecting' | 'open' | 'reconnecting' | 'closed' | 'unauthorized' | 'unavailable'
|
|
23
|
+
|
|
24
|
+
/** `room.publish`: who a message goes to, and whether it can be replayed. */
|
|
25
|
+
export interface PublishOptions {
|
|
26
|
+
/** Not back to the sender. */
|
|
27
|
+
exceptSelf?: boolean
|
|
28
|
+
/** Only to this connection, wherever it is: the `id` a handler was given. */
|
|
29
|
+
to?: string
|
|
30
|
+
/** Not kept for a resume: a cursor, a heartbeat, anything the next one replaces. */
|
|
31
|
+
volatile?: boolean
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** A channel instance as a server handler has it. */
|
|
35
|
+
export interface Room<P, M> {
|
|
36
|
+
params: P
|
|
37
|
+
/** To everyone in the room, on every instance. */
|
|
38
|
+
publish<K extends keyof M & string>(name: K, message: M[K], options?: PublishOptions): Promise<void>
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** `channel()`: the params, the messages each side may send, and who may join. */
|
|
42
|
+
export interface ChannelDef<P extends StandardSchemaV1 | undefined, C extends Schemas, S extends Schemas> {
|
|
43
|
+
params?: P
|
|
44
|
+
/** Required (R001). Runs on every join and at each session re-check. `() => true` must be written out. */
|
|
45
|
+
authorize: (args: { ctx: Context; params: Out<P> }) => boolean | Promise<boolean>
|
|
46
|
+
/** Client → server, always validated. */
|
|
47
|
+
client?: C
|
|
48
|
+
/** Server → client, validated in dev and tests. */
|
|
49
|
+
server?: S
|
|
50
|
+
/** `id` is the sender's connection, the same one `left` gets. */
|
|
51
|
+
on?: { [K in keyof C]?: (args: { msg: Out<C[K]>; id: string; ctx: Context; params: Out<P>; room: Room<Out<P>, Inputs<S>> }) => unknown }
|
|
52
|
+
/** A connection stopped being in the room: it left, closed, or lost `authorize`. Not called if its instance dies. */
|
|
53
|
+
left?: (args: { id: string; ctx: Context; params: Out<P>; room: Room<Out<P>, Inputs<S>> }) => unknown
|
|
54
|
+
limits?: { perSecond?: number }
|
|
55
|
+
$id?: string
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** A live join of a channel, from `.connect()`. */
|
|
59
|
+
export interface Connection<C, S> {
|
|
60
|
+
readonly status: Derived<ChannelStatus>
|
|
61
|
+
/** Which room this is. Changes when a function argument moves to another, even one already open. */
|
|
62
|
+
readonly room: Derived<string | undefined>
|
|
63
|
+
/** The last refusal or failure. */
|
|
64
|
+
readonly error: unknown
|
|
65
|
+
/** Ticks when the server couldn't resume: messages may be missing, so refetch what the room's history is. */
|
|
66
|
+
readonly resets: Derived<number>
|
|
67
|
+
/** Keep a room's messages of one name, in the cache: a remount within `keep` finds them. `key` drops a repeat. */
|
|
68
|
+
collect<K extends keyof S & string>(name: K, options?: { max?: number; key?: (message: S[K]) => unknown }): Derived<S[K][]>
|
|
69
|
+
/** Each message of one name, until the owner is disposed. */
|
|
70
|
+
on<K extends keyof S & string>(name: K, fn: (message: S[K]) => void): () => void
|
|
71
|
+
onReset(fn: () => void): () => void
|
|
72
|
+
/** Resolves on the server's ack, rejects with what it refused. Waits up to 10 s for the socket; never retries. */
|
|
73
|
+
send<K extends keyof C & string>(name: K, message: C[K]): Promise<void>
|
|
74
|
+
/** Join again after `unauthorized`. */
|
|
75
|
+
retry(): void
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
type Arg<P> = P | (() => P | Skip)
|
|
79
|
+
/** How long a join outlives the last component using it. */
|
|
80
|
+
export interface ConnectOptions { keep?: string | number }
|
|
81
|
+
|
|
82
|
+
/** What `channel()` returns: connect from a page, publish from the server. */
|
|
83
|
+
export interface Channel<P, C, S> {
|
|
84
|
+
/** In a component or scope. A function argument re-joins when what it reads changes; `skip` leaves. */
|
|
85
|
+
connect(...args: [P] extends [void] ? [params?: undefined | (() => undefined | Skip), options?: ConnectOptions] : [params: Arg<P>, options?: ConnectOptions]): Connection<C, S>
|
|
86
|
+
/** On the server, from a mutation or a job. */
|
|
87
|
+
in(...args: [P] extends [void] ? [] : [params: P]): { publish<K extends keyof S & string>(name: K, message: S[K]): Promise<void> }
|
|
88
|
+
readonly kind: 'channel'
|
|
89
|
+
readonly $id: string
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const FN = Symbol.for('weft.fn')
|
|
93
|
+
let auto = 0
|
|
94
|
+
|
|
95
|
+
/** A named room of schema-checked messages over the shared socket. */
|
|
96
|
+
export function channel<P extends StandardSchemaV1 | undefined = undefined, C extends Schemas = {}, S extends Schemas = {}>(def: ChannelDef<P, C, S>): Channel<In<P>, Inputs<C>, Outputs<S>> {
|
|
97
|
+
const id = def.$id ?? `c${++auto}`
|
|
98
|
+
if (!(def as { $stub?: boolean }).$stub && !def.authorize) throw new StartError('R001', `\`${id}\` has no \`authorize\`: anyone could join it`, 'add `authorize` (`() => true` must be written out)')
|
|
99
|
+
const fn: Fn = { kind: 'channel', $id: id, def: def as Fn['def'] }
|
|
100
|
+
fns.set(id, fn)
|
|
101
|
+
return {
|
|
102
|
+
kind: 'channel', $id: id, [FN]: fn,
|
|
103
|
+
connect: (params?: unknown, options?: ConnectOptions) => connect(fn, params, options),
|
|
104
|
+
in: (params?: unknown) => ({ publish: (name: string, message: unknown) => runtime.channels!.publish(fn, params, name, message) }),
|
|
105
|
+
} as never
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** `sharedState()`: the key and value schemas, the first value, and who reads and writes. */
|
|
109
|
+
export interface SharedDef<K extends StandardSchemaV1, V extends StandardSchemaV1> {
|
|
110
|
+
key: K
|
|
111
|
+
value: V
|
|
112
|
+
/** What a reader has until the server says otherwise. Kept in the client bundle, so plain data. */
|
|
113
|
+
initial: Out<V>
|
|
114
|
+
/** Both required (R001). */
|
|
115
|
+
authorize: { read: (args: { ctx: Context; key: Out<K> }) => boolean | Promise<boolean>; write: (args: { ctx: Context; key: Out<K> }) => boolean | Promise<boolean> }
|
|
116
|
+
$id?: string
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** A box in a scope: reads as a value, and a write is optimistic, sent, and last writer wins on the server. */
|
|
120
|
+
export type SharedBox<T> = State<T> & { readonly status: Derived<ChannelStatus>; readonly error: unknown }
|
|
121
|
+
|
|
122
|
+
/** What `sharedState()` returns: read a value on a page, set it from the server. */
|
|
123
|
+
export interface SharedState<K, V> {
|
|
124
|
+
connect(key: K | (() => K | Skip), options?: ConnectOptions): SharedBox<V>
|
|
125
|
+
/** On the server: write it from a mutation or a job. */
|
|
126
|
+
in(key: K): { set(value: V): Promise<void> }
|
|
127
|
+
readonly kind: 'shared'
|
|
128
|
+
readonly $id: string
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** One value per key that every client holding that key sees change. */
|
|
132
|
+
export function sharedState<K extends StandardSchemaV1, V extends StandardSchemaV1>(name: string, def: SharedDef<K, V>): SharedState<In<K>, Out<V>> {
|
|
133
|
+
const id = def.$id ?? `shared:${name}`
|
|
134
|
+
if (!(def as { $stub?: boolean }).$stub && (!def.authorize?.read || !def.authorize?.write)) throw new StartError('R001', `\`${id}\` needs \`authorize: { read, write }\`: anyone could read or write it`, '`() => true` must be written out')
|
|
135
|
+
const fn: Fn = { kind: 'shared', $id: id, def }
|
|
136
|
+
fns.set(id, fn)
|
|
137
|
+
return {
|
|
138
|
+
kind: 'shared', $id: id, [FN]: fn,
|
|
139
|
+
connect: (key: unknown, options?: ConnectOptions) => shared(fn, key, options),
|
|
140
|
+
in: (key: unknown) => ({ set: (value: unknown) => runtime.channels!.set(fn, key, value) }),
|
|
141
|
+
} as never
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// ---- joins ----------------------------------------------------------------------------------------------------------
|
|
145
|
+
|
|
146
|
+
interface ListSpec { name: string; max: number; key?: ((m: never) => unknown) | undefined }
|
|
147
|
+
interface List extends ListSpec { box: State<unknown[]> }
|
|
148
|
+
interface Handle { on: Map<string, Set<(m: never) => void>>; resets: Set<() => void>; value?(v: { v: unknown; at: number }): void }
|
|
149
|
+
interface Join {
|
|
150
|
+
n: number
|
|
151
|
+
key: string
|
|
152
|
+
fn: Fn
|
|
153
|
+
params: unknown
|
|
154
|
+
refs: number
|
|
155
|
+
timer?: ReturnType<typeof setTimeout> | undefined
|
|
156
|
+
cursor?: string | undefined
|
|
157
|
+
joined: boolean
|
|
158
|
+
refused: boolean
|
|
159
|
+
status: State<ChannelStatus>
|
|
160
|
+
error: State<unknown>
|
|
161
|
+
resets: State<number>
|
|
162
|
+
lists: Map<string, List>
|
|
163
|
+
handles: Set<Handle>
|
|
164
|
+
/** `send`s waiting for the join. */
|
|
165
|
+
waiting: Set<() => void>
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const joins = new Map<string, Join>()
|
|
169
|
+
const byN = new Map<number, Join>()
|
|
170
|
+
let nth = 0
|
|
171
|
+
|
|
172
|
+
const KEEP = 30_000
|
|
173
|
+
const ms = (keep: string | number = KEEP): number => {
|
|
174
|
+
if (typeof keep === 'number') return keep
|
|
175
|
+
const m = /^(\d+)(ms|s|m)$/.exec(keep)
|
|
176
|
+
return m ? +m[1]! * { ms: 1, s: 1000, m: 60_000 }[m[2] as 'ms' | 's' | 'm'] : Infinity
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function acquire(fn: Fn, params: unknown): Join {
|
|
180
|
+
const key = keyOf(fn.$id, params)
|
|
181
|
+
let j = joins.get(key)
|
|
182
|
+
if (!j) {
|
|
183
|
+
j = createRoot(() => ({ n: ++nth, key, fn, params, refs: 0, joined: false, refused: false, status: state<ChannelStatus>('connecting'), error: state<unknown>(undefined), resets: state(0), lists: new Map(), handles: new Set(), waiting: new Set() }), null)
|
|
184
|
+
if (DEV && net() && !net()!.resets.includes(reset)) net()!.resets.push(reset)
|
|
185
|
+
joins.set(key, j)
|
|
186
|
+
byN.set(j.n, j)
|
|
187
|
+
if (live) enter(j)
|
|
188
|
+
else start()
|
|
189
|
+
}
|
|
190
|
+
clearTimeout(j.timer)
|
|
191
|
+
j.timer = undefined
|
|
192
|
+
j.refs++
|
|
193
|
+
return j
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function release(j: Join, keep?: string | number) {
|
|
197
|
+
if (--j.refs) return
|
|
198
|
+
const t = ms(keep)
|
|
199
|
+
if (t === Infinity) return
|
|
200
|
+
if (t) j.timer = setTimeout(() => drop(j), t)
|
|
201
|
+
else drop(j)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function drop(j: Join) {
|
|
205
|
+
joins.delete(j.key)
|
|
206
|
+
byN.delete(j.n)
|
|
207
|
+
j.status.value = 'closed'
|
|
208
|
+
if (live) send(['l', j.n])
|
|
209
|
+
for (const w of Array.from(j.waiting)) w()
|
|
210
|
+
if (!joins.size) live?.close()
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function hold(j: Join, keep?: string | number): () => void {
|
|
214
|
+
const token = {}
|
|
215
|
+
const stop = () => {
|
|
216
|
+
net()?.open.delete(token)
|
|
217
|
+
release(j, keep)
|
|
218
|
+
}
|
|
219
|
+
if (DEV) net()?.open.set(token, [`channel \`${j.key}\``, stop])
|
|
220
|
+
return stop
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function listOf(j: Join, spec: ListSpec): List {
|
|
224
|
+
const id = `${spec.name}:${spec.max}:${spec.key ? String(spec.key) : ''}`
|
|
225
|
+
let l = j.lists.get(id)
|
|
226
|
+
if (!l) j.lists.set(id, (l = createRoot(() => ({ ...spec, box: state<unknown[]>([]) }), null)))
|
|
227
|
+
return l
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const NONE: never[] = []
|
|
231
|
+
type View = Join | undefined
|
|
232
|
+
|
|
233
|
+
/** The part `connect` and `shared` have in common: the current join, as the argument moves. */
|
|
234
|
+
function follow(fn: Fn, arg: unknown, options: ConnectOptions | undefined, handle: Handle, joined?: (j: Join) => void): State<View> {
|
|
235
|
+
const view = state<View>(undefined)
|
|
236
|
+
effect(() => {
|
|
237
|
+
const params = typeof arg === 'function' ? (arg as () => unknown)() : arg
|
|
238
|
+
if (params === skip) return undefined
|
|
239
|
+
return untrack(() => {
|
|
240
|
+
const j = acquire(fn, params)
|
|
241
|
+
j.handles.add(handle)
|
|
242
|
+
joined?.(j)
|
|
243
|
+
const stop = hold(j, options?.keep)
|
|
244
|
+
onCleanup(() => {
|
|
245
|
+
j.handles.delete(handle)
|
|
246
|
+
stop()
|
|
247
|
+
})
|
|
248
|
+
return j
|
|
249
|
+
})
|
|
250
|
+
}, now => { view.value = now })
|
|
251
|
+
return view
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function owned(fn: Fn) {
|
|
255
|
+
if (!getOwner()) throw new StartError('W051', DEV ? `\`${fn.$id}.connect()\` was called outside a component or scope: nothing would close it` : fn.$id, DEV ? 'call it in a component, a `defineState` setup or `createRoot(dispose => …)`' : undefined)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** `@weft-js/start/share`'s way in: a join that nothing on the page declared, so a tab shares this socket and its lifecycle. */
|
|
259
|
+
export const connectBuiltIn = ($id: string) => connect({ kind: 'channel', $id, def: {} }, undefined, { keep: 0 })
|
|
260
|
+
|
|
261
|
+
function connect(fn: Fn, arg: unknown, options?: ConnectOptions): Connection<Record<string, never>, Record<string, never>> {
|
|
262
|
+
owned(fn)
|
|
263
|
+
if (currentScope()) {
|
|
264
|
+
const closed = { value: 'connecting' as ChannelStatus }
|
|
265
|
+
return { status: closed, room: { value: undefined }, error: undefined, resets: { value: 0 }, collect: () => ({ value: NONE }), on: () => () => {}, onReset: () => () => {}, send: () => Promise.reject(new FnError({ message: 'a channel has no socket on the server' }, 500)), retry() {} }
|
|
266
|
+
}
|
|
267
|
+
const handle: Handle = { on: new Map(), resets: new Set() }
|
|
268
|
+
const wanted: ListSpec[] = []
|
|
269
|
+
const view = follow(fn, arg, options, handle, j => { for (const w of wanted) listOf(j, w) })
|
|
270
|
+
return {
|
|
271
|
+
status: derived(() => view.value?.status.value ?? 'closed'),
|
|
272
|
+
room: derived(() => view.value?.key),
|
|
273
|
+
get error() { return view.value?.error.value },
|
|
274
|
+
resets: derived(() => view.value?.resets.value ?? 0),
|
|
275
|
+
collect(name, { max = 200, key } = {}) {
|
|
276
|
+
const spec: ListSpec = { name, max, key }
|
|
277
|
+
wanted.push(spec)
|
|
278
|
+
const had = untrack(() => view.value)
|
|
279
|
+
if (had) listOf(had, spec)
|
|
280
|
+
return derived(() => {
|
|
281
|
+
const j = view.value
|
|
282
|
+
return (j ? listOf(j, spec).box.value : NONE) as never[]
|
|
283
|
+
})
|
|
284
|
+
},
|
|
285
|
+
on(name, listener) {
|
|
286
|
+
const set = handle.on.get(name) ?? new Set()
|
|
287
|
+
handle.on.set(name, set)
|
|
288
|
+
set.add(listener)
|
|
289
|
+
const stop = () => void set.delete(listener)
|
|
290
|
+
onCleanup(stop)
|
|
291
|
+
return stop
|
|
292
|
+
},
|
|
293
|
+
onReset(listener) {
|
|
294
|
+
handle.resets.add(listener)
|
|
295
|
+
const stop = () => void handle.resets.delete(listener)
|
|
296
|
+
onCleanup(stop)
|
|
297
|
+
return stop
|
|
298
|
+
},
|
|
299
|
+
send(name, message) {
|
|
300
|
+
const j = untrack(() => view.value)
|
|
301
|
+
return j ? post(j, name, message) : Promise.reject(new FnError({ message: 'this channel is closed' }, 400))
|
|
302
|
+
},
|
|
303
|
+
retry() {
|
|
304
|
+
const j = untrack(() => view.value)
|
|
305
|
+
if (j?.refused) rejoin(j)
|
|
306
|
+
},
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** `Box.w` is the one way a write gets in, so a subclass sees every write, and `Instance` never touches it. */
|
|
311
|
+
class Shared<T> extends Box<T> {
|
|
312
|
+
declare remote?: boolean
|
|
313
|
+
declare pending: number
|
|
314
|
+
declare base: { v: T; at: number }
|
|
315
|
+
declare view: State<View>
|
|
316
|
+
declare statusOf: Derived<ChannelStatus>
|
|
317
|
+
declare problem: State<unknown>
|
|
318
|
+
get status() { return this.statusOf }
|
|
319
|
+
get error() { return this.problem.value }
|
|
320
|
+
/** A server value: applied unless a local write is still on its way. */
|
|
321
|
+
apply(v: { v: T; at: number }) {
|
|
322
|
+
if (v.at >= this.base.at) this.base = v
|
|
323
|
+
if (!this.pending) this.quiet(this.base.v)
|
|
324
|
+
}
|
|
325
|
+
quiet(v: T) {
|
|
326
|
+
this.remote = true
|
|
327
|
+
this.w(v)
|
|
328
|
+
this.remote = false
|
|
329
|
+
}
|
|
330
|
+
override w(v: T) {
|
|
331
|
+
if (this.remote) return super.w(v)
|
|
332
|
+
const j = untrack(() => this.view.value)
|
|
333
|
+
if (!j) return super.w(v)
|
|
334
|
+
super.w(v)
|
|
335
|
+
this.pending++
|
|
336
|
+
this.problem.value = undefined
|
|
337
|
+
void track(post(j, '$set', v).then(() => { this.pending-- }, e => {
|
|
338
|
+
this.pending--
|
|
339
|
+
this.problem.value = e
|
|
340
|
+
}).finally(() => { if (!this.pending) this.quiet(this.base.v) }))
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function shared(fn: Fn, arg: unknown, options?: ConnectOptions): SharedBox<unknown> {
|
|
345
|
+
owned(fn)
|
|
346
|
+
const initial = fn.def.initial
|
|
347
|
+
const [b] = boxes(() => new Shared<unknown>(initial))
|
|
348
|
+
b.pending = 0
|
|
349
|
+
b.base = { v: initial, at: 0 }
|
|
350
|
+
b.problem = state<unknown>(undefined)
|
|
351
|
+
if (currentScope()) {
|
|
352
|
+
b.statusOf = { value: 'connecting' }
|
|
353
|
+
b.view = state<View>(undefined)
|
|
354
|
+
return b
|
|
355
|
+
}
|
|
356
|
+
const handle: Handle = { on: new Map(), resets: new Set(), value: v => b.apply(v) }
|
|
357
|
+
b.view = follow(fn, arg, options, handle, () => { b.base = { v: initial, at: 0 } })
|
|
358
|
+
b.statusOf = derived(() => b.view.value?.status.value ?? 'closed')
|
|
359
|
+
return b
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// ---- sends ----------------------------------------------------------------------------------------------------------
|
|
363
|
+
|
|
364
|
+
const ACK_WAIT = 10_000
|
|
365
|
+
const acks = new Map<number, { ok: () => void; no: (e: unknown) => void; n: number }>()
|
|
366
|
+
let ackId = 0
|
|
367
|
+
|
|
368
|
+
async function post(j: Join, name: string, message: unknown): Promise<void> {
|
|
369
|
+
if (!SOCKETS && !(DEV && net())) throw new FnError({ code: 'R024', message: "this deploy can't hold a socket: not sent" }, 503)
|
|
370
|
+
if (!j.joined && !j.refused) {
|
|
371
|
+
await new Promise<void>(ok => {
|
|
372
|
+
const done = () => {
|
|
373
|
+
clearTimeout(t)
|
|
374
|
+
j.waiting.delete(done)
|
|
375
|
+
ok()
|
|
376
|
+
}
|
|
377
|
+
const t = setTimeout(done, ACK_WAIT)
|
|
378
|
+
j.waiting.add(done)
|
|
379
|
+
})
|
|
380
|
+
}
|
|
381
|
+
if (!j.joined) throw new FnError({ ...(j.refused && { code: 'R011' }), message: j.refused ? 'not allowed in this channel' : 'no connection: not sent' }, j.refused ? 403 : 503)
|
|
382
|
+
const ack = ++ackId
|
|
383
|
+
const p = new Promise<void>((ok, no) => { acks.set(ack, { ok, no, n: j.n }) })
|
|
384
|
+
send(['m', j.n, ack, name, message])
|
|
385
|
+
return track(p)
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// ---- the socket ------------------------------------------------------------------------------------------------------
|
|
389
|
+
|
|
390
|
+
/** What the server sends a channel's client: the first element says which. */
|
|
391
|
+
export type Frame =
|
|
392
|
+
| ['h', { b?: string }]
|
|
393
|
+
| ['j', number, string | undefined]
|
|
394
|
+
| ['m', number, string | undefined, string, never]
|
|
395
|
+
| ['r', number, string | undefined]
|
|
396
|
+
| ['e', number, { code?: string; message?: string }]
|
|
397
|
+
| ['a', number, number, { code?: string; message?: string } | undefined]
|
|
398
|
+
|
|
399
|
+
/** One end of a socket, as a channel holds it. */
|
|
400
|
+
export interface SocketLink {
|
|
401
|
+
send(text: string): void
|
|
402
|
+
close(): void
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/** This tab's line on the browser's one socket (link.ts). `done` gets the close code. */
|
|
406
|
+
function browser(onFrame: (d: Frame) => void, closed: (code: number) => void): SocketLink {
|
|
407
|
+
return openLine('c', '', text => onFrame(JSON.parse(text)), closed)
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** @weft-js/core/test only: an opaque specifier, so a bundler keeps neither the fake nor the socket server (research 30 §10). */
|
|
411
|
+
const FAKE = './channel-fake.ts' // with its extension: nothing resolves this one for a linked package
|
|
412
|
+
function fake(onFrame: (d: Frame) => void, closed: (code: number) => void): SocketLink {
|
|
413
|
+
let link: SocketLink | undefined
|
|
414
|
+
let up!: () => void
|
|
415
|
+
void track(new Promise<void>(ok => { up = ok }))
|
|
416
|
+
const loading = import(/* @vite-ignore */ FAKE).then(m => m.open(onFrame, closed, up)).then(l => (link = l), e => (up(), closed(1006), Promise.reject(e))).catch(() => {})
|
|
417
|
+
return { send: t => void track(loading.then(() => link?.send(t))), close: () => { void loading.then(() => link?.close()) } }
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const BASE = 500, CAP = 30_000, PING = 20_000, DEAD = 45_000, HIDDEN = 300_000
|
|
421
|
+
|
|
422
|
+
let live: SocketLink | undefined
|
|
423
|
+
let open = false
|
|
424
|
+
let running = false
|
|
425
|
+
let attempt = 0
|
|
426
|
+
let build: string | undefined
|
|
427
|
+
let nudge: (() => void) | undefined
|
|
428
|
+
let parked = false
|
|
429
|
+
let hiddenAt: ReturnType<typeof setTimeout> | undefined
|
|
430
|
+
|
|
431
|
+
/** A leader tab can be hidden while a sibling is looking: `@weft-js/start/share` says when this socket must stay. */
|
|
432
|
+
export const keepOpen: { stay?: (() => boolean) | undefined } = {}
|
|
433
|
+
|
|
434
|
+
const send = (f: unknown[]) => live?.send(JSON.stringify(f))
|
|
435
|
+
const statusAll = (s: ChannelStatus) => { for (const j of joins.values()) if (!j.refused) j.status.value = s }
|
|
436
|
+
|
|
437
|
+
function enter(j: Join) {
|
|
438
|
+
if (!open || !live) return
|
|
439
|
+
send(['j', j.n, j.fn.$id, j.params, j.cursor])
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function rejoin(j: Join) {
|
|
443
|
+
j.refused = false
|
|
444
|
+
j.status.value = 'connecting'
|
|
445
|
+
if (running) enter(j)
|
|
446
|
+
else start()
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function start() {
|
|
450
|
+
if (running) return
|
|
451
|
+
// a function host: no socket to dial, so say so once instead of retrying forever
|
|
452
|
+
if (!SOCKETS && !(DEV && net())) return statusAll('unavailable')
|
|
453
|
+
running = true
|
|
454
|
+
queueMicrotask(() => void run())
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
function dispatch(j: Join, name: string, msg: never) {
|
|
458
|
+
if (name === '$v') {
|
|
459
|
+
for (const h of j.handles) h.value?.(msg)
|
|
460
|
+
return
|
|
461
|
+
}
|
|
462
|
+
for (const l of j.lists.values()) {
|
|
463
|
+
if (l.name !== name) continue
|
|
464
|
+
const was = untrack(() => l.box.value)
|
|
465
|
+
const key = l.key?.(msg)
|
|
466
|
+
const at = l.key ? was.findIndex(m => l.key!(m as never) === key) : -1
|
|
467
|
+
const next = at >= 0 ? was.map((m, i) => (i === at ? msg : m)) : [...was, msg]
|
|
468
|
+
l.box.value = next.length > l.max ? next.slice(-l.max) : next
|
|
469
|
+
}
|
|
470
|
+
for (const h of j.handles) for (const f of h.on.get(name) ?? []) f(msg)
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
function frame(d: Frame) {
|
|
474
|
+
batch(() => {
|
|
475
|
+
const j = typeof d[1] === 'number' ? byN.get(d[1]) : undefined
|
|
476
|
+
if (d[0] === 'h') {
|
|
477
|
+
if (build && d[1].b && d[1].b !== build) rt.stale.value = true
|
|
478
|
+
build = d[1].b
|
|
479
|
+
open = true
|
|
480
|
+
attempt = 0
|
|
481
|
+
for (const x of joins.values()) if (!x.refused) enter(x)
|
|
482
|
+
} else if (d[0] === 'j' && j) {
|
|
483
|
+
j.cursor ??= d[2]
|
|
484
|
+
j.joined = true
|
|
485
|
+
j.error.value = undefined
|
|
486
|
+
j.status.value = 'open'
|
|
487
|
+
for (const w of Array.from(j.waiting)) w()
|
|
488
|
+
} else if (d[0] === 'm' && j) {
|
|
489
|
+
j.cursor = d[2]
|
|
490
|
+
dispatch(j, d[3], d[4])
|
|
491
|
+
} else if (d[0] === 'r' && j) {
|
|
492
|
+
j.cursor = d[2]
|
|
493
|
+
for (const l of j.lists.values()) l.box.value = []
|
|
494
|
+
j.resets.value++
|
|
495
|
+
for (const h of j.handles) for (const f of h.resets) f()
|
|
496
|
+
} else if (d[0] === 'e' && j) {
|
|
497
|
+
j.joined = false
|
|
498
|
+
j.refused = true
|
|
499
|
+
j.error.value = new FnError(d[2], d[2].code === 'R011' ? 403 : 400)
|
|
500
|
+
j.status.value = d[2].code === 'R011' ? 'unauthorized' : 'closed'
|
|
501
|
+
for (const w of Array.from(j.waiting)) w()
|
|
502
|
+
} else if (d[0] === 'a') {
|
|
503
|
+
const p = acks.get(d[2])
|
|
504
|
+
acks.delete(d[2])
|
|
505
|
+
if (d[3]) p?.no(new FnError(d[3], d[3].code === 'R011' ? 403 : 400))
|
|
506
|
+
else p?.ok()
|
|
507
|
+
}
|
|
508
|
+
})
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
const sleep = (t: number) => new Promise<void>(ok => {
|
|
512
|
+
const timer = setTimeout(ok, t)
|
|
513
|
+
nudge = () => {
|
|
514
|
+
clearTimeout(timer)
|
|
515
|
+
nudge = undefined
|
|
516
|
+
ok()
|
|
517
|
+
}
|
|
518
|
+
})
|
|
519
|
+
|
|
520
|
+
async function run() {
|
|
521
|
+
listen(true)
|
|
522
|
+
const fakeNet = () => (DEV ? net() : undefined)
|
|
523
|
+
while (joins.size) {
|
|
524
|
+
if (parked || ! navigator.onLine) {
|
|
525
|
+
statusAll('reconnecting')
|
|
526
|
+
await sleep(CAP)
|
|
527
|
+
continue
|
|
528
|
+
}
|
|
529
|
+
let code = 0
|
|
530
|
+
let dead: ReturnType<typeof setTimeout> | undefined
|
|
531
|
+
let ping: ReturnType<typeof setInterval> | undefined
|
|
532
|
+
const alive = () => {
|
|
533
|
+
clearTimeout(dead)
|
|
534
|
+
if (!fakeNet()) dead = setTimeout(() => live?.close(), DEAD)
|
|
535
|
+
}
|
|
536
|
+
await new Promise<void>(done => {
|
|
537
|
+
const take = (d: Frame) => {
|
|
538
|
+
alive()
|
|
539
|
+
frame(d)
|
|
540
|
+
}
|
|
541
|
+
live = (fakeNet() ? fake : browser)(take, c => {
|
|
542
|
+
code = c
|
|
543
|
+
done()
|
|
544
|
+
})
|
|
545
|
+
alive()
|
|
546
|
+
if (!fakeNet()) ping = setInterval(() => send(['p']), PING)
|
|
547
|
+
})
|
|
548
|
+
clearTimeout(dead)
|
|
549
|
+
clearInterval(ping)
|
|
550
|
+
live = undefined
|
|
551
|
+
open = false
|
|
552
|
+
for (const j of joins.values()) j.joined = false
|
|
553
|
+
for (const [id, p] of acks) {
|
|
554
|
+
acks.delete(id)
|
|
555
|
+
p.no(new FnError({ message: 'the connection dropped: this may or may not have been sent' }, 503))
|
|
556
|
+
}
|
|
557
|
+
if (!joins.size) break
|
|
558
|
+
const n = fakeNet()
|
|
559
|
+
if (n) n.reconnects++
|
|
560
|
+
if (code === 4401) for (const j of joins.values()) j.status.value = 'unauthorized'
|
|
561
|
+
else statusAll('reconnecting')
|
|
562
|
+
if (!n) await sleep(code === HANDOVER ? 0 : backoff())
|
|
563
|
+
}
|
|
564
|
+
running = false
|
|
565
|
+
attempt = 0
|
|
566
|
+
listen(false)
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/** Full jitter, 0.5 s doubling to 30 s (research 30 §6). */
|
|
570
|
+
const backoff = () => Math.random() * Math.min(CAP, BASE * 2 ** attempt++)
|
|
571
|
+
|
|
572
|
+
// ---- the page -------------------------------------------------------------------------------------------------------
|
|
573
|
+
|
|
574
|
+
const wake = () => {
|
|
575
|
+
parked = false
|
|
576
|
+
nudge?.()
|
|
577
|
+
}
|
|
578
|
+
function park() {
|
|
579
|
+
parked = true
|
|
580
|
+
live?.close()
|
|
581
|
+
}
|
|
582
|
+
const parkHidden = () => {
|
|
583
|
+
if (keepOpen.stay?.()) hiddenAt = setTimeout(parkHidden, HIDDEN)
|
|
584
|
+
else park()
|
|
585
|
+
}
|
|
586
|
+
function onVisibility() {
|
|
587
|
+
clearTimeout(hiddenAt)
|
|
588
|
+
if (document.visibilityState === 'hidden') hiddenAt = setTimeout(parkHidden, HIDDEN)
|
|
589
|
+
else wake()
|
|
590
|
+
}
|
|
591
|
+
/** A sibling came back while this tab's socket was parked. */
|
|
592
|
+
export const wakeSocket = () => wake()
|
|
593
|
+
const onOffline = () => {
|
|
594
|
+
socketDown()
|
|
595
|
+
live?.close()
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
function listen(on: boolean) {
|
|
599
|
+
if (typeof window === 'undefined') return
|
|
600
|
+
const m = on ? 'addEventListener' : 'removeEventListener'
|
|
601
|
+
window[m]('online', wake)
|
|
602
|
+
window[m]('offline', onOffline)
|
|
603
|
+
window[m]('pagehide', park)
|
|
604
|
+
window[m]('pageshow', wake)
|
|
605
|
+
document[m]('visibilitychange', onVisibility)
|
|
606
|
+
if (!on) clearTimeout(hiddenAt)
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/** @weft-js/core/test, after each test. */
|
|
610
|
+
function reset() {
|
|
611
|
+
for (const j of joins.values()) clearTimeout(j.timer)
|
|
612
|
+
joins.clear()
|
|
613
|
+
byN.clear()
|
|
614
|
+
acks.clear()
|
|
615
|
+
live?.close()
|
|
616
|
+
nudge?.()
|
|
617
|
+
build = undefined
|
|
618
|
+
parked = false
|
|
619
|
+
attempt = 0
|
|
620
|
+
net()!.reconnects = 0
|
|
621
|
+
untrack(() => { rt.stale.value = false })
|
|
622
|
+
}
|
package/src/client.tsx
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// What an app's entry imports. `run.tsx` is the client: the router, the runtime and whatever hydrates. It's a chunk of its
|
|
2
|
+
// own so a page with only lazy islands can be read without it (`boot.ts`, PLAN §11); a page with work to do has it
|
|
3
|
+
// preloaded, and starts it at once.
|
|
4
|
+
|
|
5
|
+
import './gate.css'
|
|
6
|
+
import { boot } from './boot'
|
|
7
|
+
import type { ClientRoutes, start as run } from './run'
|
|
8
|
+
|
|
9
|
+
export type { ClientRoutes } from './run'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Hydrate the page in `#app` and take over its links. Resolves once hydrated, which on a page of lazy islands is when
|
|
13
|
+
* one is first needed. `eager`: don't wait for that.
|
|
14
|
+
*/
|
|
15
|
+
export function start(routes: ClientRoutes, options: { container?: Element; eager?: boolean } = {}): ReturnType<typeof run> {
|
|
16
|
+
return new Promise((settle, fail) => {
|
|
17
|
+
const full = () => import('./run').then(m => m.start(routes, options)).then(settle).catch(e => (fail(e), Promise.reject(e)))
|
|
18
|
+
options.eager ? void full() : boot(full)
|
|
19
|
+
})
|
|
20
|
+
}
|