| 35 | * Execute a command and buffer all output into strings |
| 36 | */ |
| 37 | export async function execBuffered( |
| 38 | runtime: Runtime, |
| 39 | command: string, |
| 40 | options: ExecOptions & { stdin?: string } |
| 41 | ): Promise<ExecResult> { |
| 42 | const stream = await runtime.exec(command, options); |
| 43 | |
| 44 | // Write stdin if provided |
| 45 | if (options.stdin !== undefined) { |
| 46 | const writer = stream.stdin.getWriter(); |
| 47 | try { |
| 48 | await writer.write(new TextEncoder().encode(options.stdin)); |
| 49 | await writer.close(); |
| 50 | } catch (err) { |
| 51 | writer.releaseLock(); |
| 52 | throw err; |
| 53 | } |
| 54 | } else { |
| 55 | // Close stdin immediately if no input |
| 56 | await stream.stdin.close(); |
| 57 | } |
| 58 | |
| 59 | // Read stdout and stderr concurrently |
| 60 | const [stdout, stderr, exitCode, duration] = await Promise.all([ |
| 61 | streamToString(stream.stdout), |
| 62 | streamToString(stream.stderr), |
| 63 | stream.exitCode, |
| 64 | stream.duration, |
| 65 | ]); |
| 66 | |
| 67 | return { stdout, stderr, exitCode, duration }; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Read file contents as a UTF-8 string |