@bigmistqke/whiteboard 0.3.1 → 0.5.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 (174) hide show
  1. package/README.md +119 -18
  2. package/bin/whiteboard.ts +300 -71
  3. package/dist/bin/whiteboard.js +21 -0
  4. package/dist/client/runtime.js +132 -54
  5. package/dist/define.js +21 -2
  6. package/dist/plugins/callout/index.js +3 -1
  7. package/dist/plugins/callout/public/style.css +30 -0
  8. package/dist/plugins/chat/index.js +7 -4
  9. package/dist/plugins/chat/{client.js → public/client.js} +1 -1
  10. package/dist/plugins/chat/public/style.css +98 -0
  11. package/dist/plugins/code/index.js +3 -1
  12. package/dist/plugins/code/public/style.css +68 -0
  13. package/dist/plugins/comments/index.js +4 -5
  14. package/dist/plugins/comments/public/client.js +19 -0
  15. package/dist/plugins/comments/public/style.css +185 -0
  16. package/dist/plugins/details/index.js +7 -2
  17. package/dist/plugins/details/public/style.css +7 -0
  18. package/dist/plugins/draft/README.md +14 -0
  19. package/dist/plugins/draft/index.js +21 -0
  20. package/dist/plugins/draft/public/style.css +21 -0
  21. package/dist/plugins/form/index.js +37 -8
  22. package/dist/plugins/form/public/style.css +126 -0
  23. package/dist/plugins/gallery/index.js +4 -3
  24. package/dist/plugins/gallery/public/client.js +1 -0
  25. package/dist/plugins/gallery/public/style.css +90 -0
  26. package/dist/plugins/grid/index.js +9 -3
  27. package/dist/plugins/grid/public/style.css +71 -0
  28. package/dist/plugins/iframe/index.js +13 -14
  29. package/dist/plugins/iframe/public/style.css +13 -0
  30. package/dist/plugins/index.js +7 -1
  31. package/dist/plugins/mermaid/index.js +3 -1
  32. package/dist/plugins/mermaid/{style.css → public/style.css} +7 -2
  33. package/dist/plugins/steps/README.md +42 -0
  34. package/dist/plugins/steps/index.js +99 -0
  35. package/dist/plugins/steps/public/client.js +1 -0
  36. package/dist/plugins/steps/public/style.css +77 -0
  37. package/dist/plugins/terminal/host.js +9 -3
  38. package/dist/plugins/terminal/index.js +10 -9
  39. package/dist/plugins/terminal/{style.css → public/style.css} +199 -45
  40. package/dist/plugins/terminal/sizes.js +2 -1
  41. package/dist/plugins/toc/README.md +21 -3
  42. package/dist/plugins/toc/index.js +12 -5
  43. package/dist/plugins/toc/public/client.js +1 -0
  44. package/dist/plugins/toc/public/style.css +99 -0
  45. package/dist/plugins/window/README.md +29 -0
  46. package/dist/plugins/window/index.js +64 -0
  47. package/dist/solid/index.js +14 -11
  48. package/dist/src/auth.js +2 -1
  49. package/dist/src/client/page/base.css +1078 -199
  50. package/dist/src/client/page/main.js +188 -39
  51. package/dist/src/loader.js +14 -3
  52. package/dist/src/package.js +3 -6
  53. package/dist/src/pages/blocks.js +8 -13
  54. package/dist/src/pages/markdown.js +1 -1
  55. package/dist/src/pages/render.js +106 -20
  56. package/dist/src/pages/window.js +31 -0
  57. package/dist/src/public.js +24 -0
  58. package/dist/src/server.js +251 -23
  59. package/package.json +5 -2
  60. package/plugins/callout/index.ts +3 -1
  61. package/plugins/callout/public/style.css +30 -0
  62. package/plugins/chat/autosize.ts +43 -9
  63. package/plugins/chat/client.tsx +22 -5
  64. package/plugins/chat/index.ts +22 -8
  65. package/plugins/chat/public/style.css +98 -0
  66. package/plugins/claude-code/index.ts +49 -14
  67. package/plugins/code/diff.ts +28 -8
  68. package/plugins/code/index.ts +33 -21
  69. package/plugins/code/public/style.css +68 -0
  70. package/plugins/comments/client.ts +139 -71
  71. package/plugins/comments/index.ts +9 -7
  72. package/plugins/comments/public/style.css +185 -0
  73. package/plugins/details/index.ts +7 -2
  74. package/plugins/details/public/style.css +7 -0
  75. package/plugins/draft/README.md +14 -0
  76. package/plugins/draft/index.ts +23 -0
  77. package/plugins/draft/public/style.css +21 -0
  78. package/plugins/forge/index.ts +16 -3
  79. package/plugins/form/index.ts +75 -27
  80. package/plugins/form/public/style.css +126 -0
  81. package/plugins/gallery/client.tsx +9 -3
  82. package/plugins/gallery/index.ts +31 -9
  83. package/plugins/gallery/public/style.css +90 -0
  84. package/plugins/grid/index.ts +19 -5
  85. package/plugins/grid/public/style.css +71 -0
  86. package/plugins/iframe/index.ts +20 -23
  87. package/plugins/iframe/public/style.css +13 -0
  88. package/plugins/index.ts +20 -1
  89. package/plugins/mermaid/index.ts +7 -3
  90. package/plugins/mermaid/{style.css → public/style.css} +7 -2
  91. package/plugins/steps/README.md +42 -0
  92. package/plugins/steps/client.ts +66 -0
  93. package/plugins/steps/index.ts +127 -0
  94. package/plugins/steps/public/style.css +77 -0
  95. package/plugins/terminal/client.ts +105 -48
  96. package/plugins/terminal/host.ts +143 -35
  97. package/plugins/terminal/index.ts +84 -25
  98. package/plugins/terminal/public/style.css +225 -0
  99. package/plugins/terminal/server.ts +64 -13
  100. package/plugins/terminal/sizes.ts +16 -4
  101. package/plugins/toc/README.md +21 -3
  102. package/plugins/toc/client.ts +90 -30
  103. package/plugins/toc/index.ts +26 -10
  104. package/plugins/toc/public/style.css +99 -0
  105. package/plugins/window/README.md +29 -0
  106. package/plugins/window/index.ts +85 -0
  107. package/skills/whiteboard/SKILL.md +3 -1
  108. package/solid/index.tsx +24 -9
  109. package/src/auth.ts +28 -9
  110. package/src/client/forms.ts +74 -28
  111. package/src/client/markdown.ts +15 -4
  112. package/src/client/page/base.css +1078 -199
  113. package/src/client/page/elsewhere.ts +32 -15
  114. package/src/client/page/fresh.ts +91 -23
  115. package/src/client/page/hold.ts +11 -2
  116. package/src/client/page/live.ts +10 -6
  117. package/src/client/page/main.ts +124 -42
  118. package/src/client/page/orphans.ts +25 -10
  119. package/src/client/page/sort.ts +14 -4
  120. package/src/client/page/sticky.ts +11 -3
  121. package/src/client/page/theme.ts +11 -7
  122. package/src/client/page/waiting.ts +141 -34
  123. package/src/client/page/window.ts +40 -0
  124. package/src/client/reactive.ts +19 -0
  125. package/src/client/read.ts +17 -10
  126. package/src/client/runtime.ts +170 -40
  127. package/src/client/scroll.ts +14 -4
  128. package/src/client/toast.ts +19 -5
  129. package/src/config.ts +20 -5
  130. package/src/define.ts +119 -31
  131. package/src/events.ts +111 -24
  132. package/src/html.ts +6 -2
  133. package/src/icons.ts +4 -2
  134. package/src/load.ts +10 -2
  135. package/src/loader.ts +81 -12
  136. package/src/package.ts +9 -8
  137. package/src/pages/blocks.ts +20 -14
  138. package/src/pages/index.ts +2 -2
  139. package/src/pages/markdown.ts +113 -34
  140. package/src/pages/render.ts +233 -41
  141. package/src/pages/window.ts +78 -0
  142. package/src/plugins.ts +90 -24
  143. package/src/public.ts +39 -0
  144. package/src/running.ts +8 -2
  145. package/src/serve.ts +29 -7
  146. package/src/server.ts +591 -110
  147. package/src/status.ts +15 -9
  148. package/dist/plugins/callout/style.css +0 -12
  149. package/dist/plugins/chat/style.css +0 -24
  150. package/dist/plugins/code/style.css +0 -29
  151. package/dist/plugins/comments/client.js +0 -10
  152. package/dist/plugins/comments/style.css +0 -40
  153. package/dist/plugins/details/style.css +0 -3
  154. package/dist/plugins/form/style.css +0 -44
  155. package/dist/plugins/gallery/client.js +0 -1
  156. package/dist/plugins/gallery/style.css +0 -27
  157. package/dist/plugins/grid/style.css +0 -25
  158. package/dist/plugins/iframe/client.js +0 -1
  159. package/dist/plugins/iframe/style.css +0 -29
  160. package/dist/plugins/toc/client.js +0 -1
  161. package/dist/plugins/toc/style.css +0 -19
  162. package/plugins/callout/style.css +0 -12
  163. package/plugins/chat/style.css +0 -24
  164. package/plugins/code/style.css +0 -29
  165. package/plugins/comments/style.css +0 -40
  166. package/plugins/details/style.css +0 -3
  167. package/plugins/form/style.css +0 -44
  168. package/plugins/gallery/style.css +0 -27
  169. package/plugins/grid/style.css +0 -25
  170. package/plugins/iframe/client.ts +0 -46
  171. package/plugins/iframe/style.css +0 -29
  172. package/plugins/terminal/style.css +0 -71
  173. package/plugins/toc/style.css +0 -19
  174. /package/dist/plugins/terminal/{client.js → public/client.js} +0 -0
