@bigmistqke/whiteboard 0.4.0 → 0.6.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 (128) hide show
  1. package/README.md +177 -26
  2. package/bin/whiteboard.ts +331 -16
  3. package/dist/bin/whiteboard.js +268 -17
  4. package/dist/client/runtime.js +66 -23
  5. package/dist/plugins/ansi/README.md +36 -0
  6. package/dist/plugins/ansi/index.js +64 -0
  7. package/dist/plugins/ansi/public/style.css +28 -0
  8. package/dist/plugins/badge/README.md +28 -0
  9. package/dist/plugins/badge/index.js +24 -0
  10. package/dist/plugins/badge/public/style.css +30 -0
  11. package/dist/plugins/chat/public/style.css +7 -2
  12. package/dist/plugins/code/README.md +4 -2
  13. package/dist/plugins/code/diff.js +12 -2
  14. package/dist/plugins/draft/README.md +14 -0
  15. package/dist/plugins/draft/index.js +21 -0
  16. package/dist/plugins/draft/public/style.css +21 -0
  17. package/dist/plugins/grid/README.md +12 -12
  18. package/dist/plugins/grid/index.js +7 -19
  19. package/dist/plugins/grid/public/style.css +4 -33
  20. package/dist/plugins/iframe/README.md +7 -7
  21. package/dist/plugins/iframe/index.js +11 -11
  22. package/dist/plugins/iframe/public/style.css +15 -0
  23. package/dist/plugins/index.js +11 -3
  24. package/dist/plugins/lightbox/README.md +22 -0
  25. package/dist/plugins/lightbox/index.js +20 -0
  26. package/dist/plugins/lightbox/public/client.js +4 -0
  27. package/dist/plugins/lightbox/public/style.css +66 -0
  28. package/dist/plugins/steps/README.md +42 -0
  29. package/dist/plugins/steps/index.js +60 -0
  30. package/dist/plugins/steps/public/client.js +1 -0
  31. package/dist/plugins/steps/public/style.css +77 -0
  32. package/dist/plugins/terminal/host.js +9 -3
  33. package/dist/plugins/terminal/public/client.js +1 -1
  34. package/dist/plugins/terminal/sizes.js +2 -1
  35. package/dist/plugins/toc/README.md +21 -3
  36. package/dist/plugins/toc/index.js +8 -2
  37. package/dist/plugins/toc/public/client.js +1 -1
  38. package/dist/plugins/toc/public/style.css +10 -4
  39. package/dist/plugins/window/README.md +34 -14
  40. package/dist/plugins/window/index.js +21 -13
  41. package/dist/src/auth.js +6 -4
  42. package/dist/src/client/page/base.css +61 -1
  43. package/dist/src/client/page/main.js +74 -20
  44. package/dist/src/entries.js +211 -0
  45. package/dist/src/events.js +1 -1
  46. package/dist/src/hub.js +272 -0
  47. package/dist/src/loader.js +12 -3
  48. package/dist/src/package.js +23 -1
  49. package/dist/src/pages/blocks.js +1 -0
  50. package/dist/src/pages/index.js +1 -1
  51. package/dist/src/pages/markdown.js +87 -33
  52. package/dist/src/pages/render.js +157 -28
  53. package/dist/src/pages/window.js +17 -18
  54. package/dist/src/running.js +37 -2
  55. package/dist/src/serve.js +111 -2
  56. package/dist/src/server.js +264 -23
  57. package/package.json +3 -2
  58. package/plugins/ansi/README.md +36 -0
  59. package/plugins/ansi/index.ts +95 -0
  60. package/plugins/ansi/public/style.css +28 -0
  61. package/plugins/badge/README.md +28 -0
  62. package/plugins/badge/index.ts +35 -0
  63. package/plugins/badge/public/style.css +30 -0
  64. package/plugins/chat/public/style.css +7 -2
  65. package/plugins/code/README.md +4 -2
  66. package/plugins/code/diff.ts +14 -2
  67. package/plugins/code/index.ts +3 -1
  68. package/plugins/draft/README.md +14 -0
  69. package/plugins/draft/index.ts +23 -0
  70. package/plugins/draft/public/style.css +21 -0
  71. package/plugins/grid/README.md +12 -12
  72. package/plugins/grid/index.ts +11 -26
  73. package/plugins/grid/public/style.css +4 -33
  74. package/plugins/iframe/README.md +7 -7
  75. package/plugins/iframe/index.ts +15 -15
  76. package/plugins/iframe/public/style.css +15 -0
  77. package/plugins/index.ts +10 -2
  78. package/plugins/lightbox/README.md +22 -0
  79. package/plugins/lightbox/client.ts +99 -0
  80. package/plugins/lightbox/index.ts +25 -0
  81. package/plugins/lightbox/public/style.css +66 -0
  82. package/plugins/mermaid/index.ts +4 -2
  83. package/plugins/steps/README.md +42 -0
  84. package/plugins/steps/client.ts +75 -0
  85. package/plugins/steps/index.ts +84 -0
  86. package/plugins/steps/public/style.css +77 -0
  87. package/plugins/terminal/client.ts +16 -7
  88. package/plugins/terminal/host.ts +12 -6
  89. package/plugins/terminal/sizes.ts +3 -1
  90. package/plugins/toc/README.md +21 -3
  91. package/plugins/toc/client.ts +47 -6
  92. package/plugins/toc/index.ts +19 -6
  93. package/plugins/toc/public/style.css +10 -4
  94. package/plugins/window/README.md +34 -14
  95. package/plugins/window/index.ts +20 -13
  96. package/skills/whiteboard/SKILL.md +18 -10
  97. package/src/auth.ts +8 -5
  98. package/src/client/base.ts +19 -0
  99. package/src/client/forms.ts +2 -1
  100. package/src/client/markdown.ts +2 -1
  101. package/src/client/page/base.css +61 -1
  102. package/src/client/page/elsewhere.ts +72 -16
  103. package/src/client/page/fresh.ts +2 -1
  104. package/src/client/page/main.ts +18 -6
  105. package/src/client/page/sort.ts +2 -1
  106. package/src/client/runtime.ts +62 -25
  107. package/src/define.ts +126 -19
  108. package/src/entries.ts +258 -0
  109. package/src/events.ts +2 -1
  110. package/src/hub.ts +355 -0
  111. package/src/loader.ts +14 -3
  112. package/src/package.ts +22 -0
  113. package/src/pages/blocks.ts +3 -0
  114. package/src/pages/index.ts +6 -4
  115. package/src/pages/markdown.ts +89 -32
  116. package/src/pages/render.ts +181 -28
  117. package/src/pages/window.ts +23 -25
  118. package/src/running.ts +47 -1
  119. package/src/serve.ts +114 -1
  120. package/src/server.ts +332 -26
  121. package/dist/plugins/gallery/README.md +0 -30
  122. package/dist/plugins/gallery/index.js +0 -75
  123. package/dist/plugins/gallery/public/client.js +0 -1
  124. package/dist/plugins/gallery/public/style.css +0 -90
  125. package/plugins/gallery/README.md +0 -30
  126. package/plugins/gallery/client.tsx +0 -70
  127. package/plugins/gallery/index.ts +0 -89
  128. package/plugins/gallery/public/style.css +0 -90
