@bigmistqke/workpad 0.0.0-stage → 0.18.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/LICENSE +21 -0
- package/README.md +781 -2
- package/bin/workpad.ts +1509 -0
- package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
- package/dist/bin/workpad.js +1190 -0
- package/dist/client/runtime.js +1194 -0
- package/dist/define.js +27 -0
- package/dist/plugins/alert/README.md +18 -0
- package/dist/plugins/alert/index.js +40 -0
- package/dist/plugins/alert/public/style.css +40 -0
- package/dist/plugins/ansi/README.md +36 -0
- package/dist/plugins/ansi/index.js +63 -0
- package/dist/plugins/ansi/public/style.css +28 -0
- package/dist/plugins/badge/README.md +32 -0
- package/dist/plugins/badge/index.js +28 -0
- package/dist/plugins/badge/public/style.css +30 -0
- package/dist/plugins/chat/README.md +81 -0
- package/dist/plugins/chat/index.js +96 -0
- package/dist/plugins/chat/public/client.js +3 -0
- package/dist/plugins/chat/public/style.css +143 -0
- package/dist/plugins/claude/README.md +116 -0
- package/dist/plugins/claude/channel.js +130 -0
- package/dist/plugins/claude/doctor.js +316 -0
- package/dist/plugins/claude/index.js +199 -0
- package/dist/plugins/claude/session.js +68 -0
- package/dist/plugins/code/README.md +54 -0
- package/dist/plugins/code/diff.js +75 -0
- package/dist/plugins/code/index.js +105 -0
- package/dist/plugins/code/public/client.js +1 -0
- package/dist/plugins/code/public/style.css +99 -0
- package/dist/plugins/comments/README.md +30 -0
- package/dist/plugins/comments/index.js +39 -0
- package/dist/plugins/comments/public/client.js +24 -0
- package/dist/plugins/comments/public/style.css +224 -0
- package/dist/plugins/details/README.md +24 -0
- package/dist/plugins/details/index.js +28 -0
- package/dist/plugins/details/public/style.css +7 -0
- package/dist/plugins/draft/README.md +14 -0
- package/dist/plugins/draft/index.js +27 -0
- package/dist/plugins/draft/public/style.css +21 -0
- package/dist/plugins/forge/README.md +22 -0
- package/dist/plugins/forge/index.js +83 -0
- package/dist/plugins/form/README.md +126 -0
- package/dist/plugins/form/index.js +179 -0
- package/dist/plugins/form/public/style.css +172 -0
- package/dist/plugins/grid/README.md +45 -0
- package/dist/plugins/grid/index.js +35 -0
- package/dist/plugins/grid/public/style.css +98 -0
- package/dist/plugins/iframe/README.md +18 -0
- package/dist/plugins/iframe/index.js +49 -0
- package/dist/plugins/iframe/public/style.css +28 -0
- package/dist/plugins/index.js +44 -0
- package/dist/plugins/lightbox/README.md +24 -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/mermaid/README.md +21 -0
- package/dist/plugins/mermaid/index.js +42 -0
- package/dist/plugins/mermaid/public/style.css +21 -0
- package/dist/plugins/opencode/README.md +48 -0
- package/dist/plugins/opencode/channel.js +92 -0
- package/dist/plugins/opencode/index.js +64 -0
- package/dist/plugins/opencode/shim.js +150 -0
- package/dist/plugins/quiet/README.md +26 -0
- package/dist/plugins/quiet/index.js +23 -0
- package/dist/plugins/tabs/README.md +44 -0
- package/dist/plugins/tabs/index.js +75 -0
- package/dist/plugins/tabs/public/client.js +1 -0
- package/dist/plugins/tabs/public/style.css +98 -0
- package/dist/plugins/terminal/README.md +63 -0
- package/dist/plugins/terminal/host.js +338 -0
- package/dist/plugins/terminal/index.js +170 -0
- package/dist/plugins/terminal/modes.js +32 -0
- package/dist/plugins/terminal/public/client.js +41 -0
- package/dist/plugins/terminal/public/style.css +511 -0
- package/dist/plugins/terminal/server.js +256 -0
- package/dist/plugins/terminal/sizes.js +58 -0
- package/dist/plugins/timeline/README.md +48 -0
- package/dist/plugins/timeline/index.js +83 -0
- package/dist/plugins/timeline/public/style.css +138 -0
- package/dist/plugins/toc/README.md +40 -0
- package/dist/plugins/toc/index.js +29 -0
- package/dist/plugins/toc/public/client.js +1 -0
- package/dist/plugins/toc/public/style.css +117 -0
- package/dist/plugins/window/README.md +51 -0
- package/dist/plugins/window/index.js +91 -0
- package/dist/solid/index.js +41 -0
- package/dist/src/agents.js +81 -0
- package/dist/src/anchors.js +52 -0
- package/dist/src/api.js +282 -0
- package/dist/src/archive.js +49 -0
- package/dist/src/auth.js +67 -0
- package/dist/src/build.js +159 -0
- package/dist/src/client/page/base.css +1762 -0
- package/dist/src/client/page/main.js +2098 -0
- package/dist/src/config.js +76 -0
- package/dist/src/document.js +93 -0
- package/dist/src/entries.js +264 -0
- package/dist/src/events.js +322 -0
- package/dist/src/files.js +47 -0
- package/dist/src/formstate.js +65 -0
- package/dist/src/help.js +211 -0
- package/dist/src/html.js +34 -0
- package/dist/src/hub.js +341 -0
- package/dist/src/icons.js +6 -0
- package/dist/src/listen.js +132 -0
- package/dist/src/listeners.js +64 -0
- package/dist/src/load.js +81 -0
- package/dist/src/loader.js +179 -0
- package/dist/src/migrate.js +314 -0
- package/dist/src/package.js +52 -0
- package/dist/src/pages/blocks.js +67 -0
- package/dist/src/pages/board.js +59 -0
- package/dist/src/pages/index.js +27 -0
- package/dist/src/pages/markdown.js +863 -0
- package/dist/src/pages/render.js +665 -0
- package/dist/src/pages/window.js +29 -0
- package/dist/src/plugins.js +283 -0
- package/dist/src/public.js +24 -0
- package/dist/src/rename.js +130 -0
- package/dist/src/running.js +146 -0
- package/dist/src/serve.js +188 -0
- package/dist/src/server.js +1105 -0
- package/dist/src/site.js +179 -0
- package/dist/src/state.js +155 -0
- package/dist/src/status.js +323 -0
- package/dist/src/thread.js +68 -0
- package/dist/src/watchdog.js +38 -0
- package/dist/src/ws.js +128 -0
- package/package.json +154 -4
- package/plugins/README.md +304 -0
- package/plugins/alert/README.md +18 -0
- package/plugins/alert/index.ts +50 -0
- package/plugins/alert/public/style.css +40 -0
- 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 +32 -0
- package/plugins/badge/index.ts +36 -0
- package/plugins/badge/public/style.css +30 -0
- package/plugins/chat/README.md +81 -0
- package/plugins/chat/autosize.ts +139 -0
- package/plugins/chat/client.tsx +187 -0
- package/plugins/chat/index.ts +146 -0
- package/plugins/chat/public/style.css +143 -0
- package/plugins/claude/README.md +116 -0
- package/plugins/claude/channel.ts +136 -0
- package/plugins/claude/doctor.ts +274 -0
- package/plugins/claude/index.ts +248 -0
- package/plugins/claude/session.ts +70 -0
- package/plugins/code/README.md +54 -0
- package/plugins/code/client.ts +73 -0
- package/plugins/code/diff.ts +96 -0
- package/plugins/code/index.ts +136 -0
- package/plugins/code/public/style.css +99 -0
- package/plugins/comments/README.md +30 -0
- package/plugins/comments/client.ts +723 -0
- package/plugins/comments/index.ts +48 -0
- package/plugins/comments/public/style.css +224 -0
- package/plugins/details/README.md +24 -0
- package/plugins/details/index.ts +30 -0
- package/plugins/details/public/style.css +7 -0
- package/plugins/draft/README.md +14 -0
- package/plugins/draft/index.ts +23 -0
- package/plugins/draft/public/style.css +21 -0
- package/plugins/forge/README.md +22 -0
- package/plugins/forge/index.ts +84 -0
- package/plugins/form/README.md +126 -0
- package/plugins/form/index.ts +199 -0
- package/plugins/form/public/style.css +172 -0
- package/plugins/grid/README.md +45 -0
- package/plugins/grid/index.ts +41 -0
- package/plugins/grid/public/style.css +98 -0
- package/plugins/iframe/README.md +18 -0
- package/plugins/iframe/index.ts +53 -0
- package/plugins/iframe/public/style.css +28 -0
- package/plugins/index.ts +49 -0
- package/plugins/lightbox/README.md +24 -0
- package/plugins/lightbox/client.ts +109 -0
- package/plugins/lightbox/index.ts +25 -0
- package/plugins/lightbox/public/style.css +66 -0
- package/plugins/mermaid/README.md +21 -0
- package/plugins/mermaid/index.ts +46 -0
- package/plugins/mermaid/public/style.css +21 -0
- package/plugins/opencode/README.md +48 -0
- package/plugins/opencode/channel.ts +112 -0
- package/plugins/opencode/index.ts +91 -0
- package/plugins/opencode/shim.ts +166 -0
- package/plugins/quiet/README.md +26 -0
- package/plugins/quiet/index.ts +28 -0
- package/plugins/tabs/README.md +44 -0
- package/plugins/tabs/client.ts +96 -0
- package/plugins/tabs/index.ts +103 -0
- package/plugins/tabs/public/style.css +98 -0
- package/plugins/terminal/README.md +63 -0
- package/plugins/terminal/client.ts +391 -0
- package/plugins/terminal/host.ts +434 -0
- package/plugins/terminal/index.ts +199 -0
- package/plugins/terminal/modes.ts +30 -0
- package/plugins/terminal/public/style.css +225 -0
- package/plugins/terminal/server.ts +266 -0
- package/plugins/terminal/sizes.ts +72 -0
- package/plugins/timeline/README.md +48 -0
- package/plugins/timeline/index.ts +82 -0
- package/plugins/timeline/public/style.css +138 -0
- package/plugins/toc/README.md +40 -0
- package/plugins/toc/client.ts +236 -0
- package/plugins/toc/index.ts +55 -0
- package/plugins/toc/public/style.css +117 -0
- package/plugins/window/README.md +51 -0
- package/plugins/window/index.ts +92 -0
- package/skills/workpad/SKILL.md +588 -0
- package/solid/index.tsx +80 -0
- package/src/agents.ts +133 -0
- package/src/anchors.ts +85 -0
- package/src/api.ts +444 -0
- package/src/archive.ts +75 -0
- package/src/auth.ts +99 -0
- package/src/build.ts +213 -0
- package/src/client/base.ts +19 -0
- package/src/client/forms.ts +420 -0
- package/src/client/markdown.ts +147 -0
- package/src/client/page/base.css +1762 -0
- package/src/client/page/board.ts +137 -0
- package/src/client/page/elsewhere.ts +153 -0
- package/src/client/page/fresh.ts +343 -0
- package/src/client/page/heading-link.ts +32 -0
- package/src/client/page/live.ts +134 -0
- package/src/client/page/main.ts +398 -0
- package/src/client/page/orphans.ts +64 -0
- package/src/client/page/place.ts +260 -0
- package/src/client/page/sort.ts +49 -0
- package/src/client/page/sticky.ts +58 -0
- package/src/client/page/theme.ts +36 -0
- package/src/client/page/view.ts +9 -0
- package/src/client/page/waiting.ts +278 -0
- package/src/client/page/window.ts +40 -0
- package/src/client/reactive.ts +19 -0
- package/src/client/read.ts +111 -0
- package/src/client/runtime.ts +499 -0
- package/src/client/scroll.ts +90 -0
- package/src/client/toast.ts +99 -0
- package/src/config.ts +111 -0
- package/src/define.ts +997 -0
- package/src/document.ts +191 -0
- package/src/entries.ts +352 -0
- package/src/events.ts +463 -0
- package/src/files.ts +74 -0
- package/src/formstate.ts +72 -0
- package/src/help.ts +238 -0
- package/src/html.ts +44 -0
- package/src/hub.ts +426 -0
- package/src/icons.ts +18 -0
- package/src/listen.ts +171 -0
- package/src/listeners.ts +89 -0
- package/src/load.ts +113 -0
- package/src/loader.ts +227 -0
- package/src/migrate.ts +352 -0
- package/src/package.ts +62 -0
- package/src/pages/blocks.ts +106 -0
- package/src/pages/board.ts +89 -0
- package/src/pages/index.ts +79 -0
- package/src/pages/markdown.ts +976 -0
- package/src/pages/render.ts +748 -0
- package/src/pages/window.ts +69 -0
- package/src/plugins.ts +319 -0
- package/src/public.ts +39 -0
- package/src/rename.ts +147 -0
- package/src/running.ts +171 -0
- package/src/serve.ts +221 -0
- package/src/server.ts +1385 -0
- package/src/site.ts +170 -0
- package/src/state.ts +327 -0
- package/src/status.ts +466 -0
- package/src/thread.ts +98 -0
- package/src/watchdog.ts +53 -0
- package/src/ws.ts +155 -0
package/src/running.ts
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// Finding a project's running server: where .workpad/server.json says it is (its URL when one is
|
|
2
|
+
// configured, else its port at localhost), checked by asking the server which project it serves (the
|
|
3
|
+
// port may since have been taken by another project's). The command line uses it to reach the
|
|
4
|
+
// server, and a starting server to see whether one runs already.
|
|
5
|
+
import fs from 'node:fs'
|
|
6
|
+
import net from 'node:net'
|
|
7
|
+
import type { Project } from './config.ts'
|
|
8
|
+
import { machineServerFile, matchesEntries, readMachineConfig } from './entries.ts'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* How long a server may take to say it is there: a loaded machine (many agents, builds) can take
|
|
12
|
+
* seconds, and a server taken for gone is one a command starts again or does without.
|
|
13
|
+
*/
|
|
14
|
+
const HEALTH_TIMEOUT = 5000
|
|
15
|
+
|
|
16
|
+
/** The port the last server of this project recorded, or null. */
|
|
17
|
+
export function recordedPort(project: Project): number | null {
|
|
18
|
+
try {
|
|
19
|
+
return Number(JSON.parse(fs.readFileSync(project.serverFile, 'utf8')).port) || null
|
|
20
|
+
} catch {
|
|
21
|
+
return null
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Where the server a record (a server.json) names is reached: the URL it recorded (a server in a
|
|
27
|
+
* container, see serverUrl in entries.ts), else its port at localhost; null without a record.
|
|
28
|
+
*/
|
|
29
|
+
export function recordedOrigin(file: string): string | null {
|
|
30
|
+
try {
|
|
31
|
+
const record = JSON.parse(fs.readFileSync(file, 'utf8'))
|
|
32
|
+
if (typeof record.url === 'string' && record.url) return record.url
|
|
33
|
+
return Number(record.port) ? `http://localhost:${Number(record.port)}` : null
|
|
34
|
+
} catch {
|
|
35
|
+
return null
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The process server.json names for this project, if it names one. */
|
|
40
|
+
export function recordedPid(project: Project): number | null {
|
|
41
|
+
try {
|
|
42
|
+
return Number(JSON.parse(fs.readFileSync(project.serverFile, 'utf8')).pid) || null
|
|
43
|
+
} catch {
|
|
44
|
+
return null
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Does process `pid` run? One of another user's (EPERM) does too. */
|
|
49
|
+
export function alive(pid: number): boolean {
|
|
50
|
+
try {
|
|
51
|
+
process.kill(pid, 0)
|
|
52
|
+
return true
|
|
53
|
+
} catch (err) {
|
|
54
|
+
return (err as NodeJS.ErrnoException).code === 'EPERM'
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The base URL of this project's running server, or null. `except`: a process to not count (the asking server itself). */
|
|
59
|
+
export async function runningServer(project: Project, { except }: { except?: number } = {}): Promise<string | null> {
|
|
60
|
+
const base = recordedOrigin(project.serverFile)
|
|
61
|
+
if (!base) return null
|
|
62
|
+
try {
|
|
63
|
+
const res = await fetch(`${base}/api/health`, { signal: AbortSignal.timeout(HEALTH_TIMEOUT) })
|
|
64
|
+
const health = res.ok ? await res.json() : null
|
|
65
|
+
return health?.dir === project.dir && health.pid !== except ? base : null
|
|
66
|
+
} catch {
|
|
67
|
+
return null
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The shared server as its health says: where it is, its version, the projects it serves. */
|
|
72
|
+
export interface SharedServer {
|
|
73
|
+
origin: string
|
|
74
|
+
pid: number
|
|
75
|
+
version: string
|
|
76
|
+
projects: { entry: string; dir: string; base: string }[]
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The port the shared server recorded in the machine folder, or null. */
|
|
80
|
+
export function recordedSharedPort(file: string): number | null {
|
|
81
|
+
try {
|
|
82
|
+
return Number(JSON.parse(fs.readFileSync(file, 'utf8')).port) || null
|
|
83
|
+
} catch {
|
|
84
|
+
return null
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The machine's running shared server, or null: where its record says it is, checked by asking it.
|
|
90
|
+
* `rescan`: have it look for projects first (one just made, say). `except`: a process not to count.
|
|
91
|
+
*/
|
|
92
|
+
export async function sharedServer(
|
|
93
|
+
file: string,
|
|
94
|
+
{ rescan = false, except }: { rescan?: boolean; except?: number } = {},
|
|
95
|
+
): Promise<SharedServer | null> {
|
|
96
|
+
const origin = recordedOrigin(file)
|
|
97
|
+
if (!origin) return null
|
|
98
|
+
try {
|
|
99
|
+
const res = await fetch(`${origin}/api/health${rescan ? '?rescan' : ''}`, {
|
|
100
|
+
signal: AbortSignal.timeout(HEALTH_TIMEOUT),
|
|
101
|
+
})
|
|
102
|
+
const health = res.ok ? await res.json() : null
|
|
103
|
+
if (!health?.hub || health.pid === except) return null
|
|
104
|
+
return { origin, pid: health.pid, version: String(health.version ?? '0.0.0'), projects: health.projects ?? [] }
|
|
105
|
+
} catch {
|
|
106
|
+
return null
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Does something accept connections at `origin` (a server's, as recordedOrigin gives it)? For a
|
|
112
|
+
* server whose health check got no answer: one that accepts but doesn't answer is stuck (its event
|
|
113
|
+
* loop blocked, or out of file descriptors), which is not the same as one that isn't running, and is
|
|
114
|
+
* said otherwise. Localhost is asked at 127.0.0.1, where the servers listen by default.
|
|
115
|
+
*/
|
|
116
|
+
export function accepts(origin: string, timeout = 1000): Promise<boolean> {
|
|
117
|
+
const url = new URL(origin)
|
|
118
|
+
const host = url.hostname === 'localhost' ? '127.0.0.1' : url.hostname.replace(/^\[|\]$/g, '')
|
|
119
|
+
const port = Number(url.port) || (url.protocol === 'https:' ? 443 : 80)
|
|
120
|
+
return new Promise(resolve => {
|
|
121
|
+
const socket = net.connect({ port, host })
|
|
122
|
+
const done = (ok: boolean) => {
|
|
123
|
+
socket.destroy()
|
|
124
|
+
resolve(ok)
|
|
125
|
+
}
|
|
126
|
+
socket.setTimeout(timeout, () => done(false))
|
|
127
|
+
socket.once('connect', () => done(true))
|
|
128
|
+
socket.once('error', () => done(false))
|
|
129
|
+
})
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** A server at `origin` that accepts connections and gives its health check no answer at all. */
|
|
133
|
+
export async function stuckAt(origin: string): Promise<boolean> {
|
|
134
|
+
if (!(await accepts(origin))) return false
|
|
135
|
+
try {
|
|
136
|
+
await fetch(`${origin}/api/health`, { signal: AbortSignal.timeout(2000) })
|
|
137
|
+
return false
|
|
138
|
+
} catch {
|
|
139
|
+
return true
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** What to say about a server at `origin` that accepts connections but didn't answer its health check. */
|
|
144
|
+
export const stuck = (origin: string, log: string) =>
|
|
145
|
+
`the server at ${origin} accepts connections but doesn't answer: it is stuck (its event loop blocked, or out of file descriptors). Restart it; ${log} may say why`
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Is this project one of the machine's entries (see src/entries.ts)? Then the shared server serves
|
|
149
|
+
* it, under its path, and no server of its own runs. A project outside every include runs its own.
|
|
150
|
+
*/
|
|
151
|
+
export function isShared(proj: Project): boolean {
|
|
152
|
+
const config = readMachineConfig()
|
|
153
|
+
return !!config && !!matchesEntries(config, proj.dir)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The URL a project is served at now (`http://localhost:4777`, or `http://localhost:4777/rg/develop`
|
|
158
|
+
* under the shared server; at the URL the server recorded when it has one,
|
|
159
|
+
* `https://workpad.local/rg/develop`), or null when nothing serves it.
|
|
160
|
+
*/
|
|
161
|
+
export async function projectServer(proj: Project): Promise<string | null> {
|
|
162
|
+
if (!isShared(proj)) return runningServer(proj)
|
|
163
|
+
const file = machineServerFile()
|
|
164
|
+
const shared = await sharedServer(file)
|
|
165
|
+
if (!shared) return null
|
|
166
|
+
// a project made a moment ago: the server looks again
|
|
167
|
+
const mine =
|
|
168
|
+
shared.projects.find(p => p.dir === proj.dir) ??
|
|
169
|
+
(await sharedServer(file, { rescan: true }))?.projects.find(p => p.dir === proj.dir)
|
|
170
|
+
return mine ? `${shared.origin}${mine.base}` : null
|
|
171
|
+
}
|
package/src/serve.ts
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
// Running a project's server as a process of its own (`workpad serve`, which `workpad show`
|
|
2
|
+
// starts in the background): one server per project, on the port it had last time, else the
|
|
3
|
+
// default, else the next free one, recorded in .workpad/server.json so the command line finds it
|
|
4
|
+
// (with $WORKPAD_URL, where it is reached instead of at localhost: see serverUrl in entries.ts).
|
|
5
|
+
import fs from 'node:fs'
|
|
6
|
+
import http from 'node:http'
|
|
7
|
+
import path from 'node:path'
|
|
8
|
+
import { DEFAULT_PORT, type Project } from './config.ts'
|
|
9
|
+
import { createServer } from './server.ts'
|
|
10
|
+
import { watchServer, watchStalls } from './watchdog.ts'
|
|
11
|
+
import {
|
|
12
|
+
alive,
|
|
13
|
+
recordedPid,
|
|
14
|
+
recordedPort,
|
|
15
|
+
recordedSharedPort,
|
|
16
|
+
runningServer,
|
|
17
|
+
sharedServer,
|
|
18
|
+
type SharedServer,
|
|
19
|
+
} from './running.ts'
|
|
20
|
+
import {
|
|
21
|
+
listenHost,
|
|
22
|
+
machineConfigFile,
|
|
23
|
+
machineDir,
|
|
24
|
+
machineServerFile,
|
|
25
|
+
readMachineConfig,
|
|
26
|
+
serverUrl,
|
|
27
|
+
stateDir,
|
|
28
|
+
} from './entries.ts'
|
|
29
|
+
import { createHub } from './hub.ts'
|
|
30
|
+
import { compareVersions, VERSION } from './package.ts'
|
|
31
|
+
|
|
32
|
+
/** Listen on the previous port, else the default, else the next free one; then record it in server.json. */
|
|
33
|
+
export async function start(project: Project): Promise<number> {
|
|
34
|
+
// one server per project: another may have started a moment ago
|
|
35
|
+
const other = await runningServer(project, { except: process.pid })
|
|
36
|
+
if (other) {
|
|
37
|
+
console.log(`workpad: ${project.name} is already served at ${other}`)
|
|
38
|
+
process.exit(0)
|
|
39
|
+
}
|
|
40
|
+
// one that runs but doesn't answer yet (starting, or busy as every client reconnects to it) is
|
|
41
|
+
// serving too: a second would take another port, and the first would leave for it
|
|
42
|
+
const recorded = recordedPid(project)
|
|
43
|
+
if (recorded && recorded !== process.pid && alive(recorded)) {
|
|
44
|
+
console.log(`workpad: ${project.name} is already served by pid ${recorded}, which doesn't answer yet`)
|
|
45
|
+
process.exit(0)
|
|
46
|
+
}
|
|
47
|
+
fs.mkdirSync(project.pagesDir, { recursive: true })
|
|
48
|
+
const machine = readMachineConfig()
|
|
49
|
+
const host = listenHost(machine)
|
|
50
|
+
const origins = machine?.origins ?? []
|
|
51
|
+
// the machine config's url is the shared server's: a project's own takes $WORKPAD_URL alone
|
|
52
|
+
const url = serverUrl(null)
|
|
53
|
+
const wp = createServer(project, { origins: () => origins })
|
|
54
|
+
await wp.config()
|
|
55
|
+
const server = http.createServer((req, res) => {
|
|
56
|
+
void wp.handle(req, res)
|
|
57
|
+
})
|
|
58
|
+
server.on('upgrade', (req, conn, head) => wp.upgrade(req, conn, head))
|
|
59
|
+
const candidates = [
|
|
60
|
+
...new Set(
|
|
61
|
+
[recordedPort(project), DEFAULT_PORT, ...Array.from({ length: 50 }, (_, i) => DEFAULT_PORT + 1 + i)].filter(
|
|
62
|
+
(p): p is number => !!p,
|
|
63
|
+
),
|
|
64
|
+
),
|
|
65
|
+
]
|
|
66
|
+
for (const port of candidates) {
|
|
67
|
+
const ok = await listenOn(server, port, host)
|
|
68
|
+
if (!ok) continue
|
|
69
|
+
fs.writeFileSync(
|
|
70
|
+
project.serverFile,
|
|
71
|
+
JSON.stringify({ port, pid: process.pid, ...(url && { url }) }, null, 2) + '\n',
|
|
72
|
+
)
|
|
73
|
+
// listening: from now on its stalls, slow requests and socket errors go to its log
|
|
74
|
+
watchServer(server)
|
|
75
|
+
watchStalls()
|
|
76
|
+
// the command that started this server waits for it; others can now find it
|
|
77
|
+
try {
|
|
78
|
+
fs.unlinkSync(path.join(project.wp, 'server.lock'))
|
|
79
|
+
} catch {}
|
|
80
|
+
const cleanup = () => {
|
|
81
|
+
try {
|
|
82
|
+
if (JSON.parse(fs.readFileSync(project.serverFile, 'utf8')).pid === process.pid)
|
|
83
|
+
fs.unlinkSync(project.serverFile)
|
|
84
|
+
} catch {}
|
|
85
|
+
}
|
|
86
|
+
process.on('exit', cleanup)
|
|
87
|
+
// one server per project: should another have started at the same moment, whichever one
|
|
88
|
+
// server.json names last stays, and the others leave (their clients reconnect to it)
|
|
89
|
+
const yieldToOther = async () => {
|
|
90
|
+
if (await runningServer(project, { except: process.pid })) {
|
|
91
|
+
console.log('workpad: another server of this project took over')
|
|
92
|
+
process.exit(0)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
setTimeout(yieldToOther, 1500).unref()
|
|
96
|
+
setInterval(yieldToOther, 30000).unref()
|
|
97
|
+
for (const signal of ['SIGINT', 'SIGTERM', 'SIGHUP'] as const) process.on(signal, () => process.exit(0))
|
|
98
|
+
console.log(`workpad: ${project.name} (${project.dir}) at ${url ?? `http://localhost:${port}`}`)
|
|
99
|
+
return port
|
|
100
|
+
}
|
|
101
|
+
throw new Error(`no free port from ${DEFAULT_PORT} to ${DEFAULT_PORT + 50}`)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// ---- the shared server ---------------------------------------------------------------------------
|
|
105
|
+
// One per machine (see entries.ts and hub.ts): on the machine config's port, else the default, else
|
|
106
|
+
// the next free one, recorded in the machine folder's server.json with its version, and its URL
|
|
107
|
+
// when the machine config or $WORKPAD_URL gives one (see serverUrl in entries.ts). A newer
|
|
108
|
+
// workpad takes over from an older one: it asks it to exit, then listens in its place. An older
|
|
109
|
+
// one, or one as new, leaves a running one be.
|
|
110
|
+
|
|
111
|
+
/** Run the shared server. Exits (0) when one as new or newer runs already. */
|
|
112
|
+
export async function startShared(dir = machineDir(), state = stateDir()): Promise<number> {
|
|
113
|
+
const file = machineServerFile(state)
|
|
114
|
+
const other = await sharedServer(file, { except: process.pid })
|
|
115
|
+
if (other) {
|
|
116
|
+
if (compareVersions(VERSION, other.version) <= 0) {
|
|
117
|
+
console.log(`workpad: the shared server ${other.version} runs at ${other.origin}`)
|
|
118
|
+
process.exit(0)
|
|
119
|
+
}
|
|
120
|
+
await takeOver(other)
|
|
121
|
+
}
|
|
122
|
+
const config = readMachineConfig(dir)
|
|
123
|
+
if (!config) throw new Error(`no machine config: ${machineConfigFile(dir)}`)
|
|
124
|
+
const url = serverUrl(config)
|
|
125
|
+
fs.mkdirSync(state, { recursive: true })
|
|
126
|
+
const hub = createHub({ dir, state, onShutdown: () => process.exit(0) })
|
|
127
|
+
const server = http.createServer((req, res) => {
|
|
128
|
+
void hub.handle(req, res)
|
|
129
|
+
})
|
|
130
|
+
server.on('upgrade', (req, conn, head) => hub.upgrade(req, conn, head))
|
|
131
|
+
const first = config.port ?? DEFAULT_PORT
|
|
132
|
+
const candidates = [
|
|
133
|
+
...new Set(
|
|
134
|
+
[
|
|
135
|
+
other ? Number(new URL(other.origin).port) : null,
|
|
136
|
+
recordedSharedPort(file),
|
|
137
|
+
first,
|
|
138
|
+
...Array.from({ length: 50 }, (_, i) => first + 1 + i),
|
|
139
|
+
].filter((p): p is number => !!p),
|
|
140
|
+
),
|
|
141
|
+
]
|
|
142
|
+
for (const port of candidates) {
|
|
143
|
+
if (!(await listenOn(server, port, listenHost(config)))) continue
|
|
144
|
+
fs.writeFileSync(
|
|
145
|
+
file,
|
|
146
|
+
JSON.stringify({ port, pid: process.pid, version: VERSION, ...(url && { url }) }, null, 2) + '\n',
|
|
147
|
+
)
|
|
148
|
+
watchServer(server)
|
|
149
|
+
watchStalls()
|
|
150
|
+
try {
|
|
151
|
+
fs.unlinkSync(path.join(state, 'server.lock'))
|
|
152
|
+
} catch {}
|
|
153
|
+
process.on('exit', () => {
|
|
154
|
+
try {
|
|
155
|
+
if (JSON.parse(fs.readFileSync(file, 'utf8')).pid === process.pid) fs.unlinkSync(file)
|
|
156
|
+
} catch {}
|
|
157
|
+
})
|
|
158
|
+
// should another of the same version have started at the same moment, the one server.json
|
|
159
|
+
// names stays; one newer than this takes over by asking
|
|
160
|
+
const yieldToOther = async () => {
|
|
161
|
+
if (await sharedServer(file, { except: process.pid })) {
|
|
162
|
+
console.log('workpad: another shared server took over')
|
|
163
|
+
process.exit(0)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
setTimeout(yieldToOther, 1500).unref()
|
|
167
|
+
setInterval(yieldToOther, 30000).unref()
|
|
168
|
+
for (const signal of ['SIGINT', 'SIGTERM', 'SIGHUP'] as const) process.on(signal, () => process.exit(0))
|
|
169
|
+
const origin = url ?? `http://localhost:${port}`
|
|
170
|
+
console.log(`workpad ${VERSION}: the shared server at ${origin}`)
|
|
171
|
+
for (const p of hub.projects()) console.log(` ${origin}${p.base}/ ${p.dir}`)
|
|
172
|
+
return port
|
|
173
|
+
}
|
|
174
|
+
throw new Error(`no free port from ${first} to ${first + 50}`)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Ask an older shared server to make way, and wait until it has (else end its process). */
|
|
178
|
+
async function takeOver(other: SharedServer) {
|
|
179
|
+
console.log(`workpad ${VERSION}: taking over from the shared server ${other.version} at ${other.origin}`)
|
|
180
|
+
try {
|
|
181
|
+
await fetch(`${other.origin}/api/shutdown`, {
|
|
182
|
+
method: 'POST',
|
|
183
|
+
headers: { 'content-type': 'application/json' },
|
|
184
|
+
body: JSON.stringify({ version: VERSION }),
|
|
185
|
+
signal: AbortSignal.timeout(2000),
|
|
186
|
+
})
|
|
187
|
+
} catch {}
|
|
188
|
+
const gone = async () => {
|
|
189
|
+
try {
|
|
190
|
+
await fetch(`${other.origin}/api/health`, { signal: AbortSignal.timeout(300) })
|
|
191
|
+
return false
|
|
192
|
+
} catch {
|
|
193
|
+
return true
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
for (let i = 0; i < 25; i++) {
|
|
197
|
+
if (await gone()) return
|
|
198
|
+
await new Promise(r => setTimeout(r, 200))
|
|
199
|
+
}
|
|
200
|
+
// one from before /api/shutdown: its process, by the pid it gave
|
|
201
|
+
try {
|
|
202
|
+
process.kill(other.pid, 'SIGTERM')
|
|
203
|
+
} catch {}
|
|
204
|
+
for (let i = 0; i < 25 && !(await gone()); i++) await new Promise(r => setTimeout(r, 200))
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function listenOn(server: http.Server, port: number, host: string): Promise<boolean> {
|
|
208
|
+
return new Promise((resolve, reject) => {
|
|
209
|
+
const onError = (err: NodeJS.ErrnoException) => {
|
|
210
|
+
server.off('listening', onListening)
|
|
211
|
+
err.code === 'EADDRINUSE' ? resolve(false) : reject(err)
|
|
212
|
+
}
|
|
213
|
+
const onListening = () => {
|
|
214
|
+
server.off('error', onError)
|
|
215
|
+
resolve(true)
|
|
216
|
+
}
|
|
217
|
+
server.once('error', onError)
|
|
218
|
+
server.once('listening', onListening)
|
|
219
|
+
server.listen(port, host)
|
|
220
|
+
})
|
|
221
|
+
}
|