typescript-virtual-container 1.3.4 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +0 -1
- package/README.md +674 -1504
- package/benchmark-results.txt +21 -21
- package/builds/self-standalone.js +274 -208
- package/builds/self-standalone.js.map +4 -4
- package/builds/standalone-wo-sftp.js +201 -149
- package/builds/standalone-wo-sftp.js.map +4 -4
- package/builds/standalone.js +263 -211
- package/builds/standalone.js.map +4 -4
- package/builds/web-full-api.min.js +3 -3
- package/builds/web-full-api.min.js.map +4 -4
- package/builds/web.min.js +2 -2
- package/builds/web.min.js.map +4 -4
- package/bun.lock +14 -12
- package/dist/SSHClient/index.d.ts.map +1 -1
- package/dist/SSHClient/index.js +5 -3
- package/dist/SSHMimic/executor.d.ts +1 -3
- package/dist/SSHMimic/executor.d.ts.map +1 -1
- package/dist/SSHMimic/executor.js +20 -22
- package/dist/SSHMimic/index.d.ts.map +1 -1
- package/dist/SSHMimic/index.js +5 -3
- package/dist/SSHMimic/sftp.d.ts.map +1 -1
- package/dist/SSHMimic/sftp.js +26 -21
- package/dist/VirtualShell/shell.d.ts.map +1 -1
- package/dist/VirtualShell/shell.js +25 -3
- package/dist/VirtualShell/shellParser.d.ts +1 -8
- package/dist/VirtualShell/shellParser.d.ts.map +1 -1
- package/dist/VirtualShell/shellParser.js +2 -81
- package/dist/VirtualUserManager/index.d.ts +7 -1
- package/dist/VirtualUserManager/index.d.ts.map +1 -1
- package/dist/VirtualUserManager/index.js +47 -16
- package/dist/commands/adduser.d.ts +10 -4
- package/dist/commands/adduser.d.ts.map +1 -1
- package/dist/commands/adduser.js +75 -12
- package/dist/commands/alias.d.ts +5 -0
- package/dist/commands/alias.d.ts.map +1 -1
- package/dist/commands/alias.js +5 -0
- package/dist/commands/apt.d.ts +5 -0
- package/dist/commands/apt.d.ts.map +1 -1
- package/dist/commands/apt.js +5 -0
- package/dist/commands/awk.d.ts +10 -8
- package/dist/commands/awk.d.ts.map +1 -1
- package/dist/commands/awk.js +156 -28
- package/dist/commands/cd.d.ts.map +1 -1
- package/dist/commands/cd.js +0 -3
- package/dist/commands/clear.d.ts +5 -0
- package/dist/commands/clear.d.ts.map +1 -1
- package/dist/commands/clear.js +5 -0
- package/dist/commands/command-helpers.d.ts.map +1 -1
- package/dist/commands/command-helpers.js +8 -0
- package/dist/commands/declare.d.ts +5 -0
- package/dist/commands/declare.d.ts.map +1 -1
- package/dist/commands/declare.js +5 -0
- package/dist/commands/deluser.d.ts +12 -0
- package/dist/commands/deluser.d.ts.map +1 -1
- package/dist/commands/deluser.js +72 -6
- package/dist/commands/df.d.ts +5 -0
- package/dist/commands/df.d.ts.map +1 -1
- package/dist/commands/df.js +5 -0
- package/dist/commands/du.d.ts +5 -0
- package/dist/commands/du.d.ts.map +1 -1
- package/dist/commands/du.js +5 -0
- package/dist/commands/export.d.ts +5 -0
- package/dist/commands/export.d.ts.map +1 -1
- package/dist/commands/export.js +5 -0
- package/dist/commands/grep.d.ts.map +1 -1
- package/dist/commands/grep.js +22 -4
- package/dist/commands/groups.d.ts +5 -0
- package/dist/commands/groups.d.ts.map +1 -1
- package/dist/commands/groups.js +5 -0
- package/dist/commands/gzip.d.ts +5 -2
- package/dist/commands/gzip.d.ts.map +1 -1
- package/dist/commands/gzip.js +48 -28
- package/dist/commands/head.d.ts.map +1 -1
- package/dist/commands/head.js +12 -3
- package/dist/commands/htop.d.ts +5 -0
- package/dist/commands/htop.d.ts.map +1 -1
- package/dist/commands/htop.js +5 -0
- package/dist/commands/kill.d.ts +5 -0
- package/dist/commands/kill.d.ts.map +1 -1
- package/dist/commands/kill.js +5 -0
- package/dist/commands/ln.d.ts +2 -0
- package/dist/commands/ln.d.ts.map +1 -1
- package/dist/commands/ln.js +22 -0
- package/dist/commands/ls.d.ts.map +1 -1
- package/dist/commands/ls.js +15 -0
- package/dist/commands/lsb-release.d.ts +5 -0
- package/dist/commands/lsb-release.d.ts.map +1 -1
- package/dist/commands/lsb-release.js +5 -0
- package/dist/commands/mkdir.d.ts +5 -0
- package/dist/commands/mkdir.d.ts.map +1 -1
- package/dist/commands/mkdir.js +5 -0
- package/dist/commands/mv.d.ts +5 -0
- package/dist/commands/mv.d.ts.map +1 -1
- package/dist/commands/mv.js +5 -0
- package/dist/commands/nano.d.ts +5 -0
- package/dist/commands/nano.d.ts.map +1 -1
- package/dist/commands/nano.js +5 -0
- package/dist/commands/neofetch.d.ts +5 -0
- package/dist/commands/neofetch.d.ts.map +1 -1
- package/dist/commands/neofetch.js +8 -5
- package/dist/commands/passwd.d.ts +8 -0
- package/dist/commands/passwd.d.ts.map +1 -1
- package/dist/commands/passwd.js +32 -11
- package/dist/commands/ping.d.ts +5 -0
- package/dist/commands/ping.d.ts.map +1 -1
- package/dist/commands/ping.js +5 -0
- package/dist/commands/printf.d.ts +5 -0
- package/dist/commands/printf.d.ts.map +1 -1
- package/dist/commands/printf.js +43 -12
- package/dist/commands/ps.d.ts +5 -0
- package/dist/commands/ps.d.ts.map +1 -1
- package/dist/commands/ps.js +5 -0
- package/dist/commands/read.d.ts +5 -0
- package/dist/commands/read.d.ts.map +1 -1
- package/dist/commands/read.js +5 -0
- package/dist/commands/registry.d.ts.map +1 -1
- package/dist/commands/registry.js +4 -1
- package/dist/commands/rm.d.ts +5 -0
- package/dist/commands/rm.d.ts.map +1 -1
- package/dist/commands/rm.js +5 -0
- package/dist/commands/runtime.d.ts.map +1 -1
- package/dist/commands/runtime.js +1 -57
- package/dist/commands/sed.d.ts +5 -0
- package/dist/commands/sed.d.ts.map +1 -1
- package/dist/commands/sed.js +5 -0
- package/dist/commands/set.d.ts +5 -6
- package/dist/commands/set.d.ts.map +1 -1
- package/dist/commands/set.js +5 -22
- package/dist/commands/sh.d.ts +6 -0
- package/dist/commands/sh.d.ts.map +1 -1
- package/dist/commands/sh.js +6 -0
- package/dist/commands/shift.d.ts +10 -0
- package/dist/commands/shift.d.ts.map +1 -1
- package/dist/commands/shift.js +10 -0
- package/dist/commands/sleep.d.ts +5 -0
- package/dist/commands/sleep.d.ts.map +1 -1
- package/dist/commands/sleep.js +5 -0
- package/dist/commands/sort.d.ts +5 -0
- package/dist/commands/sort.d.ts.map +1 -1
- package/dist/commands/sort.js +5 -0
- package/dist/commands/source.d.ts +5 -0
- package/dist/commands/source.d.ts.map +1 -1
- package/dist/commands/source.js +5 -0
- package/dist/commands/stat.d.ts +7 -0
- package/dist/commands/stat.d.ts.map +1 -0
- package/dist/commands/stat.js +56 -0
- package/dist/commands/su.d.ts +13 -0
- package/dist/commands/su.d.ts.map +1 -1
- package/dist/commands/su.js +45 -14
- package/dist/commands/sudo.d.ts.map +1 -1
- package/dist/commands/sudo.js +5 -0
- package/dist/commands/tail.d.ts +5 -0
- package/dist/commands/tail.d.ts.map +1 -1
- package/dist/commands/tail.js +15 -3
- package/dist/commands/tar.d.ts +5 -0
- package/dist/commands/tar.d.ts.map +1 -1
- package/dist/commands/tar.js +40 -10
- package/dist/commands/tee.d.ts +5 -0
- package/dist/commands/tee.d.ts.map +1 -1
- package/dist/commands/tee.js +5 -0
- package/dist/commands/touch.d.ts +5 -0
- package/dist/commands/touch.d.ts.map +1 -1
- package/dist/commands/touch.js +5 -0
- package/dist/commands/tr.d.ts.map +1 -1
- package/dist/commands/tr.js +45 -10
- package/dist/commands/tree.d.ts +5 -0
- package/dist/commands/tree.d.ts.map +1 -1
- package/dist/commands/tree.js +5 -0
- package/dist/commands/true.d.ts +10 -0
- package/dist/commands/true.d.ts.map +1 -1
- package/dist/commands/true.js +10 -0
- package/dist/commands/type.d.ts +5 -0
- package/dist/commands/type.d.ts.map +1 -1
- package/dist/commands/type.js +5 -0
- package/dist/commands/uname.d.ts +5 -0
- package/dist/commands/uname.d.ts.map +1 -1
- package/dist/commands/uname.js +5 -0
- package/dist/commands/uniq.d.ts +5 -0
- package/dist/commands/uniq.d.ts.map +1 -1
- package/dist/commands/uniq.js +5 -0
- package/dist/commands/unset.d.ts +5 -0
- package/dist/commands/unset.d.ts.map +1 -1
- package/dist/commands/unset.js +5 -0
- package/dist/commands/uptime.d.ts +5 -0
- package/dist/commands/uptime.d.ts.map +1 -1
- package/dist/commands/uptime.js +5 -0
- package/dist/commands/wc.d.ts +5 -0
- package/dist/commands/wc.d.ts.map +1 -1
- package/dist/commands/wc.js +5 -0
- package/dist/commands/wget.d.ts +5 -0
- package/dist/commands/wget.d.ts.map +1 -1
- package/dist/commands/wget.js +5 -0
- package/dist/commands/who.d.ts +5 -0
- package/dist/commands/who.d.ts.map +1 -1
- package/dist/commands/who.js +5 -0
- package/dist/commands/whoami.d.ts +5 -0
- package/dist/commands/whoami.d.ts.map +1 -1
- package/dist/commands/whoami.js +5 -0
- package/dist/commands/xargs.d.ts +5 -0
- package/dist/commands/xargs.d.ts.map +1 -1
- package/dist/commands/xargs.js +5 -0
- package/dist/self-standalone.js +254 -30
- package/dist/types/commands.d.ts +36 -0
- package/dist/types/commands.d.ts.map +1 -1
- package/dist/utils/tokenize.d.ts +20 -0
- package/dist/utils/tokenize.d.ts.map +1 -0
- package/dist/utils/tokenize.js +74 -0
- package/examples/web.min.js +2 -2
- package/package.json +1 -1
- package/src/SSHClient/index.ts +6 -3
- package/src/SSHMimic/executor.ts +21 -44
- package/src/SSHMimic/index.ts +7 -5
- package/src/SSHMimic/sftp.ts +28 -21
- package/src/VirtualShell/shell.ts +34 -4
- package/src/VirtualShell/shellParser.ts +2 -103
- package/src/VirtualUserManager/index.ts +43 -19
- package/src/commands/adduser.ts +86 -13
- package/src/commands/alias.ts +5 -0
- package/src/commands/apt.ts +5 -0
- package/src/commands/awk.ts +154 -29
- package/src/commands/cd.ts +0 -4
- package/src/commands/clear.ts +5 -0
- package/src/commands/command-helpers.ts +9 -0
- package/src/commands/declare.ts +5 -0
- package/src/commands/deluser.ts +84 -7
- package/src/commands/df.ts +5 -0
- package/src/commands/du.ts +5 -0
- package/src/commands/export.ts +5 -0
- package/src/commands/grep.ts +21 -8
- package/src/commands/groups.ts +5 -0
- package/src/commands/gzip.ts +54 -28
- package/src/commands/head.ts +14 -4
- package/src/commands/htop.ts +5 -0
- package/src/commands/kill.ts +5 -0
- package/src/commands/ln.ts +22 -0
- package/src/commands/ls.ts +17 -0
- package/src/commands/lsb-release.ts +5 -0
- package/src/commands/mkdir.ts +5 -0
- package/src/commands/mv.ts +5 -0
- package/src/commands/nano.ts +5 -0
- package/src/commands/neofetch.ts +8 -6
- package/src/commands/passwd.ts +35 -12
- package/src/commands/ping.ts +5 -0
- package/src/commands/printf.ts +30 -13
- package/src/commands/ps.ts +5 -0
- package/src/commands/read.ts +5 -0
- package/src/commands/registry.ts +4 -1
- package/src/commands/rm.ts +5 -0
- package/src/commands/runtime.ts +1 -61
- package/src/commands/sed.ts +5 -0
- package/src/commands/set.ts +5 -24
- package/src/commands/sh.ts +9 -3
- package/src/commands/shift.ts +10 -0
- package/src/commands/sleep.ts +5 -0
- package/src/commands/sort.ts +5 -0
- package/src/commands/source.ts +5 -0
- package/src/commands/stat.ts +61 -0
- package/src/commands/su.ts +54 -16
- package/src/commands/sudo.ts +5 -0
- package/src/commands/tail.ts +17 -3
- package/src/commands/tar.ts +38 -15
- package/src/commands/tee.ts +5 -0
- package/src/commands/touch.ts +5 -0
- package/src/commands/tr.ts +54 -10
- package/src/commands/tree.ts +5 -0
- package/src/commands/true.ts +10 -0
- package/src/commands/type.ts +5 -0
- package/src/commands/uname.ts +5 -0
- package/src/commands/uniq.ts +5 -0
- package/src/commands/unset.ts +5 -0
- package/src/commands/uptime.ts +5 -0
- package/src/commands/wc.ts +5 -0
- package/src/commands/wget.ts +5 -0
- package/src/commands/who.ts +5 -0
- package/src/commands/whoami.ts +5 -0
- package/src/commands/xargs.ts +5 -0
- package/src/self-standalone.ts +316 -33
- package/src/types/commands.ts +37 -0
- package/src/utils/tokenize.ts +78 -0
- package/builds/web-iife.min.js +0 -13
- package/builds/web-iife.min.js.map +0 -7
package/src/commands/shift.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import type { ShellModule } from "../types/commands";
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Shift positional parameters (remove first N arguments).
|
|
5
|
+
* @category shell
|
|
6
|
+
* @params ["[n]"]
|
|
7
|
+
*/
|
|
3
8
|
export const shiftCommand: ShellModule = {
|
|
4
9
|
name: "shift",
|
|
5
10
|
description: "Shift positional parameters",
|
|
@@ -22,6 +27,11 @@ export const shiftCommand: ShellModule = {
|
|
|
22
27
|
},
|
|
23
28
|
};
|
|
24
29
|
|
|
30
|
+
/**
|
|
31
|
+
* Trap signals and execute actions on signal receipt or shell exit.
|
|
32
|
+
* @category shell
|
|
33
|
+
* @params ["[action] [signal...]"]
|
|
34
|
+
*/
|
|
25
35
|
export const trapCommand: ShellModule = {
|
|
26
36
|
name: "trap",
|
|
27
37
|
description: "Trap signals and events",
|
package/src/commands/sleep.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import type { ShellModule } from "../types/commands";
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Delay execution for a specified number of seconds.
|
|
5
|
+
* @category system
|
|
6
|
+
* @params ["<seconds>"]
|
|
7
|
+
*/
|
|
3
8
|
export const sleepCommand: ShellModule = {
|
|
4
9
|
name: "sleep",
|
|
5
10
|
description: "Delay execution",
|
package/src/commands/sort.ts
CHANGED
|
@@ -2,6 +2,11 @@ import type { ShellModule } from "../types/commands";
|
|
|
2
2
|
import { ifFlag } from "./command-helpers";
|
|
3
3
|
import { assertPathAccess, resolvePath } from "./helpers";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Sort lines of text with various options (reverse, numeric, unique).
|
|
7
|
+
* @category text
|
|
8
|
+
* @params ["[-r] [-n] [-u] [-k <col>] [file...]"]
|
|
9
|
+
*/
|
|
5
10
|
export const sortCommand: ShellModule = {
|
|
6
11
|
name: "sort",
|
|
7
12
|
description: "Sort lines of text",
|
package/src/commands/source.ts
CHANGED
|
@@ -2,6 +2,11 @@ import type { ShellModule } from "../types/commands";
|
|
|
2
2
|
import { resolvePath } from "./helpers";
|
|
3
3
|
import { runCommand } from "./runtime";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Execute commands from a file in the current shell environment.
|
|
7
|
+
* @category shell
|
|
8
|
+
* @params ["<file> [args...]"]
|
|
9
|
+
*/
|
|
5
10
|
export const sourceCommand: ShellModule = {
|
|
6
11
|
name: "source",
|
|
7
12
|
aliases: ["."],
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { ShellModule } from "../types/commands";
|
|
2
|
+
import { resolvePath } from "./helpers";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Display file or filesystem status.
|
|
6
|
+
* Outputs: path, size, mode, type, timestamps — similar to `stat` on Linux.
|
|
7
|
+
*/
|
|
8
|
+
export const statCommand: ShellModule = {
|
|
9
|
+
name: "stat",
|
|
10
|
+
description: "Display file status",
|
|
11
|
+
category: "files",
|
|
12
|
+
params: ["[-c <format>] <file>"],
|
|
13
|
+
run: ({ shell, cwd, args }) => {
|
|
14
|
+
const fmtIdx = args.findIndex((a) => a === "-c" || a === "--format");
|
|
15
|
+
const fmt = fmtIdx !== -1 ? args[fmtIdx + 1] : undefined;
|
|
16
|
+
const file = args.find((a) => !a.startsWith("-") && a !== fmt);
|
|
17
|
+
if (!file) return { stderr: "stat: missing operand\n", exitCode: 1 };
|
|
18
|
+
|
|
19
|
+
const p = resolvePath(cwd, file);
|
|
20
|
+
if (!shell.vfs.exists(p)) {
|
|
21
|
+
return { stderr: `stat: cannot stat '${file}': No such file or directory\n`, exitCode: 1 };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const st = shell.vfs.stat(p);
|
|
25
|
+
const isDir = st.type === "directory";
|
|
26
|
+
const _isLink = shell.vfs.isSymlink(p);
|
|
27
|
+
const isSymlink = shell.vfs.isSymlink(p);
|
|
28
|
+
const modePerm = (mode: number): string => {
|
|
29
|
+
const bits = [0o400,0o200,0o100,0o040,0o020,0o010,0o004,0o002,0o001];
|
|
30
|
+
const syms = ["r","w","x","r","w","x","r","w","x"];
|
|
31
|
+
return (isDir ? "d" : isSymlink ? "l" : "-") +
|
|
32
|
+
bits.map((b, i) => mode & b ? syms[i] : "-").join("");
|
|
33
|
+
};
|
|
34
|
+
const octal = (st.mode).toString(8).padStart(4, "0");
|
|
35
|
+
const modeStr = modePerm(st.mode);
|
|
36
|
+
const size = "size" in st ? (st as {size: number}).size : 0;
|
|
37
|
+
const ts = (d: Date) => d.toISOString().replace("T", " ").replace(/\.\d+Z$/, " +0000");
|
|
38
|
+
|
|
39
|
+
// -c format string support
|
|
40
|
+
if (fmt) {
|
|
41
|
+
const out = fmt
|
|
42
|
+
.replace("%n", file)
|
|
43
|
+
.replace("%s", String(size))
|
|
44
|
+
.replace("%a", octal.slice(1)) // access in octal (no leading 0)
|
|
45
|
+
.replace("%A", modeStr)
|
|
46
|
+
.replace("%F", isSymlink ? "symbolic link" : isDir ? "directory" : "regular file")
|
|
47
|
+
.replace("%y", ts(st.updatedAt))
|
|
48
|
+
.replace("%z", ts(st.updatedAt));
|
|
49
|
+
return { stdout: `${out}\n`, exitCode: 0 };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const out = [
|
|
53
|
+
` File: ${file}${isSymlink ? ` -> ${shell.vfs.resolveSymlink(p)}` : ""}`,
|
|
54
|
+
` Size: ${size}${"\t".repeat(3)}${isSymlink ? "symbolic link" : isDir ? "directory" : "regular file"}`,
|
|
55
|
+
`Access: (${octal}/${modeStr}) Uid: ( 0/ root) Gid: ( 0/ root)`,
|
|
56
|
+
`Modify: ${ts(st.updatedAt)}`,
|
|
57
|
+
`Change: ${ts(st.updatedAt)}`,
|
|
58
|
+
].join("\n");
|
|
59
|
+
return { stdout: `${out}\n`, exitCode: 0 };
|
|
60
|
+
},
|
|
61
|
+
};
|
package/src/commands/su.ts
CHANGED
|
@@ -1,33 +1,71 @@
|
|
|
1
1
|
import type { ShellModule } from "../types/commands";
|
|
2
|
-
import {
|
|
2
|
+
import { runCommand } from "./runtime";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Switch to another user account.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* su [username] — switch to username (defaults to root)
|
|
9
|
+
* su - [username] — login shell (changes cwd to target home)
|
|
10
|
+
* su -c 'cmd' [user] — run command as user
|
|
11
|
+
*
|
|
12
|
+
* - Root can switch to any user without a password.
|
|
13
|
+
* - Non-root sudoers must enter the target user's password.
|
|
14
|
+
* - Non-sudoers are denied.
|
|
15
|
+
* - Switching to a non-existent user returns an error immediately.
|
|
16
|
+
*/
|
|
4
17
|
export const suCommand: ShellModule = {
|
|
5
18
|
name: "su",
|
|
6
19
|
description: "Switch user",
|
|
7
20
|
category: "users",
|
|
8
|
-
params: ["- <username
|
|
9
|
-
run: ({ authUser, shell, args }) => {
|
|
10
|
-
const
|
|
11
|
-
const
|
|
21
|
+
params: ["[-] [-c <cmd>] [username]"],
|
|
22
|
+
run: async ({ authUser, shell, args, hostname, mode, cwd }) => {
|
|
23
|
+
const loginShellFlag = args.includes("-") || args.includes("-l") || args.includes("--login");
|
|
24
|
+
const cIdx = args.indexOf("-c");
|
|
25
|
+
const cmdLine = cIdx !== -1 ? args[cIdx + 1] : undefined;
|
|
26
|
+
const filteredArgs = args.filter((_, i) =>
|
|
27
|
+
i !== cIdx && i !== cIdx + 1
|
|
28
|
+
).filter((a) => a !== "-" && a !== "-l" && a !== "--login");
|
|
29
|
+
const targetUser = filteredArgs.find((a) => !a.startsWith("-")) ?? "root";
|
|
12
30
|
|
|
13
|
-
|
|
14
|
-
|
|
31
|
+
// Verify target user exists
|
|
32
|
+
if (!shell.users.listUsers().includes(targetUser)) {
|
|
33
|
+
return { stderr: `su: user '${targetUser}' does not exist\n`, exitCode: 1 };
|
|
15
34
|
}
|
|
16
35
|
|
|
17
|
-
|
|
18
|
-
|
|
36
|
+
// Root switches freely without any password
|
|
37
|
+
if (authUser === "root") {
|
|
38
|
+
if (cmdLine) {
|
|
39
|
+
return runCommand(
|
|
40
|
+
cmdLine,
|
|
41
|
+
targetUser,
|
|
42
|
+
hostname,
|
|
43
|
+
mode,
|
|
44
|
+
loginShellFlag ? `/home/${targetUser}` : cwd,
|
|
45
|
+
shell,
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
switchUser: targetUser,
|
|
50
|
+
nextCwd: loginShellFlag ? `/home/${targetUser}` : undefined,
|
|
51
|
+
exitCode: 0,
|
|
52
|
+
};
|
|
19
53
|
}
|
|
20
54
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
) {
|
|
25
|
-
return { stderr: "su: authentication failure", exitCode: 1 };
|
|
55
|
+
// Non-sudoers denied
|
|
56
|
+
if (!shell.users.isSudoer(authUser)) {
|
|
57
|
+
return { stderr: "su: permission denied\n", exitCode: 1 };
|
|
26
58
|
}
|
|
27
59
|
|
|
60
|
+
// Sudoers must enter target user's password via challenge
|
|
28
61
|
return {
|
|
29
|
-
|
|
30
|
-
|
|
62
|
+
sudoChallenge: {
|
|
63
|
+
username: targetUser,
|
|
64
|
+
targetUser,
|
|
65
|
+
commandLine: cmdLine ?? null,
|
|
66
|
+
loginShell: loginShellFlag,
|
|
67
|
+
prompt: "Password: ",
|
|
68
|
+
},
|
|
31
69
|
exitCode: 0,
|
|
32
70
|
};
|
|
33
71
|
},
|
package/src/commands/sudo.ts
CHANGED
|
@@ -2,6 +2,11 @@ import type { ShellModule } from "../types/commands";
|
|
|
2
2
|
import { parseArgs } from "./command-helpers";
|
|
3
3
|
import { runCommand } from "./runtime";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Execute a command as another user (superuser by default).
|
|
7
|
+
* @category users
|
|
8
|
+
* @params ["[-u user] [-c cmd] [command]"]
|
|
9
|
+
*/
|
|
5
10
|
function parseSudoArgs(args: string[]): {
|
|
6
11
|
targetUser: string;
|
|
7
12
|
loginShell: boolean;
|
package/src/commands/tail.ts
CHANGED
|
@@ -2,6 +2,11 @@ import type { ShellModule } from "../types/commands";
|
|
|
2
2
|
import { getFlag } from "./command-helpers";
|
|
3
3
|
import { assertPathAccess, resolvePath } from "./helpers";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Output the last part of files or stdin.
|
|
7
|
+
* @category text
|
|
8
|
+
* @params ["[-n <lines>] [file...]"]
|
|
9
|
+
*/
|
|
5
10
|
export const tailCommand: ShellModule = {
|
|
6
11
|
name: "tail",
|
|
7
12
|
description: "Output last lines",
|
|
@@ -9,12 +14,21 @@ export const tailCommand: ShellModule = {
|
|
|
9
14
|
params: ["[-n <lines>] [file...]"],
|
|
10
15
|
run: ({ authUser, shell, cwd, args, stdin }) => {
|
|
11
16
|
const nArg = getFlag(args, ["-n"]);
|
|
12
|
-
const
|
|
13
|
-
const
|
|
17
|
+
const shortN = args.find((a) => /^-\d+$/.test(a));
|
|
18
|
+
const n = typeof nArg === "string"
|
|
19
|
+
? parseInt(nArg, 10)
|
|
20
|
+
: shortN ? parseInt(shortN.slice(1), 10) : 10;
|
|
21
|
+
const positionals = args.filter(
|
|
22
|
+
(a) => !a.startsWith("-") && a !== nArg && a !== String(n),
|
|
23
|
+
);
|
|
14
24
|
|
|
15
25
|
const take = (content: string) => {
|
|
16
26
|
const lines = content.split("\n");
|
|
17
|
-
|
|
27
|
+
// If content ends with \n, last element is ""; exclude from count
|
|
28
|
+
const hasTrailingNewline = content.endsWith("\n");
|
|
29
|
+
const meaningful = hasTrailingNewline ? lines.slice(0, -1) : lines;
|
|
30
|
+
const sliced = meaningful.slice(Math.max(0, meaningful.length - n));
|
|
31
|
+
return sliced.join("\n") + (hasTrailingNewline ? "\n" : "");
|
|
18
32
|
};
|
|
19
33
|
|
|
20
34
|
if (positionals.length === 0) {
|
package/src/commands/tar.ts
CHANGED
|
@@ -1,32 +1,55 @@
|
|
|
1
1
|
import type { ShellModule } from "../types/commands";
|
|
2
|
-
import { ifFlag } from "./command-helpers";
|
|
3
2
|
import { resolvePath } from "./helpers";
|
|
4
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Archive or extract files with tar and optional gzip compression.
|
|
6
|
+
* @category archive
|
|
7
|
+
* @params ["[-czf|-xzf|-tf] <archive> [files...]"]
|
|
8
|
+
*/
|
|
5
9
|
export const tarCommand: ShellModule = {
|
|
6
10
|
name: "tar",
|
|
7
11
|
description: "Archive utility",
|
|
8
12
|
category: "archive",
|
|
9
13
|
params: ["[-czf|-xzf|-tf] <archive> [files...]"],
|
|
10
14
|
run: ({ authUser, shell, cwd, args }) => {
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
15
|
+
// Expand combined flags: -czf or czf (bare mode string) → ["-c", "-z", "-f"]
|
|
16
|
+
const expanded: string[] = [];
|
|
17
|
+
let foundModeStr = false;
|
|
18
|
+
for (const a of args) {
|
|
19
|
+
if (/^-[a-zA-Z]{2,}$/.test(a)) {
|
|
20
|
+
// -czf style
|
|
21
|
+
for (const ch of a.slice(1)) expanded.push(`-${ch}`);
|
|
22
|
+
} else if (!foundModeStr && /^[cxtdru]{1,}[a-zA-Z]*$/.test(a) && !a.includes("/") && !a.startsWith("-")) {
|
|
23
|
+
// czf bare style (first non-path arg)
|
|
24
|
+
foundModeStr = true;
|
|
25
|
+
for (const ch of a) expanded.push(`-${ch}`);
|
|
26
|
+
} else {
|
|
27
|
+
expanded.push(a);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const create = expanded.includes("-c");
|
|
32
|
+
const extract = expanded.includes("-x");
|
|
33
|
+
const list = expanded.includes("-t");
|
|
34
|
+
const fIdx = expanded.indexOf("-f");
|
|
35
|
+
const archiveName = fIdx !== -1
|
|
36
|
+
? expanded[fIdx + 1]
|
|
37
|
+
: expanded.find((a) => a.endsWith(".tar") || a.endsWith(".tar.gz") || a.endsWith(".tgz"));
|
|
38
|
+
|
|
39
|
+
if (!create && !extract && !list) {
|
|
40
|
+
return { stderr: "tar: must specify -c, -x, or -t\n", exitCode: 1 };
|
|
41
|
+
}
|
|
22
42
|
|
|
23
43
|
if (!archiveName)
|
|
24
|
-
return { stderr: "tar: no archive specified", exitCode: 1 };
|
|
44
|
+
return { stderr: "tar: no archive specified\n", exitCode: 1 };
|
|
25
45
|
const archivePath = resolvePath(cwd, archiveName);
|
|
26
46
|
|
|
27
47
|
if (create) {
|
|
28
|
-
|
|
29
|
-
|
|
48
|
+
// Skip flags and archive name from file list
|
|
49
|
+
const skipNext2 = new Set<number>();
|
|
50
|
+
if (fIdx !== -1) skipNext2.add(fIdx + 1);
|
|
51
|
+
const fileArgs = expanded.filter((a, i) =>
|
|
52
|
+
!a.startsWith("-") && a !== archiveName && !skipNext2.has(i),
|
|
30
53
|
);
|
|
31
54
|
const entries: Record<string, string> = {};
|
|
32
55
|
for (const f of fileArgs) {
|
package/src/commands/tee.ts
CHANGED
|
@@ -2,6 +2,11 @@ import type { ShellModule } from "../types/commands";
|
|
|
2
2
|
import { ifFlag } from "./command-helpers";
|
|
3
3
|
import { resolvePath } from "./helpers";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Read stdin and write to stdout and files simultaneously.
|
|
7
|
+
* @category text
|
|
8
|
+
* @params ["[-a] <file...>"]
|
|
9
|
+
*/
|
|
5
10
|
export const teeCommand: ShellModule = {
|
|
6
11
|
name: "tee",
|
|
7
12
|
description: "Read stdin, write to stdout and files",
|
package/src/commands/touch.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import type { ShellModule } from "../types/commands";
|
|
2
2
|
import { assertPathAccess, resolvePath } from "./helpers";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Create empty files or update file timestamps.
|
|
6
|
+
* @category files
|
|
7
|
+
* @params ["<file>"]
|
|
8
|
+
*/
|
|
4
9
|
export const touchCommand: ShellModule = {
|
|
5
10
|
name: "touch",
|
|
6
11
|
description: "Create or update files",
|
package/src/commands/tr.ts
CHANGED
|
@@ -1,26 +1,70 @@
|
|
|
1
1
|
import type { ShellModule } from "../types/commands";
|
|
2
2
|
import { ifFlag } from "./command-helpers";
|
|
3
3
|
|
|
4
|
+
function unescapeTrSet(s: string): string {
|
|
5
|
+
return s
|
|
6
|
+
.replace(/\\n/g, "\n")
|
|
7
|
+
.replace(/\\t/g, "\t")
|
|
8
|
+
.replace(/\\r/g, "\r")
|
|
9
|
+
.replace(/\\\\/g, "\\");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function expandTrSet(s: string): string[] {
|
|
13
|
+
const chars: string[] = [];
|
|
14
|
+
const unescaped = unescapeTrSet(s);
|
|
15
|
+
let i = 0;
|
|
16
|
+
while (i < unescaped.length) {
|
|
17
|
+
// Range: a-z, A-Z, 0-9
|
|
18
|
+
if (i + 2 < unescaped.length && unescaped[i + 1] === "-") {
|
|
19
|
+
const from = unescaped.charCodeAt(i);
|
|
20
|
+
const to = unescaped.charCodeAt(i + 2);
|
|
21
|
+
if (from <= to) {
|
|
22
|
+
for (let c = from; c <= to; c++) chars.push(String.fromCharCode(c));
|
|
23
|
+
i += 3;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
chars.push(unescaped[i]!);
|
|
28
|
+
i++;
|
|
29
|
+
}
|
|
30
|
+
return chars;
|
|
31
|
+
}
|
|
32
|
+
|
|
4
33
|
export const trCommand: ShellModule = {
|
|
5
34
|
name: "tr",
|
|
6
35
|
description: "Translate or delete characters",
|
|
7
36
|
category: "text",
|
|
8
|
-
params: ["[-d] <set1> [set2]"],
|
|
37
|
+
params: ["[-d] [-s] <set1> [set2]"],
|
|
9
38
|
run: ({ args, stdin }) => {
|
|
10
|
-
const del
|
|
39
|
+
const del = ifFlag(args, ["-d"]);
|
|
40
|
+
const squeeze = ifFlag(args, ["-s"]);
|
|
11
41
|
const positionals = args.filter((a) => !a.startsWith("-"));
|
|
12
|
-
const
|
|
13
|
-
const
|
|
42
|
+
const set1chars = expandTrSet(positionals[0] ?? "");
|
|
43
|
+
const set2chars = expandTrSet(positionals[1] ?? "");
|
|
44
|
+
|
|
14
45
|
let input = stdin ?? "";
|
|
46
|
+
|
|
15
47
|
if (del) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
48
|
+
const deleteSet = new Set(set1chars);
|
|
49
|
+
input = [...input].filter((c) => !deleteSet.has(c)).join("");
|
|
50
|
+
} else if (set2chars.length > 0) {
|
|
51
|
+
// Build translation map
|
|
52
|
+
const map = new Map<string, string>();
|
|
53
|
+
for (let i = 0; i < set1chars.length; i++) {
|
|
54
|
+
map.set(
|
|
55
|
+
set1chars[i]!,
|
|
56
|
+
set2chars[i] ?? set2chars[set2chars.length - 1] ?? "",
|
|
57
|
+
);
|
|
22
58
|
}
|
|
59
|
+
input = [...input].map((c) => map.get(c) ?? c).join("");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (squeeze && set2chars.length > 0) {
|
|
63
|
+
// Squeeze repeated characters in set2
|
|
64
|
+
const squeezeSet = new Set(set2chars);
|
|
65
|
+
input = input.replace(/(.)\1+/g, (_, c) => squeezeSet.has(c) ? c : _);
|
|
23
66
|
}
|
|
67
|
+
|
|
24
68
|
return { stdout: input, exitCode: 0 };
|
|
25
69
|
},
|
|
26
70
|
};
|
package/src/commands/tree.ts
CHANGED
|
@@ -2,6 +2,11 @@ import type { ShellModule } from "../types/commands";
|
|
|
2
2
|
import { getArg } from "./command-helpers";
|
|
3
3
|
import { assertPathAccess, resolvePath } from "./helpers";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Display directory structure in a tree format.
|
|
7
|
+
* @category navigation
|
|
8
|
+
* @params ["[path]"]
|
|
9
|
+
*/
|
|
5
10
|
export const treeCommand: ShellModule = {
|
|
6
11
|
name: "tree",
|
|
7
12
|
description: "Display directory tree",
|
package/src/commands/true.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import type { ShellModule } from "../types/commands";
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Always return success (exit code 0).
|
|
5
|
+
* @category shell
|
|
6
|
+
* @params []
|
|
7
|
+
*/
|
|
3
8
|
export const trueCommand: ShellModule = {
|
|
4
9
|
name: "true",
|
|
5
10
|
description: "Return success exit code",
|
|
@@ -8,6 +13,11 @@ export const trueCommand: ShellModule = {
|
|
|
8
13
|
run: () => ({ exitCode: 0 }),
|
|
9
14
|
};
|
|
10
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Always return failure (exit code 1).
|
|
18
|
+
* @category shell
|
|
19
|
+
* @params []
|
|
20
|
+
*/
|
|
11
21
|
export const falseCommand: ShellModule = {
|
|
12
22
|
name: "false",
|
|
13
23
|
description: "Return failure exit code",
|
package/src/commands/type.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import type { ShellModule } from "../types/commands";
|
|
2
2
|
import { resolveModule } from "./registry";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Describe how a command name would be interpreted (builtin, function, or file).
|
|
6
|
+
* @category shell
|
|
7
|
+
* @params ["<command...>"]
|
|
8
|
+
*/
|
|
4
9
|
export const typeCommand: ShellModule = {
|
|
5
10
|
name: "type",
|
|
6
11
|
description: "Describe how a command would be interpreted",
|
package/src/commands/uname.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import type { ShellModule } from "../types/commands";
|
|
2
2
|
import { ifFlag } from "./command-helpers";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Print system information (kernel name, version, machine type).
|
|
6
|
+
* @category system
|
|
7
|
+
* @params ["[-a] [-s] [-r] [-m]"]
|
|
8
|
+
*/
|
|
4
9
|
export const unameCommand: ShellModule = {
|
|
5
10
|
name: "uname",
|
|
6
11
|
description: "Print system information",
|
package/src/commands/uniq.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import type { ShellModule } from "../types/commands";
|
|
2
2
|
import { ifFlag } from "./command-helpers";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Report or filter out repeated consecutive lines.
|
|
6
|
+
* @category text
|
|
7
|
+
* @params ["[-c] [-d] [-u] [file]"]
|
|
8
|
+
*/
|
|
4
9
|
export const uniqCommand: ShellModule = {
|
|
5
10
|
name: "uniq",
|
|
6
11
|
description: "Report or filter out repeated lines",
|
package/src/commands/unset.ts
CHANGED
package/src/commands/uptime.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import type { ShellModule } from "../types/commands";
|
|
2
2
|
import { ifFlag } from "./command-helpers";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Tell how long the system has been running.
|
|
6
|
+
* @category system
|
|
7
|
+
* @params ["[-p] [-s]"]
|
|
8
|
+
*/
|
|
4
9
|
export const uptimeCommand: ShellModule = {
|
|
5
10
|
name: "uptime",
|
|
6
11
|
description: "Tell how long the system has been running",
|
package/src/commands/wc.ts
CHANGED
|
@@ -2,6 +2,11 @@ import type { ShellModule } from "../types/commands";
|
|
|
2
2
|
import { ifFlag } from "./command-helpers";
|
|
3
3
|
import { assertPathAccess, resolvePath } from "./helpers";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Count words, lines, and/or bytes in files or stdin.
|
|
7
|
+
* @category text
|
|
8
|
+
* @params ["[-l] [-w] [-c] [file...]"]
|
|
9
|
+
*/
|
|
5
10
|
export const wcCommand: ShellModule = {
|
|
6
11
|
name: "wc",
|
|
7
12
|
description: "Count words/lines/bytes",
|
package/src/commands/wget.ts
CHANGED
|
@@ -2,6 +2,11 @@ import type { ShellModule } from "../types/commands";
|
|
|
2
2
|
import { ifFlag, parseArgs } from "./command-helpers";
|
|
3
3
|
import { assertPathAccess, resolvePath, stripUrlFilename } from "./helpers";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Download files from the web (fetch-based implementation).
|
|
7
|
+
* @category network
|
|
8
|
+
* @params ["[options] <url>"]
|
|
9
|
+
*/
|
|
5
10
|
export const wgetCommand: ShellModule = {
|
|
6
11
|
name: "wget",
|
|
7
12
|
description: "File downloader (pure fetch)",
|
package/src/commands/who.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { formatLoginDate } from "../SSHMimic/loginFormat";
|
|
2
2
|
import type { ShellModule } from "../types/commands";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Show active user sessions.
|
|
6
|
+
* @category system
|
|
7
|
+
* @params []
|
|
8
|
+
*/
|
|
4
9
|
export const whoCommand: ShellModule = {
|
|
5
10
|
name: "who",
|
|
6
11
|
description: "Show active sessions",
|
package/src/commands/whoami.ts
CHANGED
package/src/commands/xargs.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import type { ShellModule } from "../types/commands";
|
|
2
2
|
import { runCommand } from "./runtime";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Build and execute commands from stdin arguments.
|
|
6
|
+
* @category text
|
|
7
|
+
* @params ["[command] [args...]"]
|
|
8
|
+
*/
|
|
4
9
|
export const xargsCommand: ShellModule = {
|
|
5
10
|
name: "xargs",
|
|
6
11
|
description: "Build and execute command lines from stdin",
|