@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
package/src/form.tsx
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
// `<Form of={mutation}>` (research 29 §5). The markup is a real `<form method="post">` with the mutation's id in a
|
|
2
|
+
// hidden field, so it works before and without JS: the server runs the mutation and answers 303, or the same page
|
|
3
|
+
// with the errors (server.tsx). Once hydrated, a submit calls the mutation in place, with `optimistic` and
|
|
4
|
+
// single-flight as in any call. Validation is the server's in both cases: client stubs have no schema.
|
|
5
|
+
//
|
|
6
|
+
// Reachable only from routes that import it, so an app without forms doesn't pay.
|
|
7
|
+
|
|
8
|
+
import { derived, flush, state, untrack, type Derived, type State } from '@kerf-js/core'
|
|
9
|
+
import {
|
|
10
|
+
currentScope,
|
|
11
|
+
decodeForm,
|
|
12
|
+
fieldsOf,
|
|
13
|
+
fnOf,
|
|
14
|
+
StartError,
|
|
15
|
+
SUBMIT,
|
|
16
|
+
type FnError,
|
|
17
|
+
type Mutation,
|
|
18
|
+
} from './data'
|
|
19
|
+
import type { FormFailure } from './exec'
|
|
20
|
+
import { fieldValue, type FieldKind, type FormValue } from './form/value'
|
|
21
|
+
|
|
22
|
+
export type { FormValue } from './form/value'
|
|
23
|
+
|
|
24
|
+
/** A mutation input's field names: `title`, and `a.b` one level down. */
|
|
25
|
+
type Field<I> = I extends object
|
|
26
|
+
? {
|
|
27
|
+
[K in keyof I & string]: NonNullable<I[K]> extends object
|
|
28
|
+
? K | `${K}.${keyof NonNullable<I[K]> & string}`
|
|
29
|
+
: K
|
|
30
|
+
}[keyof I & string]
|
|
31
|
+
: string
|
|
32
|
+
|
|
33
|
+
type FieldProps<V> = {
|
|
34
|
+
name: string
|
|
35
|
+
value?: V
|
|
36
|
+
'aria-invalid'?: true
|
|
37
|
+
'aria-describedby'?: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The state and fields `<Form>` gives its children. */
|
|
41
|
+
export interface FormApi<I = unknown, O = unknown> {
|
|
42
|
+
/** This form's submit is in flight (`mutation.pending` is every caller's). */
|
|
43
|
+
readonly pending: Derived<boolean>
|
|
44
|
+
/** Errors by field, from the schema or `invalid()`. */
|
|
45
|
+
readonly errors: Derived<Record<string, string>>
|
|
46
|
+
/** The failure that names no field. */
|
|
47
|
+
readonly message: Derived<string | undefined>
|
|
48
|
+
/** The last successful result. */
|
|
49
|
+
readonly result: Derived<O | undefined>
|
|
50
|
+
/** One field's error (its reader re-runs only when that field's error changes). */
|
|
51
|
+
error(name: Field<I>): string | undefined
|
|
52
|
+
/** What was typed, when a no-JS submit came back with errors. */
|
|
53
|
+
value(name: Field<I>): FormValue
|
|
54
|
+
/** Spread on the input: `name`, the value to restore, and the invalid state. */
|
|
55
|
+
field(name: Field<I>, kind: 'text' | 'email'): FieldProps<string>
|
|
56
|
+
field(name: Field<I>, kind: 'multiple'): FieldProps<string[]>
|
|
57
|
+
field(name: Field<I>, kind?: FieldKind): FieldProps<FormValue>
|
|
58
|
+
/** The field's error text, in a live region that's in the page before it has anything to say. */
|
|
59
|
+
Error(props: { name: Field<I>; class?: string }): unknown
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** `<Form>`: the mutation, and what to do with its outcome. */
|
|
63
|
+
export interface FormProps<I, O> {
|
|
64
|
+
of: Mutation<I, O>
|
|
65
|
+
/** Tells two forms for one mutation apart on the no-JS path. */
|
|
66
|
+
id?: string
|
|
67
|
+
/** Empty the fields after a success (default true). A no-JS submit always comes back to a fresh page. */
|
|
68
|
+
reset?: boolean
|
|
69
|
+
onSuccess?: (result: O) => void
|
|
70
|
+
onError?: (error: unknown) => void
|
|
71
|
+
children?: ((form: FormApi<I, O>) => unknown) | string | number | boolean | object | null
|
|
72
|
+
[attr: string]: unknown
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let page: FormFailure | null | undefined
|
|
76
|
+
|
|
77
|
+
/** The failure this form came back with: the request's on the server, `#kerf-form` in the hydrating page. */
|
|
78
|
+
function failure(id: string, key?: string): FormFailure | undefined {
|
|
79
|
+
const scope = currentScope()
|
|
80
|
+
let f: FormFailure | undefined | null = scope?.form
|
|
81
|
+
if (!scope && typeof document === 'object') {
|
|
82
|
+
if (page === undefined) {
|
|
83
|
+
const el = document.getElementById('kerf-form')
|
|
84
|
+
page = el && JSON.parse(el.textContent)
|
|
85
|
+
el?.remove()
|
|
86
|
+
}
|
|
87
|
+
f = page
|
|
88
|
+
}
|
|
89
|
+
if (f?.id !== id || f.key !== key) return undefined
|
|
90
|
+
if (!scope) page = null // once: a later visit to this page starts clean
|
|
91
|
+
return f
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const at = (values: Record<string, unknown> | undefined, name: string) =>
|
|
95
|
+
name
|
|
96
|
+
.split('.')
|
|
97
|
+
.reduce<unknown>((v, k) => (v as Record<string, unknown> | undefined)?.[k], values) as FormValue
|
|
98
|
+
|
|
99
|
+
function createSubmit<O>(
|
|
100
|
+
m: { [SUBMIT]: (input: unknown, form: true | FormData) => Promise<O> },
|
|
101
|
+
o: {
|
|
102
|
+
calls: State<number>
|
|
103
|
+
errors: State<Record<string, string>>
|
|
104
|
+
message: State<string | undefined>
|
|
105
|
+
result: State<O | undefined>
|
|
106
|
+
reset: () => boolean | undefined
|
|
107
|
+
onSuccess: () => ((result: O) => void) | undefined
|
|
108
|
+
onError: () => ((error: unknown) => void) | undefined
|
|
109
|
+
},
|
|
110
|
+
) {
|
|
111
|
+
const { calls, errors, message, result, reset, onSuccess, onError } = o
|
|
112
|
+
return async function submit(e: SubmitEvent) {
|
|
113
|
+
e.preventDefault()
|
|
114
|
+
if (calls.value) return
|
|
115
|
+
const el = e.target as HTMLFormElement
|
|
116
|
+
const data = new FormData(el, e.submitter)
|
|
117
|
+
const files = [...data.values()].some((v) => typeof v !== 'string' && v.size)
|
|
118
|
+
calls.value++
|
|
119
|
+
try {
|
|
120
|
+
const v = await m[SUBMIT](decodeForm(data), files ? data : true)
|
|
121
|
+
errors.value = {}
|
|
122
|
+
message.value = undefined
|
|
123
|
+
result.value = v
|
|
124
|
+
if (untrack(reset) !== false) el.reset()
|
|
125
|
+
untrack(onSuccess)?.(v)
|
|
126
|
+
} catch (err) {
|
|
127
|
+
const issues = (err as FnError).issues
|
|
128
|
+
const f = fieldsOf(issues)
|
|
129
|
+
errors.value = f.errors
|
|
130
|
+
message.value = issues ? f.message : (err as Error).message
|
|
131
|
+
flush()
|
|
132
|
+
el.querySelector<HTMLElement>('[aria-invalid="true"]')?.focus()
|
|
133
|
+
untrack(onError)?.(err)
|
|
134
|
+
} finally {
|
|
135
|
+
calls.value--
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function invalidMutation() {
|
|
141
|
+
return new StartError(
|
|
142
|
+
'F001',
|
|
143
|
+
'`<Form of>` needs a mutation',
|
|
144
|
+
'`<Form of={addTodo}>`, with `addTodo` a `mutation()` export of a server module',
|
|
145
|
+
)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** A form that posts to a mutation, and works before any code has loaded. */
|
|
149
|
+
export function Form<I, O>({
|
|
150
|
+
of,
|
|
151
|
+
id,
|
|
152
|
+
reset,
|
|
153
|
+
onSuccess,
|
|
154
|
+
onError,
|
|
155
|
+
children,
|
|
156
|
+
...rest
|
|
157
|
+
}: FormProps<I, O>) {
|
|
158
|
+
const m = untrack(() => of) as Mutation<I, O> & {
|
|
159
|
+
[SUBMIT]: (input: unknown, form: true | FormData) => Promise<O>
|
|
160
|
+
}
|
|
161
|
+
const key = untrack(() => id)
|
|
162
|
+
const fnId = fnOf(m)?.$id
|
|
163
|
+
if (fnOf(m)?.kind !== 'mutation') throw invalidMutation()
|
|
164
|
+
const seed = failure(fnId, key)
|
|
165
|
+
const errors = state<Record<string, string>>(seed?.errors ?? {})
|
|
166
|
+
const message = state(seed?.message)
|
|
167
|
+
const result = state<O | undefined>(undefined)
|
|
168
|
+
const calls = state(0)
|
|
169
|
+
const errorId = (name: string) => `${key ?? fnId}-${name}-error`
|
|
170
|
+
|
|
171
|
+
function field(name: Field<I>, kind: 'text' | 'email'): FieldProps<string>
|
|
172
|
+
function field(name: Field<I>, kind: 'multiple'): FieldProps<string[]>
|
|
173
|
+
function field(name: Field<I>, kind?: FieldKind): FieldProps<FormValue>
|
|
174
|
+
function field(name: Field<I>, kind?: FieldKind): FieldProps<FormValue> {
|
|
175
|
+
const value = fieldValue(at(seed?.values, name), kind)
|
|
176
|
+
const invalid = !!errors.get(name)
|
|
177
|
+
return {
|
|
178
|
+
name,
|
|
179
|
+
...(value !== undefined && { value }),
|
|
180
|
+
...(invalid && { 'aria-invalid': true as const, 'aria-describedby': errorId(name) }),
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const form: FormApi<I, O> = {
|
|
185
|
+
pending: derived(() => calls.value > 0),
|
|
186
|
+
errors,
|
|
187
|
+
message,
|
|
188
|
+
result,
|
|
189
|
+
error: (name) => errors.get(name),
|
|
190
|
+
value: (name) => at(seed?.values, name),
|
|
191
|
+
field,
|
|
192
|
+
Error: (props) => (
|
|
193
|
+
<p id={errorId(props.name)} class={props.class} role="alert">
|
|
194
|
+
{errors.get(props.name)}
|
|
195
|
+
</p>
|
|
196
|
+
),
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const submit = createSubmit(m, {
|
|
200
|
+
calls,
|
|
201
|
+
errors,
|
|
202
|
+
message,
|
|
203
|
+
result,
|
|
204
|
+
reset: () => reset,
|
|
205
|
+
onSuccess: () => onSuccess,
|
|
206
|
+
onError: () => onError,
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
return (
|
|
210
|
+
<form
|
|
211
|
+
method="post"
|
|
212
|
+
{...rest}
|
|
213
|
+
aria-busy={form.pending.value || undefined}
|
|
214
|
+
onSubmit={(e) => void submit(e)}
|
|
215
|
+
>
|
|
216
|
+
<input type="hidden" name="$fn" value={fnId} />
|
|
217
|
+
{key !== undefined && <input type="hidden" name="$form" value={key} />}
|
|
218
|
+
{typeof children === 'function'
|
|
219
|
+
? (children as (api: FormApi<I, O>) => unknown)(form)
|
|
220
|
+
: children}
|
|
221
|
+
</form>
|
|
222
|
+
)
|
|
223
|
+
}
|
package/src/gate.css
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/* The late half of the style gate: `firstPaint` hides `data-kerf="styled"` in the page, and this shows it. */
|
|
2
|
+
[data-kerf="styled"] { visibility: visible; animation: none; }
|
|
3
|
+
@media (prefers-reduced-motion: no-preference) {
|
|
4
|
+
[data-kerf="styled"] { animation: kerf-reveal 160ms ease-out; }
|
|
5
|
+
}
|
|
6
|
+
@keyframes kerf-reveal { from { opacity: 0; } }
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { Bus } from '../bus'
|
|
2
|
+
import type { Channel, Ev, Limits } from '../hub'
|
|
3
|
+
|
|
4
|
+
const VOLATILE = '{"v":1'
|
|
5
|
+
const unref = <T>(t: T) => ((t as { unref?(): void } | null | undefined)?.unref?.(), t)
|
|
6
|
+
|
|
7
|
+
interface Rings {
|
|
8
|
+
bus: Bus
|
|
9
|
+
limits: Limits
|
|
10
|
+
epoch: string
|
|
11
|
+
channels: Map<string, Channel>
|
|
12
|
+
seq: number
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function trim(state: Rings, ch: Channel) {
|
|
16
|
+
const old = Date.now() - state.limits.window
|
|
17
|
+
let drop = 0
|
|
18
|
+
while (
|
|
19
|
+
drop < ch.ring.length &&
|
|
20
|
+
(ch.ring[drop]!.at < old || ch.ring.length - drop > state.limits.ring)
|
|
21
|
+
)
|
|
22
|
+
drop++
|
|
23
|
+
if (!drop) return
|
|
24
|
+
ch.floor = ch.ring[drop - 1]!.seq
|
|
25
|
+
ch.ring.splice(0, drop)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function channel(state: Rings, name: string): Channel {
|
|
29
|
+
const { channels, bus } = state
|
|
30
|
+
const ch = channels.get(name)
|
|
31
|
+
if (ch) {
|
|
32
|
+
clearTimeout(ch.idle)
|
|
33
|
+
ch.idle = undefined
|
|
34
|
+
return ch
|
|
35
|
+
}
|
|
36
|
+
const made: Channel = {
|
|
37
|
+
name,
|
|
38
|
+
ring: [],
|
|
39
|
+
floor: state.seq,
|
|
40
|
+
listeners: new Set(),
|
|
41
|
+
got: undefined as never,
|
|
42
|
+
ready: undefined as never,
|
|
43
|
+
}
|
|
44
|
+
channels.set(name, made)
|
|
45
|
+
made.got = (raw) => {
|
|
46
|
+
const ev = { seq: ++state.seq, at: Date.now(), raw }
|
|
47
|
+
// Cursors and presence beats are only worth having now: in the ring they'd push out what a resume needs.
|
|
48
|
+
if (!raw.startsWith(VOLATILE)) {
|
|
49
|
+
made.ring.push(ev)
|
|
50
|
+
trim(state, made)
|
|
51
|
+
}
|
|
52
|
+
for (const on of made.listeners) on(ev)
|
|
53
|
+
}
|
|
54
|
+
made.ready = bus
|
|
55
|
+
.subscribe(name, made.got, () => {
|
|
56
|
+
made.ring.length = 0
|
|
57
|
+
made.floor = state.seq
|
|
58
|
+
for (const on of made.listeners) on(null)
|
|
59
|
+
})
|
|
60
|
+
.then(
|
|
61
|
+
(stop) => {
|
|
62
|
+
made.stop = stop
|
|
63
|
+
made.floor = state.seq
|
|
64
|
+
},
|
|
65
|
+
(e) => {
|
|
66
|
+
channels.delete(name)
|
|
67
|
+
throw e
|
|
68
|
+
},
|
|
69
|
+
)
|
|
70
|
+
made.ready.catch(() => {})
|
|
71
|
+
return made
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function unlisten(state: Rings, ch: Channel, on: (ev: Ev | null) => void) {
|
|
75
|
+
ch.listeners.delete(on)
|
|
76
|
+
if (ch.listeners.size || ch.idle) return
|
|
77
|
+
ch.idle = unref(
|
|
78
|
+
setTimeout(() => {
|
|
79
|
+
ch.stop?.()
|
|
80
|
+
state.channels.delete(ch.name)
|
|
81
|
+
}, state.limits.window),
|
|
82
|
+
)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function replay(state: Rings, ch: Channel, after: string): Ev[] | undefined {
|
|
86
|
+
const [e, n] = after.split(':')
|
|
87
|
+
const at = Number(n)
|
|
88
|
+
trim(state, ch)
|
|
89
|
+
if (e !== state.epoch || !(at >= ch.floor)) return undefined
|
|
90
|
+
return ch.ring.filter((ev) => ev.seq > at)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function createChannels(bus: Bus, limits: Limits, epoch: string) {
|
|
94
|
+
const state: Rings = { bus, limits, epoch, channels: new Map(), seq: 0 }
|
|
95
|
+
return {
|
|
96
|
+
channels: state.channels,
|
|
97
|
+
seq: () => state.seq,
|
|
98
|
+
channel: (name: string) => channel(state, name),
|
|
99
|
+
unlisten: (ch: Channel, on: (ev: Ev | null) => void) => unlisten(state, ch, on),
|
|
100
|
+
replay: (ch: Channel, after: string) => replay(state, ch, after),
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { StartError, type Context } from '../data'
|
|
2
|
+
import { createScope, guard, signedIn, type Scope, type ScopeOptions } from '../exec'
|
|
3
|
+
import type { Source } from '../changes'
|
|
4
|
+
import type { Body, Channel, Conn, HubOptions, Limits, Sink, Sub } from './types'
|
|
5
|
+
|
|
6
|
+
const unref = <T>(t: T) => ((t as { unref?(): void } | null | undefined)?.unref?.(), t)
|
|
7
|
+
interface Dependencies {
|
|
8
|
+
conns: Map<string, Conn>
|
|
9
|
+
limits: Limits
|
|
10
|
+
epoch: string
|
|
11
|
+
options: HubOptions
|
|
12
|
+
source: Source | undefined
|
|
13
|
+
pokes: () => Channel
|
|
14
|
+
want: () => Promise<unknown> | undefined
|
|
15
|
+
rest: () => void
|
|
16
|
+
cursorOf: (n: number) => string
|
|
17
|
+
seq: () => number
|
|
18
|
+
wire: () => { m: string; d?: number }
|
|
19
|
+
change: (c: Conn, request: Request, opts: ScopeOptions, body: Body) => Promise<void>
|
|
20
|
+
replayPokes: (c: Conn, after: string) => void
|
|
21
|
+
fail: (c: Conn, s: Sub, e: unknown) => void
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function bye(deps: Dependencies, c: Conn, retry: number) {
|
|
25
|
+
c.sink.write('b', JSON.stringify({ retry }))
|
|
26
|
+
close(deps, c)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function close(deps: Dependencies, c: Conn) {
|
|
30
|
+
const { conns, rest } = deps
|
|
31
|
+
if (!conns.delete(c.k)) return
|
|
32
|
+
for (const t of c.timers) clearTimeout(t)
|
|
33
|
+
for (const s of c.subs.values()) s.ac.abort()
|
|
34
|
+
c.subs.clear()
|
|
35
|
+
c.sink.close()
|
|
36
|
+
rest()
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function recheck(deps: Dependencies, c: Conn) {
|
|
40
|
+
const { conns, fail } = deps
|
|
41
|
+
const authed = [...c.subs.values()].filter((s) => signedIn(s.fn))
|
|
42
|
+
if (!authed.length) return
|
|
43
|
+
const ctx = await createScope(authed[0]!.request, authed[0]!.options)
|
|
44
|
+
.ctx()
|
|
45
|
+
.catch(() => ({}) as Context)
|
|
46
|
+
if (!conns.has(c.k)) return
|
|
47
|
+
for (const s of authed) {
|
|
48
|
+
try {
|
|
49
|
+
await guard(s.fn, ctx)
|
|
50
|
+
} catch (e) {
|
|
51
|
+
fail(c, s, e)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function connect(
|
|
57
|
+
deps: Dependencies,
|
|
58
|
+
a: { request: Request; scope: Scope; body: Body; sink: Sink; line: boolean },
|
|
59
|
+
) {
|
|
60
|
+
const { request, scope, body, sink, line } = a
|
|
61
|
+
const {
|
|
62
|
+
conns,
|
|
63
|
+
limits,
|
|
64
|
+
epoch,
|
|
65
|
+
options,
|
|
66
|
+
source,
|
|
67
|
+
pokes,
|
|
68
|
+
want,
|
|
69
|
+
cursorOf,
|
|
70
|
+
seq,
|
|
71
|
+
wire,
|
|
72
|
+
change,
|
|
73
|
+
replayPokes,
|
|
74
|
+
} = deps
|
|
75
|
+
const opts = scope.options
|
|
76
|
+
const ctx = await scope.ctx()
|
|
77
|
+
const user = (ctx as { user?: { id?: string } }).user?.id
|
|
78
|
+
if (
|
|
79
|
+
!line &&
|
|
80
|
+
user !== undefined &&
|
|
81
|
+
[...conns.values()].filter((c) => c.user === user).length >= limits.connections
|
|
82
|
+
) {
|
|
83
|
+
throw new StartError(
|
|
84
|
+
'R009',
|
|
85
|
+
`user \`${user}\` already has ${limits.connections} realtime connections`,
|
|
86
|
+
'close tabs, or raise `limits.connections`',
|
|
87
|
+
{ status: 429 },
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
const c: Conn = {
|
|
91
|
+
k: crypto.randomUUID(),
|
|
92
|
+
sink,
|
|
93
|
+
user,
|
|
94
|
+
subs: new Map(),
|
|
95
|
+
tags: new Map(),
|
|
96
|
+
held: new Set(),
|
|
97
|
+
bytes: 0,
|
|
98
|
+
timers: [],
|
|
99
|
+
}
|
|
100
|
+
conns.set(c.k, c)
|
|
101
|
+
await pokes().ready.catch((e) => {
|
|
102
|
+
conns.delete(c.k)
|
|
103
|
+
throw e
|
|
104
|
+
})
|
|
105
|
+
await want() // before `h`: a client that reads next must not miss a change
|
|
106
|
+
sink.write(
|
|
107
|
+
'h',
|
|
108
|
+
JSON.stringify({
|
|
109
|
+
k: c.k,
|
|
110
|
+
e: epoch,
|
|
111
|
+
b: options.build ?? '',
|
|
112
|
+
c: cursorOf(seq()),
|
|
113
|
+
...(source && { l: wire() }),
|
|
114
|
+
}),
|
|
115
|
+
)
|
|
116
|
+
await change(c, request, opts, body)
|
|
117
|
+
if (body.c) replayPokes(c, body.c)
|
|
118
|
+
startTimers(deps, c, sink)
|
|
119
|
+
return c
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function startTimers(deps: Dependencies, c: Conn, sink: Sink) {
|
|
123
|
+
const { limits, options } = deps
|
|
124
|
+
let ticks = 0
|
|
125
|
+
const every = Math.max(1, Math.round(limits.session / limits.heartbeat))
|
|
126
|
+
c.timers.push(
|
|
127
|
+
unref(
|
|
128
|
+
setInterval(() => {
|
|
129
|
+
sink.ping?.()
|
|
130
|
+
if (++ticks % every === 0) void recheck(deps, c)
|
|
131
|
+
}, limits.heartbeat),
|
|
132
|
+
),
|
|
133
|
+
)
|
|
134
|
+
if (options.maxDuration)
|
|
135
|
+
c.timers.push(
|
|
136
|
+
unref(setTimeout(() => bye(deps, c, 0), Math.max(1, options.maxDuration - 10) * 1000)),
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function createConnections(deps: Dependencies) {
|
|
141
|
+
return {
|
|
142
|
+
bye: (c: Conn, retry: number) => bye(deps, c, retry),
|
|
143
|
+
close: (c: Conn) => close(deps, c),
|
|
144
|
+
connect: (request: Request, scope: Scope, body: Body, sink: Sink, line = false) =>
|
|
145
|
+
connect(deps, { request, scope, body, sink, line }),
|
|
146
|
+
}
|
|
147
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { Conn, Limits } from '../hub'
|
|
2
|
+
|
|
3
|
+
type Sub = Conn['subs'] extends Map<number, infer S> ? S : never
|
|
4
|
+
|
|
5
|
+
const HIGH = 64 * 1024
|
|
6
|
+
|
|
7
|
+
export function createDelivery(
|
|
8
|
+
limits: Limits,
|
|
9
|
+
cursorOf: (n: number) => string,
|
|
10
|
+
bye: (c: Conn, retry: number) => void,
|
|
11
|
+
) {
|
|
12
|
+
function poke(c: Conn, tags: string[], at: number) {
|
|
13
|
+
if (!tags.length) return
|
|
14
|
+
if (!c.held.size && c.sink.backlog() < HIGH)
|
|
15
|
+
return c.sink.write('p', JSON.stringify({ t: tags, c: cursorOf(at) }))
|
|
16
|
+
const p = (c.pokes ??= { t: new Set(), c: '' })
|
|
17
|
+
for (const t of tags) p.t.add(t)
|
|
18
|
+
p.c = cursorOf(at)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Over `queue`: the client gets a bye, reconnects and resumes, rather than this process growing. */
|
|
22
|
+
function hold(c: Conn, s: Sub, raw: string): boolean {
|
|
23
|
+
const size = raw.length
|
|
24
|
+
if (!s.buffer) {
|
|
25
|
+
for (const x of s.q) c.bytes -= x.size
|
|
26
|
+
s.q.length = 0
|
|
27
|
+
} else if (s.q.length >= s.buffer) {
|
|
28
|
+
for (const x of s.q) c.bytes -= x.size
|
|
29
|
+
s.q.length = 0
|
|
30
|
+
return false
|
|
31
|
+
}
|
|
32
|
+
s.q.push({ raw, size })
|
|
33
|
+
c.bytes += size
|
|
34
|
+
c.held.add(s)
|
|
35
|
+
if (c.bytes > limits.queue) bye(c, 0)
|
|
36
|
+
return true
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Written to the client, or held. False: the buffer overflowed, so this stream resets. */
|
|
40
|
+
function deliver(c: Conn, s: Sub, raw: string): boolean {
|
|
41
|
+
if (!c.held.size && c.sink.backlog() < HIGH) return (c.sink.write('m', raw), true)
|
|
42
|
+
return hold(c, s, raw)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function drain(c: Conn) {
|
|
46
|
+
if (c.pokes && c.sink.backlog() < HIGH) {
|
|
47
|
+
c.sink.write('p', JSON.stringify({ t: [...c.pokes.t], c: c.pokes.c }))
|
|
48
|
+
c.pokes = undefined
|
|
49
|
+
}
|
|
50
|
+
for (const s of c.held) {
|
|
51
|
+
while (s.q.length && c.sink.backlog() < HIGH) {
|
|
52
|
+
const x = s.q.shift()!
|
|
53
|
+
c.bytes -= x.size
|
|
54
|
+
c.sink.write('m', x.raw)
|
|
55
|
+
}
|
|
56
|
+
if (s.q.length) return
|
|
57
|
+
c.held.delete(s)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return { poke, deliver, drain }
|
|
62
|
+
}
|