dsh-ris-ide 1.7.3 → 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 +19 -7
- package/bin/proxy-janitor.mjs +106 -11
- package/bin/risd.mjs +287 -0
- package/bin/run-with-log.ps1 +43 -5
- package/lib/client.js +515 -1152
- package/lib/index.js +1124 -55
- package/lib/janitor.js +106 -15
- package/lib/proxy.js +86 -7
- package/lib/proxybridge-bundle.js +798 -0
- package/lib/redirector.js +484 -35
- 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 +4 -1
- package/test/client-build.test.mjs +113 -0
- package/test/client-viewer.test.mjs +166 -200
- 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 +21 -4
- package/test/host-window.test.mjs +4 -4
- package/test/janitor.test.mjs +29 -1
- package/test/lifecycle.test.mjs +26 -5
- package/test/merge-owners.test.mjs +155 -0
- package/test/proxy-restart.test.mjs +37 -0
- package/test/proxy-window.test.mjs +45 -0
- package/test/proxy.test.mjs +41 -0
- package/test/proxybridge-bundle.test.mjs +687 -0
- package/test/redirector-bundle.test.mjs +391 -0
- package/test/redirector.test.mjs +59 -9
- 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,348 @@
|
|
|
1
|
+
// Program supervisor owned by the daemon.
|
|
2
|
+
//
|
|
3
|
+
// The panel's program rows are the same rows here: discovery + log naming + pidfile
|
|
4
|
+
// liveness all come from `lib/index.js`, so a program started in one dsh is shown
|
|
5
|
+
// (and stoppable) from another dsh, and keeps running after dsh exits.
|
|
6
|
+
//
|
|
7
|
+
// Only the "session-external" half lives here: list / start / stop / restart / log.
|
|
8
|
+
// File tree, editor, search and the panel UI stay in the plugin.
|
|
9
|
+
import { closeSync, existsSync, openSync, readSync, statSync } from 'node:fs'
|
|
10
|
+
import { isAbsolute, resolve } from 'node:path'
|
|
11
|
+
import {
|
|
12
|
+
configMatchesWorkspace,
|
|
13
|
+
decoratePrograms,
|
|
14
|
+
programsFromConfigEntries,
|
|
15
|
+
readProgramConfig,
|
|
16
|
+
scanPrograms,
|
|
17
|
+
readPidFile,
|
|
18
|
+
startProgram,
|
|
19
|
+
stopLogPath,
|
|
20
|
+
} from '../index.js'
|
|
21
|
+
import { pidAlive } from '../proxy.js'
|
|
22
|
+
|
|
23
|
+
/** Panel default (same as `resolvePath(cwd, 'logs')` in the plugin). */
|
|
24
|
+
export const DEFAULT_LOGS_DIRNAME = 'logs'
|
|
25
|
+
/** Bytes returned by one log read when the client does not ask for more. */
|
|
26
|
+
export const DEFAULT_LOG_BYTES = 64 * 1024
|
|
27
|
+
/** Ceiling for one log read; a follower pages by offset instead. */
|
|
28
|
+
export const MAX_LOG_BYTES = 512 * 1024
|
|
29
|
+
|
|
30
|
+
/** Absolute logs directory for a workspace. */
|
|
31
|
+
export function logsDirFor(cwd, rawDir = '') {
|
|
32
|
+
const dir = typeof rawDir === 'string' && rawDir !== '' ? rawDir : DEFAULT_LOGS_DIRNAME
|
|
33
|
+
return isAbsolute(dir) ? dir : resolve(cwd, dir)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The panel's program list, served by the daemon.
|
|
38
|
+
*
|
|
39
|
+
* Fast path mirrors the panel: a config file that still matches the filesystem IS
|
|
40
|
+
* the list (no tree walk). Otherwise scan and let the scanner name/dedupe rows.
|
|
41
|
+
*/
|
|
42
|
+
export async function listPrograms(cwd, { dir = '', refresh = false } = {}) {
|
|
43
|
+
const logsDir = logsDirFor(cwd, dir)
|
|
44
|
+
const config = await readProgramConfig(cwd)
|
|
45
|
+
const fast = config.exists === true
|
|
46
|
+
&& refresh !== true
|
|
47
|
+
&& configMatchesWorkspace(cwd, config.entries)
|
|
48
|
+
const programs = fast
|
|
49
|
+
? await decoratePrograms(cwd, logsDir, programsFromConfigEntries(cwd, config.entries, config.defaults))
|
|
50
|
+
: await scanPrograms(cwd, logsDir, refresh === true)
|
|
51
|
+
return { cwd, dir: logsDir, programs }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Resolve a program key (id → log name → exact name → unique name prefix).
|
|
56
|
+
*
|
|
57
|
+
* An exact name that matches more than one row is ambiguous, not "the first one":
|
|
58
|
+
* the same module can be configured in two directories under one name, and the
|
|
59
|
+
* caller must use the id instead of silently starting/stopping the wrong row
|
|
60
|
+
* (review B-P2-12).
|
|
61
|
+
* @param {Array<object>} programs - rows from `listPrograms()`.
|
|
62
|
+
* @param {string} wanted - id, log name, name or unambiguous name prefix.
|
|
63
|
+
* @returns {{program: object|null, ambiguous: Array<object>}} match result.
|
|
64
|
+
*/
|
|
65
|
+
export function findProgramMatch(programs, wanted) {
|
|
66
|
+
if (typeof wanted !== 'string' || wanted === '') return { program: null, ambiguous: [] }
|
|
67
|
+
const exact = programs.find((row) => row.id === wanted)
|
|
68
|
+
if (exact !== undefined) return { program: exact, ambiguous: [] }
|
|
69
|
+
const byLog = programs.find((row) => row.logName === wanted)
|
|
70
|
+
if (byLog !== undefined) return { program: byLog, ambiguous: [] }
|
|
71
|
+
const byName = programs.filter((row) => row.name === wanted)
|
|
72
|
+
if (byName.length === 1) return { program: byName[0], ambiguous: [] }
|
|
73
|
+
if (byName.length > 1) return { program: null, ambiguous: byName }
|
|
74
|
+
const prefixed = programs.filter((row) => String(row.name).startsWith(wanted))
|
|
75
|
+
if (prefixed.length === 1) return { program: prefixed[0], ambiguous: [] }
|
|
76
|
+
return { program: null, ambiguous: prefixed.length > 1 ? prefixed : [] }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Resolve a program by id, log name, exact name or a unique name prefix. */
|
|
80
|
+
export function findProgram(programs, wanted) {
|
|
81
|
+
return findProgramMatch(programs, wanted).program
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** `findProgram()` plus the error the HTTP layer should answer with. */
|
|
85
|
+
function findProgramOrError(programs, wanted) {
|
|
86
|
+
const match = findProgramMatch(programs, wanted)
|
|
87
|
+
if (match.program !== null) return { program: match.program, error: null }
|
|
88
|
+
if (match.ambiguous.length > 0) {
|
|
89
|
+
const ids = match.ambiguous.map((row) => row.id).join('、')
|
|
90
|
+
return {
|
|
91
|
+
program: null,
|
|
92
|
+
error: { ok: false, code: 'ambiguous', message: `「${wanted}」匹配到 ${match.ambiguous.length} 个程序(${ids});请改用 id 或 logName`, status: 409 },
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return { program: null, error: { ok: false, code: 'no-program', message: `未找到程序「${wanted}」`, status: 404 } }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Start one program. Already-running rows are a successful no-op (the panel's rule). */
|
|
99
|
+
export async function startProgramByKey(cwd, wanted, { dir = '', options = {} } = {}) {
|
|
100
|
+
const listed = await listPrograms(cwd, { dir })
|
|
101
|
+
const { program, error } = findProgramOrError(listed.programs, wanted)
|
|
102
|
+
if (program === null) return error
|
|
103
|
+
const started = await startProgram(cwd, listed.dir, program, options)
|
|
104
|
+
const pid = started?.pid ?? started?.program?.running?.pid ?? null
|
|
105
|
+
return { ok: true, cwd, dir: listed.dir, program: { id: program.id, logName: program.logName, logPath: program.logPath }, alreadyRunning: started?.alreadyRunning === true, pid }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Stop one program by its log pidfile (the wrapper is verified before signaling). */
|
|
109
|
+
export async function stopProgramByKey(cwd, wanted, { dir = '' } = {}) {
|
|
110
|
+
const listed = await listPrograms(cwd, { dir })
|
|
111
|
+
const { program, error } = findProgramOrError(listed.programs, wanted)
|
|
112
|
+
if (program === null) return error
|
|
113
|
+
try {
|
|
114
|
+
const outcome = await stopLogPath(program.logPath)
|
|
115
|
+
return { ok: true, cwd, dir: listed.dir, stopped: outcome.stopped === true, pid: outcome.pid ?? null, program: { id: program.id, logName: program.logName, logPath: program.logPath } }
|
|
116
|
+
} catch (error) {
|
|
117
|
+
const status = typeof error?.status === 'number' ? error.status : 500
|
|
118
|
+
return { ok: false, code: error?.code ?? 'stop-failed', message: error?.message ?? String(error), status }
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Restart = stop (if running) then start, returning both halves. */
|
|
123
|
+
export async function restartProgramByKey(cwd, wanted, { dir = '', options = {} } = {}) {
|
|
124
|
+
const listed = await listPrograms(cwd, { dir })
|
|
125
|
+
const { program, error } = findProgramOrError(listed.programs, wanted)
|
|
126
|
+
if (program === null) return error
|
|
127
|
+
let stopped = null
|
|
128
|
+
if (program.running?.alive === true) {
|
|
129
|
+
try {
|
|
130
|
+
stopped = await stopLogPath(program.logPath)
|
|
131
|
+
} catch (error) {
|
|
132
|
+
return { ok: false, code: error?.code ?? 'stop-failed', message: error?.message ?? String(error), status: typeof error?.status === 'number' ? error.status : 500 }
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const started = await startProgramByKey(cwd, wanted, { dir, options })
|
|
136
|
+
return Object.assign({}, started, { stopped })
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Read a log window.
|
|
141
|
+
*
|
|
142
|
+
* `offset` is a byte position from a previous read. Without it the tail is
|
|
143
|
+
* returned (so the panel's first paint is useful on a large file); with it only
|
|
144
|
+
* the new bytes come back, which is what a follower needs after a reconnect.
|
|
145
|
+
* `nextOffset` is always the file size the caller should use next.
|
|
146
|
+
*/
|
|
147
|
+
export async function readProgramLog(cwd, wanted, { dir = '', offset = null, bytes = DEFAULT_LOG_BYTES } = {}) {
|
|
148
|
+
const listed = await listPrograms(cwd, { dir })
|
|
149
|
+
const { program, error } = findProgramOrError(listed.programs, wanted)
|
|
150
|
+
if (program === null) return error
|
|
151
|
+
const path = program.logPath
|
|
152
|
+
if (existsSync(path) !== true) {
|
|
153
|
+
const reset = Number.isFinite(offset) && Math.floor(offset) > 0
|
|
154
|
+
return { ok: true, path, size: 0, offset: 0, nextOffset: 0, text: '', reset, running: program.running ?? null }
|
|
155
|
+
}
|
|
156
|
+
const size = statSync(path).size
|
|
157
|
+
const limit = Math.max(1024, Math.min(MAX_LOG_BYTES, Number.isFinite(bytes) ? Math.floor(bytes) : DEFAULT_LOG_BYTES))
|
|
158
|
+
let start
|
|
159
|
+
if (Number.isFinite(offset) && offset >= 0 && offset <= size) start = Math.floor(offset)
|
|
160
|
+
else start = Math.max(0, size - limit)
|
|
161
|
+
if (size - start > limit) start = size - limit
|
|
162
|
+
const length = size - start
|
|
163
|
+
// `offset > size` means the caller's byte position is gone (truncated file):
|
|
164
|
+
// the tail is returned, but the caller must replace rather than append it.
|
|
165
|
+
const truncated = Number.isFinite(offset) && Math.floor(offset) > size
|
|
166
|
+
if (length === 0) {
|
|
167
|
+
return { ok: true, path, size, offset: start, nextOffset: size, text: '', reset: truncated, running: program.running ?? null }
|
|
168
|
+
}
|
|
169
|
+
const fd = openSync(path, 'r')
|
|
170
|
+
try {
|
|
171
|
+
const buffer = Buffer.allocUnsafe(length)
|
|
172
|
+
readSync(fd, buffer, 0, length, start)
|
|
173
|
+
return { ok: true, path, size, offset: start, nextOffset: size, text: buffer.toString('utf8'), reset: truncated, running: program.running ?? null }
|
|
174
|
+
} finally {
|
|
175
|
+
closeSync(fd)
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
/** How often the follower stats the log file. */
|
|
181
|
+
export const LOG_FOLLOW_INTERVAL_MS = 400
|
|
182
|
+
/** How long a stopped program's stream keeps draining before it ends. */
|
|
183
|
+
export const LOG_FOLLOW_STOPPED_GRACE_MS = 2500
|
|
184
|
+
/** Heartbeat comment so proxies/browsers keep the stream open. */
|
|
185
|
+
export const LOG_FOLLOW_PING_MS = 15_000
|
|
186
|
+
|
|
187
|
+
/** Whether a program row is alive right now (pidfile + kill(0); never a tree walk). */
|
|
188
|
+
async function programAlive(row) {
|
|
189
|
+
const record = await readPidFile(row.logPath)
|
|
190
|
+
if (record === undefined) return false
|
|
191
|
+
// The wrapper is the pid that owns the log writes, so a stopping service keeps
|
|
192
|
+
// counting as alive while it drains its shutdown output (the panel's
|
|
193
|
+
// readService() uses the same pid || wrapperPid rule — review B-P2-4).
|
|
194
|
+
return pidAlive(record.pid) === true || pidAlive(record.wrapperPid) === true
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Follow one program's log for an SSE relay.
|
|
199
|
+
*
|
|
200
|
+
* Frames (the `event:` name equals `type`):
|
|
201
|
+
* - `log` `{path,size,offset,nextOffset,text,running}` — new bytes only
|
|
202
|
+
* - `reset` same shape but `offset` restarted at 0 (truncation/rotation)
|
|
203
|
+
* - `state` `{running}` — the service started/stopped
|
|
204
|
+
* - `end` `{reason:'stopped'|'gone'}` then the stream closes
|
|
205
|
+
*
|
|
206
|
+
* The caller owns the response; `stop()` unsubscribes immediately.
|
|
207
|
+
*/
|
|
208
|
+
export function startLogFollow(row, { offset = null, bytes = DEFAULT_LOG_BYTES, intervalMs = LOG_FOLLOW_INTERVAL_MS, stoppedGraceMs = LOG_FOLLOW_STOPPED_GRACE_MS, alive = programAlive } = {}, onFrame) {
|
|
209
|
+
const path = row.logPath
|
|
210
|
+
const limit = Math.max(1024, Math.min(MAX_LOG_BYTES, Number.isFinite(bytes) ? Math.floor(bytes) : DEFAULT_LOG_BYTES))
|
|
211
|
+
const hasOffset = Number.isFinite(offset) && offset >= 0
|
|
212
|
+
let cursor = hasOffset ? Math.floor(offset) : 0
|
|
213
|
+
let lastSize = 0
|
|
214
|
+
let lastIno = 0
|
|
215
|
+
let lastCtimeMs = 0
|
|
216
|
+
let running = row.running?.alive === true
|
|
217
|
+
let stoppedSince = running ? null : Date.now()
|
|
218
|
+
let timer = null
|
|
219
|
+
let ping = null
|
|
220
|
+
let closed = false
|
|
221
|
+
let ticking = false
|
|
222
|
+
|
|
223
|
+
/** One stat so `size`, inode and ctime all describe the same file version. */
|
|
224
|
+
const statLog = () => {
|
|
225
|
+
try {
|
|
226
|
+
const st = statSync(path)
|
|
227
|
+
return { size: st.size, ino: Number(st.ino) || 0, ctimeMs: Number(st.ctimeMs) || 0 }
|
|
228
|
+
} catch {
|
|
229
|
+
return { size: 0, ino: 0, ctimeMs: 0 }
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const readWindow = (from, st = statLog()) => {
|
|
234
|
+
const { size, ino, ctimeMs } = st
|
|
235
|
+
if (size <= 0) return { size, ino, ctimeMs, start: 0, next: 0, text: '' }
|
|
236
|
+
let start = from
|
|
237
|
+
if (!Number.isFinite(start) || start < 0 || start > size) start = Math.max(0, size - limit)
|
|
238
|
+
const length = Math.min(limit, size - start)
|
|
239
|
+
if (length <= 0) return { size, ino, ctimeMs, start, next: start, text: '' }
|
|
240
|
+
const fd = openSync(path, 'r')
|
|
241
|
+
try {
|
|
242
|
+
const buffer = Buffer.allocUnsafe(length)
|
|
243
|
+
readSync(fd, buffer, 0, length, start)
|
|
244
|
+
return { size, ino, ctimeMs, start, next: start + length, text: buffer.toString('utf8') }
|
|
245
|
+
} finally {
|
|
246
|
+
closeSync(fd)
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const emit = (frame) => {
|
|
251
|
+
if (closed) return
|
|
252
|
+
try { onFrame(frame) } catch { /* the socket owns its own errors */ }
|
|
253
|
+
if (frame.type === 'end') stop()
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const tick = async () => {
|
|
257
|
+
if (closed || ticking) return
|
|
258
|
+
ticking = true
|
|
259
|
+
try {
|
|
260
|
+
const present = (await alive(row)) === true
|
|
261
|
+
if (present !== running) {
|
|
262
|
+
running = present
|
|
263
|
+
stoppedSince = present ? null : Date.now()
|
|
264
|
+
emit({ type: 'state', running })
|
|
265
|
+
}
|
|
266
|
+
const st = statLog()
|
|
267
|
+
// Rotation is not only "the file got shorter": logrotate-style truncate +
|
|
268
|
+
// copy can leave the same length, and replacing the file changes the inode
|
|
269
|
+
// (review B-P2-3).
|
|
270
|
+
const replaced = lastIno !== 0 && st.ino !== 0 && st.ino !== lastIno
|
|
271
|
+
const sameSizeRewrite = st.size === lastSize && st.size > 0 && lastCtimeMs !== 0 && st.ctimeMs !== lastCtimeMs
|
|
272
|
+
if (st.size < lastSize || st.size < cursor || replaced || sameSizeRewrite) {
|
|
273
|
+
// Rotated, truncated or replaced: the byte we were waiting for is gone.
|
|
274
|
+
const window = readWindow(0, st)
|
|
275
|
+
cursor = window.next
|
|
276
|
+
lastSize = window.size
|
|
277
|
+
lastIno = window.ino
|
|
278
|
+
lastCtimeMs = window.ctimeMs
|
|
279
|
+
emit({ type: 'reset', path, size: window.size, offset: window.start, nextOffset: window.next, text: window.text, running })
|
|
280
|
+
return
|
|
281
|
+
}
|
|
282
|
+
if (st.size > cursor) {
|
|
283
|
+
const window = readWindow(cursor, st)
|
|
284
|
+
cursor = window.next
|
|
285
|
+
lastSize = window.size
|
|
286
|
+
lastIno = window.ino
|
|
287
|
+
lastCtimeMs = window.ctimeMs
|
|
288
|
+
emit({ type: 'log', path, size: window.size, offset: window.start, nextOffset: window.next, text: window.text, running })
|
|
289
|
+
if (present === false) stoppedSince = Date.now()
|
|
290
|
+
return
|
|
291
|
+
}
|
|
292
|
+
lastSize = st.size
|
|
293
|
+
if (st.ino !== 0) lastIno = st.ino
|
|
294
|
+
lastCtimeMs = st.ctimeMs
|
|
295
|
+
if (present === false && stoppedSince !== null && Date.now() - stoppedSince >= stoppedGraceMs) {
|
|
296
|
+
emit({ type: 'end', reason: 'stopped' })
|
|
297
|
+
}
|
|
298
|
+
} finally {
|
|
299
|
+
ticking = false
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// A chained timer instead of setInterval: a slow `alive`/stat call (WSL /mnt/c)
|
|
304
|
+
// must never overlap the next tick and reorder `state`/`stoppedSince`
|
|
305
|
+
// (review B-P2-2).
|
|
306
|
+
const schedule = () => {
|
|
307
|
+
if (closed) return
|
|
308
|
+
timer = setTimeout(() => {
|
|
309
|
+
tick().catch(() => {}).finally(() => { if (!closed) schedule() })
|
|
310
|
+
}, intervalMs)
|
|
311
|
+
timer.unref?.()
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const initialStat = statLog()
|
|
315
|
+
// An offset past EOF is a truncated/replaced file: start over from the tail and
|
|
316
|
+
// say `reset`, otherwise an appending client duplicates the tail (B-P2-1).
|
|
317
|
+
const truncated = hasOffset && cursor > initialStat.size
|
|
318
|
+
if (truncated) cursor = Math.max(0, initialStat.size - limit)
|
|
319
|
+
let first
|
|
320
|
+
try {
|
|
321
|
+
first = readWindow(hasOffset ? cursor : Math.max(0, initialStat.size - limit), initialStat)
|
|
322
|
+
} catch {
|
|
323
|
+
first = { size: 0, ino: 0, ctimeMs: 0, start: 0, next: 0, text: '' }
|
|
324
|
+
}
|
|
325
|
+
cursor = first.next
|
|
326
|
+
lastSize = first.size
|
|
327
|
+
lastIno = first.ino
|
|
328
|
+
lastCtimeMs = first.ctimeMs
|
|
329
|
+
if (truncated) {
|
|
330
|
+
emit({ type: 'reset', path, size: first.size, offset: first.start, nextOffset: first.next, text: first.text, running })
|
|
331
|
+
} else if (first.text !== '') {
|
|
332
|
+
emit({ type: 'log', path, size: first.size, offset: first.start, nextOffset: first.next, text: first.text, running })
|
|
333
|
+
}
|
|
334
|
+
schedule()
|
|
335
|
+
ping = setInterval(() => { if (!closed) try { onFrame({ type: 'ping' }) } catch { /* ignore */ } }, LOG_FOLLOW_PING_MS)
|
|
336
|
+
ping.unref?.()
|
|
337
|
+
|
|
338
|
+
function stop() {
|
|
339
|
+
if (closed) return
|
|
340
|
+
closed = true
|
|
341
|
+
if (timer !== null) clearTimeout(timer)
|
|
342
|
+
if (ping !== null) clearInterval(ping)
|
|
343
|
+
timer = null
|
|
344
|
+
ping = null
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return { stop, get offset() { return cursor } }
|
|
348
|
+
}
|