typescript-virtual-container 1.4.6 → 1.4.8
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/.vscode/settings.json +1 -0
- package/README.md +0 -1
- package/benchmark-results.txt +21 -21
- package/builds/self-standalone.js +1111 -299
- package/builds/self-standalone.js.map +4 -4
- package/builds/standalone-wo-sftp.js +993 -183
- package/builds/standalone-wo-sftp.js.map +4 -4
- package/builds/standalone.cjs +984 -173
- package/builds/standalone.cjs.map +4 -4
- package/dist/SSHMimic/exec.d.ts.map +1 -1
- package/dist/SSHMimic/exec.js +0 -1
- package/dist/SSHMimic/exec.js.map +1 -1
- package/dist/SSHMimic/index.d.ts.map +1 -1
- package/dist/SSHMimic/index.js +3 -1
- package/dist/SSHMimic/index.js.map +1 -1
- package/dist/SSHMimic/sftp.d.ts.map +1 -1
- package/dist/SSHMimic/sftp.js +0 -6
- package/dist/SSHMimic/sftp.js.map +1 -1
- package/dist/VirtualFileSystem/binaryPack.d.ts.map +1 -1
- package/dist/VirtualFileSystem/binaryPack.js +21 -9
- package/dist/VirtualFileSystem/binaryPack.js.map +1 -1
- package/dist/VirtualFileSystem/index.d.ts +93 -0
- package/dist/VirtualFileSystem/index.d.ts.map +1 -1
- package/dist/VirtualFileSystem/index.js +361 -46
- package/dist/VirtualFileSystem/index.js.map +1 -1
- package/dist/VirtualFileSystem/internalTypes.d.ts +23 -4
- package/dist/VirtualFileSystem/internalTypes.d.ts.map +1 -1
- package/dist/VirtualFileSystem/journal.d.ts +47 -0
- package/dist/VirtualFileSystem/journal.d.ts.map +1 -0
- package/dist/VirtualFileSystem/journal.js +178 -0
- package/dist/VirtualFileSystem/journal.js.map +1 -0
- package/dist/VirtualFileSystem/path.js +1 -1
- package/dist/VirtualFileSystem/path.js.map +1 -1
- package/dist/VirtualShell/idleManager.d.ts +65 -0
- package/dist/VirtualShell/idleManager.d.ts.map +1 -0
- package/dist/VirtualShell/idleManager.js +106 -0
- package/dist/VirtualShell/idleManager.js.map +1 -0
- package/dist/VirtualShell/index.d.ts +28 -0
- package/dist/VirtualShell/index.d.ts.map +1 -1
- package/dist/VirtualShell/index.js +48 -0
- package/dist/VirtualShell/index.js.map +1 -1
- package/dist/VirtualShell/shell.js +4 -4
- package/dist/VirtualShell/shell.js.map +1 -1
- package/dist/commands/man.d.ts.map +1 -1
- package/dist/commands/man.js +5 -27
- package/dist/commands/man.js.map +1 -1
- package/dist/commands/manuals-bundle.d.ts +11 -0
- package/dist/commands/manuals-bundle.d.ts.map +1 -0
- package/dist/commands/manuals-bundle.js +898 -0
- package/dist/commands/manuals-bundle.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/modules/linuxRootfs.d.ts +8 -1
- package/dist/modules/linuxRootfs.d.ts.map +1 -1
- package/dist/modules/linuxRootfs.js +47 -14
- package/dist/modules/linuxRootfs.js.map +1 -1
- package/dist/self-standalone.js +16 -1
- package/dist/self-standalone.js.map +1 -1
- package/dist/standalone.js +22 -0
- package/dist/standalone.js.map +1 -1
- package/docs/assets/hierarchy.js +1 -1
- package/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/classes/HoneyPot.html +8 -8
- package/docs/classes/IdleManager.html +159 -0
- package/docs/classes/SshClient.html +18 -18
- package/docs/classes/VirtualFileSystem.html +57 -32
- package/docs/classes/VirtualPackageManager.html +12 -12
- package/docs/classes/VirtualSftpServer.html +3 -3
- package/docs/classes/VirtualShell.html +40 -25
- package/docs/classes/VirtualSshServer.html +5 -5
- package/docs/classes/VirtualUserManager.html +26 -26
- package/docs/functions/assertDiff.html +1 -1
- package/docs/functions/diffSnapshots.html +1 -1
- package/docs/functions/formatDiff.html +1 -1
- package/docs/functions/getArg.html +1 -1
- package/docs/functions/getFlag.html +1 -1
- package/docs/functions/ifFlag.html +1 -1
- package/docs/hierarchy.html +1 -1
- package/docs/index.html +1 -2
- package/docs/interfaces/AuditLogEntry.html +2 -2
- package/docs/interfaces/CommandContext.html +11 -11
- package/docs/interfaces/CommandResult.html +12 -12
- package/docs/interfaces/ExecStream.html +5 -5
- package/docs/interfaces/HoneyPotStats.html +2 -2
- package/docs/interfaces/IdleManagerOptions.html +7 -0
- package/docs/interfaces/InstalledPackage.html +10 -10
- package/docs/interfaces/NanoEditorSession.html +4 -4
- package/docs/interfaces/PackageDefinition.html +13 -13
- package/docs/interfaces/PackageFile.html +4 -4
- package/docs/interfaces/RemoveOptions.html +2 -2
- package/docs/interfaces/ShellEnv.html +3 -3
- package/docs/interfaces/ShellModule.html +7 -7
- package/docs/interfaces/ShellProperties.html +4 -4
- package/docs/interfaces/ShellStream.html +6 -6
- package/docs/interfaces/SudoChallenge.html +8 -8
- package/docs/interfaces/VfsBaseNode.html +6 -6
- package/docs/interfaces/VfsDiff.html +5 -5
- package/docs/interfaces/VfsDiffEntry.html +3 -3
- package/docs/interfaces/VfsDiffModified.html +5 -5
- package/docs/interfaces/VfsDirectoryNode.html +7 -7
- package/docs/interfaces/VfsFileNode.html +8 -8
- package/docs/interfaces/VfsOptions.html +18 -4
- package/docs/interfaces/VfsSnapshot.html +2 -2
- package/docs/interfaces/VfsSnapshotBaseNode.html +3 -3
- package/docs/interfaces/VfsSnapshotDirectoryNode.html +4 -4
- package/docs/interfaces/VfsSnapshotFileNode.html +5 -5
- package/docs/interfaces/WriteFileOptions.html +3 -3
- package/docs/modules.html +1 -1
- package/docs/types/CommandMode.html +1 -1
- package/docs/types/CommandOutcome.html +1 -1
- package/docs/types/IdleState.html +1 -0
- package/docs/types/VfsNodeStats.html +1 -1
- package/docs/types/VfsNodeType.html +1 -1
- package/docs/types/VfsPersistenceMode.html +1 -1
- package/docs/types/VfsSnapshotNode.html +1 -1
- package/package.json +5 -4
- package/scripts/generate-manuals-bundle.mjs +49 -0
- package/src/SSHMimic/exec.ts +0 -1
- package/src/SSHMimic/index.ts +3 -1
- package/src/SSHMimic/sftp.ts +0 -6
- package/src/VirtualFileSystem/binaryPack.ts +21 -9
- package/src/VirtualFileSystem/index.ts +369 -52
- package/src/VirtualFileSystem/internalTypes.ts +24 -4
- package/src/VirtualFileSystem/journal.ts +163 -0
- package/src/VirtualFileSystem/path.ts +1 -1
- package/src/VirtualShell/idleManager.ts +133 -0
- package/src/VirtualShell/index.ts +48 -0
- package/src/VirtualShell/shell.ts +4 -4
- package/src/commands/man.ts +5 -35
- package/src/commands/manuals-bundle.ts +898 -0
- package/src/index.ts +2 -0
- package/src/modules/linuxRootfs.ts +58 -14
- package/src/self-standalone.ts +14 -1
- package/src/standalone.ts +23 -0
- package/builds/standalone.js +0 -491
- package/builds/standalone.js.map +0 -7
|
@@ -1,19 +1,39 @@
|
|
|
1
|
-
export type InternalNode = InternalFileNode | InternalDirectoryNode;
|
|
1
|
+
export type InternalNode = InternalFileNode | InternalStubNode | InternalDirectoryNode;
|
|
2
2
|
|
|
3
3
|
interface InternalBaseNode {
|
|
4
4
|
name: string;
|
|
5
5
|
mode: number;
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
/** Unix timestamp in ms — avoids Date object overhead (~80 bytes each). */
|
|
7
|
+
createdAt: number;
|
|
8
|
+
/** Unix timestamp in ms. */
|
|
9
|
+
updatedAt: number;
|
|
8
10
|
}
|
|
9
11
|
|
|
10
12
|
export interface InternalFileNode extends InternalBaseNode {
|
|
11
13
|
type: "file";
|
|
12
14
|
content: Buffer;
|
|
13
15
|
compressed: boolean;
|
|
16
|
+
/** When true, content has been purged from RAM. Reloaded from snapshot on demand. */
|
|
17
|
+
evicted?: true;
|
|
18
|
+
/** Byte length of the original (uncompressed) content — preserved when evicted. */
|
|
19
|
+
size?: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Lazy stub — stores static rootfs file content as a plain string.
|
|
24
|
+
* No Buffer allocation until the file is actually read or written.
|
|
25
|
+
* On first write, promoted to a real InternalFileNode.
|
|
26
|
+
*/
|
|
27
|
+
export interface InternalStubNode extends InternalBaseNode {
|
|
28
|
+
type: "stub";
|
|
29
|
+
/** Raw UTF-8 content — never compressed, never evicted. */
|
|
30
|
+
stubContent: string;
|
|
14
31
|
}
|
|
15
32
|
|
|
16
33
|
export interface InternalDirectoryNode extends InternalBaseNode {
|
|
17
34
|
type: "directory";
|
|
18
|
-
|
|
35
|
+
/** Null-prototype object — avoids Map overhead (~40% less RAM per entry). */
|
|
36
|
+
children: Record<string, InternalNode>;
|
|
37
|
+
/** Cached child count — avoids O(n) Object.keys() on hot paths. */
|
|
38
|
+
_childCount: number;
|
|
19
39
|
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* journal.ts — Write-Ahead Log for VirtualFileSystem "fs" mode.
|
|
3
|
+
*
|
|
4
|
+
* Each mutating VFS operation is appended to `vfs-journal.bin` immediately.
|
|
5
|
+
* On `restoreMirror()` the base snapshot is loaded first, then the journal
|
|
6
|
+
* is replayed on top. On `flushMirror()` a new checkpoint is written and
|
|
7
|
+
* the journal is truncated.
|
|
8
|
+
*
|
|
9
|
+
* Entry format (binary, little-endian):
|
|
10
|
+
* [1B op] [2B path_len] [path bytes] [payload per op]
|
|
11
|
+
*
|
|
12
|
+
* Op codes:
|
|
13
|
+
* 0x01 WRITE — [4B content_len] [content bytes] [4B mode]
|
|
14
|
+
* 0x02 MKDIR — [4B mode]
|
|
15
|
+
* 0x03 REMOVE — (no payload)
|
|
16
|
+
* 0x04 CHMOD — [4B mode]
|
|
17
|
+
* 0x05 MOVE — [2B dest_len] [dest bytes]
|
|
18
|
+
* 0x06 SYMLINK — [2B target_len] [target bytes]
|
|
19
|
+
*/
|
|
20
|
+
/** biome-ignore-all lint/style/useNamingConvention: JournalOp modes */
|
|
21
|
+
|
|
22
|
+
import * as fsSync from "node:fs";
|
|
23
|
+
|
|
24
|
+
export const JournalOp = {
|
|
25
|
+
WRITE: 0x01,
|
|
26
|
+
MKDIR: 0x02,
|
|
27
|
+
REMOVE: 0x03,
|
|
28
|
+
CHMOD: 0x04,
|
|
29
|
+
MOVE: 0x05,
|
|
30
|
+
SYMLINK: 0x06,
|
|
31
|
+
} as const;
|
|
32
|
+
|
|
33
|
+
export type JournalOp = typeof JournalOp[keyof typeof JournalOp];
|
|
34
|
+
|
|
35
|
+
export interface JournalEntry {
|
|
36
|
+
op: JournalOp;
|
|
37
|
+
path: string;
|
|
38
|
+
content?: Buffer;
|
|
39
|
+
mode?: number;
|
|
40
|
+
dest?: string; // MOVE destination, SYMLINK target
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const ENC = "utf8" as const;
|
|
44
|
+
|
|
45
|
+
function writeString2(buf: Buffer, offset: number, s: string): number {
|
|
46
|
+
const b = Buffer.from(s, ENC);
|
|
47
|
+
buf.writeUInt16LE(b.length, offset);
|
|
48
|
+
b.copy(buf, offset + 2);
|
|
49
|
+
return 2 + b.length;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Serialise one entry to a Buffer. */
|
|
53
|
+
export function encodeEntry(e: JournalEntry): Buffer {
|
|
54
|
+
const pathBuf = Buffer.from(e.path, ENC);
|
|
55
|
+
let payloadLen = 0;
|
|
56
|
+
|
|
57
|
+
if (e.op === JournalOp.WRITE) {
|
|
58
|
+
payloadLen = 4 + (e.content?.length ?? 0) + 4;
|
|
59
|
+
} else if (e.op === JournalOp.MKDIR) {
|
|
60
|
+
payloadLen = 4;
|
|
61
|
+
} else if (e.op === JournalOp.REMOVE) {
|
|
62
|
+
payloadLen = 0;
|
|
63
|
+
} else if (e.op === JournalOp.CHMOD) {
|
|
64
|
+
payloadLen = 4;
|
|
65
|
+
} else if (e.op === JournalOp.MOVE || e.op === JournalOp.SYMLINK) {
|
|
66
|
+
payloadLen = 2 + Buffer.byteLength(e.dest ?? "", ENC);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const total = 1 + 2 + pathBuf.length + payloadLen;
|
|
70
|
+
const buf = Buffer.allocUnsafe(total);
|
|
71
|
+
let off = 0;
|
|
72
|
+
|
|
73
|
+
buf.writeUInt8(e.op, off++);
|
|
74
|
+
buf.writeUInt16LE(pathBuf.length, off); off += 2;
|
|
75
|
+
pathBuf.copy(buf, off); off += pathBuf.length;
|
|
76
|
+
|
|
77
|
+
if (e.op === JournalOp.WRITE) {
|
|
78
|
+
const c = e.content ?? Buffer.alloc(0);
|
|
79
|
+
buf.writeUInt32LE(c.length, off); off += 4;
|
|
80
|
+
c.copy(buf, off); off += c.length;
|
|
81
|
+
buf.writeUInt32LE(e.mode ?? 0o644, off); off += 4;
|
|
82
|
+
} else if (e.op === JournalOp.MKDIR) {
|
|
83
|
+
buf.writeUInt32LE(e.mode ?? 0o755, off); off += 4;
|
|
84
|
+
} else if (e.op === JournalOp.CHMOD) {
|
|
85
|
+
buf.writeUInt32LE(e.mode ?? 0o644, off); off += 4;
|
|
86
|
+
} else if (e.op === JournalOp.MOVE || e.op === JournalOp.SYMLINK) {
|
|
87
|
+
off += writeString2(buf, off, e.dest ?? "");
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return buf;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Parse all entries from a journal Buffer. Returns empty array on corrupt data. */
|
|
94
|
+
export function decodeJournal(buf: Buffer): JournalEntry[] {
|
|
95
|
+
const entries: JournalEntry[] = [];
|
|
96
|
+
let off = 0;
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
while (off < buf.length) {
|
|
100
|
+
if (off + 3 > buf.length) break;
|
|
101
|
+
const op = buf.readUInt8(off++) as JournalOp;
|
|
102
|
+
const pathLen = buf.readUInt16LE(off); off += 2;
|
|
103
|
+
if (off + pathLen > buf.length) break;
|
|
104
|
+
const path = buf.subarray(off, off + pathLen).toString(ENC); off += pathLen;
|
|
105
|
+
|
|
106
|
+
if (op === JournalOp.WRITE) {
|
|
107
|
+
if (off + 4 > buf.length) break;
|
|
108
|
+
const cLen = buf.readUInt32LE(off); off += 4;
|
|
109
|
+
if (off + cLen + 4 > buf.length) break;
|
|
110
|
+
const content = Buffer.from(buf.subarray(off, off + cLen)); off += cLen;
|
|
111
|
+
const mode = buf.readUInt32LE(off); off += 4;
|
|
112
|
+
entries.push({ op, path, content, mode });
|
|
113
|
+
} else if (op === JournalOp.MKDIR) {
|
|
114
|
+
if (off + 4 > buf.length) break;
|
|
115
|
+
const mode = buf.readUInt32LE(off); off += 4;
|
|
116
|
+
entries.push({ op, path, mode });
|
|
117
|
+
} else if (op === JournalOp.REMOVE) {
|
|
118
|
+
entries.push({ op, path });
|
|
119
|
+
} else if (op === JournalOp.CHMOD) {
|
|
120
|
+
if (off + 4 > buf.length) break;
|
|
121
|
+
const mode = buf.readUInt32LE(off); off += 4;
|
|
122
|
+
entries.push({ op, path, mode });
|
|
123
|
+
} else if (op === JournalOp.MOVE || op === JournalOp.SYMLINK) {
|
|
124
|
+
if (off + 2 > buf.length) break;
|
|
125
|
+
const dLen = buf.readUInt16LE(off); off += 2;
|
|
126
|
+
if (off + dLen > buf.length) break;
|
|
127
|
+
const dest = buf.subarray(off, off + dLen).toString(ENC); off += dLen;
|
|
128
|
+
entries.push({ op, path, dest });
|
|
129
|
+
} else {
|
|
130
|
+
// Unknown op — skip rest of buffer to avoid corrupt replay
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
} catch {
|
|
135
|
+
// Truncated or corrupt entry — return what we have
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return entries;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Append a single entry to the journal file (O_APPEND, atomic write). */
|
|
142
|
+
export function appendJournalEntry(journalPath: string, entry: JournalEntry): void {
|
|
143
|
+
const buf = encodeEntry(entry);
|
|
144
|
+
const fd = fsSync.openSync(journalPath, fsSync.constants.O_WRONLY | fsSync.constants.O_CREAT | fsSync.constants.O_APPEND);
|
|
145
|
+
try {
|
|
146
|
+
fsSync.writeSync(fd, buf);
|
|
147
|
+
} finally {
|
|
148
|
+
fsSync.closeSync(fd);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Read and decode all entries from a journal file. Returns [] if file is absent/empty. */
|
|
153
|
+
export function readJournal(journalPath: string): JournalEntry[] {
|
|
154
|
+
if (!fsSync.existsSync(journalPath)) return [];
|
|
155
|
+
const buf = fsSync.readFileSync(journalPath);
|
|
156
|
+
if (buf.length === 0) return [];
|
|
157
|
+
return decodeJournal(buf);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Delete the journal file (after a successful checkpoint). */
|
|
161
|
+
export function truncateJournal(journalPath: string): void {
|
|
162
|
+
if (fsSync.existsSync(journalPath)) fsSync.unlinkSync(journalPath);
|
|
163
|
+
}
|
|
@@ -33,7 +33,7 @@ export function getNode(
|
|
|
33
33
|
throw new Error(`Path '${normalized}' does not exist.`);
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
const next = current.children
|
|
36
|
+
const next: InternalNode | undefined = current.children[part];
|
|
37
37
|
if (!next) {
|
|
38
38
|
throw new Error(`Path '${normalized}' does not exist.`);
|
|
39
39
|
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* IdleManager — cold-start / freeze-thaw system for VirtualShell.
|
|
3
|
+
*
|
|
4
|
+
* When a shell has had no activity for `idleThresholdMs`, the VFS tree is
|
|
5
|
+
* serialised to a compact binary buffer and the in-memory tree is released.
|
|
6
|
+
* On the next command the tree is reconstructed in ~0.1 ms.
|
|
7
|
+
*
|
|
8
|
+
* Memory freed per idle shell: the full JS object graph (~250+ InternalNodes
|
|
9
|
+
* for a typical rootfs + user files). The frozen buffer itself stays in RAM
|
|
10
|
+
* but is ~27% smaller than the live tree and GC-friendly (one flat Buffer).
|
|
11
|
+
*
|
|
12
|
+
* CPU freed: the VFS auto-flush setInterval is suspended while frozen.
|
|
13
|
+
*
|
|
14
|
+
* Usage:
|
|
15
|
+
* ```ts
|
|
16
|
+
* const idle = new IdleManager(shell, { idleThresholdMs: 60_000 });
|
|
17
|
+
* idle.start();
|
|
18
|
+
* // call idle.ping() on every shell activity (exec, keypress, …)
|
|
19
|
+
* // call idle.stop() on shell destroy
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { EventEmitter } from "node:events";
|
|
24
|
+
import type VirtualFileSystem from "../VirtualFileSystem";
|
|
25
|
+
import { decodeVfs } from "../VirtualFileSystem/binaryPack";
|
|
26
|
+
|
|
27
|
+
export interface IdleManagerOptions {
|
|
28
|
+
/**
|
|
29
|
+
* Milliseconds of inactivity before the shell is frozen.
|
|
30
|
+
* Default: 60_000 (1 minute).
|
|
31
|
+
*/
|
|
32
|
+
idleThresholdMs?: number;
|
|
33
|
+
/**
|
|
34
|
+
* How often the idle check runs.
|
|
35
|
+
* Default: 15_000 (15 seconds).
|
|
36
|
+
*/
|
|
37
|
+
checkIntervalMs?: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type IdleState = "active" | "frozen";
|
|
41
|
+
|
|
42
|
+
export class IdleManager extends EventEmitter {
|
|
43
|
+
private readonly vfs: VirtualFileSystem;
|
|
44
|
+
private readonly idleThresholdMs: number;
|
|
45
|
+
private readonly checkIntervalMs: number;
|
|
46
|
+
|
|
47
|
+
private _state: IdleState = "active";
|
|
48
|
+
private _lastActivity = Date.now();
|
|
49
|
+
private _frozenBuffer: Buffer | null = null;
|
|
50
|
+
private _checkTimer: ReturnType<typeof setInterval> | null = null;
|
|
51
|
+
|
|
52
|
+
/** Emitted when the shell is frozen (VFS tree released). */
|
|
53
|
+
declare on: ((event: "freeze", listener: () => void) => this) &
|
|
54
|
+
((event: "thaw", listener: () => void) => this) &
|
|
55
|
+
((event: string, listener: (...args: unknown[]) => void) => this);
|
|
56
|
+
|
|
57
|
+
constructor(vfs: VirtualFileSystem, options: IdleManagerOptions = {}) {
|
|
58
|
+
super();
|
|
59
|
+
this.vfs = vfs;
|
|
60
|
+
this.idleThresholdMs = options.idleThresholdMs ?? 60_000;
|
|
61
|
+
this.checkIntervalMs = options.checkIntervalMs ?? 15_000;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Start monitoring for idle. Call once after shell initialisation. */
|
|
65
|
+
public start(): void {
|
|
66
|
+
if (this._checkTimer) return;
|
|
67
|
+
this._lastActivity = Date.now();
|
|
68
|
+
this._checkTimer = setInterval(() => this._check(), this.checkIntervalMs);
|
|
69
|
+
// Don't block process exit
|
|
70
|
+
if (typeof this._checkTimer === "object" && this._checkTimer !== null && "unref" in this._checkTimer) {
|
|
71
|
+
(this._checkTimer as NodeJS.Timeout).unref();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Stop monitoring and thaw if frozen. Call on shell destroy. */
|
|
76
|
+
public async stop(): Promise<void> {
|
|
77
|
+
if (this._checkTimer) {
|
|
78
|
+
clearInterval(this._checkTimer);
|
|
79
|
+
this._checkTimer = null;
|
|
80
|
+
}
|
|
81
|
+
if (this._state === "frozen") await this._thaw();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Signal activity — resets the idle clock and thaws if frozen.
|
|
86
|
+
* Call this before every exec / keypress / session event.
|
|
87
|
+
*/
|
|
88
|
+
public async ping(): Promise<void> {
|
|
89
|
+
this._lastActivity = Date.now();
|
|
90
|
+
if (this._state === "frozen") await this._thaw();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Current idle state. */
|
|
94
|
+
public get state(): IdleState {
|
|
95
|
+
return this._state;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Ms since last activity. */
|
|
99
|
+
public get idleMs(): number {
|
|
100
|
+
return Date.now() - this._lastActivity;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// ── Internal ──────────────────────────────────────────────────────────────
|
|
104
|
+
|
|
105
|
+
private _check(): void {
|
|
106
|
+
if (this._state === "frozen") return; // already frozen
|
|
107
|
+
if (Date.now() - this._lastActivity >= this.idleThresholdMs) {
|
|
108
|
+
void this._freeze();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private async _freeze(): Promise<void> {
|
|
113
|
+
if (this._state === "frozen") return;
|
|
114
|
+
// Flush any pending writes before freezing
|
|
115
|
+
await this.vfs.stopAutoFlush();
|
|
116
|
+
// Serialise the live tree to a compact binary buffer
|
|
117
|
+
this._frozenBuffer = this.vfs.encodeBinary();
|
|
118
|
+
// Release the live tree — GC can now collect all InternalNode objects
|
|
119
|
+
this.vfs.releaseTree();
|
|
120
|
+
this._state = "frozen";
|
|
121
|
+
this.emit("freeze");
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private async _thaw(): Promise<void> {
|
|
125
|
+
if (this._state !== "frozen" || !this._frozenBuffer) return;
|
|
126
|
+
// Reconstruct the tree from the frozen buffer (~0.07 ms for typical rootfs)
|
|
127
|
+
const root = decodeVfs(this._frozenBuffer);
|
|
128
|
+
this.vfs.importRootTree(root);
|
|
129
|
+
this._frozenBuffer = null;
|
|
130
|
+
this._state = "active";
|
|
131
|
+
this.emit("thaw");
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -14,6 +14,7 @@ import { createPerfLogger } from "../utils/perfLogger";
|
|
|
14
14
|
import VirtualFileSystem, { type VfsOptions } from "../VirtualFileSystem";
|
|
15
15
|
import { VirtualPackageManager } from "../VirtualPackageManager";
|
|
16
16
|
import { VirtualUserManager } from "../VirtualUserManager";
|
|
17
|
+
import { IdleManager, type IdleManagerOptions } from "./idleManager";
|
|
17
18
|
import { startShell } from "./shell";
|
|
18
19
|
|
|
19
20
|
/**
|
|
@@ -143,6 +144,8 @@ class VirtualShell extends EventEmitter {
|
|
|
143
144
|
properties: ShellProperties;
|
|
144
145
|
/** Unix ms timestamp of shell creation — used by `uptime` and `/proc/uptime`. */
|
|
145
146
|
startTime: number;
|
|
147
|
+
/** Idle / cold-start manager — null until `enableIdleManagement()` is called. */
|
|
148
|
+
private _idle: IdleManager | null = null;
|
|
146
149
|
private initialized: Promise<void>;
|
|
147
150
|
|
|
148
151
|
/**
|
|
@@ -236,6 +239,7 @@ class VirtualShell extends EventEmitter {
|
|
|
236
239
|
*/
|
|
237
240
|
executeCommand(rawInput: string, authUser: string, cwd: string): void {
|
|
238
241
|
perf.mark("executeCommand");
|
|
242
|
+
if (this._idle) void this._idle.ping();
|
|
239
243
|
runCommand(rawInput, authUser, this.hostname, "shell", cwd, this);
|
|
240
244
|
this.emit("command", { command: rawInput, user: authUser, cwd });
|
|
241
245
|
}
|
|
@@ -262,6 +266,7 @@ class VirtualShell extends EventEmitter {
|
|
|
262
266
|
terminalSize: { cols: number; rows: number },
|
|
263
267
|
): void {
|
|
264
268
|
perf.mark("startInteractiveSession");
|
|
269
|
+
if (this._idle) void this._idle.ping();
|
|
265
270
|
// Interactive shell logic
|
|
266
271
|
this.emit("session:start", { user: authUser, sessionId, remoteAddress });
|
|
267
272
|
startShell(
|
|
@@ -404,6 +409,49 @@ class VirtualShell extends EventEmitter {
|
|
|
404
409
|
this.users.assertWriteWithinQuota(authUser, targetPath, content);
|
|
405
410
|
this.vfs.writeFile(targetPath, content);
|
|
406
411
|
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Enable idle detection and cold-start freeze/thaw for this shell.
|
|
415
|
+
*
|
|
416
|
+
* After `idleThresholdMs` of inactivity the VFS tree is serialised and
|
|
417
|
+
* released from RAM. The next command transparently restores it in ~0.1 ms.
|
|
418
|
+
*
|
|
419
|
+
* @example
|
|
420
|
+
* ```ts
|
|
421
|
+
* await shell.ensureInitialized();
|
|
422
|
+
* shell.enableIdleManagement({ idleThresholdMs: 60_000 });
|
|
423
|
+
* ```
|
|
424
|
+
*/
|
|
425
|
+
public enableIdleManagement(options?: IdleManagerOptions): void {
|
|
426
|
+
if (this._idle) return; // already enabled
|
|
427
|
+
this._idle = new IdleManager(this.vfs, options);
|
|
428
|
+
this._idle.on("freeze", () => this.emit("shell:freeze"));
|
|
429
|
+
this._idle.on("thaw", () => this.emit("shell:thaw"));
|
|
430
|
+
this._idle.start();
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Disable idle management and thaw the shell if currently frozen.
|
|
435
|
+
* Safe to call even if idle management was never enabled.
|
|
436
|
+
*/
|
|
437
|
+
public async disableIdleManagement(): Promise<void> {
|
|
438
|
+
if (!this._idle) return;
|
|
439
|
+
await this._idle.stop();
|
|
440
|
+
this._idle = null;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Current idle state — `"active"` or `"frozen"`.
|
|
445
|
+
* Returns `"active"` when idle management is disabled.
|
|
446
|
+
*/
|
|
447
|
+
public get idleState(): "active" | "frozen" {
|
|
448
|
+
return this._idle?.state ?? "active";
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/** Milliseconds since last shell activity. 0 when idle management is disabled. */
|
|
452
|
+
public get idleMs(): number {
|
|
453
|
+
return this._idle?.idleMs ?? 0;
|
|
454
|
+
}
|
|
407
455
|
}
|
|
408
456
|
|
|
409
457
|
export { VirtualShell };
|
|
@@ -202,7 +202,7 @@ export function startShell(
|
|
|
202
202
|
cwd = result.nextCwd;
|
|
203
203
|
}
|
|
204
204
|
|
|
205
|
-
|
|
205
|
+
// WAL: checkpoint handled by auto-flush timer
|
|
206
206
|
renderLine();
|
|
207
207
|
}
|
|
208
208
|
|
|
@@ -221,7 +221,7 @@ export function startShell(
|
|
|
221
221
|
activeSession.targetPath,
|
|
222
222
|
updatedContent,
|
|
223
223
|
);
|
|
224
|
-
|
|
224
|
+
// WAL: checkpoint handled by auto-flush timer
|
|
225
225
|
} catch {
|
|
226
226
|
// If temp file does not exist, nano exited without writing.
|
|
227
227
|
}
|
|
@@ -498,7 +498,7 @@ export function startShell(
|
|
|
498
498
|
historyDraft = "";
|
|
499
499
|
stream.write("bye\r\n");
|
|
500
500
|
pushHistory("bye");
|
|
501
|
-
|
|
501
|
+
// WAL: checkpoint handled by auto-flush timer
|
|
502
502
|
stream.write("logout\r\n");
|
|
503
503
|
stream.exit(0);
|
|
504
504
|
stream.end();
|
|
@@ -657,7 +657,7 @@ export function startShell(
|
|
|
657
657
|
cursorPos = 0;
|
|
658
658
|
}
|
|
659
659
|
|
|
660
|
-
|
|
660
|
+
// WAL: checkpoint handled by auto-flush timer
|
|
661
661
|
}
|
|
662
662
|
|
|
663
663
|
renderLine();
|
package/src/commands/man.ts
CHANGED
|
@@ -1,43 +1,10 @@
|
|
|
1
1
|
import type { ShellModule } from "../types/commands";
|
|
2
|
+
import { MANUALS } from "./manuals-bundle";
|
|
2
3
|
|
|
3
4
|
const MANUAL_ALIASES: Record<string, string> = {
|
|
4
5
|
gunzip: "gzip",
|
|
5
6
|
};
|
|
6
7
|
|
|
7
|
-
const manualCache = new Map<string, string | null>();
|
|
8
|
-
const manualsBaseUrl = `${__dirname}/manuals/`;
|
|
9
|
-
|
|
10
|
-
async function dynamicImport(specifier: string): Promise<unknown> {
|
|
11
|
-
const importer = new Function(
|
|
12
|
-
"moduleName",
|
|
13
|
-
"return import(moduleName)",
|
|
14
|
-
) as (moduleName: string) => Promise<unknown>;
|
|
15
|
-
return importer(specifier);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async function loadBundledManual(commandName: string): Promise<string | null> {
|
|
19
|
-
const normalized = commandName.toLowerCase();
|
|
20
|
-
const lookupName = MANUAL_ALIASES[normalized] ?? normalized;
|
|
21
|
-
const cacheKey = `builtin:${lookupName}`;
|
|
22
|
-
if (manualCache.has(cacheKey)) {
|
|
23
|
-
return manualCache.get(cacheKey) ?? null;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
try {
|
|
27
|
-
const fsModule = (await dynamicImport("node:fs/promises")) as {
|
|
28
|
-
readFile: (path: URL, encoding: "utf8") => Promise<string>;
|
|
29
|
-
};
|
|
30
|
-
const manualUrl = new URL(`${lookupName}.txt`, manualsBaseUrl);
|
|
31
|
-
const content = await fsModule.readFile(manualUrl, "utf8");
|
|
32
|
-
const page = content.replace(/\n$/, "");
|
|
33
|
-
manualCache.set(cacheKey, page);
|
|
34
|
-
return page;
|
|
35
|
-
} catch {
|
|
36
|
-
manualCache.set(cacheKey, null);
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
8
|
export const manCommand: ShellModule = {
|
|
42
9
|
name: "man",
|
|
43
10
|
description: "Interface to the system reference manuals",
|
|
@@ -53,7 +20,10 @@ export const manCommand: ShellModule = {
|
|
|
53
20
|
return { stdout: shell.vfs.readFile(manPath), exitCode: 0 };
|
|
54
21
|
}
|
|
55
22
|
|
|
56
|
-
|
|
23
|
+
// Bundled manuals — available in all build modes (standalone, web, dev)
|
|
24
|
+
const normalized = name.toLowerCase();
|
|
25
|
+
const lookupName = MANUAL_ALIASES[normalized] ?? normalized;
|
|
26
|
+
const page = MANUALS[lookupName] ?? null;
|
|
57
27
|
if (page) return { stdout: page, exitCode: 0 };
|
|
58
28
|
|
|
59
29
|
return { stderr: `No manual entry for ${name}`, exitCode: 16 };
|