@soffits/dsh-card-board 0.6.0 → 0.7.1

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.
@@ -8,6 +8,8 @@
8
8
  "sam"
9
9
  ],
10
10
  "order": [
11
+ "summary",
12
+ "countdown",
11
13
  "weather",
12
14
  "savings",
13
15
  "energy",
@@ -24,6 +26,10 @@
24
26
  "next-weeks": {
25
27
  "w": 2,
26
28
  "h": 1
29
+ },
30
+ "countdown": {
31
+ "w": 2,
32
+ "h": 1
27
33
  }
28
34
  }
29
35
  }
@@ -13,6 +13,10 @@
13
13
  "label": {
14
14
  "en": "done",
15
15
  "zh": "已完成"
16
+ },
17
+ "sub": {
18
+ "en": "2 left today",
19
+ "zh": "今天还剩 2 件"
16
20
  }
17
21
  },
18
22
  "rows": [
@@ -21,7 +25,9 @@
21
25
  "en": "Water the plants",
22
26
  "zh": "给植物浇水"
23
27
  },
24
- "checked": true
28
+ "checked": true,
29
+ "key": "chore-1",
30
+ "icon": "flower-2"
25
31
  },
26
32
  {
27
33
  "title": {
@@ -59,7 +65,9 @@
59
65
  "kind": "snooze",
60
66
  "id": "library-books:snooze"
61
67
  }
62
- ]
68
+ ],
69
+ "key": "chore-2",
70
+ "icon": "book-open"
63
71
  },
64
72
  {
65
73
  "title": {
@@ -90,12 +98,16 @@
90
98
  "prompt": "Help me write a short, polite follow-up to the landlord about the heater."
91
99
  }
92
100
  }
93
- ]
101
+ ],
102
+ "key": "chore-3",
103
+ "icon": "trash-2"
94
104
  }
95
105
  ],
96
106
  "empty": {
97
107
  "en": "Nothing left for today.",
98
108
  "zh": "今天没有待办了。"
99
109
  }
100
- }
110
+ },
111
+ "icon": "list-todo",
112
+ "tone": "accent"
101
113
  }
@@ -0,0 +1,57 @@
1
+ {
2
+ "type": "countdown",
3
+ "title": {
4
+ "en": "Trip to the coast",
5
+ "zh": "去海边"
6
+ },
7
+ "updatedAt": "2026-03-02T09:30:00Z",
8
+ "icon": "train-front",
9
+ "tone": "accent",
10
+ "data": {
11
+ "to": "2026-03-04T07:40:00+01:00",
12
+ "label": {
13
+ "en": "until the train leaves",
14
+ "zh": "距离火车出发"
15
+ },
16
+ "sub": {
17
+ "en": "3 h 10 m, one change",
18
+ "zh": "3 小时 10 分,换乘一次"
19
+ },
20
+ "steps": [
21
+ {
22
+ "at": "2026-03-04T07:15:00+01:00",
23
+ "label": {
24
+ "en": "At the station",
25
+ "zh": "到车站"
26
+ }
27
+ },
28
+ {
29
+ "at": "2026-03-04T07:40:00+01:00",
30
+ "label": {
31
+ "en": "Train to Lyon",
32
+ "zh": "去里昂的火车"
33
+ }
34
+ },
35
+ {
36
+ "at": "2026-03-04T09:20:00+01:00",
37
+ "label": {
38
+ "en": "Change in Lyon",
39
+ "zh": "里昂换乘"
40
+ }
41
+ },
42
+ {
43
+ "at": "2026-03-04T10:50:00+01:00",
44
+ "label": {
45
+ "en": "Arrive in Marseille",
46
+ "zh": "到马赛"
47
+ },
48
+ "sub": {
49
+ "en": "14° and sunny",
50
+ "zh": "14°,晴"
51
+ },
52
+ "zone": "Europe/Paris"
53
+ }
54
+ ],
55
+ "expiresAt": "2026-03-04T10:50:00+01:00"
56
+ }
57
+ }
@@ -17,7 +17,9 @@
17
17
  "en": "dry",
18
18
  "zh": "干燥"
19
19
  },
20
- "tone": "ok"
20
+ "tone": "ok",
21
+ "key": "house-1",
22
+ "icon": "droplets"
21
23
  },
