diff --git a/src/core/command.ts b/src/core/command.ts index 6c66299..644d318 100644 --- a/src/core/command.ts +++ b/src/core/command.ts @@ -1,8 +1,14 @@ +import type { CommandIO } from "./command_io"; import type { Result } from "./result"; import type { ShellState } from "./shell_state"; import type { ShellError } from "./util/errors"; -type CommandFn = (state: ShellState, flags: string[], args: string[]) => CommandOutput; +export type CommandFn = ( + io: CommandIO, + state: ShellState, + flags: string[], + args: string[], +) => CommandOutput; export interface CommandDescriptor { fn: CommandFn; diff --git a/src/core/process_context.ts b/src/core/command_io.ts similarity index 82% rename from src/core/process_context.ts rename to src/core/command_io.ts index c09a4b6..fb64c84 100644 --- a/src/core/process_context.ts +++ b/src/core/command_io.ts @@ -1,6 +1,6 @@ import type { Signal } from "./signal"; -export interface ProcessContext { +export interface CommandIO { writeStdout(data: Uint8Array): void; writeStderr(data: Uint8Array): void; getSignal(): Promise; diff --git a/src/shell/process.ts b/src/shell/process.ts new file mode 100644 index 0000000..f2c0be5 --- /dev/null +++ b/src/shell/process.ts @@ -0,0 +1,47 @@ +import type { CommandFn } from "@core/command"; +import type { ProcessContext } from "@core/command_io"; +import type { Signal } from "@core/signal"; +import type { 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 readonly maxChunkByteSize: number; + + pid: number; + name: string; + commandFn: CommandFn; + state: ShellState; + args: string[]; + flags: string[]; + events: EventEmitter; + + constructor() { + this.maxChunkByteSize = 1024; + } + + private divideToChunks(data: Uint8Array): Uint8Array[] {} + + private writeToOutput(chunks: Uint8Array[], eventType: "stdout" | "stderr", data: Uint8Array): void { + chunks.push(data); + + if (data.length <= this.maxChunkByteSize) { + this.events.emit(eventType, { pid: this.pid, data }); + return; + } else { + const chunk = data.slice(this.maxChunkByteSize); + } + } + + private chunkData(data: Uint8Array): Uint8Array[] {} + + start(): void { + const context: ProcessContext = { + writeStdout: data => this.writeOutput(data), + }; + } +} diff --git a/src/shell/shell.ts b/src/shell/shell.ts index 493a606..f651790 100644 --- a/src/shell/shell.ts +++ b/src/shell/shell.ts @@ -112,5 +112,5 @@ export interface ShellEventMap { process: { pid: number; name: string }; stdout: { pid: number; data: Uint8Array }; stderr: { pid: number; data: Uint8Array }; - result: { pid: number; result: Result }; + result: { pid: number; result: Result; state: ShellState }; }