@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/icons.md ADDED
@@ -0,0 +1,108 @@
1
+ # Icons
2
+
3
+ `@kerf-js/veneer-icons` is kerf's own set: 651 original icons in veneer's three expression tiers. Outline at quiet and balanced, pixel art on a 16×16 grid at vivid. Each has two forms, hollow (the default) and solid, drawn apart in every tier and shaded with dither.
4
+
5
+ `kerf add icons` installs it and adds `icons()` to the config. veneer already depends on it.
6
+
7
+ ## Using one
8
+
9
+ Three forms, and all three build to the same bytes:
10
+
11
+ ```tsx
12
+ import { CheckIcon } from '@kerf-js/veneer-icons'
13
+ <CheckIcon />
14
+
15
+ // Or: import * as Icons
16
+ import Icons from '@kerf-js/veneer-icons'
17
+ <Icons.Check />
18
+
19
+ // Or import from '@kerf-js/veneer-icons/icon'.
20
+ import { Icon } from '@kerf-js/veneer'
21
+ <Icon name="check" />
22
+ ```
23
+
24
+ Props on any of them:
25
+
26
+ ```tsx
27
+ // Decorative: aria-hidden, focusable="false"
28
+ <CheckIcon />
29
+ <CheckIcon label="Saved" /> // role="img" with that name
30
+ <CheckIcon size={24} /> // otherwise veneer's icon sizes apply
31
+ // This tier regardless of the page's expression.
32
+ <CheckIcon tier="vivid" />
33
+ ```
34
+
35
+ An icon you don't use costs nothing. One adds about 164 B of brotli with all tiers in. `sideEffects` lists only CSS, so a named import ships that icon.
36
+
37
+ ## Names and variants
38
+
39
+ One scheme, generated from the art with its types:
40
+
41
+ | Export | Pack | `<Icon>` | What it is |
42
+ |---|---|---|---|
43
+ | `BellIcon` | `Icons.Bell` | `name="bell"` | hollow, the default |
44
+ | `BellIconHollow` | `Icons.BellHollow` | | the same, by name |
45
+ | `BellIconSolid` | `Icons.BellSolid` | `solid` | the solid form |
46
+ | `BellIconAction` | `Icons.BellAction` | `action` | has a built-in animation |
47
+
48
+ The tier is a prop, never a name. Hollow and solid describe the object: an open mark like an arrow is an outlined arrow in hollow and a filled one in solid, never a badge. Pick it on the sheet: `npm run sheet` in the package writes `contact-sheet.html`, with tier, variant, size, theme and search. `dither-sheet.html` compares the treatments.
49
+
50
+ ## A wrong name
51
+
52
+ `<Icon name>` is typed, so a bad name is a type error. Untyped code gets the same from the compiler and from `kerf check`:
53
+
54
+ ```
55
+ I001 no icon 'chekc', did you mean 'check'?
56
+ ```
57
+
58
+ It fails the build, and so does a variant an icon doesn't have.
59
+
60
+ ## Names that aren't literal
61
+
62
+ The build follows a name through variables, props, conditionals, maps and component boundaries, and ships what it finds. A name it can't follow, such as one from an API, is legal. `kerf check` reports it as I002 with the file and the cost (I003 if the pack itself is passed around). That use then gets one of two things:
63
+
64
+ - **The pack, as its own chunk.** The default. About 111 KB of brotli for all 651 with every tier in, loaded only by a page that reaches it. A page stays about 1.6 KB bigger than one with no dynamic name.
65
+ - **Single icons from a URL.** `icons({ source: 'https://cdn.example.com/icons/' })`. Nothing of the pack is built; each icon is fetched when first drawn.
66
+
67
+ `kerf icons publish <dir>` writes every icon and variant as static `.svg` files, with a `manifest.json`, for that URL. It writes files and uploads nothing. Action icons aren't published.
68
+
69
+ A remote icon renders an empty `<svg data-icon data-loading>` of the right size until it arrives, then swaps with no layout shift, and carries `data-failed` if the fetch fails. It stays empty on the server, so SSR needs no network. Elements are allowlisted before they are inserted. A Content-Security-Policy needs that origin in `connect-src`.
70
+
71
+ ## Actions
72
+
73
+ Five icons have a built-in animation: `trash` (the lid lifts), `bell` (it rings), `check` (it draws on), `refresh` (it spins), `copy` (the sheet stacks).
74
+
75
+ ```tsx
76
+ // Hover, focus and press on the icon or its parent.
77
+ <Icon name="trash" action />
78
+ <Icon name="refresh" action="on" /> // now; refresh keeps spinning
79
+ // 'hover' | 'focus' | 'press' | 'on'
80
+ <TrashIconAction action="press" />
81
+ ```
82
+
83
+ It's CSS alone, and each one's stylesheet ships only if that icon is used (about 230 B of CSS and 50 B of JS, brotli). Outlines ease; pixels move in whole cells, in frames. veneer's motion dial and `prefers-reduced-motion` turn it off. An action on another icon is I001.
84
+
85
+ ## Tiers
86
+
87
+ With no options each icon carries its tiers, and CSS shows the one for the nearest `data-expression`. Switching needs no script, doesn't flash and doesn't change the box. A nested `data-expression` is honoured. The build keeps only the tiers your config and `data-expression` attributes name.
88
+
89
+ To compile one tier in, with no switch, name it in the config:
90
+
91
+ <!-- example: skip @kerf-js/veneer-icons/plugin has no type declarations yet (TS7016 under strict) -->
92
+ ```ts
93
+ import { icons } from '@kerf-js/veneer-icons/plugin'
94
+
95
+ export default { plugins: [icons({ tier: 'vivid' })] }
96
+ ```
97
+
98
+ `tier` is `quiet`, `balanced` or `vivid`. veneer's own icons follow it too. After that a page's `data-expression` no longer changes icons: run `kerf plugins sync` if you change it.
99
+
100
+ ## Pixel sizes
101
+
102
+ The pixel tier is exact at 16, 32 and 48. At 20 and 24 a 1× screen rounds cells unevenly; a 2× screen draws 24 evenly. Prefer 16 or 32 at vivid.
103
+
104
+ ## Styling
105
+
106
+ A solid cuts its marks with `--veneer-icon-knock`, which defaults to `Canvas`. On a coloured surface, like a solid button, set it to that surface's colour.
107
+
108
+ Icons are `currentColor`. The kerf stroke, the one that says what the icon means, is `[data-w]`, so `svg [data-w] { stroke: var(--veneer-color-accent-text) }` colours it. Its weight is `--veneer-icon-kerf`.
package/docs/lists.md ADDED
@@ -0,0 +1,25 @@
1
+ # Lists and conditionals
2
+
3
+ - `xs.map(x => <Row key={x.id} … />)` in JSX is a keyed list. Each key keeps its row; a new object under the same
4
+ key updates the row in place.
5
+ - Over objects, a missing `key` is **W010** (autofix to `key={x.id}` when items have `id`). Over primitives, the
6
+ value is the key.
7
+ - `xs.filter(…).sort(…).map(…)` works; put the chain in a `derived` when other code needs it too.
8
+ - No early `return` inside the `.map` callback (**W013**). Filter first.
9
+ - `cond && <X/>` with a number on the left renders `0` (**W011**). Write `n > 0 &&`.
10
+ - **Stable items (W014).** `xs.value = xs.value.map(p => ({ ...p, price }))` gives every item a new identity, so
11
+ every row re-runs on every change (1 DOM write, hundreds of re-runs). Replace only what changed:
12
+
13
+ ```ts
14
+ products.value = products.value.map(p =>
15
+ p.id === id ? { ...p, price } : p,
16
+ )
17
+ ```
18
+
19
+ The compiler sees this inside one file; `kerf check` follows the box through props into other files.
20
+ - **One entry per row (W017).** A row that reads `cart[p.id]` (a prop) or `cart.value[p.id]` depends on the whole
21
+ `cart`, so adding one item re-runs all 150 rows. Read the entry from the box: `cart.get(p.id)` re-runs only the row
22
+ whose entry changed (`Object.is`). For a prop, pass the box (`cart={cart}`, typed `State<Cart>`); the box form
23
+ has an autofix. The same holds one step further: passing `live={live.value[s.id]}` to a row component, or a row
24
+ component reading `live.value[stock.id]` (in JSX or `derived`), re-runs every row. Pass the box and read
25
+ `live.get(stock.id)` in the row.
package/docs/meta.md ADDED
@@ -0,0 +1,92 @@
1
+ # Page metadata and share cards
2
+
3
+ Every page has a title, a description, a canonical URL, Open Graph and Twitter tags and a share card, in the first
4
+ response, where a crawler can read them. Most of it is automatic. The words are yours.
5
+
6
+ ## Set `meta` as you build the page
7
+
8
+ Writing a page includes writing what a search result or a shared link says about it. A title, and one plain sentence
9
+ on what the reader gets, from what is on the page:
10
+
11
+ ```tsx
12
+ import type { Meta } from '@kerf-js/start'
13
+
14
+ export const meta: Meta = {
15
+ title: 'Pricing',
16
+ description: 'Three plans, billed monthly, ' +
17
+ 'and what each one includes.',
18
+ }
19
+ ```
20
+
21
+ On a param route, build it from `data` and `params`, so each page says what it is:
22
+
23
+ <!-- example: skip needs the app's own getUser query -->
24
+ ```tsx
25
+ // src/routes/users/[id].tsx
26
+ import { notFound, type LoadArgs, type Meta } from '@kerf-js/start'
27
+ import { getUser } from '#/server/users'
28
+
29
+ export const load = async ({ params }: LoadArgs<'/users/[id]'>) => {
30
+ const user = await getUser.fetch({ id: params.id })
31
+ return user ?? notFound()
32
+ }
33
+ export const meta: Meta<typeof load> = ({ data }) => ({
34
+ title: data.name,
35
+ description: data.bio,
36
+ })
37
+ ```
38
+
39
+ `meta` is plain data: it runs once per request after the loaders and before the first byte. It can't read a box (H006)
40
+ or a deferred field (H007). Set `title: { absolute: 'Home' }` to skip the template, `og`, `twitter` and `extra` for
41
+ what else a page needs, and `jsonLd` for structured data. Layers merge in this order, later winning: `site` in
42
+ `kerf.config.ts`, what the page rendered, each layout's `meta`, the route's `meta`.
43
+
44
+ ## What kerf does for you
45
+
46
+ With no `meta`, or a `meta` that leaves a field out:
47
+
48
+ - **Title:** the page's `h1`, or with none its first `h2` or `h3`, through `site.title`.
49
+ - **Description:** the first paragraph after the `h1` that reads as prose, inside `<main>`, with lists, code, tables,
50
+ navigation and footers skipped, cleaned, and cut at a sentence by 155 characters. With none, `site.description`.
51
+ - **Share card:** `site.image` defaults to `'auto'`: a 1200×630 PNG of the title, description, app name and address,
52
+ in veneer's colours and the text plugin's faces. `site.card` sets them (`scheme: 'light'`, `accent: '#rrggbb'`).
53
+ `routes/x.og.tsx` replaces one route's card, `_default.og.tsx` every card; `og: { image: '/x.png' }` or `image: false`
54
+ skips it. The URL holds a hash of what it draws, so it's cached forever and changes when the page does.
55
+ - **`twitter:card`** is `summary_large_image` when there is an image. **Canonical and `og:url`** come from the origin.
56
+ - **Structured data:** a `WebSite` on `/` when `site.name` is set, and nothing else unless `jsonLd` sets it: kerf
57
+ doesn't guess.
58
+
59
+ Pages with a card are drawn by `kerf build` when they have no params or are in `prerender`. Every other param page's
60
+ card is drawn on the first request, and only for params the route's own `load` accepts. A static host has no server, so
61
+ there a param route needs `prerender` (H002).
62
+
63
+ ## The site's address
64
+
65
+ Absolute URLs need one: `site.url`, or `KERF_SITE_URL`, or what the host sets (`VERCEL_PROJECT_PRODUCTION_URL`,
66
+ `VERCEL_URL`, Netlify's `URL`, `CF_PAGES_URL`, `RENDER_EXTERNAL_URL`, `RAILWAY_PUBLIC_DOMAIN`, `FLY_APP_NAME`). With none, a
67
+ server uses the request's origin, which is only what its `Host` header said, and warns once in production (H001). A
68
+ prerendered page has no request, so it stays relative: crawlers don't resolve that, so set `site.url` before you ship.
69
+
70
+ ## See it, and fix it
71
+
72
+ `kerf check` reports metadata for every route, from what the routes really produce. Param routes are sampled from
73
+ `prerender` and from the links on rendered pages. Each diagnostic says what to write; H011's fix is a `meta` to start from.
74
+
75
+ | Code | What it says |
76
+ |---|---|
77
+ | H001 | no `site.url` |
78
+ | H002 | a param route's card, on a static host, with no `prerender` |
79
+ | H003, H004 | no description; a title over 60 or a description over 160 |
80
+ | H005, H008 | a card's style or image failed |
81
+ | H006, H007 | `meta` read a box or a deferred field |
82
+ | H011 | no `meta`, or a field left out: says what was derived |
83
+ | H012, H013 | two routes share a title or a description |
84
+ | H014 | a param route whose pages all look alike |
85
+ | H015 | a description that reads like code, markdown or a fragment |
86
+ | H016 | a route that couldn't be checked here (`check: { routes: false }` turns the pass off) |
87
+
88
+ `kerf routes --meta` is a table of each page: title, description, image and where each came from (`route`, `layout`,
89
+ `config`, `auto`, `card`). `--json` has every tag. It's the `routes_meta` tool of `kerf mcp`.
90
+
91
+ Never slice raw markdown or code into `description`: `body.slice(0, 155)` is how a page's card ends up reading
92
+ `- xs.map(x = <Row`. Write the sentence, or leave it out and let the page speak.
@@ -0,0 +1,166 @@
1
+ # How pay is built
2
+
3
+ A plugin is one pure manifest. `@kerf-js/pay` is the worked example: payments, with a webhook, a ledger, a fake for tests, its
4
+ own diagnostics and `kerf add` steps. It uses only what any plugin can use, and a test holds that to be true: `kerf`,
5
+ `@kerf-js/start` and the CLI don't name it. Read `packages/pay/plugin.mjs` next to this page. It's 50 lines.
6
+
7
+ ## The shape
8
+
9
+ ```
10
+ packages/pay/ packages/pay/ (`src/stripe.ts`)
11
+ plugin.mjs the manifest plugin.mjs `pay.rail` provider
12
+ src/index.ts client-safe src/index.ts stripeRail(options)
13
+ src/server.ts the ledger
14
+ src/test.ts fakePay, payRailSuite
15
+ src/checks.mjs PAY-codes
16
+ src/setup.mjs `kerf add pay`
17
+ docs/pay.md `kerf docs pay`
18
+ ```
19
+
20
+ Two packages, because they answer different questions. `@kerf-js/pay` is what payments means here, and it names no provider.
21
+ A rail is how a payment happens. `fakePay` is a second rail, which is how you know the seam is real.
22
+
23
+ ## 1. Start from `definePlugin`
24
+
25
+ ```js
26
+ export const pay = definePlugin({
27
+ name: '@kerf-js/pay',
28
+ version: '0.1.0', kerf: '*', namespace: 'PAY', permissions: {},
29
+ contribute: () => ({
30
+ provides: [...], diagnostics, check, docs, setup,
31
+ agents, catalog, map,
32
+ }),
33
+ })
34
+ ```
35
+
36
+ `kerf check` loads every plugin, so `plugin.mjs` stays pure and light: no I/O, nothing heavy imported. Heavy parts sit behind
37
+ `import()`. `namespace` is the prefix of your diagnostic codes, by default `<name>/`. `permissions` is what the plugin
38
+ reaches. Pay reaches nothing, so it's `{}`; the Stripe plugin declares `net: ['api.stripe.com']` and the two `env` keys it reads,
39
+ and `kerf add` shows them for approval (`kerf docs config` has the lock).
40
+
41
+ ## 2. Reach the server with a slot
42
+
43
+ A slot is a typed seam: the id, a version, keyed or one, and the sides it runs on. Providers name a module, which kerf
44
+ imports lazily, on that side only. That's why an app that doesn't use a plugin carries none of its bytes.
45
+
46
+ Pay provides `server.extension`, the slot for anything that needs a server presence:
47
+
48
+ ```js
49
+ provide(
50
+ defineSlot({
51
+ id: 'server.extension', version: 1,
52
+ cardinality: 'keyed', sides: ['server'],
53
+ }),
54
+ {
55
+ key: 'pay',
56
+ sides: { server: {
57
+ module: '@kerf-js/pay/server', export: 'extension',
58
+ } },
59
+ },
60
+ )
61
+ ```
62
+
63
+ The key is the mount: `/_kerf/x/pay/...`. A keyed provider also needs `src/<key>.server.ts`, the app's own configuration, and
64
+ kerf passes that module's default export to your provider. So `src/pay.server.ts` is `definePay({...})`, and the provider is
65
+ one line: `extension = app => app`. A `ServerExtension` (`@kerf-js/start/extension`) may have any of:
66
+
67
+ | Member | What it does | What pay uses it for |
68
+ | --- | --- | --- |
69
+ | `handle(request, path, api)` | Answers `/_kerf/x/<key>/<path>` | Webhooks; `api.invalidate(tags)` pokes live queries |
70
+ | `context(ctx)` | Adds to `ctx`, once per request | `ctx.entitlements` |
71
+ | `guard(fn, ctx)` | Runs before every server function | `requires: ['pro']`, a typed 403 |
72
+ | `options` | The `FnOptions` keys it enforces; with no extension, a call using one is refused | `requires` |
73
+ | `personal` | `ctx` keys a shared cache can't read | `entitlements` (Q006) |
74
+ | `check(dev)` | Refuses a bad boot | The in-memory ledger in production |
75
+
76
+ Types follow: augment `Context` and `FnOptions` in `declare module '@kerf-js/start'`, and `ctx.entitlements` and `requires`
77
+ are typed in any app that imports your server module. An error class with an `answer` getter reaches the client as a
78
+ typed error, with that code and status.
79
+
80
+ ## 3. Define a slot of your own
81
+
82
+ Pay's `pay.rail` slot is why Stripe is one line in `plugins`. Say what a provider must implement, as a TypeScript interface
83
+ (`PayRail`), and ship a suite that holds any provider to it (`payRailSuite`). Your consumer reads the slot like any other:
84
+
85
+ ```ts
86
+ const { load, providers } = await import('kerf:slot/pay.rail')
87
+ const rail = await load('stripe')
88
+ ```
89
+
90
+ Another provider means another `provide(...)`, and app code doesn't change. A plugin's own slot is named `<plugin name>.<slot>`;
91
+ `pay.rail` keeps its short name only because it was in kerf's slot table from the start. `@kerf-js/pay/stripe` is a short manifest,
92
+ and a good template for a provider.
93
+
94
+ ## 4. Report your own diagnostics
95
+
96
+ `diagnostics` declares each code: severity, what it catches, the fix. `check` finds them:
97
+
98
+ ```js
99
+ check: {
100
+ include: ['src/**/*.{ts,tsx}'],
101
+ run: ctx => import('./src/checks.mjs')
102
+ .then(c => c.run(ctx)),
103
+ }
104
+ ```
105
+
106
+ `run(ctx)` gets `files`, `read`, `exists`, `jsx`, `slots`, and returns findings: `{ code, file, start, end, why, help?, fix? }`.
107
+ A `fix` is `{ description, safe, edits: [{ start, end, text }] }`; `kerf fix` applies the safe ones. Kerf does the spans,
108
+ `--json` and `kerf docs PAY012`. A code outside your namespace is `P111`. A hook that throws is `P119` and nothing worse.
109
+
110
+ `src/checks.mjs` is the example to copy: a regex decides which files parse, TypeScript is imported only there, and the
111
+ checks that say the most are the ones a type can't: PAY013 (an entitlement read on the client that no server code names),
112
+ PAY011 (a webhook handler with no signature check). Test a hook as a plain function of a map of files
113
+ (`test/checks.test.mjs`).
114
+
115
+ ## 5. Give it a fake
116
+
117
+ A plugin that talks to the outside world ships the fake with it. `fakePay` is a real `PayRail` in memory, and reaches `@kerf-js/core/test`
118
+ only through what's public: `net.extension(pay)` puts your extension in the test's scope and resets it afterwards, `net.poke`
119
+ delivers tags to live queries, `clock` moves time. Events go through the app's real webhook and ledger, so a test
120
+ sees what a user sees. It's imported from `@kerf-js/pay/test`, since core can't depend on a plugin.
121
+
122
+ ## 6. Make `kerf add` do the wiring
123
+
124
+ `setup` is data. `kerf add pay` reads it after installing the package:
125
+
126
+ ```js
127
+ export const setup = {
128
+ with: ['@kerf-js/pay/stripe'], // added too
129
+ // Refused without changes until these are present.
130
+ needs: ['db', 'auth'],
131
+ run({ engine, layer, read }) {
132
+ return {
133
+ files: {...}, edits: {...},
134
+ env: { STRIPE_SECRET_KEY: 'sk_test_placeholder' },
135
+ next: ['...'],
136
+ }
137
+ },
138
+ }
139
+ ```
140
+
141
+ `run` describes and never writes, so a dry run and a refusal are free. A file that already exists stops it before anything
142
+ changes. `env` goes to `.env` and `.env.example`: use placeholders.
143
+
144
+ ## 7. Say what an agent needs
145
+
146
+ - `agents`: one paragraph for the generated `AGENTS.md`, 1 KB at most.
147
+ - `docs`: `{ topic, summary, file }`, served by `kerf docs <topic>`.
148
+ - `catalog`: your exports, `{ name, import, summary }`, listed by `kerf plugins --json`.
149
+ - `map`: "want this, call that" lines.
150
+ - `diagnostics`: each is a page: `kerf docs PAY012`.
151
+
152
+ ## Checklist
153
+
154
+ 1. `plugin.mjs` is pure, with heavy code behind `import()`.
155
+ 2. No app code names your provider; one line in `plugins` changes it.
156
+ 3. Client entries hold no server code. `packed.mjs` checks that an app without you has none of your bytes.
157
+ 4. A fake that goes through your real code path.
158
+ 5. A suite for providers, passing on your fake and each real provider.
159
+ 6. Every failure a type can't catch is a code with a fix.
160
+ 7. If the API lacks something you need, add it for everyone. Pay added `server.extension`, `setup`, plugin `docs`, `net.extension`
161
+ and the `FnOptions` interface that way.
162
+
163
+ ## Rust, and when not to
164
+
165
+ A plugin can carry its own wasm, with a TS fallback and nothing in client bytes. Measure first. `packages/pay/bench/verify.mjs`
166
+ did, for webhook verification, and pay stayed on WebCrypto, which was fast enough.
package/docs/props.md ADDED
@@ -0,0 +1,88 @@
1
+ # Props
2
+
3
+ ## Types
4
+
5
+ Leave a component's props unannotated. kerf infers them from its callers and its body, in the editor (the plugin in
6
+ `tsconfig.json`), `kerf check`, `kerf tsc` and `kerf dev`. Nothing is left at run time.
7
+
8
+ ```tsx
9
+ function Row(props) {
10
+ const { label, onPick } = props
11
+ return (
12
+ <li>
13
+ <button type="button" onClick={onPick}>
14
+ {label.toUpperCase()}
15
+ </button>
16
+ </li>
17
+ )
18
+ }
19
+ export default function List() {
20
+ return (
21
+ <ul>
22
+ <Row label="One" onPick={() => console.log('picked one')} />
23
+ </ul>
24
+ )
25
+ }
26
+ ```
27
+
28
+ | Case | What you get |
29
+ |---|---|
30
+ | Several callers | A union if the body handles it; if not, **W084** at the odd caller |
31
+ | Some callers omit a prop | Optional; a default value also types it |
32
+ | `<Button {...props} />` | `Button`'s props |
33
+ | A component in a variable, map or prop | Its sites count, and prop sets merge |
34
+ | Callers that differ by a string prop | A union of shapes the body can narrow |
35
+ | `src/routes` default export | `RouteProps` |
36
+ | An export | `kerf dev` or `kerf fix` writes the type with an `/*inferred*/` comment, so it shows in a diff. **W082** when callers move on; delete the comment to keep the type |
37
+ | An `any` from a caller | The body's use, or `any`, with **W083** naming where it came from |
38
+ | No caller | **W080**, with what to do |
39
+ | `bind:x={box}` | The box's value, and it satisfies a required `x` under `kerf tsc` and in the editor |
40
+
41
+ Write a type yourself when two props must agree (a type parameter), when the file is `.jsx`, or for a public API with no
42
+ caller in this repository. `Props<typeof Button>` names another component's props. `tsc` alone reports TS7006 on an
43
+ inferred parameter; run `kerf tsc` in CI. `check: { props: false }` turns this off; `'manual'` leaves writing to `kerf fix`.
44
+
45
+ ## Reactivity
46
+
47
+ The compiler turns a component's props into live reads, so a parent writing `count={count.value}` is correct.
48
+
49
+ | Form | Behaviour |
50
+ |---|---|
51
+ | `({ a, b }) =>` | Each name is a live read of `props.a` |
52
+ | `(props) =>` | `props.a` is a live getter |
53
+ | `({ size = 'md' }) =>` | Default applied on every read when the prop is `undefined` |
54
+ | `({ a, ...rest }) =>` | `rest` is a live object; `<div {...rest}>` stays live |
55
+ | `const x = props.y` in the body | `x` is another name for the prop: each use reads it live, at no cost |
56
+ | `const { a: b, c = 1, ...rest } = props` in the body | The same as in the parameter: renames, defaults and rest stay live |
57
+ | `const n = props.items.length` in the body | A path off a prop is live too |
58
+ | `({ user: { name } }) =>` | **W006**: nested destructuring can't stay live. Read `user.name` at the use site (autofix) |
59
+
60
+ - The local isn't a copy: every use, closures included, reads the prop as it is at that moment. For a value that must
61
+ stay as it was, write the snapshot out: `const first = untrack(() => props.x)`.
62
+ - Computing in the body runs once: `const n = items.length + 1`, `const a = props.a ?? 1` or `makeChart(items)` is
63
+ **W001**. Use `derived`, `watch`, JSX or a handler.
64
+ - Array patterns and computed keys (`const [a] = props.pair`, `const { [k]: a } = props`) are **W001**: read them at
65
+ the use site.
66
+ - `const` stays read-only: assigning to a `const` alias is **W001**, which says to use `let` or `props.x =`.
67
+ - `state(prop)` never follows the prop (**W005**). Use `state.linked(() => prop)` for an editable local copy.
68
+ - Pass a box (`value={coupon}`) only when the child must write it; otherwise pass `.value`.
69
+
70
+ ## Writing a prop
71
+
72
+ A component can write its own props. `let` makes a local writable; `+=`, `++` and `let { a } = props` work too.
73
+
74
+ | In the child | Parent passed `n={x}` | Parent passed `bind:n={box}` |
75
+ |---|---|---|
76
+ | Read `n` or `props.n` | Live | Live |
77
+ | `let n = props.n; n = 5` | Holds in the child until the parent's value next changes, then the parent wins | Writes `box` |
78
+ | `props.n = 5` | The same | The same: writes `box` |
79
+ | `const n = props.n; n = 5` | **W001**: use `let` or `props.n =` | **W001** |
80
+
81
+ `bind:` at the call site alone decides whether a write leaves the component. `<Card n={box.value} />` never writes
82
+ `box`, whatever `Card` does. It takes a box, on a component (**W061** otherwise). `kerf tsc` and the editor read
83
+ `bind:n={box}` as `n` with the box's value, so it satisfies a required prop and a wrong type is an error. Plain `tsc`
84
+ can't connect `bind:n` to `n` and reports the prop as missing: run `kerf tsc`. `n={bind(box)}` is the same binding in
85
+ a form plain `tsc` accepts. An island's props arrive as data, so a write there is always local.
86
+
87
+ Only a whole prop can be written this way. `let n = props.user.name; n = 1` is **W001**; `props.user.name = 1` writes
88
+ that object, as in any JavaScript.
@@ -0,0 +1,26 @@
1
+ # Reading reactive values
2
+
3
+ `state(initial)` returns a writable `State<T>`. Read or assign its `.value`.
4
+ `derived(fn)` returns a read-only `Derived<T>`. Read `.value` to evaluate it and
5
+ track its dependencies.
6
+
7
+ Use `memo(fn)` when a caller needs an accessor instead of a box:
8
+
9
+ ```ts
10
+ import { derived, memo, state } from '@kerf-js/core'
11
+
12
+ const count = state(1)
13
+ const doubled = derived(() => count.value * 2)
14
+ const readDoubled = memo(() => count.value * 2)
15
+
16
+ console.log(doubled.value, readDoubled()) // 2, 2
17
+ ```
18
+
19
+ Both forms cache their result and refresh it when a dependency changes. `memo`
20
+ returns a function, so call it; it has no `.value` property. The compiler also uses
21
+ this accessor form to cache component children.
22
+
23
+ For side effects, `watch(source, callback)` tracks reads in `source`. Keep the
24
+ source pure and give reactive work an owner with a component or `createRoot`.
25
+ Graph tracking, mount queues and renderer helpers belong to
26
+ `@kerf-js/core/internal`; application code uses the public lifecycle APIs.
package/docs/rules.md ADDED
@@ -0,0 +1,39 @@
1
+ ```tsx
2
+ import { derived } from '@kerf-js/core'
3
+
4
+ export function Cart({ items, taxRate }: {
5
+ items: { id: string; name: string; price: number }[]
6
+ taxRate: number
7
+ }) {
8
+ const subtotal = derived(() =>
9
+ items.reduce((s, i) => s + i.price, 0),
10
+ )
11
+ const total = derived(() => subtotal.value * (1 + taxRate))
12
+ return (
13
+ <section>
14
+ <ul>
15
+ {items.map(item => <li key={item.id}>{item.name}</li>)}
16
+ </ul>
17
+ <p>{total.value.toFixed(2)}</p>
18
+ </section>
19
+ )
20
+ }
21
+ ```
22
+
23
+ 1. **State is a box. Read and write `.value`**, including in JSX (W015).
24
+ 2. **Compute from other values with `derived`**, not `watch` or the component body (W001).
25
+ 3. **JSX is live.** Each `{expr}` and attribute re-evaluates when its inputs change.
26
+ 4. **Props are current.** Destructure one level (W006). Read in `derived`, `watch`, JSX or handlers.
27
+ `const x = props.y` is a live read-only alias; `let x = props.y` and `props.y =` write locally; only `bind:y={box}`
28
+ writes out.
29
+ 5. **The body runs once.** No state-based early returns (W040) or `let` counters (W041). Branch in JSX.
30
+ 6. **Pass a box only to give write access** (`value={coupon}`, child prop typed `State<string | null>`), or so list
31
+ rows can read one entry: `cart.get(p.id)` re-runs one row, `cart[p.id]` or `cart.value[p.id]` re-runs them all
32
+ (W017), in the list and in the row component alike.
33
+ 7. **Lists and conditionals**: `.map` with `key` (W010), `&&`, ternaries. Keep items
34
+ stable: replace only the changed item (`p.id === id ? { ...p, price } : p`), never `({ ...p })` every item
35
+ (W014).
36
+ 8. **Copy a prop into state with `state.linked(() => prop)`**, not `state(prop)` (W005). `untrack(() => …)` is a
37
+ snapshot that never updates: never render it (W018).
38
+ 9. **Side effects go in `watch(() => source, value => cleanup?)`**; DOM work in `onMount`. `flush()` applies pending
39
+ DOM updates now.