22
24
  {
23
25
  "label": {
@@ -35,7 +37,9 @@
35
37
  20.6,
36
38
  20.3,
37
39
  20.5
38
- ]
40
+ ],
41
+ "key": "house-2",
42
+ "icon": "thermometer"
39
43
  },
40
44
  {
41
45
  "label": {
@@ -60,7 +64,9 @@
60
64
  "zh": "上锁"
61
65
  }
62
66
  }
63
- ]
67
+ ],
68
+ "key": "house-3",
69
+ "icon": "lock"
64
70
  },
65
71
  {
66
72
  "label": {
@@ -71,7 +77,9 @@
71
77
  "en": "offline",
72
78
  "zh": "离线"
73
79
  },
74
- "tone": "bad"
80
+ "tone": "bad",
81
+ "key": "house-4",
82
+ "icon": "plug"
75
83
  }
76
84
  ]
77
85
  },
@@ -87,5 +95,7 @@
87
95
  "url": "https://example.com/home-panel"
88
96
  }
89
97
  }
90
- ]
98
+ ],
99
+ "icon": "house",
100
+ "tone": "ok"
91
101
  }
@@ -75,5 +75,7 @@
75
75
  }
76
76
  }
77
77
  ]
78
- }
78
+ },
79
+ "icon": "piggy-bank",
80
+ "tone": "ok"
79
81
  }
@@ -0,0 +1,50 @@
1
+ {
2
+ "type": "summary",
3
+ "title": {
4
+ "en": "Summary",
5
+ "zh": "概览"
6
+ },
7
+ "updatedAt": "2026-03-02T09:30:00Z",
8
+ "staleAfter": 7200,
9
+ "data": {
10
+ "text": {
11
+ "en": "A quiet day: the library books are due, and the train leaves the day after tomorrow.",
12
+ "zh": "今天比较轻松:图书馆的书今天到期,后天坐火车。"
13
+ },
14
+ "chips": [
15
+ {
16
+ "icon": "train-front",
17
+ "text": {
18
+ "en": "Train",
19
+ "zh": "火车"
20
+ },
21
+ "tone": "accent",
22
+ "to": "2026-03-04T07:40:00+01:00"
23
+ },
24
+ {
25
+ "icon": "book-open",
26
+ "text": {
27
+ "en": "Books due today",
28
+ "zh": "书今天到期"
29
+ },
30
+ "tone": "warn"
31
+ },
32
+ {
33
+ "icon": "cloud-sun",
34
+ "text": {
35
+ "en": "6° and bright",
36
+ "zh": "6°,晴间多云"
37
+ },
38
+ "tone": "warn"
39
+ },
40
+ {
41
+ "icon": "circle-check",
42
+ "text": {
43
+ "en": "1 of 3 done",
44
+ "zh": "完成 1/3"
45
+ },
46
+ "tone": "ok"
47
+ }
48
+ ]
49
+ }
50
+ }
@@ -16,14 +16,50 @@
16
16
  "value": 4.5,
17
17
  "unit": "°C",
18
18
  "delta": {
19
- "value": -2.1,
20
- "unit": "°C",
21
- "tone": "accent"
19
+ "text": {
20
+ "en": "2.1° colder than yesterday",
21
+ "zh": "比昨天冷 2.1°"
22
+ },
23
+ "tone": "accent",
24
+ "dir": "down"
22
25
  },
23
26
  "sub": {
24
27
  "en": "feels like 1 °C",
25
28
  "zh": "体感 1 °C"
26
- }
29
+ },
30
+ "strip": [
31
+ {
32
+ "label": "10",
33
+ "value": 4.8,
34
+ "icon": "cloud-sun"
35
+ },
36
+ {
37
+ "label": "11",
38
+ "value": 5.6,
39
+ "icon": "sun"
40
+ },
41
+ {
42
+ "label": "12",
43
+ "value": 6.2,
44
+ "icon": "sun",
45
+ "on": true
46
+ },
47
+ {
48
+ "label": "13",
49
+ "value": 5.9,
50
+ "icon": "cloud-sun"
51
+ },
52
+ {
53
+ "label": "14",
54
+ "value": 5.1,
55
+ "icon": "cloud"
56
+ },
57
+ {
58
+ "label": "15",
59
+ "value": 4.3,
60
+ "icon": "cloud-drizzle"
61
+ }
62
+ ]
27
63
  },
