Skip to content

Commit ae1b55b

Browse files
committed
Use only documented methods of FileSink on Bun.
1 parent 1f8def3 commit ae1b55b

1 file changed

Lines changed: 26 additions & 4 deletions

File tree

src/spawn/bun.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
/**
2+
* Adapt Bun's `FileSink` to a Web Streams `UnderlyingSink`. `FileSink` exposes
3+
* `write`/`end` (close + flush) but no `close`/`abort` matching the spec, so
4+
* `new WritableStream(fileSink)` would only work because of an undocumented
5+
* runtime `.close` alias. This adapter sticks to the documented `.end` API.
6+
*
7+
* @param {import('bun').FileSink} sink
8+
* @returns {UnderlyingSink}
9+
*/
10+
const makeStdinSink = sink => ({
11+
async write(/** @type {any} */ chunk) {
12+
await sink.write(chunk);
13+
},
14+
async close() {
15+
await sink.end();
16+
},
17+
async abort(reason) {
18+
await sink.end(reason instanceof Error ? reason : new Error(String(reason)));
19+
}
20+
});
21+
122
const sanitize = (value, defaultValue = 'ignore') => {
223
switch (value) {
324
case 'pipe':
@@ -47,10 +68,11 @@ class Subprocess {
4768
return code;
4869
});
4970

50-
// @ts-expect-error TODO: Bun's `FileSink` is not a Web Streams `UnderlyingSink` —
51-
// `start`/`write` happen to line up but `close` does not (FileSink uses `end`),
52-
// so closing this WritableStream won't signal EOF to the child process.
53-
this.stdin = this.childProcess.stdin ? new WritableStream(this.childProcess.stdin) : null;
71+
const stdinSink = this.childProcess.stdin;
72+
this.stdin =
73+
stdinSink && typeof stdinSink !== 'number'
74+
? new WritableStream(makeStdinSink(stdinSink))
75+
: null;
5476
this.stdout = this.childProcess.stdout || null;
5577
this.stderr = this.childProcess.stderr || null;
5678
}

0 commit comments

Comments
 (0)