@kerf-js/cli 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (176) hide show
  1. package/LICENSE +16 -0
  2. package/README.md +554 -2
  3. package/bin/kerf.mjs +15 -0
  4. package/docs/async.md +50 -0
  5. package/docs/browser.md +53 -0
  6. package/docs/config.md +127 -0
  7. package/docs/core.md +30 -0
  8. package/docs/deploy.md +108 -0
  9. package/docs/design.md +106 -0
  10. package/docs/entrypoints.md +83 -0
  11. package/docs/fonts.md +118 -0
  12. package/docs/icons.md +108 -0
  13. package/docs/lists.md +25 -0
  14. package/docs/meta.md +92 -0
  15. package/docs/plugins.md +166 -0
  16. package/docs/props.md +88 -0
  17. package/docs/reactivity.md +26 -0
  18. package/docs/rules.md +39 -0
  19. package/docs/rust.md +253 -0
  20. package/docs/start-short.md +10 -0
  21. package/docs/start.md +642 -0
  22. package/docs/state.md +122 -0
  23. package/docs/style.md +82 -0
  24. package/docs/surface.md +158 -0
  25. package/docs/testing.md +81 -0
  26. package/docs/theme.md +263 -0
  27. package/docs/ui-state.md +105 -0
  28. package/docs/verify.md +61 -0
  29. package/package.json +47 -3
  30. package/src/add/config.mjs +93 -0
  31. package/src/add/errors.mjs +2 -0
  32. package/src/add/setup.mjs +88 -0
  33. package/src/add-rust.mjs +178 -0
  34. package/src/add.mjs +392 -0
  35. package/src/agents-md.mjs +240 -0
  36. package/src/alias.mjs +70 -0
  37. package/src/audit/page.mjs +313 -0
  38. package/src/audit/report.mjs +69 -0
  39. package/src/audit/utils.mjs +5 -0
  40. package/src/audit.mjs +319 -0
  41. package/src/browser/cli.mjs +53 -0
  42. package/src/browser/client.mjs +299 -0
  43. package/src/browser/commands.mjs +258 -0
  44. package/src/capture.mjs +21 -0
  45. package/src/catalog/audit.mjs +129 -0
  46. package/src/catalog/auth.mjs +40 -0
  47. package/src/catalog/compiler.mjs +337 -0
  48. package/src/catalog/contracts.mjs +18 -0
  49. package/src/catalog/database.mjs +52 -0
  50. package/src/catalog/deploy.mjs +117 -0
  51. package/src/catalog/design.mjs +159 -0
  52. package/src/catalog/forms.mjs +25 -0
  53. package/src/catalog/hydration.mjs +29 -0
  54. package/src/catalog/icons.mjs +24 -0
  55. package/src/catalog/media.mjs +163 -0
  56. package/src/catalog/metadata.mjs +116 -0
  57. package/src/catalog/plugins.mjs +228 -0
  58. package/src/catalog/realtime.mjs +195 -0
  59. package/src/catalog/rust.mjs +75 -0
  60. package/src/catalog/server-functions.mjs +100 -0
  61. package/src/catalog/state.mjs +39 -0
  62. package/src/catalog/style-groups.mjs +54 -0
  63. package/src/catalog/tests.mjs +127 -0
  64. package/src/catalog/text.mjs +200 -0
  65. package/src/catalog/ui-state.mjs +42 -0
  66. package/src/catalog.mjs +103 -0
  67. package/src/check/runtime.mjs +280 -0
  68. package/src/check.mjs +321 -0
  69. package/src/cli/authoring.mjs +213 -0
  70. package/src/cli/project.mjs +302 -0
  71. package/src/cli.mjs +340 -0
  72. package/src/config.d.ts +1 -0
  73. package/src/config.mjs +5 -0
  74. package/src/contributions.mjs +119 -0
  75. package/src/db-layer.mjs +41 -0
  76. package/src/deploy/cdn.mjs +142 -0
  77. package/src/deploy/files.mjs +266 -0
  78. package/src/deploy/fit.mjs +166 -0
  79. package/src/deploy/hosts.mjs +126 -0
  80. package/src/deploy/index/cli.mjs +20 -0
  81. package/src/deploy/index/environment.mjs +69 -0
  82. package/src/deploy/index/steps.mjs +87 -0
  83. package/src/deploy/index.mjs +387 -0
  84. package/src/deploy/needs.mjs +149 -0
  85. package/src/design-checks/accessible-names.mjs +62 -0
  86. package/src/design-checks/contrast.mjs +43 -0
  87. package/src/design-checks/csp.mjs +60 -0
  88. package/src/design-checks/files.mjs +59 -0
  89. package/src/design-checks/heading-links.mjs +61 -0
  90. package/src/design-checks/interaction.mjs +65 -0
  91. package/src/design-checks/jsx.mjs +67 -0
  92. package/src/design-checks/layout-values.mjs +68 -0
  93. package/src/design-checks/native-controls.mjs +48 -0
  94. package/src/design-checks/outline.mjs +39 -0
  95. package/src/design-checks/reporter.mjs +37 -0
  96. package/src/design-checks/rules.mjs +274 -0
  97. package/src/design-checks/values.mjs +262 -0
  98. package/src/design-checks.mjs +49 -0
  99. package/src/diag.mjs +98 -0
  100. package/src/docs.mjs +228 -0
  101. package/src/doctor.mjs +42 -0
  102. package/src/files.mjs +60 -0
  103. package/src/fix.mjs +48 -0
  104. package/src/gate/after.mjs +40 -0
  105. package/src/gate/before.mjs +54 -0
  106. package/src/gate/classify.mjs +72 -0
  107. package/src/gate/mutation.mjs +209 -0
  108. package/src/gate/resolve-cases.mjs +22 -0
  109. package/src/gate/result.mjs +63 -0
  110. package/src/gate/run.mjs +75 -0
  111. package/src/gate.mjs +178 -0
  112. package/src/index.mjs +9 -0
  113. package/src/infer/box-props.cjs +140 -0
  114. package/src/infer/core-boxes.cjs +46 -0
  115. package/src/infer/derived-writes.cjs +95 -0
  116. package/src/infer/engine/checks.cjs +193 -0
  117. package/src/infer/engine/flow.cjs +293 -0
  118. package/src/infer/engine/report.cjs +158 -0
  119. package/src/infer/engine/utils.cjs +15 -0
  120. package/src/infer/engine.cjs +265 -0
  121. package/src/infer/facts/sites.cjs +114 -0
  122. package/src/infer/facts.cjs +345 -0
  123. package/src/infer/hosts.cjs +161 -0
  124. package/src/infer/lsp.mjs +240 -0
  125. package/src/infer/session.cjs +161 -0
  126. package/src/infer/shadow.cjs +54 -0
  127. package/src/infer/worker.cjs +145 -0
  128. package/src/jsx-scan.mjs +128 -0
  129. package/src/kerf.mjs +98 -0
  130. package/src/load-config/schema.mjs +300 -0
  131. package/src/load-config.mjs +302 -0
  132. package/src/map.mjs +177 -0
  133. package/src/mcp/browser-tools.mjs +155 -0
  134. package/src/mcp.mjs +273 -0
  135. package/src/meta-table.mjs +63 -0
  136. package/src/mutate.mjs +224 -0
  137. package/src/plugin-checks.mjs +118 -0
  138. package/src/plugins.mjs +258 -0
  139. package/src/project.mjs +310 -0
  140. package/src/props-checks.mjs +64 -0
  141. package/src/props-listener.mjs +56 -0
  142. package/src/react-checks.mjs +67 -0
  143. package/src/server-state-checks.mjs +34 -0
  144. package/src/share-checks.mjs +158 -0
  145. package/src/site.mjs +46 -0
  146. package/src/slots.mjs +181 -0
  147. package/src/start/build.mjs +116 -0
  148. package/src/start/routes.mjs +38 -0
  149. package/src/start.mjs +394 -0
  150. package/src/state-checks/core-state.mjs +223 -0
  151. package/src/state-checks/modules.mjs +17 -0
  152. package/src/state-checks/scan.mjs +232 -0
  153. package/src/state-checks/server-graph.mjs +65 -0
  154. package/src/state-checks.mjs +189 -0
  155. package/src/style/find.mjs +121 -0
  156. package/src/style.mjs +327 -0
  157. package/src/surface-checks.mjs +384 -0
  158. package/src/tailwind.mjs +23 -0
  159. package/src/tasks.mjs +261 -0
  160. package/src/test-rules/context.mjs +109 -0
  161. package/src/test-rules/file-rules.mjs +279 -0
  162. package/src/test-rules/rule-helpers.mjs +244 -0
  163. package/src/test-rules/syntax.mjs +223 -0
  164. package/src/test-rules/test-rules.mjs +127 -0
  165. package/src/testdiff.mjs +219 -0
  166. package/src/tests.mjs +120 -0
  167. package/src/text-checks.mjs +332 -0
  168. package/src/tokens.mjs +33 -0
  169. package/src/trust/scan.mjs +95 -0
  170. package/src/trust.mjs +394 -0
  171. package/src/workspace.mjs +182 -0
  172. package/ts-plugin/context.js +57 -0
  173. package/ts-plugin/index.js +17 -0
  174. package/ts-plugin/mapping.js +65 -0
  175. package/ts-plugin/package.json +1 -0
  176. package/ts-plugin/proxy.js +144 -0
