typescript-virtual-container 1.2.8 → 1.3.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.
Files changed (307) hide show
  1. package/.vscode/settings.json +0 -1
  2. package/README.md +462 -44
  3. package/biome.json +7 -0
  4. package/dist/SSHMimic/exec.d.ts.map +1 -1
  5. package/dist/SSHMimic/executor.d.ts.map +1 -1
  6. package/dist/SSHMimic/executor.js +35 -21
  7. package/dist/SSHMimic/index.d.ts.map +1 -1
  8. package/dist/SSHMimic/index.js +20 -6
  9. package/dist/VirtualFileSystem/binaryPack.d.ts.map +1 -1
  10. package/dist/VirtualFileSystem/binaryPack.js +29 -6
  11. package/dist/VirtualFileSystem/index.d.ts.map +1 -1
  12. package/dist/VirtualFileSystem/index.js +36 -13
  13. package/dist/VirtualPackageManager/index.d.ts +202 -0
  14. package/dist/VirtualPackageManager/index.d.ts.map +1 -0
  15. package/dist/VirtualPackageManager/index.js +825 -0
  16. package/dist/VirtualShell/index.d.ts +93 -12
  17. package/dist/VirtualShell/index.d.ts.map +1 -1
  18. package/dist/VirtualShell/index.js +95 -13
  19. package/dist/VirtualShell/shell.d.ts.map +1 -1
  20. package/dist/VirtualShell/shell.js +3 -1
  21. package/dist/VirtualShell/shellParser.d.ts.map +1 -1
  22. package/dist/VirtualUserManager/index.d.ts +52 -20
  23. package/dist/VirtualUserManager/index.d.ts.map +1 -1
  24. package/dist/VirtualUserManager/index.js +54 -20
  25. package/dist/commands/adduser.d.ts +6 -0
  26. package/dist/commands/adduser.d.ts.map +1 -1
  27. package/dist/commands/adduser.js +6 -0
  28. package/dist/commands/alias.d.ts +9 -0
  29. package/dist/commands/alias.d.ts.map +1 -0
  30. package/dist/commands/alias.js +63 -0
  31. package/dist/commands/apt.d.ts +9 -0
  32. package/dist/commands/apt.d.ts.map +1 -0
  33. package/dist/commands/apt.js +205 -0
  34. package/dist/commands/awk.d.ts +11 -0
  35. package/dist/commands/awk.d.ts.map +1 -1
  36. package/dist/commands/awk.js +15 -2
  37. package/dist/commands/base64.d.ts +5 -0
  38. package/dist/commands/base64.d.ts.map +1 -1
  39. package/dist/commands/base64.js +9 -1
  40. package/dist/commands/cat.d.ts +5 -0
  41. package/dist/commands/cat.d.ts.map +1 -1
  42. package/dist/commands/cat.js +35 -8
  43. package/dist/commands/cd.d.ts +5 -0
  44. package/dist/commands/cd.d.ts.map +1 -1
  45. package/dist/commands/cd.js +5 -0
  46. package/dist/commands/chmod.d.ts +5 -0
  47. package/dist/commands/chmod.d.ts.map +1 -1
  48. package/dist/commands/chmod.js +57 -3
  49. package/dist/commands/command-helpers.d.ts +78 -4
  50. package/dist/commands/command-helpers.d.ts.map +1 -1
  51. package/dist/commands/command-helpers.js +78 -4
  52. package/dist/commands/cp.d.ts +5 -0
  53. package/dist/commands/cp.d.ts.map +1 -1
  54. package/dist/commands/cp.js +5 -0
  55. package/dist/commands/curl.d.ts +5 -0
  56. package/dist/commands/curl.d.ts.map +1 -1
  57. package/dist/commands/curl.js +106 -26
  58. package/dist/commands/cut.d.ts +5 -0
  59. package/dist/commands/cut.d.ts.map +1 -1
  60. package/dist/commands/cut.js +8 -1
  61. package/dist/commands/date.d.ts +5 -0
  62. package/dist/commands/date.d.ts.map +1 -1
  63. package/dist/commands/date.js +7 -1
  64. package/dist/commands/declare.d.ts +3 -0
  65. package/dist/commands/declare.d.ts.map +1 -0
  66. package/dist/commands/declare.js +39 -0
  67. package/dist/commands/diff.d.ts +5 -0
  68. package/dist/commands/diff.d.ts.map +1 -1
  69. package/dist/commands/diff.js +5 -0
  70. package/dist/commands/dpkg.d.ts +9 -0
  71. package/dist/commands/dpkg.d.ts.map +1 -0
  72. package/dist/commands/dpkg.js +161 -0
  73. package/dist/commands/du.d.ts.map +1 -1
  74. package/dist/commands/du.js +8 -2
  75. package/dist/commands/echo.d.ts +5 -0
  76. package/dist/commands/echo.d.ts.map +1 -1
  77. package/dist/commands/echo.js +33 -12
  78. package/dist/commands/env.d.ts +5 -0
  79. package/dist/commands/env.d.ts.map +1 -1
  80. package/dist/commands/env.js +11 -1
  81. package/dist/commands/exit.d.ts +5 -0
  82. package/dist/commands/exit.d.ts.map +1 -1
  83. package/dist/commands/exit.js +12 -2
  84. package/dist/commands/export.d.ts.map +1 -1
  85. package/dist/commands/export.js +3 -1
  86. package/dist/commands/find.d.ts +5 -0
  87. package/dist/commands/find.d.ts.map +1 -1
  88. package/dist/commands/find.js +5 -0
  89. package/dist/commands/free.d.ts +8 -0
  90. package/dist/commands/free.d.ts.map +1 -0
  91. package/dist/commands/free.js +43 -0
  92. package/dist/commands/grep.d.ts +5 -0
  93. package/dist/commands/grep.d.ts.map +1 -1
  94. package/dist/commands/grep.js +12 -2
  95. package/dist/commands/gzip.d.ts +5 -0
  96. package/dist/commands/gzip.d.ts.map +1 -1
  97. package/dist/commands/gzip.js +18 -2
  98. package/dist/commands/head.d.ts +5 -0
  99. package/dist/commands/head.d.ts.map +1 -1
  100. package/dist/commands/head.js +5 -0
  101. package/dist/commands/help.d.ts.map +1 -1
  102. package/dist/commands/help.js +98 -45
  103. package/dist/commands/helpers.d.ts +3 -0
  104. package/dist/commands/helpers.d.ts.map +1 -1
  105. package/dist/commands/helpers.js +3 -0
  106. package/dist/commands/history.d.ts +8 -0
  107. package/dist/commands/history.d.ts.map +1 -0
  108. package/dist/commands/history.js +26 -0
  109. package/dist/commands/hostname.d.ts +5 -0
  110. package/dist/commands/hostname.d.ts.map +1 -1
  111. package/dist/commands/hostname.js +5 -0
  112. package/dist/commands/id.d.ts.map +1 -1
  113. package/dist/commands/id.js +4 -1
  114. package/dist/commands/index.d.ts +2 -10
  115. package/dist/commands/index.d.ts.map +1 -1
  116. package/dist/commands/index.js +2 -231
  117. package/dist/commands/ls.d.ts.map +1 -1
  118. package/dist/commands/ls.js +6 -3
  119. package/dist/commands/lsb-release.d.ts +3 -0
  120. package/dist/commands/lsb-release.d.ts.map +1 -0
  121. package/dist/commands/lsb-release.js +56 -0
  122. package/dist/commands/man.d.ts +3 -0
  123. package/dist/commands/man.d.ts.map +1 -0
  124. package/dist/commands/man.js +155 -0
  125. package/dist/commands/nano.js +1 -1
  126. package/dist/commands/neofetch.d.ts.map +1 -1
  127. package/dist/commands/neofetch.js +6 -1
  128. package/dist/commands/node.d.ts +9 -0
  129. package/dist/commands/node.d.ts.map +1 -0
  130. package/dist/commands/node.js +316 -0
  131. package/dist/commands/npm.d.ts +19 -0
  132. package/dist/commands/npm.d.ts.map +1 -0
  133. package/dist/commands/npm.js +109 -0
  134. package/dist/commands/ping.d.ts.map +1 -1
  135. package/dist/commands/ping.js +7 -2
  136. package/dist/commands/printf.d.ts +3 -0
  137. package/dist/commands/printf.d.ts.map +1 -0
  138. package/dist/commands/printf.js +113 -0
  139. package/dist/commands/ps.d.ts.map +1 -1
  140. package/dist/commands/ps.js +30 -6
  141. package/dist/commands/python.d.ts +30 -0
  142. package/dist/commands/python.d.ts.map +1 -0
  143. package/dist/commands/python.js +2058 -0
  144. package/dist/commands/read.d.ts +3 -0
  145. package/dist/commands/read.d.ts.map +1 -0
  146. package/dist/commands/read.js +34 -0
  147. package/dist/commands/registry.d.ts +8 -0
  148. package/dist/commands/registry.d.ts.map +1 -0
  149. package/dist/commands/registry.js +229 -0
  150. package/dist/commands/runtime.d.ts +6 -0
  151. package/dist/commands/runtime.d.ts.map +1 -0
  152. package/dist/commands/runtime.js +280 -0
  153. package/dist/commands/sed.d.ts.map +1 -1
  154. package/dist/commands/sed.js +11 -3
  155. package/dist/commands/set.d.ts.map +1 -1
  156. package/dist/commands/set.js +9 -3
  157. package/dist/commands/sh.d.ts.map +1 -1
  158. package/dist/commands/sh.js +69 -30
  159. package/dist/commands/shift.d.ts +5 -0
  160. package/dist/commands/shift.d.ts.map +1 -0
  161. package/dist/commands/shift.js +52 -0
  162. package/dist/commands/sleep.d.ts.map +1 -1
  163. package/dist/commands/sort.d.ts.map +1 -1
  164. package/dist/commands/sort.js +4 -2
  165. package/dist/commands/source.d.ts +3 -0
  166. package/dist/commands/source.d.ts.map +1 -0
  167. package/dist/commands/source.js +34 -0
  168. package/dist/commands/sudo.js +1 -1
  169. package/dist/commands/tar.d.ts.map +1 -1
  170. package/dist/commands/tar.js +11 -3
  171. package/dist/commands/tee.d.ts.map +1 -1
  172. package/dist/commands/tee.js +8 -6
  173. package/dist/commands/test.d.ts +3 -0
  174. package/dist/commands/test.d.ts.map +1 -0
  175. package/dist/commands/test.js +114 -0
  176. package/dist/commands/tr.d.ts.map +1 -1
  177. package/dist/commands/tr.js +3 -1
  178. package/dist/commands/true.d.ts +4 -0
  179. package/dist/commands/true.d.ts.map +1 -0
  180. package/dist/commands/true.js +14 -0
  181. package/dist/commands/type.d.ts +3 -0
  182. package/dist/commands/type.d.ts.map +1 -0
  183. package/dist/commands/type.js +34 -0
  184. package/dist/commands/uname.d.ts.map +1 -1
  185. package/dist/commands/uname.js +4 -1
  186. package/dist/commands/uniq.d.ts.map +1 -1
  187. package/dist/commands/uptime.d.ts +3 -0
  188. package/dist/commands/uptime.d.ts.map +1 -0
  189. package/dist/commands/uptime.js +43 -0
  190. package/dist/commands/wget.d.ts.map +1 -1
  191. package/dist/commands/wget.js +92 -96
  192. package/dist/commands/which.d.ts +3 -0
  193. package/dist/commands/which.d.ts.map +1 -0
  194. package/dist/commands/which.js +32 -0
  195. package/dist/commands/xargs.d.ts.map +1 -1
  196. package/dist/commands/xargs.js +1 -1
  197. package/dist/index.d.ts +15 -11
  198. package/dist/index.d.ts.map +1 -1
  199. package/dist/index.js +9 -8
  200. package/dist/modules/linuxRootfs.d.ts +41 -0
  201. package/dist/modules/linuxRootfs.d.ts.map +1 -0
  202. package/dist/modules/linuxRootfs.js +440 -0
  203. package/dist/modules/neofetch.d.ts.map +1 -1
  204. package/dist/modules/neofetch.js +1 -0
  205. package/dist/standalone-wo-sftp.d.ts +2 -0
  206. package/dist/standalone-wo-sftp.d.ts.map +1 -0
  207. package/dist/standalone-wo-sftp.js +30 -0
  208. package/dist/utils/expand.d.ts +50 -0
  209. package/dist/utils/expand.d.ts.map +1 -0
  210. package/dist/utils/expand.js +183 -0
  211. package/dist/utils/vfsDiff.d.ts +90 -0
  212. package/dist/utils/vfsDiff.d.ts.map +1 -0
  213. package/dist/utils/vfsDiff.js +177 -0
  214. package/package.json +3 -1
  215. package/src/SSHMimic/exec.ts +10 -1
  216. package/src/SSHMimic/executor.ts +105 -21
  217. package/src/SSHMimic/index.ts +49 -15
  218. package/src/VirtualFileSystem/binaryPack.ts +35 -8
  219. package/src/VirtualFileSystem/index.ts +78 -28
  220. package/src/VirtualPackageManager/index.ts +979 -0
  221. package/src/VirtualShell/index.ts +133 -14
  222. package/src/VirtualShell/shell.ts +23 -3
  223. package/src/VirtualShell/shellParser.ts +134 -36
  224. package/src/VirtualUserManager/index.ts +62 -22
  225. package/src/commands/adduser.ts +6 -0
  226. package/src/commands/alias.ts +64 -0
  227. package/src/commands/apt.ts +228 -0
  228. package/src/commands/awk.ts +20 -6
  229. package/src/commands/base64.ts +13 -2
  230. package/src/commands/cat.ts +40 -8
  231. package/src/commands/cd.ts +5 -0
  232. package/src/commands/chmod.ts +53 -3
  233. package/src/commands/command-helpers.ts +78 -4
  234. package/src/commands/cp.ts +5 -0
  235. package/src/commands/curl.ts +118 -33
  236. package/src/commands/cut.ts +8 -1
  237. package/src/commands/date.ts +7 -1
  238. package/src/commands/declare.ts +44 -0
  239. package/src/commands/diff.ts +17 -3
  240. package/src/commands/dpkg.ts +180 -0
  241. package/src/commands/du.ts +17 -5
  242. package/src/commands/echo.ts +41 -12
  243. package/src/commands/env.ts +11 -1
  244. package/src/commands/exit.ts +12 -2
  245. package/src/commands/export.ts +3 -1
  246. package/src/commands/find.ts +5 -0
  247. package/src/commands/free.ts +47 -0
  248. package/src/commands/grep.ts +12 -2
  249. package/src/commands/gzip.ts +28 -4
  250. package/src/commands/head.ts +5 -0
  251. package/src/commands/help.ts +121 -47
  252. package/src/commands/helpers.ts +8 -0
  253. package/src/commands/history.ts +34 -0
  254. package/src/commands/hostname.ts +5 -0
  255. package/src/commands/id.ts +4 -1
  256. package/src/commands/index.ts +9 -255
  257. package/src/commands/ls.ts +6 -3
  258. package/src/commands/lsb-release.ts +58 -0
  259. package/src/commands/man.ts +166 -0
  260. package/src/commands/nano.ts +1 -1
  261. package/src/commands/neofetch.ts +6 -1
  262. package/src/commands/node.ts +341 -0
  263. package/src/commands/npm.ts +132 -0
  264. package/src/commands/ping.ts +10 -3
  265. package/src/commands/printf.ts +112 -0
  266. package/src/commands/ps.ts +40 -6
  267. package/src/commands/python.ts +2229 -0
  268. package/src/commands/read.ts +41 -0
  269. package/src/commands/registry.ts +244 -0
  270. package/src/commands/runtime.ts +353 -0
  271. package/src/commands/sed.ts +27 -9
  272. package/src/commands/set.ts +9 -3
  273. package/src/commands/sh.ts +170 -44
  274. package/src/commands/shift.ts +53 -0
  275. package/src/commands/sleep.ts +2 -1
  276. package/src/commands/sort.ts +10 -6
  277. package/src/commands/source.ts +47 -0
  278. package/src/commands/sudo.ts +1 -1
  279. package/src/commands/tar.ts +28 -7
  280. package/src/commands/tee.ts +7 -1
  281. package/src/commands/test.ts +135 -0
  282. package/src/commands/tr.ts +3 -1
  283. package/src/commands/true.ts +17 -0
  284. package/src/commands/type.ts +43 -0
  285. package/src/commands/uname.ts +5 -1
  286. package/src/commands/uniq.ts +8 -2
  287. package/src/commands/uptime.ts +49 -0
  288. package/src/commands/wget.ts +105 -119
  289. package/src/commands/which.ts +37 -0
  290. package/src/commands/xargs.ts +11 -2
  291. package/src/index.ts +27 -18
  292. package/src/modules/linuxRootfs.ts +642 -0
  293. package/src/modules/neofetch.ts +1 -0
  294. package/src/standalone-wo-sftp.ts +38 -0
  295. package/src/utils/expand.ts +238 -0
  296. package/src/utils/vfsDiff.ts +275 -0
  297. package/standalone-wo-sftp.js +507 -0
  298. package/standalone-wo-sftp.js.map +7 -0
  299. package/standalone.js +486 -109
  300. package/standalone.js.map +4 -4
  301. package/tests/bun-test-shim.ts +9 -1
  302. package/tests/command-helpers.test.ts +1 -5
  303. package/tests/new-features.test.ts +1036 -0
  304. package/tests/parser-executor.test.ts +27 -27
  305. package/tests/sftp.test.ts +122 -42
  306. package/tests/users.test.ts +23 -5
  307. package/CHANGELOG.md +0 -150
@@ -0,0 +1,642 @@
1
+ /** biome-ignore-all lint/style/useNamingConvention: ENV VAR KEYS */
2
+ /**
3
+ * linuxRootfs.ts
4
+ *
5
+ * Bootstraps a realistic Linux directory hierarchy in the VFS.
6
+ * Called once during VirtualShell initialization. Idempotent — skips
7
+ * paths that already exist so FS-mode snapshots survive restarts.
8
+ */
9
+
10
+ import * as os from "node:os";
11
+ import type { ShellProperties } from "../VirtualShell";
12
+ import type VirtualFileSystem from "../VirtualFileSystem";
13
+ import type { VirtualUserManager } from "../VirtualUserManager";
14
+
15
+ // ─── helpers ────────────────────────────────────────────────────────────────
16
+
17
+ function ensureDir(vfs: VirtualFileSystem, path: string, mode = 0o755): void {
18
+ if (!vfs.exists(path)) vfs.mkdir(path, mode);
19
+ }
20
+
21
+ function ensureFile(
22
+ vfs: VirtualFileSystem,
23
+ path: string,
24
+ content: string,
25
+ mode = 0o644,
26
+ ): void {
27
+ if (!vfs.exists(path)) vfs.writeFile(path, content, { mode });
28
+ }
29
+
30
+ // ─── /etc ────────────────────────────────────────────────────────────────────
31
+
32
+ function bootstrapEtc(
33
+ vfs: VirtualFileSystem,
34
+ hostname: string,
35
+ props: ShellProperties,
36
+ ): void {
37
+ ensureDir(vfs, "/etc");
38
+
39
+ // os-release — authoritative distro identity used by neofetch, lsb_release
40
+ ensureFile(
41
+ vfs,
42
+ "/etc/os-release",
43
+ `${[
44
+ `NAME="Fortune GNU/Linux"`,
45
+ `PRETTY_NAME="${props.os}"`,
46
+ `ID=fortune`,
47
+ `ID_LIKE=debian`,
48
+ `HOME_URL="https://github.com/itsrealfortune/typescript-virtual-container"`,
49
+ `VERSION_CODENAME=aurora`,
50
+ `VERSION_ID="1.0"`,
51
+ ].join("\n")}\n`,
52
+ );
53
+
54
+ ensureFile(vfs, "/etc/debian_version", "12.0\n");
55
+ ensureFile(vfs, "/etc/hostname", `${hostname}\n`);
56
+ ensureFile(vfs, "/etc/shells", "/bin/sh\n/bin/bash\n/usr/bin/bash\n");
57
+ ensureFile(
58
+ vfs,
59
+ "/etc/profile",
60
+ `${[
61
+ "export PATH=/usr/local/bin:/usr/bin:/bin",
62
+ "export PS1='\\u@\\h:\\w\\$ '",
63
+ ].join("\n")}\n`,
64
+ );
65
+
66
+ ensureFile(vfs, "/etc/issue", `Fortune GNU/Linux 1.0 \\n \\l\n`);
67
+
68
+ ensureFile(
69
+ vfs,
70
+ "/etc/motd",
71
+ ["", `Welcome to ${props.os}`, `Kernel: ${props.kernel}`, ""].join("\n"),
72
+ );
73
+
74
+ // APT sources
75
+ ensureDir(vfs, "/etc/apt");
76
+ ensureDir(vfs, "/etc/apt/sources.list.d");
77
+ ensureFile(
78
+ vfs,
79
+ "/etc/apt/sources.list",
80
+ `${[
81
+ "# Fortune GNU/Linux package sources",
82
+ "deb [virtual] fortune://packages.fortune.local aurora main contrib",
83
+ "deb [virtual] fortune://security.fortune.local aurora-security main",
84
+ ].join("\n")}\n`,
85
+ );
86
+
87
+ // network stubs
88
+ ensureDir(vfs, "/etc/network");
89
+ ensureFile(
90
+ vfs,
91
+ "/etc/network/interfaces",
92
+ `${[
93
+ "auto lo",
94
+ "iface lo inet loopback",
95
+ "",
96
+ "auto eth0",
97
+ "iface eth0 inet dhcp",
98
+ ].join("\n")}\n`,
99
+ );
100
+
101
+ ensureFile(
102
+ vfs,
103
+ "/etc/resolv.conf",
104
+ "nameserver 1.1.1.1\nnameserver 8.8.8.8\n",
105
+ );
106
+
107
+ ensureFile(
108
+ vfs,
109
+ "/etc/hosts",
110
+ `${[
111
+ "127.0.0.1 localhost",
112
+ `127.0.1.1 ${hostname}`,
113
+ "::1 localhost ip6-localhost ip6-loopback",
114
+ ].join("\n")}\n`,
115
+ );
116
+
117
+ ensureDir(vfs, "/etc/cron.d");
118
+ ensureDir(vfs, "/etc/init.d");
119
+ ensureDir(vfs, "/etc/systemd");
120
+ ensureDir(vfs, "/etc/systemd/system");
121
+ }
122
+
123
+ // ─── /etc/passwd + /etc/group + /etc/shadow ─────────────────────────────────
124
+
125
+ /**
126
+ * Sync `/etc/passwd`, `/etc/group`, and `/etc/shadow` from the
127
+ * VirtualUserManager's current user list into the VFS.
128
+ * @param vfs VirtualFileSystem instance to write files into
129
+ * @param users VirtualUserManager to source users from
130
+ */
131
+ export function syncEtcPasswd(
132
+ vfs: VirtualFileSystem,
133
+ users: VirtualUserManager,
134
+ ): void {
135
+ const userList = users.listUsers();
136
+
137
+ const passwdLines = [
138
+ "root:x:0:0:root:/root:/bin/bash",
139
+ "daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin",
140
+ "www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin",
141
+ "nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin",
142
+ ];
143
+
144
+ let uid = 1000;
145
+ for (const u of userList) {
146
+ if (u === "root") continue;
147
+ passwdLines.push(`${u}:x:${uid}:${uid}::/home/${u}:/bin/bash`);
148
+ uid++;
149
+ }
150
+
151
+ vfs.writeFile("/etc/passwd", `${passwdLines.join("\n")}\n`);
152
+
153
+ const groupLines = [
154
+ "root:x:0:",
155
+ "daemon:x:1:",
156
+ `sudo:x:27:${userList.filter((u) => users.isSudoer(u)).join(",")}`,
157
+ `users:x:100:${userList.filter((u) => u !== "root").join(",")}`,
158
+ "nogroup:x:65534:",
159
+ ];
160
+ vfs.writeFile("/etc/group", `${groupLines.join("\n")}\n`);
161
+
162
+ // shadow — fake hashes, never real
163
+ const shadowLines = [
164
+ "root:*:19000:0:99999:7:::",
165
+ "daemon:*:19000:0:99999:7:::",
166
+ ];
167
+ for (const u of userList) {
168
+ if (u === "root") continue;
169
+ shadowLines.push(`${u}:!:19000:0:99999:7:::`);
170
+ }
171
+ vfs.writeFile("/etc/shadow", `${shadowLines.join("\n")}\n`, { mode: 0o640 });
172
+ }
173
+
174
+ // ─── /proc ───────────────────────────────────────────────────────────────────
175
+
176
+ /** Derive a stable virtual PID from a tty string like "pts/0" → 1000, "pts/1" → 1001 */
177
+ function ttyToPid(tty: string): number {
178
+ const match = tty.match(/(\d+)$/);
179
+ return 1000 + (match?.[1] ? parseInt(match[1], 10) : 0);
180
+ }
181
+
182
+ /** Write /proc/<pid>/ subtree for a single virtual process */
183
+ function writeProcPid(
184
+ vfs: VirtualFileSystem,
185
+ pid: number,
186
+ username: string,
187
+ _tty: string,
188
+ cmdline: string,
189
+ startedAt: string,
190
+ env: Record<string, string>,
191
+ ): void {
192
+ const dir = `/proc/${pid}`;
193
+ ensureDir(vfs, dir);
194
+ ensureDir(vfs, `${dir}/fd`);
195
+ ensureDir(vfs, `${dir}/fdinfo`);
196
+
197
+ const uptimeSec = Math.floor(
198
+ (Date.now() - new Date(startedAt).getTime()) / 1000,
199
+ );
200
+
201
+ vfs.writeFile(`${dir}/cmdline`, `${cmdline.replace(/\s+/g, "\0")}\0`);
202
+ vfs.writeFile(`${dir}/comm`, cmdline.split(/\s+/)[0] ?? "bash");
203
+ vfs.writeFile(
204
+ `${dir}/status`,
205
+ `${[
206
+ `Name: ${cmdline.split(/\s+/)[0] ?? "bash"}`,
207
+ `State: S (sleeping)`,
208
+ `Pid: ${pid}`,
209
+ `PPid: 1`,
210
+ `Uid: 0\t0\t0\t0`,
211
+ `Gid: 0\t0\t0\t0`,
212
+ `VmRSS: 4096 kB`,
213
+ `VmSize: 16384 kB`,
214
+ `Threads: 1`,
215
+ ].join("\n")}\n`,
216
+ );
217
+ vfs.writeFile(
218
+ `${dir}/stat`,
219
+ `${pid} (${cmdline.split(/\s+/)[0] ?? "bash"}) S 1 ${pid} ${pid} 0 -1 4194304 0 0 0 0 ${uptimeSec} 0 0 0 20 0 1 0 0 16384 4096 0\n`,
220
+ );
221
+ vfs.writeFile(
222
+ `${dir}/environ`,
223
+ `${Object.entries(env)
224
+ .map(([k, v]) => `${k}=${v}`)
225
+ .join("\0")}\0`,
226
+ );
227
+ vfs.writeFile(`${dir}/cwd`, `/home/${username}\0`);
228
+ vfs.writeFile(`${dir}/exe`, "/bin/bash\0");
229
+
230
+ // Standard fd entries
231
+ vfs.writeFile(`${dir}/fd/0`, "");
232
+ vfs.writeFile(`${dir}/fd/1`, "");
233
+ vfs.writeFile(`${dir}/fd/2`, "");
234
+ }
235
+
236
+ /**
237
+ * Populate and refresh `/proc` virtual entries based on host stats and
238
+ * provided active sessions. Rewrites `/proc/uptime`, `/proc/meminfo`,
239
+ * `/proc/cpuinfo`, `/proc/<pid>` entries and `/proc/self` content.
240
+ * @param vfs VirtualFileSystem instance
241
+ * @param props ShellProperties used for version strings
242
+ * @param hostname Hostname to write into /proc/hostname
243
+ * @param shellStartTime Start time used to compute uptime
244
+ * @param sessions Optional active sessions list to populate per-pid entries
245
+ */
246
+ export function refreshProc(
247
+ vfs: VirtualFileSystem,
248
+ props: ShellProperties,
249
+ hostname: string,
250
+ shellStartTime: number,
251
+ sessions?: import("../VirtualUserManager").VirtualActiveSession[],
252
+ ): void {
253
+ ensureDir(vfs, "/proc");
254
+
255
+ const uptimeSec = Math.floor((Date.now() - shellStartTime) / 1000);
256
+ vfs.writeFile(
257
+ "/proc/uptime",
258
+ `${uptimeSec}.00 ${Math.floor(uptimeSec * 0.9)}.00\n`,
259
+ );
260
+
261
+ const totalMemKb = Math.floor(os.totalmem() / 1024);
262
+ const freeMemKb = Math.floor(os.freemem() / 1024);
263
+ const availMemKb = Math.floor(freeMemKb * 0.95);
264
+ vfs.writeFile(
265
+ "/proc/meminfo",
266
+ `${[
267
+ `MemTotal: ${String(totalMemKb).padStart(10)} kB`,
268
+ `MemFree: ${String(freeMemKb).padStart(10)} kB`,
269
+ `MemAvailable: ${String(availMemKb).padStart(10)} kB`,
270
+ `Buffers: ${String(Math.floor(totalMemKb * 0.02)).padStart(10)} kB`,
271
+ `Cached: ${String(Math.floor(totalMemKb * 0.15)).padStart(10)} kB`,
272
+ `SwapTotal: ${String(Math.floor(totalMemKb * 0.5)).padStart(10)} kB`,
273
+ `SwapFree: ${String(Math.floor(totalMemKb * 0.5)).padStart(10)} kB`,
274
+ ].join("\n")}\n`,
275
+ );
276
+
277
+ const cpus = os.cpus();
278
+ const cpuLines: string[] = [];
279
+ for (let i = 0; i < cpus.length; i++) {
280
+ const c = cpus[i];
281
+ if (!c) continue;
282
+ const mhz = c.speed.toFixed(3);
283
+ cpuLines.push(
284
+ `processor\t: ${i}`,
285
+ `model name\t: ${c.model}`,
286
+ `cpu MHz\t\t: ${mhz}`,
287
+ `cache size\t: 8192 KB`,
288
+ "",
289
+ );
290
+ }
291
+ vfs.writeFile("/proc/cpuinfo", `${cpuLines.join("\n")}\n`);
292
+
293
+ vfs.writeFile(
294
+ "/proc/version",
295
+ `Linux version ${props.kernel} (fortune@build) (gcc version 12.2.0) #1 SMP\n`,
296
+ );
297
+
298
+ vfs.writeFile("/proc/hostname", `${hostname}\n`);
299
+
300
+ // /proc/loadavg
301
+ const load = (Math.random() * 0.5).toFixed(2);
302
+ const numProcs = 1 + (sessions?.length ?? 0);
303
+ vfs.writeFile(
304
+ "/proc/loadavg",
305
+ `${load} ${load} ${load} ${numProcs}/${numProcs} 1\n`,
306
+ );
307
+
308
+ // /proc/net stubs
309
+ ensureDir(vfs, "/proc/net");
310
+ ensureFile(
311
+ vfs,
312
+ "/proc/net/dev",
313
+ `${[
314
+ "Inter-| Receive | Transmit",
315
+ " face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed",
316
+ " lo: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0",
317
+ " eth0: 131072 1024 0 0 0 0 0 0 65536 512 0 0 0 0 0 0",
318
+ ].join("\n")}\n`,
319
+ );
320
+
321
+ // ── /proc/1 — init process ────────────────────────────────────────────────
322
+ writeProcPid(
323
+ vfs,
324
+ 1,
325
+ "root",
326
+ "pts/0",
327
+ "/sbin/init",
328
+ new Date(shellStartTime).toISOString(),
329
+ {},
330
+ );
331
+
332
+ // ── /proc/<pid> per session ───────────────────────────────────────────────
333
+ const activeSessions = sessions ?? [];
334
+ for (const session of activeSessions) {
335
+ const pid = ttyToPid(session.tty);
336
+ writeProcPid(
337
+ vfs,
338
+ pid,
339
+ session.username,
340
+ session.tty,
341
+ "bash",
342
+ session.startedAt,
343
+ {
344
+ USER: session.username,
345
+ HOME: `/home/${session.username}`,
346
+ TERM: "xterm-256color",
347
+ SHELL: "/bin/bash",
348
+ },
349
+ );
350
+ }
351
+
352
+ // ── /proc/self — symlink to current session PID or 1 ────────────────────
353
+ // We can't know which session is "current" at populate time,
354
+ // so /proc/self is a directory that mirrors the most recent session,
355
+ // or init if no sessions. Commands that read /proc/self get consistent data.
356
+ const selfPid =
357
+ activeSessions.length > 0
358
+ ? ttyToPid(activeSessions[activeSessions.length - 1]!.tty)
359
+ : 1;
360
+
361
+ // Remove existing /proc/self and recreate as content copy
362
+ if (vfs.exists("/proc/self")) {
363
+ try {
364
+ vfs.remove("/proc/self");
365
+ } catch {}
366
+ }
367
+ // /proc/self is a real directory (not a symlink, which VFS may not support for dirs)
368
+ const selfSrc = `/proc/${selfPid}`;
369
+ if (vfs.exists(selfSrc)) {
370
+ ensureDir(vfs, "/proc/self");
371
+ ensureDir(vfs, "/proc/self/fd");
372
+ for (const entry of vfs.list(selfSrc)) {
373
+ const srcPath = `${selfSrc}/${entry}`;
374
+ const dstPath = `/proc/self/${entry}`;
375
+ try {
376
+ const st = vfs.stat(srcPath);
377
+ if (st.type === "file") {
378
+ vfs.writeFile(dstPath, vfs.readFile(srcPath));
379
+ }
380
+ } catch {}
381
+ }
382
+ vfs.writeFile(
383
+ "/proc/self/status",
384
+ vfs.exists(`${selfSrc}/status`) ? vfs.readFile(`${selfSrc}/status`) : "",
385
+ );
386
+ } else {
387
+ // Fallback minimal /proc/self
388
+ ensureDir(vfs, "/proc/self");
389
+ vfs.writeFile("/proc/self/cmdline", "bash\0");
390
+ vfs.writeFile("/proc/self/comm", "bash");
391
+ vfs.writeFile(
392
+ "/proc/self/status",
393
+ "Name:\tbash\nState:\tS (sleeping)\nPid:\t1\nPPid:\t0\n",
394
+ );
395
+ vfs.writeFile("/proc/self/environ", "");
396
+ vfs.writeFile("/proc/self/cwd", "/root\0");
397
+ vfs.writeFile("/proc/self/exe", "/bin/bash\0");
398
+ }
399
+ }
400
+
401
+ // ─── /sys ─────────────────────────────────────────────────────────────────────
402
+
403
+ function bootstrapSys(vfs: VirtualFileSystem, props: ShellProperties): void {
404
+ ensureDir(vfs, "/sys");
405
+ ensureDir(vfs, "/sys/devices");
406
+ ensureDir(vfs, "/sys/devices/virtual");
407
+ ensureDir(vfs, "/sys/devices/virtual/dmi");
408
+ ensureDir(vfs, "/sys/devices/virtual/dmi/id");
409
+
410
+ ensureFile(
411
+ vfs,
412
+ "/sys/devices/virtual/dmi/id/sys_vendor",
413
+ "Fortune Systems\n",
414
+ );
415
+ ensureFile(
416
+ vfs,
417
+ "/sys/devices/virtual/dmi/id/product_name",
418
+ "VirtualContainer v1\n",
419
+ );
420
+ ensureFile(vfs, "/sys/devices/virtual/dmi/id/board_name", "fortune-board\n");
421
+
422
+ ensureDir(vfs, "/sys/class");
423
+ ensureDir(vfs, "/sys/class/net");
424
+
425
+ ensureDir(vfs, "/sys/kernel");
426
+ ensureFile(vfs, "/sys/kernel/hostname", "fortune-vm\n");
427
+ ensureFile(vfs, "/sys/kernel/osrelease", `${props.kernel}\n`);
428
+ ensureFile(vfs, "/sys/kernel/ostype", "Linux\n");
429
+ }
430
+
431
+ // ─── /dev ─────────────────────────────────────────────────────────────────────
432
+
433
+ function bootstrapDev(vfs: VirtualFileSystem): void {
434
+ ensureDir(vfs, "/dev");
435
+ ensureFile(vfs, "/dev/null", "", 0o666);
436
+ ensureFile(vfs, "/dev/zero", "", 0o666);
437
+ ensureFile(vfs, "/dev/random", "", 0o444);
438
+ ensureFile(vfs, "/dev/urandom", "", 0o444);
439
+ ensureDir(vfs, "/dev/pts");
440
+ ensureDir(vfs, "/dev/shm");
441
+ }
442
+
443
+ // ─── /usr ─────────────────────────────────────────────────────────────────────
444
+
445
+ function bootstrapUsr(vfs: VirtualFileSystem): void {
446
+ ensureDir(vfs, "/usr");
447
+ ensureDir(vfs, "/usr/bin");
448
+ ensureDir(vfs, "/usr/sbin");
449
+ ensureDir(vfs, "/usr/local");
450
+ ensureDir(vfs, "/usr/local/bin");
451
+ ensureDir(vfs, "/usr/local/lib");
452
+ ensureDir(vfs, "/usr/local/share");
453
+ ensureDir(vfs, "/usr/share");
454
+ ensureDir(vfs, "/usr/share/doc");
455
+ ensureDir(vfs, "/usr/share/man");
456
+ ensureDir(vfs, "/usr/share/man/man1");
457
+ ensureDir(vfs, "/usr/lib");
458
+
459
+ // Stub binaries so `which` can find built-in commands
460
+ const builtins = [
461
+ "sh",
462
+ "bash",
463
+ "ls",
464
+ "cat",
465
+ "echo",
466
+ "grep",
467
+ "find",
468
+ "sort",
469
+ "head",
470
+ "tail",
471
+ "cut",
472
+ "tr",
473
+ "sed",
474
+ "awk",
475
+ "wc",
476
+ "tee",
477
+ "tar",
478
+ "gzip",
479
+ "gunzip",
480
+ "touch",
481
+ "mkdir",
482
+ "rm",
483
+ "mv",
484
+ "cp",
485
+ "chmod",
486
+ "ln",
487
+ "pwd",
488
+ "env",
489
+ "date",
490
+ "sleep",
491
+ "id",
492
+ "whoami",
493
+ "hostname",
494
+ "uname",
495
+ "ps",
496
+ "kill",
497
+ "df",
498
+ "du",
499
+ "curl",
500
+ "wget",
501
+ "nano",
502
+ "diff",
503
+ "uniq",
504
+ "xargs",
505
+ "base64",
506
+ ];
507
+ for (const bin of builtins) {
508
+ ensureFile(
509
+ vfs,
510
+ `/usr/bin/${bin}`,
511
+ `#!/bin/sh\nexec builtin ${bin} "$@"\n`,
512
+ 0o755,
513
+ );
514
+ }
515
+
516
+ // lsb_release script
517
+ ensureFile(
518
+ vfs,
519
+ "/usr/bin/lsb_release",
520
+ '#!/bin/sh\nexec lsb_release "$@"\n',
521
+ 0o755,
522
+ );
523
+ }
524
+
525
+ // ─── /var ─────────────────────────────────────────────────────────────────────
526
+
527
+ function bootstrapVar(vfs: VirtualFileSystem): void {
528
+ ensureDir(vfs, "/var");
529
+ ensureDir(vfs, "/var/log");
530
+ ensureDir(vfs, "/var/tmp");
531
+ ensureDir(vfs, "/var/run");
532
+ ensureDir(vfs, "/var/cache");
533
+ ensureDir(vfs, "/var/cache/apt");
534
+ ensureDir(vfs, "/var/cache/apt/archives");
535
+ ensureDir(vfs, "/var/lib");
536
+ ensureDir(vfs, "/var/lib/apt");
537
+ ensureDir(vfs, "/var/lib/apt/lists");
538
+ ensureDir(vfs, "/var/lib/dpkg");
539
+ ensureDir(vfs, "/var/lib/dpkg/info");
540
+
541
+ // dpkg status — starts empty, apt install populates it
542
+ ensureFile(vfs, "/var/lib/dpkg/status", "");
543
+ ensureFile(vfs, "/var/lib/dpkg/available", "");
544
+
545
+ // syslog stub
546
+ ensureFile(
547
+ vfs,
548
+ "/var/log/syslog",
549
+ `${new Date().toUTCString()} fortune kernel: Virtual container started\n`,
550
+ );
551
+ ensureFile(vfs, "/var/log/auth.log", "");
552
+ ensureFile(vfs, "/var/log/dpkg.log", "");
553
+ ensureFile(vfs, "/var/log/apt/history.log", "");
554
+ ensureFile(vfs, "/var/log/apt/term.log", "");
555
+ }
556
+
557
+ // ─── /bin + /sbin symlinks ────────────────────────────────────────────────────
558
+
559
+ function bootstrapBin(vfs: VirtualFileSystem): void {
560
+ // On modern Debian/Ubuntu /bin is a symlink to /usr/bin
561
+ if (!vfs.exists("/bin")) {
562
+ vfs.symlink("/usr/bin", "/bin");
563
+ }
564
+ if (!vfs.exists("/sbin")) {
565
+ vfs.symlink("/usr/sbin", "/sbin");
566
+ }
567
+ if (!vfs.exists("/lib")) {
568
+ ensureDir(vfs, "/lib");
569
+ }
570
+ if (!vfs.exists("/lib64")) {
571
+ ensureDir(vfs, "/lib64");
572
+ }
573
+ }
574
+
575
+ // ─── /tmp ─────────────────────────────────────────────────────────────────────
576
+
577
+ function bootstrapTmp(vfs: VirtualFileSystem): void {
578
+ ensureDir(vfs, "/tmp", 0o1777);
579
+ }
580
+
581
+ // ─── /root ────────────────────────────────────────────────────────────────────
582
+
583
+ function bootstrapRoot(vfs: VirtualFileSystem): void {
584
+ ensureDir(vfs, "/root", 0o700);
585
+ ensureFile(
586
+ vfs,
587
+ "/root/.bashrc",
588
+ `${[
589
+ "# root .bashrc",
590
+ "export PS1='\\[\\033[0;31m\\]\\u@\\h\\[\\033[0m\\]:\\[\\033[0;34m\\]\\w\\[\\033[0m\\]# '",
591
+ "export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
592
+ "alias ll='ls -la'",
593
+ "alias la='ls -A'",
594
+ ].join("\n")}\n`,
595
+ );
596
+ ensureFile(vfs, "/root/.profile", "[ -f ~/.bashrc ] && . ~/.bashrc\n");
597
+ // Fix: /home/root should map to /root for root user
598
+ if (!vfs.exists("/home/root")) {
599
+ vfs.symlink("/root", "/home/root");
600
+ }
601
+ }
602
+
603
+ // ─── /opt + /srv + /mnt + /media ─────────────────────────────────────────────
604
+
605
+ function bootstrapMisc(vfs: VirtualFileSystem): void {
606
+ ensureDir(vfs, "/opt");
607
+ ensureDir(vfs, "/srv");
608
+ ensureDir(vfs, "/mnt");
609
+ ensureDir(vfs, "/media");
610
+ }
611
+
612
+ // ─── main entry point ─────────────────────────────────────────────────────────
613
+
614
+ /**
615
+ * Bootstraps the full Linux rootfs hierarchy in the VFS.
616
+ * Safe to call multiple times — idempotent.
617
+ *
618
+ * @param vfs Target virtual filesystem.
619
+ * @param users User manager (for /etc/passwd sync).
620
+ * @param hostname Virtual hostname.
621
+ * @param props Shell properties (kernel, os, arch).
622
+ * @param shellStartTime Unix ms of shell creation (for uptime).
623
+ */
624
+ export function bootstrapLinuxRootfs(
625
+ vfs: VirtualFileSystem,
626
+ users: VirtualUserManager,
627
+ hostname: string,
628
+ props: ShellProperties,
629
+ shellStartTime: number,
630
+ ): void {
631
+ bootstrapEtc(vfs, hostname, props);
632
+ bootstrapSys(vfs, props);
633
+ bootstrapDev(vfs);
634
+ bootstrapUsr(vfs);
635
+ bootstrapVar(vfs);
636
+ bootstrapBin(vfs);
637
+ bootstrapTmp(vfs);
638
+ bootstrapRoot(vfs);
639
+ bootstrapMisc(vfs);
640
+ refreshProc(vfs, props, hostname, shellStartTime, []);
641
+ syncEtcPasswd(vfs, users);
642
+ }
@@ -321,6 +321,7 @@ export function buildNeofetchOutput(info: NeofetchInfo): string {
321
321
  `Kernel: ${fields.kernel}`,
322
322
  `Uptime: ${uptime}`,
323
323
  // `Packages: ${fields.packages}`,
324
+ `Packages: ${fields.packages}`,
324
325
  `Shell: ${fields.shell}`,
325
326
  // `Shell Props: ${fields.shellProps}`,
326
327
  `Resolution: ${fields.resolution}`,
@@ -0,0 +1,38 @@
1
+ import { SshMimic } from "./SSHMimic/index";
2
+ import { VirtualShell } from "./VirtualShell";
3
+
4
+ const hostname = process.env.SSH_MIMIC_HOSTNAME ?? "typescript-vm";
5
+ const virtualShell = new VirtualShell(hostname, undefined, {
6
+ mode: "fs",
7
+ snapshotPath: ".vfs",
8
+ });
9
+
10
+ virtualShell.addCommand("demo", [], () => {
11
+ return {
12
+ stdout: "This is a demo command. It does nothing useful.",
13
+ exitCode: 0,
14
+ };
15
+ });
16
+
17
+ new SshMimic({
18
+ port: 2222,
19
+ hostname,
20
+ shell: virtualShell,
21
+ })
22
+ .start()
23
+ .catch((error: unknown) => {
24
+ console.error("Failed to start SSH Mimic:", error);
25
+ process.exit(1);
26
+ });
27
+
28
+ process.on("uncaughtException", (error) => {
29
+ console.log("Oh my god, something terrible happened: ", error);
30
+ });
31
+
32
+ process.on("unhandledRejection", (error, promise) => {
33
+ console.log(
34
+ " Oh Lord! We forgot to handle a promise rejection here: ",
35
+ promise,
36
+ );
37
+ console.log(" The error was: ", error);
38
+ });