shapewall 0.0.0-stage → 0.2.2

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 (304) hide show
  1. package/CHANGELOG.md +139 -0
  2. package/Dockerfile.agent +45 -0
  3. package/LICENSE +202 -0
  4. package/NOTICE +12 -0
  5. package/README.md +651 -2
  6. package/THIRD_PARTY.md +165 -0
  7. package/bin/shapewall.js +29 -0
  8. package/dist/agent/package-lock.json +8384 -0
  9. package/dist/client/assets/BoardChat-D5SFd8HE.js +28 -0
  10. package/dist/client/assets/BuzzDialog-HWXBy52W.js +1 -0
  11. package/dist/client/assets/ChecksPanel-DTdImWoD.js +1 -0
  12. package/dist/client/assets/History-CEv6b-av.js +1 -0
  13. package/dist/client/assets/LocalSettings-C4vriEVQ.js +1 -0
  14. package/dist/client/assets/Management-DCfhcEUY.js +1 -0
  15. package/dist/client/assets/QuickConnect-DQGgUsl1.js +1 -0
  16. package/dist/client/assets/ShareDialog-C_plIto0.js +2 -0
  17. package/dist/client/assets/app-update-dialog-2hPz_N9G.js +1 -0
  18. package/dist/client/assets/circle-x-BZMPi8Zf.js +1 -0
  19. package/dist/client/assets/index-ByQRKVit.css +2 -0
  20. package/dist/client/assets/index-DweQ8Qtu.js +29 -0
  21. package/dist/client/assets/refresh-cw-CRow0Nbq.js +1 -0
  22. package/dist/client/assets/runtime-Bft6x_6E.js +4 -0
  23. package/dist/client/assets/square-A8EnIYLR.js +1 -0
  24. package/dist/client/assets/tts-dist-DYlBQmwG.js +18 -0
  25. package/dist/client/assets/tts-en-g2p-BPUtBFGG.js +1 -0
  26. package/dist/client/assets/tts-ort-wasm-simd-threaded-qh98TCA_.wasm +0 -0
  27. package/dist/client/assets/tts-ort.wasm.bundle.min-rEkcs_ow.js +12 -0
  28. package/dist/client/assets/tts-rolldown-runtime-D8NudN84.js +1 -0
  29. package/dist/client/assets/tts-spacy-en-tokenizer-6SNMl--A.json +1 -0
  30. package/dist/client/assets/tts-tts.worker-Bzm6aWbv.js +1 -0
  31. package/dist/client/assets/tts-us_gold-DcjeNMH1.json +92589 -0
  32. package/dist/client/assets/tts-us_silver-C964mEB5.json +93363 -0
  33. package/dist/client/assets/ui-CLbws0rx.js +41 -0
  34. package/dist/client/build-info.json +1 -0
  35. package/dist/client/favicon.svg +7 -0
  36. package/dist/client/icons/apple-touch-icon.png +0 -0
  37. package/dist/client/icons/icon-192.png +0 -0
  38. package/dist/client/icons/icon-512.png +0 -0
  39. package/dist/client/icons/maskable-512.png +0 -0
  40. package/dist/client/index.html +25 -0
  41. package/dist/client/manifest.webmanifest +15 -0
  42. package/dist/client/sw.js +53 -0
  43. package/dist/client/theme-boot.js +27 -0
  44. package/dist/console/assets/LocalAccountsApp-BQCcmj3x.js +1 -0
  45. package/dist/console/assets/LocalConsole-Bb2FUDs7.js +1 -0
  46. package/dist/console/assets/app-update-dialog-DIeYepLZ.js +1 -0
  47. package/dist/console/assets/console-C7uJLLOn.js +51 -0
  48. package/dist/console/assets/console-R8IBY3Am.css +2 -0
  49. package/dist/console/build-info.json +1 -0
  50. package/dist/console/console.html +21 -0
  51. package/dist/console/favicon.svg +7 -0
  52. package/dist/console/icons/apple-touch-icon.png +0 -0
  53. package/dist/console/icons/icon-192.png +0 -0
  54. package/dist/console/icons/icon-512.png +0 -0
  55. package/dist/console/icons/maskable-512.png +0 -0
  56. package/dist/console/manifest.webmanifest +15 -0
  57. package/dist/console/theme-boot.js +27 -0
  58. package/docs/agent-hardening.md +342 -0
  59. package/docs/agent-recipes.md +187 -0
  60. package/docs/agent-workflow.md +320 -0
  61. package/docs/agents.md +894 -0
  62. package/docs/architecture.md +242 -0
  63. package/docs/buzz.md +140 -0
  64. package/docs/capture-warnings.md +57 -0
  65. package/docs/config.md +489 -0
  66. package/docs/deployment.md +289 -0
  67. package/docs/design-system.md +162 -0
  68. package/docs/github.md +107 -0
  69. package/docs/hosted.md +764 -0
  70. package/docs/images/board-overview.png +0 -0
  71. package/docs/images/journeys.png +0 -0
  72. package/docs/images/live-comment.png +0 -0
  73. package/docs/images/phone-comment.png +0 -0
  74. package/docs/images/phone-overview.png +0 -0
  75. package/docs/images/variant.png +0 -0
  76. package/docs/interaction-audits.md +40 -0
  77. package/docs/journeys.md +137 -0
  78. package/docs/local-accounts.md +80 -0
  79. package/docs/notifications.md +237 -0
  80. package/docs/page-checks.md +69 -0
  81. package/docs/performance.md +174 -0
  82. package/docs/starter-agent.md +325 -0
  83. package/docs/updates.md +87 -0
  84. package/examples/audits/interaction.audit.json +17 -0
  85. package/package.json +132 -4
  86. package/src/accounts/avatar.js +121 -0
  87. package/src/accounts/local.js +464 -0
  88. package/src/agent/acp/command.js +204 -0
  89. package/src/agent/acp/detect.js +75 -0
  90. package/src/agent/acp/sandbox.js +160 -0
  91. package/src/agent/agent-health.js +75 -0
  92. package/src/agent/backends/acp.js +1297 -0
  93. package/src/agent/backends/index.js +61 -0
  94. package/src/agent/backends/openai.js +308 -0
  95. package/src/agent/composer.js +221 -0
  96. package/src/agent/config.js +338 -0
  97. package/src/agent/context.js +191 -0
  98. package/src/agent/control.js +122 -0
  99. package/src/agent/errors.js +17 -0
  100. package/src/agent/feedback-loop.js +137 -0
  101. package/src/agent/manage.js +94 -0
  102. package/src/agent/next-steps.js +51 -0
  103. package/src/agent/owner-mcp.js +38 -0
  104. package/src/agent/plain-text.js +52 -0
  105. package/src/agent/ratings-core.js +213 -0
  106. package/src/agent/ratings.js +100 -0
  107. package/src/agent/reply-text.js +67 -0
  108. package/src/agent/routes.js +1065 -0
  109. package/src/agent/run.js +326 -0
  110. package/src/agent/settings-routes.js +704 -0
  111. package/src/agent/showable.js +23 -0
  112. package/src/agent/starter/approvals.js +181 -0
  113. package/src/agent/starter/build-image.js +90 -0
  114. package/src/agent/starter/cli.js +163 -0
  115. package/src/agent/starter/disk.js +53 -0
  116. package/src/agent/starter/doctor.js +180 -0
  117. package/src/agent/starter/egress-proxy.js +351 -0
  118. package/src/agent/starter/gate.js +90 -0
  119. package/src/agent/starter/loop.js +114 -0
  120. package/src/agent/starter/model-relay.js +289 -0
  121. package/src/agent/starter/pause.js +14 -0
  122. package/src/agent/starter/probe.js +272 -0
  123. package/src/agent/starter/prompt.js +41 -0
  124. package/src/agent/starter/push-guard.js +146 -0
  125. package/src/agent/starter/rpc.js +164 -0
  126. package/src/agent/starter/runtime.js +287 -0
  127. package/src/agent/starter/sandbox-main.js +121 -0
  128. package/src/agent/starter/settings.js +290 -0
  129. package/src/agent/starter/source.js +69 -0
  130. package/src/agent/starter/supervisor.js +755 -0
  131. package/src/agent/starter/sweep.js +46 -0
  132. package/src/agent/starter/tools.js +524 -0
  133. package/src/agent/starter/watch.js +84 -0
  134. package/src/agent/starter/worktree.js +409 -0
  135. package/src/agent/text-diff.js +210 -0
  136. package/src/agent/thread-history.js +223 -0
  137. package/src/agent/threads.js +399 -0
  138. package/src/agent/tools.js +200 -0
  139. package/src/agent/turn-changes.js +424 -0
  140. package/src/agent/undo-state.js +26 -0
  141. package/src/agent/user-settings.js +308 -0
  142. package/src/auth/record.js +375 -0
  143. package/src/broken-links.js +164 -0
  144. package/src/buzz/client.js +75 -0
  145. package/src/buzz/config.js +151 -0
  146. package/src/buzz/environment.js +13 -0
  147. package/src/buzz/gateway.js +192 -0
  148. package/src/buzz/routes.js +61 -0
  149. package/src/buzz/signer-child.js +205 -0
  150. package/src/buzz/store.js +89 -0
  151. package/src/buzz/transport.js +60 -0
  152. package/src/buzz/verify.js +140 -0
  153. package/src/capture/actions.js +159 -0
  154. package/src/capture/capture.js +776 -0
  155. package/src/capture/dom-names.js +189 -0
  156. package/src/capture/element-map.js +509 -0
  157. package/src/capture/live.js +827 -0
  158. package/src/capture/media-scheme.js +317 -0
  159. package/src/capture/network-summary.js +86 -0
  160. package/src/capture/pacing.js +34 -0
  161. package/src/capture/requests.js +167 -0
  162. package/src/capture/sanitize.js +752 -0
  163. package/src/checks/accessibility.js +134 -0
  164. package/src/checks/budgets.js +58 -0
  165. package/src/checks/cli.js +55 -0
  166. package/src/checks/comparison.js +113 -0
  167. package/src/checks/environments.js +32 -0
  168. package/src/checks/flow-config.js +82 -0
  169. package/src/checks/flow-origin.js +53 -0
  170. package/src/checks/flows.js +398 -0
  171. package/src/checks/lighthouse-auth.js +145 -0
  172. package/src/checks/lighthouse-findings.js +130 -0
  173. package/src/checks/lighthouse.js +395 -0
  174. package/src/checks/robots.js +85 -0
  175. package/src/checks/routes.js +52 -0
  176. package/src/checks/run-control.js +259 -0
  177. package/src/checks/runner.js +724 -0
  178. package/src/checks/schema.js +133 -0
  179. package/src/checks/seo-analysis.js +443 -0
  180. package/src/checks/seo-probe.js +190 -0
  181. package/src/checks/seo-snapshot.js +99 -0
  182. package/src/checks/store.js +214 -0
  183. package/src/checks/transport.js +47 -0
  184. package/src/cli/args.js +319 -0
  185. package/src/cli/capture.js +79 -0
  186. package/src/cli/commands.js +92 -0
  187. package/src/cli/comments.js +84 -0
  188. package/src/cli/help.js +243 -0
  189. package/src/cli/init.js +214 -0
  190. package/src/cli/misc.js +64 -0
  191. package/src/cli/notify.js +87 -0
  192. package/src/cli/open.js +22 -0
  193. package/src/cli/serve.js +274 -0
  194. package/src/cli/variant.js +82 -0
  195. package/src/cli/watch.js +70 -0
  196. package/src/cli.js +130 -0
  197. package/src/comment-fields.js +76 -0
  198. package/src/comments-cli.js +208 -0
  199. package/src/config-reload.js +100 -0
  200. package/src/config.js +1061 -0
  201. package/src/devices-cli.js +21 -0
  202. package/src/diff.js +95 -0
  203. package/src/discover.js +284 -0
  204. package/src/doctor.js +295 -0
  205. package/src/event-log.js +268 -0
  206. package/src/events-cli.js +102 -0
  207. package/src/events.js +113 -0
  208. package/src/export.js +119 -0
  209. package/src/feedback.js +246 -0
  210. package/src/github.js +551 -0
  211. package/src/hosted/LICENSE.md +105 -0
  212. package/src/hosted/access-cli.js +78 -0
  213. package/src/hosted/admin.js +682 -0
  214. package/src/hosted/agent-approvals.js +163 -0
  215. package/src/hosted/agent-policy.js +189 -0
  216. package/src/hosted/app.js +510 -0
  217. package/src/hosted/auth-routes.js +540 -0
  218. package/src/hosted/auth.js +536 -0
  219. package/src/hosted/backup.js +483 -0
  220. package/src/hosted/board-context.js +35 -0
  221. package/src/hosted/board-routes.js +486 -0
  222. package/src/hosted/board-view.js +94 -0
  223. package/src/hosted/clean-worker.js +24 -0
  224. package/src/hosted/cleaner.js +89 -0
  225. package/src/hosted/comments.js +355 -0
  226. package/src/hosted/console-routes.js +401 -0
  227. package/src/hosted/cookies.js +18 -0
  228. package/src/hosted/crypto.js +80 -0
  229. package/src/hosted/db.js +496 -0
  230. package/src/hosted/device-routes.js +175 -0
  231. package/src/hosted/device.js +233 -0
  232. package/src/hosted/events.js +63 -0
  233. package/src/hosted/guards.js +172 -0
  234. package/src/hosted/guest-view.js +65 -0
  235. package/src/hosted/http.js +39 -0
  236. package/src/hosted/import.js +300 -0
  237. package/src/hosted/mail.js +73 -0
  238. package/src/hosted/mcp-http.js +71 -0
  239. package/src/hosted/membership.js +114 -0
  240. package/src/hosted/operator.js +478 -0
  241. package/src/hosted/ops-cli.js +345 -0
  242. package/src/hosted/pages.js +146 -0
  243. package/src/hosted/permissions.js +113 -0
  244. package/src/hosted/presence.js +372 -0
  245. package/src/hosted/projects.js +124 -0
  246. package/src/hosted/public.js +109 -0
  247. package/src/hosted/push.js +622 -0
  248. package/src/hosted/quotas.js +115 -0
  249. package/src/hosted/ratelimit.js +96 -0
  250. package/src/hosted/rebind.js +316 -0
  251. package/src/hosted/retention.js +128 -0
  252. package/src/hosted/server.js +260 -0
  253. package/src/hosted/setup-link.js +64 -0
  254. package/src/hosted/setup.js +323 -0
  255. package/src/hosted/shares.js +444 -0
  256. package/src/hosted/tailscale.js +204 -0
  257. package/src/hosted/tenancy.js +301 -0
  258. package/src/hosted/token-api.js +455 -0
  259. package/src/hosted/token-events.js +60 -0
  260. package/src/hosted/tokens.js +57 -0
  261. package/src/hosted/turnstile.js +49 -0
  262. package/src/hosted/usercontent.js +154 -0
  263. package/src/hosted/zip.js +155 -0
  264. package/src/invocation.js +58 -0
  265. package/src/journey-paths.js +282 -0
  266. package/src/journeys.js +189 -0
  267. package/src/links.js +106 -0
  268. package/src/local-projects.js +182 -0
  269. package/src/lock.js +193 -0
  270. package/src/mcp.js +417 -0
  271. package/src/mentions.js +151 -0
  272. package/src/net.js +15 -0
  273. package/src/notify/config.js +275 -0
  274. package/src/notify/index.js +301 -0
  275. package/src/notify/lease.js +73 -0
  276. package/src/notify/messages.js +212 -0
  277. package/src/notify/run.js +174 -0
  278. package/src/pixeldiff.js +49 -0
  279. package/src/png.js +143 -0
  280. package/src/reactions.js +111 -0
  281. package/src/remote/cli.js +133 -0
  282. package/src/remote/credentials.js +62 -0
  283. package/src/remote/ids.js +12 -0
  284. package/src/remote/login.js +155 -0
  285. package/src/remote/remote-mcp.js +237 -0
  286. package/src/remote/remote-watch.js +291 -0
  287. package/src/remote/remote.js +313 -0
  288. package/src/remote/share-state.js +78 -0
  289. package/src/remote/share.js +369 -0
  290. package/src/remote/terminal.js +13 -0
  291. package/src/serve-state.js +64 -0
  292. package/src/server/app.js +824 -0
  293. package/src/server/devices.js +186 -0
  294. package/src/server/local-console.js +28 -0
  295. package/src/server/local-user.js +59 -0
  296. package/src/server/pairing-routes.js +159 -0
  297. package/src/server/share-routes.js +89 -0
  298. package/src/server/tailscale.js +97 -0
  299. package/src/sitemap.js +431 -0
  300. package/src/store.js +983 -0
  301. package/src/tokens.js +11 -0
  302. package/src/update/routes.js +245 -0
  303. package/src/watch.js +403 -0
  304. package/src/which.js +58 -0
