fixed naming convention and text casing to use CamelCase on class fields. Process manager implemented, not tested, needs integration with current shell implementation
This commit is contained in:
+55
-54
@@ -9,29 +9,29 @@ import { EventEmitter } from "./event_emitter";
|
||||
import type { ShellEventMap } from "./shell";
|
||||
|
||||
export class Process {
|
||||
private signalQueue: Signal[] = [];
|
||||
private signalHandler: ((signal: Signal) => void) | null = null;
|
||||
private stdoutChunks: Uint8Array[] = [];
|
||||
private stderrChunks: Uint8Array[] = [];
|
||||
private settled: boolean = false;
|
||||
private readonly maxChunkByteSize: number;
|
||||
private _SignalQueue: Signal[] = [];
|
||||
private _SignalHandler: ((signal: Signal) => void) | null = null;
|
||||
private _StdoutChunks: Uint8Array[] = [];
|
||||
private _StderrChunks: Uint8Array[] = [];
|
||||
private _Settled: boolean = false;
|
||||
private readonly _MaxChunkByteSize: number;
|
||||
|
||||
readonly pid: number;
|
||||
readonly commandDescriptor: CommandDescriptor;
|
||||
readonly state: ShellState;
|
||||
readonly args: string[];
|
||||
readonly flags: string[];
|
||||
events: EventEmitter<ShellEventMap>;
|
||||
readonly PID: number;
|
||||
readonly CommandDescriptor: CommandDescriptor;
|
||||
readonly State: ShellState;
|
||||
readonly Args: string[];
|
||||
readonly Flags: string[];
|
||||
EventEmitter: EventEmitter<ShellEventMap>;
|
||||
|
||||
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;
|
||||
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>();
|
||||
this._MaxChunkByteSize = 1024;
|
||||
this.EventEmitter = new EventEmitter<ShellEventMap>();
|
||||
}
|
||||
|
||||
private concatUint8Arrays(arrays: Uint8Array[]): Uint8Array {
|
||||
@@ -49,12 +49,12 @@ export class Process {
|
||||
|
||||
// Tested
|
||||
private divideToChunks(data: Uint8Array): Uint8Array[] {
|
||||
const nChunks: number = Math.ceil(data.length / this.maxChunkByteSize);
|
||||
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);
|
||||
const start = this._MaxChunkByteSize * i;
|
||||
const end = this._MaxChunkByteSize * (i + 1);
|
||||
|
||||
chunks.push(data.subarray(start, end));
|
||||
}
|
||||
@@ -63,49 +63,49 @@ export class Process {
|
||||
}
|
||||
|
||||
private writeToOutput(chunks: Uint8Array[], eventType: "stdout" | "stderr", data: Uint8Array): void {
|
||||
if (this.settled) return;
|
||||
if (this._Settled) return;
|
||||
|
||||
if (data.length <= this.maxChunkByteSize) {
|
||||
if (data.length <= this._MaxChunkByteSize) {
|
||||
chunks.push(data);
|
||||
this.events.emit(eventType, { pid: this.pid, data });
|
||||
this.EventEmitter.emit(eventType, { pid: this.PID, data });
|
||||
return;
|
||||
} else {
|
||||
const arr = this.divideToChunks(data);
|
||||
for (const ent of arr) {
|
||||
chunks.push(ent);
|
||||
this.events.emit(eventType, { pid: this.pid, data: ent });
|
||||
this.EventEmitter.emit(eventType, { pid: this.PID, data: ent });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
start(): void {
|
||||
public execute(): void {
|
||||
const io: CommandIO = {
|
||||
writeStdout: (data: Uint8Array) => this.writeToOutput(this.stdoutChunks, "stdout", data),
|
||||
writeStderr: (data: Uint8Array) => this.writeToOutput(this.stderrChunks, "stderr", data),
|
||||
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();
|
||||
const signal: Signal | undefined = this._SignalQueue.shift();
|
||||
if (signal !== undefined) return Promise.resolve(signal);
|
||||
|
||||
const promise = new Promise<Signal>(resolve => {
|
||||
this.signalHandler = resolve;
|
||||
this._SignalHandler = resolve;
|
||||
});
|
||||
return promise;
|
||||
},
|
||||
};
|
||||
|
||||
const promise = this.commandDescriptor.fn(io, this.state, this.flags, this.args);
|
||||
const promise = this.CommandDescriptor.fn(io, this.State, this.Flags, this.Args);
|
||||
|
||||
promise
|
||||
.then(output => {
|
||||
if (this.settled) return;
|
||||
if (this._Settled) return;
|
||||
|
||||
let result: ResultSuccess | ResultError<ShellError>;
|
||||
const stdout: Uint8Array = this.concatUint8Arrays(this.stdoutChunks);
|
||||
const stderr: Uint8Array = this.concatUint8Arrays(this.stderrChunks);
|
||||
const stdout: Uint8Array = this.concatUint8Arrays(this._StdoutChunks);
|
||||
const stderr: Uint8Array = this.concatUint8Arrays(this._StderrChunks);
|
||||
|
||||
if (output.result.type === "success") {
|
||||
result = createSuccessResult(
|
||||
this.commandDescriptor.name,
|
||||
this.CommandDescriptor.name,
|
||||
0,
|
||||
createImmutableBuffer(stdout),
|
||||
createImmutableBuffer(stderr),
|
||||
@@ -118,11 +118,11 @@ export class Process {
|
||||
};
|
||||
}
|
||||
|
||||
this.events.emit("result", { pid: this.pid, result, state: output.state });
|
||||
this.settled = true;
|
||||
this.EventEmitter.emit("result", { pid: this.PID, result, state: output.state });
|
||||
this._Settled = true;
|
||||
})
|
||||
.catch(error => {
|
||||
if (this.settled) return;
|
||||
if (this._Settled) return;
|
||||
|
||||
const result: ResultError<ShellError> = createErrorResult(
|
||||
"GenericError",
|
||||
@@ -130,44 +130,45 @@ export class Process {
|
||||
{},
|
||||
createImmutableBuffer(new Uint8Array()),
|
||||
createImmutableBuffer(new Uint8Array()),
|
||||
this.commandDescriptor.name,
|
||||
this.CommandDescriptor.name,
|
||||
{
|
||||
name: "Caught error",
|
||||
message: String(error),
|
||||
},
|
||||
);
|
||||
|
||||
this.events.emit("result", { pid: this.pid, result, state: this.state });
|
||||
this.EventEmitter.emit("result", { pid: this.PID, result, state: this.State });
|
||||
});
|
||||
}
|
||||
|
||||
sendSignal(signal: Signal): void {
|
||||
if (this.settled) return;
|
||||
public sendSignal(signal: Signal): void {
|
||||
if (this._Settled) return;
|
||||
|
||||
//Make global signals an enum later on
|
||||
if (signal.type === "SIGKILL") {
|
||||
if (this.signalHandler !== null) {
|
||||
this.signalHandler(signal);
|
||||
this.signalHandler = null;
|
||||
if (this._SignalHandler !== null) {
|
||||
this._SignalHandler(signal);
|
||||
this._SignalHandler = null;
|
||||
}
|
||||
|
||||
this.settled = true;
|
||||
this._Settled = true;
|
||||
const result: ResultError<SignalTerminationError> = createErrorResult(
|
||||
"SignalTermination",
|
||||
`Process ${this.pid} killed by signal sigkill`,
|
||||
`Process ${this.PID} killed by signal sigkill`,
|
||||
{ signalNumber: 9, signalName: signal.type },
|
||||
createImmutableBuffer(new Uint8Array()),
|
||||
createImmutableBuffer(new Uint8Array()),
|
||||
this.commandDescriptor.name,
|
||||
this.CommandDescriptor.name,
|
||||
);
|
||||
|
||||
this.events.emit("result", { pid: this.pid, result, state: this.state });
|
||||
this.EventEmitter.emit("result", { pid: this.PID, result, state: this.State });
|
||||
return;
|
||||
} else {
|
||||
if (this.signalHandler !== null) {
|
||||
this.signalHandler(signal);
|
||||
this.signalHandler = null;
|
||||
if (this._SignalHandler !== null) {
|
||||
this._SignalHandler(signal);
|
||||
this._SignalHandler = null;
|
||||
} else {
|
||||
this.signalQueue.push(signal);
|
||||
this._SignalQueue.push(signal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user