Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stream to/from a file descriptor in node?

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

Note I need Streams, so fs.open/fs.read/fs.write are not sufficient.

like image 401
Bartvds Avatar asked Jul 05 '14 01:07

Bartvds


2 Answers

When you call fs.createReadStream you can pass 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 a fd option, the filename is ignored.

like image 188
Joe Hildebrand Avatar answered Oct 23 '22 06:10

Joe Hildebrand


// 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");
like image 4
Fordi Avatar answered Oct 23 '22 05:10

Fordi