How to pass to / from file descriptor in node?

fs.createReadStream() and fs.createWriteStream() only support file paths, but I need to read (or write) from a file descriptor (passed / from a child process).

Note. I need threads, so fs.open/fs.read/fs.write not enough.

+6
stream file-descriptor child-process
source share
2 answers

When you call fs.createReadStream , you can pass it in a file descriptor:

 var fs = require('fs'); var fd = fs.openSync('/tmp/tmp.js', 'r'); var s = fs.createReadStream(null, {fd: fd}); s.pipe(process.stdout); 

If there is an fd option, the file name is ignored.

+11
source share
 // Open &3: process.oob1 = fs.createWriteStream(null, { fd: 3 }); // Write to &3 / oob1 (out-of-band 1) process.oob1.write("Note: this will throw an exception without 3>&1 or something else declaring the existence of &3"); 
+1
source share

All Articles