GithubHelp home page GithubHelp logo

hook-stream's Introduction

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 = require('hook-stream')
const map = require('through2-map')
const treis = require('treis')

const [ unhook, stream ] = hookStream(process.stderr)

stream
  .pipe(map((x) => `STDOUT ${x}`))
  .pipe(process.stdout)

console.error('foo')
process.stderr.write('bar')
// treis prints function input and output to stderr
treis(x => x)(1)

unhook()

output

% node examples/1.js
STDOUT foo
foo
STDOUT bar
bar
STDOUT λ1 x: 1
λ1 x: 1
STDOUT λ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 code
const log = process.stderr.write.bind(process.stderr)

// .....

// won't work because hookStream replaces process.stderr.write with its own
// function, but log already has reference to the original
const [ unhook, stream ] = hookStream(process.stderr)

hook-stream's People

Contributors

raine avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.