package/src/define.ts CHANGED
@@ -6,10 +6,12 @@
6
6
  // between them and a browser. Everything else is a plugin: the syntax pages are written in (blocks),
7
7
  // behaviour on every page, commands, routes. defineConfig and definePlugin only return their
8
8
  // argument; they exist for types.
9
+ import type { IncomingMessage, ServerResponse } from 'node:http'
9
10
  import type { Html } from './html.ts'
10
11
 
11
12
  // HTML from a template literal, escaped by default: for a block, a plugin's `shell`
12
13
  export { html, raw, escape, Html } from './html.ts'
14
+ export { windowFrame, type WindowOptions } from './pages/window.ts'
13
15
 
14
16
  // ---- config ------------------------------------------------------------------------------------
15
17
 
@@ -30,6 +32,26 @@ export interface Config {
30
32
  plugins?: Plugin[]
31
33
  /** a stylesheet linked on every page, after the built-in ones: the project's own look */
32
34
  style?: URL | string
35
+ /** what `whiteboard build` makes: a static site of some pages, without what is only for writing them */
36
+ build?: BuildConfig
37
+ }
38
+
39
+ /**
40
+ * `whiteboard build`: pages rendered once, as plain HTML files with their images and styles, links
41
+ * between them relative. What is for writing them is left out: the live channel, the event log,
42
+ * and the plugins and blocks in `skip`. What is for reading stays (steps, ⤢, the theme, contents).
43
+ */
44
+ export interface BuildConfig {
45
+ /**
46
+ * Which pages, as globs from the project's folder; `!` leaves out: `['docs/**\/*.md', '!docs/notes/**']`.
47
+ * Each lands in `out` at its place below the folder they share (`docs/api/forms.md` →
48
+ * `api/forms.html`); a README.md or index.md is its folder's index.html.
49
+ */
50
+ pages?: string[]
51
+ /** plugins or blocks left out of the site (default: form, chat, comments, draft) */
52
+ skip?: string[]
53
+ /** the folder the site goes to, from the project's folder (default: site) */
54
+ out?: string
33
55
  }
