Created
January 25, 2025 03:45
-
-
Save mattfysh/84f27256e13a2facad5bf649dbf229f5 to your computer and use it in GitHub Desktop.
Bun extra stdio fd's + IPC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'node:fs' | |
import { log } from './log.js' | |
const fds = fs.readdirSync('/dev/fd').join(' ') | |
console.log('child.fds', fds) | |
process.send('test IPC') | |
log('node', 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'node:fs' | |
export function log(proc, fd) { | |
const reader = fs.createReadStream(null, { fd }) | |
const td = new TextDecoder() | |
const writer = fs.createWriteStream(null, { fd }) | |
async function read() { | |
for await (const chunk of reader) { | |
const text = td.decode(chunk) | |
console.log(`[${proc}]:${fd} >read :: ${text}`) | |
} | |
} | |
function write() { | |
writer.write(`[${proc}]:${fd} >write`) | |
} | |
read() | |
write() | |
setInterval(write, 2_000) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Bun from 'bun' | |
import { log } from './log.js' | |
const child = Bun.spawn({ | |
cmd: ['node', 'child.js'], | |
stdio: ['inherit', 'inherit', 'inherit', 'pipe'], | |
serialization: 'json', | |
ipc(msg) { | |
console.log('[bun] IPC', msg) | |
}, | |
onExit(_, exitCode) { | |
console.log('[bun] child.exitCode', exitCode) | |
} | |
}); | |
console.log('child.stdio', child.stdio) | |
log('bun', child.stdio.at(-1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment