typescript-virtual-container 1.2.4 → 1.2.6

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 (267) hide show
  1. package/README.md +1056 -1239
  2. package/benchmark-results.txt +20 -20
  3. package/dist/SSHMimic/exec.js +2 -2
  4. package/dist/SSHMimic/executor.d.ts +6 -7
  5. package/dist/SSHMimic/executor.d.ts.map +1 -1
  6. package/dist/SSHMimic/executor.js +77 -60
  7. package/dist/SSHMimic/index.d.ts +19 -2
  8. package/dist/SSHMimic/index.d.ts.map +1 -1
  9. package/dist/SSHMimic/index.js +106 -24
  10. package/dist/SSHMimic/sftp.d.ts.map +1 -1
  11. package/dist/SSHMimic/sftp.js +14 -0
  12. package/dist/VirtualFileSystem/index.d.ts +115 -88
  13. package/dist/VirtualFileSystem/index.d.ts.map +1 -1
  14. package/dist/VirtualFileSystem/index.js +389 -264
  15. package/dist/VirtualShell/index.d.ts +3 -4
  16. package/dist/VirtualShell/index.d.ts.map +1 -1
  17. package/dist/VirtualShell/index.js +4 -6
  18. package/dist/VirtualShell/shell.d.ts.map +1 -1
  19. package/dist/VirtualShell/shell.js +19 -2
  20. package/dist/VirtualShell/shellParser.d.ts +20 -2
  21. package/dist/VirtualShell/shellParser.d.ts.map +1 -1
  22. package/dist/VirtualShell/shellParser.js +229 -120
  23. package/dist/VirtualUserManager/index.d.ts +25 -0
  24. package/dist/VirtualUserManager/index.d.ts.map +1 -1
  25. package/dist/VirtualUserManager/index.js +33 -0
  26. package/dist/commands/adduser.d.ts.map +1 -1
  27. package/dist/commands/adduser.js +2 -0
  28. package/dist/commands/awk.d.ts +3 -0
  29. package/dist/commands/awk.d.ts.map +1 -0
  30. package/dist/commands/awk.js +29 -0
  31. package/dist/commands/base64.d.ts +3 -0
  32. package/dist/commands/base64.d.ts.map +1 -0
  33. package/dist/commands/base64.js +20 -0
  34. package/dist/commands/cat.d.ts.map +1 -1
  35. package/dist/commands/cat.js +2 -0
  36. package/dist/commands/cd.d.ts.map +1 -1
  37. package/dist/commands/cd.js +2 -0
  38. package/dist/commands/chmod.d.ts +3 -0
  39. package/dist/commands/chmod.d.ts.map +1 -0
  40. package/dist/commands/chmod.js +33 -0
  41. package/dist/commands/clear.d.ts.map +1 -1
  42. package/dist/commands/clear.js +4 -1
  43. package/dist/commands/cp.d.ts +3 -0
  44. package/dist/commands/cp.d.ts.map +1 -0
  45. package/dist/commands/cp.js +70 -0
  46. package/dist/commands/curl.d.ts.map +1 -1
  47. package/dist/commands/curl.js +2 -0
  48. package/dist/commands/cut.d.ts +3 -0
  49. package/dist/commands/cut.d.ts.map +1 -0
  50. package/dist/commands/cut.js +27 -0
  51. package/dist/commands/date.d.ts +3 -0
  52. package/dist/commands/date.d.ts.map +1 -0
  53. package/dist/commands/date.js +22 -0
  54. package/dist/commands/deluser.d.ts.map +1 -1
  55. package/dist/commands/deluser.js +2 -0
  56. package/dist/commands/df.d.ts +3 -0
  57. package/dist/commands/df.d.ts.map +1 -0
  58. package/dist/commands/df.js +16 -0
  59. package/dist/commands/diff.d.ts +3 -0
  60. package/dist/commands/diff.d.ts.map +1 -0
  61. package/dist/commands/diff.js +40 -0
  62. package/dist/commands/du.d.ts +3 -0
  63. package/dist/commands/du.d.ts.map +1 -0
  64. package/dist/commands/du.js +39 -0
  65. package/dist/commands/echo.d.ts.map +1 -1
  66. package/dist/commands/echo.js +2 -0
  67. package/dist/commands/env.d.ts.map +1 -1
  68. package/dist/commands/env.js +6 -14
  69. package/dist/commands/export.d.ts.map +1 -1
  70. package/dist/commands/export.js +11 -21
  71. package/dist/commands/find.d.ts +3 -0
  72. package/dist/commands/find.d.ts.map +1 -0
  73. package/dist/commands/find.js +50 -0
  74. package/dist/commands/grep.d.ts.map +1 -1
  75. package/dist/commands/grep.js +58 -35
  76. package/dist/commands/groups.d.ts +3 -0
  77. package/dist/commands/groups.d.ts.map +1 -0
  78. package/dist/commands/groups.js +12 -0
  79. package/dist/commands/gzip.d.ts +4 -0
  80. package/dist/commands/gzip.d.ts.map +1 -0
  81. package/dist/commands/gzip.js +40 -0
  82. package/dist/commands/head.d.ts +3 -0
  83. package/dist/commands/head.d.ts.map +1 -0
  84. package/dist/commands/head.js +32 -0
  85. package/dist/commands/help.d.ts +1 -1
  86. package/dist/commands/help.d.ts.map +1 -1
  87. package/dist/commands/help.js +75 -3
  88. package/dist/commands/hostname.d.ts.map +1 -1
  89. package/dist/commands/hostname.js +2 -0
  90. package/dist/commands/htop.d.ts.map +1 -1
  91. package/dist/commands/htop.js +2 -0
  92. package/dist/commands/id.d.ts +3 -0
  93. package/dist/commands/id.d.ts.map +1 -0
  94. package/dist/commands/id.js +14 -0
  95. package/dist/commands/index.d.ts +5 -2
  96. package/dist/commands/index.d.ts.map +1 -1
  97. package/dist/commands/index.js +104 -87
  98. package/dist/commands/kill.d.ts +3 -0
  99. package/dist/commands/kill.d.ts.map +1 -0
  100. package/dist/commands/kill.js +13 -0
  101. package/dist/commands/ln.d.ts +3 -0
  102. package/dist/commands/ln.d.ts.map +1 -0
  103. package/dist/commands/ln.js +44 -0
  104. package/dist/commands/ls.d.ts.map +1 -1
  105. package/dist/commands/ls.js +2 -0
  106. package/dist/commands/mkdir.d.ts.map +1 -1
  107. package/dist/commands/mkdir.js +2 -0
  108. package/dist/commands/mv.d.ts +3 -0
  109. package/dist/commands/mv.d.ts.map +1 -0
  110. package/dist/commands/mv.js +37 -0
  111. package/dist/commands/nano.d.ts.map +1 -1
  112. package/dist/commands/nano.js +2 -0
  113. package/dist/commands/neofetch.d.ts.map +1 -1
  114. package/dist/commands/neofetch.js +2 -0
  115. package/dist/commands/passwd.d.ts.map +1 -1
  116. package/dist/commands/passwd.js +2 -0
  117. package/dist/commands/ping.d.ts +3 -0
  118. package/dist/commands/ping.d.ts.map +1 -0
  119. package/dist/commands/ping.js +18 -0
  120. package/dist/commands/ps.d.ts +3 -0
  121. package/dist/commands/ps.d.ts.map +1 -0
  122. package/dist/commands/ps.js +17 -0
  123. package/dist/commands/pwd.d.ts.map +1 -1
  124. package/dist/commands/pwd.js +2 -0
  125. package/dist/commands/rm.d.ts.map +1 -1
  126. package/dist/commands/rm.js +2 -0
  127. package/dist/commands/sed.d.ts +3 -0
  128. package/dist/commands/sed.d.ts.map +1 -0
  129. package/dist/commands/sed.js +47 -0
  130. package/dist/commands/set.d.ts +3 -0
  131. package/dist/commands/set.d.ts.map +1 -1
  132. package/dist/commands/set.js +19 -46
  133. package/dist/commands/sh.d.ts +0 -1
  134. package/dist/commands/sh.d.ts.map +1 -1
  135. package/dist/commands/sh.js +228 -35
  136. package/dist/commands/sleep.d.ts +3 -0
  137. package/dist/commands/sleep.d.ts.map +1 -0
  138. package/dist/commands/sleep.js +13 -0
  139. package/dist/commands/sort.d.ts +3 -0
  140. package/dist/commands/sort.d.ts.map +1 -0
  141. package/dist/commands/sort.js +37 -0
  142. package/dist/commands/su.d.ts.map +1 -1
  143. package/dist/commands/su.js +2 -0
  144. package/dist/commands/sudo.d.ts.map +1 -1
  145. package/dist/commands/sudo.js +2 -0
  146. package/dist/commands/tail.d.ts +3 -0
  147. package/dist/commands/tail.d.ts.map +1 -0
  148. package/dist/commands/tail.js +35 -0
  149. package/dist/commands/tar.d.ts +3 -0
  150. package/dist/commands/tar.d.ts.map +1 -0
  151. package/dist/commands/tar.js +64 -0
  152. package/dist/commands/tee.d.ts +3 -0
  153. package/dist/commands/tee.d.ts.map +1 -0
  154. package/dist/commands/tee.js +29 -0
  155. package/dist/commands/touch.d.ts.map +1 -1
  156. package/dist/commands/touch.js +2 -0
  157. package/dist/commands/tr.d.ts +3 -0
  158. package/dist/commands/tr.d.ts.map +1 -0
  159. package/dist/commands/tr.js +24 -0
  160. package/dist/commands/tree.d.ts.map +1 -1
  161. package/dist/commands/tree.js +2 -0
  162. package/dist/commands/uname.d.ts +3 -0
  163. package/dist/commands/uname.d.ts.map +1 -0
  164. package/dist/commands/uname.js +21 -0
  165. package/dist/commands/uniq.d.ts +3 -0
  166. package/dist/commands/uniq.d.ts.map +1 -0
  167. package/dist/commands/uniq.js +33 -0
  168. package/dist/commands/unset.d.ts.map +1 -1
  169. package/dist/commands/unset.js +6 -10
  170. package/dist/commands/wc.d.ts +3 -0
  171. package/dist/commands/wc.d.ts.map +1 -0
  172. package/dist/commands/wc.js +50 -0
  173. package/dist/commands/wget.d.ts.map +1 -1
  174. package/dist/commands/wget.js +2 -0
  175. package/dist/commands/who.d.ts.map +1 -1
  176. package/dist/commands/who.js +2 -0
  177. package/dist/commands/whoami.d.ts.map +1 -1
  178. package/dist/commands/whoami.js +2 -0
  179. package/dist/commands/xargs.d.ts +3 -0
  180. package/dist/commands/xargs.d.ts.map +1 -0
  181. package/dist/commands/xargs.js +16 -0
  182. package/dist/index.d.ts +1 -0
  183. package/dist/index.d.ts.map +1 -1
  184. package/dist/types/commands.d.ts +13 -0
  185. package/dist/types/commands.d.ts.map +1 -1
  186. package/dist/types/pipeline.d.ts +20 -0
  187. package/dist/types/pipeline.d.ts.map +1 -1
  188. package/package.json +5 -2
  189. package/scripts/publish-package.sh +70 -0
  190. package/src/SSHMimic/exec.ts +2 -2
  191. package/src/SSHMimic/executor.ts +95 -98
  192. package/src/SSHMimic/index.ts +138 -57
  193. package/src/SSHMimic/sftp.ts +15 -0
  194. package/src/VirtualFileSystem/index.ts +464 -292
  195. package/src/VirtualShell/index.ts +4 -6
  196. package/src/VirtualShell/shell.ts +19 -2
  197. package/src/VirtualShell/shellParser.ts +202 -168
  198. package/src/VirtualUserManager/index.ts +36 -0
  199. package/src/commands/adduser.ts +2 -0
  200. package/src/commands/awk.ts +30 -0
  201. package/src/commands/base64.ts +18 -0
  202. package/src/commands/cat.ts +2 -0
  203. package/src/commands/cd.ts +2 -0
  204. package/src/commands/chmod.ts +35 -0
  205. package/src/commands/clear.ts +4 -1
  206. package/src/commands/cp.ts +78 -0
  207. package/src/commands/curl.ts +2 -0
  208. package/src/commands/cut.ts +29 -0
  209. package/src/commands/date.ts +24 -0
  210. package/src/commands/deluser.ts +2 -0
  211. package/src/commands/df.ts +18 -0
  212. package/src/commands/diff.ts +29 -0
  213. package/src/commands/du.ts +39 -0
  214. package/src/commands/echo.ts +2 -0
  215. package/src/commands/env.ts +6 -16
  216. package/src/commands/export.ts +11 -24
  217. package/src/commands/find.ts +63 -0
  218. package/src/commands/grep.ts +51 -38
  219. package/src/commands/groups.ts +14 -0
  220. package/src/commands/gzip.ts +31 -0
  221. package/src/commands/head.ts +37 -0
  222. package/src/commands/help.ts +81 -3
  223. package/src/commands/hostname.ts +2 -0
  224. package/src/commands/htop.ts +2 -0
  225. package/src/commands/id.ts +16 -0
  226. package/src/commands/index.ts +114 -133
  227. package/src/commands/kill.ts +14 -0
  228. package/src/commands/ln.ts +49 -0
  229. package/src/commands/ls.ts +2 -0
  230. package/src/commands/mkdir.ts +2 -0
  231. package/src/commands/mv.ts +45 -0
  232. package/src/commands/nano.ts +2 -0
  233. package/src/commands/neofetch.ts +2 -0
  234. package/src/commands/passwd.ts +2 -0
  235. package/src/commands/ping.ts +20 -0
  236. package/src/commands/ps.ts +19 -0
  237. package/src/commands/pwd.ts +2 -0
  238. package/src/commands/rm.ts +2 -0
  239. package/src/commands/sed.ts +45 -0
  240. package/src/commands/set.ts +19 -50
  241. package/src/commands/sh.ts +192 -43
  242. package/src/commands/sleep.ts +14 -0
  243. package/src/commands/sort.ts +37 -0
  244. package/src/commands/su.ts +2 -0
  245. package/src/commands/sudo.ts +2 -0
  246. package/src/commands/tail.ts +39 -0
  247. package/src/commands/tar.ts +58 -0
  248. package/src/commands/tee.ts +25 -0
  249. package/src/commands/touch.ts +2 -0
  250. package/src/commands/tr.ts +24 -0
  251. package/src/commands/tree.ts +2 -0
  252. package/src/commands/uname.ts +20 -0
  253. package/src/commands/uniq.ts +28 -0
  254. package/src/commands/unset.ts +5 -12
  255. package/src/commands/wc.ts +50 -0
  256. package/src/commands/wget.ts +2 -0
  257. package/src/commands/who.ts +2 -0
  258. package/src/commands/whoami.ts +2 -0
  259. package/src/commands/xargs.ts +17 -0
  260. package/src/index.ts +1 -0
  261. package/src/types/commands.ts +14 -0
  262. package/src/types/pipeline.ts +23 -0
  263. package/standalone.js +93 -55
  264. package/standalone.js.map +4 -4
  265. package/tests/bun-test-shim.ts +1 -0
  266. package/tests/sftp.test.ts +115 -191
  267. package/tests/users.test.ts +42 -88
