shapewall 0.7.3 → 0.9.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 (105) hide show
  1. package/CHANGELOG.md +121 -0
  2. package/README.md +7 -3
  3. package/bin/shapewall.js +2 -1
  4. package/dist/agent/package-lock.json +130 -72
  5. package/dist/client/assets/BoardChat-BawjlTc7.js +27 -0
  6. package/dist/client/assets/{BuzzDialog-DwEAN6va.js → BuzzDialog-DnjF36OB.js} +1 -1
  7. package/dist/client/assets/ChecksPanel-DRFcFhCj.js +1 -0
  8. package/dist/client/assets/{History-Cp4QRJti.js → History-FzpzGmXZ.js} +1 -1
  9. package/dist/client/assets/LocalSettings-BGFl4VKr.js +1 -0
  10. package/dist/client/assets/{Management-B-x1faV8.js → Management-BGdmGtby.js} +1 -1
  11. package/dist/client/assets/QuickConnect-CiVmxi0l.js +1 -0
  12. package/dist/client/assets/{ShareDialog-CdLdd6WQ.js → ShareDialog-D0tmOGcQ.js} +2 -2
  13. package/dist/client/assets/{app-update-dialog-BneaJeqQ.js → app-update-dialog-BQxVHa5e.js} +1 -1
  14. package/dist/client/assets/{circle-x-D5MkDLJk.js → circle-x-Dxsku6s6.js} +1 -1
  15. package/dist/client/assets/error-reports-sdk-COuSIAQO.js +15 -0
  16. package/dist/client/assets/index-B_m65HWT.css +2 -0
  17. package/dist/client/assets/index-C1ZwriJU.js +37 -0
  18. package/dist/client/assets/{play--xrZ7VCR.js → play-vJhLsLhL.js} +1 -1
  19. package/dist/client/assets/{refresh-cw-B-npxKae.js → refresh-cw-TbGHlgDq.js} +1 -1
  20. package/dist/client/assets/ui-CogdFhRH.js +41 -0
  21. package/dist/client/build-info.json +1 -1
  22. package/dist/client/index.html +3 -3
  23. package/dist/client/sw.js +2 -2
  24. package/dist/console/assets/{LocalAccountsApp-uKpz8EwR.js → LocalAccountsApp-ntQzkgTS.js} +1 -1
  25. package/dist/console/assets/{LocalConsole-XeqnTeRB.js → LocalConsole-Rg-MTvSJ.js} +2 -2
  26. package/dist/console/assets/{LocalUsage-Br3Ddp7_.js → LocalUsage-BGCSfmBU.js} +1 -1
  27. package/dist/console/assets/{app-update-dialog-h11DnNZi.js → app-update-dialog-CEhV7th4.js} +1 -1
  28. package/dist/console/assets/app-version-BiMzvHaD.js +1 -0
  29. package/dist/console/assets/console-BD5_Gha1.js +11 -0
  30. package/dist/console/assets/console-PWt8SxrJ.css +2 -0
  31. package/dist/console/assets/error-reports-sdk-COuSIAQO.js +15 -0
  32. package/dist/console/assets/local-agents-UprGVyLj.js +6 -0
  33. package/dist/console/assets/minus-Cnq0kKnu.js +1 -0
  34. package/dist/console/assets/{tailnet-DJGcztR5.js → tailnet-Cqtbptzq.js} +2 -2
  35. package/dist/console/assets/theme-CB-cwoG6.js +1 -0
  36. package/dist/console/assets/ui-HgfwUo3E.js +41 -0
  37. package/dist/console/assets/{usage-fzmilDTS.js → usage-BCIwgDP9.js} +2 -2
  38. package/dist/console/assets/{usage-DjmUlnXL.js → usage-CeY6XnGX.js} +1 -1
  39. package/dist/console/build-info.json +1 -1
  40. package/dist/console/console.html +8 -8
  41. package/docs/README.md +64 -0
  42. package/docs/agent-hardening.md +3 -3
  43. package/docs/agent-recipes.md +3 -3
  44. package/docs/agent-workflow.md +6 -4
  45. package/docs/agents.md +25 -16
  46. package/docs/architecture.md +9 -2
  47. package/docs/config.md +53 -25
  48. package/docs/deployment.md +19 -3
  49. package/docs/github.md +1 -1
  50. package/docs/hosted.md +265 -70
  51. package/docs/local-accounts.md +1 -1
  52. package/docs/performance.md +3 -2
  53. package/docs/pull-requests.md +1 -1
  54. package/docs/starter-agent.md +4 -4
  55. package/docs/where-to-run.md +1 -1
  56. package/package.json +5 -2
  57. package/src/agent/backends/acp.js +39 -9
  58. package/src/capture/capture.js +35 -5
  59. package/src/capture/destination.js +67 -0
  60. package/src/capture/load-errors.js +82 -0
  61. package/src/checks/cli.js +41 -5
  62. package/src/cli/args.js +1 -0
  63. package/src/cli/capture.js +5 -0
  64. package/src/cli/comments.js +41 -3
  65. package/src/cli/help.js +4 -1
  66. package/src/cli/init.js +8 -4
  67. package/src/cli/misc.js +3 -3
  68. package/src/cli/serve.js +37 -6
  69. package/src/comment-fields.js +5 -0
  70. package/src/comments-cli.js +26 -5
  71. package/src/config-keys.js +191 -0
  72. package/src/config-reload.js +4 -1
  73. package/src/config.js +33 -2
  74. package/src/doctor.js +188 -39
  75. package/src/error-scrub.js +135 -0
  76. package/src/error-tracking.js +105 -0
  77. package/src/feedback.js +36 -2
  78. package/src/hosted/app.js +9 -3
  79. package/src/hosted/auth.js +14 -8
  80. package/src/hosted/board-routes.js +6 -6
  81. package/src/hosted/db.js +50 -0
  82. package/src/hosted/pages.js +16 -7
  83. package/src/hosted/server.js +4 -0
  84. package/src/hosted/token-api.js +17 -1
  85. package/src/hosted/usage-shape.js +1 -1
  86. package/src/missing-package.js +11 -0
  87. package/src/ports.js +35 -0
  88. package/src/serve-state.js +16 -0
  89. package/src/server/app.js +18 -14
  90. package/src/server/local-console.js +7 -3
  91. package/src/watch.js +6 -0
  92. package/dist/client/assets/BoardChat-BVbIXN0v.js +0 -27
  93. package/dist/client/assets/ChecksPanel-BJmSzge1.js +0 -1
  94. package/dist/client/assets/LocalSettings-Bt52or8s.js +0 -1
  95. package/dist/client/assets/QuickConnect-C6sW-MA7.js +0 -1
  96. package/dist/client/assets/index-C-pM8Kf2.js +0 -37
  97. package/dist/client/assets/index-C2_XIPRO.css +0 -2
  98. package/dist/client/assets/ui-2lMx-4bc.js +0 -41
  99. package/dist/console/assets/app-version-IdaEWb4n.js +0 -1
  100. package/dist/console/assets/console-BDuWmrw9.js +0 -11
  101. package/dist/console/assets/console-BGRSS8Ru.css +0 -2
  102. package/dist/console/assets/local-agents-BkSN9lXJ.js +0 -6
  103. package/dist/console/assets/minus-887msXez.js +0 -1
  104. package/dist/console/assets/theme-CPfJOmcz.js +0 -1
  105. package/dist/console/assets/ui-DnyJhoQU.js +0 -41
