Claimed Bun's singleton stdin reader before loading extensions and passed the owned stream into RPC and RPC-UI mode. Added process-level regressions for both modes with a startup extension that attempts to lock stdin. Fixes #5898
39 lines
859 B
TypeScript
39 lines
859 B
TypeScript
/**
|
|
* Claims Bun's singleton stdin reader immediately and exposes a separately readable stream.
|
|
* RPC startup uses this before extension discovery so in-process modules cannot steal protocol input.
|
|
*/
|
|
export function claimRpcInput(): ReadableStream<Uint8Array> {
|
|
const reader = Bun.stdin.stream().getReader();
|
|
let released = false;
|
|
const release = () => {
|
|
if (released) return;
|
|
released = true;
|
|
try {
|
|
reader.releaseLock();
|
|
} catch {}
|
|
};
|
|
return new ReadableStream({
|
|
async pull(controller) {
|
|
try {
|
|
const result = await reader.read();
|
|
if (result.done) {
|
|
release();
|
|
controller.close();
|
|
} else {
|
|
controller.enqueue(result.value);
|
|
}
|
|
} catch (error) {
|
|
release();
|
|
controller.error(error);
|
|
}
|
|
},
|
|
async cancel() {
|
|
try {
|
|
await reader.cancel();
|
|
} finally {
|
|
release();
|
|
}
|
|
},
|
|
});
|
|
}
|