drain-aware-transform
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

DrainAwareTransform

Backpressure-aware stream.Transform class

Well-behaved node.js Readable streams should respect the Stream API and stop pushing data when push() returns false. The problem is that there is no built-in way for a Readable to know when its internal buffer is drained enough to resume pushing.

This class extends stream.Transform with an event 'continue' that is analogous to the 'drain' event of stream.Writable. When push() returns false, the transform waits until its buffer is drained and the _read() private method is called again. Then 'continue' is emitted and the transform can continue pushing data.

A convenience method 'safePush' is also provided. 'safePush' pushes data and then returns a Promise that resolves when it is safe to push again.

abstract class DrainAwareTransform extends stream.Transform {
  protected safePush(chunk: any, encoding?: BufferEncoding): Promise<void>;
}

Getting Started

npm install 'drain-aware-transform'
import DrainAwareTransform from 'drain-aware-transform';

class MyTransform extends DrainAwareTransform {

  _transform(chunk, encoding, callback) {
    // your implementation
  }

  _flush(callback) {
    // your implementation
  }
}

Package Sidebar

Install

npm i drain-aware-transform

Weekly Downloads

7

Version

1.0.1

License

CC0-1.0

Unpacked Size

12.8 kB

Total Files

5

Last publish

Collaborators

  • dmurvihill