hook-stream
Allows listening to a given stream's writes through a new, readable stream.
Useful for listening to a 3rd party library's writes to stdout
or stderr
.
install
npm install --save hook-stream
API
hookStream(stream: Stream, options?: Object): [unhook: Function, readable: Stream]
Takes a stream
to hook into and optional options
object.
Returns a tuple of unhook
function and a newly created readable stream that
emits writes to stream
.
example
Direct writes to stderr
to stdout
.
const hookStream = const map = const treis = const unhook stream = stream consoleprocessstderr// treis prints function input and output to stderr1
output
% node examples/1.jsSTDOUT foofooSTDOUT barbarSTDOUT λ1 x: 1λ1 x: 1STDOUT λ1 => 1λ1 => 1
caveat
This method does not work if a third party lib saves a reference to "original"
stream.write
(e.g. process.stderr.write
) before hookStream
is called.
// somewhere else, maybe in some lib's codeconst log = processstderrwrite // ..... // won't work because hookStream replaces process.stderr.write with its own// function, but log already has reference to the originalconst unhook stream =