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,316 @@
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 { ifFlag } from "./command-helpers";
11
+ import { resolvePath } from "./helpers";
12
+ const VIRTUAL_VERSION = "v18.19.0";
13
+ const VIRTUAL_VERSIONS = {
14
+ node: VIRTUAL_VERSION,
15
+ npm: "9.2.0",
16
+ v8: "10.2.154.26-node.22",
17
+ };
18
+ // ─── sandbox context ──────────────────────────────────────────────────────────
19
+ function makeContext(outputLines, stderrLines) {
20
+ const fakeProcess = {
21
+ version: VIRTUAL_VERSION,
22
+ versions: VIRTUAL_VERSIONS,
23
+ platform: "linux",
24
+ arch: "x64",
25
+ env: {
26
+ NODE_ENV: "production",
27
+ HOME: "/root",
28
+ PATH: "/usr/local/bin:/usr/bin:/bin",
29
+ },
30
+ argv: ["node"],
31
+ stdout: {
32
+ write: (s) => {
33
+ outputLines.push(s);
34
+ return true;
35
+ },
36
+ },
37
+ stderr: {
38
+ write: (s) => {
39
+ stderrLines.push(s);
40
+ return true;
41
+ },
42
+ },
43
+ exit: (code = 0) => {
44
+ throw new ExitSignal(code);
45
+ },
46
+ cwd: () => "/root",
47
+ hrtime: () => [0, 0],
48
+ };
49
+ const fakeConsole = {
50
+ log: (...a) => outputLines.push(a.map(formatValue).join(" ")),
51
+ error: (...a) => stderrLines.push(a.map(formatValue).join(" ")),
52
+ warn: (...a) => stderrLines.push(a.map(formatValue).join(" ")),
53
+ info: (...a) => outputLines.push(a.map(formatValue).join(" ")),
54
+ dir: (v) => outputLines.push(formatValue(v)),
55
+ };
56
+ const fakeRequire = (mod) => {
57
+ // Provide stubs for common modules
58
+ switch (mod) {
59
+ case "path":
60
+ return {
61
+ join: (...parts) => parts.join("/").replace(/\/+/g, "/"),
62
+ resolve: (...parts) => `/${parts.join("/").replace(/^\/+/, "")}`,
63
+ dirname: (p) => p.split("/").slice(0, -1).join("/") || "/",
64
+ basename: (p) => p.split("/").pop() ?? "",
65
+ extname: (p) => {
66
+ const b = p.split("/").pop() ?? "";
67
+ const d = b.lastIndexOf(".");
68
+ return d > 0 ? b.slice(d) : "";
69
+ },
70
+ sep: "/",
71
+ delimiter: ":",
72
+ };
73
+ case "os":
74
+ return {
75
+ platform: () => "linux",
76
+ arch: () => "x64",
77
+ type: () => "Linux",
78
+ hostname: () => "fortune-vm",
79
+ homedir: () => "/root",
80
+ tmpdir: () => "/tmp",
81
+ EOL: "\n",
82
+ };
83
+ case "util":
84
+ return {
85
+ format: (...a) => a.map(formatValue).join(" "),
86
+ inspect: (v) => formatValue(v),
87
+ };
88
+ case "fs":
89
+ case "fs/promises":
90
+ throw new Error(`Cannot require '${mod}': filesystem access not available in virtual runtime`);
91
+ case "child_process":
92
+ case "net":
93
+ case "http":
94
+ case "https":
95
+ throw new Error(`Cannot require '${mod}': not available in virtual runtime`);
96
+ default:
97
+ throw new Error(`Cannot find module '${mod}'`);
98
+ }
99
+ };
100
+ fakeRequire.resolve = (id) => {
101
+ throw new Error(`Cannot resolve '${id}'`);
102
+ };
103
+ fakeRequire.cache = {};
104
+ fakeRequire.extensions = {};
105
+ return vm.createContext({
106
+ // Core globals
107
+ console: fakeConsole,
108
+ process: fakeProcess,
109
+ require: fakeRequire,
110
+ // JS built-ins
111
+ Math,
112
+ JSON,
113
+ Object,
114
+ Array,
115
+ String,
116
+ Number,
117
+ Boolean,
118
+ Symbol,
119
+ Date,
120
+ RegExp,
121
+ Error,
122
+ TypeError,
123
+ RangeError,
124
+ SyntaxError,
125
+ Promise,
126
+ Map,
127
+ Set,
128
+ WeakMap,
129
+ WeakSet,
130
+ parseInt,
131
+ parseFloat,
132
+ isNaN,
133
+ isFinite,
134
+ encodeURIComponent,
135
+ decodeURIComponent,
136
+ encodeURI,
137
+ decodeURI,
138
+ setTimeout: () => { },
139
+ clearTimeout: () => { },
140
+ setInterval: () => { },
141
+ clearInterval: () => { },
142
+ queueMicrotask: () => { },
143
+ globalThis: undefined, // set below
144
+ undefined,
145
+ Infinity,
146
+ NaN,
147
+ });
148
+ }
149
+ class ExitSignal {
150
+ code;
151
+ constructor(code) {
152
+ this.code = code;
153
+ }
154
+ }
155
+ function formatValue(v) {
156
+ if (v === null)
157
+ return "null";
158
+ if (v === undefined)
159
+ return "undefined";
160
+ if (typeof v === "string")
161
+ return v;
162
+ if (typeof v === "function")
163
+ return `[Function: ${v.name || "(anonymous)"}]`;
164
+ if (Array.isArray(v))
165
+ return `[ ${v.map(formatValue).join(", ")} ]`;
166
+ if (v instanceof Error)
167
+ return `${v.name}: ${v.message}`;
168
+ if (typeof v === "object") {
169
+ try {
170
+ const entries = Object.entries(v)
171
+ .map(([k, val]) => `${k}: ${formatValue(val)}`)
172
+ .join(", ");
173
+ return `{ ${entries} }`;
174
+ }
175
+ catch {
176
+ return "[Object]";
177
+ }
178
+ }
179
+ return String(v);
180
+ }
181
+ // ─── execution ────────────────────────────────────────────────────────────────
182
+ function runJs(code) {
183
+ const outputLines = [];
184
+ const stderrLines = [];
185
+ const ctx = makeContext(outputLines, stderrLines);
186
+ let exitCode = 0;
187
+ try {
188
+ const result = vm.runInContext(code, ctx, { timeout: 5000 });
189
+ // If the expression returned a value and nothing was console.log'd, print it
190
+ if (result !== undefined && outputLines.length === 0) {
191
+ outputLines.push(formatValue(result));
192
+ }
193
+ }
194
+ catch (err) {
195
+ if (err instanceof ExitSignal) {
196
+ exitCode = err.code;
197
+ }
198
+ else if (err instanceof Error) {
199
+ stderrLines.push(`${err.name}: ${err.message}`);
200
+ exitCode = 1;
201
+ }
202
+ else {
203
+ stderrLines.push(String(err));
204
+ exitCode = 1;
205
+ }
206
+ }
207
+ return {
208
+ stdout: outputLines.length ? `${outputLines.join("\n")}\n` : "",
209
+ stderr: stderrLines.length ? `${stderrLines.join("\n")}\n` : "",
210
+ exitCode,
211
+ };
212
+ }
213
+ function runJsFile(code) {
214
+ // If the code is a single expression (no semicolons, no newlines, no statements),
215
+ // wrap it to capture the return value like a REPL would
216
+ const trimmed = code.trim();
217
+ const isExpression = !trimmed.includes("\n") &&
218
+ !trimmed.startsWith("const ") &&
219
+ !trimmed.startsWith("let ") &&
220
+ !trimmed.startsWith("var ") &&
221
+ !trimmed.startsWith("function ") &&
222
+ !trimmed.startsWith("class ") &&
223
+ !trimmed.startsWith("if ") &&
224
+ !trimmed.startsWith("for ") &&
225
+ !trimmed.startsWith("while ") &&
226
+ !trimmed.startsWith("import ") &&
227
+ !trimmed.startsWith("//");
228
+ if (isExpression)
229
+ return runJs(trimmed);
230
+ // Multi-line: wrap in IIFE
231
+ return runJs(`(async () => { ${code} })()`);
232
+ }
233
+ // ─── command ──────────────────────────────────────────────────────────────────
234
+ /**
235
+ * `node` virtual runtime command. Executes JS in a safe sandbox with
236
+ * limited globals and no host FS/child process access.
237
+ * @category system
238
+ * @params []
239
+ */
240
+ export const nodeCommand = {
241
+ name: "node",
242
+ description: "JavaScript runtime (virtual)",
243
+ category: "system",
244
+ params: ["[--version] [-e <expr>] [-p <expr>] [file]"],
245
+ run: ({ args, shell, cwd }) => {
246
+ // Require explicit installation via `apt install nodejs`
247
+ if (!shell.packageManager.isInstalled("nodejs")) {
248
+ return {
249
+ stderr: "bash: node: command not found\nHint: install it with: apt install nodejs\n",
250
+ exitCode: 127,
251
+ };
252
+ }
253
+ if (ifFlag(args, ["--version", "-v"])) {
254
+ return { stdout: `${VIRTUAL_VERSION}\n`, exitCode: 0 };
255
+ }
256
+ if (ifFlag(args, ["--versions"])) {
257
+ return {
258
+ stdout: `${JSON.stringify(VIRTUAL_VERSIONS, null, 2)}\n`,
259
+ exitCode: 0,
260
+ };
261
+ }
262
+ // -e 'expr'
263
+ const eIdx = args.findIndex((a) => a === "-e" || a === "--eval");
264
+ if (eIdx !== -1) {
265
+ const expr = args[eIdx + 1];
266
+ if (!expr)
267
+ return { stderr: "node: -e requires an argument\n", exitCode: 1 };
268
+ const { stdout, stderr, exitCode } = runJs(expr);
269
+ return {
270
+ stdout: stdout || undefined,
271
+ stderr: stderr || undefined,
272
+ exitCode,
273
+ };
274
+ }
275
+ // -p 'expr' — print mode
276
+ const pIdx = args.findIndex((a) => a === "-p" || a === "--print");
277
+ if (pIdx !== -1) {
278
+ const expr = args[pIdx + 1];
279
+ if (!expr)
280
+ return { stderr: "node: -p requires an argument\n", exitCode: 1 };
281
+ const { stdout, stderr, exitCode } = runJs(expr);
282
+ return {
283
+ stdout: stdout || (exitCode === 0 ? "\n" : undefined),
284
+ stderr: stderr || undefined,
285
+ exitCode,
286
+ };
287
+ }
288
+ // node <file>
289
+ const file = args.find((a) => !a.startsWith("-"));
290
+ if (file) {
291
+ const filePath = resolvePath(cwd, file);
292
+ if (!shell.vfs.exists(filePath)) {
293
+ return {
294
+ stderr: `node: cannot open file '${file}': No such file or directory\n`,
295
+ exitCode: 1,
296
+ };
297
+ }
298
+ const code = shell.vfs.readFile(filePath);
299
+ const { stdout, stderr, exitCode } = runJsFile(code);
300
+ return {
301
+ stdout: stdout || undefined,
302
+ stderr: stderr || undefined,
303
+ exitCode,
304
+ };
305
+ }
306
+ // No args — REPL hint
307
+ return {
308
+ stdout: [
309
+ `Welcome to Node.js ${VIRTUAL_VERSION}.`,
310
+ `Type ".exit" to exit the REPL.`,
311
+ `> `,
312
+ ].join("\n"),
313
+ exitCode: 0,
314
+ };
315
+ },
316
+ };
@@ -0,0 +1,19 @@
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
+ /**
8
+ * `npm` virtual CLI. Requires `apt install npm` in the virtual package manager.
9
+ * @category system
10
+ * @params ["<command> [args]"]
11
+ */
12
+ export declare const npmCommand: ShellModule;
13
+ /**
14
+ * `npx` virtual runner. Requires `apt install npm` in the virtual package manager.
15
+ * @category system
16
+ * @params ["<package> [args]"]
17
+ */
18
+ export declare const npxCommand: ShellModule;
19
+ //# sourceMappingURL=npm.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"npm.d.ts","sourceRoot":"","sources":["../../src/commands/npm.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAMrD;;;;GAIG;AACH,eAAO,MAAM,UAAU,EAAE,WAqFxB,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,UAAU,EAAE,WAuBxB,CAAC"}
@@ -0,0 +1,109 @@
1
+ import { ifFlag } from "./command-helpers";
2
+ const NPM_VERSION = "9.2.0";
3
+ const NODE_VERSION = "18.19.0";
4
+ /**
5
+ * `npm` virtual CLI. Requires `apt install npm` in the virtual package manager.
6
+ * @category system
7
+ * @params ["<command> [args]"]
8
+ */
9
+ export const npmCommand = {
10
+ name: "npm",
11
+ description: "Node.js package manager (virtual)",
12
+ category: "system",
13
+ params: ["<command> [args]"],
14
+ run: ({ args, shell }) => {
15
+ // Require explicit installation
16
+ if (!shell.packageManager.isInstalled("npm")) {
17
+ return {
18
+ stderr: "bash: npm: command not found\nHint: install it with: apt install npm\n",
19
+ exitCode: 127,
20
+ };
21
+ }
22
+ if (ifFlag(args, ["--version", "-v"])) {
23
+ return { stdout: `${NPM_VERSION}\n`, exitCode: 0 };
24
+ }
25
+ const sub = args[0]?.toLowerCase();
26
+ switch (sub) {
27
+ case "version":
28
+ case "-version":
29
+ return {
30
+ stdout: `{ npm: '${NPM_VERSION}', node: '${NODE_VERSION}', v8: '10.2.154.26' }\n`,
31
+ exitCode: 0,
32
+ };
33
+ case "install":
34
+ case "i":
35
+ case "add":
36
+ return {
37
+ stderr: "npm warn: package installation is not available in the virtual runtime.\nnpm warn: This environment simulates npm CLI behaviour only.\n",
38
+ exitCode: 1,
39
+ };
40
+ case "run":
41
+ case "exec":
42
+ case "x":
43
+ return {
44
+ stderr: `npm error: script execution is not available in the virtual runtime.\n`,
45
+ exitCode: 1,
46
+ };
47
+ case "init":
48
+ return {
49
+ stdout: "Wrote to /home/user/package.json\n",
50
+ exitCode: 0,
51
+ };
52
+ case "list":
53
+ case "ls":
54
+ return {
55
+ stdout: `${sub === "ls" || sub === "list" ? "virtual-env@1.0.0" : ""}\n└── (empty)\n`,
56
+ exitCode: 0,
57
+ };
58
+ case "help":
59
+ case undefined:
60
+ return {
61
+ stdout: `${[
62
+ `npm ${NPM_VERSION}`,
63
+ "",
64
+ "Usage: npm <command>",
65
+ "",
66
+ "Commands:",
67
+ " install (not available in virtual runtime)",
68
+ " run (not available in virtual runtime)",
69
+ " exec (not available in virtual runtime)",
70
+ " list List installed packages",
71
+ " version Print versions",
72
+ " --version Print npm version",
73
+ ].join("\n")}\n`,
74
+ exitCode: 0,
75
+ };
76
+ default:
77
+ return {
78
+ stderr: `npm error: unknown command: ${sub}\n`,
79
+ exitCode: 1,
80
+ };
81
+ }
82
+ },
83
+ };
84
+ /**
85
+ * `npx` virtual runner. Requires `apt install npm` in the virtual package manager.
86
+ * @category system
87
+ * @params ["<package> [args]"]
88
+ */
89
+ export const npxCommand = {
90
+ name: "npx",
91
+ description: "Node.js package runner (virtual)",
92
+ category: "system",
93
+ params: ["<package> [args]"],
94
+ run: ({ args, shell }) => {
95
+ if (!shell.packageManager.isInstalled("npm")) {
96
+ return {
97
+ stderr: "bash: npx: command not found\nHint: install it with: apt install npm\n",
98
+ exitCode: 127,
99
+ };
100
+ }
101
+ if (ifFlag(args, ["--version"])) {
102
+ return { stdout: `${NPM_VERSION}\n`, exitCode: 0 };
103
+ }
104
+ return {
105
+ stderr: `npx: package execution is not available in the virtual runtime.\n`,
106
+ exitCode: 1,
107
+ };
108
+ },
109
+ };
@@ -1 +1 @@
1
- {"version":3,"file":"ping.d.ts","sourceRoot":"","sources":["../../src/commands/ping.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGrD,eAAO,MAAM,WAAW,EAAE,WAmBzB,CAAC"}
1
+ {"version":3,"file":"ping.d.ts","sourceRoot":"","sources":["../../src/commands/ping.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGrD,eAAO,MAAM,WAAW,EAAE,WAuBzB,CAAC"}
@@ -5,7 +5,9 @@ export const pingCommand = {
5
5
  category: "network",
6
6
  params: ["[-c <count>] <host>"],
7
7
  run: ({ args }) => {
8
- const { flagsWithValues, positionals } = parseArgs(args, { flagsWithValue: ["-c", "-i", "-W"] });
8
+ const { flagsWithValues, positionals } = parseArgs(args, {
9
+ flagsWithValue: ["-c", "-i", "-W"],
10
+ });
9
11
  const host = positionals[0] ?? "localhost";
10
12
  const countArg = flagsWithValues.get("-c");
11
13
  const count = countArg ? Math.max(1, parseInt(countArg, 10) || 4) : 4;
@@ -0,0 +1,3 @@
1
+ import type { ShellModule } from "../types/commands";
2
+ export declare const printfCommand: ShellModule;
3
+ //# sourceMappingURL=printf.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"printf.d.ts","sourceRoot":"","sources":["../../src/commands/printf.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAoGrD,eAAO,MAAM,aAAa,EAAE,WAW3B,CAAC"}
@@ -0,0 +1,113 @@
1
+ /**
2
+ * Render a printf format string with given arguments.
3
+ * Supports: %s %d %i %f %o %x %X %% \n \t \r \\
4
+ */
5
+ function renderPrintf(fmt, args) {
6
+ let argIdx = 0;
7
+ let out = "";
8
+ let i = 0;
9
+ while (i < fmt.length) {
10
+ if (fmt[i] === "\\" && i + 1 < fmt.length) {
11
+ switch (fmt[i + 1]) {
12
+ case "n":
13
+ out += "\n";
14
+ i += 2;
15
+ continue;
16
+ case "t":
17
+ out += "\t";
18
+ i += 2;
19
+ continue;
20
+ case "r":
21
+ out += "\r";
22
+ i += 2;
23
+ continue;
24
+ case "\\":
25
+ out += "\\";
26
+ i += 2;
27
+ continue;
28
+ case "a":
29
+ out += "\x07";
30
+ i += 2;
31
+ continue;
32
+ case "b":
33
+ out += "\x08";
34
+ i += 2;
35
+ continue;
36
+ case "f":
37
+ out += "\x0C";
38
+ i += 2;
39
+ continue;
40
+ case "v":
41
+ out += "\x0B";
42
+ i += 2;
43
+ continue;
44
+ default:
45
+ out += fmt[i];
46
+ i++;
47
+ continue;
48
+ }
49
+ }
50
+ if (fmt[i] === "%" && i + 1 < fmt.length) {
51
+ // Optional width/precision: %[-][width][.prec]spec
52
+ let j = i + 1;
53
+ if (fmt[j] === "-")
54
+ j++;
55
+ while (j < fmt.length && /\d/.test(fmt[j]))
56
+ j++;
57
+ if (fmt[j] === ".") {
58
+ j++;
59
+ while (j < fmt.length && /\d/.test(fmt[j]))
60
+ j++;
61
+ }
62
+ const spec = fmt[j];
63
+ const arg = args[argIdx++] ?? "";
64
+ switch (spec) {
65
+ case "s":
66
+ out += arg;
67
+ break;
68
+ case "d":
69
+ case "i":
70
+ out += String(parseInt(arg, 10) || 0);
71
+ break;
72
+ case "f":
73
+ out += String(parseFloat(arg) || 0);
74
+ break;
75
+ case "o":
76
+ out += (parseInt(arg, 10) || 0).toString(8);
77
+ break;
78
+ case "x":
79
+ out += (parseInt(arg, 10) || 0).toString(16);
80
+ break;
81
+ case "X":
82
+ out += (parseInt(arg, 10) || 0).toString(16).toUpperCase();
83
+ break;
84
+ case "%":
85
+ out += "%";
86
+ argIdx--;
87
+ break;
88
+ default:
89
+ out += fmt[i];
90
+ i++;
91
+ continue;
92
+ }
93
+ i = j + 1;
94
+ continue;
95
+ }
96
+ out += fmt[i];
97
+ i++;
98
+ }
99
+ return out;
100
+ }
101
+ export const printfCommand = {
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)
109
+ return { stderr: "printf: missing format string", exitCode: 1 };
110
+ const output = renderPrintf(fmt, args.slice(1));
111
+ return { stdout: output, exitCode: 0 };
112
+ },
113
+ };
@@ -1 +1 @@
1
- {"version":3,"file":"ps.d.ts","sourceRoot":"","sources":["../../src/commands/ps.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGrD,eAAO,MAAM,SAAS,EAAE,WAqCvB,CAAC"}
1
+ {"version":3,"file":"ps.d.ts","sourceRoot":"","sources":["../../src/commands/ps.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGrD,eAAO,MAAM,SAAS,EAAE,WAiDvB,CAAC"}
@@ -6,7 +6,10 @@ export const psCommand = {
6
6
  params: ["[-a] [-u] [-x] [aux]"],
7
7
  run: ({ authUser, shell, args }) => {
8
8
  const sessions = shell.users.listActiveSessions();
9
- const showUser = ifFlag(args, ["-u"]) || args.includes("u") || args.includes("aux") || args.includes("au");
9
+ const showUser = ifFlag(args, ["-u"]) ||
10
+ args.includes("u") ||
11
+ args.includes("aux") ||
12
+ args.includes("au");
10
13
  const showAll = ifFlag(args, ["-a", "-x"]) || args.includes("a") || args.includes("aux");
11
14
  if (showUser) {
12
15
  const header = "USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND";
@@ -0,0 +1,30 @@
1
+ /** biome-ignore-all lint/suspicious/useIterableCallbackReturn: intentional side-effect forEach */
2
+ /**
3
+ * python.ts — Virtual Python 3 interpreter.
4
+ *
5
+ * Implements a genuine mini-interpreter capable of:
6
+ * - print(), len(), type(), range(), list(), str(), int(), float(), bool()
7
+ * - max(), min(), abs(), sum(), sorted(), reversed(), enumerate(), zip()
8
+ * - str methods: upper(), lower(), strip(), split(), join(), replace(),
9
+ * startswith(), endswith(), format(), count(), find()
10
+ * - list methods: append(), extend(), pop(), sort(), reverse(), index()
11
+ * - dict: keys(), values(), items(), get()
12
+ * - import os, sys, math, json, re (stubs)
13
+ * - f-strings, multi-line scripts, assignments, for/while loops, if/elif/else
14
+ * - functions (def), return, class basics
15
+ */
16
+ import type { ShellModule } from "../types/commands";
17
+ declare global {
18
+ interface Map<K, V> {
19
+ also?: ((fn: (m: Map<K, V>) => void) => Map<K, V>) | undefined;
20
+ }
21
+ }
22
+ /**
23
+ * Virtual Python 3 interpreter command. Implements a small Python subset
24
+ * for scripts and `-c` invocations. Requires `apt install python3` in the
25
+ * virtual package manager to be available.
26
+ * @category system
27
+ * @params ["[--version] [-c <code>] [-V] [file]"]
28
+ */
29
+ export declare const python3Command: ShellModule;
30
+ //# sourceMappingURL=python.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"python.d.ts","sourceRoot":"","sources":["../../src/commands/python.ts"],"names":[],"mappings":"AAAA,kGAAkG;AAClG;;;;;;;;;;;;;GAaG;AACH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAqlErD,OAAO,CAAC,MAAM,CAAC;IACd,UAAU,GAAG,CAAC,CAAC,EAAE,CAAC;QACjB,IAAI,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,IAAI,KAAK,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC;KAC/D;CACD;AAID;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,EAAE,WAiE5B,CAAC"}