28
64
  {
29
65
  "label": {
@@ -34,5 +70,7 @@
34
70
  "unit": "%"
35
71
  }
36
72
  ]
37
- }
73
+ },
74
+ "icon": "cloud-sun",
75
+ "tone": "warn"
38
76
  }
package/index.js CHANGED
@@ -15,6 +15,8 @@ import { createPeople, expandHome } from './src/access.js'
15
15
  import { createMessages } from './src/messages.js'
16
16
  import { createPresses } from './src/presses.js'
17
17
  import { createRoutes } from './src/routes.js'
18
+ import { createSeenStore } from './src/seen.js'
19
+ import { createWatches } from './src/watch.js'
18
20
 
19
21
  export const name = 'dsh-card-board'
20
22
  export const inject = ['connection']
@@ -34,7 +36,7 @@ export const Config = z.object({
34
36
  peopleKey: z.string().default('')
35
37
  .description('The top-level key in peopleFile that holds the mapping. Empty means the whole file is the mapping.'),
36
38
  pollSeconds: z.natural().min(5).max(3600).default(30)
37
- .description('How often an open, visible page checks for changes.'),
39
+ .description('How often an open, visible page checks for changes while its live connection is down.'),
38
40
  actionLog: z.string().default('')
39
41
  .description('Where presses of done, snooze and event actions are appended (JSON lines). Empty means .actions.jsonl in dataDir.'),
40
42
  actionTimeoutSeconds: z.natural().min(10).default(300)
@@ -65,12 +67,16 @@ export function apply(ctx, config) {
65
67
  timeoutSeconds: config.actionTimeoutSeconds ?? 300,
66
68
  warn: message => logger.warn(message),
67
69
  })
70
+ const watches = createWatches()
71
+ ctx.on('dispose', () => watches.close())
68
72
  const routes = createRoutes({
69
73
  dataDir,
70
74
  people,
71
75
  identity: () => ctx.get('requestIdentity'),
72
76
  pollSeconds: config.pollSeconds ?? 30,
73
77
  presses,
78
+ seen: createSeenStore(join(dataDir, '.seen')),
79
+ watches,
74
80
  messages: createMessages(fileURLToPath(new URL('./locale', import.meta.url))),
75
81
  version,
76
82
  })
package/locale/en.json CHANGED
@@ -6,8 +6,6 @@
6
6
  "ui": {
7
7
  "panel": "Boards",
8
8
  "title": "Boards",
9
- "updated": "Updated {time}",
10
- "refreshFailed": "Couldn’t refresh; trying again",
11
9
  "loading": "Loading…",
12
10
  "emptyTitle": "No boards yet.",
13
11
  "emptyHint": "A board shows up here once a script writes its board.json into the data folder.",
@@ -28,6 +26,14 @@
28
26
  "summary.latestName": "latest",
29
27
  "summary.goal": "goal: {value}",
30
28
  "summary.progress": "progress: {done}/{total}",
31
- "action.chatNamed": "Ask {name}"
29
+ "action.chatNamed": "Ask {name}",
30
+ "now": "now",
31
+ "fromTime": "from {time}",
32
+ "ofTotal": "of {total}",
33
+ "dayLine": "{day} {part}",
34
+ "dayPart.morning": "morning",
35
+ "dayPart.afternoon": "afternoon",
36
+ "dayPart.evening": "evening",
37
+ "dayPart.night": "night"
32
38
  }
33
39
  }
package/locale/fr.json CHANGED
@@ -6,8 +6,6 @@
6
6
  "ui": {
7
7
  "panel": "Tableaux",
8
8
  "title": "Tableaux",
9
- "updated": "Mis à jour {time}",
10
- "refreshFailed": "Actualisation impossible, nouvelle tentative en cours",
11
9
  "loading": "Chargement…",
12
10
  "emptyTitle": "Aucun tableau pour l’instant.",
13
11
  "emptyHint": "Un tableau apparaît ici dès qu’un script écrit son board.json dans le dossier de données.",
@@ -28,6 +26,14 @@
28
26
  "summary.latestName": "dernière valeur",
29
27
  "summary.goal": "objectif : {value}",
30
28
  "summary.progress": "progression : {done}/{total}",
31
- "action.chatNamed": "Demander à {name}"
29
+ "action.chatNamed": "Demander à {name}",
30
+ "now": "maintenant",
31
+ "fromTime": "à partir de {time}",
32
+ "ofTotal": "sur {total}",
33
+ "dayLine": "{day} {part}",
34
+ "dayPart.morning": "matin",
35
+ "dayPart.afternoon": "après-midi",
36
+ "dayPart.evening": "soir",
37
+ "dayPart.night": "nuit"
32
38
  }
