File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -32,6 +32,36 @@ async function pipedBytes(write: string) {
3232}
3333
3434describe . skipIf ( process . platform === "win32" ) ( "writeStdout" , ( ) => {
35+ test ( "rejects when the consumer closes before the write" , async ( ) => {
36+ const writer = `
37+ import { writeStdout } from ${ JSON . stringify ( stdio ) }
38+ process.stdout.on("error", () => {})
39+ await Bun.stdin.text()
40+ try {
41+ await writeStdout("x".repeat(${ size } ))
42+ process.exit(0)
43+ } catch (error) {
44+ process.stderr.write(error.code)
45+ process.exit(1)
46+ }
47+ `
48+ const child = Bun . spawn ( [ process . execPath , "-e" , writer ] , {
49+ stdin : "pipe" ,
50+ stdout : "pipe" ,
51+ stderr : "pipe" ,
52+ } )
53+ try {
54+ await child . stdout . cancel ( )
55+ child . stdin . end ( )
56+ const [ stderr , exit ] = await Promise . all ( [ new Response ( child . stderr ) . text ( ) , child . exited ] )
57+ expect ( stderr ) . toBe ( "EPIPE" )
58+ expect ( exit ) . toBe ( 1 )
59+ } finally {
60+ child . kill ( )
61+ await child . exited
62+ }
63+ } )
64+
3565 test ( "delivers a large piped write in full before process.exit" , async ( ) => {
3666 expect ( await pipedBytes ( "await writeStdout(text)" ) ) . toBe ( size )
3767 } )
Original file line number Diff line number Diff line change 33export function writeStdout ( text : string ) {
44 return new Promise < void > ( ( resolve , reject ) => {
55 process . stdout . write ( text , ( error ) => {
6- // The reader closed the pipe early, e.g. `opencode api get ... | head`.
7- if ( error && ( error as NodeJS . ErrnoException ) . code !== "EPIPE" ) return reject ( error )
6+ if ( error ) return reject ( error )
87 resolve ( )
98 } )
109 } )
You can’t perform that action at this time.
0 commit comments