typescript-virtual-container 1.2.9 → 1.3.1

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 (281) hide show
  1. package/.vscode/settings.json +0 -1
  2. package/README.md +141 -50
  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 +32 -16
  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.map +1 -1
  14. package/dist/VirtualPackageManager/index.js +192 -43
  15. package/dist/VirtualShell/index.d.ts +10 -4
  16. package/dist/VirtualShell/index.d.ts.map +1 -1
  17. package/dist/VirtualShell/index.js +18 -7
  18. package/dist/VirtualShell/shell.d.ts.map +1 -1
  19. package/dist/VirtualShell/shell.js +3 -1
  20. package/dist/VirtualShell/shellParser.d.ts.map +1 -1
  21. package/dist/VirtualUserManager/index.d.ts.map +1 -1
  22. package/dist/commands/adduser.d.ts +6 -0
  23. package/dist/commands/adduser.d.ts.map +1 -1
  24. package/dist/commands/adduser.js +6 -0
  25. package/dist/commands/alias.d.ts +5 -0
  26. package/dist/commands/alias.d.ts.map +1 -1
  27. package/dist/commands/alias.js +5 -0
  28. package/dist/commands/apt.d.ts +5 -0
  29. package/dist/commands/apt.d.ts.map +1 -1
  30. package/dist/commands/apt.js +32 -9
  31. package/dist/commands/awk.d.ts +11 -0
  32. package/dist/commands/awk.d.ts.map +1 -1
  33. package/dist/commands/awk.js +15 -2
  34. package/dist/commands/base64.d.ts +5 -0
  35. package/dist/commands/base64.d.ts.map +1 -1
  36. package/dist/commands/base64.js +9 -1
  37. package/dist/commands/cat.d.ts +5 -0
  38. package/dist/commands/cat.d.ts.map +1 -1
  39. package/dist/commands/cat.js +10 -2
  40. package/dist/commands/cd.d.ts +5 -0
  41. package/dist/commands/cd.d.ts.map +1 -1
  42. package/dist/commands/cd.js +5 -0
  43. package/dist/commands/chmod.d.ts +5 -0
  44. package/dist/commands/chmod.d.ts.map +1 -1
  45. package/dist/commands/chmod.js +5 -0
  46. package/dist/commands/cp.d.ts +5 -0
  47. package/dist/commands/cp.d.ts.map +1 -1
  48. package/dist/commands/cp.js +5 -0
  49. package/dist/commands/curl.d.ts +5 -0
  50. package/dist/commands/curl.d.ts.map +1 -1
  51. package/dist/commands/curl.js +34 -6
  52. package/dist/commands/cut.d.ts +5 -0
  53. package/dist/commands/cut.d.ts.map +1 -1
  54. package/dist/commands/cut.js +8 -1
  55. package/dist/commands/date.d.ts +5 -0
  56. package/dist/commands/date.d.ts.map +1 -1
  57. package/dist/commands/date.js +7 -1
  58. package/dist/commands/declare.d.ts +3 -0
  59. package/dist/commands/declare.d.ts.map +1 -0
  60. package/dist/commands/declare.js +39 -0
  61. package/dist/commands/diff.d.ts +5 -0
  62. package/dist/commands/diff.d.ts.map +1 -1
  63. package/dist/commands/diff.js +5 -0
  64. package/dist/commands/dpkg.d.ts +5 -0
  65. package/dist/commands/dpkg.d.ts.map +1 -1
  66. package/dist/commands/dpkg.js +24 -7
  67. package/dist/commands/du.d.ts.map +1 -1
  68. package/dist/commands/du.js +8 -2
  69. package/dist/commands/echo.d.ts +5 -0
  70. package/dist/commands/echo.d.ts.map +1 -1
  71. package/dist/commands/echo.js +13 -4
  72. package/dist/commands/env.d.ts +5 -0
  73. package/dist/commands/env.d.ts.map +1 -1
  74. package/dist/commands/env.js +11 -1
  75. package/dist/commands/exit.d.ts +5 -0
  76. package/dist/commands/exit.d.ts.map +1 -1
  77. package/dist/commands/exit.js +12 -2
  78. package/dist/commands/export.d.ts.map +1 -1
  79. package/dist/commands/export.js +3 -1
  80. package/dist/commands/find.d.ts +5 -0
  81. package/dist/commands/find.d.ts.map +1 -1
  82. package/dist/commands/find.js +5 -0
  83. package/dist/commands/free.d.ts +5 -0
  84. package/dist/commands/free.d.ts.map +1 -1
  85. package/dist/commands/free.js +5 -0
  86. package/dist/commands/grep.d.ts +5 -0
  87. package/dist/commands/grep.d.ts.map +1 -1
  88. package/dist/commands/grep.js +12 -2
  89. package/dist/commands/gzip.d.ts +5 -0
  90. package/dist/commands/gzip.d.ts.map +1 -1
  91. package/dist/commands/gzip.js +18 -2
  92. package/dist/commands/head.d.ts +5 -0
  93. package/dist/commands/head.d.ts.map +1 -1
  94. package/dist/commands/head.js +5 -0
  95. package/dist/commands/help.d.ts.map +1 -1
  96. package/dist/commands/help.js +98 -45
  97. package/dist/commands/history.d.ts +5 -0
  98. package/dist/commands/history.d.ts.map +1 -1
  99. package/dist/commands/history.js +5 -0
  100. package/dist/commands/hostname.d.ts +5 -0
  101. package/dist/commands/hostname.d.ts.map +1 -1
  102. package/dist/commands/hostname.js +5 -0
  103. package/dist/commands/id.d.ts.map +1 -1
  104. package/dist/commands/id.js +4 -1
  105. package/dist/commands/index.d.ts +2 -17
  106. package/dist/commands/index.d.ts.map +1 -1
  107. package/dist/commands/index.js +2 -340
  108. package/dist/commands/ls.d.ts.map +1 -1
  109. package/dist/commands/ls.js +3 -1
  110. package/dist/commands/lsb-release.d.ts.map +1 -1
  111. package/dist/commands/lsb-release.js +8 -2
  112. package/dist/commands/nano.js +1 -1
  113. package/dist/commands/neofetch.js +1 -1
  114. package/dist/commands/node.d.ts +9 -0
  115. package/dist/commands/node.d.ts.map +1 -0
  116. package/dist/commands/node.js +316 -0
  117. package/dist/commands/npm.d.ts +19 -0
  118. package/dist/commands/npm.d.ts.map +1 -0
  119. package/dist/commands/npm.js +109 -0
  120. package/dist/commands/ping.d.ts.map +1 -1
  121. package/dist/commands/ping.js +3 -1
  122. package/dist/commands/printf.d.ts +3 -0
  123. package/dist/commands/printf.d.ts.map +1 -0
  124. package/dist/commands/printf.js +113 -0
  125. package/dist/commands/ps.d.ts.map +1 -1
  126. package/dist/commands/ps.js +4 -1
  127. package/dist/commands/python.d.ts +30 -0
  128. package/dist/commands/python.d.ts.map +1 -0
  129. package/dist/commands/python.js +2058 -0
  130. package/dist/commands/read.d.ts +3 -0
  131. package/dist/commands/read.d.ts.map +1 -0
  132. package/dist/commands/read.js +34 -0
  133. package/dist/commands/registry.d.ts +8 -0
  134. package/dist/commands/registry.d.ts.map +1 -0
  135. package/dist/commands/registry.js +229 -0
  136. package/dist/commands/runtime.d.ts +6 -0
  137. package/dist/commands/runtime.d.ts.map +1 -0
  138. package/dist/commands/runtime.js +280 -0
  139. package/dist/commands/sed.d.ts.map +1 -1
  140. package/dist/commands/sed.js +11 -3
  141. package/dist/commands/set.d.ts.map +1 -1
  142. package/dist/commands/set.js +9 -3
  143. package/dist/commands/sh.d.ts.map +1 -1
  144. package/dist/commands/sh.js +57 -36
  145. package/dist/commands/shift.d.ts +5 -0
  146. package/dist/commands/shift.d.ts.map +1 -0
  147. package/dist/commands/shift.js +52 -0
  148. package/dist/commands/sleep.d.ts.map +1 -1
  149. package/dist/commands/sort.d.ts.map +1 -1
  150. package/dist/commands/sort.js +4 -2
  151. package/dist/commands/source.d.ts.map +1 -1
  152. package/dist/commands/source.js +5 -2
  153. package/dist/commands/sudo.js +1 -1
  154. package/dist/commands/tar.d.ts.map +1 -1
  155. package/dist/commands/tar.js +11 -3
  156. package/dist/commands/tee.d.ts.map +1 -1
  157. package/dist/commands/tee.js +8 -6
  158. package/dist/commands/test.d.ts.map +1 -1
  159. package/dist/commands/test.js +46 -24
  160. package/dist/commands/tr.d.ts.map +1 -1
  161. package/dist/commands/tr.js +3 -1
  162. package/dist/commands/true.d.ts +4 -0
  163. package/dist/commands/true.d.ts.map +1 -0
  164. package/dist/commands/true.js +14 -0
  165. package/dist/commands/type.d.ts.map +1 -1
  166. package/dist/commands/type.js +1 -1
  167. package/dist/commands/uname.d.ts.map +1 -1
  168. package/dist/commands/uname.js +4 -1
  169. package/dist/commands/uniq.d.ts.map +1 -1
  170. package/dist/commands/uptime.d.ts.map +1 -1
  171. package/dist/commands/uptime.js +4 -1
  172. package/dist/commands/wget.d.ts.map +1 -1
  173. package/dist/commands/wget.js +32 -7
  174. package/dist/commands/which.d.ts.map +1 -1
  175. package/dist/commands/xargs.d.ts.map +1 -1
  176. package/dist/commands/xargs.js +1 -1
  177. package/dist/index.d.ts +15 -14
  178. package/dist/index.d.ts.map +1 -1
  179. package/dist/index.js +9 -9
  180. package/dist/modules/linuxRootfs.d.ts +18 -1
  181. package/dist/modules/linuxRootfs.d.ts.map +1 -1
  182. package/dist/modules/linuxRootfs.js +160 -17
  183. package/dist/standalone-wo-sftp.d.ts +2 -0
  184. package/dist/standalone-wo-sftp.d.ts.map +1 -0
  185. package/dist/standalone-wo-sftp.js +30 -0
  186. package/dist/utils/expand.d.ts +50 -0
  187. package/dist/utils/expand.d.ts.map +1 -0
  188. package/dist/utils/expand.js +183 -0
  189. package/dist/utils/vfsDiff.d.ts +90 -0
  190. package/dist/utils/vfsDiff.d.ts.map +1 -0
  191. package/dist/utils/vfsDiff.js +177 -0
  192. package/package.json +2 -1
  193. package/src/SSHMimic/exec.ts +10 -1
  194. package/src/SSHMimic/executor.ts +104 -18
  195. package/src/SSHMimic/index.ts +49 -15
  196. package/src/VirtualFileSystem/binaryPack.ts +35 -8
  197. package/src/VirtualFileSystem/index.ts +78 -28
  198. package/src/VirtualPackageManager/index.ts +208 -49
  199. package/src/VirtualShell/index.ts +35 -7
  200. package/src/VirtualShell/shell.ts +23 -3
  201. package/src/VirtualShell/shellParser.ts +134 -36
  202. package/src/VirtualUserManager/index.ts +7 -2
  203. package/src/commands/adduser.ts +6 -0
  204. package/src/commands/alias.ts +5 -1
  205. package/src/commands/apt.ts +47 -17
  206. package/src/commands/awk.ts +20 -6
  207. package/src/commands/base64.ts +13 -2
  208. package/src/commands/cat.ts +13 -5
  209. package/src/commands/cd.ts +5 -0
  210. package/src/commands/chmod.ts +5 -0
  211. package/src/commands/cp.ts +5 -0
  212. package/src/commands/curl.ts +56 -12
  213. package/src/commands/cut.ts +8 -1
  214. package/src/commands/date.ts +7 -1
  215. package/src/commands/declare.ts +44 -0
  216. package/src/commands/diff.ts +17 -3
  217. package/src/commands/dpkg.ts +33 -11
  218. package/src/commands/du.ts +17 -5
  219. package/src/commands/echo.ts +22 -9
  220. package/src/commands/env.ts +11 -1
  221. package/src/commands/exit.ts +12 -2
  222. package/src/commands/export.ts +3 -1
  223. package/src/commands/find.ts +5 -0
  224. package/src/commands/free.ts +9 -2
  225. package/src/commands/grep.ts +12 -2
  226. package/src/commands/gzip.ts +28 -4
  227. package/src/commands/head.ts +5 -0
  228. package/src/commands/help.ts +121 -47
  229. package/src/commands/history.ts +7 -2
  230. package/src/commands/hostname.ts +5 -0
  231. package/src/commands/id.ts +4 -1
  232. package/src/commands/index.ts +9 -360
  233. package/src/commands/ls.ts +5 -3
  234. package/src/commands/lsb-release.ts +8 -2
  235. package/src/commands/nano.ts +1 -1
  236. package/src/commands/neofetch.ts +1 -1
  237. package/src/commands/node.ts +341 -0
  238. package/src/commands/npm.ts +132 -0
  239. package/src/commands/ping.ts +6 -2
  240. package/src/commands/printf.ts +112 -0
  241. package/src/commands/ps.ts +21 -9
  242. package/src/commands/python.ts +2229 -0
  243. package/src/commands/read.ts +41 -0
  244. package/src/commands/registry.ts +244 -0
  245. package/src/commands/runtime.ts +353 -0
  246. package/src/commands/sed.ts +27 -9
  247. package/src/commands/set.ts +9 -3
  248. package/src/commands/sh.ts +159 -55
  249. package/src/commands/shift.ts +53 -0
  250. package/src/commands/sleep.ts +2 -1
  251. package/src/commands/sort.ts +10 -6
  252. package/src/commands/source.ts +15 -3
  253. package/src/commands/sudo.ts +1 -1
  254. package/src/commands/tar.ts +28 -7
  255. package/src/commands/tee.ts +7 -1
  256. package/src/commands/test.ts +61 -26
  257. package/src/commands/tr.ts +3 -1
  258. package/src/commands/true.ts +17 -0
  259. package/src/commands/type.ts +6 -3
  260. package/src/commands/uname.ts +5 -1
  261. package/src/commands/uniq.ts +8 -2
  262. package/src/commands/uptime.ts +4 -1
  263. package/src/commands/wget.ts +51 -12
  264. package/src/commands/which.ts +5 -2
  265. package/src/commands/xargs.ts +11 -2
  266. package/src/index.ts +23 -24
  267. package/src/modules/linuxRootfs.ts +233 -30
  268. package/src/standalone-wo-sftp.ts +38 -0
  269. package/src/utils/expand.ts +238 -0
  270. package/src/utils/vfsDiff.ts +275 -0
  271. package/standalone-wo-sftp.js +507 -0
  272. package/standalone-wo-sftp.js.map +7 -0
  273. package/standalone.js +253 -191
  274. package/standalone.js.map +4 -4
  275. package/tests/bun-test-shim.ts +9 -1
  276. package/tests/command-helpers.test.ts +1 -5
  277. package/tests/new-features.test.ts +415 -5
  278. package/tests/parser-executor.test.ts +27 -27
  279. package/tests/sftp.test.ts +122 -42
  280. package/tests/users.test.ts +23 -5
  281. package/CHANGELOG.md +0 -150
