dsh-ris-ide 1.7.2 → 1.8.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 +43 -4
- package/bin/check-listing.mjs +247 -0
- package/bin/proxy-janitor.mjs +183 -0
- package/bin/risd.mjs +287 -0
- package/bin/run-with-log.ps1 +43 -5
- package/lib/client-codemirror.js +92 -66
- package/lib/client.js +774 -89
- package/lib/index.js +2256 -21
- package/lib/janitor.js +377 -0
- package/lib/proxy.js +1577 -0
- package/lib/proxybridge-bundle.js +798 -0
- package/lib/redirector.js +2558 -0
- package/lib/risd/build.js +91 -0
- package/lib/risd/debug.js +374 -0
- package/lib/risd/git.js +207 -0
- package/lib/risd/leases.js +124 -0
- package/lib/risd/lifecycle.js +148 -0
- package/lib/risd/log.js +59 -0
- package/lib/risd/ping.js +94 -0
- package/lib/risd/programs.js +348 -0
- package/lib/risd/server.js +1478 -0
- package/lib/risd/state.js +137 -0
- package/lib/risd/ui.js +1705 -0
- package/lib/risd/view.js +79 -0
- package/lib/risd-client.js +282 -0
- package/lib/standalone/window.html +14 -16
- package/lib/startup.js +114 -0
- package/package.json +7 -2
- package/skills/dsh-ris-ide-debug/SKILL.md +37 -2
- package/test/client-build.test.mjs +113 -0
- package/test/client-viewer.test.mjs +314 -20
- package/test/companion.test.mjs +65 -0
- package/test/dsh-origin.test.mjs +140 -0
- package/test/dsh-window-refresh.test.mjs +100 -0
- package/test/harness.test.mjs +9 -0
- package/test/host-noloop.test.mjs +74 -0
- package/test/host-tail.test.mjs +54 -5
- package/test/host-window.test.mjs +4 -4
- package/test/inherit-engine.test.mjs +175 -0
- package/test/install-phase.test.mjs +36 -0
- package/test/janitor.test.mjs +172 -0
- package/test/lifecycle.test.mjs +109 -0
- package/test/merge-owners.test.mjs +155 -0
- package/test/proxy-restart.test.mjs +37 -0
- package/test/proxy-route.test.mjs +599 -0
- package/test/proxy-window.test.mjs +45 -0
- package/test/proxy.test.mjs +914 -0
- package/test/proxybridge-bundle.test.mjs +687 -0
- package/test/redirector-bundle.test.mjs +391 -0
- package/test/redirector.test.mjs +571 -0
- package/test/risd-api.test.mjs +470 -0
- package/test/risd-bridge-degrade.test.mjs +101 -0
- package/test/risd-build.test.mjs +20 -0
- package/test/risd-cli-home.test.mjs +50 -0
- package/test/risd-client.test.mjs +130 -0
- package/test/risd-debug.test.mjs +261 -0
- package/test/risd-election.test.mjs +104 -0
- package/test/risd-engine.test.mjs +132 -0
- package/test/risd-entry.test.mjs +118 -0
- package/test/risd-git.test.mjs +109 -0
- package/test/risd-handoff.test.mjs +116 -0
- package/test/risd-heal.test.mjs +50 -0
- package/test/risd-helpers.mjs +108 -0
- package/test/risd-host-p2.test.mjs +147 -0
- package/test/risd-janitor-guard.test.mjs +46 -0
- package/test/risd-lease.test.mjs +83 -0
- package/test/risd-lifecycle.test.mjs +210 -0
- package/test/risd-log-follow.test.mjs +227 -0
- package/test/risd-mode.test.mjs +161 -0
- package/test/risd-ping.test.mjs +99 -0
- package/test/risd-programs.test.mjs +169 -0
- package/test/risd-review-p2.test.mjs +217 -0
- package/test/risd-shutdown.test.mjs +97 -0
- package/test/risd-teardown.test.mjs +87 -0
- package/test/risd-tools.test.mjs +211 -0
- package/test/risd-ui.test.mjs +445 -0
- package/test/risd-write.test.mjs +170 -0
- package/test/startup-reconcile.test.mjs +100 -0
- package/test/windows.test.mjs +37 -2
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* risd — persistent state (`<home>/risd.json`) and the spawn lock.
|
|
3
|
+
*
|
|
4
|
+
* The DAEMON is the only writer. Plugins and the CLI only read: a missing or
|
|
5
|
+
* unparseable file simply means "no daemon", never an error. The token in this
|
|
6
|
+
* file is the only credential the daemon accepts, which is why the file is
|
|
7
|
+
* written 0600 into a 0700 directory and replaced atomically (tmp + rename) —
|
|
8
|
+
* a reader must see either the old state or the new one, never half of each.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-risd/state
|
|
11
|
+
*/
|
|
12
|
+
import { closeSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs'
|
|
13
|
+
import { dirname, join } from 'node:path'
|
|
14
|
+
import { randomBytes } from 'node:crypto'
|
|
15
|
+
import { proxyHomeDir } from '../proxy.js'
|
|
16
|
+
|
|
17
|
+
/** API contract version. Bumped only on a breaking `/v1` change. */
|
|
18
|
+
export const RISD_API_VERSION = 1
|
|
19
|
+
export const RISD_STATE_FILENAME = 'risd.json'
|
|
20
|
+
export const RISD_LOCK_FILENAME = 'risd.spawn.lock'
|
|
21
|
+
/** A spawn lock older than this is stale: the process that took it is gone. */
|
|
22
|
+
export const SPAWN_LOCK_STALE_MS = 10_000
|
|
23
|
+
export const DEFAULT_RISD_PORT = 11082
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Where the daemon keeps its state, honouring the same home override the proxy
|
|
27
|
+
* engine uses (`DSH_RIS_IDE_PROXY_HOME`), so tests and odd setups only ever
|
|
28
|
+
* have to set one variable.
|
|
29
|
+
* @param home - explicit home; defaults to the machine-level proxy home.
|
|
30
|
+
* @returns absolute directory.
|
|
31
|
+
*/
|
|
32
|
+
export function risdHomeDir(home = proxyHomeDir()) {
|
|
33
|
+
// `process.env.X = undefined` stores the STRING "undefined" (Node coerces), so a
|
|
34
|
+
// CLI run without --home used to plant `<cwd>/undefined/` and the plugin could
|
|
35
|
+
// never find the daemon. Treat empty/non-string/"undefined" as "not given".
|
|
36
|
+
if (typeof home !== 'string' || home === '' || home === 'undefined') return proxyHomeDir()
|
|
37
|
+
return home
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function risdStatePath(home = proxyHomeDir()) {
|
|
41
|
+
return join(home, RISD_STATE_FILENAME)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function risdLockPath(home = proxyHomeDir()) {
|
|
45
|
+
return join(home, RISD_LOCK_FILENAME)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Read the daemon state file.
|
|
50
|
+
* @param home - daemon home.
|
|
51
|
+
* @returns the parsed state, or null when absent/unreadable/corrupt.
|
|
52
|
+
*/
|
|
53
|
+
export function readRisdState(home = proxyHomeDir()) {
|
|
54
|
+
try {
|
|
55
|
+
const raw = JSON.parse(readFileSync(risdStatePath(home), 'utf8'))
|
|
56
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) return null
|
|
57
|
+
return raw
|
|
58
|
+
} catch {
|
|
59
|
+
return null
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Atomically replace the state file (0600, directory 0700).
|
|
65
|
+
* @param state - state to persist.
|
|
66
|
+
* @param home - daemon home.
|
|
67
|
+
*/
|
|
68
|
+
export function writeRisdState(state, home = proxyHomeDir()) {
|
|
69
|
+
const path = risdStatePath(home)
|
|
70
|
+
mkdirSync(dirname(path), { recursive: true, mode: 0o700 })
|
|
71
|
+
const tmp = `${path}.${process.pid}.tmp`
|
|
72
|
+
writeFileSync(tmp, `${JSON.stringify(state, null, 2)}\n`, { mode: 0o600 })
|
|
73
|
+
renameSync(tmp, path)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Remove the state file (shutdown, or cleaning up a failed start). */
|
|
77
|
+
export function removeRisdState(home = proxyHomeDir()) {
|
|
78
|
+
rmSync(risdStatePath(home), { force: true })
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** 32 random bytes as hex: enough entropy that the token never needs rotating. */
|
|
82
|
+
export function newRisdToken() {
|
|
83
|
+
return randomBytes(32).toString('hex')
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Take the spawn lock (`O_EXCL`) so two dsh instances racing to start risd do
|
|
88
|
+
* not both fork a server. The lock is advisory — daemon-side EADDRINUSE is what
|
|
89
|
+
* makes single-instance hold — it only avoids the useless double spawn.
|
|
90
|
+
* @param home - daemon home.
|
|
91
|
+
* @param ownerPid - the process taking the lock.
|
|
92
|
+
* @returns true when this process now holds the lock.
|
|
93
|
+
*/
|
|
94
|
+
export function acquireSpawnLock(home = proxyHomeDir(), ownerPid = process.pid) {
|
|
95
|
+
mkdirSync(home, { recursive: true, mode: 0o700 })
|
|
96
|
+
let fd
|
|
97
|
+
try {
|
|
98
|
+
fd = openSync(risdLockPath(home), 'wx', 0o600)
|
|
99
|
+
} catch (error) {
|
|
100
|
+
if (error?.code === 'EEXIST') return false
|
|
101
|
+
throw error
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
writeFileSync(fd, `${JSON.stringify({ pid: ownerPid, at: new Date().toISOString() })}\n`)
|
|
105
|
+
} finally {
|
|
106
|
+
closeSync(fd)
|
|
107
|
+
}
|
|
108
|
+
return true
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function releaseSpawnLock(home = proxyHomeDir()) {
|
|
112
|
+
rmSync(risdLockPath(home), { force: true })
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Age of the current spawn lock.
|
|
117
|
+
* @param home - daemon home.
|
|
118
|
+
* @returns milliseconds since the lock was last written, or null when absent.
|
|
119
|
+
*/
|
|
120
|
+
export function spawnLockAgeMs(home = proxyHomeDir()) {
|
|
121
|
+
try {
|
|
122
|
+
return Math.max(0, Date.now() - statSync(risdLockPath(home)).mtimeMs)
|
|
123
|
+
} catch {
|
|
124
|
+
return null
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Remove the spawn lock if nobody has refreshed it for `staleMs`.
|
|
130
|
+
* @returns true when a stale lock was removed.
|
|
131
|
+
*/
|
|
132
|
+
export function dropStaleSpawnLock(home = proxyHomeDir(), staleMs = SPAWN_LOCK_STALE_MS) {
|
|
133
|
+
const age = spawnLockAgeMs(home)
|
|
134
|
+
if (age === null || age < staleMs) return false
|
|
135
|
+
releaseSpawnLock(home)
|
|
136
|
+
return true
|
|
137
|
+
}
|