@@ -473,14 +473,17 @@ export function createAuth({ db, origin, rpId, rpName = 'Shapewall', now = () =>
473
473
  // passkey confirms it). `device` is a short label from the User-Agent
474
474
  // ("Chrome on macOS"), never the raw header.
475
475
  //
476
- // A tailnet session is stored with via 'passkey' and its login in
477
- // tailnet_login (the column's CHECK predates it, and a build before tailnet
478
- // sign-in then reads it as a session that never passed a passkey check).
476
+ // A tailnet session is stored with via 'tailnet' and its login in
477
+ // tailnet_login (migration 18; before it, via said 'passkey'). What it is
478
+ // read as comes from tailnet_login all the same (viaOf): the login is
479
+ // what binds it to the identity, and a row that says 'passkey' with a
480
+ // login (written by a previous build run on this database) is a tailnet
481
+ // session too. It is never verified at sign-in: no passkey was checked.
479
482
  function createSession(userId, { verified = true, via = 'passkey', device = null, ip = null, tailnetLogin = null } = {}) {
480
483
  const token = randomToken(32)
484
+ if (!['passkey', 'recovery', 'tailnet'].includes(via)) throw new Error(`unknown sign-in method: ${via}`)
481
485
  if (via === 'tailnet' && !tailnetLogin) throw new Error('a tailnet session needs its login')
482
- if (via === 'tailnet') via = 'passkey'
483
- else tailnetLogin = null
486
+ if (via !== 'tailnet') tailnetLogin = null
484
487
  q.run(
485
488
  `INSERT INTO sessions (token_hash, id, user_id, created_at, last_seen_at, expires_at, verified_at, via, user_agent, ip, tailnet_login)
486
489
  VALUES (:hash, :id, :user, :at, :at, :expires, :verified, :via, :device, :ip, :tailnet)`,
@@ -490,7 +493,7 @@ export function createAuth({ db, origin, rpId, rpName = 'Shapewall', now = () =>
490
493
  user: userId,
491
494
  at: iso(),
492
495
  expires: iso(SESSION_DAYS * DAY),
493
- verified: verified && via === 'passkey' && !tailnetLogin ? iso() : NEVER_VERIFIED,
496
+ verified: verified && via === 'passkey' ? iso() : NEVER_VERIFIED,
494
497
  via,
495
498
  tailnet: tailnetLogin,
496
499
  device: device ? String(device).slice(0, 80) : null,
@@ -506,8 +509,11 @@ export function createAuth({ db, origin, rpId, rpName = 'Shapewall', now = () =>
506
509
  // never use such a session as an ordinary one after a downgrade.
507
510
  const sessionHash = (token, tailnetLogin) => sha256(tailnetLogin ? `tailnet:${token}` : token)
508
511
 
509
- // How a session came in, as the API and the audit log say it.
510
- const viaOf = (row) => (row.tailnet_login ? 'tailnet' : row.via)
512
+ // How a session came in, as the API, the guards and the audit log say it:
513
+ // decided by tailnet_login (which the hash is bound to), not by the via
514
+ // column alone. A row without a login is never read as a tailnet session,
515
+ // and a row with one always is.
516
+ const viaOf = (row) => (row.tailnet_login ? 'tailnet' : row.via === 'tailnet' ? 'passkey' : row.via)
511
517
 
512
518
  // The account's live sessions for the Account page: ids, never hashes.
513
519
  function listSessions(userId, currentHash) {
@@ -643,12 +643,12 @@ export function mountBoardRoutes(
643
643
  '/boards/:bid/comments/:cid/reopen',
644
644
  canWrite,
645
645
  withBoard((req, res, ctx) => {
646
- res.json({
647
- comment: shown(
648
- req,
649
- transaction(db, () => ctx.comments.update(req.params.cid, { status: 'open' }, now())),
650
- ),
651
- })
646
+ const comment = transaction(db, () => ctx.comments.update(req.params.cid, { status: 'open' }, now()))
647
+ // The resolve beside this one is audited and so is the delete below;
648
+ // reopening was not, so the log showed work being closed and never
649
+ // being put back.
650
+ auditOf(req, 'comment_reopened', `${ctx.board.id}#${comment.id}`, { project: req.project.id })
651
+ res.json({ comment: shown(req, comment) })
652
652
  }),
653
653
  )
654
654
  router.delete(
package/src/hosted/db.js CHANGED
@@ -726,6 +726,56 @@ const MIGRATIONS = [
726
726
  visits INTEGER NOT NULL DEFAULT 1
727
727
  );
728
728
  `,
729
+ // 18. A tailnet sign-in is recorded as one (0.9.0). sessions.via gains the
730
+ // value 'tailnet', and every session that came in by tailnet identity
731
+ // (tailnet_login set) is marked so. Before, such a session was stored as
732
+ // via 'passkey' with its login beside it: the code read it as a tailnet
733
+ // session (src/hosted/auth.js viaOf, from tailnet_login), but the row said
734
+ // passkey to anyone who read the table. SQLite cannot change a CHECK in
735
+ // place, so the table is rebuilt the way SQLite documents it (new table,
736
+ // copy, drop, rename, indexes again), inside the ledger's transaction:
737
+ // either all of it happens or none. Nothing references sessions (no
738
+ // foreign key, trigger or view), so the drop cascades to nothing. A row
739
+ // whose account is gone (only possible if foreign keys were ever off;
740
+ // such a session can never be read) is not copied rather than failing the
741
+ // start. A login is never '' (the code reads '' as no login, the SQL here
742
+ // as one; no code writes it, so only a hand-made row can have it): the
743
+ // copy turns a '' login into NULL and keeps that row's via: it is then a
744
+ // session with no login like any other, found by the hash of its token
745
+ // (one stored under a tailnet hash is never found, the stricter end), and
746
+ // a check keeps '' out from then on. The last CHECK keeps
747
+ // 'tailnet' to rows that carry a login; a row with a login may still say
748
+ // 'passkey', which is what a previous build writes if it runs on this
749
+ // database (docs/hosted.md, "Migration 18 and rollback"), and is read as a
750
+ // tailnet session all the same.
751
+ `
752
+ CREATE TABLE sessions_v18 (
753
+ token_hash TEXT PRIMARY KEY,
754
+ user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
755
+ created_at TEXT NOT NULL,
756
+ last_seen_at TEXT NOT NULL,
757
+ expires_at TEXT NOT NULL,
758
+ verified_at TEXT NOT NULL,
759
+ via TEXT NOT NULL DEFAULT 'passkey' CHECK (via IN ('passkey', 'recovery', 'tailnet')),
760
+ id TEXT,
761
+ user_agent TEXT,
762
+ ip TEXT,
763
+ admin_seen_at TEXT,
764
+ tailnet_login TEXT,
765
+ CHECK (tailnet_login IS NULL OR tailnet_login <> ''),
766
+ CHECK (via <> 'tailnet' OR (tailnet_login IS NOT NULL AND tailnet_login <> ''))
767
+ );
768
+ INSERT INTO sessions_v18
769
+ (token_hash, user_id, created_at, last_seen_at, expires_at, verified_at, via, id, user_agent, ip, admin_seen_at, tailnet_login)
770
+ SELECT token_hash, user_id, created_at, last_seen_at, expires_at, verified_at,
771
+ CASE WHEN tailnet_login IS NOT NULL AND tailnet_login <> '' THEN 'tailnet' ELSE via END,
772
+ id, user_agent, ip, admin_seen_at, NULLIF(tailnet_login, '')
773
+ FROM sessions WHERE user_id IN (SELECT id FROM users);
774
+ DROP TABLE sessions;
775
+ ALTER TABLE sessions_v18 RENAME TO sessions;
776
+ CREATE INDEX sessions_user ON sessions(user_id);
777
+ CREATE UNIQUE INDEX sessions_id ON sessions(id);
778
+ `,
729
779
  ]
730
780
 
731
781
  export async function openDatabase(file) {
@@ -12,6 +12,7 @@ import { readPublic } from './public.js'
12
12
  import { readGuest } from './shares.js'
13
13
  import { roleIn, tenant } from './tenancy.js'
14
14
  import { TURNSTILE_ORIGIN } from './turnstile.js'
15
+ import { cspWithErrors, errorMeta } from '../error-tracking.js'
15
16
 
16
17
  const CONSOLE_CSP =
17
18
  "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; frame-src 'none'; frame-ancestors 'none'; base-uri 'none'; form-action 'none'; object-src 'none'"
@@ -26,18 +27,20 @@ const removeWorker = (req, res) => {
26
27
  )
27
28
  }
28
29
 
29
- export function mountPages(app, { db, dataDir, consoleDir, clientDir, turnstile, sessionOf, BOARD_PAGE_CSP, deps }) {
30
+ export function mountPages(app, { db, dataDir, consoleDir, clientDir, turnstile, sessionOf, BOARD_PAGE_CSP, deps, errorTracking = null }) {
30
31
  // ---- Pages: the console and the board client ----
31
32
  // The console page gets a fresh nonce per response: the only inline thing
32
33
  // it allows is a <style> element that the dialog's scroll lock adds with
33
34
  // that nonce. Scripts come from 'self' only.
34
35
  const consoleFile = path.join(consoleDir, 'console.html')
35
- const consoleHtml = fs.existsSync(consoleFile) ? fs.readFileSync(consoleFile, 'utf8') : null
36
+ // Opt-in error reports: a <meta> tag tells the page (src/error-tracking.js).
37
+ const withErrors = (html) => (html && errorTracking ? html.replace('</head>', `${errorMeta(errorTracking)}</head>`) : html)
38
+ const consoleHtml = withErrors(fs.existsSync(consoleFile) ? fs.readFileSync(consoleFile, 'utf8') : null)
36
39
  const sendConsole = (req, res, status = 200) => {
37
40
  if (!consoleHtml) return res.status(status).type('text').send('The Shapewall console is not built yet. Run "npm run build".')
38
41
  res.status(status)
39
42
  const nonce = randomToken(18)
40
- let csp = CONSOLE_CSP.replace("style-src 'self'", `style-src 'self' 'nonce-${nonce}'`)
43
+ let csp = cspWithErrors(CONSOLE_CSP, errorTracking).replace("style-src 'self'", `style-src 'self' 'nonce-${nonce}'`)
41
44
  // The bot check's widget, only on a server that has one.
42
45
  if (turnstile)
43
46
  csp = csp
@@ -71,6 +74,8 @@ export function mountPages(app, { db, dataDir, consoleDir, clientDir, turnstile,
71
74
  app.get(route, (req, res) => sendConsole(req, res))
72
75
 
73
76
  const clientBuilt = fs.existsSync(path.join(clientDir, 'index.html'))
77
+ const boardHtml = clientBuilt && errorTracking ? withErrors(fs.readFileSync(path.join(clientDir, 'index.html'), 'utf8')) : null
78
+ const sendBoard = (res) => (boardHtml ? res.type('html').send(boardHtml) : res.sendFile('index.html', { root: clientDir }))
74
79
  // The client's own files under each project path; they hold no data.
75
80
  // The board's offline service worker caches the app shell; hosted boards
76
81
  // sit behind a session, so this one only removes itself.
@@ -103,7 +108,7 @@ export function mountPages(app, { db, dataDir, consoleDir, clientDir, turnstile,
103
108
  res.setHeader('Content-Security-Policy', BOARD_PAGE_CSP)
104
109
  res.setHeader('X-Frame-Options', 'DENY')
105
110
  res.setHeader('Cache-Control', 'no-cache')
106
- res.sendFile('index.html', { root: clientDir })
111
+ sendBoard(res)
107
112
  })
108
113
 
109
114
  // A public project's board: the same client on the view-only API. No
@@ -124,7 +129,7 @@ export function mountPages(app, { db, dataDir, consoleDir, clientDir, turnstile,
124
129
  res.setHeader('Content-Security-Policy', BOARD_PAGE_CSP)
125
130
  res.setHeader('X-Frame-Options', 'DENY')
126
131
  res.setHeader('Cache-Control', 'no-store')
127
- res.sendFile('index.html', { root: clientDir })
132
+ sendBoard(res)
128
133
  })
129
134
 
130
135
  // A share link's board: the same client, for a guest with the cookie that
@@ -140,11 +145,15 @@ export function mountPages(app, { db, dataDir, consoleDir, clientDir, turnstile,
140
145
  }
141
146
  app.get('/s/:sid', (req, res) => {
142
147
  if (!req.path.endsWith('/')) return res.redirect(302, `/s/${encodeURIComponent(req.params.sid)}/`)
143
- if (!readGuest(req, deps)) return res.redirect(302, '/share?ended=1')
148
+ const guest = readGuest(req, deps)
149
+ if (!guest) return res.redirect(302, '/share?ended=1')
150
+ // A comment link's guest may dictate a comment (the board page itself,
151
+ // same origin only); a view-only link keeps the microphone off.
152
+ if (guest.mode === 'comment') res.setHeader('Permissions-Policy', 'camera=(), microphone=(self), geolocation=()')
144
153
  if (!clientBuilt) return res.type('text').send('The Shapewall client is not built yet. Run "npm run build".')
145
154
  res.setHeader('Content-Security-Policy', BOARD_PAGE_CSP)
146
155
  res.setHeader('X-Frame-Options', 'DENY')
147
156
  res.setHeader('Cache-Control', 'no-store')
148
- res.sendFile('index.html', { root: clientDir })
157
+ sendBoard(res)
149
158
  })
150
159
  }
@@ -267,6 +267,9 @@ export async function startHosted({
267
267
  }
268
268
  }
269
269
 
270
+ // Opt-in error reports (SHAPEWALL_ERROR_DSN); nothing is loaded without it.
271
+ const { startErrorTracking } = await import('../error-tracking.js')
272
+ const errorTracking = await startErrorTracking(env, { log: (line) => out(line) })
270
273
  const warning = proxyWarning({ origin: publicOrigin, host, trustProxy })
271
274
  if (warning) out(warning)
272
275
  const common = {
@@ -282,6 +285,7 @@ export async function startHosted({
282
285
  turnstile,
283
286
  reportEmail,
284
287
  ownersSeeSignIns,
288
+ errorTracking,
285
289
  log: (line) => out(line),
286
290
  }
287
291
  const app = createHostedApp({
@@ -284,12 +284,24 @@ export function mountTokenApi({
284
284
  // when it asks (`guests=1`), so an agent never picks one up by default.
285
285
  function remoteReport(req, ctx, { status = 'open', pageId = null, mention = null, guests = false } = {}) {
286
286
  const context = contextFor(ctx)
287
+ const claim = req.token.scopes.includes('claim')
287
288
  let guestsHidden = 0
289
+ // A claim token reads only the comments its agent may take: the rest are
290
+ // another agent's work or nobody's yet, and that is deliberate. What was
291
+ // not deliberate is that the count then read like the board's own, so
292
+ // "2 comments on testbed" with a third open and routed elsewhere looked
293
+ // like the whole picture. Counted like the guests' one, and said out
294
+ // loud by the formatter.
295
+ let routedElsewhere = 0
288
296
  const comments = ctx.comments
289
297
  .list({ status: status === 'all' ? null : status, mention })
290
298
  .map((comment) => dispatchView(req.t, req.project.id, ctx.board.id, comment))
291
- .filter((comment) => !req.token.scopes.includes('claim') || (comment.dispatchable && mine(req, comment[COMMENT_ROUTING])))
292
299
  .filter((comment) => !pageId || comment.pageId === pageId || context.variants.get(comment.pageId)?.pageId === pageId)
300
+ .filter((comment) => {
301
+ if (!claim || (comment.dispatchable && mine(req, comment[COMMENT_ROUTING]))) return true
302
+ routedElsewhere += 1
303
+ return false
304
+ })
293
305
  .filter((comment) => {
294
306
  if (guests || !comment.authorGuest) return true
295
307
  guestsHidden += 1
@@ -307,6 +319,7 @@ export function mountTokenApi({
307
319
  },
308
320
  status,
309
321
  guestsHidden,
322
+ routedElsewhere,
310
323
  count: comments.length,
311
324
  comments,
312
325
  }
@@ -635,6 +648,9 @@ export function mountTokenApi({
635
648
  v1Json,
636
649
  v1Comment((req, res, ctx) => {
637
650
  const comment = transaction(db, () => ctx.comments.update(req.params.cid, { status: 'open' }, now()))
651
+ // Reopening is as much of a decision as resolving, and the log held
652
+ // only the resolve: whoever put the work back was nowhere in it.
653
+ auditOf(req, 'comment_reopened', `${ctx.board.id}#${comment.id}`, { project: req.project.id })
638
654
  res.json({ comment: v1Item(req, ctx, comment) })
639
655
  }),
640
656
  )
@@ -200,7 +200,7 @@ export const ACTION_GROUPS = {
200
200
  'chat_thread_deleted',
201
201
  'chat_thread_agent_changed',
202
202
  ],
203
- comments: ['comment_resolved', 'comment_deleted'],
203
+ comments: ['comment_resolved', 'comment_reopened', 'comment_deleted'],
204
204
  exports: ['workspace_exported', 'audit_exported', 'usage_exported', 'access_exported'],
205
205
  }
206
206
  export const ACTION_GROUP_IDS = Object.keys(ACTION_GROUPS)
@@ -0,0 +1,11 @@
1
+ // Node's "Cannot find package 'express' imported from ..." means
2
+ // Shapewall's own install is incomplete (an interrupted npm install, a
3
+ // pruned node_modules). Said plainly, with the fix; null for any other error.
4
+ // No imports: the CLI uses this when loading a module has just failed.
5
+
6
+ export function missingPackageMessage(error) {
7
+ if (error?.code !== 'ERR_MODULE_NOT_FOUND') return null
8
+ const name = /Cannot find package '([^']+)'/.exec(String(error.message || ''))?.[1]
9
+ if (!name) return null
10
+ return `Shapewall's install is incomplete: the package "${name}" is missing from node_modules. Reinstall it ("npm install" in this project, or "npm install -g shapewall" for a global install), then run "shapewall doctor".`
11
+ }
package/src/ports.js ADDED
@@ -0,0 +1,35 @@
1
+ // Whether a port is free, and the next one that is: for `serve` when its
2
+ // port is taken, and for `doctor`, so a suggested port is one that works.
3
+
4
+ import net from 'node:net'
5
+
6
+ // 'free', 'in-use', or the error code when the port cannot be tried at all
7
+ // (EACCES for a low port, EADDRNOTAVAIL for an address this machine lacks).
8
+ export function probePort(port, host = '127.0.0.1') {
9
+ return new Promise((resolve) => {
10
+ const server = net.createServer()
11
+ server.unref()
12
+ server.once('error', (error) => resolve(error.code === 'EADDRINUSE' ? 'in-use' : error.code || 'error'))
13
+ server.listen({ port, host, exclusive: true }, () => server.close(() => resolve('free')))
14
+ })
15
+ }
16
+
17
+ // The first free port after `port` (up to `tries` of them), or null.
18
+ export async function nextFreePort(port, host = '127.0.0.1', { tries = 20, probe = probePort } = {}) {
19
+ for (let candidate = port + 1; candidate <= Math.min(65535, port + tries); candidate += 1) {
20
+ if ((await probe(candidate, host)) === 'free') return candidate
21
+ }
22
+ return null
23
+ }
24
+
25
+ // What to say when `port` is taken: the sentence and the next step, with a
26
+ // port that was free a moment ago (or none found).
27
+ export function portInUse({ port, host, free, command, running = null }) {
28
+ const message = `Port ${port} on ${host} is in use${running ? ` (Shapewall is already serving this project at ${running})` : ''}.`
29
+ const fix = running
30
+ ? `Open ${running}, or stop that server first.`
31
+ : free
32
+ ? `Use "${command(free)}" (port ${free} is free), or stop what is using port ${port}.`
33
+ : `Stop what is using port ${port}, or pick another with "${command('PORT')}".`
34
+ return { message, fix }
35
+ }
@@ -51,6 +51,22 @@ export function clearServeState(config) {
51
51
  }
52
52
  }
53
53
 
54
+ // What serve.json records, running or not: { url, port, alive }, or null.
55
+ // A file a crashed server left still says which port the project was last
56
+ // served on, so doctor checks that one rather than the default.
57
+ export function recordedServe(config) {
58
+ if (!config?.outputDir) return null
59
+ try {
60
+ const state = JSON.parse(fs.readFileSync(stateFile(config), 'utf8'))
61
+ if (typeof state?.url !== 'string' || !/^https?:\/\//.test(state.url)) return null
62
+ const port = Number(new URL(state.url).port)
63
+ if (!Number.isInteger(port) || port < 1 || port > 65535) return null
64
+ return { url: state.url, port, alive: alive(state.pid) }
65
+ } catch {
66
+ return null
67
+ }
68
+ }
69
+
54
70
  // The running server's URL, or null.
55
71
  export function runningBoardUrl(config) {
56
72
  if (!config?.outputDir) return null
package/src/server/app.js CHANGED
@@ -53,6 +53,7 @@ import { createLocalAccounts } from '../accounts/local.js'
53
53
  import { createBuzzClient } from '../buzz/client.js'
54
54
  import { buzzRouter } from '../buzz/routes.js'
55
55
  import { requireCleanBuzzEnvironment } from '../buzz/environment.js'
56
+ import { cspWithErrors, errorMeta, errorSettingsFrom, reportServerError } from '../error-tracking.js'
56
57
 
57
58
  const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..')
58
59
  export const CLIENT_DIR = path.join(ROOT, 'dist', 'client')
@@ -62,7 +63,7 @@ export const OPEN_LINK_TTL_MS = 60 * 1000
62
63
 
63
64
  // The board shell. Inline styles are needed by the canvas library; scripts
64
65
  // only from this origin; live pages only in same-origin frames.
65
- const APP_CSP =
66
+ const BASE_APP_CSP =
66
67
  "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self' data:; connect-src 'self'; frame-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'none'; object-src 'none'"
67
68
  // The natural voice's worker (client/src/chat/tts, built as assets/tts-*):
68
69
  // it compiles ONNX Runtime's WebAssembly and downloads the Kokoro model and
@@ -187,6 +188,10 @@ export function createApp({
187
188
  } = {}) {
188
189
  if (buzzBridge) requireCleanBuzzEnvironment()
189
190
  const chatOn = chat && !readOnly
191
+ // Opt-in error reports (SHAPEWALL_ERROR_DSN, src/error-tracking.js): the page tells the browser.
192
+ const errorSettings = errorSettingsFrom(env)
193
+ const errors = errorSettings?.dsn ? errorSettings : null
194
+ const APP_CSP = cspWithErrors(BASE_APP_CSP, errors)
190
195
  const app = express()
191
196
  // `serve --tailscale`: first, how the request came in. The serve entry's
192
197
  // secret comes off its path here, and whether it is the owner by tailnet
@@ -764,7 +769,7 @@ export function createApp({
764
769
  app.use('/api', (req, res) => fail(res, 404, 'not_found', 'No such API route.'))
765
770
 
766
771
  // This stays behind the same Host and remote-access gates as the board.
767
- mountLocalConsole(app, consoleDir, { accounts: Boolean(account) })
772
+ mountLocalConsole(app, consoleDir, { accounts: Boolean(account), errors })
768
773
 
769
774
  // The client: hashed assets cached forever, index.html never.
770
775
  if (fs.existsSync(path.join(clientDir, 'index.html'))) {
@@ -774,22 +779,14 @@ export function createApp({
774
779
  )
775
780
  // Mark only a paired-owner server's shell: static exports, hosted and
776
781
  // demo clients must never probe the local pairing endpoint.
777
- if (remote || account)
782
+ const shellMeta = `${remote ? '<meta name="shapewall-remote">\n' : ''}${account ? '<meta name="shapewall-local-accounts">\n' : ''}${errorMeta(errors)}`
783
+ if (shellMeta)
778
784
  app.get(['/', '/index.html'], (req, res) => {
779
785
  res.setHeader('Content-Security-Policy', APP_CSP)
780
786
  res.setHeader('Permissions-Policy', 'camera=(), microphone=(self), geolocation=()')
781
787
  res.setHeader('X-Frame-Options', 'DENY')
782
788
  res.setHeader('Cache-Control', 'no-cache')
783
- res
784
- .type('html')
785
- .send(
786
- fs
787
- .readFileSync(path.join(clientDir, 'index.html'), 'utf8')
788
- .replace(
789
- '</head>',
790
- `${remote ? '<meta name="shapewall-remote">\n' : ''}${account ? '<meta name="shapewall-local-accounts">\n' : ''}</head>`,
791
- ),
792
- )
789
+ res.type('html').send(fs.readFileSync(path.join(clientDir, 'index.html'), 'utf8').replace('</head>', `${shellMeta}</head>`))
793
790
  })
794
791
  app.use(
795
792
  express.static(clientDir, {
@@ -812,6 +809,10 @@ export function createApp({
812
809
  res.setHeader('Permissions-Policy', 'camera=(), microphone=(self), geolocation=()')
813
810
  res.setHeader('X-Frame-Options', 'DENY')
814
811
  res.setHeader('Cache-Control', 'no-cache')
812
+ if (errors)
813
+ return res
814
+ .type('html')
815
+ .send(fs.readFileSync(path.join(clientDir, 'index.html'), 'utf8').replace('</head>', `${errorMeta(errors)}</head>`))
815
816
  // root keeps the dotfile check to the file name, so an install under a
816
817
  // hidden folder (~/.local/...) still serves.
817
818
  res.sendFile('index.html', { root: clientDir })
@@ -824,7 +825,10 @@ export function createApp({
824
825
  // eslint-disable-next-line no-unused-vars
825
826
  app.use((error, req, res, next) => {
826
827
  const status = Number(error.status || error.statusCode) || 500
827
- if (status >= 500) log(`error: ${error.stack || error.message}`)
828
+ if (status >= 500) {
829
+ log(`error: ${error.stack || error.message}`)
830
+ reportServerError(error)
831
+ }
828
832
  if (req.path.startsWith('/api/')) {
829
833
  if (error instanceof CommentError && status < 500) return fail(res, status, error.code, error.message)
830
834
  return fail(
@@ -4,23 +4,27 @@ import fs from 'node:fs'
4
4
  import path from 'node:path'
5
5
  import { randomBytes } from 'node:crypto'
6
6
  import express from 'express'
7
+ import { cspWithErrors, errorMeta } from '../error-tracking.js'
7
8
 
8
9
  const CSP =
9
10
  "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; frame-src 'none'; frame-ancestors 'none'; base-uri 'none'; form-action 'none'; object-src 'none'"
10
11
 
11
- export function mountLocalConsole(app, consoleDir, { accounts = false } = {}) {
12
+ export function mountLocalConsole(app, consoleDir, { accounts = false, errors = null } = {}) {
12
13
  app.get([/^\/console(?:\/.*)?$/, '/account', '/signin', '/signup', '/_console/console.html'], (req, res) => {
13
14
  res.setHeader('Cache-Control', 'no-store')
14
15
  res.setHeader('X-Frame-Options', 'DENY')
15
16
  const nonce = randomBytes(18).toString('base64')
16
- res.setHeader('Content-Security-Policy', CSP.replace("style-src 'self'", `style-src 'self' 'nonce-${nonce}'`))
17
+ res.setHeader('Content-Security-Policy', cspWithErrors(CSP, errors).replace("style-src 'self'", `style-src 'self' 'nonce-${nonce}'`))
17
18
  const file = path.join(consoleDir, 'console.html')
18
19
  if (!fs.existsSync(file)) return res.status(503).type('text').send('The Shapewall console is not built yet. Run "npm run build".')
19
20
  res.type('html').send(
20
21
  fs
21
22
  .readFileSync(file, 'utf8')
22
23
  .replace('__SW_NONCE__', nonce)
23
- .replace('</head>', `<meta name="shapewall-local-console">\n${accounts ? '<meta name="shapewall-local-accounts">\n' : ''}</head>`),
24
+ .replace(
25
+ '</head>',
26
+ `<meta name="shapewall-local-console">\n${accounts ? '<meta name="shapewall-local-accounts">\n' : ''}${errorMeta(errors)}</head>`,
27
+ ),
24
28
  )
25
29
  })
26
30
  app.use('/_console/assets', express.static(path.join(consoleDir, 'assets'), { immutable: true, maxAge: '1y', fallthrough: false }))
package/src/watch.js CHANGED
@@ -383,6 +383,12 @@ export async function watchComments({
383
383
  log(
384
384
  `Watching board "${board.id}" for new comments${argv ? ` (running ${path.basename(argv[0])}, ${concurrency} at a time)` : ''}${claim ? '' : ', without claiming them'}. Ctrl+C stops.`,
385
385
  )
386
+ // Printing claims too: each comment printed stays queued until whatever
387
+ // reads stdout moves it on, so it is never handed out twice.
388
+ if (claim && !argv)
389
+ log(
390
+ 'Each comment printed is marked queued. Whatever reads them sets its status ("shapewall comments status ID working|done|needs-human"), or "status ID clear" hands it out again; --no-claim marks nothing.',
391
+ )
386
392
  await runScan()
387
393
  while (!stopped) await new Promise((resolve) => (wake = resolve))
388
394
  }