package/docs/rust.md ADDED
@@ -0,0 +1,253 @@
1
+ # Rust: `native()` and `remote()`
2
+
3
+ Rust is optional. kerf needs no Rust toolchain to create, run, build or check an app, on any platform: the compiler ships as
4
+ WASM, and a prebuilt native addon installs by itself where one fits, which makes builds faster and changes no output.
5
+ `kerf doctor` says which one is running. Rust is for this page: the code you choose to write in it, which is the only thing
6
+ that calls `cargo`.
7
+
8
+ A server function can have a Rust body. The client doesn't change: the same import, call, cache, tags and errors. Zod
9
+ stays the only schema. Reach for this at about 1 ms of CPU per call or on byte payloads (search, parsing, images), not for
10
+ IO-bound CRUD.
11
+
12
+ | | `native('media')` | `remote('rust://search')` |
13
+ |---|---|---|
14
+ | Runs | in the server's process: an addon, or WASM where one can't load | a sidecar, over loopback or another host |
15
+ | Good for | one hot CPU function | many functions, IO, anything with its own state or runtime |
16
+ | Costs per call | ~10–30 µs | ~50–80 µs |
17
+ | A panic costs | that call | that call |
18
+ | A hang costs | a worker thread until it returns | nothing here: the sidecar is killable |
19
+
20
+ `kerf add rust [name]` writes a working crate, its module and one function, with `native()`; `--remote` writes a sidecar.
21
+ The crate's code is the same `impl Service` either way, so moving between them is a line in `src/server/rust.ts` and the
22
+ glue at the bottom of the crate.
23
+
24
+ ## Swap a `run` for `impl`
25
+
26
+ ```ts
27
+ // src/server/rust.ts: the only place Rust targets are named
28
+ import { remote } from '@kerf-js/start/rust'
29
+ export const search = remote('rust://search')
30
+ ```
31
+
32
+ <!-- example: skip imports src/server/rust.ts from the block above -->
33
+ ```ts
34
+ // src/server/docs.ts
35
+ import { query } from '@kerf-js/start'
36
+ import { z } from 'zod'
37
+ import { search } from './rust'
38
+
39
+ export const searchDocs = query({
40
+ input: z.object({
41
+ q: z.string().trim().min(1).max(200),
42
+ limit: z.int().min(1).max(50).default(10),
43
+ }),
44
+ output: z.array(z.object({
45
+ id: z.string(), title: z.string(), score: z.number(),
46
+ })),
47
+ tags: ['docs'],
48
+ // Was: run: ({ input }) => rank(input.q, input.limit)
49
+ impl: search,
50
+ })
51
+ ```
52
+
53
+ `output` is required on an `impl`. `crates/search` is the crate, unless config says otherwise. `kerf add rust` writes all of
54
+ this; the `kerf` path is inside `@kerf-js/start`, because the crate isn't on crates.io yet:
55
+
56
+ ```toml
57
+ # crates/search/Cargo.toml
58
+ [dependencies]
59
+ serde = { version = "1", features = ["derive"] }
60
+ tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
61
+ kerf = { path = "../../node_modules/@kerf-js/start/crate" }
62
+
63
+ [build-dependencies.kerf]
64
+ path = "../../node_modules/@kerf-js/start/crate"
65
+ default-features = false
66
+ features = ["build"]
67
+ ```
68
+
69
+ ```rust
70
+ // crates/search/build.rs
71
+ fn main() { kerf::build::generate("search"); }
72
+ ```
73
+
74
+ ```rust
75
+ // crates/search/src/main.rs
76
+ // Types and the `Service` trait are generated from Zod.
77
+ kerf::contract!();
78
+ use contract::*;
79
+
80
+ struct Search;
81
+
82
+ impl Service for Search {
83
+ async fn search_docs(
84
+ &self,
85
+ ctx: kerf::Ctx,
86
+ input: SearchDocsInput,
87
+ ) -> Result<SearchDocsOutput, kerf::Error> {
88
+ ctx.touch("docs");
89
+ Ok(vec![])
90
+ }
91
+ }
92
+
93
+ #[tokio::main]
94
+ async fn main() -> std::io::Result<()> {
95
+ kerf::serve(kerf::router(contract::functions(Search))).await
96
+ }
97
+ ```
98
+
99
+ Use Rust 1.99 or newer (`rust-toolchain.toml` pins it). A method is the function's name in snake case; a type is its name plus `Input`, `Output`, or a
100
+ property's name in pascal case.
101
+
102
+ ## In-process: `native()`
103
+
104
+ The same `Service`, in a library. `crate-type = ["cdylib"]`, and `features = ["native"]` on `kerf` in place of the server:
105
+
106
+ ```rust
107
+ // crates/media/src/lib.rs
108
+ kerf::contract!();
109
+ use contract::*;
110
+
111
+ struct Media;
112
+ impl Service for Media {
113
+ async fn thumbnail(
114
+ &self,
115
+ ctx: kerf::Ctx,
116
+ input: ThumbnailInput,
117
+ ) -> Result<ThumbnailOutput, kerf::Error> { /* … */ }
118
+ }
119
+
120
+ kerf::native!(contract::functions(Media));
121
+ ```
122
+
123
+ ```ts
124
+ // src/server/rust.ts
125
+ import { native } from '@kerf-js/start/rust'
126
+ // Options: timeout, claims, validate, as for remote().
127
+ export const media = native('media')
128
+ ```
129
+
130
+ - An addon runs each call on the worker pool, so a long one doesn't hold the event loop. `native()` sizes the pool to the
131
+ machine unless `UV_THREADPOOL_SIZE` is set.
132
+ - `kerf build` also builds `wasm32-unknown-unknown` with SIMD on. With no addon for the platform, the server loads the
133
+ `.wasm` and says so. WASM runs on the event loop, as a TS `run` does.
134
+ - There's no async runtime in it. A method may be `async`, but one that waits on IO belongs in `remote()`; on WASM, one that
135
+ waits is R108.
136
+ - `timeout` stops TS waiting (R107). A call can't be stopped from outside: check `ctx.cancelled()` in a long loop.
137
+ - A panic is R108 for that call. On WASM it ends the instance, and the next call gets a new one, so a static is lost.
138
+ - Bytes cross as JSON, so `z.base64()` for now: the call costs more than the `Buffer` it could be.
139
+
140
+ ## What happens where
141
+
142
+ TS keeps the public edge: sessions, CSRF, input validation, the cache, `auth`, `tags`, `invalidates`. Rust gets one crossing
143
+ per call (a signed POST for a sidecar): the parsed input, the user's claims (never cookies), `traceparent` and a deadline.
144
+ TS then validates the output, in production too. Set `remote(target, { validate: false })` to trust it.
145
+
146
+ Options on `remote(target, options)`: `url` (else `KERF_RUST_<NAME>_URL`), `timeout` (ms, default 10000), `claims`
147
+ (`user => …`, default `id`, `roles`, `tenant`), `validate`.
148
+
149
+ ## The context
150
+
151
+ | A function that is | Its `ctx` | `user()` |
152
+ |---|---|---|
153
+ | `auth: 'user'` | `kerf::UserCtx` | `&User`, always there |
154
+ | cached with shared scope | `kerf::SharedCtx` | doesn't exist, so it can't leak into a shared cache |
155
+ | anything else | `kerf::Ctx` | `Option<&User>` |
156
+
157
+ All three have `ctx.touch(table)`, `ctx.tag(tag)`, `ctx.remaining()`, `ctx.cancelled()`, `ctx.request_id()`,
158
+ `ctx.traceparent()`.
159
+
160
+ - `ctx.touch("docs")` reports a table it read or wrote. In dev it feeds Q011, like a TS `run`'s. A cached query with `tags`
161
+ that never calls it is R105; a query that reads no table should drop `tags`.
162
+ - `ctx.tag("doc:7")` adds a cache tag. It joins the declared ones; Rust can't remove any.
163
+
164
+ ## Typed errors
165
+
166
+ <!-- example: skip a fragment of src/server/docs.ts -->
167
+ ```ts
168
+ export const getDoc = query({
169
+ input, output,
170
+ errors: { NotFound: z.object({ id: z.string() }) },
171
+ impl: search,
172
+ })
173
+ // A TS body raises the same thing:
174
+ // run: () => raise('NotFound', { id })
175
+ ```
176
+
177
+ ```rust
178
+ async fn get_doc(
179
+ &self,
180
+ ctx: kerf::Ctx,
181
+ input: GetDocInput,
182
+ ) -> Result<GetDocOutput, GetDocError> {
183
+ Err(GetDocError::NotFound(GetDocNotFound {
184
+ id: input.id.to_string(),
185
+ }))
186
+ }
187
+ ```
188
+
189
+ The client catches `e.code === 'NotFound'` and `e.data`, either way. A panic or `kerf::Error` is R108: the client gets a
190
+ request id, and the detail stays in the sidecar's log.
191
+
192
+ ## Types that cross
193
+
194
+ - JSON only. `z.date()` is R101: use `z.iso.datetime()`, or `z.codec(z.iso.datetime(), z.date(), …)` for a `Date` in TS.
195
+ - `.trim()`, `.default()` and codecs are fine. A one-way `.transform()` on an input is R101, as it can't be encoded back.
196
+ - An output may transform: TS parses what Rust sends.
197
+ - `z.void()` is R101: use `z.null()`.
198
+ - Tag a union of objects with `z.discriminatedUnion`. A plain one is R102, or set `rust: { unions: 'allow' }`.
199
+ - The wire is the input side of each schema, so Rust sees what the codec encodes.
200
+
201
+ ## `kerf dev`, `build`, `start`
202
+
203
+ - `kerf dev` builds each sidecar, waits for the first build, and rebuilds on an edit. The new binary starts beside the old
204
+ one, takes over when it's healthy, and the old one drains, so no request fails. A failed build keeps the old one
205
+ running and prints R103 or R109.
206
+ - A `native()` crate rebuilds the same way, and no restart is needed: each build is a new file, the next call loads it, and
207
+ calls on the old one finish. An addon can't be unloaded, so a long session holds its old copies (about 0.5 MB each); they
208
+ go when `kerf dev` stops.
209
+ - `kerf check` runs `cargo check`, and stops at the Rust a schema edit broke, with the function's id (R103). For a
210
+ `native()` crate it checks the WASM build too (R104), and reads each cached query for `touch` (R105).
211
+ - `kerf build` runs `cargo build --release`, runs the sidecars while it prerenders, and ships them in `.output/rust/`.
212
+ A `native()` crate's addon and `.wasm` go in `.output/server/rust/`, so whatever moves the server moves them.
213
+ - `kerf start` runs them on loopback, restarts a crash with backoff, and stops them when it stops. A dead sidecar costs
214
+ its functions (R107), not the server.
215
+
216
+ ## Deployment and settings
217
+
218
+ ```ts
219
+ // kerf.config.ts
220
+ rust: {
221
+ unions: 'allow', // let plain object unions through (R102)
222
+ services: {
223
+ search: { crate: 'native/search' }, // default: crates/<name>
224
+ core: { external: true }, // built and run elsewhere: no cargo
225
+ // Links C; this host has an addon, so no fallback (R104).
226
+ imaging: { wasm: false },
227
+ },
228
+ }
229
+ ```
230
+
231
+ A sidecar on another host needs `external: true`, `KERF_RUST_<NAME>_URL`, and the same `KERF_RUST_SECRET` on both sides;
232
+ `kerf dev` and `kerf start` make those themselves for sidecars they run. Bind it privately: it trusts only the signature,
233
+ and the claims in it. The binary reads `KERF_LISTEN` (default a free loopback port); a build reads `KERF_CONTRACT`, or finds
234
+ `.kerf/contract.json` above the crate. `GET /_kerf/health` gives its service, contract hash, functions and pid. An edge preset can call `remote()`, as it is only
235
+ `fetch`, but something else must run the sidecar. `native()` needs a file system, so on an edge preset it's R104: use
236
+ `remote()`, the same crate behind `kerf::router`.
237
+
238
+ Each call is a `tracing` span with `trace_id` and `parent_id`. With the `otel` feature and your own OpenTelemetry layer,
239
+ the span continues TS's trace.
240
+
241
+ In a test, `handlers` replaces a function as it does a `run`, and nothing crosses.
242
+
243
+ ## Diagnostics
244
+
245
+ - **R101** no `output`, a type JSON can't carry, or a one-way input transform.
246
+ - **R102** a plain `z.union` of objects.
247
+ - **R103** the Rust and the contract disagree: at `cargo`, in a binary's hash, or in an output or undeclared error at runtime.
248
+ - **R104** `native()` on a preset that can't run it, a `.wasm` that won't build, or a build with nothing for this platform.
249
+ - **R105** a cached query with `tags` that reports no table.
250
+ - **R106** no address, no secret, or a refused signature.
251
+ - **R107** not answering, or past the deadline.
252
+ - **R108** a panic or `kerf::Error`.
253
+ - **R109** the crate doesn't build for a reason unrelated to the contract, or it isn't there.
@@ -0,0 +1,10 @@
1
+ `@kerf-js/start` app: file routes in `src/routes` (`_layout.tsx`, `[id].tsx`, `load`, `meta`), server functions in `src/server/**` or `*.server.ts`. Import your own code as `#/server/todos` (`src/server/todos`); generated files are `kerf:slot/<id>` and `kerf:tokens/<name>`.
2
+
3
+ - **Reads:** `query({ tags: ['todos'], auth: 'user', run: ({ ctx }) => ctx.db.select().from(todos) })`. In a component `todos().value ?? []`; in `load` and handlers `await todos.fetch()`.
4
+ - **Writes:** `mutation({ input, auth: 'user', invalidates: ['todos'], optimistic, run })`.
5
+ - **Content an owner edits** (menu, settings, pages): read with `cache: { life: 'hours', warm: true }`; edits go to a draft (`preview.start(id)` shows the owner), publishing applies it, slow follow-ups go in `after()`. `kerf docs start` § Publishing.
6
+ - **Database:** Drizzle; `await` every statement. Schema change: `kerf db generate` (Q015). Inputs: `schemas.todos.insert.pick({ title: true })`.
7
+ - **Auth:** `ctx.user` is the user or `null`; `auth: 'user'` refuses anyone else (A002), `{ role: 'admin' }` or `{ can: (ctx) => … }` the wrong user (A006). With auth on, mutations need a signed-in user unless `auth: 'public'` (sign-in, sign-up, sign-out). Guard pages with `redirect('/login')` in a layout's `load`; `run` still checks who owns a row. Sessions live in the database; pages cache them 5 minutes in a signed cookie (`session.cookieCache`). JWTs are API-only (`api: { functions }`).
8
+ - **Never** copy query results into `state`, import `db` from a component (Q012), `fetch` `/_kerf/*`, or poll (`todos.live()`; rooms: `channel`).
9
+ - **Navigation:** chrome shared by sections (sidebar, tabs) goes in the one layout above them, following `currentPath()`: a per-section `_layout.tsx` is replaced on each move. Route changes show a skeleton from the page's markup; `export const skeleton = Named` (plain markup, class and data-* only) overrides it.
10
+ - **Every page sets `meta` as you build it**, from `data` and `params`: `kerf docs meta`.