WRITE UNIT TESTS!!!
first async command working, no unit tests done AGAING
This commit is contained in:
+71
-25
@@ -1,18 +1,40 @@
|
||||
import { COMMAND_REGISTRY } from "@core/command_registry";
|
||||
import { createErrorResult, createSuccessResult, type Result, type ResultError } from "@core/result";
|
||||
import { createErrorResult, type Result } from "@core/result";
|
||||
import { getCurrentSession } from "@core/session_manager";
|
||||
import { withExitCode, withHistory, type ShellState } from "@core/shell_state";
|
||||
import type { CommandNotFoundError, ParseError, PermissionDeniedError, ShellError } from "@core/util/errors";
|
||||
import type { ShellError } from "@core/util/errors";
|
||||
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
||||
import { parseCommand } from "@core/util/parser";
|
||||
import { EventEmitter } from "./event_emitter";
|
||||
import { ProcessManager } from "./process_manager";
|
||||
|
||||
export class Shell {
|
||||
static #Instance: Shell;
|
||||
static #Promise: Promise<Shell> | null = null;
|
||||
private _State: Readonly<ShellState>;
|
||||
private _ProcessManager: ProcessManager = new ProcessManager();
|
||||
private _EventEmitter: EventEmitter<ShellEventMap> = new EventEmitter();
|
||||
private _GlobalKeyMap: ReadonlyMap<string, string>;
|
||||
|
||||
public BoundPID: number | null = null;
|
||||
|
||||
private constructor(state: ShellState) {
|
||||
this._State = Object.freeze(state);
|
||||
this._GlobalKeyMap = new 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._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);
|
||||
});
|
||||
}
|
||||
|
||||
static async init(initialState: ShellState): Promise<Shell> {
|
||||
@@ -35,64 +57,76 @@ export class Shell {
|
||||
return Shell.#Promise;
|
||||
}
|
||||
|
||||
public async Execute(input: string): Promise<Result<ShellError>> {
|
||||
public async execute(input: string): Promise<void> {
|
||||
let parsed;
|
||||
try {
|
||||
parsed = parseCommand(input);
|
||||
} catch {
|
||||
return createErrorResult(
|
||||
const result = createErrorResult(
|
||||
"ParseError",
|
||||
`Parsing error - ${input}`,
|
||||
{},
|
||||
createImmutableBuffer(new Uint8Array()),
|
||||
createImmutableBuffer(new Uint8Array()),
|
||||
"",
|
||||
) satisfies ResultError<ParseError>;
|
||||
);
|
||||
|
||||
this._State = withExitCode(this._State, result.error.exitCode);
|
||||
this._EventEmitter.emit("result", { pid: 0, result, state: this._State });
|
||||
return;
|
||||
}
|
||||
|
||||
if (parsed.command === "") {
|
||||
return createSuccessResult(
|
||||
"",
|
||||
0,
|
||||
createImmutableBuffer(new Uint8Array()),
|
||||
createImmutableBuffer(new Uint8Array()),
|
||||
);
|
||||
}
|
||||
if (parsed.command === "") return;
|
||||
|
||||
this._State = withHistory(this._State, input);
|
||||
const command = COMMAND_REGISTRY.get(parsed.command);
|
||||
|
||||
if (!command)
|
||||
return createErrorResult(
|
||||
if (!command) {
|
||||
const result = createErrorResult(
|
||||
"CommandNotFound",
|
||||
`Command ${parsed.command} not found`,
|
||||
{ command: parsed.command },
|
||||
createImmutableBuffer(new Uint8Array()),
|
||||
createImmutableBuffer(new Uint8Array()),
|
||||
parsed.command,
|
||||
) satisfies ResultError<CommandNotFoundError>;
|
||||
);
|
||||
|
||||
this._State = withExitCode(this._State, result.error.exitCode);
|
||||
this._EventEmitter.emit("result", { pid: 0, result, state: this._State });
|
||||
return;
|
||||
}
|
||||
|
||||
if (command.requiresRoot && getCurrentSession(this._State).user.uid != 0) {
|
||||
return createErrorResult(
|
||||
const result = createErrorResult(
|
||||
"PermissionDenied",
|
||||
`Permission Denied`,
|
||||
{ resource: `Execution of the ${command.name} command` },
|
||||
createImmutableBuffer(new Uint8Array()),
|
||||
createImmutableBuffer(new Uint8Array()),
|
||||
parsed.command,
|
||||
) satisfies ResultError<PermissionDeniedError>;
|
||||
);
|
||||
|
||||
this._State = withExitCode(this._State, result.error.exitCode);
|
||||
this._EventEmitter.emit("result", { pid: 0, result, state: this._State });
|
||||
return;
|
||||
}
|
||||
|
||||
const out = command.fn(this._State, parsed.flags, parsed.args);
|
||||
this._State = out.state;
|
||||
const pid = this._ProcessManager.createProcess(this._State, command, parsed.flags, parsed.args);
|
||||
this.BoundPID = pid;
|
||||
}
|
||||
|
||||
if (out.result.type === "error") {
|
||||
this._State = withExitCode(this._State, out.result.error.exitCode);
|
||||
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 {
|
||||
this._State = withExitCode(this._State, out.result.exitCode);
|
||||
if (this.BoundPID !== null) {
|
||||
this._ProcessManager.sendSignal(this.BoundPID, { type: "INPUT", payload: input });
|
||||
}
|
||||
}
|
||||
|
||||
return out.result;
|
||||
}
|
||||
|
||||
public get State(): Readonly<ShellState> {
|
||||
@@ -106,6 +140,18 @@ export class Shell {
|
||||
}
|
||||
return Shell.#Instance;
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user