33
39
  }
package/locale/zh.json CHANGED
@@ -6,8 +6,6 @@
6
6
  "ui": {
7
7
  "panel": "面板",
8
8
  "title": "面板",
9
- "updated": "{time} 更新",
10
- "refreshFailed": "刷新失败,稍后重试",
11
9
  "loading": "加载中…",
12
10
  "emptyTitle": "还没有面板。",
13
11
  "emptyHint": "脚本把 board.json 写进数据目录后,面板就会出现在这里。",
@@ -28,6 +26,14 @@
28
26
  "summary.latestName": "最新",
29
27
  "summary.goal": "目标:{value}",
30
28
  "summary.progress": "进度:{done}/{total}",
31
- "action.chatNamed": "问问 {name}"
29
+ "action.chatNamed": "问问 {name}",
30
+ "now": "现在",
31
+ "fromTime": "{time} 起",
32
+ "ofTotal": "共 {total}",
33
+ "dayLine": "{day}{part}",
34
+ "dayPart.morning": "上午",
35
+ "dayPart.afternoon": "下午",
36
+ "dayPart.evening": "傍晚",
37
+ "dayPart.night": "夜里"
32
38
  }
33
39
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soffits/dsh-card-board",
3
- "version": "0.6.0",
3
+ "version": "0.7.1",
4
4
  "description": "A data-driven card board for DeepSeek Harness: scripts write small JSON cards, the Web UI shows each person the boards they may see.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "files": [
37
37
  "bin",
38
- "src",
38
+ "src/*.js",
39
39
  "client.js",
40
40
  "index.js",
41
41
  "cordis.patch.yml",
@@ -57,6 +57,7 @@
57
57
  "@deepseek-ai/dsh-client-locale",
58
58
  "@deepseek-ai/dsh-client-ui-layout",
59
59
  "@deepseek-ai/dsh-client-ui-sidebar",
60
+ "@soffits/dsh-ui-kit",
60
61
  "@soffits/dsh-ui-actions"
61
62
  ]
62
63
  }
@@ -64,10 +65,11 @@
64
65
  "icon": "./icon.svg",
65
66
  "engines": {
66
67
  "node": ">=22",
67
- "dsh": ">=0.2.1-rc.2 <0.3.0"
68
+ "dsh": ">=0.2.2-rc.1 <0.3.0-0"
68
69
  },
69
70
  "scripts": {
70
- "check": "for f in index.js client.js bin/*.js src/*.js test/*.js; do node --check \"$f\" || exit 1; done && node bin/dsh-card-board.js check --dir examples/boards",
71
+ "build": "node scripts/build.mjs",
72
+ "check": "node scripts/build.mjs --check && for f in index.js client.js bin/*.js src/*.js src/client/*.js test/*.js; do node --check \"$f\" || exit 1; done && node bin/dsh-card-board.js check --dir examples/boards",
71
73
  "test": "node --test test/*.test.js"
72
74
  },
73
75
  "dependencies": {
@@ -78,6 +80,8 @@
78
80
  "@deepseek-ai/dsh-client-connection": "0.2.2-rc.1",
79
81
  "@deepseek-ai/dsh-credentials": "0.2.2-rc.1",
80
82
  "@deepseek-ai/dsh-host-webserver": "0.2.2-rc.1",
81
- "acorn": "^8.19.0"
83
+ "acorn": "^8.19.0",
84
+ "esbuild": "^0.28.2",
85
+ "@deepseek-ai/dsh-client-ui-primitives": "0.2.2-rc.1"
82
86
  }
83
87
  }
package/src/routes.js CHANGED
@@ -1,24 +1,37 @@
1
1
  /**
2
- * The two read-only routes the page polls. They are exact Fetch routes under
3
- * DSH's `/api`, so DSH admits the browser first and a `requestIdentity`
4
- * provider sees the request; every answer is decided here, per request, from
5
- * the server's own view of who is asking.
2
+ * The page's routes. They are exact Fetch routes under DSH's `/api`, so DSH
3
+ * admits the browser first and a `requestIdentity` provider sees the request;
4
+ * every answer is decided here, per request, from the server's own view of who
5
+ * is asking.
6
6
  *
7
- * GET /api/card-board/boards the boards this viewer may see
8
- * GET /api/card-board/board?id=<id> one board with its cards (ETag, 304)
9
- * GET /api/card-board/locale?tag=fr the page's own words for a language (from locale/*.json)
10
- * POST /api/card-board/action press a done, snooze or event action
7
+ * GET /api/card-board/boards the boards this viewer may see
8
+ * GET /api/card-board/board?id=<id> one board with its cards (ETag, 304)
9
+ * GET /api/card-board/card?board=<id>&id=<card> one card (ETag, 304)
10
+ * GET /api/card-board/events?board=<id> Server-Sent Events: which card or board.json changed
11
+ * POST /api/card-board/seen the card versions the viewer has seen
12
+ * GET /api/card-board/locale?tag=fr the page's own words for a language (from locale/*.json)
13
+ * POST /api/card-board/action press a step, done, snooze or event action
14
+ *
15
+ * Every card entry carries its content `version` (and `rows`, the versions of its keyed rows) and, when something
16
+ * in it is new since the viewer last looked, `fresh: { card: true, rows: [keys] }`.
11
17
  */
18
+ import { join } from 'node:path'
12
19
  import { maySee, viewerOf } from './access.js'
13
20
  import { LANGUAGE_ID } from './messages.js'
14
21
  import { ID_PATTERN, PRESS_KINDS, findAction } from './schema.js'
15
- import { boardTag, cardPath, listBoards, readBoard, readCard, readCards, resolveDataDir } from './store.js'
22
+ import { cardVersions, freshness, parseSeen } from './seen.js'
23
+ import { boardTag, cardPath, cardTag, listBoards, readBoard, readCard, readCards, resolveDataDir } from './store.js'
16
24
 
17
25
  export const BOARDS_PATH = '/api/card-board/boards'
18
26
  export const BOARD_PATH = '/api/card-board/board'
27
+ export const CARD_PATH = '/api/card-board/card'
28
+ export const EVENTS_PATH = '/api/card-board/events'
29
+ export const SEEN_PATH = '/api/card-board/seen'
19
30
  export const ACTION_PATH = '/api/card-board/action'
20
31
  export const LOCALE_PATH = '/api/card-board/locale'
21
32
  const DATE_TIME = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:\d{2})$/
33
+ /** A comment line now and then keeps proxies from closing a quiet stream. */
34
+ export const HEARTBEAT_MS = 25_000
22
35
 
