🚀 Launch Week Day 4:Introducing the Alert Details Page: A Better Way to Explore Alerts.Learn More →
Socket
Book a DemoInstallSign in
Socket

stream-observer

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stream-observer

Listen for data events on a readable stream without triggering flowing mode

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

stream-observer

Listen for data events on a readable stream without triggering flowing mode.

npm

Build status js-standard-style

Why?

Normally a readable stream will start to emit data events (i.e. enter "flowing mode") right after you attach the first data event listener.

But sometimes it's desirable to attach a data event listener without the side effect of enabling flowing mode.

E.g. if you want to subscribe to the content of a stream but are not in charge of when the stream starts flowing - say for instance if you hand the stream off to someone else that are not attaching their own data event listener right away.

This module gives you that ability.

Installation

npm install stream-observer --save

Usage

const fs = require('fs')
const streamObserver = require('stream-observer')

const stream = fs.createReadStream(__filename)

// Register observer to listen for data events once the stream starts
// flowing. Callback will be called with the data chunks.
streamObserver(stream, function (chunk) {
  console.log(`stream produced ${chunk.length} bytes of data`)
})

// Wait a little before starting to read data from the stream.
setTimeout(function () {
  stream.pipe(process.stdout)
}, 1000)

API

fn = streamObserver(stream, callback)

Arguments:

  • stream - the readable stream you wish to observe
  • callback - the callback will be attached to the data event of the stream without triggering flowing mode

Returns a function that you can call if you want to observer to stop observing.

License

MIT

Keywords

readable

FAQs

Package last updated on 25 Jun 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts