@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/hub.ts
ADDED
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
// The shared server: one per machine, serving every project of the machine config's entries, each
|
|
2
|
+
// under its own path (see entries.ts). A request handler like a project's server, so tests make one
|
|
3
|
+
// for a throwaway machine folder and send it requests; listening and its record are serve.ts.
|
|
4
|
+
//
|
|
5
|
+
// / the overview: every project's pages as columns, by state or by project (pages/board.ts);
|
|
6
|
+
// a project of the server's own (overview/ in the machine folder), rendered and
|
|
7
|
+
// live like any project's overview
|
|
8
|
+
// /<entry>/<path>/… a project, as its own server serves it at / (createServer with a base)
|
|
9
|
+
// /api/health { hub: true, pid, version, projects }; `?rescan` looks for projects first
|
|
10
|
+
// /api/index every project's pages and what is open on them, as JSON (?entry=: one entry's),
|
|
11
|
+
// for a page's menu of what waits elsewhere
|
|
12
|
+
// /api/shutdown POST { version }: a newer workpad takes over; an older one is refused
|
|
13
|
+
// /api/projects, /api/pages, /api/forms, /api/chats, /api/subscriptions, /api/agents
|
|
14
|
+
// the API's lists (state.ts) across every project, each item with its
|
|
15
|
+
// project's path (`project`): every open form of the machine in one ask
|
|
16
|
+
// /api/stream every project's log as it grows, as server-sent events, each event with its
|
|
17
|
+
// project; projects that appear meanwhile are followed too
|
|
18
|
+
// /api/listen?entry=<name>[&as=<who>]
|
|
19
|
+
// a listen over a whole entry, for as long as the connection is open: held on
|
|
20
|
+
// every page of its projects, also those that appear meanwhile; its stream is
|
|
21
|
+
// told the entry's projects (listeners.ts). Any number run
|
|
22
|
+
//
|
|
23
|
+
// A project's server is made when the project is found and closed when it goes, so a project
|
|
24
|
+
// appears and disappears without a restart. Each keeps its own state, config and log, as it would
|
|
25
|
+
// on a port of its own: the shared server only routes.
|
|
26
|
+
import fs from 'node:fs'
|
|
27
|
+
import path from 'node:path'
|
|
28
|
+
import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
29
|
+
import type { Duplex } from 'node:stream'
|
|
30
|
+
import { project as projectAt, type Project } from './config.ts'
|
|
31
|
+
import { followEntries, machineDir, readMachineConfig, stateDir, type EntryProject } from './entries.ts'
|
|
32
|
+
import { compareVersions, VERSION } from './package.ts'
|
|
33
|
+
import { API_LISTS, createServer, type ApiList, type WorkpadServer } from './server.ts'
|
|
34
|
+
import type { LiveEvent } from './state.ts'
|
|
35
|
+
import type { IndexPage, IndexProject, Listener, PageStatus } from './define.ts'
|
|
36
|
+
import { holdStream, listenerOf } from './listeners.ts'
|
|
37
|
+
|
|
38
|
+
export interface HubOptions {
|
|
39
|
+
/** the machine folder (its config.json), which the server only reads; default machineDir() */
|
|
40
|
+
dir?: string
|
|
41
|
+
/** the server's own folder, which it writes (the overview's project); default stateDir() */
|
|
42
|
+
state?: string
|
|
43
|
+
/** what `~` means in the globs (tests) */
|
|
44
|
+
home?: string
|
|
45
|
+
/** how often to look for projects coming and going, in ms */
|
|
46
|
+
interval?: number
|
|
47
|
+
/** a newer workpad asked this one to make way (POST /api/shutdown) */
|
|
48
|
+
onShutdown?: () => void
|
|
49
|
+
/** the version this server says it is (tests); default the package's */
|
|
50
|
+
version?: string
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** A project the shared server serves: where, and its own server. */
|
|
54
|
+
interface Served extends EntryProject {
|
|
55
|
+
project: Project
|
|
56
|
+
server: WorkpadServer
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface Hub {
|
|
60
|
+
handle(req: IncomingMessage, res: ServerResponse): Promise<void>
|
|
61
|
+
/** an upgrade request (the HTTP server's 'upgrade' event): a page's live channel */
|
|
62
|
+
upgrade(req: IncomingMessage, conn: Duplex, head: Buffer): void
|
|
63
|
+
/** the projects it serves now */
|
|
64
|
+
projects(): EntryProject[]
|
|
65
|
+
/** look for projects coming and going now */
|
|
66
|
+
rescan(): EntryProject[]
|
|
67
|
+
close(): void
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function createHub({
|
|
71
|
+
dir = machineDir(),
|
|
72
|
+
state = stateDir(),
|
|
73
|
+
home,
|
|
74
|
+
interval,
|
|
75
|
+
onShutdown,
|
|
76
|
+
version = VERSION,
|
|
77
|
+
}: HubOptions = {}): Hub {
|
|
78
|
+
const served = new Map<string, Served>()
|
|
79
|
+
// the machine config's origins, as last read: the projects' servers ask for them on each request
|
|
80
|
+
let origins: readonly string[] = []
|
|
81
|
+
const allowed = () => origins
|
|
82
|
+
// the listens over a whole entry (`workpad listen` without a page in one of its projects): each
|
|
83
|
+
// held as a listener on every project of its entry, and what tells its stream the projects
|
|
84
|
+
interface EntryListen {
|
|
85
|
+
entry: string
|
|
86
|
+
listener: Listener
|
|
87
|
+
held: Map<Served, () => void>
|
|
88
|
+
tell: () => void
|
|
89
|
+
}
|
|
90
|
+
const entryListens = new Set<EntryListen>()
|
|
91
|
+
/** An entry listen held on one of its projects, so the project's pages say it listens. */
|
|
92
|
+
function hold(s: Served, el: EntryListen) {
|
|
93
|
+
if (!el.held.has(s)) el.held.set(s, s.server.addListener(el.listener))
|
|
94
|
+
}
|
|
95
|
+
/** Let go of a project (gone, or the listen ended). */
|
|
96
|
+
function release(s: Served, el: EntryListen) {
|
|
97
|
+
el.held.get(s)?.()
|
|
98
|
+
el.held.delete(s)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// the streams across every project (/api/stream): each follows every project's log, and the
|
|
102
|
+
// projects that appear later
|
|
103
|
+
interface Stream {
|
|
104
|
+
query: Record<string, string>
|
|
105
|
+
send: (e: LiveEvent) => void
|
|
106
|
+
followed: Map<Served, () => void>
|
|
107
|
+
}
|
|
108
|
+
const allStreams = new Set<Stream>()
|
|
109
|
+
const followAll = (st: Stream) => {
|
|
110
|
+
for (const s of served.values()) if (!st.followed.has(s)) st.followed.set(s, s.server.stream(st.query, st.send))
|
|
111
|
+
for (const [s, stop] of st.followed)
|
|
112
|
+
if (![...served.values()].includes(s)) {
|
|
113
|
+
stop()
|
|
114
|
+
st.followed.delete(s)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function sync(projects: EntryProject[]) {
|
|
119
|
+
// projects come and go: the overview lists them
|
|
120
|
+
setImmediate(() => touch())
|
|
121
|
+
// a project whose entry's files changed is made again with it
|
|
122
|
+
const wanted = new Map(projects.map(p => [`${p.base}\n${p.dir}\n${p.files ?? ''}`, p]))
|
|
123
|
+
for (const [key, s] of served)
|
|
124
|
+
if (!wanted.has(key)) {
|
|
125
|
+
for (const el of entryListens) release(s, el)
|
|
126
|
+
s.server.close()
|
|
127
|
+
served.delete(key)
|
|
128
|
+
console.log(`workpad: ${s.base} is gone (${s.dir})`)
|
|
129
|
+
}
|
|
130
|
+
for (const [key, p] of wanted) {
|
|
131
|
+
if (served.has(key)) continue
|
|
132
|
+
const project = projectAt(p.dir)
|
|
133
|
+
if (!project) continue
|
|
134
|
+
const server = createServer(project, {
|
|
135
|
+
base: p.base,
|
|
136
|
+
entry: p.entry,
|
|
137
|
+
label: [p.group, p.name].filter(Boolean).join(' › '),
|
|
138
|
+
onChange: () => changed(p.base),
|
|
139
|
+
origins: allowed,
|
|
140
|
+
filesDefault: p.files,
|
|
141
|
+
})
|
|
142
|
+
const s: Served = { ...p, project, server }
|
|
143
|
+
served.set(key, s)
|
|
144
|
+
for (const el of entryListens) if (el.entry === p.entry) hold(s, el)
|
|
145
|
+
console.log(`workpad: ${p.base} → ${p.dir}`)
|
|
146
|
+
}
|
|
147
|
+
// an entry listen follows its projects coming and going
|
|
148
|
+
for (const el of entryListens) el.tell()
|
|
149
|
+
for (const st of allStreams) followAll(st)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// the overview at /: a project of the shared server's own, in the machine folder, whose overview
|
|
153
|
+
// lists every project's pages; rendered and live like any project's, so it looks like one
|
|
154
|
+
const overviewDir = path.join(state, 'overview')
|
|
155
|
+
fs.mkdirSync(path.join(overviewDir, '.workpad', 'pages'), { recursive: true })
|
|
156
|
+
const root = createServer(projectAt(overviewDir)!, {
|
|
157
|
+
name: 'workpad',
|
|
158
|
+
files: false,
|
|
159
|
+
origins: allowed,
|
|
160
|
+
overview: async () => {
|
|
161
|
+
const projects = await Promise.all(
|
|
162
|
+
[...served.values()].map(async s => ({
|
|
163
|
+
...(await indexOf(s)),
|
|
164
|
+
files: await s.server.files().catch(() => false),
|
|
165
|
+
})),
|
|
166
|
+
)
|
|
167
|
+
const boxed = (process.env.WORKPAD_SANDBOX ?? '').split(':').filter(Boolean)
|
|
168
|
+
return {
|
|
169
|
+
projects,
|
|
170
|
+
note: `workpad ${version} · ${boxed.length ? `boxed: ${boxed.join(', ')}` : 'not boxed'} · ${dir}/config.json`,
|
|
171
|
+
}
|
|
172
|
+
},
|
|
173
|
+
})
|
|
174
|
+
// what the overview lists changed: told a moment later, once for several changes
|
|
175
|
+
let touching: ReturnType<typeof setTimeout> | undefined
|
|
176
|
+
// the projects that changed since: each other project's open pages are told, for their menu of
|
|
177
|
+
// what waits elsewhere (pushed, so no page has to ask every project now and then)
|
|
178
|
+
const changes = new Set<string>()
|
|
179
|
+
const touch = () => {
|
|
180
|
+
clearTimeout(touching)
|
|
181
|
+
touching = setTimeout(() => {
|
|
182
|
+
root.touch()
|
|
183
|
+
const from = [...changes]
|
|
184
|
+
changes.clear()
|
|
185
|
+
for (const s of served.values()) if (from.some(b => b !== s.base)) s.server.elsewhere()
|
|
186
|
+
}, 150)
|
|
187
|
+
}
|
|
188
|
+
const changed = (base: string) => {
|
|
189
|
+
changes.add(base)
|
|
190
|
+
touch()
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const entries = followEntries(
|
|
194
|
+
() => {
|
|
195
|
+
const config = readMachineConfig(dir)
|
|
196
|
+
origins = config?.origins ?? []
|
|
197
|
+
return config
|
|
198
|
+
},
|
|
199
|
+
sync,
|
|
200
|
+
{ interval, home },
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
/** The project a path is under: the longest base that is the path or a folder above it. */
|
|
204
|
+
const owner = (p: string): Served | null => {
|
|
205
|
+
let best: Served | null = null
|
|
206
|
+
for (const s of served.values())
|
|
207
|
+
if ((p === s.base || p.startsWith(s.base + '/')) && (!best || s.base.length > best.base.length)) best = s
|
|
208
|
+
return best
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** A project's pages and what waits on each, as the index and the overview give them. */
|
|
212
|
+
async function indexOf(s: Served): Promise<IndexProject> {
|
|
213
|
+
const at = {
|
|
214
|
+
entry: s.entry,
|
|
215
|
+
group: s.group,
|
|
216
|
+
name: s.name,
|
|
217
|
+
label: [s.group, s.name].filter(Boolean).join(' › '),
|
|
218
|
+
dir: s.dir,
|
|
219
|
+
base: s.base,
|
|
220
|
+
}
|
|
221
|
+
let statuses: PageStatus[]
|
|
222
|
+
try {
|
|
223
|
+
statuses = await s.server.status()
|
|
224
|
+
} catch (err) {
|
|
225
|
+
return { ...at, pages: [], active: 0, error: (err as Error).message }
|
|
226
|
+
}
|
|
227
|
+
const pages = statuses
|
|
228
|
+
.map((p): IndexPage => ({
|
|
229
|
+
page: p.page,
|
|
230
|
+
title: p.title,
|
|
231
|
+
url: `${s.base}/${p.page.split('/').map(encodeURIComponent).join('/')}`,
|
|
232
|
+
mtime: mtimeOf(p.file),
|
|
233
|
+
open: p.open,
|
|
234
|
+
unread: p.unread.length,
|
|
235
|
+
unreceived: p.unreceived.length,
|
|
236
|
+
}))
|
|
237
|
+
.toSorted((a, b) => b.mtime - a.mtime)
|
|
238
|
+
return { ...at, pages, active: pages[0]?.mtime ?? 0 }
|
|
239
|
+
}
|
|
240
|
+
const index = () => Promise.all([...served.values()].map(indexOf))
|
|
241
|
+
|
|
242
|
+
const send = (res: ServerResponse, status: number, body: string, type = 'text/html; charset=utf-8') => {
|
|
243
|
+
res.writeHead(status, { 'content-type': type, 'cache-control': 'no-store' })
|
|
244
|
+
res.end(body)
|
|
245
|
+
}
|
|
246
|
+
const sendJson = (res: ServerResponse, status: number, data: unknown) =>
|
|
247
|
+
send(res, status, JSON.stringify(data), 'application/json')
|
|
248
|
+
const list = () =>
|
|
249
|
+
[...served.values()].map(({ entry, group, name, dir, base }) => ({ entry, group, name, dir, base }))
|
|
250
|
+
|
|
251
|
+
function readBody(req: IncomingMessage): Promise<string> {
|
|
252
|
+
return new Promise((resolve, reject) => {
|
|
253
|
+
let body = ''
|
|
254
|
+
req.setEncoding('utf8')
|
|
255
|
+
req.on('data', c => {
|
|
256
|
+
body += c
|
|
257
|
+
if (body.length > 1e4) req.destroy()
|
|
258
|
+
})
|
|
259
|
+
req.on('end', () => resolve(body))
|
|
260
|
+
req.on('error', reject)
|
|
261
|
+
})
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
async function handle(req: IncomingMessage, res: ServerResponse): Promise<void> {
|
|
265
|
+
try {
|
|
266
|
+
const url = new URL((req.url ?? '/').replace(/^\/+/, '/'), 'http://localhost')
|
|
267
|
+
const p = decodeURIComponent(url.pathname)
|
|
268
|
+
if (p === '/api/health') {
|
|
269
|
+
if (url.searchParams.has('rescan')) entries.rescan()
|
|
270
|
+
return sendJson(res, 200, { ok: true, hub: true, pid: process.pid, version, dir, projects: list() })
|
|
271
|
+
}
|
|
272
|
+
if (p === '/api/index') {
|
|
273
|
+
const entry = url.searchParams.get('entry')
|
|
274
|
+
return sendJson(
|
|
275
|
+
res,
|
|
276
|
+
200,
|
|
277
|
+
(await index()).filter(x => !entry || x.entry === entry),
|
|
278
|
+
)
|
|
279
|
+
}
|
|
280
|
+
if (p === '/api/shutdown' && req.method === 'POST') {
|
|
281
|
+
// only from the command line: a page of a project has an Origin
|
|
282
|
+
if (req.headers.origin) return sendJson(res, 403, { error: 'not from a browser' })
|
|
283
|
+
let asker = ''
|
|
284
|
+
try {
|
|
285
|
+
asker = String(JSON.parse(await readBody(req)).version ?? '')
|
|
286
|
+
} catch {}
|
|
287
|
+
// an older workpad never replaces a newer one, nor one as new
|
|
288
|
+
if (!asker || compareVersions(asker, version) <= 0)
|
|
289
|
+
return sendJson(res, 409, { error: `workpad ${version} runs here; ${asker || 'that'} is not newer` })
|
|
290
|
+
sendJson(res, 200, { ok: true, version })
|
|
291
|
+
setImmediate(() => onShutdown?.())
|
|
292
|
+
return
|
|
293
|
+
}
|
|
294
|
+
if (p === '/api/listen' && url.searchParams.has('entry')) return listenEntry(req, res, url.searchParams)
|
|
295
|
+
// the API across every project: each project's list, put together
|
|
296
|
+
const what = /^\/api\/(\w+)$/.exec(p)?.[1]
|
|
297
|
+
if (req.method === 'GET' && what && (API_LISTS as readonly string[]).includes(what)) {
|
|
298
|
+
const query = Object.fromEntries(url.searchParams)
|
|
299
|
+
const all = await Promise.all(
|
|
300
|
+
[...served.values()].map(s => s.server.read(what as ApiList, query).catch(() => [] as unknown[])),
|
|
301
|
+
)
|
|
302
|
+
return sendJson(res, 200, all.flat())
|
|
303
|
+
}
|
|
304
|
+
if (p === '/api/stream') return streamAll(req, res, Object.fromEntries(url.searchParams))
|
|
305
|
+
if (p === '/favicon.ico') {
|
|
306
|
+
res.writeHead(204)
|
|
307
|
+
return void res.end()
|
|
308
|
+
}
|
|
309
|
+
// the project's overview is at its folder: /rg/develop/
|
|
310
|
+
const s = owner(p)
|
|
311
|
+
if (s && p === s.base) {
|
|
312
|
+
res.writeHead(302, { location: `${s.base}/${url.search}` })
|
|
313
|
+
return void res.end()
|
|
314
|
+
}
|
|
315
|
+
return route(req, p, url).handle(req, res)
|
|
316
|
+
} catch (err) {
|
|
317
|
+
console.error(err)
|
|
318
|
+
send(res, 500, String((err as Error)?.stack || err), 'text/plain')
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Whose request this is: a project's (`req.url` made the path within it), or else the overview's.
|
|
324
|
+
*/
|
|
325
|
+
function route(req: IncomingMessage, p: string, url: URL): WorkpadServer {
|
|
326
|
+
let s = owner(p)
|
|
327
|
+
if (s) {
|
|
328
|
+
// the path less the project's part, encoded again for the project's server to decode
|
|
329
|
+
req.url = p.slice(s.base.length).split('/').map(encodeURIComponent).join('/') + url.search
|
|
330
|
+
return s.server
|
|
331
|
+
}
|
|
332
|
+
// A plugin's browser module that writes `/api/<name>/…` itself, from before api.url: the page
|
|
333
|
+
// that asked is in the Referer, and the request is that project's.
|
|
334
|
+
const from = req.headers.referer ? new URL(req.headers.referer).pathname : null
|
|
335
|
+
s = /^\/(api|_)\//.test(p) && from ? owner(decodeURIComponent(from)) : null
|
|
336
|
+
req.url = s ? (req.url ?? '/').replace(/^\/+/, '/') : req.url
|
|
337
|
+
// anything else outside the projects is the overview's: /, its page, its files and live channel
|
|
338
|
+
return s ? s.server : root
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** An upgrade (a page's live channel), routed as a request is. */
|
|
342
|
+
function upgrade(req: IncomingMessage, conn: Duplex, head: Buffer) {
|
|
343
|
+
try {
|
|
344
|
+
const url = new URL((req.url ?? '/').replace(/^\/+/, '/'), 'http://localhost')
|
|
345
|
+
route(req, decodeURIComponent(url.pathname), url).upgrade(req, conn, head)
|
|
346
|
+
} catch (err) {
|
|
347
|
+
console.error(err)
|
|
348
|
+
conn.destroy()
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* A listen over a whole entry (`workpad listen` without a page in one of its projects): any
|
|
354
|
+
* number run. Each project holds it as a listener, so its pages say who listens; its stream is
|
|
355
|
+
* told the entry's projects, at once and whenever they change. The command itself reads their logs.
|
|
356
|
+
*/
|
|
357
|
+
function listenEntry(req: IncomingMessage, res: ServerResponse, q: URLSearchParams) {
|
|
358
|
+
const entry = q.get('entry')!
|
|
359
|
+
const mine = () => [...served.values()].filter(s => s.entry === entry)
|
|
360
|
+
const el: EntryListen = { entry, listener: listenerOf(q), held: new Map(), tell: () => {} }
|
|
361
|
+
for (const s of mine()) hold(s, el)
|
|
362
|
+
entryListens.add(el)
|
|
363
|
+
const stream = holdStream(req, res, () => {
|
|
364
|
+
closers.delete(stream.end)
|
|
365
|
+
entryListens.delete(el)
|
|
366
|
+
for (const s of [...el.held.keys()]) release(s, el)
|
|
367
|
+
})
|
|
368
|
+
el.tell = () => stream.send('projects', { projects: mine().map(s => ({ dir: s.dir, base: s.base })) })
|
|
369
|
+
closers.add(stream.end)
|
|
370
|
+
el.tell()
|
|
371
|
+
}
|
|
372
|
+
const closers = new Set<() => void>()
|
|
373
|
+
|
|
374
|
+
/** /api/stream across every project: as a project's, each event with its project. */
|
|
375
|
+
function streamAll(req: IncomingMessage, res: ServerResponse, query: Record<string, string>) {
|
|
376
|
+
res.writeHead(200, { 'content-type': 'text/event-stream', 'cache-control': 'no-store', connection: 'keep-alive' })
|
|
377
|
+
res.write(': streaming\n\n')
|
|
378
|
+
const st: Stream = {
|
|
379
|
+
query,
|
|
380
|
+
send: e => res.write(`event: event\nid: ${e.id}\ndata: ${JSON.stringify(e)}\n\n`),
|
|
381
|
+
followed: new Map(),
|
|
382
|
+
}
|
|
383
|
+
allStreams.add(st)
|
|
384
|
+
followAll(st)
|
|
385
|
+
const ping = setInterval(() => res.write(': ping\n\n'), 25000)
|
|
386
|
+
const end = () => {
|
|
387
|
+
clearInterval(ping)
|
|
388
|
+
allStreams.delete(st)
|
|
389
|
+
for (const stop of st.followed.values()) stop()
|
|
390
|
+
st.followed.clear()
|
|
391
|
+
closers.delete(close)
|
|
392
|
+
}
|
|
393
|
+
const close = () => {
|
|
394
|
+
end()
|
|
395
|
+
res.end()
|
|
396
|
+
}
|
|
397
|
+
closers.add(close)
|
|
398
|
+
req.on('close', end)
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
return {
|
|
402
|
+
handle,
|
|
403
|
+
upgrade,
|
|
404
|
+
projects: list,
|
|
405
|
+
rescan() {
|
|
406
|
+
entries.rescan()
|
|
407
|
+
return list()
|
|
408
|
+
},
|
|
409
|
+
close() {
|
|
410
|
+
clearTimeout(touching)
|
|
411
|
+
root.close()
|
|
412
|
+
for (const c of closers) c()
|
|
413
|
+
entries.stop()
|
|
414
|
+
for (const s of served.values()) s.server.close()
|
|
415
|
+
served.clear()
|
|
416
|
+
},
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const mtimeOf = (file: string) => {
|
|
421
|
+
try {
|
|
422
|
+
return fs.statSync(file).mtimeMs
|
|
423
|
+
} catch {
|
|
424
|
+
return 0
|
|
425
|
+
}
|
|
426
|
+
}
|
package/src/icons.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Icons the page and the server both draw, as SVG markup. No Node in it: the browser runtime
|
|
2
|
+
// imports it too.
|
|
3
|
+
|
|
4
|
+
/** An inbox: what awaits the reader (the header's menu, the overview). */
|
|
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>'
|
|
7
|
+
|
|
8
|
+
/** Copy: two sheets (the list of who listens copies an agent's id with it). */
|
|
9
|
+
export const COPY_ICON =
|
|
10
|
+
'<svg viewBox="0 0 16 16" width="13" height="13" aria-hidden="true"><rect x="5.5" y="5.5" width="8" height="8" rx="1.5" fill="none" stroke="currentColor" stroke-width="1.4"/><path d="M10.5 3.5v-.5A1.5 1.5 0 0 0 9 1.5H4A1.5 1.5 0 0 0 2.5 3v5A1.5 1.5 0 0 0 4 9.5h.5" fill="none" stroke="currentColor" stroke-width="1.4"/></svg>'
|
|
11
|
+
|
|
12
|
+
/** Done: a check (what was copied). */
|
|
13
|
+
export const CHECK_ICON =
|
|
14
|
+
'<svg viewBox="0 0 16 16" width="13" height="13" aria-hidden="true"><path d="M3 8.5 6.5 12 13 4.5" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"/></svg>'
|
|
15
|
+
|
|
16
|
+
/** "Awaiting you: 2 things", and where: what the inbox says to a reader who hovers it. */
|
|
17
|
+
export const awaitingLabel = (n: number, where = '') =>
|
|
18
|
+
`Awaiting you: ${n === 1 ? '1 question or message' : `${n} questions and messages`}${where}`
|
package/src/listen.ts
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// The command's side of listening (`workpad listen`): holding a listener at a server. A listen
|
|
2
|
+
// registers at its project's server first and only then reads the log, so from its first answer on
|
|
3
|
+
// the server knows it listens. A listen without a page in a project of the machine's entries covers
|
|
4
|
+
// the whole entry: the shared server names its projects (listeners.ts), at once and on every change,
|
|
5
|
+
// and the listen hands out from each project's log.
|
|
6
|
+
import http from 'node:http'
|
|
7
|
+
import https from 'node:https'
|
|
8
|
+
import type { Followed, Snapshot } from './listeners.ts'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A held GET to the server, over http or https as its address says: a server reached at an https
|
|
12
|
+
* URL (a container behind a proxy, see serverUrl) is held the same way, its certificate checked as
|
|
13
|
+
* Node checks any (a private CA: NODE_EXTRA_CA_CERTS).
|
|
14
|
+
*/
|
|
15
|
+
const get = (url: string, onResponse: (res: http.IncomingMessage) => void): http.ClientRequest =>
|
|
16
|
+
(url.startsWith('https:') ? https : http).get(url, onResponse)
|
|
17
|
+
|
|
18
|
+
export interface ListenOptions {
|
|
19
|
+
/** the server's base URL; null (or a throw) while none runs, tried again */
|
|
20
|
+
at: () => Promise<string | null>
|
|
21
|
+
/** what to ask /api/listen for: page, type, agent, or `entry` */
|
|
22
|
+
query: Record<string, string>
|
|
23
|
+
/** a listen over a whole entry: the projects it covers, at once and on every change */
|
|
24
|
+
onSnapshot?: (snapshot: Snapshot) => void
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Hold a listener at a server for as long as the process runs, and again after the server restarts.
|
|
29
|
+
* Resolves once the server holds it; with `orNone`, also after a first try that found no server
|
|
30
|
+
* running (a listen reads the log all the same, and registers once one appears).
|
|
31
|
+
*/
|
|
32
|
+
export function listen({ at, query, onSnapshot }: ListenOptions, { orNone = false } = {}): Promise<void> {
|
|
33
|
+
return new Promise(settled => {
|
|
34
|
+
const connect = async () => {
|
|
35
|
+
let base: string | null = null
|
|
36
|
+
try {
|
|
37
|
+
base = await at()
|
|
38
|
+
} catch {
|
|
39
|
+
/* none runs */
|
|
40
|
+
}
|
|
41
|
+
if (!base) {
|
|
42
|
+
if (orNone) settled()
|
|
43
|
+
return setTimeout(connect, 2000).unref()
|
|
44
|
+
}
|
|
45
|
+
let again = false
|
|
46
|
+
const retry = (ms: number) => {
|
|
47
|
+
if (!again) {
|
|
48
|
+
again = true
|
|
49
|
+
setTimeout(connect, ms).unref()
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const req = get(`${base}/api/listen?${new URLSearchParams(query)}`, res => {
|
|
53
|
+
res.setEncoding('utf8')
|
|
54
|
+
settled()
|
|
55
|
+
// held: the connection alone does not keep the command running. Not before: until the
|
|
56
|
+
// server answers, nothing else may keep it alive, and it would exit mid-connect
|
|
57
|
+
res.socket.unref()
|
|
58
|
+
let buf = ''
|
|
59
|
+
res.on('data', (chunk: string) => {
|
|
60
|
+
buf += chunk
|
|
61
|
+
// an event ends at a blank line: `event: projects` and its data
|
|
62
|
+
let end
|
|
63
|
+
while ((end = buf.indexOf('\n\n')) >= 0) {
|
|
64
|
+
const block = buf.slice(0, end)
|
|
65
|
+
buf = buf.slice(end + 2)
|
|
66
|
+
const event = /^event: (.*)$/m.exec(block)?.[1]
|
|
67
|
+
const data = /^data: (.*)$/m.exec(block)?.[1]
|
|
68
|
+
if (event === 'projects' && data && onSnapshot) onSnapshot(JSON.parse(data) as Snapshot)
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
res.on('close', () => retry(1000))
|
|
72
|
+
})
|
|
73
|
+
req.on('error', () => retry(2000))
|
|
74
|
+
}
|
|
75
|
+
void connect()
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* A listen over the projects a server names: `start` is called once for each project as it appears,
|
|
81
|
+
* to hand out from its log; a project no longer named is stopped (`signal`).
|
|
82
|
+
* @returns what takes each snapshot the server sends
|
|
83
|
+
*/
|
|
84
|
+
export function everyProject(start: (project: Followed, signal: AbortSignal) => void): (snapshot: Snapshot) => void {
|
|
85
|
+
const followed = new Map<string, AbortController>()
|
|
86
|
+
return ({ projects }) => {
|
|
87
|
+
for (const p of projects) {
|
|
88
|
+
if (followed.has(p.dir)) continue
|
|
89
|
+
const stop = new AbortController()
|
|
90
|
+
followed.set(p.dir, stop)
|
|
91
|
+
start(p, stop.signal)
|
|
92
|
+
}
|
|
93
|
+
for (const [dir, stop] of followed)
|
|
94
|
+
if (!projects.some(p => p.dir === dir)) {
|
|
95
|
+
stop.abort()
|
|
96
|
+
followed.delete(dir)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** What a channel's line does with what the server sends it (`CommandContext.channel`). */
|
|
102
|
+
export interface ChannelHandlers {
|
|
103
|
+
/** `workpad agent ping` in the session: push the pong, with this code, into it */
|
|
104
|
+
onPing(code: string): void
|
|
105
|
+
/** the agent confirmed a pong: its messages reach it */
|
|
106
|
+
onConfirmed(): void
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Hold an agent channel's line at the server (/api/channel) for as long as the process runs, and
|
|
111
|
+
* again after the server restarts, then saying it was confirmed: the server forgets that. `move`:
|
|
112
|
+
* the session it serves changed (a resume); it was confirmed for the process, not the session, so
|
|
113
|
+
* that stays.
|
|
114
|
+
*/
|
|
115
|
+
export function holdChannel(
|
|
116
|
+
at: () => Promise<string | null>,
|
|
117
|
+
first: string,
|
|
118
|
+
{ onPing, onConfirmed }: ChannelHandlers,
|
|
119
|
+
): { move(session: string): void } {
|
|
120
|
+
const id = Math.random().toString(36).slice(2) + Date.now().toString(36)
|
|
121
|
+
let session = first
|
|
122
|
+
let confirmed = false
|
|
123
|
+
let current: http.ClientRequest | null = null
|
|
124
|
+
const connect = async () => {
|
|
125
|
+
let base: string | null = null
|
|
126
|
+
try {
|
|
127
|
+
base = await at()
|
|
128
|
+
} catch {
|
|
129
|
+
/* none runs */
|
|
130
|
+
}
|
|
131
|
+
if (!base) return void setTimeout(connect, 2000).unref()
|
|
132
|
+
let again = false
|
|
133
|
+
const retry = (ms: number) => {
|
|
134
|
+
if (again) return
|
|
135
|
+
again = true
|
|
136
|
+
setTimeout(connect, ms).unref()
|
|
137
|
+
}
|
|
138
|
+
const query = new URLSearchParams({ id, session, ...(confirmed && { confirmed: '1' }) })
|
|
139
|
+
const req = (current = get(`${base}/api/channel?${query}`, res => {
|
|
140
|
+
res.setEncoding('utf8')
|
|
141
|
+
res.socket.unref()
|
|
142
|
+
let buf = ''
|
|
143
|
+
res.on('data', (chunk: string) => {
|
|
144
|
+
buf += chunk
|
|
145
|
+
let end
|
|
146
|
+
while ((end = buf.indexOf('\n\n')) >= 0) {
|
|
147
|
+
const block = buf.slice(0, end)
|
|
148
|
+
buf = buf.slice(end + 2)
|
|
149
|
+
const event = /^event: (.*)$/m.exec(block)?.[1]
|
|
150
|
+
const data = /^data: (.*)$/m.exec(block)?.[1]
|
|
151
|
+
if (event === 'ping' && data) onPing((JSON.parse(data) as { code: string }).code)
|
|
152
|
+
if (event === 'confirmed' && !confirmed) {
|
|
153
|
+
confirmed = true
|
|
154
|
+
onConfirmed()
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
})
|
|
158
|
+
res.on('close', () => retry(1000))
|
|
159
|
+
}))
|
|
160
|
+
req.on('error', () => retry(2000))
|
|
161
|
+
}
|
|
162
|
+
void connect()
|
|
163
|
+
return {
|
|
164
|
+
move(next) {
|
|
165
|
+
if (next === session) return
|
|
166
|
+
session = next
|
|
167
|
+
// its close connects again, with the new session
|
|
168
|
+
current?.destroy()
|
|
169
|
+
},
|
|
170
|
+
}
|
|
171
|
+
}
|