package/src/running.ts CHANGED
@@ -4,6 +4,12 @@
4
4
  import fs from 'node:fs'
5
5
  import type { Project } from './config.ts'
6
6
 
7
+ /**
8
+ * How long a server may take to say it is there: a loaded machine (many agents, builds) can take
9
+ * seconds, and a server taken for gone is one a command starts again or does without.
10
+ */
11
+ const HEALTH_TIMEOUT = 5000
12
+
7
13
  /** The port the last server of this project recorded, or null. */
8
14
  export function recordedPort(project: Project): number | null {
9
15
  try {
@@ -19,10 +25,50 @@ export async function runningServer(project: Project, { except }: { except?: num
19
25
  if (!port) return null
20
26
  const base = `http://localhost:${port}`
21
27
  try {
22
- const res = await fetch(`${base}/api/health`, { signal: AbortSignal.timeout(800) })
28
+ const res = await fetch(`${base}/api/health`, { signal: AbortSignal.timeout(HEALTH_TIMEOUT) })
23
29
  const health = res.ok ? await res.json() : null
24
30
  return health?.dir === project.dir && health.pid !== except ? base : null
25
31
  } catch {
26
32
  return null
27
33
  }
28
34
  }
35
+
36
+ /** The shared server as its health says: where it is, its version, the projects it serves. */
37
+ export interface SharedServer {
38
+ origin: string
39
+ pid: number
40
+ version: string
41
+ projects: { entry: string; dir: string; base: string }[]
42
+ }
43
+
44
+ /** The port the shared server recorded in the machine folder, or null. */
45
+ export function recordedSharedPort(file: string): number | null {
46
+ try {
47
+ return Number(JSON.parse(fs.readFileSync(file, 'utf8')).port) || null
48
+ } catch {
49
+ return null
50
+ }
51
+ }
52
+
53
+ /**
54
+ * The machine's running shared server, or null: the port in its record, checked by asking it.
55
+ * `rescan`: have it look for projects first (one just made, say). `except`: a process not to count.
56
+ */
57
+ export async function sharedServer(
58
+ file: string,
59
+ { rescan = false, except }: { rescan?: boolean; except?: number } = {},
60
+ ): Promise<SharedServer | null> {
61
+ const port = recordedSharedPort(file)
62
+ if (!port) return null
63
+ const origin = `http://localhost:${port}`
64
+ try {
65
+ const res = await fetch(`${origin}/api/health${rescan ? '?rescan' : ''}`, {
66
+ signal: AbortSignal.timeout(HEALTH_TIMEOUT),
67
+ })
68
+ const health = res.ok ? await res.json() : null
69
+ if (!health?.hub || health.pid === except) return null
70
+ return { origin, pid: health.pid, version: String(health.version ?? '0.0.0'), projects: health.projects ?? [] }
71
+ } catch {
72
+ return null
73
+ }
74
+ }
package/src/serve.ts CHANGED
@@ -6,7 +6,10 @@ import http from 'node:http'
6
6
  import path from 'node:path'
7
7
  import { DEFAULT_PORT, type Project } from './config.ts'
8
8
  import { createServer } from './server.ts'
9
- import { recordedPort, runningServer } from './running.ts'
9
+ import { recordedPort, recordedSharedPort, runningServer, sharedServer, type SharedServer } from './running.ts'
10
+ import { machineConfigFile, machineDir, machineServerFile, readMachineConfig, stateDir } from './entries.ts'
11
+ import { createHub } from './hub.ts'
12
+ import { compareVersions, VERSION } from './package.ts'
10
13
 
11
14
  /** Listen on the previous port, else the default, else the next free one; then record it in server.json. */
12
15
  export async function start(project: Project): Promise<number> {
@@ -72,3 +75,113 @@ export async function start(project: Project): Promise<number> {
72
75
  }
73
76
  throw new Error(`no free port from ${DEFAULT_PORT} to ${DEFAULT_PORT + 50}`)
74
77
  }
78
+
79
+ // ---- the shared server ---------------------------------------------------------------------------
80
+ // One per machine (see entries.ts and hub.ts): on the machine config's port, else the default, else
81
+ // the next free one, recorded in the machine folder's server.json with its version. A newer
82
+ // whiteboard takes over from an older one: it asks it to exit, then listens in its place. An older
83
+ // one, or one as new, leaves a running one be.
84
+
85
+ /** Run the shared server. Exits (0) when one as new or newer runs already. */
86
+ export async function startShared(dir = machineDir(), state = stateDir()): Promise<number> {
87
+ const file = machineServerFile(state)
88
+ const other = await sharedServer(file, { except: process.pid })
89
+ if (other) {
90
+ if (compareVersions(VERSION, other.version) <= 0) {
91
+ console.log(`whiteboard: the shared server ${other.version} runs at ${other.origin}`)
92
+ process.exit(0)
93
+ }
94
+ await takeOver(other)
95
+ }
96
+ const config = readMachineConfig(dir)
97
+ if (!config) throw new Error(`no machine config: ${machineConfigFile(dir)}`)
98
+ fs.mkdirSync(state, { recursive: true })
99
+ const hub = createHub({ dir, state, onShutdown: () => process.exit(0) })
100
+ const server = http.createServer((req, res) => {
101
+ void hub.handle(req, res)
102
+ })
103
+ const first = config.port ?? DEFAULT_PORT
104
+ const candidates = [
105
+ ...new Set(
106
+ [
107
+ other ? Number(new URL(other.origin).port) : null,
108
+ recordedSharedPort(file),
109
+ first,
110
+ ...Array.from({ length: 50 }, (_, i) => first + 1 + i),
111
+ ].filter((p): p is number => !!p),
112
+ ),
113
+ ]
114
+ for (const port of candidates) {
115
+ if (!(await listenOn(server, port))) continue
116
+ fs.writeFileSync(file, JSON.stringify({ port, pid: process.pid, version: VERSION }, null, 2) + '\n')
117
+ try {
118
+ fs.unlinkSync(path.join(state, 'server.lock'))
119
+ } catch {}
120
+ process.on('exit', () => {
121
+ try {
122
+ if (JSON.parse(fs.readFileSync(file, 'utf8')).pid === process.pid) fs.unlinkSync(file)
123
+ } catch {}
124
+ })
125
+ // should another of the same version have started at the same moment, the one server.json
126
+ // names stays; one newer than this takes over by asking
127
+ const yieldToOther = async () => {
128
+ if (await sharedServer(file, { except: process.pid })) {
129
+ console.log('whiteboard: another shared server took over')
130
+ process.exit(0)
131
+ }
132
+ }
133
+ setTimeout(yieldToOther, 1500).unref()
134
+ setInterval(yieldToOther, 30000).unref()
135
+ for (const signal of ['SIGINT', 'SIGTERM', 'SIGHUP'] as const) process.on(signal, () => process.exit(0))
136
+ console.log(`whiteboard ${VERSION}: the shared server at http://localhost:${port}`)
137
+ for (const p of hub.projects()) console.log(` http://localhost:${port}${p.base}/ ${p.dir}`)
138
+ return port
139
+ }
140
+ throw new Error(`no free port from ${first} to ${first + 50}`)
141
+ }
142
+
143
+ /** Ask an older shared server to make way, and wait until it has (else end its process). */
144
+ async function takeOver(other: SharedServer) {
145
+ console.log(`whiteboard ${VERSION}: taking over from the shared server ${other.version} at ${other.origin}`)
146
+ try {
147
+ await fetch(`${other.origin}/api/shutdown`, {
148
+ method: 'POST',
149
+ headers: { 'content-type': 'application/json' },
150
+ body: JSON.stringify({ version: VERSION }),
151
+ signal: AbortSignal.timeout(2000),
152
+ })
153
+ } catch {}
154
+ const gone = async () => {
155
+ try {
156
+ await fetch(`${other.origin}/api/health`, { signal: AbortSignal.timeout(300) })
157
+ return false
158
+ } catch {
159
+ return true
160
+ }
161
+ }
162
+ for (let i = 0; i < 25; i++) {
163
+ if (await gone()) return
164
+ await new Promise(r => setTimeout(r, 200))
165
+ }
166
+ // one from before /api/shutdown: its process, by the pid it gave
167
+ try {
168
+ process.kill(other.pid, 'SIGTERM')
169
+ } catch {}
170
+ for (let i = 0; i < 25 && !(await gone()); i++) await new Promise(r => setTimeout(r, 200))
171
+ }
172
+
173
+ function listenOn(server: http.Server, port: number): Promise<boolean> {
174
+ return new Promise((resolve, reject) => {
175
+ const onError = (err: NodeJS.ErrnoException) => {
176
+ server.off('listening', onListening)
177
+ err.code === 'EADDRINUSE' ? resolve(false) : reject(err)
178
+ }
179
+ const onListening = () => {
180
+ server.off('error', onError)
181
+ resolve(true)
182
+ }
183
+ server.once('error', onError)
184
+ server.once('listening', onListening)
185
+ server.listen(port, '127.0.0.1')
186
+ })
187
+ }