34
56
 
35
57
  export const defineConfig = (config: Config): Config => config
@@ -56,7 +78,14 @@ export interface Plugin {
56
78
  * go to the block of that name. Block names are unique across a project's plugins.
57
79
  */
58
80
  blocks?: Record<string, Block<any>>
59
- /** a stylesheet linked on every page (for blocks, their own `style` links it only where they're used) */
81
+ /**
82
+ * The folder its browser files are served from, and nothing else of it: its client modules,
83
+ * stylesheets, images, fonts (TypeScript there is served with its types stripped). `client` and
84
+ * `style` here and in its blocks name files in it. definePlugin defaults it to `public/` next to
85
+ * the module that calls it; a plain object plugin has the project's `.whiteboard/public/`.
86
+ */
87
+ public?: URL | string
88
+ /** a stylesheet linked on every page, in its public folder (a block's `style` links only where it is used) */
60
89
  style?: URL | string
61
90
  /**
62
91
  * Text that links somewhere, as written in plain text (`#12` → an issue): the core turns each
@@ -89,7 +118,25 @@ export interface Plugin {
89
118
  session?(): string | null | undefined | Promise<string | null | undefined>
90
119
  }
91
120
 
92
- export const definePlugin = (plugin: Plugin): Plugin => plugin
121
+ /**
122
+ * A plugin, with its public folder defaulted to `public/` next to the module that calls this
123
+ * (found from the call stack), when it doesn't say one.
124
+ */
125
+ export const definePlugin = (plugin: Plugin): Plugin =>
126
+ plugin.public != null ? plugin : { ...plugin, public: callerPublic() }
127
+
128
+ /** `public/` next to the module that called definePlugin: the first frame of the stack in another file. */
129
+ function callerPublic(): URL | undefined {
130
+ const own = import.meta.url
131
+ for (const line of (new Error().stack ?? '').split('\n').slice(1)) {
132
+ const m = /(file:\/\/[^\s)]+?|\/[^\s():]+?):\d+:\d+/.exec(line)
133
+ if (!m) continue
134
+ const url = m[1].startsWith('file:') ? m[1] : `file://${m[1]}`
135
+ if (url === own) continue
136
+ return new URL('./public/', url)
137
+ }
138
+ return undefined
139
+ }
93
140
 
94
141
  /** A pattern in a page's text that links somewhere (Plugin.links). */