@@ -0,0 +1,259 @@
1
+ import { spawn } from 'node:child_process'
2
+ import express from 'express'
3
+
4
+ // Page checks started from the board: `shapewall checks run` as a child
5
+ // process of the local server, exactly like the terminal command (same
6
+ // runner, limits, auth and page actions). One run at a time per server.
7
+ // The hosted server never mounts this: it must not execute checks.
8
+ export const CHECKS_RUN_LIMIT = 100
9
+ const PROGRESS = /^Checking ([a-z0-9][a-z0-9-]{0,63}) ([a-z][a-z0-9-]{0,31})$/
10
+ const DONE = /^Checked (\d+) page views\. (\d+) failed\./
11
+ // A stopped run saved only the views it finished (cli.js, exit code 3).
12
+ const STOPPED = /^Stopped after (\d+) of (\d+) page views\. (\d+) failed\./
13
+ // A run that outlives this is stopped, so a hung browser or auth script
14
+ // cannot hold the one-run lock (and block captures) until a restart.
15
+ export const CHECKS_RUN_BASE_MS = 5 * 60_000
16
+ export const CHECKS_RUN_PER_VIEW_MS = 3 * 60_000
17
+ // Credentials and query strings out of an error line before it is shown.
18
+ const scrub = (text) =>
19
+ text
20
+ .replace(/[a-z][a-z0-9+.-]*:\/\/[^\s/@]*@/gi, (m) => m.slice(0, m.indexOf('//') + 2))
21
+ .replace(/([a-z][a-z0-9+.-]*:\/\/[^\s?#]*)[?#]\S*/gi, '$1')
22
+
23
+ export function createChecksRuns({
24
+ config,
25
+ bin,
26
+ children,
27
+ log = () => {},
28
+ spawnImpl = spawn,
29
+ now = () => Date.now(),
30
+ timeoutMs = (views) => CHECKS_RUN_BASE_MS + views * CHECKS_RUN_PER_VIEW_MS,
31
+ }) {
32
+ let current = null
33
+ const running = () => Boolean(current && current.state === 'running')
34
+ const publicState = (run) =>
35
+ run && {
36
+ boardId: run.boardId,
37
+ state: run.state,
38
+ environment: run.environment,
39
+ seo: run.seo,
40
+ pages: run.pages,
41
+ total: run.total,
42
+ done: run.done,
43
+ current: run.current,
44
+ startedAt: new Date(run.startedAt).toISOString(),
45
+ finishedAt: run.finishedAt ? new Date(run.finishedAt).toISOString() : null,
46
+ checked: run.checked,
47
+ failed: run.failed,
48
+ message: run.message,
49
+ // Stop was asked for; the runner is abandoning the current view.
50
+ stopping: run.state === 'running' && Boolean(run.stopReason),
51
+ }
52
+
53
+ function start(board, { environment = null, pages = null, seo = true } = {}) {
54
+ const selected = pages ? board.pages.filter((p) => pages.includes(p.id)) : board.pages
55
+ const total = selected.reduce((n, p) => n + p.viewports.length, 0)
56
+ if (!total) throw Object.assign(new Error('Choose at least one page to check.'), { status: 400, code: 'no_pages' })
57
+ if (total > CHECKS_RUN_LIMIT)
58
+ throw Object.assign(
59
+ new Error(`That is ${total} page views; a run checks at most ${CHECKS_RUN_LIMIT}. Choose fewer pages or use the terminal.`),
60
+ { status: 400, code: 'too_many_views' },
61
+ )
62
+ const args = [bin, 'checks', 'run', '--board', board.id, '--limit', String(total)]
63
+ if (environment) args.push('--env', environment)
64
+ if (pages) args.push('--only', selected.map((p) => p.id).join(','))
65
+ // A checks.seo page list stays in charge when SEO is included: no flag,
66
+ // so the CLI applies the list instead of probing every page.
67
+ if (!seo) args.push('--no-seo')
68
+ else if (!Array.isArray(config.checks?.seo)) args.push('--seo')
69
+ if (config.file) args.push('--config', config.file)
70
+ const run = {
71
+ boardId: board.id,
72
+ state: 'running',
73
+ environment,
74
+ seo,
75
+ pages: pages ? selected.map((p) => p.id) : null,
76
+ total,
77
+ done: 0,
78
+ current: null,
79
+ startedAt: now(),
80
+ finishedAt: null,
81
+ checked: null,
82
+ failed: null,
83
+ message: null,
84
+ }
85
+ const child = spawnImpl(process.execPath, args, { stdio: ['ignore', 'pipe', 'pipe'], cwd: config.dir, env: process.env })
86
+ child.boardId = board.id
87
+ child.kind = 'checks'
88
+ children.add(child)
89
+ current = run
90
+ let buffer = ''
91
+ let lastError = ''
92
+ const onLine = (line) => {
93
+ const progress = PROGRESS.exec(line)
94
+ if (progress) {
95
+ run.current = { pageId: progress[1], viewport: progress[2] }
96
+ run.done = Math.min(run.total, run.done + (run.started ? 1 : 0))
97
+ run.started = true
98
+ return
99
+ }
100
+ const done = DONE.exec(line)
101
+ if (done) {
102
+ run.checked = Number(done[1])
103
+ run.failed = Number(done[2])
104
+ }
105
+ const stopped = STOPPED.exec(line)
106
+ if (stopped) {
107
+ run.stoppedAt = Number(stopped[1])
108
+ run.failed = Number(stopped[3])
109
+ }
110
+ }
111
+ child.stdout?.on('data', (chunk) => {
112
+ buffer = (buffer + chunk.toString('utf8')).slice(-8192)
113
+ let end
114
+ while ((end = buffer.indexOf('\n')) >= 0) {
115
+ onLine(buffer.slice(0, end).trim())
116
+ buffer = buffer.slice(end + 1)
117
+ }
118
+ })
119
+ child.stderr?.on('data', (chunk) => {
120
+ lastError = (lastError + chunk.toString('utf8')).slice(-600)
121
+ })
122
+ const stop = (reason) => {
123
+ if (run.state !== 'running' || run.stopReason) return false
124
+ run.stopReason = reason
125
+ child.kill('SIGTERM')
126
+ setTimeout(() => {
127
+ if (child.exitCode === null && child.signalCode === null) child.kill('SIGKILL')
128
+ }, 10_000).unref?.()
129
+ return true
130
+ }
131
+ const timer = setTimeout(() => stop('timeout'), timeoutMs(total))
132
+ timer.unref?.()
133
+ run.stop = stop
134
+ const finish = (code) => {
135
+ if (run.state !== 'running') return
136
+ clearTimeout(timer)
137
+ children.delete(child)
138
+ if (buffer.trim()) onLine(buffer.trim())
139
+ run.finishedAt = now()
140
+ run.current = null
141
+ // A stop that came too late (the runner had finished every view) is a
142
+ // normal result; otherwise only the finished views were saved.
143
+ if (run.stopReason && run.checked === null) {
144
+ run.state = 'stopped'
145
+ run.checked = run.stoppedAt ?? 0
146
+ run.done = run.checked
147
+ const minutes = Math.max(1, Math.round(timeoutMs(total) / 60_000))
148
+ run.message =
149
+ `Stopped after ${run.checked} of ${run.total} page views` +
150
+ (run.stopReason === 'timeout' ? `: the run took longer than ${minutes} minute${minutes === 1 ? '' : 's'}.` : '.') +
151
+ ' Page views not checked keep their earlier results.'
152
+ } else if (run.checked !== null) {
153
+ run.done = run.checked
154
+ run.state = run.failed ? 'partial' : 'complete'
155
+ run.message = run.failed
156
+ ? `Checked ${run.checked} page views; ${run.failed} could not finish.`
157
+ : `Checked ${run.checked} page view${run.checked === 1 ? '' : 's'}.`
158
+ } else {
159
+ run.state = 'failed'
160
+ // The CLI's own one-line reason ("shapewall: ..."), without paths.
161
+ const reason = scrub(lastError)
162
+ .trim()
163
+ .split('\n')
164
+ .pop()
165
+ ?.replace(/^shapewall:\s*/, '')
166
+ .replace(/(?<![\w:/.])(?:\/[\w.-]+){2,}/g, '…')
167
+ .slice(0, 300)
168
+ run.message = reason || `The checks run stopped (exit ${code ?? 'signal'}).`
169
+ }
170
+ }
171
+ // 'close', not 'exit': stdout may still hold the summary line at exit.
172
+ child.on('close', (code) => finish(code))
173
+ child.on('error', (error) => {
174
+ log(`checks of "${board.id}" could not start: ${error.message}`)
175
+ finish(null)
176
+ })
177
+ return run
178
+ }
179
+
180
+ return {
181
+ running,
182
+ runningFor: (boardId) => running() && current.boardId === boardId,
183
+ state: (boardId) => (current?.boardId === boardId ? publicState(current) : null),
184
+ // Stops this board's running run; false when there is none.
185
+ cancel: (boardId) => Boolean(current?.boardId === boardId && current.stop?.('owner')),
186
+ start,
187
+ }
188
+ }
189
+
190
+ // GET /api/boards/:boardId/checks/run the latest run of this board and whether this browser may start one
191
+ // POST /api/boards/:boardId/checks/run { env?, pages?, seo? } starts one (owner only)
192
+ // DELETE /api/boards/:boardId/checks/run stops this board's running run (owner only)
193
+ // Mount after the checks read guard. `allowed(req)` is the owner check;
194
+ // `blocked(board)` names a conflicting job (a capture) or returns null.
195
+ export function checksRunRouter({ config, runs, allowed, blocked = () => null, enabled = true, sameOrigin = () => true }) {
196
+ const router = express.Router({ mergeParams: true })
197
+ const fail = (res, status, code, message) => res.status(status).json({ error: { code, message } })
198
+ const boardOf = (req) => config.boards.find((b) => b.id === req.params.boardId) || null
199
+ const refusal = (req) =>
200
+ !enabled
201
+ ? 'Checks can’t be started from this board. Run them in the installation terminal.'
202
+ : !allowed(req)
203
+ ? 'Only the owner can run checks from the board. Open the private link printed by shapewall serve, or sign in.'
204
+ : null
205
+ router.get('/', (req, res) => {
206
+ const board = boardOf(req)
207
+ if (!board) return fail(res, 404, 'unknown_board', 'No such board.')
208
+ const reason = refusal(req)
209
+ res.set('Cache-Control', 'no-store').json({
210
+ canRun: !reason,
211
+ reason,
212
+ limit: CHECKS_RUN_LIMIT,
213
+ // Include SEO starts on unless the config turns SEO off.
214
+ seoDefault: Array.isArray(config.checks?.seo) ? config.checks.seo.length > 0 : config.checks?.seo !== false,
215
+ privatePages: board.pages.filter((p) => p.seo?.indexable === false).map((p) => p.id),
216
+ run: runs.state(board.id),
217
+ })
218
+ })
219
+ router.post('/', express.json({ limit: '8kb' }), (req, res) => {
220
+ res.set('Cache-Control', 'no-store')
221
+ const board = boardOf(req)
222
+ if (!board) return fail(res, 404, 'unknown_board', 'No such board.')
223
+ if (!req.is('application/json')) return fail(res, 415, 'json_required', 'Send JSON (Content-Type: application/json).')
224
+ const reason = refusal(req)
225
+ if (reason || !sameOrigin(req)) return fail(res, 403, 'checks_owner_only', reason || 'Start checks from this board’s own page.')
226
+ if (runs.running())
227
+ return fail(res, 409, 'checks_running', 'A checks run is already in progress. Wait for it to finish before starting another.')
228
+ const conflict = blocked(board)
229
+ if (conflict) return fail(res, 409, 'capture_running', conflict)
230
+ const body = req.body || {}
231
+ const environment = body.env ?? null
232
+ if (environment !== null && !board.environments.some((env) => env.id === environment))
233
+ return fail(res, 400, 'unknown_environment', 'No such environment on this board.')
234
+ const pages = body.pages ?? null
235
+ if (pages !== null) {
236
+ const known = new Set(board.pages.map((p) => p.id))
237
+ if (!Array.isArray(pages) || !pages.length || pages.length > 100 || !pages.every((id) => typeof id === 'string' && known.has(id)))
238
+ return fail(res, 400, 'unknown_page', 'pages: a list of page ids on this board.')
239
+ }
240
+ if (body.seo !== undefined && typeof body.seo !== 'boolean') return fail(res, 400, 'invalid_seo', 'seo must be true or false.')
241
+ try {
242
+ const run = runs.start(board, { environment, pages: pages ? [...new Set(pages)] : null, seo: body.seo !== false })
243
+ res.status(202).json({ started: true, run: runs.state(run.boardId) })
244
+ } catch (error) {
245
+ if (error.status) return fail(res, error.status, error.code, error.message)
246
+ throw error
247
+ }
248
+ })
249
+ router.delete('/', (req, res) => {
250
+ res.set('Cache-Control', 'no-store')
251
+ const board = boardOf(req)
252
+ if (!board) return fail(res, 404, 'unknown_board', 'No such board.')
253
+ const reason = refusal(req)
254
+ if (reason || !sameOrigin(req)) return fail(res, 403, 'checks_owner_only', reason || 'Stop checks from this board’s own page.')
255
+ if (!runs.cancel(board.id)) return fail(res, 409, 'checks_not_running', 'No checks run of this board is in progress.')
256
+ res.json({ stopping: true, run: runs.state(board.id) })
257
+ })
258
+ return router
259
+ }