@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.
- package/LICENSE +16 -0
- package/README.md +554 -2
- package/bin/kerf.mjs +15 -0
- package/docs/async.md +50 -0
- package/docs/browser.md +53 -0
- package/docs/config.md +127 -0
- package/docs/core.md +30 -0
- package/docs/deploy.md +108 -0
- package/docs/design.md +106 -0
- package/docs/entrypoints.md +83 -0
- package/docs/fonts.md +118 -0
- package/docs/icons.md +108 -0
- package/docs/lists.md +25 -0
- package/docs/meta.md +92 -0
- package/docs/plugins.md +166 -0
- package/docs/props.md +88 -0
- package/docs/reactivity.md +26 -0
- package/docs/rules.md +39 -0
- package/docs/rust.md +253 -0
- package/docs/start-short.md +10 -0
- package/docs/start.md +642 -0
- package/docs/state.md +122 -0
- package/docs/style.md +82 -0
- package/docs/surface.md +158 -0
- package/docs/testing.md +81 -0
- package/docs/theme.md +263 -0
- package/docs/ui-state.md +105 -0
- package/docs/verify.md +61 -0
- package/package.json +47 -3
- package/src/add/config.mjs +93 -0
- package/src/add/errors.mjs +2 -0
- package/src/add/setup.mjs +88 -0
- package/src/add-rust.mjs +178 -0
- package/src/add.mjs +392 -0
- package/src/agents-md.mjs +240 -0
- package/src/alias.mjs +70 -0
- package/src/audit/page.mjs +313 -0
- package/src/audit/report.mjs +69 -0
- package/src/audit/utils.mjs +5 -0
- package/src/audit.mjs +319 -0
- package/src/browser/cli.mjs +53 -0
- package/src/browser/client.mjs +299 -0
- package/src/browser/commands.mjs +258 -0
- package/src/capture.mjs +21 -0
- package/src/catalog/audit.mjs +129 -0
- package/src/catalog/auth.mjs +40 -0
- package/src/catalog/compiler.mjs +337 -0
- package/src/catalog/contracts.mjs +18 -0
- package/src/catalog/database.mjs +52 -0
- package/src/catalog/deploy.mjs +117 -0
- package/src/catalog/design.mjs +159 -0
- package/src/catalog/forms.mjs +25 -0
- package/src/catalog/hydration.mjs +29 -0
- package/src/catalog/icons.mjs +24 -0
- package/src/catalog/media.mjs +163 -0
- package/src/catalog/metadata.mjs +116 -0
- package/src/catalog/plugins.mjs +228 -0
- package/src/catalog/realtime.mjs +195 -0
- package/src/catalog/rust.mjs +75 -0
- package/src/catalog/server-functions.mjs +100 -0
- package/src/catalog/state.mjs +39 -0
- package/src/catalog/style-groups.mjs +54 -0
- package/src/catalog/tests.mjs +127 -0
- package/src/catalog/text.mjs +200 -0
- package/src/catalog/ui-state.mjs +42 -0
- package/src/catalog.mjs +103 -0
- package/src/check/runtime.mjs +280 -0
- package/src/check.mjs +321 -0
- package/src/cli/authoring.mjs +213 -0
- package/src/cli/project.mjs +302 -0
- package/src/cli.mjs +340 -0
- package/src/config.d.ts +1 -0
- package/src/config.mjs +5 -0
- package/src/contributions.mjs +119 -0
- package/src/db-layer.mjs +41 -0
- package/src/deploy/cdn.mjs +142 -0
- package/src/deploy/files.mjs +266 -0
- package/src/deploy/fit.mjs +166 -0
- package/src/deploy/hosts.mjs +126 -0
- package/src/deploy/index/cli.mjs +20 -0
- package/src/deploy/index/environment.mjs +69 -0
- package/src/deploy/index/steps.mjs +87 -0
- package/src/deploy/index.mjs +387 -0
- package/src/deploy/needs.mjs +149 -0
- package/src/design-checks/accessible-names.mjs +62 -0
- package/src/design-checks/contrast.mjs +43 -0
- package/src/design-checks/csp.mjs +60 -0
- package/src/design-checks/files.mjs +59 -0
- package/src/design-checks/heading-links.mjs +61 -0
- package/src/design-checks/interaction.mjs +65 -0
- package/src/design-checks/jsx.mjs +67 -0
- package/src/design-checks/layout-values.mjs +68 -0
- package/src/design-checks/native-controls.mjs +48 -0
- package/src/design-checks/outline.mjs +39 -0
- package/src/design-checks/reporter.mjs +37 -0
- package/src/design-checks/rules.mjs +274 -0
- package/src/design-checks/values.mjs +262 -0
- package/src/design-checks.mjs +49 -0
- package/src/diag.mjs +98 -0
- package/src/docs.mjs +228 -0
- package/src/doctor.mjs +42 -0
- package/src/files.mjs +60 -0
- package/src/fix.mjs +48 -0
- package/src/gate/after.mjs +40 -0
- package/src/gate/before.mjs +54 -0
- package/src/gate/classify.mjs +72 -0
- package/src/gate/mutation.mjs +209 -0
- package/src/gate/resolve-cases.mjs +22 -0
- package/src/gate/result.mjs +63 -0
- package/src/gate/run.mjs +75 -0
- package/src/gate.mjs +178 -0
- package/src/index.mjs +9 -0
- package/src/infer/box-props.cjs +140 -0
- package/src/infer/core-boxes.cjs +46 -0
- package/src/infer/derived-writes.cjs +95 -0
- package/src/infer/engine/checks.cjs +193 -0
- package/src/infer/engine/flow.cjs +293 -0
- package/src/infer/engine/report.cjs +158 -0
- package/src/infer/engine/utils.cjs +15 -0
- package/src/infer/engine.cjs +265 -0
- package/src/infer/facts/sites.cjs +114 -0
- package/src/infer/facts.cjs +345 -0
- package/src/infer/hosts.cjs +161 -0
- package/src/infer/lsp.mjs +240 -0
- package/src/infer/session.cjs +161 -0
- package/src/infer/shadow.cjs +54 -0
- package/src/infer/worker.cjs +145 -0
- package/src/jsx-scan.mjs +128 -0
- package/src/kerf.mjs +98 -0
- package/src/load-config/schema.mjs +300 -0
- package/src/load-config.mjs +302 -0
- package/src/map.mjs +177 -0
- package/src/mcp/browser-tools.mjs +155 -0
- package/src/mcp.mjs +273 -0
- package/src/meta-table.mjs +63 -0
- package/src/mutate.mjs +224 -0
- package/src/plugin-checks.mjs +118 -0
- package/src/plugins.mjs +258 -0
- package/src/project.mjs +310 -0
- package/src/props-checks.mjs +64 -0
- package/src/props-listener.mjs +56 -0
- package/src/react-checks.mjs +67 -0
- package/src/server-state-checks.mjs +34 -0
- package/src/share-checks.mjs +158 -0
- package/src/site.mjs +46 -0
- package/src/slots.mjs +181 -0
- package/src/start/build.mjs +116 -0
- package/src/start/routes.mjs +38 -0
- package/src/start.mjs +394 -0
- package/src/state-checks/core-state.mjs +223 -0
- package/src/state-checks/modules.mjs +17 -0
- package/src/state-checks/scan.mjs +232 -0
- package/src/state-checks/server-graph.mjs +65 -0
- package/src/state-checks.mjs +189 -0
- package/src/style/find.mjs +121 -0
- package/src/style.mjs +327 -0
- package/src/surface-checks.mjs +384 -0
- package/src/tailwind.mjs +23 -0
- package/src/tasks.mjs +261 -0
- package/src/test-rules/context.mjs +109 -0
- package/src/test-rules/file-rules.mjs +279 -0
- package/src/test-rules/rule-helpers.mjs +244 -0
- package/src/test-rules/syntax.mjs +223 -0
- package/src/test-rules/test-rules.mjs +127 -0
- package/src/testdiff.mjs +219 -0
- package/src/tests.mjs +120 -0
- package/src/text-checks.mjs +332 -0
- package/src/tokens.mjs +33 -0
- package/src/trust/scan.mjs +95 -0
- package/src/trust.mjs +394 -0
- package/src/workspace.mjs +182 -0
- package/ts-plugin/context.js +57 -0
- package/ts-plugin/index.js +17 -0
- package/ts-plugin/mapping.js +65 -0
- package/ts-plugin/package.json +1 -0
- package/ts-plugin/proxy.js +144 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// W043 and W044 in `kerf check`. W043: a React habit that compiles and renders something else. `data-x={true}` is a literal, so it can
|
|
2
|
+
// be told from kerf's own `data-open={open}`, whose presence form `[data-open]` selectors depend on (docs/react-props.md).
|
|
3
|
+
|
|
4
|
+
import { diagnostic, positions } from './diag.mjs'
|
|
5
|
+
import { scanJsx } from './jsx-scan.mjs'
|
|
6
|
+
|
|
7
|
+
const NATIVE = /^[a-z][\w-]*$/
|
|
8
|
+
const LITERAL = /\bdata-[\w-]+\s*=\s*\{\s*(?:true|false)\s*\}/
|
|
9
|
+
const RANDOM = /\b(?:crypto\.randomUUID|Date\.now|Math\.random|performance\.now)\s*\(/
|
|
10
|
+
|
|
11
|
+
/** @param {{ files: { rel: string, text: string }[] }} options the checked .tsx/.jsx files */
|
|
12
|
+
export function reactChecks({ files }) {
|
|
13
|
+
const out = []
|
|
14
|
+
for (const { rel, text } of files) {
|
|
15
|
+
if (!LITERAL.test(text) && !RANDOM.test(text)) continue
|
|
16
|
+
const pos = positions(text)
|
|
17
|
+
for (const el of scanJsx(text, rel).elements) {
|
|
18
|
+
for (const a of el.attrs) {
|
|
19
|
+
if (a.name === 'key' && a.kind === 'expr' && RANDOM.test(a.expr))
|
|
20
|
+
out.push(
|
|
21
|
+
diagnostic({
|
|
22
|
+
code: 'W044',
|
|
23
|
+
file: rel,
|
|
24
|
+
span: pos.span(a.start, a.end),
|
|
25
|
+
offset: [a.start, a.end],
|
|
26
|
+
why: `\`key={${a.expr}}\` differs between the server and the client, so this row can't hydrate and is built again, and it differs on every render.`,
|
|
27
|
+
}),
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
if (!NATIVE.test(el.name)) continue
|
|
31
|
+
for (const a of el.attrs) {
|
|
32
|
+
if (
|
|
33
|
+
!a.name.startsWith('data-') ||
|
|
34
|
+
a.kind !== 'expr' ||
|
|
35
|
+
(a.expr !== 'true' && a.expr !== 'false')
|
|
36
|
+
)
|
|
37
|
+
continue
|
|
38
|
+
const on = a.expr === 'true'
|
|
39
|
+
// Removing the attribute takes the space before it too.
|
|
40
|
+
const [from, to] = on ? [a.start, a.end] : [text.slice(0, a.start).trimEnd().length, a.end]
|
|
41
|
+
const why = on
|
|
42
|
+
? `\`${a.name}={true}\` writes \`${a.name}=""\`, not React's \`"true"\`, so \`[${a.name}="true"]\` won't match. Write \`${a.name}\` for a presence flag, or \`${a.name}="true"\` for the word.`
|
|
43
|
+
: `\`${a.name}={false}\` writes no attribute, not React's \`"false"\`. Drop it, or write \`${a.name}="false"\` for the word.`
|
|
44
|
+
const fix = {
|
|
45
|
+
description: on
|
|
46
|
+
? `Write \`${a.name}\`, which renders the same`
|
|
47
|
+
: `Remove \`${a.name}\`, which renders the same`,
|
|
48
|
+
safe: true,
|
|
49
|
+
edits: [
|
|
50
|
+
{ file: rel, span: pos.span(from, to), offset: [from, to], text: on ? a.name : '' },
|
|
51
|
+
],
|
|
52
|
+
}
|
|
53
|
+
out.push(
|
|
54
|
+
diagnostic({
|
|
55
|
+
code: 'W043',
|
|
56
|
+
file: rel,
|
|
57
|
+
span: pos.span(a.start, a.end),
|
|
58
|
+
offset: [a.start, a.end],
|
|
59
|
+
why,
|
|
60
|
+
fix,
|
|
61
|
+
}),
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return out
|
|
67
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// W030 joins server reachability with proven core state calls at module evaluation.
|
|
2
|
+
import { diagnostic, positions } from './diag.mjs'
|
|
3
|
+
import { serverGraph } from './state-checks/server-graph.mjs'
|
|
4
|
+
import { coreState, moduleCalls } from './state-checks/core-state.mjs'
|
|
5
|
+
|
|
6
|
+
/** State created by a server-reachable module is shared across requests unless explicitly global. */
|
|
7
|
+
export function serverStateChecks(root, options) {
|
|
8
|
+
const graph = serverGraph(root, options)
|
|
9
|
+
if (!graph.size) return []
|
|
10
|
+
const resolve = coreState(root, graph)
|
|
11
|
+
const out = []
|
|
12
|
+
for (const [file, { sf, entry }] of graph) {
|
|
13
|
+
const pos = positions(sf.text)
|
|
14
|
+
moduleCalls(sf, (call) => {
|
|
15
|
+
if (!['state', 'linked'].includes(resolve(call.expression))) return
|
|
16
|
+
const start = call.getStart(sf),
|
|
17
|
+
end = call.end
|
|
18
|
+
out.push(
|
|
19
|
+
diagnostic({
|
|
20
|
+
code: 'W030',
|
|
21
|
+
severity: 'error',
|
|
22
|
+
file,
|
|
23
|
+
span: pos.span(start, end),
|
|
24
|
+
offset: [start, end],
|
|
25
|
+
why: `This module creates state reachable from server entry \`${entry}\`; requests share it.`,
|
|
26
|
+
help:
|
|
27
|
+
'Create state inside a request/component factory or a context owned by the request. ' +
|
|
28
|
+
'Use `state.global` only when sharing across requests is intended.',
|
|
29
|
+
}),
|
|
30
|
+
)
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
return out.sort((a, b) => a.file.localeCompare(b.file) || a.offset[0] - b.offset[0])
|
|
34
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// U001–U005 in `kerf check` (research 45 §10): `interaction({ share })` and `ui`. TypeScript's parser only, no type
|
|
2
|
+
// checking: what's read is the call's own text, and a name counts as an interaction row only if this file made it.
|
|
3
|
+
|
|
4
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
import ts from 'typescript'
|
|
7
|
+
import { diagnostic, positions } from './diag.mjs'
|
|
8
|
+
|
|
9
|
+
const UNSTABLE = /\b(?:Math\.random|Date\.now|crypto\.randomUUID|performance\.now)\b/
|
|
10
|
+
const USER_TYPED = /\b(?:location\.|URLSearchParams|useSearchParams|searchParams|params\.get)\b/
|
|
11
|
+
/** veneer components that take `ui`. Anything else would write `ui="[object Object]"` on its element. */
|
|
12
|
+
const TAKES_UI = new Set(['Button', 'Checkbox', 'Switch', 'Radio', 'Toggle', 'Chip', 'Card'])
|
|
13
|
+
|
|
14
|
+
const rowsOf = (sf) => {
|
|
15
|
+
const rows = new Set()
|
|
16
|
+
const visit = (n) => {
|
|
17
|
+
if (
|
|
18
|
+
ts.isVariableDeclaration(n) &&
|
|
19
|
+
ts.isIdentifier(n.name) &&
|
|
20
|
+
n.initializer &&
|
|
21
|
+
ts.isCallExpression(n.initializer) &&
|
|
22
|
+
n.initializer.expression.getText?.(sf) === 'interaction'
|
|
23
|
+
)
|
|
24
|
+
rows.add(n.name.text)
|
|
25
|
+
ts.forEachChild(n, visit)
|
|
26
|
+
}
|
|
27
|
+
visit(sf)
|
|
28
|
+
return rows
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const veneerNames = (sf) => {
|
|
32
|
+
const names = new Map()
|
|
33
|
+
for (const n of sf.statements) {
|
|
34
|
+
if (
|
|
35
|
+
!ts.isImportDeclaration(n) ||
|
|
36
|
+
!n.moduleSpecifier.text.startsWith('@kerf-js/veneer') ||
|
|
37
|
+
!n.importClause?.namedBindings ||
|
|
38
|
+
!ts.isNamedImports(n.importClause.namedBindings)
|
|
39
|
+
)
|
|
40
|
+
continue
|
|
41
|
+
for (const s of n.importClause.namedBindings.elements)
|
|
42
|
+
names.set(s.name.text, (s.propertyName ?? s.name).text)
|
|
43
|
+
}
|
|
44
|
+
return names
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function shareChecks(root, { files }) {
|
|
48
|
+
const out = []
|
|
49
|
+
let sharesIn
|
|
50
|
+
for (const { rel, text } of files) {
|
|
51
|
+
sharesIn = checkFile(rel, text, out, sharesIn)
|
|
52
|
+
}
|
|
53
|
+
if (sharesIn && existsSync(join(root, 'src/routes'))) {
|
|
54
|
+
const file = ['src/realtime.server.ts', 'src/realtime.server.js']
|
|
55
|
+
.map((f) => join(root, f))
|
|
56
|
+
.find(existsSync)
|
|
57
|
+
if (!file || !/\bshare\s*:/.test(readFileSync(file, 'utf8'))) {
|
|
58
|
+
out.push(
|
|
59
|
+
diagnostic({
|
|
60
|
+
code: 'U003',
|
|
61
|
+
file: sharesIn,
|
|
62
|
+
span: { start: { line: 1, column: 1 }, end: { line: 1, column: 1 } },
|
|
63
|
+
offset: [0, 0],
|
|
64
|
+
why: 'This app shares UI state and no `realtime({ share })` says who may: tabs work, and the socket refuses every key.',
|
|
65
|
+
}),
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return out
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function checkFile(rel, text, out, sharesIn) {
|
|
73
|
+
const usesRow = text.includes('@kerf-js/core/interaction')
|
|
74
|
+
if (!usesRow && !/\bui=\{/.test(text)) return sharesIn
|
|
75
|
+
const sf = ts.createSourceFile(
|
|
76
|
+
rel,
|
|
77
|
+
text,
|
|
78
|
+
ts.ScriptTarget.Latest,
|
|
79
|
+
true,
|
|
80
|
+
/\.[jt]sx$/.test(rel) ? ts.ScriptKind.TSX : ts.ScriptKind.TS,
|
|
81
|
+
)
|
|
82
|
+
const pos = positions(text)
|
|
83
|
+
const report = (code, n, why) =>
|
|
84
|
+
out.push(
|
|
85
|
+
diagnostic({
|
|
86
|
+
code,
|
|
87
|
+
file: rel,
|
|
88
|
+
span: pos.span(n.getStart(sf), n.end),
|
|
89
|
+
offset: [n.getStart(sf), n.end],
|
|
90
|
+
why,
|
|
91
|
+
}),
|
|
92
|
+
)
|
|
93
|
+
const rows = rowsOf(sf),
|
|
94
|
+
veneer = veneerNames(sf)
|
|
95
|
+
const markShare = () => {
|
|
96
|
+
sharesIn ??= rel
|
|
97
|
+
}
|
|
98
|
+
const visit = makeVisitor({ sf, usesRow, rows, veneer, report, markShare })
|
|
99
|
+
visit(sf)
|
|
100
|
+
return sharesIn
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function makeVisitor({ sf, usesRow, rows, veneer, report, markShare }) {
|
|
104
|
+
const visit = (n) => {
|
|
105
|
+
if (
|
|
106
|
+
ts.isCallExpression(n) &&
|
|
107
|
+
ts.isIdentifier(n.expression) &&
|
|
108
|
+
n.expression.text === 'interaction' &&
|
|
109
|
+
usesRow
|
|
110
|
+
) {
|
|
111
|
+
const share =
|
|
112
|
+
n.arguments[0] && ts.isObjectLiteralExpression(n.arguments[0])
|
|
113
|
+
? n.arguments[0].properties.find((p) => p.name?.getText(sf) === 'share')
|
|
114
|
+
: undefined
|
|
115
|
+
if (share) {
|
|
116
|
+
markShare()
|
|
117
|
+
const value = share.getText(sf)
|
|
118
|
+
if (UNSTABLE.test(value))
|
|
119
|
+
report(
|
|
120
|
+
'U001',
|
|
121
|
+
share,
|
|
122
|
+
'A `share` key that changes on every load never meets another tab or client.',
|
|
123
|
+
)
|
|
124
|
+
else if (USER_TYPED.test(value))
|
|
125
|
+
report(
|
|
126
|
+
'U002',
|
|
127
|
+
share,
|
|
128
|
+
'This `share` key comes from what the user typed or the URL, so `realtime({ share })` is authorising a string they chose.',
|
|
129
|
+
)
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (ts.isBinaryExpression(n) && n.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
|
|
133
|
+
const l = n.left
|
|
134
|
+
const target = ts.isPropertyAccessExpression(l) && l.name.text === 'value' ? l.expression : l
|
|
135
|
+
if (
|
|
136
|
+
ts.isPropertyAccessExpression(target) &&
|
|
137
|
+
target.name.text === 'remote' &&
|
|
138
|
+
ts.isIdentifier(target.expression) &&
|
|
139
|
+
rows.has(target.expression.text)
|
|
140
|
+
)
|
|
141
|
+
report('U004', n, '`remote` is what others are doing: only the network writes it.')
|
|
142
|
+
}
|
|
143
|
+
if (ts.isJsxSelfClosingElement(n) || ts.isJsxOpeningElement(n)) {
|
|
144
|
+
const tag = n.tagName.getText(sf),
|
|
145
|
+
ui = n.attributes.properties.find(
|
|
146
|
+
(a) => ts.isJsxAttribute(a) && a.name.getText(sf) === 'ui',
|
|
147
|
+
)
|
|
148
|
+
if (ui && veneer.has(tag) && !TAKES_UI.has(veneer.get(tag)))
|
|
149
|
+
report(
|
|
150
|
+
'U005',
|
|
151
|
+
ui,
|
|
152
|
+
`\`${veneer.get(tag)}\` doesn't take \`ui\` yet, so this writes an attribute and draws nothing.`,
|
|
153
|
+
)
|
|
154
|
+
}
|
|
155
|
+
ts.forEachChild(n, visit)
|
|
156
|
+
}
|
|
157
|
+
return visit
|
|
158
|
+
}
|
package/src/site.mjs
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Config-level page-metadata checks (research 20 §6). Without routing they see only `site`: the defaults every page
|
|
2
|
+
// starts from. Route metas (Phase 3) will run the same checks on each route's merged result.
|
|
3
|
+
|
|
4
|
+
const absolute = /^(https?:)?\/\//
|
|
5
|
+
|
|
6
|
+
/** @param {import('./config').SiteConfig} site @param {(code: string, why: string, near: string | null) => void} report */
|
|
7
|
+
export function checkSite(site, report) {
|
|
8
|
+
const image = typeof site.image === 'object' ? site.image.url : site.image
|
|
9
|
+
if (image && !site.url && !absolute.test(image)) {
|
|
10
|
+
const what =
|
|
11
|
+
image === 'auto'
|
|
12
|
+
? "The generated card (`image: 'auto'`) is served from a relative URL"
|
|
13
|
+
: `\`site.image\` (\`${image}\`) is relative`
|
|
14
|
+
report(
|
|
15
|
+
'H001',
|
|
16
|
+
`${what} and \`site.url\` is not set, so og:image (and canonical, og:url) can't be absolute. Crawlers don't resolve relative URLs.`,
|
|
17
|
+
'image',
|
|
18
|
+
)
|
|
19
|
+
}
|
|
20
|
+
if (!site.description?.trim())
|
|
21
|
+
report(
|
|
22
|
+
'H003',
|
|
23
|
+
'Pages have no description: `site.description` is not set and no route overrides it yet.',
|
|
24
|
+
'site',
|
|
25
|
+
)
|
|
26
|
+
// The default page title is `name`; a template's fixed part is in every other title.
|
|
27
|
+
const fixed = site.title ? site.title.replace('%s', '').length : 0
|
|
28
|
+
if (site.name && site.name.length > 60)
|
|
29
|
+
report(
|
|
30
|
+
'H004',
|
|
31
|
+
`The default title \`${site.name}\` is ${site.name.length} characters (over 60).`,
|
|
32
|
+
'name',
|
|
33
|
+
)
|
|
34
|
+
if (fixed > 40)
|
|
35
|
+
report(
|
|
36
|
+
'H004',
|
|
37
|
+
`The title template's fixed part is ${fixed} characters, leaving under 20 of 60 for the page title.`,
|
|
38
|
+
'title',
|
|
39
|
+
)
|
|
40
|
+
if (site.description && site.description.length > 160)
|
|
41
|
+
report(
|
|
42
|
+
'H004',
|
|
43
|
+
`\`site.description\` is ${site.description.length} characters (over 160).`,
|
|
44
|
+
'description',
|
|
45
|
+
)
|
|
46
|
+
}
|
package/src/slots.mjs
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
// Slot table (research 13 §2.2) and the binding rules, resolved from config data alone. Phase 1 executes no plugin.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Every slot a plugin can fill: how many providers it takes, where it runs, and what's used when none does.
|
|
5
|
+
* @type {{ id: string, version: number, cardinality: string, sides: string[], default: string | string[] | null, phase: string }[]}
|
|
6
|
+
*/
|
|
7
|
+
export const slotTable = [
|
|
8
|
+
{ id: 'font.build', version: 1, cardinality: 'one', sides: ['build'], default: null, phase: '2' },
|
|
9
|
+
{
|
|
10
|
+
id: 'media.build',
|
|
11
|
+
version: 1,
|
|
12
|
+
cardinality: 'one',
|
|
13
|
+
sides: ['build'],
|
|
14
|
+
default: null,
|
|
15
|
+
phase: '2',
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
id: 'media.encode',
|
|
19
|
+
version: 1,
|
|
20
|
+
cardinality: 'keyed',
|
|
21
|
+
sides: ['build', 'server'],
|
|
22
|
+
default: null,
|
|
23
|
+
phase: '3c',
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
id: 'media.server',
|
|
27
|
+
version: 1,
|
|
28
|
+
cardinality: 'one',
|
|
29
|
+
sides: ['server'],
|
|
30
|
+
default: null,
|
|
31
|
+
phase: '3',
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
id: 'text.measure',
|
|
35
|
+
version: 1,
|
|
36
|
+
cardinality: 'one',
|
|
37
|
+
sides: ['client', 'worker'],
|
|
38
|
+
default: null,
|
|
39
|
+
phase: '2 (Surfaces)',
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
id: 'surface.renderer',
|
|
43
|
+
version: 1,
|
|
44
|
+
cardinality: 'keyed',
|
|
45
|
+
sides: ['client'],
|
|
46
|
+
default: ['canvas2d', 'webgl2'],
|
|
47
|
+
phase: '2 (Surfaces)',
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
id: 'realtime.bus',
|
|
51
|
+
version: 1,
|
|
52
|
+
cardinality: 'one',
|
|
53
|
+
sides: ['server'],
|
|
54
|
+
default: 'memory',
|
|
55
|
+
phase: '3',
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
id: 'db.adapter',
|
|
59
|
+
version: 1,
|
|
60
|
+
cardinality: 'keyed',
|
|
61
|
+
sides: ['server'],
|
|
62
|
+
default: ['drizzle'],
|
|
63
|
+
phase: '3c',
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
id: 'server.extension',
|
|
67
|
+
version: 1,
|
|
68
|
+
cardinality: 'keyed',
|
|
69
|
+
sides: ['server'],
|
|
70
|
+
default: null,
|
|
71
|
+
phase: '6c',
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
id: 'pay.rail',
|
|
75
|
+
version: 1,
|
|
76
|
+
cardinality: 'keyed',
|
|
77
|
+
sides: ['server', 'client', 'native'],
|
|
78
|
+
default: null,
|
|
79
|
+
phase: '6c',
|
|
80
|
+
},
|
|
81
|
+
]
|
|
82
|
+
|
|
83
|
+
export function slotDef(id) {
|
|
84
|
+
const def = slotTable.find((s) => s.id === id)
|
|
85
|
+
if (def) return def
|
|
86
|
+
if (/^device\.[a-z][\w-]*$/.test(id))
|
|
87
|
+
return {
|
|
88
|
+
id,
|
|
89
|
+
version: 1,
|
|
90
|
+
cardinality: 'keyed',
|
|
91
|
+
sides: ['native', 'client'],
|
|
92
|
+
default: null,
|
|
93
|
+
phase: '6b',
|
|
94
|
+
}
|
|
95
|
+
return undefined
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @param {{ plugins?: Array<{ name: string, provides?: Array<string | { slot: string, key: string }> }>, slots?: Record<string, unknown> }} config
|
|
100
|
+
* @param {(code: string, why: string, near: string) => void} report
|
|
101
|
+
* @returns {Record<string, unknown>} the `slots` block of `kerf check --json`
|
|
102
|
+
*/
|
|
103
|
+
export function bindSlots(config, report) {
|
|
104
|
+
const plugins = config.plugins ?? []
|
|
105
|
+
const chosen = config.slots ?? {}
|
|
106
|
+
const names = new Set(plugins.map((p) => p.name))
|
|
107
|
+
|
|
108
|
+
/** slot id → key ('' for `one`) → provider names */
|
|
109
|
+
const offers = new Map()
|
|
110
|
+
for (const p of [...plugins].sort((a, b) => a.name.localeCompare(b.name))) {
|
|
111
|
+
for (const item of p.provides ?? []) {
|
|
112
|
+
const { slot, key = '' } = typeof item === 'string' ? { slot: item } : item
|
|
113
|
+
if (!slotDef(slot) && !slot.startsWith(`${p.name}.`)) {
|
|
114
|
+
report(
|
|
115
|
+
'P008',
|
|
116
|
+
`\`${p.name}\` provides \`${slot}\`, which is not a slot kerf defines.`,
|
|
117
|
+
p.name,
|
|
118
|
+
)
|
|
119
|
+
continue
|
|
120
|
+
}
|
|
121
|
+
const byKey = offers.get(slot) ?? new Map()
|
|
122
|
+
byKey.set(key, [...(byKey.get(key) ?? []), p.name])
|
|
123
|
+
offers.set(slot, byKey)
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
for (const [id, pick] of Object.entries(chosen)) {
|
|
128
|
+
if (!slotDef(id) && !offers.has(id)) {
|
|
129
|
+
report('P008', `\`slots\` names \`${id}\`, which is not a slot kerf defines.`, id)
|
|
130
|
+
continue
|
|
131
|
+
}
|
|
132
|
+
const picks = typeof pick === 'string' ? { '': pick } : pick
|
|
133
|
+
for (const [key, name] of Object.entries(picks)) {
|
|
134
|
+
if (name.startsWith('kerf:default')) continue
|
|
135
|
+
if (!names.has(name))
|
|
136
|
+
report('P009', `\`slots['${id}']\` picks \`${name}\`, which is not in \`plugins\`.`, id)
|
|
137
|
+
else if (!(offers.get(id)?.get(key) ?? []).includes(name))
|
|
138
|
+
report('P009', `\`slots['${id}']\` picks \`${name}\`, which does not provide it.`, id)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** @type {Set<string>} */
|
|
143
|
+
const known = new Set([...slotTable.map((s) => s.id), ...offers.keys()])
|
|
144
|
+
const ids = [...known].sort()
|
|
145
|
+
const out = {}
|
|
146
|
+
for (const id of ids) {
|
|
147
|
+
const def = slotDef(id) ?? { cardinality: 'one', default: null }
|
|
148
|
+
const byKey = offers.get(id) ?? new Map()
|
|
149
|
+
const pick = chosen[id]
|
|
150
|
+
const resolve = (key, fallback) => {
|
|
151
|
+
const providers = byKey.get(key) ?? []
|
|
152
|
+
const named = typeof pick === 'string' ? (key === '' ? pick : undefined) : pick?.[key]
|
|
153
|
+
if (named && (named.startsWith('kerf:default') || providers.includes(named))) return named
|
|
154
|
+
if (providers.length === 1) return providers[0]
|
|
155
|
+
if (providers.length > 1) {
|
|
156
|
+
const where = key ? `\`${id}\` key \`${key}\`` : `\`${id}\``
|
|
157
|
+
report(
|
|
158
|
+
'P100',
|
|
159
|
+
`${where} has ${providers.length} providers: ${providers.join(', ')}. Array order never decides.`,
|
|
160
|
+
providers[0],
|
|
161
|
+
)
|
|
162
|
+
return null
|
|
163
|
+
}
|
|
164
|
+
return fallback
|
|
165
|
+
}
|
|
166
|
+
if (def.cardinality === 'one') {
|
|
167
|
+
out[id] = resolve('', typeof def.default === 'string' ? `kerf:default:${def.default}` : null)
|
|
168
|
+
} else {
|
|
169
|
+
/** @type {Set<string>} */
|
|
170
|
+
const keys = new Set([
|
|
171
|
+
...(Array.isArray(def.default) ? def.default : []),
|
|
172
|
+
...[...byKey.keys()].filter(Boolean),
|
|
173
|
+
])
|
|
174
|
+
const block = {}
|
|
175
|
+
for (const key of [...keys].sort())
|
|
176
|
+
block[key] = resolve(key, def.default?.includes(key) ? 'kerf:default' : null)
|
|
177
|
+
out[id] = block
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return out
|
|
181
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { audit, builtPublic } from '../audit.mjs'
|
|
2
|
+
import { writeMap } from '../map.mjs'
|
|
3
|
+
import { diagnostic } from '../diag.mjs'
|
|
4
|
+
import { loadConfig } from '../load-config.mjs'
|
|
5
|
+
import { host, presetFor } from '../deploy/hosts.mjs'
|
|
6
|
+
|
|
7
|
+
export async function finishBuild({
|
|
8
|
+
root,
|
|
9
|
+
config,
|
|
10
|
+
preset,
|
|
11
|
+
rust,
|
|
12
|
+
refuse,
|
|
13
|
+
running,
|
|
14
|
+
shipSidecars,
|
|
15
|
+
output,
|
|
16
|
+
warned,
|
|
17
|
+
unsafe,
|
|
18
|
+
forced,
|
|
19
|
+
loaded,
|
|
20
|
+
t,
|
|
21
|
+
writeContract,
|
|
22
|
+
}) {
|
|
23
|
+
const contract = await writeContract({
|
|
24
|
+
root,
|
|
25
|
+
site: config.site,
|
|
26
|
+
preset,
|
|
27
|
+
rust,
|
|
28
|
+
live: (await loadConfig(root)).config?.live,
|
|
29
|
+
verify: true,
|
|
30
|
+
})
|
|
31
|
+
for (const refused of contract.diagnostics.filter((d) => d.severity === 'error')) refuse(refused)
|
|
32
|
+
if (running) shipSidecars(root)
|
|
33
|
+
await writeMap(root)
|
|
34
|
+
// Last, the pages as built, in a clean Chrome at a phone's width and a desktop's (audit.mjs): an error fails the build.
|
|
35
|
+
const settings = (await loadConfig(root)).config?.build?.audit ?? true
|
|
36
|
+
const audited =
|
|
37
|
+
settings === false
|
|
38
|
+
? {
|
|
39
|
+
diagnostics: [
|
|
40
|
+
diagnostic({
|
|
41
|
+
code: 'L000',
|
|
42
|
+
severity: 'warning',
|
|
43
|
+
file: 'kerf.config.ts',
|
|
44
|
+
why: '`build.audit` is false: the built pages were not audited.',
|
|
45
|
+
}),
|
|
46
|
+
],
|
|
47
|
+
}
|
|
48
|
+
: await audit(root, {
|
|
49
|
+
dir: builtPublic(root),
|
|
50
|
+
...(typeof settings === 'object' && settings),
|
|
51
|
+
})
|
|
52
|
+
const pages = audited.diagnostics.map((d) =>
|
|
53
|
+
d.severity === 'error' && unsafe.allows(d) ? unsafe.let(d) : d,
|
|
54
|
+
)
|
|
55
|
+
const kept = [
|
|
56
|
+
...contract.diagnostics.filter((d) => !(d.severity === 'error' && unsafe.allows(d))),
|
|
57
|
+
...warned,
|
|
58
|
+
].map((d) => diagnostic({ file: '.kerf/contract.json', ...d }))
|
|
59
|
+
const diagnostics = [
|
|
60
|
+
...kept,
|
|
61
|
+
...pages,
|
|
62
|
+
...forced.map((d) => diagnostic({ file: '.kerf/contract.json', ...d })),
|
|
63
|
+
]
|
|
64
|
+
return {
|
|
65
|
+
ok: !diagnostics.some((d) => d.severity === 'error'),
|
|
66
|
+
preset,
|
|
67
|
+
output,
|
|
68
|
+
contract: contract.path,
|
|
69
|
+
functions: contract.functions,
|
|
70
|
+
notes: loaded.contributions.flatMap((c) => c.manifest.notes ?? []),
|
|
71
|
+
audited: audited.pages?.length ?? 0,
|
|
72
|
+
diagnostics,
|
|
73
|
+
ms: Math.round(performance.now() - t),
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export async function startBuildSidecars({
|
|
78
|
+
start,
|
|
79
|
+
root,
|
|
80
|
+
config,
|
|
81
|
+
preset,
|
|
82
|
+
rust,
|
|
83
|
+
refuse,
|
|
84
|
+
writeContract,
|
|
85
|
+
}) {
|
|
86
|
+
const { buildSidecars, shipSidecars, startSidecars, usesRemote } = await start(root, 'sidecar')
|
|
87
|
+
const contracted = () => writeContract({ root, site: config.site, preset, rust })
|
|
88
|
+
let warned = []
|
|
89
|
+
// Sidecars first: prerendering a share card runs its route's `load`, which may read one.
|
|
90
|
+
const running = usesRemote(root)
|
|
91
|
+
? await contracted().then(async (c) => {
|
|
92
|
+
const stop = c.diagnostics.find((d) => d.severity === 'error')
|
|
93
|
+
if (stop) refuse(stop)
|
|
94
|
+
const built = await buildSidecars({ root, contract: c.contract, rust, preset })
|
|
95
|
+
const failed = built.diagnostics.find((d) => d.severity === 'error')
|
|
96
|
+
if (failed) refuse(failed)
|
|
97
|
+
warned = built.diagnostics
|
|
98
|
+
return startSidecars({ root, built: true, log: () => {} })
|
|
99
|
+
})
|
|
100
|
+
: undefined
|
|
101
|
+
return { running, warned, shipSidecars }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export async function buildPreset(root, { preset, runtime, hostRuntime, PRESETS }) {
|
|
105
|
+
const chosen = runtime ?? (await loadConfig(root)).config?.runtime ?? hostRuntime()
|
|
106
|
+
// `deploy.target` picks the preset when nothing else does: a build for Fly is the runtime's server, for Netlify `netlify`
|
|
107
|
+
const target = !preset && !runtime && (await loadConfig(root)).config?.deploy?.target
|
|
108
|
+
if (target) preset = presetFor(host(target), chosen)
|
|
109
|
+
if (!preset && !PRESETS[chosen])
|
|
110
|
+
throw Object.assign(
|
|
111
|
+
new Error(`unknown runtime "${chosen}": use ${Object.keys(PRESETS).join(', ')}`),
|
|
112
|
+
{ code: 'E_USAGE' },
|
|
113
|
+
)
|
|
114
|
+
preset ??= PRESETS[chosen]
|
|
115
|
+
return preset
|
|
116
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
import { diagnostic, locate, positions } from '../diag.mjs'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Route findings as diagnostics, pointing at the `meta` export, or at where one goes. H011's fix is that export, as an
|
|
7
|
+
* edit an agent can apply: not safe, since the words are a start and not the page's best.
|
|
8
|
+
*/
|
|
9
|
+
export function routeDiagnostics(root, found) {
|
|
10
|
+
const text = (f) => {
|
|
11
|
+
try {
|
|
12
|
+
return readFileSync(join(root, f), 'utf8')
|
|
13
|
+
} catch {
|
|
14
|
+
return ''
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return found.map(({ code, severity, file, route, why, help }) => {
|
|
18
|
+
const source = text(file)
|
|
19
|
+
const at =
|
|
20
|
+
code === 'H011' &&
|
|
21
|
+
help?.startsWith('export const') &&
|
|
22
|
+
!/\bexport\s+(?:const|function|async function)\s+meta\b/.test(source)
|
|
23
|
+
? source.indexOf('export default')
|
|
24
|
+
: -1
|
|
25
|
+
const place = positions(source)
|
|
26
|
+
const fix = at >= 0 && {
|
|
27
|
+
fix: {
|
|
28
|
+
safe: false,
|
|
29
|
+
description: 'Add `meta`',
|
|
30
|
+
edits: [{ file, span: place.span(at, at), offset: [at, at], text: `${help}\n\n` }],
|
|
31
|
+
},
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
...diagnostic({ code, severity, file, why, help, ...locate(source, 'meta'), ...fix }),
|
|
35
|
+
route,
|
|
36
|
+
}
|
|
37
|
+
})
|
|
38
|
+
}
|