Skip to content

Instantly share code, notes, and snippets.

@mattfysh
Created January 25, 2025 03:45
Show Gist options
  • Save mattfysh/84f27256e13a2facad5bf649dbf229f5 to your computer and use it in GitHub Desktop.
Save mattfysh/84f27256e13a2facad5bf649dbf229f5 to your computer and use it in GitHub Desktop.
Bun extra stdio fd's + IPC
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)
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)
}
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