@@ -1,7 +1,10 @@
1
1
  /** biome-ignore-all lint/style/useNamingConvention: env variables */
2
2
  import type { ShellModule } from "../types/commands";
3
+ /** @deprecated use env.vars from CommandContext */
3
4
  export declare function getEnvVar(name: string): string | undefined;
5
+ /** @deprecated use env.vars from CommandContext */
4
6
  export declare function setEnvVar(name: string, value: string): void;
7
+ /** @deprecated use env.vars from CommandContext */
5
8
  export declare function getAllEnvVars(authUser: string): Record<string, string>;
6
9
  export declare const setCommand: ShellModule;
7
10
  //# sourceMappingURL=set.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"set.d.ts","sourceRoot":"","sources":["../../src/commands/set.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAarD,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAE1D;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAE3D;AAED,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAItE;AAED,eAAO,MAAM,UAAU,EAAE,WA4CxB,CAAC"}
1
+ {"version":3,"file":"set.d.ts","sourceRoot":"","sources":["../../src/commands/set.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAWrD,mDAAmD;AACnD,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAA6B;AACxF,mDAAmD;AACnD,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAA8B;AAC1F,mDAAmD;AACnD,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAItE;AAED,eAAO,MAAM,UAAU,EAAE,WAkBxB,CAAC"}
@@ -1,64 +1,37 @@
1
- import { getArg } from "./command-helpers";
2
- // Simple in-memory environment variables store
3
- // In a real implementation, this would be per-session/per-user
4
- const envVars = {
1
+ // Legacy global store kept for compatibility with older callers
2
+ const _globalEnv = {
5
3
  PATH: "/usr/local/bin:/usr/bin:/bin",
6
4
  HOME: "/home/user",
7
5
  SHELL: "/bin/sh",
8
6
  TERM: "xterm-256color",
9
7
  USER: "user",
10
8
  };
11
- export function getEnvVar(name) {
12
- return envVars[name];
13
- }
14
- export function setEnvVar(name, value) {
15
- envVars[name] = value;
16
- }
9
+ /** @deprecated use env.vars from CommandContext */
10
+ export function getEnvVar(name) { return _globalEnv[name]; }
11
+ /** @deprecated use env.vars from CommandContext */
12
+ export function setEnvVar(name, value) { _globalEnv[name] = value; }
13
+ /** @deprecated use env.vars from CommandContext */
17
14
  export function getAllEnvVars(authUser) {
18
- envVars.USER = authUser;
19
- envVars.HOME = `/home/${authUser}`;
20
- return { ...envVars };
15
+ _globalEnv.USER = authUser;
16
+ _globalEnv.HOME = `/home/${authUser}`;
17
+ return { ..._globalEnv };
21
18
  }
22
19
  export const setCommand = {
23
20
  name: "set",
21
+ description: "Display or set shell variables",
22
+ category: "shell",
24
23
  params: ["[VAR=value]"],
25
- run: ({ args }) => {
26
- // No arguments: display all environment variables
24
+ run: ({ args, env }) => {
27
25
  if (args.length === 0) {
28
- const output = Object.entries(envVars)
29
- .map(([key, value]) => `${key}=${value}`)
30
- .sort()
31
- .join("\n");
32
- return { stdout: output, exitCode: 0 };
26
+ const out = Object.entries(env.vars).map(([k, v]) => `${k}=${v}`).join("\n");
27
+ return { stdout: out, exitCode: 0 };
33
28
  }
34
- // Parse VAR=value format
35
- const assignments = [];
36
- for (let index = 0;; index += 1) {
37
- const arg = getArg(args, index);
38
- if (!arg) {
39
- break;
40
- }
29
+ for (const arg of args) {
41
30
  if (arg.includes("=")) {
42
- const [varName, varValue] = arg.split("=", 2);
43
- if (varName && varValue !== undefined) {
44
- setEnvVar(varName, varValue);
45
- assignments.push(arg);
46
- }
47
- }
48
- else {
49
- // If no '=' present, display that specific variable
50
- const value = getEnvVar(arg);
51
- if (value !== undefined) {
52
- assignments.push(`${arg}=${value}`);
53
- }
54
- else {
55
- assignments.push(`${arg}: not set`);
56
- }
31
+ const eq = arg.indexOf("=");
32
+ env.vars[arg.slice(0, eq)] = arg.slice(eq + 1);
57
33
  }
58
34
  }
59
- return {
60
- stdout: assignments.length > 0 ? assignments.join("\n") : "",
61
- exitCode: 0,
62
- };
35
+ return { exitCode: 0 };
63
36
  },
64
37
  };
@@ -1,4 +1,3 @@
1
1
  import type { ShellModule } from "../types/commands";
2
- /** Simple shell script executor with basic variable support */
3
2
  export declare const shCommand: ShellModule;
4
3
  //# sourceMappingURL=sh.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sh.d.ts","sourceRoot":"","sources":["../../src/commands/sh.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAkB,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAIrE,+DAA+D;AAC/D,eAAO,MAAM,SAAS,EAAE,WAmDvB,CAAC"}
1
+ {"version":3,"file":"sh.d.ts","sourceRoot":"","sources":["../../src/commands/sh.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAiC,WAAW,EAAE,MAAM,mBAAmB,CAAC;AA8KpF,eAAO,MAAM,SAAS,EAAE,WA+BvB,CAAC"}
@@ -1,45 +1,238 @@
1
- import { getArg, getFlag } from "./command-helpers";
1
+ import { getArg, ifFlag } from "./command-helpers";
2
+ import { resolvePath } from "./helpers";
2
3
  import { runCommand } from "./index";
3
- /** Simple shell script executor with basic variable support */
4
+ /** Expand $VAR and ${VAR:-default} in a line using the current env */
5
+ function expandVars(line, env, lastExit) {
6
+ return line
7
+ .replace(/\$\?/g, String(lastExit))
8
+ .replace(/\$\{([^}:]+):-([^}]*)\}/g, (_, n, d) => env[n] ?? d)
9
+ .replace(/\$\{([^}]+)\}/g, (_, n) => env[n] ?? "")
10
+ .replace(/\$([A-Za-z_][A-Za-z0-9_]*)/g, (_, n) => env[n] ?? "")
11
+ .replace(/^~(\/|$)/, `${env.HOME ?? "/home/user"}$1`);
12
+ }
13
+ /** Very small shell interpreter: supports if/elif/else/fi, for/do/done, while/do/done */
14
+ function parseBlocks(lines) {
15
+ const blocks = [];
16
+ let i = 0;
17
+ while (i < lines.length) {
18
+ const line = lines[i].trim();
19
+ if (!line || line.startsWith("#")) {
20
+ i++;
21
+ continue;
22
+ }
23
+ if (line.startsWith("if ") || line === "if") {
24
+ const cond = line.replace(/^if\s+/, "").replace(/;\s*then\s*$/, "").trim();
25
+ const thenLines = [];
26
+ const elifBlocks = [];
27
+ const elseLines = [];
28
+ let section = "then";
29
+ let elifCond = "";
30
+ i++;
31
+ while (i < lines.length && lines[i]?.trim() !== "fi") {
32
+ const l = lines[i].trim();
33
+ if (l.startsWith("elif ")) {
34
+ section = "elif";
35
+ elifCond = l.replace(/^elif\s+/, "").replace(/;\s*then\s*$/, "").trim();
36
+ elifBlocks.push({ cond: elifCond, body: [] });
37
+ }
38
+ else if (l === "else") {
39
+ section = "else";
40
+ }
41
+ else if (l !== "then") {
42
+ if (section === "then")
43
+ thenLines.push(l);
44
+ else if (section === "elif" && elifBlocks.length > 0)
45
+ elifBlocks[elifBlocks.length - 1].body.push(l);
46
+ else
47
+ elseLines.push(l);
48
+ }
49
+ i++;
50
+ }
51
+ blocks.push({ type: "if", cond, then: thenLines, elif: elifBlocks, else_: elseLines });
52
+ }
53
+ else if (line.startsWith("for ")) {
54
+ const m = line.match(/^for\s+(\w+)\s+in\s+(.+?)(?:\s*;\s*do)?$/);
55
+ if (m) {
56
+ const body = [];
57
+ i++;
58
+ while (i < lines.length && lines[i]?.trim() !== "done") {
59
+ const l = lines[i].trim();
60
+ if (l !== "do")
61
+ body.push(l);
62
+ i++;
63
+ }
64
+ blocks.push({ type: "for", var: m[1], list: m[2], body });
65
+ }
66
+ else {
67
+ blocks.push({ type: "cmd", line });
68
+ }
69
+ }
70
+ else if (line.startsWith("while ")) {
71
+ const cond = line.replace(/^while\s+/, "").replace(/;\s*do\s*$/, "").trim();
72
+ const body = [];
73
+ i++;
74
+ while (i < lines.length && lines[i]?.trim() !== "done") {
75
+ const l = lines[i].trim();
76
+ if (l !== "do")
77
+ body.push(l);
78
+ i++;
79
+ }
80
+ blocks.push({ type: "while", cond, body });
81
+ }
82
+ else {
83
+ blocks.push({ type: "cmd", line });
84
+ }
85
+ i++;
86
+ }
87
+ return blocks;
88
+ }
89
+ async function evalCondition(cond, ctx) {
90
+ const expanded = expandVars(cond, ctx.env.vars, ctx.env.lastExitCode);
91
+ // test -f / test -d / [ ... ]
92
+ const testMatch = expanded.match(/^\[?\s*(.+?)\s*\]?$/);
93
+ if (testMatch) {
94
+ const expr = testMatch[1];
95
+ // -f file
96
+ const fTest = expr.match(/^-([fdeznr])\s+(.+)$/);
97
+ if (fTest) {
98
+ const [, flag, arg] = fTest;
99
+ const p = resolvePath(ctx.cwd, arg);
100
+ if (flag === "f")
101
+ return ctx.shell.vfs.exists(p) && ctx.shell.vfs.stat(p).type === "file";
102
+ if (flag === "d")
103
+ return ctx.shell.vfs.exists(p) && ctx.shell.vfs.stat(p).type === "directory";
104
+ if (flag === "e")
105
+ return ctx.shell.vfs.exists(p);
106
+ if (flag === "z")
107
+ return (arg ?? "").length === 0;
108
+ if (flag === "n")
109
+ return (arg ?? "").length > 0;
110
+ }
111
+ // string comparison
112
+ const cmpMatch = expr.match(/^"?([^"]*)"?\s*(==|!=|=|<|>)\s*"?([^"]*)"?$/);
113
+ if (cmpMatch) {
114
+ const [, a, op, b] = cmpMatch;
115
+ if (op === "==" || op === "=")
116
+ return a === b;
117
+ if (op === "!=")
118
+ return a !== b;
119
+ }
120
+ // numeric
121
+ const numMatch = expr.match(/^(\S+)\s+(-eq|-ne|-lt|-le|-gt|-ge)\s+(\S+)$/);
122
+ if (numMatch) {
123
+ const [, a, op, b] = numMatch;
124
+ const na = Number(a), nb = Number(b);
125
+ if (op === "-eq")
126
+ return na === nb;
127
+ if (op === "-ne")
128
+ return na !== nb;
129
+ if (op === "-lt")
130
+ return na < nb;
131
+ if (op === "-le")
132
+ return na <= nb;
133
+ if (op === "-gt")
134
+ return na > nb;
135
+ if (op === "-ge")
136
+ return na >= nb;
137
+ }
138
+ }
139
+ // fallback: run command and check exit code
140
+ const r = await runCommand(expanded, ctx.authUser, ctx.hostname, ctx.mode, ctx.cwd, ctx.shell, undefined, ctx.env);
141
+ return (r.exitCode ?? 0) === 0;
142
+ }
143
+ async function runBlocks(blocks, ctx) {
144
+ let lastResult = { exitCode: 0 };
145
+ let output = "";
146
+ for (const block of blocks) {
147
+ if (block.type === "cmd") {
148
+ const expanded = expandVars(block.line, ctx.env.vars, ctx.env.lastExitCode);
149
+ const r = await runCommand(expanded, ctx.authUser, ctx.hostname, ctx.mode, ctx.cwd, ctx.shell, undefined, ctx.env);
150
+ ctx.env.lastExitCode = r.exitCode ?? 0;
151
+ if (r.stdout)
152
+ output += `${r.stdout}\n`;
153
+ if (r.stderr)
154
+ return { ...r, stdout: output.trim() };
155
+ lastResult = r;
156
+ }
157
+ else if (block.type === "if") {
158
+ let ran = false;
159
+ if (await evalCondition(block.cond, ctx)) {
160
+ const sub = await runBlocks(parseBlocks(block.then), ctx);
161
+ if (sub.stdout)
162
+ output += `${sub.stdout}\n`;
163
+ ran = true;
164
+ }
165
+ else {
166
+ for (const elif of block.elif) {
167
+ if (await evalCondition(elif.cond, ctx)) {
168
+ const sub = await runBlocks(parseBlocks(elif.body), ctx);
169
+ if (sub.stdout)
170
+ output += `${sub.stdout}\n`;
171
+ ran = true;
172
+ break;
173
+ }
174
+ }
175
+ if (!ran && block.else_.length > 0) {
176
+ const sub = await runBlocks(parseBlocks(block.else_), ctx);
177
+ if (sub.stdout)
178
+ output += `${sub.stdout}\n`;
179
+ }
180
+ }
181
+ }
182
+ else if (block.type === "for") {
183
+ const listExpanded = expandVars(block.list, ctx.env.vars, ctx.env.lastExitCode);
184
+ const items = listExpanded.trim().split(/\s+/);
185
+ for (const item of items) {
186
+ ctx.env.vars[block.var] = item;
187
+ const sub = await runBlocks(parseBlocks(block.body), ctx);
188
+ if (sub.stdout)
189
+ output += `${sub.stdout}\n`;
190
+ if (sub.closeSession)
191
+ return sub;
192
+ }
193
+ }
194
+ else if (block.type === "while") {
195
+ let iterations = 0;
196
+ while (iterations < 1000 && await evalCondition(block.cond, ctx)) {
197
+ const sub = await runBlocks(parseBlocks(block.body), ctx);
198
+ if (sub.stdout)
199
+ output += `${sub.stdout}\n`;
200
+ if (sub.closeSession)
201
+ return sub;
202
+ iterations++;
203
+ }
204
+ }
205
+ }
206
+ return { ...lastResult, stdout: output.trim() || lastResult.stdout };
207
+ }
4
208
  export const shCommand = {
5
209
  name: "sh",
6
- params: ["-c <script>", "[<file>]"],
7
210
  aliases: ["bash"],
211
+ description: "Execute shell script or command",
212
+ category: "shell",
213
+ params: ["-c <script>", "[<file>]"],
8
214
  run: async (ctx) => {
9
- const { args, authUser, hostname, mode, cwd } = ctx;
10
- // Handle -c option: sh -c "command"
11
- if (getFlag(args, "-c") && args.length >= 2) {
215
+ const { args, authUser, shell, cwd } = ctx;
216
+ // sh -c "inline script"
217
+ if (ifFlag(args, "-c")) {
12
218
  const script = getArg(args, 1) ?? "";
13
- if (!script) {
219
+ if (!script)
14
220
  return { stderr: "sh: -c requires a script", exitCode: 1 };
15
- }
16
- const scriptArgs = args.slice(2);
17
- // Split by semicolon and newline
18
- const lines = script
19
- .split(/[;\n]/)
20
- .map((line) => line.trim())
21
- .filter((line) => line && !line.startsWith("#"));
22
- let output = "";
23
- const exitCode = 0;
24
- for (const line of lines) {
25
- // Simple variable substitution
26
- let command = line;
27
- for (let i = 0; i < scriptArgs.length; i++) {
28
- const arg = scriptArgs[i] ?? "";
29
- command = command.replaceAll(`$${i}`, arg);
30
- }
31
- command = command.replaceAll("$@", scriptArgs.join(" "));
32
- // Execute the command
33
- const result = await Promise.resolve(runCommand(command, authUser, hostname, mode, cwd, ctx.shell));
34
- if (result.stdout) {
35
- output += `${result.stdout}\n`;
36
- }
37
- if (result.stderr) {
38
- return { stderr: result.stderr, exitCode: result.exitCode ?? 1 };
39
- }
40
- }
41
- return { stdout: output.trim(), exitCode };
221
+ const lines = script.split(/[;\n]/).map((l) => l.trim()).filter((l) => l && !l.startsWith("#"));
222
+ const blocks = parseBlocks(lines);
223
+ return runBlocks(blocks, ctx);
224
+ }
225
+ // sh <file>
226
+ const fileArg = args[0];
227
+ if (fileArg) {
228
+ const p = resolvePath(cwd, fileArg);
229
+ if (!shell.vfs.exists(p))
230
+ return { stderr: `sh: ${fileArg}: No such file or directory`, exitCode: 1 };
231
+ const content = shell.vfs.readFile(p);
232
+ const lines = content.split("\n").map((l) => l.trim()).filter((l) => l && !l.startsWith("#"));
233
+ const blocks = parseBlocks(lines);
234
+ return runBlocks(blocks, ctx);
42
235
  }
43
- return { stderr: "sh: invalid usage", exitCode: 1 };
236
+ return { stderr: "sh: invalid usage. Use: sh -c 'cmd' or sh <file>", exitCode: 1 };
44
237
  },
45
238
  };
@@ -0,0 +1,3 @@
1
+ import type { ShellModule } from "../types/commands";
2
+ export declare const sleepCommand: ShellModule;
3
+ //# sourceMappingURL=sleep.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sleep.d.ts","sourceRoot":"","sources":["../../src/commands/sleep.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAErD,eAAO,MAAM,YAAY,EAAE,WAW1B,CAAC"}
@@ -0,0 +1,13 @@
1
+ export const sleepCommand = {
2
+ name: "sleep",
3
+ description: "Delay execution",
4
+ category: "system",
5
+ params: ["<seconds>"],
6
+ run: async ({ args }) => {
7
+ const secs = parseFloat(args[0] ?? "1");
8
+ if (Number.isNaN(secs) || secs < 0)
9
+ return { stderr: "sleep: invalid time", exitCode: 1 };
10
+ await new Promise((r) => setTimeout(r, secs * 1000));
11
+ return { exitCode: 0 };
12
+ },
13
+ };
@@ -0,0 +1,3 @@
1
+ import type { ShellModule } from "../types/commands";
2
+ export declare const sortCommand: ShellModule;
3
+ //# sourceMappingURL=sort.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sort.d.ts","sourceRoot":"","sources":["../../src/commands/sort.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAIrD,eAAO,MAAM,WAAW,EAAE,WAgCzB,CAAC"}
@@ -0,0 +1,37 @@
1
+ import { ifFlag } from "./command-helpers";
2
+ import { assertPathAccess, resolvePath } from "./helpers";
3
+ export const sortCommand = {
4
+ name: "sort",
5
+ description: "Sort lines of text",
6
+ category: "text",
7
+ params: ["[-r] [-n] [-u] [-k <col>] [file...]"],
8
+ run: ({ authUser, shell, cwd, args, stdin }) => {
9
+ const reverse = ifFlag(args, ["-r"]);
10
+ const numeric = ifFlag(args, ["-n"]);
11
+ const unique = ifFlag(args, ["-u"]);
12
+ const files = args.filter((a) => !a.startsWith("-"));
13
+ const getContent = () => {
14
+ if (files.length > 0) {
15
+ return files.map((f) => {
16
+ try {
17
+ assertPathAccess(authUser, resolvePath(cwd, f), "sort");
18
+ return shell.vfs.readFile(resolvePath(cwd, f));
19
+ }
20
+ catch {
21
+ return "";
22
+ }
23
+ }).join("\n");
24
+ }
25
+ return stdin ?? "";
26
+ };
27
+ const lines = getContent().split("\n").filter(Boolean);
28
+ const sorted = [...lines].sort((a, b) => {
29
+ if (numeric)
30
+ return Number(a) - Number(b);
31
+ return a.localeCompare(b);
32
+ });
33
+ const result = reverse ? sorted.reverse() : sorted;
34
+ const out = unique ? [...new Set(result)] : result;
35
+ return { stdout: out.join("\n"), exitCode: 0 };
36
+ },
37
+ };
@@ -1 +1 @@
1
- {"version":3,"file":"su.d.ts","sourceRoot":"","sources":["../../src/commands/su.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGrD,eAAO,MAAM,SAAS,EAAE,WA4BvB,CAAC"}
1
+ {"version":3,"file":"su.d.ts","sourceRoot":"","sources":["../../src/commands/su.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGrD,eAAO,MAAM,SAAS,EAAE,WA8BvB,CAAC"}
@@ -1,6 +1,8 @@
1
1
  import { getArg } from "./command-helpers";
2
2
  export const suCommand = {
3
3
  name: "su",
4
+ description: "Switch user",
5
+ category: "users",
4
6
  params: ["- <username>"],
5
7
  run: ({ authUser, shell, args }) => {
6
8
  const users = shell.users;
@@ -1 +1 @@
1
- {"version":3,"file":"sudo.d.ts","sourceRoot":"","sources":["../../src/commands/sudo.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAqBrD,eAAO,MAAM,WAAW,EAAE,WA+CzB,CAAC"}
1
+ {"version":3,"file":"sudo.d.ts","sourceRoot":"","sources":["../../src/commands/sudo.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAqBrD,eAAO,MAAM,WAAW,EAAE,WAiDzB,CAAC"}
@@ -12,6 +12,8 @@ function parseSudoArgs(args) {
12
12
  }
13
13
  export const sudoCommand = {
14
14
  name: "sudo",
15
+ description: "Execute as superuser",
16
+ category: "users",
15
17
  params: ["<command...>"],
16
18
  run: async ({ authUser, hostname, mode, cwd, shell, args }) => {
17
19
  const { targetUser, loginShell, commandLine } = parseSudoArgs(args);
@@ -0,0 +1,3 @@
1
+ import type { ShellModule } from "../types/commands";
2
+ export declare const tailCommand: ShellModule;
3
+ //# sourceMappingURL=tail.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tail.d.ts","sourceRoot":"","sources":["../../src/commands/tail.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAIrD,eAAO,MAAM,WAAW,EAAE,WAkCzB,CAAC"}
@@ -0,0 +1,35 @@
1
+ import { getFlag } from "./command-helpers";
2
+ import { assertPathAccess, resolvePath } from "./helpers";
3
+ export const tailCommand = {
4
+ name: "tail",
5
+ description: "Output last lines",
6
+ category: "text",
7
+ params: ["[-n <lines>] [file...]"],
8
+ run: ({ authUser, shell, cwd, args, stdin }) => {
9
+ const nArg = getFlag(args, ["-n"]);
10
+ const n = typeof nArg === "string" ? parseInt(nArg, 10) : 10;
11
+ const positionals = args.filter((a) => !a.startsWith("-") && a !== nArg);
12
+ const take = (content) => {
13
+ const lines = content.split("\n");
14
+ return lines.slice(Math.max(0, lines.length - n)).join("\n");
15
+ };
16
+ if (positionals.length === 0) {
17
+ return { stdout: take(stdin ?? ""), exitCode: 0 };
18
+ }
19
+ const results = [];
20
+ for (const file of positionals) {
21
+ const filePath = resolvePath(cwd, file);
22
+ try {
23
+ assertPathAccess(authUser, filePath, "tail");
24
+ results.push(take(shell.vfs.readFile(filePath)));
25
+ }
26
+ catch {
27
+ return {
28
+ stderr: `tail: ${file}: No such file or directory`,
29
+ exitCode: 1,
30
+ };
31
+ }
32
+ }
33
+ return { stdout: results.join("\n"), exitCode: 0 };
34
+ },
35
+ };
@@ -0,0 +1,3 @@
1
+ import type { ShellModule } from "../types/commands";
2
+ export declare const tarCommand: ShellModule;
3
+ //# sourceMappingURL=tar.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tar.d.ts","sourceRoot":"","sources":["../../src/commands/tar.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAIrD,eAAO,MAAM,UAAU,EAAE,WAqDxB,CAAC"}
@@ -0,0 +1,64 @@
1
+ import { ifFlag } from "./command-helpers";
2
+ import { resolvePath } from "./helpers";
3
+ export const tarCommand = {
4
+ name: "tar",
5
+ description: "Archive utility",
6
+ category: "archive",
7
+ params: ["[-czf|-xzf|-tf] <archive> [files...]"],
8
+ run: ({ authUser, shell, cwd, args }) => {
9
+ const create = ifFlag(args, ["-c"]);
10
+ const extract = ifFlag(args, ["-x"]);
11
+ const list = ifFlag(args, ["-t"]);
12
+ const fFlag = args.findIndex((a) => a.includes("f"));
13
+ const archiveName = fFlag !== -1 ? args[fFlag + 1] : args.find((a) => a.endsWith(".tar") || a.endsWith(".tar.gz") || a.endsWith(".tgz"));
14
+ if (!archiveName)
15
+ return { stderr: "tar: no archive specified", exitCode: 1 };
16
+ const archivePath = resolvePath(cwd, archiveName);
17
+ if (create) {
18
+ const fileArgs = args.filter((a) => !a.startsWith("-") && a !== archiveName);
19
+ const entries = {};
20
+ for (const f of fileArgs) {
21
+ const p = resolvePath(cwd, f);
22
+ try {
23
+ const stat = shell.vfs.stat(p);
24
+ if (stat.type === "file")
25
+ entries[f] = shell.vfs.readFile(p);
26
+ else {
27
+ const walk = (dir, prefix) => {
28
+ for (const e of shell.vfs.list(dir)) {
29
+ const full = `${dir}/${e}`, rel = `${prefix}/${e}`;
30
+ const s = shell.vfs.stat(full);
31
+ if (s.type === "file")
32
+ entries[rel] = shell.vfs.readFile(full);
33
+ else
34
+ walk(full, rel);
35
+ }
36
+ };
37
+ walk(p, f);
38
+ }
39
+ }
40
+ catch {
41
+ return { stderr: `tar: ${f}: No such file or directory`, exitCode: 1 };
42
+ }
43
+ }
44
+ shell.writeFileAsUser(authUser, archivePath, JSON.stringify(entries));
45
+ return { exitCode: 0 };
46
+ }
47
+ if (list || extract) {
48
+ let entries;
49
+ try {
50
+ entries = JSON.parse(shell.vfs.readFile(archivePath));
51
+ }
52
+ catch {
53
+ return { stderr: `tar: ${archiveName}: cannot open archive`, exitCode: 1 };
54
+ }
55
+ if (list)
56
+ return { stdout: Object.keys(entries).join("\n"), exitCode: 0 };
57
+ for (const [name, content] of Object.entries(entries)) {
58
+ shell.writeFileAsUser(authUser, resolvePath(cwd, name), content);
59
+ }
60
+ return { exitCode: 0 };
61
+ }
62
+ return { stderr: "tar: must specify -c, -x, or -t", exitCode: 1 };
63
+ },
64
+ };
@@ -0,0 +1,3 @@
1
+ import type { ShellModule } from "../types/commands";
2
+ export declare const teeCommand: ShellModule;
3
+ //# sourceMappingURL=tee.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tee.d.ts","sourceRoot":"","sources":["../../src/commands/tee.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAIrD,eAAO,MAAM,UAAU,EAAE,WAoBxB,CAAC"}
@@ -0,0 +1,29 @@
1
+ import { ifFlag } from "./command-helpers";
2
+ import { resolvePath } from "./helpers";
3
+ export const teeCommand = {
4
+ name: "tee",
5
+ description: "Read stdin, write to stdout and files",
6
+ category: "text",
7
+ params: ["[-a] <file...>"],
8
+ run: ({ authUser, shell, cwd, args, stdin }) => {
9
+ const append = ifFlag(args, ["-a"]);
10
+ const files = args.filter((a) => !a.startsWith("-"));
11
+ const input = stdin ?? "";
12
+ for (const f of files) {
13
+ const p = resolvePath(cwd, f);
14
+ if (append) {
15
+ const existing = (() => { try {
16
+ return shell.vfs.readFile(p);
17
+ }
18
+ catch {
19
+ return "";
20
+ } })();
21
+ shell.writeFileAsUser(authUser, p, existing + input);
22
+ }
23
+ else {
24
+ shell.writeFileAsUser(authUser, p, input);
25
+ }
26
+ }
27
+ return { stdout: input, exitCode: 0 };
28
+ },
29
+ };
@@ -1 +1 @@
1
- {"version":3,"file":"touch.d.ts","sourceRoot":"","sources":["../../src/commands/touch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGrD,eAAO,MAAM,YAAY,EAAE,WAiB1B,CAAC"}
1
+ {"version":3,"file":"touch.d.ts","sourceRoot":"","sources":["../../src/commands/touch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGrD,eAAO,MAAM,YAAY,EAAE,WAmB1B,CAAC"}