MCPcopy Create free account
hub / github.com/Sandpack/nodebox-runtime / ShellProcess

Class ShellProcess

packages/nodebox/src/modules/shell.ts:35–152  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

33}
34
35export class ShellProcess {
36 public id?: string;
37 public state: 'running' | 'idle';
38
39 public stdout: Emitter<{
40 data: [string];
41 }>;
42 public stderr: Emitter<{
43 data: [string];
44 }>;
45 public stdin: {
46 write: (data: string | Uint8Array) => Promise<void>;
47 };
48
49 constructor(private readonly channel: MessageSender) {
50 this.state = 'running';
51 this.stdout = new Emitter();
52 this.stderr = new Emitter();
53 this.stdin = {
54 write: (data: string | Uint8Array): Promise<void> => {
55 if (!this.id) {
56 throw new Error('Failed to write to stdin, no process is currently running');
57 }
58
59 return this.channel.send('shell/stdin', { data: data, workerId: this.id });
60 },
61 };
62
63 this.forwardStdEvents();
64 }
65
66 private forwardStdEvents(): void {
67 this.channel.on('worker/tty', (message) => {
68 const { data } = message;
69
70 if (data.workerId !== this.id) {
71 return;
72 }
73
74 switch (data.payload.type) {
75 case 'out': {
76 this.stdout.emit('data', data.payload.data);
77 break;
78 }
79
80 case 'err': {
81 this.stderr.emit('data', data.payload.data);
82 break;
83 }
84 }
85 });
86 }
87
88 /**
89 * Evaluates a given module in the File
90 */
91 public async runCommand(command: string, args: Array<string>, options: ShellCommandOptions = {}): Promise<ShellInfo> {
92 invariant(!this.id, 'Failed to run "runCommand" on a ShellProcess: there is already a process running.');

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected