@kerf-js/cli 0.1.2-nightly.20261118.85 → 0.1.3
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/docs/config.md +1 -1
- package/docs/start.md +22 -0
- package/docs/testing.md +3 -0
- package/package.json +3 -3
- package/src/audit/routes.mjs +64 -0
- package/src/audit/server.mjs +143 -0
- package/src/audit.mjs +40 -54
- package/src/catalog/audit.mjs +2 -2
- package/src/cli.mjs +2 -1
- package/src/start/build.mjs +21 -2
package/docs/config.md
CHANGED
|
@@ -67,7 +67,7 @@ export default defineConfig({
|
|
|
67
67
|
| `site.mutationOrigins` | Additional origins allowed by Q010 for Kerf mutations: exact origin list or `'*'`. Default same origin. Does not set CORS headers or protect raw HTTP handlers |
|
|
68
68
|
| `site.cors` | `{ origins: ['https://page.example'], credentials?: true, headers?: ['x-agorix-grant'] }`, or `{ origins: '*' }`. Browser access to Kerf function/data endpoints; wildcard with credentials is refused (P007) |
|
|
69
69
|
| `site.security` | Headers every Start response gets unless it sets its own (`nosniff`, `Referrer-Policy`, `X-Frame-Options: SAMEORIGIN`, COOP, HSTS on https). Name a header to change it, `false` to drop it, or `security: false` for none |
|
|
70
|
-
| `site.trustProxy` | `true` or a
|
|
70
|
+
| `site.trustProxy` | `true` (one proxy) or a positive integer: trusted hops in front of the server. The client's address comes from that many entries from the right of `X-Forwarded-For`. Mutation/form CSRF uses the same hop from `X-Forwarded-Proto` (`http`/`https`) and optional `X-Forwarded-Host`; both lists must align and cover every configured hop. Without forwarded Host, preserve the public Host. Proxies must overwrite or append these headers. Malformed/short chains and forwarding an HTTPS request as HTTP are refused; direct requests without forwarding work as before. Leave trust off with nothing in front |
|
|
71
71
|
| `site.log` | `'text'` or `'json'`. Defaults to text in development and JSON lines in production. Records include `time`, `level`, `code` and `message`, plus `route`, `requestId` and `reference` when available. |
|
|
72
72
|
| `site.limits` | `{ body, upload }` in bytes: the largest JSON (1 MB) and multipart (25 MB) request body. Past it, 413 (Q018) |
|
|
73
73
|
| `runtime` | `node`, `bun` or `deno`: what `kerf build` targets (`kerf docs start`) |
|
package/docs/start.md
CHANGED
|
@@ -887,6 +887,28 @@ token. A token can't be revoked, so reads last until it expires; a mutation also
|
|
|
887
887
|
With no `BETTER_AUTH_URL` or fixed `baseURL`, no token is accepted, since nothing could check `iss` and `aud`.
|
|
888
888
|
`api: false` removes all of this.
|
|
889
889
|
|
|
890
|
+
## Reproducible builds
|
|
891
|
+
|
|
892
|
+
Set `SOURCE_DATE_EPOCH` to a non-negative Unix timestamp in seconds,
|
|
893
|
+
such as the source commit's timestamp.
|
|
894
|
+
|
|
895
|
+
```sh
|
|
896
|
+
SOURCE_DATE_EPOCH="$(git log -1 --format=%ct)" kerf build
|
|
897
|
+
```
|
|
898
|
+
|
|
899
|
+
With the same source, lockfile, toolchain, build configuration and platform,
|
|
900
|
+
Start emits the same public and server bytes across checkout paths and cache
|
|
901
|
+
states. Nitro's build date and static assets' `Last-Modified` use the epoch.
|
|
902
|
+
Asset `ETag` values still come from the bytes served. With the variable unset,
|
|
903
|
+
Nitro uses the build clock and filesystem mtimes as usual.
|
|
904
|
+
|
|
905
|
+
Pinned builds omit cached stylesheets from previous releases. Keep those
|
|
906
|
+
assets on the host if older pages still need them. Source maps, when
|
|
907
|
+
requested, use root-relative source identifiers and omit embedded source
|
|
908
|
+
text. Reproducibility also requires deterministic app and plugin inputs,
|
|
909
|
+
including prerendered data. Different operating systems, architectures,
|
|
910
|
+
native binaries, or toolchain versions can produce different bytes.
|
|
911
|
+
|
|
890
912
|
## Runtimes
|
|
891
913
|
|
|
892
914
|
The CLI runs on whichever of the three starts it, with none of the others installed: `bun --bun`, or `deno run -A
|
package/docs/testing.md
CHANGED
|
@@ -7,6 +7,9 @@ npm install -D vitest jsdom @testing-library/dom \
|
|
|
7
7
|
@testing-library/user-event
|
|
8
8
|
```
|
|
9
9
|
|
|
10
|
+
`@kerf-js/core/test` needs Vitest 5 or later: its matcher types extend Vitest 5's `Assertion<R, T>`, which Vitest 4
|
|
11
|
+
declares with one type parameter, so they don't compile against Vitest 4.
|
|
12
|
+
|
|
10
13
|
<!-- example: skip @kerf-js/core/vite has no type declarations yet (TS7016 under strict) -->
|
|
11
14
|
```ts
|
|
12
15
|
// vitest.config.ts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kerf-js/cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "The kerf command: dev, build, check, the agent layer and the MCP server",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -24,14 +24,14 @@
|
|
|
24
24
|
"node": ">=24"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"create-kerf": "0.1.
|
|
27
|
+
"create-kerf": "0.1.3",
|
|
28
28
|
"axe-core": "4.14.0",
|
|
29
29
|
"typescript": "6.0.3",
|
|
30
30
|
"yaml": "2.9.1",
|
|
31
31
|
"zod": "4.6.7"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@kerf-js/core": "^0.1.
|
|
34
|
+
"@kerf-js/core": "^0.1.3"
|
|
35
35
|
},
|
|
36
36
|
"publishConfig": {
|
|
37
37
|
"access": "public"
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// The app's page routes as `src/routes` lays them out, for the audit: which paths the app answers, and which pages a
|
|
2
|
+
// server renders with no parameters to fill in.
|
|
3
|
+
|
|
4
|
+
import { existsSync, readdirSync } from 'node:fs'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
|
|
7
|
+
/** Each route file under `src/routes`, as its path (`[id]` and `[...rest]` left in) and whether it is a page. */
|
|
8
|
+
function routeFiles(root) {
|
|
9
|
+
const dir = join(root, 'src', 'routes')
|
|
10
|
+
const out = []
|
|
11
|
+
if (!existsSync(dir)) return out
|
|
12
|
+
const walk = (at, prefix) => {
|
|
13
|
+
for (const e of readdirSync(at, { withFileTypes: true })) {
|
|
14
|
+
const name = e.name.replace(/^\(.*\)$/, '')
|
|
15
|
+
if (e.isDirectory()) {
|
|
16
|
+
walk(join(at, e.name), name ? `${prefix}/${name}` : prefix)
|
|
17
|
+
continue
|
|
18
|
+
}
|
|
19
|
+
if (
|
|
20
|
+
!/\.[jt]sx?$/.test(e.name) ||
|
|
21
|
+
/\.(server|test|spec)\./.test(e.name) ||
|
|
22
|
+
e.name.startsWith('_')
|
|
23
|
+
)
|
|
24
|
+
continue
|
|
25
|
+
const leaf = e.name.replace(/\.[jt]sx?$/, '')
|
|
26
|
+
const path = leaf === 'index' ? prefix : `${prefix}/${leaf}`
|
|
27
|
+
// the root 404 is the not-found page, and `.http`/`.og` files answer requests without being pages
|
|
28
|
+
const page = !(prefix === '' && leaf === '404') && !/\.(http|og)$/.test(leaf)
|
|
29
|
+
out.push({ path, page })
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
walk(dir, '')
|
|
33
|
+
return out
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The app's own pages as a test on a path (`src/routes`, `[id]` and `[...rest]` included). A build served as files has
|
|
38
|
+
* only its prerendered pages: a request for one of the others is the server's to answer, not a missing file.
|
|
39
|
+
*/
|
|
40
|
+
export function appRoutes(root) {
|
|
41
|
+
const tests = routeFiles(root).map(({ path }) => {
|
|
42
|
+
const re = path
|
|
43
|
+
.split('/')
|
|
44
|
+
.map((seg) =>
|
|
45
|
+
/^\[\.\.\.\w+\]$/.test(seg)
|
|
46
|
+
? '\0'
|
|
47
|
+
: /^\[\w+\]$/.test(seg)
|
|
48
|
+
? '[^/]+'
|
|
49
|
+
: seg.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'),
|
|
50
|
+
)
|
|
51
|
+
.join('/')
|
|
52
|
+
.replace(/\/\0/g, '(?:/.+)?')
|
|
53
|
+
return new RegExp(`^${re || ''}/?$`)
|
|
54
|
+
})
|
|
55
|
+
return (path) => tests.some((t) => t.test(path))
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The app's pages a server answers with no parameters to fill in: `/`, `/about`, not `/docs/[slug]`. */
|
|
59
|
+
export function staticRoutes(root) {
|
|
60
|
+
const paths = routeFiles(root)
|
|
61
|
+
.filter((r) => r.page && !r.path.includes('['))
|
|
62
|
+
.map((r) => r.path || '/')
|
|
63
|
+
return [...new Set(paths)].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0))
|
|
64
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// A server-rendered build for the audit: its pages render on request, so it prerenders none (only its share cards),
|
|
2
|
+
// and the audit runs the built server the way `kerf start` does and reads its pages from there.
|
|
3
|
+
|
|
4
|
+
import { spawn } from 'node:child_process'
|
|
5
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
6
|
+
import { createServer } from 'node:net'
|
|
7
|
+
import { join } from 'node:path'
|
|
8
|
+
import { parseEnv } from 'node:util'
|
|
9
|
+
import { diagnostic } from '../diag.mjs'
|
|
10
|
+
import { staticRoutes } from './routes.mjs'
|
|
11
|
+
import { sleep } from './utils.mjs'
|
|
12
|
+
|
|
13
|
+
/** The runtime that runs each Nitro server preset Kerf builds (`PRESETS` in start.mjs). */
|
|
14
|
+
const RUNTIMES = [
|
|
15
|
+
{ preset: /^node/, name: 'node', flags: ['--disable-warning=ExperimentalWarning'] },
|
|
16
|
+
{ preset: /^bun/, name: 'bun', flags: ['run'] },
|
|
17
|
+
{ preset: /^deno/, name: 'deno', flags: ['run', '-A'] },
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The server a build wrote to `.output`, when this machine can run it: its preset and the command. A host's function
|
|
22
|
+
* (Netlify, Vercel, Cloudflare) has a preset and no server to start here, so `run` is null.
|
|
23
|
+
* @returns {{ preset: string, run: [string, string[]] | null } | null}
|
|
24
|
+
*/
|
|
25
|
+
export function builtServer(root) {
|
|
26
|
+
const file = join(root, '.output', 'nitro.json')
|
|
27
|
+
if (!existsSync(file)) return null
|
|
28
|
+
let built
|
|
29
|
+
try {
|
|
30
|
+
built = JSON.parse(readFileSync(file, 'utf8'))
|
|
31
|
+
} catch {
|
|
32
|
+
return null
|
|
33
|
+
}
|
|
34
|
+
const preset = typeof built.preset === 'string' ? built.preset : ''
|
|
35
|
+
const entry = typeof built.serverEntry === 'string' && join(root, '.output', built.serverEntry)
|
|
36
|
+
const runtime = RUNTIMES.find((r) => r.preset.test(preset))
|
|
37
|
+
if (!entry || !existsSync(entry) || !runtime) return { preset, run: null }
|
|
38
|
+
const here = process.versions.deno ? 'deno' : process.versions.bun ? 'bun' : 'node'
|
|
39
|
+
const cmd = runtime.name === here ? process.execPath : runtime.name
|
|
40
|
+
return { preset, run: [cmd, [...runtime.flags, entry]] }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const freePort = () =>
|
|
44
|
+
new Promise((ok, no) => {
|
|
45
|
+
const probe = createServer()
|
|
46
|
+
probe.once('error', no)
|
|
47
|
+
probe.listen(0, '127.0.0.1', () => {
|
|
48
|
+
const { port } = /** @type {import('node:net').AddressInfo} */ (probe.address())
|
|
49
|
+
probe.close(() => ok(port))
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The built server on a free local port, with `.env` under what's already set (as `kerf start` runs it). Resolves
|
|
55
|
+
* once it answers, or with why it didn't.
|
|
56
|
+
*/
|
|
57
|
+
export async function runBuiltServer(root, [cmd, args], { timeout = 20_000 } = {}) {
|
|
58
|
+
const port = String(await freePort())
|
|
59
|
+
const file = join(root, '.env')
|
|
60
|
+
const env = {
|
|
61
|
+
...(existsSync(file) ? parseEnv(readFileSync(file, 'utf8')) : {}),
|
|
62
|
+
...process.env,
|
|
63
|
+
...{ PORT: port, HOST: '127.0.0.1', NITRO_PORT: port, NITRO_HOST: '127.0.0.1' },
|
|
64
|
+
}
|
|
65
|
+
const child = spawn(cmd, args, { cwd: root, env, stdio: ['ignore', 'ignore', 'pipe'] })
|
|
66
|
+
let stderr = ''
|
|
67
|
+
child.stderr.on('data', (c) => (stderr = (stderr + c).slice(-2000)))
|
|
68
|
+
let failed = ''
|
|
69
|
+
child.once('error', (e) => (failed = e.message))
|
|
70
|
+
const url = `http://127.0.0.1:${port}`
|
|
71
|
+
const close = () => child.kill()
|
|
72
|
+
for (const end = Date.now() + timeout; Date.now() < end; await sleep(100)) {
|
|
73
|
+
if (failed || child.exitCode !== null || child.signalCode !== null) break
|
|
74
|
+
try {
|
|
75
|
+
await fetch(`${url}/`, { method: 'HEAD', signal: AbortSignal.timeout(2000) })
|
|
76
|
+
return { url, close }
|
|
77
|
+
} catch {}
|
|
78
|
+
}
|
|
79
|
+
close()
|
|
80
|
+
const said = stderr.trim().split('\n').filter(Boolean).at(-1)
|
|
81
|
+
const why =
|
|
82
|
+
failed ||
|
|
83
|
+
(child.exitCode === null ? 'it did not answer' : `it exited with code ${child.exitCode}`)
|
|
84
|
+
return { error: said ? `${why} (${said.slice(0, 300)})` : why }
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The status each route answers with, 0 for none. */
|
|
88
|
+
const statusOf = (url) =>
|
|
89
|
+
fetch(url, { signal: AbortSignal.timeout(15_000) }).then(
|
|
90
|
+
(r) =>
|
|
91
|
+
r.arrayBuffer().then(
|
|
92
|
+
() => r.status,
|
|
93
|
+
() => r.status,
|
|
94
|
+
),
|
|
95
|
+
() => 0,
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The built server, started, and the pages to audit on it (`routes`, else `pick` of the app's static pages); or `skip`,
|
|
100
|
+
* why there is nothing to audit. A page that needs
|
|
101
|
+
* what the build's environment lacks (a database, a secret) answers 5xx here and not in production: it is left out and
|
|
102
|
+
* named, not failed.
|
|
103
|
+
*/
|
|
104
|
+
export async function serveBuild(root, dir, { routes, pick }) {
|
|
105
|
+
const built = builtServer(root)
|
|
106
|
+
if (!built)
|
|
107
|
+
return {
|
|
108
|
+
skip: dir
|
|
109
|
+
? `No prerendered pages in ${dir} and no built server to audit: pass \`--url\` to audit a running site.`
|
|
110
|
+
: 'No build to audit: run `kerf build` first, or pass `--url`.',
|
|
111
|
+
}
|
|
112
|
+
if (!built.run)
|
|
113
|
+
return {
|
|
114
|
+
skip: `The pages render on request, and the \`${built.preset}\` build's server doesn't run on this machine: \`kerf audit --url <deploy>\` audits it running.`,
|
|
115
|
+
}
|
|
116
|
+
const server = await runBuiltServer(root, built.run)
|
|
117
|
+
if (server.error)
|
|
118
|
+
return {
|
|
119
|
+
skip: `The pages render on request, and the built server didn't start here: ${server.error}. Run it with \`kerf start\`, then \`kerf audit --url http://localhost:3000\`.`,
|
|
120
|
+
}
|
|
121
|
+
const listed = routes ?? pick(staticRoutes(root))
|
|
122
|
+
const answered = []
|
|
123
|
+
const unanswered = []
|
|
124
|
+
for (const route of listed) {
|
|
125
|
+
const status = await statusOf(`${server.url}${route}`)
|
|
126
|
+
if (status === 0 || status >= 500) unanswered.push(`${route} (${status || 'no answer'})`)
|
|
127
|
+
else answered.push(route)
|
|
128
|
+
}
|
|
129
|
+
if (answered.length) return { server, routes: answered, unanswered }
|
|
130
|
+
server.close()
|
|
131
|
+
return {
|
|
132
|
+
skip: `The built server answered no page here (${unanswered.join(', ')}): give the build what its pages read (\`.env\`), or \`kerf audit --url <deploy>\`.`,
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** The L000 warning naming the pages the built server couldn't answer at build time. */
|
|
137
|
+
export const unansweredPages = (unanswered) =>
|
|
138
|
+
diagnostic({
|
|
139
|
+
code: 'L000',
|
|
140
|
+
severity: 'warning',
|
|
141
|
+
file: 'kerf.config.ts',
|
|
142
|
+
why: `Not audited: the built server answered ${unanswered.join(', ')} here, likely for want of what the page reads (a database, \`.env\`). \`kerf audit --url <deploy>\` audits them running.`,
|
|
143
|
+
})
|
package/src/audit.mjs
CHANGED
|
@@ -16,8 +16,13 @@ import { diagnostic } from './diag.mjs'
|
|
|
16
16
|
import { fail, sleep } from './audit/utils.mjs'
|
|
17
17
|
import { auditPage } from './audit/page.mjs'
|
|
18
18
|
import { reportFindings } from './audit/report.mjs'
|
|
19
|
+
import { appRoutes } from './audit/routes.mjs'
|
|
20
|
+
import { serveBuild, unansweredPages } from './audit/server.mjs'
|
|
19
21
|
import { loadConfig } from './load-config.mjs'
|
|
20
22
|
|
|
23
|
+
export { appRoutes, staticRoutes } from './audit/routes.mjs'
|
|
24
|
+
export { builtServer } from './audit/server.mjs'
|
|
25
|
+
|
|
21
26
|
/** The two widths Lighthouse's defaults stand for: its mobile run (a Moto G Power) and its desktop run. */
|
|
22
27
|
export const FORMS = {
|
|
23
28
|
phone: { width: 412, height: 823, deviceScaleFactor: 1.75, mobile: true },
|
|
@@ -191,51 +196,40 @@ async function chrome(exe) {
|
|
|
191
196
|
}
|
|
192
197
|
|
|
193
198
|
/**
|
|
194
|
-
*
|
|
195
|
-
*
|
|
199
|
+
* Where the audit's Chrome reads the pages, and which: a running site (`url`), the prerendered pages served as files,
|
|
200
|
+
* or the built server of a build whose pages render on request; or `skip`, why there is nothing to read.
|
|
196
201
|
*/
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
if (e.isDirectory()) {
|
|
205
|
-
walk(join(at, e.name), name ? `${prefix}/${name}` : prefix)
|
|
206
|
-
continue
|
|
207
|
-
}
|
|
208
|
-
if (
|
|
209
|
-
!/\.[jt]sx?$/.test(e.name) ||
|
|
210
|
-
/\.(server|test|spec)\./.test(e.name) ||
|
|
211
|
-
e.name.startsWith('_')
|
|
212
|
-
)
|
|
213
|
-
continue
|
|
214
|
-
const leaf = e.name.replace(/\.[jt]sx?$/, '')
|
|
215
|
-
const path = leaf === 'index' ? prefix : `${prefix}/${leaf}`
|
|
216
|
-
const re = path
|
|
217
|
-
.split('/')
|
|
218
|
-
.map((seg) =>
|
|
219
|
-
/^\[\.\.\.\w+\]$/.test(seg)
|
|
220
|
-
? '\0'
|
|
221
|
-
: /^\[\w+\]$/.test(seg)
|
|
222
|
-
? '[^/]+'
|
|
223
|
-
: seg.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'),
|
|
224
|
-
)
|
|
225
|
-
.join('/')
|
|
226
|
-
.replace(/\/\0/g, '(?:/.+)?')
|
|
227
|
-
tests.push(new RegExp(`^${re || ''}/?$`))
|
|
202
|
+
async function target(root, options) {
|
|
203
|
+
if (options.url)
|
|
204
|
+
return {
|
|
205
|
+
base: options.url.replace(/\/$/, ''),
|
|
206
|
+
statics: false,
|
|
207
|
+
routes: options.routes ?? ['/'],
|
|
208
|
+
unanswered: [],
|
|
228
209
|
}
|
|
210
|
+
const dir = options.dir ?? builtPublic(root)
|
|
211
|
+
const pages = dir ? pagesIn(dir) : []
|
|
212
|
+
const pick = (found) => (options.all ? found : samplePages(found, options.max))
|
|
213
|
+
if (pages.length) {
|
|
214
|
+
const server = await serveStatic(dir)
|
|
215
|
+
const routes = options.routes ?? pick(pages)
|
|
216
|
+
return { base: server.url, server, statics: true, routes, unanswered: [] }
|
|
229
217
|
}
|
|
230
|
-
|
|
231
|
-
|
|
218
|
+
// A build whose pages render on request prerenders none (or only its share cards): its own server answers them.
|
|
219
|
+
const served = await serveBuild(root, dir && relative(root, dir), {
|
|
220
|
+
routes: options.routes,
|
|
221
|
+
pick,
|
|
222
|
+
})
|
|
223
|
+
if (served.skip) return served
|
|
224
|
+
return { base: served.server.url, ...served, statics: false }
|
|
232
225
|
}
|
|
233
226
|
|
|
234
227
|
/**
|
|
235
228
|
* @param {string} root project root
|
|
236
229
|
* @param {{ url?: string, dir?: string, routes?: string[], all?: boolean, max?: number, forms?: (keyof FORMS)[], concurrency?: number, indexing?: 'auto' | 'public' | 'private' }} [options]
|
|
237
230
|
* `url`: audit a running site (a deploy, `kerf start`); else `dir`, a build's public directory (found when left out)
|
|
238
|
-
*
|
|
231
|
+
* `onRequest`: the pages were read from the built server of a build whose pages render on request
|
|
232
|
+
* @returns {Promise<{ ok: boolean, skipped?: string, base: string, onRequest?: boolean, pages: string[], diagnostics: object[], ms: number }>}
|
|
239
233
|
*/
|
|
240
234
|
export async function audit(root, options = {}) {
|
|
241
235
|
const t = performance.now()
|
|
@@ -261,24 +255,14 @@ export async function audit(root, options = {}) {
|
|
|
261
255
|
return skip(
|
|
262
256
|
'No Chrome or Chromium here, so the built pages were not audited (L001–L030). Set `KERF_CHROME`, or run `kerf audit --url <deploy>` where there is one.',
|
|
263
257
|
)
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
if (!base) {
|
|
268
|
-
const dir = options.dir ?? builtPublic(root)
|
|
269
|
-
if (!dir) return skip('No built pages to audit: run `kerf build` first, or pass `--url`.')
|
|
270
|
-
const pages = pagesIn(dir)
|
|
271
|
-
if (!pages.length)
|
|
272
|
-
return skip(
|
|
273
|
-
`No prerendered pages in ${relative(root, dir)} to audit: pass \`--url\` to audit a running server.`,
|
|
274
|
-
)
|
|
275
|
-
routes ??= options.all ? pages : samplePages(pages, options.max)
|
|
276
|
-
server = await serveStatic(dir)
|
|
277
|
-
base = server.url
|
|
278
|
-
}
|
|
279
|
-
routes ??= ['/']
|
|
258
|
+
const at = await target(root, options)
|
|
259
|
+
if (at.skip) return skip(at.skip)
|
|
260
|
+
const { base, server, statics, routes, unanswered } = at
|
|
280
261
|
const served = appRoutes(root)
|
|
281
|
-
const browser = await chrome(exe)
|
|
262
|
+
const browser = await chrome(exe).catch((e) => {
|
|
263
|
+
server?.close()
|
|
264
|
+
throw e
|
|
265
|
+
})
|
|
282
266
|
const findings = new Map()
|
|
283
267
|
try {
|
|
284
268
|
const jobs = routes.flatMap((route) => forms.map((form) => ({ route, form })))
|
|
@@ -289,7 +273,7 @@ export async function audit(root, options = {}) {
|
|
|
289
273
|
browser,
|
|
290
274
|
url: `${base}${job.route}`,
|
|
291
275
|
form: job.form,
|
|
292
|
-
statics
|
|
276
|
+
statics,
|
|
293
277
|
served,
|
|
294
278
|
forms: FORMS,
|
|
295
279
|
})) {
|
|
@@ -309,9 +293,11 @@ export async function audit(root, options = {}) {
|
|
|
309
293
|
server?.close()
|
|
310
294
|
}
|
|
311
295
|
const diagnostics = reportFindings(findings, routes, forms, indexing)
|
|
296
|
+
if (unanswered.length) diagnostics.push(unansweredPages(unanswered))
|
|
312
297
|
return {
|
|
313
298
|
ok: !diagnostics.some((d) => d.severity === 'error'),
|
|
314
299
|
base: options.url ?? base,
|
|
300
|
+
onRequest: !statics && !options.url,
|
|
315
301
|
pages: routes,
|
|
316
302
|
diagnostics,
|
|
317
303
|
ms: Math.round(performance.now() - t),
|
package/src/catalog/audit.mjs
CHANGED
|
@@ -6,8 +6,8 @@ export const audit = [
|
|
|
6
6
|
severity: 'warning',
|
|
7
7
|
by: 'cli',
|
|
8
8
|
catches:
|
|
9
|
-
"
|
|
10
|
-
fix: '
|
|
9
|
+
"Some or all of the built pages weren't audited: no Chrome or Chromium on this machine, no build, a server-rendered build whose server can't run here, or a page that answered 5xx at build time",
|
|
10
|
+
fix: 'The message names which: install Chrome or set `KERF_CHROME`; run `kerf build`; give the build what its pages read (`.env`); or `kerf audit --url <deploy>` to audit a running site',
|
|
11
11
|
},
|
|
12
12
|
{
|
|
13
13
|
code: 'L001',
|
package/src/cli.mjs
CHANGED
|
@@ -91,7 +91,8 @@ const usage = `usage: kerf <command> [--json] [--root <dir>]
|
|
|
91
91
|
tsc type-check the whole project with inferred props (tsc or tsgo; for CI)
|
|
92
92
|
dev [--port <n>] the dev server for an app with src/routes (Vite + Nitro)
|
|
93
93
|
build [--runtime node|bun|deno] [--preset <p>] production build to .output, and .kerf/contract.json; then the
|
|
94
|
-
built pages are audited (L-codes)
|
|
94
|
+
built pages are audited (L-codes): an error in a prerendered page fails the build, one in a
|
|
95
|
+
server-rendered page warns (\`kerf audit\` fails on both)
|
|
95
96
|
[--unsafe-allow <codes>] go past these codes (R002, L003, a family like L, or all): forces a deploy
|
|
96
97
|
audit [routes…] [--url <u>] [--indexing public|private|auto] [--all] [--max <n>] the built pages, or a running site, in a clean Chrome at a phone's
|
|
97
98
|
and a desktop's width: axe-core's WCAG rules, console, deprecations, head, sideways scroll, shift
|
package/src/start/build.mjs
CHANGED
|
@@ -31,7 +31,8 @@ export async function finishBuild({
|
|
|
31
31
|
for (const refused of contract.diagnostics.filter((d) => d.severity === 'error')) refuse(refused)
|
|
32
32
|
if (running) shipSidecars(root)
|
|
33
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
|
|
34
|
+
// Last, the pages as built, in a clean Chrome at a phone's width and a desktop's (audit.mjs): an error in a prerendered
|
|
35
|
+
// page fails the build.
|
|
35
36
|
const settings = (await loadConfig(root)).config?.build?.audit ?? true
|
|
36
37
|
const audited =
|
|
37
38
|
settings === false
|
|
@@ -49,7 +50,7 @@ export async function finishBuild({
|
|
|
49
50
|
dir: builtPublic(root),
|
|
50
51
|
...(typeof settings === 'object' && settings),
|
|
51
52
|
})
|
|
52
|
-
const pages = audited.
|
|
53
|
+
const pages = buildAuditFindings(audited).map((d) =>
|
|
53
54
|
d.severity === 'error' && unsafe.allows(d) ? unsafe.let(d) : d,
|
|
54
55
|
)
|
|
55
56
|
const kept = [
|
|
@@ -74,6 +75,24 @@ export async function finishBuild({
|
|
|
74
75
|
}
|
|
75
76
|
}
|
|
76
77
|
|
|
78
|
+
/**
|
|
79
|
+
* The audit's findings as `kerf build` reports them. Pages read from the built server of a build that renders on
|
|
80
|
+
* request were never audited at build time before, so their findings warn there rather than fail a build that used
|
|
81
|
+
* to pass; `kerf audit` still fails on them, as a prerendered page's fail both.
|
|
82
|
+
*/
|
|
83
|
+
export function buildAuditFindings(audited) {
|
|
84
|
+
if (!audited.onRequest) return audited.diagnostics
|
|
85
|
+
return audited.diagnostics.map((d) =>
|
|
86
|
+
d.severity === 'error'
|
|
87
|
+
? {
|
|
88
|
+
...d,
|
|
89
|
+
severity: 'warning',
|
|
90
|
+
why: `${d.why} A server-rendered page: a warning in \`kerf build\`, an error in \`kerf audit\`.`,
|
|
91
|
+
}
|
|
92
|
+
: d,
|
|
93
|
+
)
|
|
94
|
+
}
|
|
95
|
+
|
|
77
96
|
export async function startBuildSidecars({
|
|
78
97
|
start,
|
|
79
98
|
root,
|