141 lines
5.3 KiB
TypeScript
141 lines
5.3 KiB
TypeScript
import { COMMAND_REGISTRY } from "@core/command_registry";
|
|
import { createErrorResult, type Result } from "@core/result";
|
|
import { getCurrentSession, type SessionState } from "@core/session_manager";
|
|
import { withExitCode, withHistory, type ShellState } from "@core/shell_state";
|
|
import { SIGNAL_MAP } from "@core/signal";
|
|
import type { ShellError } from "@core/util/errors";
|
|
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
|
import { tokenize } from "@core/util/parser";
|
|
import { EventEmitter } from "./event_emitter";
|
|
import { ProcessManager } from "./process_manager";
|
|
|
|
export interface TTYState {
|
|
width: number;
|
|
height: number;
|
|
}
|
|
|
|
export class Shell {
|
|
private _State: Readonly<ShellState>;
|
|
private _Session: SessionState;
|
|
private _ProcessManager: ProcessManager = new ProcessManager();
|
|
private _EventEmitter: EventEmitter<ShellEventMap> = new EventEmitter();
|
|
private _GlobalKeyMap: ReadonlyMap<string, SIGNAL_MAP>;
|
|
|
|
public BoundPID: number | null = null;
|
|
|
|
constructor(state: ShellState) {
|
|
this._State = Object.freeze(state);
|
|
this._Session = state.sessionStack[state.sessionStack.length - 1]!;
|
|
|
|
//Temporary solution for sigint keybind
|
|
const map = new Map();
|
|
map.set("ctrl+c", SIGNAL_MAP.SIGINT);
|
|
|
|
this._GlobalKeyMap = map;
|
|
|
|
this._ProcessManager.on("stdout", payload => this._EventEmitter.emit("stdout", payload));
|
|
this._ProcessManager.on("stderr", payload => this._EventEmitter.emit("stderr", payload));
|
|
this._ProcessManager.on("process", payload => this._EventEmitter.emit("process", payload));
|
|
this._ProcessManager.on("result", payload => {
|
|
this._State = payload.state;
|
|
this._Session = this._State.sessionStack[this._State.sessionStack.length - 1]!;
|
|
this._State = withExitCode(
|
|
this._State,
|
|
payload.result.type === "success" ? payload.result.exitCode : payload.result.error.exitCode,
|
|
);
|
|
|
|
if (this.BoundPID === payload.pid) this.BoundPID = null;
|
|
this._EventEmitter.emit("result", payload);
|
|
});
|
|
this._ProcessManager.on("signal", payload => this._EventEmitter.emit("signal", payload));
|
|
}
|
|
|
|
public async execute(input: string): Promise<void> {
|
|
const parsed = tokenize(input);
|
|
|
|
if (!parsed[0]) return;
|
|
|
|
this._State = withHistory(this._State, input);
|
|
const command = COMMAND_REGISTRY.get(parsed[0]);
|
|
|
|
if (!command) {
|
|
const encoded = new TextEncoder().encode(`Command ${parsed[0]} not found`);
|
|
|
|
const result = createErrorResult(
|
|
"CommandNotFound",
|
|
`Command ${parsed[0]} not found`,
|
|
{ command: parsed[0] },
|
|
parsed[0],
|
|
createImmutableBuffer(encoded),
|
|
createImmutableBuffer(new Uint8Array()),
|
|
);
|
|
|
|
this._State = withExitCode(this._State, result.error.exitCode);
|
|
this._Session = this._State.sessionStack[this._State.sessionStack.length - 1]!;
|
|
this._EventEmitter.emit("result", { pid: 0, result, state: this._State });
|
|
return;
|
|
}
|
|
|
|
if (command.requiresRoot && getCurrentSession(this._State).user.uid != 0) {
|
|
const result = createErrorResult(
|
|
"PermissionDenied",
|
|
`Permission Denied`,
|
|
{ resource: `Execution of the ${command.name} command` },
|
|
parsed[0],
|
|
);
|
|
|
|
this._State = withExitCode(this._State, result.error.exitCode);
|
|
this._Session = this._State.sessionStack[this._State.sessionStack.length - 1]!;
|
|
this._EventEmitter.emit("result", { pid: 0, result, state: this._State });
|
|
return;
|
|
}
|
|
|
|
parsed.shift();
|
|
|
|
const pid = this._ProcessManager.createProcess(this._State, command, parsed);
|
|
this.BoundPID = pid;
|
|
}
|
|
|
|
public sendInput(input: string): void {
|
|
const signal = this._GlobalKeyMap.get(input);
|
|
|
|
if (signal !== undefined) {
|
|
if (this.BoundPID !== null) {
|
|
this._ProcessManager.sendSignal(this.BoundPID, { type: signal });
|
|
}
|
|
} else {
|
|
if (this.BoundPID !== null) {
|
|
this._ProcessManager.sendSignal(this.BoundPID, { type: SIGNAL_MAP.INPUT, payload: input });
|
|
}
|
|
}
|
|
}
|
|
|
|
public get State(): Readonly<ShellState> {
|
|
return this._State;
|
|
}
|
|
|
|
public get Session(): Readonly<SessionState> {
|
|
return this._Session;
|
|
}
|
|
|
|
public on<K extends keyof ShellEventMap>(type: K, handler: (payload: ShellEventMap[K]) => void): () => void {
|
|
return this._EventEmitter.on(type, handler);
|
|
}
|
|
|
|
public once<K extends keyof ShellEventMap>(type: K, handler: (payload: ShellEventMap[K]) => void): () => void {
|
|
return this._EventEmitter.once(type, handler);
|
|
}
|
|
|
|
public off<K extends keyof ShellEventMap>(type: K, handler: (payload: ShellEventMap[K]) => void): void {
|
|
this._EventEmitter.off(type, handler);
|
|
}
|
|
}
|
|
|
|
export interface ShellEventMap {
|
|
process: { pid: number; name: string };
|
|
stdout: { pid: number; data: Uint8Array };
|
|
stderr: { pid: number; data: Uint8Array };
|
|
result: { pid: number; result: Result<ShellError>; state: ShellState };
|
|
signal: { pid: number; sig: SIGNAL_MAP };
|
|
}
|