@@ -0,0 +1,341 @@
1
+ /** biome-ignore-all lint/style/useNamingConvention: node globals and ENV VAR KEYS */
2
+ /**
3
+ * node.ts — Virtual Node.js runtime.
4
+ *
5
+ * Uses `node:vm` for sandboxed evaluation with a controlled context that
6
+ * intercepts `process`, `require`, `console`, and all standard globals.
7
+ * No host filesystem access, no network, no child processes.
8
+ */
9
+ import vm from "node:vm";
10
+ import type { ShellModule } from "../types/commands";
11
+ import { ifFlag } from "./command-helpers";
12
+ import { resolvePath } from "./helpers";
13
+
14
+ const VIRTUAL_VERSION = "v18.19.0";
15
+ const VIRTUAL_VERSIONS = {
16
+ node: VIRTUAL_VERSION,
17
+ npm: "9.2.0",
18
+ v8: "10.2.154.26-node.22",
19
+ };
20
+
21
+ // ─── sandbox context ──────────────────────────────────────────────────────────
22
+
23
+ function makeContext(outputLines: string[], stderrLines: string[]) {
24
+ const fakeProcess = {
25
+ version: VIRTUAL_VERSION,
26
+ versions: VIRTUAL_VERSIONS,
27
+ platform: "linux",
28
+ arch: "x64",
29
+ env: {
30
+ NODE_ENV: "production",
31
+ HOME: "/root",
32
+ PATH: "/usr/local/bin:/usr/bin:/bin",
33
+ },
34
+ argv: ["node"],
35
+ stdout: {
36
+ write: (s: string) => {
37
+ outputLines.push(s);
38
+ return true;
39
+ },
40
+ },
41
+ stderr: {
42
+ write: (s: string) => {
43
+ stderrLines.push(s);
44
+ return true;
45
+ },
46
+ },
47
+ exit: (code = 0) => {
48
+ throw new ExitSignal(code);
49
+ },
50
+ cwd: () => "/root",
51
+ hrtime: () => [0, 0],
52
+ };
53
+
54
+ const fakeConsole = {
55
+ log: (...a: unknown[]) => outputLines.push(a.map(formatValue).join(" ")),
56
+ error: (...a: unknown[]) => stderrLines.push(a.map(formatValue).join(" ")),
57
+ warn: (...a: unknown[]) => stderrLines.push(a.map(formatValue).join(" ")),
58
+ info: (...a: unknown[]) => outputLines.push(a.map(formatValue).join(" ")),
59
+ dir: (v: unknown) => outputLines.push(formatValue(v)),
60
+ };
61
+
62
+ const fakeRequire = (mod: string): unknown => {
63
+ // Provide stubs for common modules
64
+ switch (mod) {
65
+ case "path":
66
+ return {
67
+ join: (...parts: string[]) => parts.join("/").replace(/\/+/g, "/"),
68
+ resolve: (...parts: string[]) =>
69
+ `/${parts.join("/").replace(/^\/+/, "")}`,
70
+ dirname: (p: string) => p.split("/").slice(0, -1).join("/") || "/",
71
+ basename: (p: string) => p.split("/").pop() ?? "",
72
+ extname: (p: string) => {
73
+ const b = p.split("/").pop() ?? "";
74
+ const d = b.lastIndexOf(".");
75
+ return d > 0 ? b.slice(d) : "";
76
+ },
77
+ sep: "/",
78
+ delimiter: ":",
79
+ };
80
+ case "os":
81
+ return {
82
+ platform: () => "linux",
83
+ arch: () => "x64",
84
+ type: () => "Linux",
85
+ hostname: () => "fortune-vm",
86
+ homedir: () => "/root",
87
+ tmpdir: () => "/tmp",
88
+ EOL: "\n",
89
+ };
90
+ case "util":
91
+ return {
92
+ format: (...a: unknown[]) => a.map(formatValue).join(" "),
93
+ inspect: (v: unknown) => formatValue(v),
94
+ };
95
+ case "fs":
96
+ case "fs/promises":
97
+ throw new Error(
98
+ `Cannot require '${mod}': filesystem access not available in virtual runtime`,
99
+ );
100
+ case "child_process":
101
+ case "net":
102
+ case "http":
103
+ case "https":
104
+ throw new Error(
105
+ `Cannot require '${mod}': not available in virtual runtime`,
106
+ );
107
+ default:
108
+ throw new Error(`Cannot find module '${mod}'`);
109
+ }
110
+ };
111
+ fakeRequire.resolve = (id: string) => {
112
+ throw new Error(`Cannot resolve '${id}'`);
113
+ };
114
+ fakeRequire.cache = {};
115
+ fakeRequire.extensions = {};
116
+
117
+ return vm.createContext({
118
+ // Core globals
119
+ console: fakeConsole,
120
+ process: fakeProcess,
121
+ require: fakeRequire,
122
+
123
+ // JS built-ins
124
+ Math,
125
+ JSON,
126
+ Object,
127
+ Array,
128
+ String,
129
+ Number,
130
+ Boolean,
131
+ Symbol,
132
+ Date,
133
+ RegExp,
134
+ Error,
135
+ TypeError,
136
+ RangeError,
137
+ SyntaxError,
138
+ Promise,
139
+ Map,
140
+ Set,
141
+ WeakMap,
142
+ WeakSet,
143
+ parseInt,
144
+ parseFloat,
145
+ isNaN,
146
+ isFinite,
147
+ encodeURIComponent,
148
+ decodeURIComponent,
149
+ encodeURI,
150
+ decodeURI,
151
+ setTimeout: () => {},
152
+ clearTimeout: () => {},
153
+ setInterval: () => {},
154
+ clearInterval: () => {},
155
+ queueMicrotask: () => {},
156
+ globalThis: undefined as unknown, // set below
157
+ undefined,
158
+ Infinity,
159
+ NaN,
160
+ });
161
+ }
162
+
163
+ class ExitSignal {
164
+ constructor(public readonly code: number) {}
165
+ }
166
+
167
+ function formatValue(v: unknown): string {
168
+ if (v === null) return "null";
169
+ if (v === undefined) return "undefined";
170
+ if (typeof v === "string") return v;
171
+ if (typeof v === "function") return `[Function: ${v.name || "(anonymous)"}]`;
172
+ if (Array.isArray(v)) return `[ ${v.map(formatValue).join(", ")} ]`;
173
+ if (v instanceof Error) return `${v.name}: ${v.message}`;
174
+ if (typeof v === "object") {
175
+ try {
176
+ const entries = Object.entries(v as Record<string, unknown>)
177
+ .map(([k, val]) => `${k}: ${formatValue(val)}`)
178
+ .join(", ");
179
+ return `{ ${entries} }`;
180
+ } catch {
181
+ return "[Object]";
182
+ }
183
+ }
184
+ return String(v);
185
+ }
186
+
187
+ // ─── execution ────────────────────────────────────────────────────────────────
188
+
189
+ function runJs(code: string): {
190
+ stdout: string;
191
+ stderr: string;
192
+ exitCode: number;
193
+ } {
194
+ const outputLines: string[] = [];
195
+ const stderrLines: string[] = [];
196
+ const ctx = makeContext(outputLines, stderrLines);
197
+
198
+ let exitCode = 0;
199
+
200
+ try {
201
+ const result = vm.runInContext(code, ctx, { timeout: 5000 });
202
+ // If the expression returned a value and nothing was console.log'd, print it
203
+ if (result !== undefined && outputLines.length === 0) {
204
+ outputLines.push(formatValue(result));
205
+ }
206
+ } catch (err) {
207
+ if (err instanceof ExitSignal) {
208
+ exitCode = err.code;
209
+ } else if (err instanceof Error) {
210
+ stderrLines.push(`${err.name}: ${err.message}`);
211
+ exitCode = 1;
212
+ } else {
213
+ stderrLines.push(String(err));
214
+ exitCode = 1;
215
+ }
216
+ }
217
+
218
+ return {
219
+ stdout: outputLines.length ? `${outputLines.join("\n")}\n` : "",
220
+ stderr: stderrLines.length ? `${stderrLines.join("\n")}\n` : "",
221
+ exitCode,
222
+ };
223
+ }
224
+
225
+ function runJsFile(code: string): {
226
+ stdout: string;
227
+ stderr: string;
228
+ exitCode: number;
229
+ } {
230
+ // If the code is a single expression (no semicolons, no newlines, no statements),
231
+ // wrap it to capture the return value like a REPL would
232
+ const trimmed = code.trim();
233
+ const isExpression =
234
+ !trimmed.includes("\n") &&
235
+ !trimmed.startsWith("const ") &&
236
+ !trimmed.startsWith("let ") &&
237
+ !trimmed.startsWith("var ") &&
238
+ !trimmed.startsWith("function ") &&
239
+ !trimmed.startsWith("class ") &&
240
+ !trimmed.startsWith("if ") &&
241
+ !trimmed.startsWith("for ") &&
242
+ !trimmed.startsWith("while ") &&
243
+ !trimmed.startsWith("import ") &&
244
+ !trimmed.startsWith("//");
245
+
246
+ if (isExpression) return runJs(trimmed);
247
+
248
+ // Multi-line: wrap in IIFE
249
+ return runJs(`(async () => { ${code} })()`);
250
+ }
251
+
252
+ // ─── command ──────────────────────────────────────────────────────────────────
253
+ /**
254
+ * `node` virtual runtime command. Executes JS in a safe sandbox with
255
+ * limited globals and no host FS/child process access.
256
+ * @category system
257
+ * @params []
258
+ */
259
+ export const nodeCommand: ShellModule = {
260
+ name: "node",
261
+ description: "JavaScript runtime (virtual)",
262
+ category: "system",
263
+ params: ["[--version] [-e <expr>] [-p <expr>] [file]"],
264
+ run: ({ args, shell, cwd }) => {
265
+ // Require explicit installation via `apt install nodejs`
266
+ if (!shell.packageManager.isInstalled("nodejs")) {
267
+ return {
268
+ stderr:
269
+ "bash: node: command not found\nHint: install it with: apt install nodejs\n",
270
+ exitCode: 127,
271
+ };
272
+ }
273
+ if (ifFlag(args, ["--version", "-v"])) {
274
+ return { stdout: `${VIRTUAL_VERSION}\n`, exitCode: 0 };
275
+ }
276
+
277
+ if (ifFlag(args, ["--versions"])) {
278
+ return {
279
+ stdout: `${JSON.stringify(VIRTUAL_VERSIONS, null, 2)}\n`,
280
+ exitCode: 0,
281
+ };
282
+ }
283
+
284
+ // -e 'expr'
285
+ const eIdx = args.findIndex((a) => a === "-e" || a === "--eval");
286
+ if (eIdx !== -1) {
287
+ const expr = args[eIdx + 1];
288
+ if (!expr)
289
+ return { stderr: "node: -e requires an argument\n", exitCode: 1 };
290
+ const { stdout, stderr, exitCode } = runJs(expr);
291
+ return {
292
+ stdout: stdout || undefined,
293
+ stderr: stderr || undefined,
294
+ exitCode,
295
+ };
296
+ }
297
+
298
+ // -p 'expr' — print mode
299
+ const pIdx = args.findIndex((a) => a === "-p" || a === "--print");
300
+ if (pIdx !== -1) {
301
+ const expr = args[pIdx + 1];
302
+ if (!expr)
303
+ return { stderr: "node: -p requires an argument\n", exitCode: 1 };
304
+ const { stdout, stderr, exitCode } = runJs(expr);
305
+ return {
306
+ stdout: stdout || (exitCode === 0 ? "\n" : undefined),
307
+ stderr: stderr || undefined,
308
+ exitCode,
309
+ };
310
+ }
311
+
312
+ // node <file>
313
+ const file = args.find((a) => !a.startsWith("-"));
314
+ if (file) {
315
+ const filePath = resolvePath(cwd, file);
316
+ if (!shell.vfs.exists(filePath)) {
317
+ return {
318
+ stderr: `node: cannot open file '${file}': No such file or directory\n`,
319
+ exitCode: 1,
320
+ };
321
+ }
322
+ const code = shell.vfs.readFile(filePath);
323
+ const { stdout, stderr, exitCode } = runJsFile(code);
324
+ return {
325
+ stdout: stdout || undefined,
326
+ stderr: stderr || undefined,
327
+ exitCode,
328
+ };
329
+ }
330
+
331
+ // No args — REPL hint
332
+ return {
333
+ stdout: [
334
+ `Welcome to Node.js ${VIRTUAL_VERSION}.`,
335
+ `Type ".exit" to exit the REPL.`,
336
+ `> `,
337
+ ].join("\n"),
338
+ exitCode: 0,
339
+ };
340
+ },
341
+ };
@@ -0,0 +1,132 @@
1
+ /**
2
+ * npm.ts — Virtual npm command.
3
+ * Gated behind `apt install npm`. Provides version info and informative
4
+ * stubs for common subcommands.
5
+ */
6
+ import type { ShellModule } from "../types/commands";
7
+ import { ifFlag } from "./command-helpers";
8
+
9
+ const NPM_VERSION = "9.2.0";
10
+ const NODE_VERSION = "18.19.0";
11
+
12
+ /**
13
+ * `npm` virtual CLI. Requires `apt install npm` in the virtual package manager.
14
+ * @category system
15
+ * @params ["<command> [args]"]
16
+ */
17
+ export const npmCommand: ShellModule = {
18
+ name: "npm",
19
+ description: "Node.js package manager (virtual)",
20
+ category: "system",
21
+ params: ["<command> [args]"],
22
+ run: ({ args, shell }) => {
23
+ // Require explicit installation
24
+ if (!shell.packageManager.isInstalled("npm")) {
25
+ return {
26
+ stderr:
27
+ "bash: npm: command not found\nHint: install it with: apt install npm\n",
28
+ exitCode: 127,
29
+ };
30
+ }
31
+
32
+ if (ifFlag(args, ["--version", "-v"])) {
33
+ return { stdout: `${NPM_VERSION}\n`, exitCode: 0 };
34
+ }
35
+
36
+ const sub = args[0]?.toLowerCase();
37
+
38
+ switch (sub) {
39
+ case "version":
40
+ case "-version":
41
+ return {
42
+ stdout: `{ npm: '${NPM_VERSION}', node: '${NODE_VERSION}', v8: '10.2.154.26' }\n`,
43
+ exitCode: 0,
44
+ };
45
+
46
+ case "install":
47
+ case "i":
48
+ case "add":
49
+ return {
50
+ stderr:
51
+ "npm warn: package installation is not available in the virtual runtime.\nnpm warn: This environment simulates npm CLI behaviour only.\n",
52
+ exitCode: 1,
53
+ };
54
+
55
+ case "run":
56
+ case "exec":
57
+ case "x":
58
+ return {
59
+ stderr: `npm error: script execution is not available in the virtual runtime.\n`,
60
+ exitCode: 1,
61
+ };
62
+
63
+ case "init":
64
+ return {
65
+ stdout: "Wrote to /home/user/package.json\n",
66
+ exitCode: 0,
67
+ };
68
+
69
+ case "list":
70
+ case "ls":
71
+ return {
72
+ stdout: `${sub === "ls" || sub === "list" ? "virtual-env@1.0.0" : ""}\n└── (empty)\n`,
73
+ exitCode: 0,
74
+ };
75
+
76
+ case "help":
77
+ case undefined:
78
+ return {
79
+ stdout: `${[
80
+ `npm ${NPM_VERSION}`,
81
+ "",
82
+ "Usage: npm <command>",
83
+ "",
84
+ "Commands:",
85
+ " install (not available in virtual runtime)",
86
+ " run (not available in virtual runtime)",
87
+ " exec (not available in virtual runtime)",
88
+ " list List installed packages",
89
+ " version Print versions",
90
+ " --version Print npm version",
91
+ ].join("\n")}\n`,
92
+ exitCode: 0,
93
+ };
94
+
95
+ default:
96
+ return {
97
+ stderr: `npm error: unknown command: ${sub}\n`,
98
+ exitCode: 1,
99
+ };
100
+ }
101
+ },
102
+ };
103
+
104
+ /**
105
+ * `npx` virtual runner. Requires `apt install npm` in the virtual package manager.
106
+ * @category system
107
+ * @params ["<package> [args]"]
108
+ */
109
+ export const npxCommand: ShellModule = {
110
+ name: "npx",
111
+ description: "Node.js package runner (virtual)",
112
+ category: "system",
113
+ params: ["<package> [args]"],
114
+ run: ({ args, shell }) => {
115
+ if (!shell.packageManager.isInstalled("npm")) {
116
+ return {
117
+ stderr:
118
+ "bash: npx: command not found\nHint: install it with: apt install npm\n",
119
+ exitCode: 127,
120
+ };
121
+ }
122
+
123
+ if (ifFlag(args, ["--version"])) {
124
+ return { stdout: `${NPM_VERSION}\n`, exitCode: 0 };
125
+ }
126
+
127
+ return {
128
+ stderr: `npx: package execution is not available in the virtual runtime.\n`,
129
+ exitCode: 1,
130
+ };
131
+ },
132
+ };
@@ -7,7 +7,9 @@ export const pingCommand: ShellModule = {
7
7
  category: "network",
8
8
  params: ["[-c <count>] <host>"],
9
9
  run: ({ args }) => {
10
- const { flagsWithValues, positionals } = parseArgs(args, { flagsWithValue: ["-c", "-i", "-W"] });
10
+ const { flagsWithValues, positionals } = parseArgs(args, {
11
+ flagsWithValue: ["-c", "-i", "-W"],
12
+ });
11
13
  const host = positionals[0] ?? "localhost";
12
14
  const countArg = flagsWithValues.get("-c");
13
15
  const count = countArg ? Math.max(1, parseInt(countArg, 10) || 4) : 4;
@@ -17,7 +19,9 @@ export const pingCommand: ShellModule = {
17
19
  lines.push(`64 bytes from ${host}: icmp_seq=${i} ttl=64 time=${ms} ms`);
18
20
  }
19
21
  lines.push(`--- ${host} ping statistics ---`);
20
- lines.push(`${count} packets transmitted, ${count} received, 0% packet loss`);
22
+ lines.push(
23
+ `${count} packets transmitted, ${count} received, 0% packet loss`,
24
+ );
21
25
  return { stdout: lines.join("\n"), exitCode: 0 };
22
26
  },
23
27
  };
@@ -0,0 +1,112 @@
1
+ import type { ShellModule } from "../types/commands";
2
+
3
+ /**
4
+ * Render a printf format string with given arguments.
5
+ * Supports: %s %d %i %f %o %x %X %% \n \t \r \\
6
+ */
7
+ function renderPrintf(fmt: string, args: string[]): string {
8
+ let argIdx = 0;
9
+ let out = "";
10
+ let i = 0;
11
+ while (i < fmt.length) {
12
+ if (fmt[i] === "\\" && i + 1 < fmt.length) {
13
+ switch (fmt[i + 1]) {
14
+ case "n":
15
+ out += "\n";
16
+ i += 2;
17
+ continue;
18
+ case "t":
19
+ out += "\t";
20
+ i += 2;
21
+ continue;
22
+ case "r":
23
+ out += "\r";
24
+ i += 2;
25
+ continue;
26
+ case "\\":
27
+ out += "\\";
28
+ i += 2;
29
+ continue;
30
+ case "a":
31
+ out += "\x07";
32
+ i += 2;
33
+ continue;
34
+ case "b":
35
+ out += "\x08";
36
+ i += 2;
37
+ continue;
38
+ case "f":
39
+ out += "\x0C";
40
+ i += 2;
41
+ continue;
42
+ case "v":
43
+ out += "\x0B";
44
+ i += 2;
45
+ continue;
46
+ default:
47
+ out += fmt[i]!;
48
+ i++;
49
+ continue;
50
+ }
51
+ }
52
+ if (fmt[i] === "%" && i + 1 < fmt.length) {
53
+ // Optional width/precision: %[-][width][.prec]spec
54
+ let j = i + 1;
55
+ if (fmt[j] === "-") j++;
56
+ while (j < fmt.length && /\d/.test(fmt[j]!)) j++;
57
+ if (fmt[j] === ".") {
58
+ j++;
59
+ while (j < fmt.length && /\d/.test(fmt[j]!)) j++;
60
+ }
61
+ const spec = fmt[j];
62
+ const arg = args[argIdx++] ?? "";
63
+ switch (spec) {
64
+ case "s":
65
+ out += arg;
66
+ break;
67
+ case "d":
68
+ case "i":
69
+ out += String(parseInt(arg, 10) || 0);
70
+ break;
71
+ case "f":
72
+ out += String(parseFloat(arg) || 0);
73
+ break;
74
+ case "o":
75
+ out += (parseInt(arg, 10) || 0).toString(8);
76
+ break;
77
+ case "x":
78
+ out += (parseInt(arg, 10) || 0).toString(16);
79
+ break;
80
+ case "X":
81
+ out += (parseInt(arg, 10) || 0).toString(16).toUpperCase();
82
+ break;
83
+ case "%":
84
+ out += "%";
85
+ argIdx--;
86
+ break;
87
+ default:
88
+ out += fmt[i]!;
89
+ i++;
90
+ continue;
91
+ }
92
+ i = j + 1;
93
+ continue;
94
+ }
95
+ out += fmt[i]!;
96
+ i++;
97
+ }
98
+ return out;
99
+ }
100
+
101
+ export const printfCommand: ShellModule = {
102
+ name: "printf",
103
+ description: "Format and print data",
104
+ category: "shell",
105
+ params: ["<format> [args...]"],
106
+ run: ({ args }) => {
107
+ const fmt = args[0];
108
+ if (!fmt) return { stderr: "printf: missing format string", exitCode: 1 };
109
+ const output = renderPrintf(fmt, args.slice(1));
110
+ return { stdout: output, exitCode: 0 };
111
+ },
112
+ };
@@ -8,22 +8,32 @@ export const psCommand: ShellModule = {
8
8
  params: ["[-a] [-u] [-x] [aux]"],
9
9
  run: ({ authUser, shell, args }) => {
10
10
  const sessions = shell.users.listActiveSessions();
11
- const showUser = ifFlag(args, ["-u"]) || args.includes("u") || args.includes("aux") || args.includes("au");
12
- const showAll = ifFlag(args, ["-a", "-x"]) || args.includes("a") || args.includes("aux");
11
+ const showUser =
12
+ ifFlag(args, ["-u"]) ||
13
+ args.includes("u") ||
14
+ args.includes("aux") ||
15
+ args.includes("au");
16
+ const showAll =
17
+ ifFlag(args, ["-a", "-x"]) || args.includes("a") || args.includes("aux");
13
18
 
14
19
  if (showUser) {
15
- const header = "USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND";
20
+ const header =
21
+ "USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND";
16
22
  const rows: string[] = [header];
17
23
  let pid = 1000;
18
24
  for (const s of sessions) {
19
25
  const user = s.username.padEnd(10).slice(0, 10);
20
- const mem = (Math.random() * 0.5).toFixed(1);
21
- const vsz = Math.floor(Math.random() * 20000 + 5000);
22
- const rss = Math.floor(Math.random() * 5000 + 1000);
23
- rows.push(`${user} ${String(pid).padStart(6)} 0.0 ${mem.padStart(4)} ${String(vsz).padStart(6)} ${String(rss).padStart(5)} ${s.tty.padEnd(8)} Ss 00:00 0:00 bash`);
26
+ const mem = (Math.random() * 0.5).toFixed(1);
27
+ const vsz = Math.floor(Math.random() * 20000 + 5000);
28
+ const rss = Math.floor(Math.random() * 5000 + 1000);
29
+ rows.push(
30
+ `${user} ${String(pid).padStart(6)} 0.0 ${mem.padStart(4)} ${String(vsz).padStart(6)} ${String(rss).padStart(5)} ${s.tty.padEnd(8)} Ss 00:00 0:00 bash`,
31
+ );
24
32
  pid++;
25
33
  }
26
- rows.push(`root ${String(pid).padStart(6)} 0.0 0.0 0 0 ? S 00:00 0:00 ps`);
34
+ rows.push(
35
+ `root ${String(pid).padStart(6)} 0.0 0.0 0 0 ? S 00:00 0:00 ps`,
36
+ );
27
37
  return { stdout: rows.join("\n"), exitCode: 0 };
28
38
  }
29
39
 
@@ -32,7 +42,9 @@ export const psCommand: ShellModule = {
32
42
  let pid = 1000;
33
43
  for (const s of sessions) {
34
44
  if (!showAll && s.username !== authUser) continue;
35
- rows.push(`${String(pid).padStart(5)} ${s.tty.padEnd(12)} 00:00:00 ${s.username === authUser ? "bash" : `bash (${s.username})`}`);
45
+ rows.push(
46
+ `${String(pid).padStart(5)} ${s.tty.padEnd(12)} 00:00:00 ${s.username === authUser ? "bash" : `bash (${s.username})`}`,
47
+ );
36
48
  pid++;
37
49
  }
38
50
  rows.push(`${String(pid).padStart(5)} pts/0 00:00:00 ps`);