Process class implemented, not tested, all green for now, need to reevaluate the test-suite and write tests for new code and changed one
This commit is contained in:
+1
-1
@@ -8,7 +8,7 @@ export type CommandFn = (
|
||||
state: ShellState,
|
||||
flags: string[],
|
||||
args: string[],
|
||||
) => CommandOutput<ShellError>;
|
||||
) => Promise<CommandOutput<ShellError>>;
|
||||
|
||||
export interface CommandDescriptor {
|
||||
fn: CommandFn;
|
||||
|
||||
@@ -18,7 +18,7 @@ export function createShellError<E extends ShellError>(
|
||||
let exitCode = EXIT_CODE_MAP[errorType] ?? 1;
|
||||
|
||||
// Special handling for signal termination
|
||||
if (errorType === "SIGNAL_TERMINATION") {
|
||||
if (errorType === "SignalTermination") {
|
||||
const signalError = extraProps as unknown as { signalNumber: number };
|
||||
exitCode = 128 + signalError.signalNumber;
|
||||
}
|
||||
|
||||
+149
-22
@@ -1,47 +1,174 @@
|
||||
import type { CommandFn } from "@core/command";
|
||||
import type { ProcessContext } from "@core/command_io";
|
||||
import type { CommandDescriptor } from "@core/command";
|
||||
import type { CommandIO } from "@core/command_io";
|
||||
import { createErrorResult, createSuccessResult, type ResultError, type ResultSuccess } from "@core/result";
|
||||
import type { ShellState } from "@core/shell_state";
|
||||
import type { Signal } from "@core/signal";
|
||||
import type { EventEmitter } from "./event_emitter";
|
||||
import type { ShellError, SignalTerminationError } from "@core/util/errors";
|
||||
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
||||
import { EventEmitter } from "./event_emitter";
|
||||
import type { ShellEventMap } from "./shell";
|
||||
|
||||
export class Process {
|
||||
private signalQueue: Signal[];
|
||||
private signalHandler: ((signal: Signal) => void) | null;
|
||||
private stdoutChunks: Uint8Array[];
|
||||
private stderrChunks: Uint8Array[];
|
||||
private settled: boolean;
|
||||
private signalQueue: Signal[] = [];
|
||||
private signalHandler: ((signal: Signal) => void) | null = null;
|
||||
private stdoutChunks: Uint8Array[] = [];
|
||||
private stderrChunks: Uint8Array[] = [];
|
||||
private settled: boolean = false;
|
||||
private readonly maxChunkByteSize: number;
|
||||
|
||||
pid: number;
|
||||
name: string;
|
||||
commandFn: CommandFn;
|
||||
state: ShellState;
|
||||
args: string[];
|
||||
flags: string[];
|
||||
readonly pid: number;
|
||||
readonly commandDescriptor: CommandDescriptor;
|
||||
readonly state: ShellState;
|
||||
readonly args: string[];
|
||||
readonly flags: string[];
|
||||
events: EventEmitter<ShellEventMap>;
|
||||
|
||||
constructor() {
|
||||
constructor(pid: number, state: ShellState, descriptor: CommandDescriptor, flags: string[], args: string[]) {
|
||||
this.pid = pid;
|
||||
this.state = state;
|
||||
this.commandDescriptor = descriptor;
|
||||
this.flags = flags;
|
||||
this.args = args;
|
||||
// Check if positive later and throw an error if so
|
||||
this.maxChunkByteSize = 1024;
|
||||
this.events = new EventEmitter<ShellEventMap>();
|
||||
}
|
||||
|
||||
private divideToChunks(data: Uint8Array): Uint8Array[] {}
|
||||
private concatUint8Arrays(arrays: Uint8Array[]): Uint8Array {
|
||||
const size = arrays.reduce((sum, arr) => sum + arr.length, 0);
|
||||
const result = new Uint8Array(size);
|
||||
|
||||
let offset = 0;
|
||||
for (const arr of arrays) {
|
||||
result.set(arr, offset);
|
||||
offset += arr.length;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Tested
|
||||
private divideToChunks(data: Uint8Array): Uint8Array[] {
|
||||
const nChunks: number = Math.ceil(data.length / this.maxChunkByteSize);
|
||||
const chunks: Uint8Array[] = [];
|
||||
|
||||
for (let i = 0; i < nChunks; i++) {
|
||||
const start = this.maxChunkByteSize * i;
|
||||
const end = this.maxChunkByteSize * (i + 1);
|
||||
|
||||
chunks.push(data.subarray(start, end));
|
||||
}
|
||||
|
||||
return chunks;
|
||||
}
|
||||
|
||||
private writeToOutput(chunks: Uint8Array[], eventType: "stdout" | "stderr", data: Uint8Array): void {
|
||||
chunks.push(data);
|
||||
if (this.settled) return;
|
||||
|
||||
if (data.length <= this.maxChunkByteSize) {
|
||||
chunks.push(data);
|
||||
this.events.emit(eventType, { pid: this.pid, data });
|
||||
return;
|
||||
} else {
|
||||
const chunk = data.slice(this.maxChunkByteSize);
|
||||
const arr = this.divideToChunks(data);
|
||||
for (const ent of arr) {
|
||||
chunks.push(ent);
|
||||
this.events.emit(eventType, { pid: this.pid, data: ent });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private chunkData(data: Uint8Array): Uint8Array[] {}
|
||||
|
||||
start(): void {
|
||||
const context: ProcessContext = {
|
||||
writeStdout: data => this.writeOutput(data),
|
||||
const io: CommandIO = {
|
||||
writeStdout: (data: Uint8Array) => this.writeToOutput(this.stdoutChunks, "stdout", data),
|
||||
writeStderr: (data: Uint8Array) => this.writeToOutput(this.stderrChunks, "stderr", data),
|
||||
getSignal: () => {
|
||||
const signal: Signal | undefined = this.signalQueue.shift();
|
||||
if (signal !== undefined) return Promise.resolve(signal);
|
||||
|
||||
const promise = new Promise<Signal>(resolve => {
|
||||
this.signalHandler = resolve;
|
||||
});
|
||||
return promise;
|
||||
},
|
||||
};
|
||||
|
||||
const promise = this.commandDescriptor.fn(io, this.state, this.flags, this.args);
|
||||
|
||||
promise
|
||||
.then(output => {
|
||||
if (this.settled) return;
|
||||
|
||||
let result: ResultSuccess | ResultError<ShellError>;
|
||||
const stdout: Uint8Array = this.concatUint8Arrays(this.stdoutChunks);
|
||||
const stderr: Uint8Array = this.concatUint8Arrays(this.stderrChunks);
|
||||
|
||||
if (output.result.type === "success") {
|
||||
result = createSuccessResult(
|
||||
this.commandDescriptor.name,
|
||||
0,
|
||||
createImmutableBuffer(stdout),
|
||||
createImmutableBuffer(stderr),
|
||||
);
|
||||
} else {
|
||||
result = {
|
||||
...output.result,
|
||||
stdout: createImmutableBuffer(stdout),
|
||||
stderr: createImmutableBuffer(stderr),
|
||||
};
|
||||
}
|
||||
|
||||
this.events.emit("result", { pid: this.pid, result, state: output.state });
|
||||
this.settled = true;
|
||||
})
|
||||
.catch(error => {
|
||||
if (this.settled) return;
|
||||
|
||||
const result: ResultError<ShellError> = createErrorResult(
|
||||
"GenericError",
|
||||
"Promised output from command function rejected",
|
||||
{},
|
||||
createImmutableBuffer(new Uint8Array()),
|
||||
createImmutableBuffer(new Uint8Array()),
|
||||
this.commandDescriptor.name,
|
||||
{
|
||||
name: "Caught error",
|
||||
message: String(error),
|
||||
},
|
||||
);
|
||||
|
||||
this.events.emit("result", { pid: this.pid, result, state: this.state });
|
||||
});
|
||||
}
|
||||
|
||||
sendSignal(signal: Signal): void {
|
||||
if (this.settled) return;
|
||||
|
||||
if (signal.type === "SIGKILL") {
|
||||
if (this.signalHandler !== null) {
|
||||
this.signalHandler(signal);
|
||||
this.signalHandler = null;
|
||||
}
|
||||
|
||||
this.settled = true;
|
||||
const result: ResultError<SignalTerminationError> = createErrorResult(
|
||||
"SignalTermination",
|
||||
`Process ${this.pid} killed by signal sigkill`,
|
||||
{ signalNumber: 9, signalName: signal.type },
|
||||
createImmutableBuffer(new Uint8Array()),
|
||||
createImmutableBuffer(new Uint8Array()),
|
||||
this.commandDescriptor.name,
|
||||
);
|
||||
|
||||
this.events.emit("result", { pid: this.pid, result, state: this.state });
|
||||
return;
|
||||
} else {
|
||||
if (this.signalHandler !== null) {
|
||||
this.signalHandler(signal);
|
||||
this.signalHandler = null;
|
||||
} else {
|
||||
this.signalQueue.push(signal);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user