23
36
  const HEADERS = {
24
37
  'content-type': 'application/json; charset=utf-8',
@@ -39,10 +52,35 @@ const NO_PROVIDER = 'This board needs a sign-in plugin that provides requestIden
39
52
  * @param {() => object | undefined} options.identity - the requestIdentity service right now, if any
40
53
  * @param {number} options.pollSeconds
41
54
  * @param {ReturnType<import('./presses.js').createPresses>} options.presses
55
+ * @param {ReturnType<import('./seen.js').createSeenStore>} options.seen
56
+ * @param {ReturnType<import('./watch.js').createWatches>} options.watches
42
57
  * @param {(tag: string) => Promise<object>} options.messages - the page's words for a language
43
58
  * @param {string} [options.version] - mixed into ETags, so a plugin update refreshes open pages
59
+ * @param {number} [options.heartbeatMs]
44
60
  */
45
- export function createRoutes({ dataDir, people, identity, pollSeconds, presses, messages, version = '' }) {
61
+ export function createRoutes({ dataDir, people, identity, pollSeconds, presses, seen, watches, messages, version = '', heartbeatMs = HEARTBEAT_MS }) {
62
+ /** The viewer and their board, or the Response that refuses them. */
63
+ async function access(board) {
64
+ const viewer = await viewerOf(people, identity())
65
+ if (viewer.unavailable) return { refused: json(503, { error: NO_PROVIDER }) }
66
+ const root = await resolveDataDir(dataDir)
67
+ const entry = root === undefined ? undefined : await readBoard(root, board)
68
+ // In people mode a missing board and a forbidden one answer the same, so ids don't leak.
69
+ if (viewer.mode === 'people' && !maySee(viewer, entry?.board)) return { refused: json(403, { error: 'You can’t see this board.' }) }
70
+ if (entry === undefined) return { refused: json(404, { error: 'No such board.' }) }
71
+ return { viewer, root, entry, person: viewer.mode === 'owner' ? 'owner' : viewer.person }
72
+ }
73
+
74
+ /** A card entry with its versions, its press states and what is new in it for this viewer. */
75
+ function decorate(card, states, seenBoard) {
76
+ let entry = states[card.id] ? { ...card, presses: states[card.id] } : card
77
+ if (card.card === undefined) return entry
78
+ const versions = cardVersions(card.card)
79
+ entry = { ...entry, ...versions }
80
+ const fresh = freshness(seenBoard, { id: card.id, ...versions })
81
+ return fresh ? { ...entry, fresh } : entry
82
+ }
83
+
46
84
  async function locale(request) {
47
85
  const tag = new URL(request.url).searchParams.get('tag') ?? 'en'
48
86
  if (!LANGUAGE_ID.test(tag)) return json(400, { error: 'Not a language id.' })
@@ -64,20 +102,95 @@ export function createRoutes({ dataDir, people, identity, pollSeconds, presses,
64
102
  async function board(request) {
65
103
  const id = new URL(request.url).searchParams.get('id') ?? ''
66
104
  if (!ID_PATTERN.test(id)) return json(400, { error: 'Not a board id.' })
67
- const viewer = await viewerOf(people, identity())
68
- if (viewer.unavailable) return json(503, { error: NO_PROVIDER })
69
- const root = await resolveDataDir(dataDir)
70
- const entry = root === undefined ? undefined : await readBoard(root, id)
71
- // In people mode a missing board and a forbidden one answer the same, so ids don't leak.
72
- if (viewer.mode === 'people' && !maySee(viewer, entry?.board)) return json(403, { error: 'You can’t see this board.' })
73
- if (entry === undefined) return json(404, { error: 'No such board.' })
105
+ const found = await access(id)
106
+ if (found.refused) return found.refused
107
+ const { viewer, root, entry, person } = found
74
108
  const states = await pressStates(root, id)
75
- const tag = await boardTag(root, id, `${version}\0${viewer.mode}\0${viewer.person ?? ''}\0${JSON.stringify(states)}`)
109
+ const tag = await boardTag(root, id, `${version}\0${viewer.mode}\0${person ?? ''}\0${await seen.tag(person)}\0${JSON.stringify(states)}`)
76
110
  if (request.headers.get('if-none-match') === tag) return new Response(null, { status: 304, headers: { ...HEADERS, etag: tag } })
77
- const cards = (await readCards(root, id, entry.board)).map(card => (states[card.id] ? { ...card, presses: states[card.id] } : card))
111
+ const seenBoard = await seen.board(person, id)
112
+ const cards = (await readCards(root, id, entry.board)).map(card => decorate(card, states, seenBoard))
78
113
  return json(200, { id, title: entry.board?.title ?? id, ...(entry.board?.assistant ? { assistant: entry.board.assistant } : {}), problems: entry.problems, cards }, { etag: tag })
79
114
  }
80
115
 
116
+ async function card(request) {
117
+ const params = new URL(request.url).searchParams
118
+ const boardId = params.get('board') ?? ''
119
+ const id = params.get('id') ?? ''
120
+ if (!ID_PATTERN.test(boardId) || !ID_PATTERN.test(id)) return json(400, { error: 'Say which board and card.' })
121
+ const found = await access(boardId)
122
+ if (found.refused) return found.refused
123
+ const { viewer, root, entry, person } = found
124
+ const states = await pressStates(root, boardId)
125
+ const tag = await cardTag(root, boardId, id, `${version}\0${viewer.mode}\0${person ?? ''}\0${await seen.tag(person)}\0${JSON.stringify(states[id] ?? null)}`)
126
+ if (request.headers.get('if-none-match') === tag) return new Response(null, { status: 304, headers: { ...HEADERS, etag: tag } })
127
+ const one = (await readCards(root, boardId, entry.board, id))[0]
128
+ if (one === undefined) return json(404, { error: 'No such card.' })
129
+ return json(200, decorate(one, states, await seen.board(person, boardId)), { etag: tag })
130
+ }
131
+
132
+ async function events(request) {
133
+ const id = new URL(request.url).searchParams.get('board') ?? ''
134
+ if (!ID_PATTERN.test(id)) return json(400, { error: 'Not a board id.' })
135
+ const found = await access(id)
136
+ if (found.refused) return found.refused
137
+ const encoder = new TextEncoder()
138
+ let finish = () => {}
139
+ const body = new ReadableStream({
140
+ async start(controller) {
141
+ let open = true
142
+ const send = text => { if (open) controller.enqueue(encoder.encode(text)) }
143
+ const data = value => send(`data: ${JSON.stringify(value)}\n\n`)
144
+ let unwatch = () => {}
145
+ const heartbeat = setInterval(() => send(': ping\n\n'), heartbeatMs)
146
+ finish = () => {
147
+ if (!open) return
148
+ open = false
149
+ clearInterval(heartbeat)
150
+ unwatch()
151
+ request.signal?.removeEventListener?.('abort', finish)
152
+ }
153
+ request.signal?.addEventListener?.('abort', finish)
154
+ send(': connected\n\n')
155
+ unwatch = await watches.watch(join(found.root, id), async change => {
156
+ if (change.type === 'board') {
157
+ // Viewers may have changed: someone who may no longer see the board hears that and nothing else.
158
+ const again = await access(id)
159
+ if (again.refused) {
160
+ data({ type: 'gone' })
161
+ finish()
162
+ try { controller.close() } catch { /* already closed */ }
163
+ return
164
+ }
165
+ }
166
+ data(change)
167
+ })
168
+ if (!open) unwatch()
169
+ else data({ type: 'hello' })
170
+ },
171
+ cancel() { finish() },
172
+ })
173
+ return new Response(body, {
174
+ status: 200,
175
+ headers: { 'content-type': 'text/event-stream; charset=utf-8', 'cache-control': 'no-cache, no-transform', 'x-accel-buffering': 'no', 'x-content-type-options': 'nosniff' },
176
+ })
177
+ }
178
+
179
+ async function seenRoute(request) {
180
+ let body
181
+ try {
182
+ body = parseSeen(await request.json())
183
+ } catch {
184
+ body = undefined
185
+ }
186
+ if (body === undefined) return json(400, { error: 'The body must be { board, cards: { <id>: { v, rows? } } }.' })
187
+ const found = await access(body.board)
188
+ if (found.refused) return found.refused
189
+ const present = new Set((await readCards(found.root, body.board, found.entry.board)).map(entry => entry.id))
190
+ await seen.record(found.person, body.board, body.cards, present)
191
+ return json(200, { ok: true })
192
+ }
193
+
81
194
  /** Press states of every card on a board that has recent presses, by card id. */
82
195
  async function pressStates(root, board) {
83
196
  const cards = new Set((await presses.list()).filter(event => event.source.board === board).map(event => event.source.card))
@@ -125,6 +238,9 @@ export function createRoutes({ dataDir, people, identity, pollSeconds, presses,
125
238
  return [
126
239
  { path: BOARDS_PATH, methods: ['GET'], requestBody: 'buffered', fetch: boards },
127
240
  { path: BOARD_PATH, methods: ['GET'], requestBody: 'buffered', fetch: board },
241
+ { path: CARD_PATH, methods: ['GET'], requestBody: 'buffered', fetch: card },
242
+ { path: EVENTS_PATH, methods: ['GET'], requestBody: 'buffered', fetch: events },
243
+ { path: SEEN_PATH, methods: ['POST'], requestBody: 'buffered', fetch: seenRoute },
128
244
  { path: LOCALE_PATH, methods: ['GET'], requestBody: 'buffered', fetch: locale },
129
245
  { path: ACTION_PATH, methods: ['POST'], requestBody: 'buffered', fetch: action },
130
246
  ]