(
filePath: string,
abortSignal?: AbortSignal
)
| 331 | } |
| 332 | |
| 333 | private writeFileViaExec( |
| 334 | filePath: string, |
| 335 | abortSignal?: AbortSignal |
| 336 | ): WritableStream<Uint8Array> { |
| 337 | const quotedPath = this.quoteForContainer(filePath); |
| 338 | const tempPath = getAtomicWriteTempPath(filePath); |
| 339 | const quotedTempPath = this.quoteForContainer(tempPath); |
| 340 | const writeCommand = `mkdir -p $(dirname ${quotedPath}) && cat > ${quotedTempPath} && mv ${quotedTempPath} ${quotedPath}`; |
| 341 | |
| 342 | let execPromise: Promise<ExecStream> | null = null; |
| 343 | const writeAbortController = new AbortController(); |
| 344 | const abortWrite = () => writeAbortController.abort(); |
| 345 | if (abortSignal?.aborted) { |
| 346 | writeAbortController.abort(); |
| 347 | } else { |
| 348 | abortSignal?.addEventListener("abort", abortWrite, { once: true }); |
| 349 | } |
| 350 | const cleanupAbortForwarder = () => { |
| 351 | abortSignal?.removeEventListener("abort", abortWrite); |
| 352 | }; |
| 353 | |
| 354 | const getExecStream = () => { |
| 355 | execPromise ??= this.exec(writeCommand, { |
| 356 | cwd: this.getContainerBasePath(), |
| 357 | timeout: 300, |
| 358 | abortSignal: writeAbortController.signal, |
| 359 | }); |
| 360 | return execPromise; |
| 361 | }; |
| 362 | |
| 363 | return new WritableStream<Uint8Array>({ |
| 364 | write: async (chunk) => { |
| 365 | const stream = await getExecStream(); |
| 366 | const writer = stream.stdin.getWriter(); |
| 367 | try { |
| 368 | await writer.write(chunk); |
| 369 | } finally { |
| 370 | writer.releaseLock(); |
| 371 | } |
| 372 | }, |
| 373 | close: async () => { |
| 374 | try { |
| 375 | const stream = await getExecStream(); |
| 376 | await stream.stdin.close(); |
| 377 | const exitCode = await stream.exitCode; |
| 378 | |
| 379 | if (exitCode !== 0) { |
| 380 | const stderr = await streamToString(stream.stderr); |
| 381 | throw new RuntimeError(`Failed to write file ${filePath}: ${stderr}`, "file_io"); |
| 382 | } |
| 383 | } finally { |
| 384 | cleanupAbortForwarder(); |
| 385 | } |
| 386 | }, |
| 387 | abort: async (reason?: unknown) => { |
| 388 | writeAbortController.abort(); |
| 389 | if (execPromise) { |
| 390 | try { |
no test coverage detected