@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.
- package/README.md +177 -26
- package/bin/whiteboard.ts +331 -16
- package/dist/bin/whiteboard.js +268 -17
- package/dist/client/runtime.js +66 -23
- package/dist/plugins/ansi/README.md +36 -0
- package/dist/plugins/ansi/index.js +64 -0
- package/dist/plugins/ansi/public/style.css +28 -0
- package/dist/plugins/badge/README.md +28 -0
- package/dist/plugins/badge/index.js +24 -0
- package/dist/plugins/badge/public/style.css +30 -0
- package/dist/plugins/chat/public/style.css +7 -2
- package/dist/plugins/code/README.md +4 -2
- package/dist/plugins/code/diff.js +12 -2
- package/dist/plugins/draft/README.md +14 -0
- package/dist/plugins/draft/index.js +21 -0
- package/dist/plugins/draft/public/style.css +21 -0
- package/dist/plugins/grid/README.md +12 -12
- package/dist/plugins/grid/index.js +7 -19
- package/dist/plugins/grid/public/style.css +4 -33
- package/dist/plugins/iframe/README.md +7 -7
- package/dist/plugins/iframe/index.js +11 -11
- package/dist/plugins/iframe/public/style.css +15 -0
- package/dist/plugins/index.js +11 -3
- package/dist/plugins/lightbox/README.md +22 -0
- package/dist/plugins/lightbox/index.js +20 -0
- package/dist/plugins/lightbox/public/client.js +4 -0
- package/dist/plugins/lightbox/public/style.css +66 -0
- package/dist/plugins/steps/README.md +42 -0
- package/dist/plugins/steps/index.js +60 -0
- package/dist/plugins/steps/public/client.js +1 -0
- package/dist/plugins/steps/public/style.css +77 -0
- package/dist/plugins/terminal/host.js +9 -3
- package/dist/plugins/terminal/public/client.js +1 -1
- package/dist/plugins/terminal/sizes.js +2 -1
- package/dist/plugins/toc/README.md +21 -3
- package/dist/plugins/toc/index.js +8 -2
- package/dist/plugins/toc/public/client.js +1 -1
- package/dist/plugins/toc/public/style.css +10 -4
- package/dist/plugins/window/README.md +34 -14
- package/dist/plugins/window/index.js +21 -13
- package/dist/src/auth.js +6 -4
- package/dist/src/client/page/base.css +61 -1
- package/dist/src/client/page/main.js +74 -20
- package/dist/src/entries.js +211 -0
- package/dist/src/events.js +1 -1
- package/dist/src/hub.js +272 -0
- package/dist/src/loader.js +12 -3
- package/dist/src/package.js +23 -1
- package/dist/src/pages/blocks.js +1 -0
- package/dist/src/pages/index.js +1 -1
- package/dist/src/pages/markdown.js +87 -33
- package/dist/src/pages/render.js +157 -28
- package/dist/src/pages/window.js +17 -18
- package/dist/src/running.js +37 -2
- package/dist/src/serve.js +111 -2
- package/dist/src/server.js +264 -23
- package/package.json +3 -2
- package/plugins/ansi/README.md +36 -0
- package/plugins/ansi/index.ts +95 -0
- package/plugins/ansi/public/style.css +28 -0
- package/plugins/badge/README.md +28 -0
- package/plugins/badge/index.ts +35 -0
- package/plugins/badge/public/style.css +30 -0
- package/plugins/chat/public/style.css +7 -2
- package/plugins/code/README.md +4 -2
- package/plugins/code/diff.ts +14 -2
- package/plugins/code/index.ts +3 -1
- package/plugins/draft/README.md +14 -0
- package/plugins/draft/index.ts +23 -0
- package/plugins/draft/public/style.css +21 -0
- package/plugins/grid/README.md +12 -12
- package/plugins/grid/index.ts +11 -26
- package/plugins/grid/public/style.css +4 -33
- package/plugins/iframe/README.md +7 -7
- package/plugins/iframe/index.ts +15 -15
- package/plugins/iframe/public/style.css +15 -0
- package/plugins/index.ts +10 -2
- package/plugins/lightbox/README.md +22 -0
- package/plugins/lightbox/client.ts +99 -0
- package/plugins/lightbox/index.ts +25 -0
- package/plugins/lightbox/public/style.css +66 -0
- package/plugins/mermaid/index.ts +4 -2
- package/plugins/steps/README.md +42 -0
- package/plugins/steps/client.ts +75 -0
- package/plugins/steps/index.ts +84 -0
- package/plugins/steps/public/style.css +77 -0
- package/plugins/terminal/client.ts +16 -7
- package/plugins/terminal/host.ts +12 -6
- package/plugins/terminal/sizes.ts +3 -1
- package/plugins/toc/README.md +21 -3
- package/plugins/toc/client.ts +47 -6
- package/plugins/toc/index.ts +19 -6
- package/plugins/toc/public/style.css +10 -4
- package/plugins/window/README.md +34 -14
- package/plugins/window/index.ts +20 -13
- package/skills/whiteboard/SKILL.md +18 -10
- package/src/auth.ts +8 -5
- package/src/client/base.ts +19 -0
- package/src/client/forms.ts +2 -1
- package/src/client/markdown.ts +2 -1
- package/src/client/page/base.css +61 -1
- package/src/client/page/elsewhere.ts +72 -16
- package/src/client/page/fresh.ts +2 -1
- package/src/client/page/main.ts +18 -6
- package/src/client/page/sort.ts +2 -1
- package/src/client/runtime.ts +62 -25
- package/src/define.ts +126 -19
- package/src/entries.ts +258 -0
- package/src/events.ts +2 -1
- package/src/hub.ts +355 -0
- package/src/loader.ts +14 -3
- package/src/package.ts +22 -0
- package/src/pages/blocks.ts +3 -0
- package/src/pages/index.ts +6 -4
- package/src/pages/markdown.ts +89 -32
- package/src/pages/render.ts +181 -28
- package/src/pages/window.ts +23 -25
- package/src/running.ts +47 -1
- package/src/serve.ts +114 -1
- package/src/server.ts +332 -26
- package/dist/plugins/gallery/README.md +0 -30
- package/dist/plugins/gallery/index.js +0 -75
- package/dist/plugins/gallery/public/client.js +0 -1
- package/dist/plugins/gallery/public/style.css +0 -90
- package/plugins/gallery/README.md +0 -30
- package/plugins/gallery/client.tsx +0 -70
- package/plugins/gallery/index.ts +0 -89
- 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(
|
|
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
|
+
}
|