95
142
  export interface TextLink {
@@ -127,8 +174,11 @@ export interface PluginContext {
127
174
  project: Project
128
175
  /** the plugin's own folder, `.whiteboard/<name>/` (made, and ignored in git) */
129
176
  dataDir: string
130
- /** whether a request carries the project's token: for a check of its own, beyond a route's `auth` */
131
- authorized(req: import('node:http').IncomingMessage): boolean
177
+ /**
178
+ * whether a request carries the project's token: for a check of its own, beyond a route's `auth`.
179
+ * No request (`shell` for a page being built) has none.
180
+ */
181
+ authorized(req: IncomingMessage | null): boolean
132
182
  /** the URL that serves a file of the plugin (its browser module, a stylesheet) */
133
183
  asset(file: URL | string): string
134
184
  }
@@ -151,13 +201,22 @@ export interface PluginHooks {
151
201
  * length in `--wb-inset-bottom` on :root, registered with `@property` so that it computes to
152
202
  * pixels. The page keeps its own fixed elements clear of it and counts what is behind it as out
153
203
  * of view.
204
+ *
205
+ * `req` is the browser's request for the page; null for a page `whiteboard build` writes, which no
206
+ * browser asked for: what is only for the project's own browser (the terminal's drawer, behind
207
+ * `authorized`) has nothing to add there.
154
208
  */
155
- shell?(req: import('node:http').IncomingMessage): { head?: string | Html; body?: string | Html } | null | Promise<{ head?: string | Html; body?: string | Html } | null>
209
+ shell?(
210
+ req: IncomingMessage | null,
211
+ ):
212
+ | { head?: string | Html; body?: string | Html }
213
+ | null
214
+ | Promise<{ head?: string | Html; body?: string | Html } | null>
156
215
  /**
157
216
  * A browser module whose default export is an `PluginMount`: on every page, the core imports
158
217
  * it once the page has mounted, and hands it the page and its api. For
159
218
  * behaviour on the page's own content (its forms, say); markup of the plugin's own can come
160
- * from `shell` instead. Serve it with `ctx.asset`.
219
+ * from `shell` instead. A file in the plugin's public folder: `'client.js'`.
161
220
  */
162
221
  client?: URL | string
163
222
  }
