@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,164 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process'
|
|
2
|
+
import { join, relative, resolve } from 'node:path'
|
|
3
|
+
|
|
4
|
+
const WASM = 'wasm32-unknown-unknown'
|
|
5
|
+
|
|
6
|
+
// ---- cargo --------------------------------------------------------------------------------------------------------
|
|
7
|
+
|
|
8
|
+
function run(cmd, args, { cwd, env, onLine }) {
|
|
9
|
+
return new Promise((ok) => {
|
|
10
|
+
let child
|
|
11
|
+
try {
|
|
12
|
+
child = spawn(cmd, args, {
|
|
13
|
+
cwd,
|
|
14
|
+
env: { ...process.env, ...env },
|
|
15
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
16
|
+
})
|
|
17
|
+
} catch (e) {
|
|
18
|
+
ok({ code: 127, stdout: '', stderr: e.message, missing: true })
|
|
19
|
+
return
|
|
20
|
+
}
|
|
21
|
+
let stdout = '',
|
|
22
|
+
stderr = ''
|
|
23
|
+
child.stdout.on('data', (d) => {
|
|
24
|
+
stdout += d
|
|
25
|
+
})
|
|
26
|
+
child.stderr.on('data', (d) => {
|
|
27
|
+
stderr += d
|
|
28
|
+
onLine?.(`${d}`)
|
|
29
|
+
})
|
|
30
|
+
child.on('error', (e) =>
|
|
31
|
+
ok({ code: 127, stdout, stderr: e.message, missing: e.code === 'ENOENT' }),
|
|
32
|
+
)
|
|
33
|
+
child.on('close', (code) => ok({ code, stdout, stderr }))
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const pascal = (name) => name[0].toUpperCase() + name.slice(1)
|
|
38
|
+
const snake = (name) => name.replace(/[A-Z]/g, (c, i) => `${i ? '_' : ''}${c.toLowerCase()}`)
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* cargo's JSON messages as diagnostics. An error that names a contract function's types or method is R103, with the
|
|
42
|
+
* function's id, so it points at the TS edit that caused it (research 06 §7); any other is R109.
|
|
43
|
+
*/
|
|
44
|
+
export function readCargo(stdout, { contract, service, root, dir }) {
|
|
45
|
+
const fns = (contract?.functions ?? []).filter((f) => f.impl?.service === service)
|
|
46
|
+
const names = fns.map((f) => ({
|
|
47
|
+
id: f.id,
|
|
48
|
+
re: new RegExp(`\\b(${pascal(f.name)}\\w*|${snake(f.name)})\\b`),
|
|
49
|
+
}))
|
|
50
|
+
const diagnostics = []
|
|
51
|
+
let exe,
|
|
52
|
+
lib,
|
|
53
|
+
ok = false
|
|
54
|
+
for (const line of stdout.split('\n')) {
|
|
55
|
+
if (!line.startsWith('{')) continue
|
|
56
|
+
let m
|
|
57
|
+
try {
|
|
58
|
+
m = JSON.parse(line)
|
|
59
|
+
} catch {
|
|
60
|
+
continue
|
|
61
|
+
}
|
|
62
|
+
if (m.reason === 'compiler-artifact' && m.executable && m.target?.kind?.includes('bin'))
|
|
63
|
+
exe = m.executable
|
|
64
|
+
if (m.reason === 'compiler-artifact' && m.target?.kind?.includes('cdylib'))
|
|
65
|
+
lib = m.filenames?.find((f) => /\.(dylib|so|dll|wasm)$/.test(f))
|
|
66
|
+
if (m.reason === 'build-finished') ok = m.success
|
|
67
|
+
if (m.reason !== 'compiler-message' || m.message?.level !== 'error' || !m.message.code) continue
|
|
68
|
+
const text = m.message.rendered ?? m.message.message
|
|
69
|
+
const hit = names.find((n) => n.re.test(text))
|
|
70
|
+
const at = m.message.spans?.find((s) => s.is_primary) ?? m.message.spans?.[0]
|
|
71
|
+
const file = at && relative(root, resolve(dir, at.file_name)).replaceAll('\\', '/')
|
|
72
|
+
diagnostics.push({
|
|
73
|
+
code: hit ? 'R103' : 'R109',
|
|
74
|
+
severity: 'error',
|
|
75
|
+
...(hit && { function: hit.id }),
|
|
76
|
+
file: file ?? '.kerf/contract.json',
|
|
77
|
+
span: at ? [at.line_start, at.column_start, at.line_end, at.column_end] : [1, 1, 1, 1],
|
|
78
|
+
why: hit
|
|
79
|
+
? `\`${hit.id}\`: the Rust no longer matches the contract: ${m.message.message}`
|
|
80
|
+
: `\`${service}\` doesn't compile: ${m.message.message}`,
|
|
81
|
+
rendered: text,
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
return { ok, exe, lib, diagnostics }
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** `verb`: `build` or `check`. Failure names what to fix: a build script's panic is already R103 with the fix. */
|
|
88
|
+
export async function cargo(verb, { root, dir, service, contract, release, target, onLine }) {
|
|
89
|
+
const args = [
|
|
90
|
+
verb,
|
|
91
|
+
'--message-format=json',
|
|
92
|
+
...(release ? ['--release'] : []),
|
|
93
|
+
...(target ? ['--target', target] : []),
|
|
94
|
+
]
|
|
95
|
+
const env = {
|
|
96
|
+
KERF_CONTRACT: join(root, '.kerf', 'contract.json'),
|
|
97
|
+
...(target === WASM && {
|
|
98
|
+
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUSTFLAGS: '-C target-feature=+simd128',
|
|
99
|
+
}),
|
|
100
|
+
}
|
|
101
|
+
const r = await run('cargo', args, { cwd: dir, env, onLine })
|
|
102
|
+
if (r.missing)
|
|
103
|
+
return {
|
|
104
|
+
ok: false,
|
|
105
|
+
diagnostics: [
|
|
106
|
+
{
|
|
107
|
+
code: 'R109',
|
|
108
|
+
severity: 'error',
|
|
109
|
+
file: 'package.json',
|
|
110
|
+
span: [1, 1, 1, 1],
|
|
111
|
+
why: `\`${service}\` is Rust you opted into, so it needs \`cargo\`, and it isn't on PATH. Install it from rustup.rs, or set \`external: true\` for a sidecar built elsewhere. The rest of kerf doesn't need Rust.`,
|
|
112
|
+
},
|
|
113
|
+
],
|
|
114
|
+
}
|
|
115
|
+
const read = readCargo(r.stdout, { contract, service, root, dir })
|
|
116
|
+
if (r.code === 0) return read
|
|
117
|
+
if (!read.diagnostics.length) {
|
|
118
|
+
const panic = /R103: ([^\n]+)/.exec(r.stderr)?.[1]
|
|
119
|
+
read.diagnostics.push({
|
|
120
|
+
code: panic ? 'R103' : 'R109',
|
|
121
|
+
severity: 'error',
|
|
122
|
+
file: relative(root, dir).replaceAll('\\', '/') + '/Cargo.toml',
|
|
123
|
+
span: [1, 1, 1, 1],
|
|
124
|
+
why:
|
|
125
|
+
panic ??
|
|
126
|
+
`cargo ${verb} failed in \`${service}\`:\n${r.stderr.trim().split('\n').slice(-12).join('\n')}`,
|
|
127
|
+
})
|
|
128
|
+
}
|
|
129
|
+
return { ...read, ok: false }
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** R104 for a `.wasm` that won't build: a warning unless the build needs it. `error`: the preset has no other way to run. */
|
|
133
|
+
export async function checkWasm({ root, dir, service, contract, preset, rust, build }) {
|
|
134
|
+
if (rust?.services?.[service]?.wasm === false) return { diagnostics: [] }
|
|
135
|
+
const r = await cargo(build ? 'build' : 'check', {
|
|
136
|
+
root,
|
|
137
|
+
dir,
|
|
138
|
+
service,
|
|
139
|
+
contract,
|
|
140
|
+
target: WASM,
|
|
141
|
+
release: build,
|
|
142
|
+
})
|
|
143
|
+
if (r.ok) return { lib: r.lib, diagnostics: [] }
|
|
144
|
+
const missing = r.diagnostics.find((d) =>
|
|
145
|
+
/can't find crate for `(std|core)`|target may not be installed/.test(
|
|
146
|
+
`${d.why}${d.rendered ?? ''}`,
|
|
147
|
+
),
|
|
148
|
+
)
|
|
149
|
+
const why = missing
|
|
150
|
+
? `\`rustup target add ${WASM}\`: this toolchain can't build \`${service}\`'s WASM fallback`
|
|
151
|
+
: `\`${service}\` has no WASM fallback: ${r.diagnostics[0]?.why ?? 'the build failed'}`
|
|
152
|
+
const needed = preset && !/^(node|bun|deno[-_]server)/.test(preset)
|
|
153
|
+
return {
|
|
154
|
+
diagnostics: [
|
|
155
|
+
{
|
|
156
|
+
code: 'R104',
|
|
157
|
+
severity: needed ? 'error' : 'warning',
|
|
158
|
+
file: relative(root, dir).replaceAll('\\', '/') + '/Cargo.toml',
|
|
159
|
+
span: [1, 1, 1, 1],
|
|
160
|
+
why: `${why}. An addon loads on this machine, and nothing else can run it. Fix it, or set \`rust.services.${service}.wasm: false\` to say so`,
|
|
161
|
+
},
|
|
162
|
+
],
|
|
163
|
+
}
|
|
164
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { resolve } from 'node:path'
|
|
2
|
+
|
|
3
|
+
/** Same spelling as `urlVariable` in rust.ts, which can't share a module with this one: it ships in the server bundle. */
|
|
4
|
+
export const urlVariable = (service) =>
|
|
5
|
+
`KERF_RUST_${service.toUpperCase().replaceAll('-', '_')}_URL`
|
|
6
|
+
|
|
7
|
+
const external = (rust, service) => rust?.services?.[service]?.external === true
|
|
8
|
+
/** The crate folder of a service. */
|
|
9
|
+
export const crateDir = (root, rust, service) =>
|
|
10
|
+
resolve(root, rust?.services?.[service]?.crate ?? `crates/${service}`)
|
|
11
|
+
|
|
12
|
+
/** The services in a contract that have a crate here. */
|
|
13
|
+
export const buildable = (contract, rust) =>
|
|
14
|
+
Object.keys(contract.services ?? {}).filter((s) => !external(rust, s))
|
|
15
|
+
export const isNative = (contract, service) => contract.services?.[service]?.kind === 'native'
|
|
16
|
+
|
|
17
|
+
export const stem = (service) => service.replaceAll('-', '_')
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { join } from 'node:path'
|
|
2
|
+
import { cargo } from './cargo.mjs'
|
|
3
|
+
import { crateDir, isNative, stem } from './common.mjs'
|
|
4
|
+
import { launch, stop } from './process.mjs'
|
|
5
|
+
import { copyFileSync, renameSync } from 'node:fs'
|
|
6
|
+
|
|
7
|
+
/** Copy, then rename: macOS kills a process that loads a signed binary overwritten in place. */
|
|
8
|
+
function place(from, to) {
|
|
9
|
+
copyFileSync(from, `${to}.tmp`)
|
|
10
|
+
renameSync(`${to}.tmp`, to)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function devBuilder(deps) {
|
|
14
|
+
const { log, states, closed } = deps
|
|
15
|
+
async function rebuild(service) {
|
|
16
|
+
const s = states.get(service)
|
|
17
|
+
if (s.building) return void (s.again = true)
|
|
18
|
+
s.building = true
|
|
19
|
+
try {
|
|
20
|
+
do {
|
|
21
|
+
s.again = false
|
|
22
|
+
await once(service, s, deps, exited)
|
|
23
|
+
} while (s.again && !closed())
|
|
24
|
+
} finally {
|
|
25
|
+
s.building = false
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function exited(service, s, child, code, signal) {
|
|
30
|
+
if (closed() || !child || child.stopped || s.current !== child) return
|
|
31
|
+
s.current = undefined
|
|
32
|
+
const wait = Math.min(100 * 2 ** (s.crashes = (s.crashes ?? 0) + 1), 5_000)
|
|
33
|
+
log(`[kerf] ${service} exited (${signal ?? code}): restarting in ${wait} ms`)
|
|
34
|
+
setTimeout(() => {
|
|
35
|
+
if (closed() || s.current || s.building) return
|
|
36
|
+
void rebuild(service) // cargo finds nothing to do, and the binary starts again
|
|
37
|
+
}, wait).unref()
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return rebuild
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function once(service, s, deps, exited) {
|
|
44
|
+
const { root, rust, secret, log, say, overlay, nativeDir, publish, contract } = deps
|
|
45
|
+
const hash = contract().services?.[service]?.hash
|
|
46
|
+
if (!hash) return
|
|
47
|
+
const dir = crateDir(root, rust, service)
|
|
48
|
+
const t = performance.now()
|
|
49
|
+
const built = await cargo('build', { root, dir, service, contract: contract() })
|
|
50
|
+
if (!built.ok || !(built.exe || built.lib)) {
|
|
51
|
+
built.diagnostics.forEach(say)
|
|
52
|
+
s.failure = built.diagnostics.map((d) => d.why).join('\n') || `\`${service}\` didn't build`
|
|
53
|
+
overlay(s.failure)
|
|
54
|
+
if (isNative(contract(), service) && !s.loaded) publish()
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
log(`[kerf] ${service}: built in ${Math.round(performance.now() - t)} ms`)
|
|
58
|
+
if (isNative(contract(), service)) {
|
|
59
|
+
s.generation = (s.generation ?? 0) + 1
|
|
60
|
+
s.loaded = { hash, addon: `${stem(service)}-${s.generation}.node` }
|
|
61
|
+
s.failure = undefined
|
|
62
|
+
place(built.lib, join(nativeDir, s.loaded.addon))
|
|
63
|
+
return publish()
|
|
64
|
+
}
|
|
65
|
+
let next
|
|
66
|
+
try {
|
|
67
|
+
next = await launch({
|
|
68
|
+
exe: built.exe,
|
|
69
|
+
service,
|
|
70
|
+
hash,
|
|
71
|
+
listen: '127.0.0.1:0',
|
|
72
|
+
secret,
|
|
73
|
+
log,
|
|
74
|
+
onExit: (code, signal) => exited(service, s, next, code, signal),
|
|
75
|
+
})
|
|
76
|
+
} catch (e) {
|
|
77
|
+
s.failure = e.message
|
|
78
|
+
log(`[kerf] ${e.message}`)
|
|
79
|
+
return overlay(e.message)
|
|
80
|
+
}
|
|
81
|
+
// The swap: new calls go to `next` from here; the old child finishes what it has, then goes.
|
|
82
|
+
const old = s.current
|
|
83
|
+
s.current = next
|
|
84
|
+
s.failure = undefined
|
|
85
|
+
s.exe = built.exe
|
|
86
|
+
s.crashes = 0
|
|
87
|
+
if (old) {
|
|
88
|
+
old.retired = true
|
|
89
|
+
if (old.inflight === 0) stop(old)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { existsSync, watch } from 'node:fs'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
import { isServerModule } from '../vite.mjs'
|
|
4
|
+
import { buildable, crateDir, isNative, urlVariable } from './common.mjs'
|
|
5
|
+
|
|
6
|
+
export async function watchServices(deps) {
|
|
7
|
+
const {
|
|
8
|
+
root,
|
|
9
|
+
rust,
|
|
10
|
+
contract,
|
|
11
|
+
setContract,
|
|
12
|
+
first,
|
|
13
|
+
states,
|
|
14
|
+
proxy,
|
|
15
|
+
publish,
|
|
16
|
+
rebuild,
|
|
17
|
+
refresh,
|
|
18
|
+
overlay,
|
|
19
|
+
log,
|
|
20
|
+
} = deps
|
|
21
|
+
const watchers = []
|
|
22
|
+
|
|
23
|
+
for (const service of buildable(contract(), rust)) {
|
|
24
|
+
const dir = crateDir(root, rust, service)
|
|
25
|
+
const s = {
|
|
26
|
+
current: undefined,
|
|
27
|
+
failure: existsSync(dir) ? undefined : `\`crates/${service}\` doesn't exist`,
|
|
28
|
+
building: false,
|
|
29
|
+
}
|
|
30
|
+
states.set(service, s)
|
|
31
|
+
if (!isNative(contract(), service)) {
|
|
32
|
+
const p = await proxy(s)
|
|
33
|
+
s.proxy = p
|
|
34
|
+
process.env[urlVariable(service)] = `http://127.0.0.1:${p.port}`
|
|
35
|
+
}
|
|
36
|
+
if (!existsSync(dir)) {
|
|
37
|
+
log(
|
|
38
|
+
`[kerf] R109 error: ${s.failure}. Point \`rust.services.${service}.crate\` at it, or set \`external: true\``,
|
|
39
|
+
)
|
|
40
|
+
if (isNative(contract(), service)) publish()
|
|
41
|
+
continue
|
|
42
|
+
}
|
|
43
|
+
if (!first.blocked) await rebuild(service)
|
|
44
|
+
const changed = debounce(() => rebuild(service))
|
|
45
|
+
watchers.push(
|
|
46
|
+
watch(dir, { recursive: true }, (_, file) => {
|
|
47
|
+
if (file && !/^target([\\/]|$)/.test(file) && !file.endsWith('.lock')) changed()
|
|
48
|
+
}),
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// A server module's edit may change a schema: the contract is regenerated, and only a change to a service's hash
|
|
53
|
+
// rebuilds it. cargo then stops where the Rust no longer fits.
|
|
54
|
+
const src = join(root, 'src')
|
|
55
|
+
const contractChanged = debounce(async () => {
|
|
56
|
+
try {
|
|
57
|
+
const fresh = await refresh()
|
|
58
|
+
if (fresh.blocked) return overlay(fresh.blocked.why)
|
|
59
|
+
const before = contract()
|
|
60
|
+
setContract(fresh.contract)
|
|
61
|
+
for (const service of buildable(contract(), rust)) {
|
|
62
|
+
if (
|
|
63
|
+
states.has(service) &&
|
|
64
|
+
before.services?.[service]?.hash !== contract().services[service].hash
|
|
65
|
+
)
|
|
66
|
+
void rebuild(service)
|
|
67
|
+
}
|
|
68
|
+
} catch (e) {
|
|
69
|
+
log(`[kerf] the contract couldn't be regenerated: ${e.message.split('\n')[0]}`)
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
if (existsSync(src))
|
|
73
|
+
watchers.push(
|
|
74
|
+
watch(src, { recursive: true }, (_, file) => {
|
|
75
|
+
if (file && isServerModule(`src/${file.replaceAll('\\', '/')}`)) contractChanged()
|
|
76
|
+
}),
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
return watchers
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const debounce = (fn, ms = 200) => {
|
|
83
|
+
let t
|
|
84
|
+
return () => {
|
|
85
|
+
clearTimeout(t)
|
|
86
|
+
t = setTimeout(() => fn(), ms)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { renameSync, writeFileSync } from 'node:fs'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
import { isNative } from './common.mjs'
|
|
4
|
+
|
|
5
|
+
export function publisher(states, nativeDir, contract) {
|
|
6
|
+
/** What the server's `native()` reads on each call: one rename, so it never sees half of it. */
|
|
7
|
+
return () => {
|
|
8
|
+
const manifest = Object.fromEntries(
|
|
9
|
+
[...states]
|
|
10
|
+
.filter(([k]) => isNative(contract(), k))
|
|
11
|
+
.map(([k, s]) => [
|
|
12
|
+
k,
|
|
13
|
+
s.loaded ?? { hash: '', failure: s.failure ?? `\`${k}\` hasn't built` },
|
|
14
|
+
]),
|
|
15
|
+
)
|
|
16
|
+
writeFileSync(join(nativeDir, 'manifest.tmp'), JSON.stringify(manifest))
|
|
17
|
+
renameSync(join(nativeDir, 'manifest.tmp'), join(nativeDir, 'manifest.json'))
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process'
|
|
2
|
+
|
|
3
|
+
/** Start a binary and wait until it listens and its health check names `hash`. Rejects with why, and leaves nothing running. */
|
|
4
|
+
export function launch({ exe, service, hash, listen, secret, onExit, log }) {
|
|
5
|
+
return new Promise((ok, no) => {
|
|
6
|
+
const child = spawn(exe, [], {
|
|
7
|
+
env: { ...process.env, KERF_LISTEN: listen, KERF_RUST_SECRET: secret },
|
|
8
|
+
stdio: ['ignore', 'pipe', 'inherit'],
|
|
9
|
+
})
|
|
10
|
+
let out = '',
|
|
11
|
+
ready = false
|
|
12
|
+
const failed = (why) => {
|
|
13
|
+
clearTimeout(timer)
|
|
14
|
+
child.kill('SIGKILL')
|
|
15
|
+
no(Object.assign(new Error(why), { code: 'R109' }))
|
|
16
|
+
}
|
|
17
|
+
const timer = setTimeout(() => failed(`\`${service}\` didn't listen within 30 s`), 30_000)
|
|
18
|
+
child.on('error', (e) => failed(`\`${service}\` didn't start: ${e.message}`))
|
|
19
|
+
child.on('exit', (code, signal) => {
|
|
20
|
+
if (!ready) return failed(`\`${service}\` exited before it listened (${signal ?? code})`)
|
|
21
|
+
onExit?.(code, signal)
|
|
22
|
+
})
|
|
23
|
+
child.stdout.on('data', (d) => void heard(d))
|
|
24
|
+
const heard = async (d) => {
|
|
25
|
+
if (ready) return void process.stdout.write(d)
|
|
26
|
+
out += d
|
|
27
|
+
const addr = /kerf-listening (\S+)/.exec(out)?.[1]
|
|
28
|
+
if (!addr) return
|
|
29
|
+
ready = true
|
|
30
|
+
clearTimeout(timer)
|
|
31
|
+
const url = `http://${addr}`
|
|
32
|
+
try {
|
|
33
|
+
const health = await (await fetch(`${url}/_kerf/health`)).json()
|
|
34
|
+
if (health.contract !== hash) {
|
|
35
|
+
ready = false
|
|
36
|
+
return failed(
|
|
37
|
+
`R103: \`${service}\` was built from contract ${health.contract}, and this app's is ${hash}: rebuild it`,
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
log?.(`[kerf] ${service}: listening on ${addr}`)
|
|
41
|
+
ok({ child, url, port: Number(addr.split(':').pop()), inflight: 0 })
|
|
42
|
+
} catch (e) {
|
|
43
|
+
ready = false
|
|
44
|
+
failed(`\`${service}\`'s health check failed: ${e.message}`)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** SIGTERM, which the crate drains on; SIGKILL if that takes too long. */
|
|
51
|
+
export function stop(p, wait = 5_000) {
|
|
52
|
+
if (!p || p.child.exitCode !== null) return
|
|
53
|
+
p.stopped = true
|
|
54
|
+
p.child.kill('SIGTERM')
|
|
55
|
+
const t = setTimeout(() => p.child.kill('SIGKILL'), wait)
|
|
56
|
+
t.unref()
|
|
57
|
+
p.child.once('exit', () => clearTimeout(t))
|
|
58
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { writeContract } from '../contract.mjs'
|
|
2
|
+
|
|
3
|
+
export function makeRefresh(root, site, rust, say) {
|
|
4
|
+
return async () => {
|
|
5
|
+
const held = process.env.KERF_BUILD
|
|
6
|
+
process.env.KERF_BUILD = '1' // the same as a build: server modules load, and nothing connects
|
|
7
|
+
try {
|
|
8
|
+
const { contract, diagnostics } = await writeContract({ root, site, rust })
|
|
9
|
+
diagnostics.filter((d) => d.code.startsWith('R1')).forEach(say)
|
|
10
|
+
return {
|
|
11
|
+
contract,
|
|
12
|
+
blocked: diagnostics.find((d) => /^R10[12]$/.test(d.code) && d.severity === 'error'),
|
|
13
|
+
}
|
|
14
|
+
} finally {
|
|
15
|
+
if (held === undefined) delete process.env.KERF_BUILD
|
|
16
|
+
else process.env.KERF_BUILD = held
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
package/src/sidecar.mjs
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
// The sidecar supervisor (research 36 §5): what `kerf dev`, `kerf build`, `kerf start` and `kerf check` do for the Rust
|
|
2
|
+
// behind `remote()`, and for `native()`'s libraries (research 39). It runs in the CLI, never in the server bundle:
|
|
3
|
+
// `remote()` is only `fetch`, so a built server runs anywhere, and a sidecar deployed on its own is one environment
|
|
4
|
+
// variable away.
|
|
5
|
+
//
|
|
6
|
+
// dev cargo build, then a stable local proxy in front of the current child. A change under the crate, or a contract
|
|
7
|
+
// change from a server module, builds a new binary, starts it, health-checks it and swaps the proxy; the old
|
|
8
|
+
// child drains and goes. A failed build leaves the old child serving and says why.
|
|
9
|
+
// build cargo build --release, then the binaries run while Nitro prerenders (a card's `load` may read one), then
|
|
10
|
+
// they go to `.output/rust/`, with a manifest of each one's contract hash.
|
|
11
|
+
// start each binary on a fixed local port, restarted with backoff if it dies. The web server keeps serving; calls in
|
|
12
|
+
// the gap fail as R107.
|
|
13
|
+
// check cargo check, with errors mapped to the function they belong to (R103).
|
|
14
|
+
//
|
|
15
|
+
// `native()` has no child and no proxy. dev: each build is a new file under `.kerf/native/`, and the manifest beside it
|
|
16
|
+
// says which, so the next call loads it in place: an addon can't be unloaded, and needn't be. build: an addon and a
|
|
17
|
+
// `.wasm` next to the server, in `.output/server/rust/`, so whatever moves the server moves them too.
|
|
18
|
+
|
|
19
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync } from 'node:fs'
|
|
20
|
+
import { createServer, request as httpRequest, Agent } from 'node:http'
|
|
21
|
+
import { randomBytes } from 'node:crypto'
|
|
22
|
+
import { join, resolve } from 'node:path'
|
|
23
|
+
import { makeRefresh } from './sidecar/refresh.mjs'
|
|
24
|
+
import { watchServices } from './sidecar/dev-watch.mjs'
|
|
25
|
+
import { buildable, isNative } from './sidecar/common.mjs'
|
|
26
|
+
import { publisher } from './sidecar/manifest.mjs'
|
|
27
|
+
import { devBuilder } from './sidecar/dev-build.mjs'
|
|
28
|
+
import { stop } from './sidecar/process.mjs'
|
|
29
|
+
|
|
30
|
+
/** The app names a sidecar: a server module imports `remote`. Nothing else here runs for an app that doesn't. */
|
|
31
|
+
export function usesRemote(root) {
|
|
32
|
+
const src = join(root, 'src')
|
|
33
|
+
if (!existsSync(src)) return false
|
|
34
|
+
return readdirSync(src, { recursive: true, withFileTypes: true }).some(
|
|
35
|
+
(f) =>
|
|
36
|
+
f.isFile() &&
|
|
37
|
+
/\.[cm]?[jt]sx?$/.test(f.name) &&
|
|
38
|
+
/['"]@kerf-js\/start\/rust['"]/.test(readFileSync(join(f.parentPath, f.name), 'utf8')),
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export { crateDir, urlVariable } from './sidecar/common.mjs'
|
|
43
|
+
|
|
44
|
+
export { cargo, checkWasm, readCargo } from './sidecar/cargo.mjs'
|
|
45
|
+
|
|
46
|
+
// ---- dev ----------------------------------------------------------------------------------------------------------
|
|
47
|
+
|
|
48
|
+
/** The stable address the dev server is given: it outlives every child, so a reload moves no one's URL. */
|
|
49
|
+
function proxy(state) {
|
|
50
|
+
const agent = new Agent({ keepAlive: true })
|
|
51
|
+
const server = createServer((req, res) => {
|
|
52
|
+
const to = state.current
|
|
53
|
+
if (!to) {
|
|
54
|
+
if (!state.failure) return void res.destroy() // between crash and restart: `remote()` reports it, R107
|
|
55
|
+
res.writeHead(422, { 'content-type': 'application/json' })
|
|
56
|
+
return res.end(
|
|
57
|
+
JSON.stringify({ ok: false, error: { code: 'Drift', message: state.failure } }),
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
to.inflight++
|
|
61
|
+
res.once('close', () => {
|
|
62
|
+
if (--to.inflight === 0 && to.retired) stop(to)
|
|
63
|
+
})
|
|
64
|
+
const up = httpRequest(
|
|
65
|
+
`${to.url}${req.url}`,
|
|
66
|
+
{ method: req.method, headers: req.headers, agent },
|
|
67
|
+
(answer) => {
|
|
68
|
+
res.writeHead(answer.statusCode, answer.headers)
|
|
69
|
+
answer.pipe(res)
|
|
70
|
+
},
|
|
71
|
+
)
|
|
72
|
+
up.on('error', () => res.destroy())
|
|
73
|
+
req.pipe(up)
|
|
74
|
+
})
|
|
75
|
+
return new Promise((ok) => {
|
|
76
|
+
server.listen(0, '127.0.0.1', () => ok({ server, agent, port: server.address().port }))
|
|
77
|
+
})
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Build and start every sidecar, and keep them current. Sets `KERF_RUST_<NAME>_URL` and `KERF_RUST_SECRET` in this
|
|
82
|
+
* process before the dev server's worker starts, so it inherits them.
|
|
83
|
+
* @returns {Promise<{ attach(server: import('vite').ViteDevServer): void, close(): Promise<void>, ready: Record<string, any> }>}
|
|
84
|
+
*/
|
|
85
|
+
export async function devSidecars({ root, site, rust, log = console.log }) {
|
|
86
|
+
root = resolve(root)
|
|
87
|
+
const secret = (process.env.KERF_RUST_SECRET ||= randomBytes(32).toString('hex'))
|
|
88
|
+
const say = (d) =>
|
|
89
|
+
log(`[kerf] ${d.code} ${d.severity}: ${d.why}${d.rendered ? `\n${d.rendered}` : ''}`)
|
|
90
|
+
let vite,
|
|
91
|
+
closed = false
|
|
92
|
+
const overlay = (message) =>
|
|
93
|
+
vite?.ws.send({ type: 'error', err: { message, stack: '', plugin: 'kerf:sidecar' } })
|
|
94
|
+
const refresh = makeRefresh(root, site, rust, say)
|
|
95
|
+
|
|
96
|
+
const first = await refresh()
|
|
97
|
+
let contract = first.contract
|
|
98
|
+
const states = new Map()
|
|
99
|
+
const nativeDir = join(root, '.kerf', 'native')
|
|
100
|
+
const publish = publisher(states, nativeDir, () => contract)
|
|
101
|
+
if (buildable(contract, rust).some((k) => isNative(contract, k))) {
|
|
102
|
+
rmSync(nativeDir, { recursive: true, force: true })
|
|
103
|
+
mkdirSync(nativeDir, { recursive: true })
|
|
104
|
+
process.env.KERF_NATIVE_DIR = nativeDir
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const rebuild = devBuilder({
|
|
108
|
+
root,
|
|
109
|
+
rust,
|
|
110
|
+
secret,
|
|
111
|
+
log,
|
|
112
|
+
say,
|
|
113
|
+
overlay,
|
|
114
|
+
states,
|
|
115
|
+
nativeDir,
|
|
116
|
+
publish,
|
|
117
|
+
contract: () => contract,
|
|
118
|
+
closed: () => closed,
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
const watchers = await watchServices({
|
|
122
|
+
root,
|
|
123
|
+
rust,
|
|
124
|
+
contract: () => contract,
|
|
125
|
+
setContract: (next) => (contract = next),
|
|
126
|
+
first,
|
|
127
|
+
states,
|
|
128
|
+
proxy,
|
|
129
|
+
publish,
|
|
130
|
+
rebuild,
|
|
131
|
+
refresh,
|
|
132
|
+
overlay,
|
|
133
|
+
log,
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
const killAll = () => {
|
|
137
|
+
closed = true
|
|
138
|
+
for (const s of states.values()) stop(s.current, 1_000)
|
|
139
|
+
}
|
|
140
|
+
process.once('exit', killAll)
|
|
141
|
+
return {
|
|
142
|
+
ready: Object.fromEntries([...states].map(([k, s]) => [k, !!(s.current || s.loaded)])),
|
|
143
|
+
attach(server) {
|
|
144
|
+
vite = server
|
|
145
|
+
},
|
|
146
|
+
async close() {
|
|
147
|
+
process.removeListener('exit', killAll)
|
|
148
|
+
killAll()
|
|
149
|
+
for (const w of watchers) w.close()
|
|
150
|
+
for (const s of states.values()) {
|
|
151
|
+
s.proxy?.agent.destroy()
|
|
152
|
+
s.proxy?.server.closeAllConnections()
|
|
153
|
+
await new Promise((ok) => {
|
|
154
|
+
if (s.proxy) s.proxy.server.close(ok)
|
|
155
|
+
else ok()
|
|
156
|
+
})
|
|
157
|
+
}
|
|
158
|
+
},
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export { buildSidecars, shipSidecars, startSidecars } from './sidecar/build.mjs'
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { StartError, type Context, type Fn, type StandardSchemaV1 } from '../data'
|
|
2
|
+
import type { Bucket } from './types'
|
|
3
|
+
|
|
4
|
+
export function take(b: Bucket, perSecond: number, burst: number) {
|
|
5
|
+
const now = Date.now()
|
|
6
|
+
b.tokens = Math.min(burst, b.tokens + ((now - b.at) * perSecond) / 1000)
|
|
7
|
+
b.at = now
|
|
8
|
+
if (b.tokens < 1) return false
|
|
9
|
+
b.tokens--
|
|
10
|
+
return true
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export async function check(schema: StandardSchemaV1 | undefined, value: unknown, what: string) {
|
|
14
|
+
if (!schema) return value
|
|
15
|
+
const r = await schema['~standard'].validate(value)
|
|
16
|
+
if (!r.issues) return r.value
|
|
17
|
+
throw new StartError(
|
|
18
|
+
'Q002',
|
|
19
|
+
`${what} was refused: ${r.issues.map((i) => i.message).join('; ')}`,
|
|
20
|
+
'send what the schema accepts',
|
|
21
|
+
{
|
|
22
|
+
status: 400,
|
|
23
|
+
issues: r.issues.map((i) => ({
|
|
24
|
+
message: i.message,
|
|
25
|
+
path: i.path?.map((p) => (typeof p === 'object' ? p.key : p)),
|
|
26
|
+
})),
|
|
27
|
+
},
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
type Authorize = ((args: { ctx: Context; params: unknown }) => unknown) &
|
|
32
|
+
Record<'read' | 'write', (args: { ctx: Context; key: unknown }) => unknown>
|
|
33
|
+
export const allowed = async (
|
|
34
|
+
fn: Fn,
|
|
35
|
+
ctx: Context,
|
|
36
|
+
params: unknown,
|
|
37
|
+
write?: boolean,
|
|
38
|
+
): Promise<boolean> => {
|
|
39
|
+
const a = fn.def.authorize as Authorize
|
|
40
|
+
return !!(await (fn.kind === 'shared'
|
|
41
|
+
? a[write ? 'write' : 'read']({ ctx, key: params })
|
|
42
|
+
: a({ ctx, params })))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const denied = (fn: Fn) =>
|
|
46
|
+
new StartError(
|
|
47
|
+
'R011',
|
|
48
|
+
`\`${fn.$id}\`: \`authorize\` refused this client`,
|
|
49
|
+
'sign in, or check `authorize`',
|
|
50
|
+
)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Hub, Ev } from '../hub'
|
|
2
|
+
import type { Sock, Join } from './types'
|
|
3
|
+
import { ASK } from './rooms'
|
|
4
|
+
|
|
5
|
+
/** Fan-out of a room's messages to one join. */
|
|
6
|
+
export function deliver(c: Sock, j: Join, h: Hub, ev: Ev, send: (c: Sock, out: unknown[]) => void) {
|
|
7
|
+
const m = (ev.p ??= JSON.parse(ev.raw)) as {
|
|
8
|
+
f?: string
|
|
9
|
+
x?: 1
|
|
10
|
+
t?: string
|
|
11
|
+
n: string
|
|
12
|
+
m: unknown
|
|
13
|
+
}
|
|
14
|
+
if (m.n === ASK || (m.x && m.f === c.k) || (m.t && m.t !== c.k)) return
|
|
15
|
+
send(c, ['m', j.n, h.cursorOf(ev.seq), m.n, m.m])
|
|
16
|
+
}
|