@@ -182,9 +241,9 @@ export interface Route {
182
241
  export interface RouteRequest {
183
242
  body: Record<string, unknown>
184
243
  query: Record<string, unknown>
185
- req: import('node:http').IncomingMessage
244
+ req: IncomingMessage
186
245
  /** only for a stream route */
187
- res?: import('node:http').ServerResponse
246
+ res?: ServerResponse
188
247
  }
189
248
 
190
249
  /** `whiteboard <plugin> [sub] …` */
@@ -274,6 +333,8 @@ export interface Rendered {
274
333
  export interface RenderedPage extends Rendered {
275
334
  html: string
276
335
  head?: string
336
+ /** the page's frontmatter (see PageHandle.frontmatter) */
337
+ frontmatter: Record<string, unknown>
277
338
  }
278
339
 
279
340
  /** A page as the core stores it: what is in the file, read as is. */
@@ -294,8 +355,10 @@ export interface PageSource extends StoredBase {
294
355
  source: string
295
356
  /** `source` without its frontmatter: the page's content */
296
357
  body: string
297
- /** the frontmatter's fields, when the file starts with one (`source` keeps it) */
298
- frontmatter: Record<string, string>
358
+ /** the frontmatter's fields, when the file starts with one (`source` keeps it): YAML, as parsed */
359
+ frontmatter: Record<string, unknown>
360
+ /** why the frontmatter couldn't be read (it is then empty), shown on the page */
361
+ frontmatterError?: string | null
299
362
  }
300
363
 
301
364
  /** Any other text file in the repository. */
@@ -331,6 +394,10 @@ export interface RenderContext {
331
394
  * not exist or nothing serves it. For relative links: resolve them from the page's folder first.
332
395
  */
333
396
  urlFor(file: string): string | null
397
+ /** false in a build (`whiteboard build`): the page is a file, with no updates and no events */
398
+ live?: boolean
399
+ /** in a build, the plugins and blocks left out: their blocks render as nothing */
400
+ skip?: ReadonlySet<string>
334
401
  }
335
402
 
336
403
  /**
@@ -344,11 +411,27 @@ export interface PageHandle {
344
411
  id: string
345
412
  kind: StoredPage['kind']
346
413
  /**
347
- * what the server render returned with the HTML now in the root, kept current on each live update:
348
- * for a markdown page, `{ islands, styles, mtime, frontmatter }`, the frontmatter's fields as
349
- * strings (`toc: true` is `'true'`)
414
+ * false on a built page (`whiteboard build`): a file, with no server behind it, so no events, no
415
+ * listeners and no updates; only what is for reading runs.
350
416
  */
351
- data: unknown
417
+ live: boolean
418
+ /** what the server render returned with the HTML now in the root; it changes with each live update */
419
+ data: Reactive<unknown>
420
+ /**
421
+ * The page's frontmatter: YAML between `---` lines at its start, as parsed; empty for a page
422
+ * without one, one that doesn't parse, and for the overview, folders and code. It changes with a
423
+ * live update that changed it, once the new page is in place (`onChange` comes before, with the
424
+ * old one still here). The top level is the core's and other tools' (`title`, `tags`); a plugin's
425
+ * own is under `plugins:`, by its name, and is its `settings`.
426
+ */
427
+ frontmatter: Reactive<Record<string, unknown>>
428
+ /**
429
+ * For a plugin's browser module, its own entry in the page's frontmatter, under `plugins:` by its
430
+ * name, as parsed: `plugins: { toc: true }` is `true` for toc, `plugins: { toc: { maxWidth: 240px } }`
431
+ * an object; undefined when the page says nothing for it (and for the page module). It changes
432
+ * when that entry does.
433
+ */
434
+ settings: Reactive<unknown>
352
435
  /** called when the stored page changed; returns an unsubscribe */
353
436
  onChange(fn: () => void): () => void
354
437
  /** the server render, afresh */
@@ -414,14 +497,22 @@ export interface Toast {
414
497
  show(): void
415
498
  }
416
499
 
417
- /** What the core gives the page and every plugin in the browser. Framework-free: getters and subscriptions. */
500
+ /**
501
+ * A value that changes: call it for what it is now, and `.on(fn)` to hear each change, with the
502
+ * new value (it returns an unsubscribe). Every changing value of the api is one, so a framework
503
+ * can turn any of them into its own reactive value with one helper (the Solid one does).
504
+ */
505
+ export interface Reactive<T> {
506
+ (): T
507
+ on(fn: (value: T) => void): () => void
508
+ }
509
+
510
+ /** What the core gives the page and every plugin in the browser. Framework-free: its changing values are Reactive. */
418
511
  export interface Api {
419
512
  /** this page's id */
420
513
  page: string
421
514
  /** every event recorded for this page, oldest first */
422
- events(): WbEvent[]
423
- /** called with the new list whenever it changes; returns an unsubscribe */
424
- onEvents(fn: (events: WbEvent[]) => void): () => void
515
+ events: Reactive<WbEvent[]>
425
516
  /** the latest event of a type, e.g. the last submit of a form */
426
517
  last(type: string): WbEvent | undefined
427
518
  /** record an event for this page: it lands in the log, where the agent reads it */
@@ -433,7 +524,7 @@ export interface Api {
433
524
  * EventSpec.forReader). An element marked `data-wb-message="<event id>"` reads its message once
434
525
  * it has been in view; `markRead` does so for messages read otherwise (a thread closed).
435
526
  */
436
- unread(): WbEvent[]
527
+ unread: Reactive<WbEvent[]>
437
528
  markRead(ids: string[]): void
438
529
  /**
439
530
  * A message's markdown (a comment, a chat message) as HTML, rendered by the server like a page:
@@ -441,8 +532,7 @@ export interface Api {
441
532
  */
442
533
  markdown(text: string): Promise<string>
443
534
  /** the agents waiting for answers on this page right now */
444
- listeners(): Listener[]
445
- onListeners(fn: (listeners: Listener[]) => void): () => void
535
+ listeners: Reactive<Listener[]>
446
536
  /**
447
537
  * Say that the page's content changed in place: the page module calls it after each patch on an update
448
538
  * (it morphs it) calls this after each patch, so that plugins working on the content
@@ -475,11 +565,10 @@ export interface Api {
475
565
  * The page's named forms and where each stands. The core looks after every `<form name>` without
476
566
  * an `action`: it sends a submit as an event, keeps drafts, puts the last answer back, and marks
477
567
  * each form with its state (`data-wb-open`, `-pending`, `-decided`). How that
478
- * looks is the page's styles'; this is for drawing more than the attributes allow (a summary, a count)
568
+ * looks is the page's styles'; this is for drawing more than the attributes allow (a summary, a
569
+ * count). It changes when the forms are marked again: the log, the listeners or the page changed.
479
570
  */
480
- forms(): FormStatus[]
481
- /** called after the forms were marked again: the log, the listeners or the page changed */
482
- onForms(fn: (forms: FormStatus[]) => void): () => void
571
+ forms: Reactive<FormStatus[]>
483
572
  }
484
573
 
485
574
  /**
@@ -507,8 +596,8 @@ export interface FormStatus {
507
596
  * `::name` and `:name`; and, when it says so (`fence`), the code fences of that language,
508
597
  * ```` ```name ````. Server functions run while the page renders; `client` runs in the browser.
509
598
  *
510
- * File references are URLs relative to the plugin's module, so they work wherever it is installed:
511
- * client: new URL('./client.js', import.meta.url)
599
+ * Its files are in its plugin's public folder, named within it, so they work wherever it is
600
+ * installed: `client: 'chart.js'`, `style: 'chart.css'`.
512
601
  */
513
602
  export interface Block<Props = unknown> {
514
603
  /**
@@ -525,9 +614,9 @@ export interface Block<Props = unknown> {
525
614
  * output; with it, what shows until it mounts.
526
615
  */
527
616
  html?(ctx: ServerContext): string | Html | null | Promise<string | Html | null>
528
- /** a browser module whose default export is a `Mount` */
617
+ /** a browser module whose default export is a `Mount`, in its plugin's public folder: `'chart.js'` */
529
618
  client?: URL | string
530
- /** a stylesheet linked on every page that uses the block */
619
+ /** a stylesheet linked on every page that uses the block, in its plugin's public folder: `'chart.css'` */
531
620
  style?: URL | string
532
621
  }
533
622
 
@@ -620,6 +709,5 @@ export type Theme = 'light' | 'dark'
620
709
  /** Passed to every Mount: the core's api, and the page's theme. */
621
710
  export interface ClientApi extends Api {
622
711
  /** the theme in effect (the reader's choice, else the system's) */
623
- theme(): Theme
624
- onTheme(fn: (theme: Theme) => void): () => void
712
+ theme: Reactive<Theme>
625
713
  }
package/src/events.ts CHANGED
@@ -61,7 +61,11 @@ export function parseLines(text: string): WbEvent[] {
61
61
  for (const line of text.split('\n')) {
62
62
  if (!line.trim()) continue
63
63
  let event
64
- try { event = JSON.parse(line) } catch { continue /* a corrupt line */ }
64
+ try {
65
+ event = JSON.parse(line)
66
+ } catch {
67
+ continue /* a corrupt line */
68
+ }
65
69
  out.push(event)
66
70
  }
67
71
  return out
@@ -78,7 +82,11 @@ export function readEvents(file: string): WbEvent[] {
78
82
  */
79
83
  export function read(file: string): { events: WbEvent[]; end: number } {
80
84
  let buf: Buffer
81
- try { buf = fs.readFileSync(file) } catch { return { events: [], end: 0 } }
85
+ try {
86
+ buf = fs.readFileSync(file)
87
+ } catch {
88
+ return { events: [], end: 0 }
89
+ }
82
90
  const end = buf.lastIndexOf(0x0a) + 1
83
91
  return { events: parseLines(buf.subarray(0, end).toString('utf8')), end }
84
92
  }
@@ -92,31 +100,54 @@ export function read(file: string): { events: WbEvent[]; end: number } {
92
100
  * shorter one. Then it reads the log again and goes on after the last event it had passed on (by
93
101
  * time, and by id among those of the same time), so nothing is passed on twice.
94
102
  */
95
- export function follow(file: string, from: number, onEvent: (event: WbEvent) => void, { interval = 200 } = {}): () => void {
103
+ export function follow(
104
+ file: string,
105
+ from: number,
106
+ onEvent: (event: WbEvent) => void,
107
+ { interval = 200 } = {},
108
+ ): () => void {
96
109
  let offset = from
97
110
  let partial = ''
98
111
  let inode: number | null = null
99
- try { inode = fs.statSync(file).ino } catch { /* not there yet */ }
112
+ try {
113
+ inode = fs.statSync(file).ino
114
+ } catch {
115
+ /* not there yet */
116
+ }
100
117
  // where it is in the log, to find its place again after a rewrite
101
118
  let lastTs = ''
102
119
  let atLastTs = new Set<string>()
103
120
  // the events before `from` count as passed on: the place starts after the last of them
104
121
  if (from > 0) {
105
122
  let before: WbEvent[] = []
106
- try { before = parseLines(fs.readFileSync(file).subarray(0, from).toString('utf8')) } catch { /* not there */ }
123
+ try {
124
+ before = parseLines(fs.readFileSync(file).subarray(0, from).toString('utf8'))
125
+ } catch {
126
+ /* not there */
127
+ }
107
128
  for (const e of before) {
108
- if (e.ts > lastTs) { lastTs = e.ts; atLastTs = new Set() }
129
+ if (e.ts > lastTs) {
130
+ lastTs = e.ts
131
+ atLastTs = new Set()
132
+ }
109
133
  if (e.ts === lastTs) atLastTs.add(e.id)
110
134
  }
111
135
  }
112
136
  const pass = (e: WbEvent) => {
113
- if (e.ts > lastTs) { lastTs = e.ts; atLastTs = new Set() }
137
+ if (e.ts > lastTs) {
138
+ lastTs = e.ts
139
+ atLastTs = new Set()
140
+ }
114
141
  atLastTs.add(e.id)
115
142
  onEvent(e)
116
143
  }
117
144
  const poll = () => {
118
145
  let stat: fs.Stats
119
- try { stat = fs.statSync(file) } catch { return }
146
+ try {
147
+ stat = fs.statSync(file)
148
+ } catch {
149
+ return
150
+ }
120
151
  if ((inode != null && stat.ino !== inode) || stat.size < offset) {
121
152
  // rewritten: read it again, and go on after the place reached
122
153
  inode = stat.ino
@@ -149,19 +180,37 @@ export function follow(file: string, from: number, onEvent: (event: WbEvent) =>
149
180
  * reads it again and goes on after the last event it passed on (see follow). Lines that aren't
150
181
  * events are kept as they are. Returns how many were dropped and changed.
151
182
  */
152
- export function rewrite(file: string, change: (event: WbEvent) => WbEvent | null): { removed: number; changed: number } {
183
+ export function rewrite(
184
+ file: string,
185
+ change: (event: WbEvent) => WbEvent | null,
186
+ ): { removed: number; changed: number } {
153
187
  for (let attempt = 0; attempt < 10; attempt++) {
154
188
  let buf: Buffer
155
- try { buf = fs.readFileSync(file) } catch { return { removed: 0, changed: 0 } }
189
+ try {
190
+ buf = fs.readFileSync(file)
191
+ } catch {
192
+ return { removed: 0, changed: 0 }
193
+ }
156
194
  const keep: string[] = []
157
- let removed = 0, changed = 0
195
+ let removed = 0,
196
+ changed = 0
158
197
  for (const line of buf.toString('utf8').split('\n')) {
159
198
  if (!line.trim()) continue
160
199
  let ev: WbEvent | null = null
161
- try { ev = JSON.parse(line) } catch { /* not an event: kept */ }
162
- if (!ev) { keep.push(line); continue }
200
+ try {
201
+ ev = JSON.parse(line)
202
+ } catch {
203
+ /* not an event: kept */
204
+ }
205
+ if (!ev) {
206
+ keep.push(line)
207
+ continue
208
+ }
163
209
  const next = change(ev)
164
- if (!next) { removed++; continue }
210
+ if (!next) {
211
+ removed++
212
+ continue
213
+ }
165
214
  const text = JSON.stringify(next)
166
215
  if (text !== JSON.stringify(ev)) changed++
167
216
  keep.push(next === ev ? line : text)
@@ -170,7 +219,10 @@ export function rewrite(file: string, change: (event: WbEvent) => WbEvent | null
170
219
  const tmp = `${file}.${process.pid}.tmp`
171
220
  fs.writeFileSync(tmp, keep.length ? keep.join('\n') + '\n' : '')
172
221
  // appended to meanwhile: that event would be lost with the old file
173
- if (fs.statSync(file).size !== buf.length) { fs.rmSync(tmp, { force: true }); continue }
222
+ if (fs.statSync(file).size !== buf.length) {
223
+ fs.rmSync(tmp, { force: true })
224
+ continue
225
+ }
174
226
  fs.renameSync(tmp, file)
175
227
  return { removed, changed }
176
228
  }
@@ -192,7 +244,10 @@ export function prune(file: string, page: string, which: (event: WbEvent) => boo
192
244
  export function editEvent(file: string, id: string, data: Record<string, unknown>): WbEvent {
193
245
  const found = readEvents(file).find(e => e.id === id)
194
246
  if (!found) throw new Error(`no event ${id} in the log`)
195
- if (fromReader(found)) throw new Error(`event ${id} is the reader's (${found.type}): what the reader sent stays as they sent it; an agent edits only its own`)
247
+ if (fromReader(found))
248
+ throw new Error(
249
+ `event ${id} is the reader's (${found.type}): what the reader sent stays as they sent it; an agent edits only its own`,
250
+ )
196
251
  let edited: WbEvent | null = null
197
252
  rewrite(file, e => (e.id === id ? (edited = { ...e, data: { ...e.data, ...data } }) : e))
198
253
  return edited ?? found
@@ -228,8 +283,10 @@ export function pick(file: string, event: WbEvent): WbEvent {
228
283
  * Which event types reach an agent unasked: a form's answers (types without a colon) always, and of
229
284
  * the plugins' events (`<plugin>:<what>`) those declared `handOut: true`, given in `handedOut`.
230
285
  */
231
- export const forAgent = (handedOut: ReadonlySet<string> = new Set()) => (type: string): boolean =>
232
- !type.includes(':') || handedOut.has(type)
286
+ export const forAgent =
287
+ (handedOut: ReadonlySet<string> = new Set()) =>
288
+ (type: string): boolean =>
289
+ !type.includes(':') || handedOut.has(type)
233
290
 
234
291
  export interface HandOutOptions {
235
292
  /** which event types it hands out unasked (see forAgent); a filter's `type` is handed out always */
@@ -262,12 +319,29 @@ export interface HandOutOptions {
262
319
  * Resolves with how many were handed out: once `count` is reached, after the backlog without
263
320
  * `follow`, or when `signal` aborts.
264
321
  */
265
- export function handOut(file: string, filter: EventFilter, onEvent: (event: WbEvent) => void, options: HandOutOptions = {}): Promise<number> {
266
- const { count = Infinity, follow: following = true, interval, signal, forAgent: isForAgent = forAgent(), skip, recheck } = options
322
+ export function handOut(
323
+ file: string,
324
+ filter: EventFilter,
325
+ onEvent: (event: WbEvent) => void,
326
+ options: HandOutOptions = {},
327
+ ): Promise<number> {
328
+ const {
329
+ count = Infinity,
330
+ follow: following = true,
331
+ interval,
332
+ signal,
333
+ forAgent: isForAgent = forAgent(),
334
+ skip,
335
+ recheck,
336
+ } = options
267
337
  return new Promise(resolve => {
268
338
  let got = 0
269
339
  let stop = () => {}
270
- const finish = () => { stop(); signal?.removeEventListener('abort', finish); resolve(got) }
340
+ const finish = () => {
341
+ stop()
342
+ signal?.removeEventListener('abort', finish)
343
+ resolve(got)
344
+ }
271
345
  // the reader's events, and of those, the ones for an agent, unless the filter names a type
272
346
  const wanted = (e: WbEvent) => fromReader(e) && matches(e, filter) && (!!filter.type || isForAgent(e.type))
273
347
  // false once enough were handed out
@@ -280,7 +354,10 @@ export function handOut(file: string, filter: EventFilter, onEvent: (event: WbEv
280
354
  const left = new Map<string, WbEvent>()
281
355
  // false once enough were handed out; a left event is kept to look at again
282
356
  const offer = (e: WbEvent) => {
283
- if (skip?.(e)) { left.set(e.id, e); return true }
357
+ if (skip?.(e)) {
358
+ left.set(e.id, e)
359
+ return true
360
+ }
284
361
  return hand(e)
285
362
  }
286
363
  if (count <= 0 || signal?.aborted) return finish()
@@ -295,12 +372,22 @@ export function handOut(file: string, filter: EventFilter, onEvent: (event: WbEv
295
372
  if (!left.size || got >= count) return
296
373
  const picked = pickedUp(read(file).events)
297
374
  for (const [id, e] of left) {
298
- if (picked.has(id)) { left.delete(id); continue }
375
+ if (picked.has(id)) {
376
+ left.delete(id)
377
+ continue
378
+ }
299
379
  if (skip?.(e)) continue
300
380
  left.delete(id)
301
381
  if (!hand(e)) return finish()
302
382
  }
303
383
  })
304
- stop = follow(file, end, e => { if (got < count && wanted(e) && !offer(e)) finish() }, { interval })
384
+ stop = follow(
385
+ file,
386
+ end,
387
+ e => {
388
+ if (got < count && wanted(e) && !offer(e)) finish()
389
+ },
390
+ { interval },
391
+ )
305
392
  })
306
393
  }
package/src/html.ts CHANGED
@@ -14,8 +14,12 @@
14
14
  /** A piece of HTML that is not escaped again. `String(piece)` is its markup. */
15
15
  export class Html {
16
16
  value: string
17
- constructor(value: string) { this.value = value }
18
- toString() { return this.value }
17
+ constructor(value: string) {
18
+ this.value = value
19
+ }
20
+ toString() {
21
+ return this.value
22
+ }
19
23
  }
20
24
 
21
25
  const ESCAPE: Record<string, string> = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }
package/src/icons.ts CHANGED
@@ -2,7 +2,9 @@
2
2
  // imports it too.
3
3
 
4
4
  /** An inbox: what awaits the reader (the header's menu, the overview). */
5
- export const INBOX_ICON = '<svg viewBox="0 0 16 16" width="13" height="13" aria-hidden="true"><path d="M2 9.5 3.8 3.2A1 1 0 0 1 4.8 2.5h6.4a1 1 0 0 1 1 .7L14 9.5V13a.5.5 0 0 1-.5.5h-11A.5.5 0 0 1 2 13Zm0 0h3.5l1 1.5h3l1-1.5H14" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linejoin="round"/></svg>'
5
+ export const INBOX_ICON =
6
+ '<svg viewBox="0 0 16 16" width="13" height="13" aria-hidden="true"><path d="M2 9.5 3.8 3.2A1 1 0 0 1 4.8 2.5h6.4a1 1 0 0 1 1 .7L14 9.5V13a.5.5 0 0 1-.5.5h-11A.5.5 0 0 1 2 13Zm0 0h3.5l1 1.5h3l1-1.5H14" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linejoin="round"/></svg>'
6
7
 
7
8
  /** "Awaiting you: 2 things", and where: what the inbox says to a reader who hovers it. */
8
- export const awaitingLabel = (n: number, where = '') => `Awaiting you: ${n === 1 ? '1 question or message' : `${n} questions and messages`}${where}`
9
+ export const awaitingLabel = (n: number, where = '') =>
10
+ `Awaiting you: ${n === 1 ? '1 question or message' : `${n} questions and messages`}${where}`
package/src/load.ts CHANGED
@@ -22,7 +22,11 @@ const PACKAGE = '@bigmistqke/whiteboard'
22
22
  function ownFile(specifier: string): string | null {
23
23
  if (specifier !== PACKAGE && !specifier.startsWith(PACKAGE + '/')) return null
24
24
  let exports: Record<string, string | { source?: string; default?: string }> | undefined
25
- try { exports = JSON.parse(fs.readFileSync(path.join(ENGINE_DIR, 'package.json'), 'utf8')).exports } catch { return null }
25
+ try {
26
+ exports = JSON.parse(fs.readFileSync(path.join(ENGINE_DIR, 'package.json'), 'utf8')).exports
27
+ } catch {
28
+ return null
29
+ }
26
30
  const target = exports?.[specifier === PACKAGE ? '.' : `./${specifier.slice(PACKAGE.length + 1)}`]
27
31
  const file = typeof target === 'string' ? target : (FROM_SOURCE && target?.source) || target?.default
28
32
  return file ? path.join(ENGINE_DIR, file) : null
@@ -57,7 +61,11 @@ export function installHooks() {
57
61
  // would load the config as CommonJS and fail on its imports. What is in .whiteboard/ is always
58
62
  // a module, by its extension; other local files keep the format their package gives them.
59
63
  const inProject = url.pathname.split('/').includes('.whiteboard')
60
- return { ...resolved, url: url.href, format: inProject ? moduleFormat(url.pathname) : resolved.format ?? moduleFormat(url.pathname) }
64
+ return {
65
+ ...resolved,
66
+ url: url.href,
67
+ format: inProject ? moduleFormat(url.pathname) : (resolved.format ?? moduleFormat(url.pathname)),
68
+ }
61
69
  },
62
70
  })
63
71
  }