All files index.js

100% Statements 311/311
100% Branches 42/42
100% Functions 12/12
100% Lines 311/311

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 3131x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 3x 3x 3x 4x 4x 29x 29x 29x 30x 30x 30x 2x 2x 29x 29x 29x 13x 13x 13x 17x 17x 29x 29x 29x 25x 25x 29x 29x 29x 29x 210x 210x 210x 3x 3x 3x 3x 3x 210x 210x 29x 29x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 54x 54x 54x 3x 3x 54x 54x 58x 58x 58x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 33x 33x 33x 33x 33x 33x 33x 18x 18x 1x 1x 1x 30x 30x 30x 30x 30x 1x 1x 1x 29x 29x 11x 11x 30x 30x 30x 30x 30x 30x 30x 30x 11x 11x 1x 1x 1x 27x 27x 27x 27x 20x 20x 20x 18x 18x 18x 18x 20x 27x 27x 23x 23x 23x 15x 15x 23x 27x 27x 18x 18x 18x 18x 18x 18x 18x 18x 18x 8x 8x 5x 5x 8x 8x 8x 10x 10x 10x 18x 27x 27x 27x 48x 48x 48x 48x 8x 8x 40x 40x 32x 32x 32x 32x 32x 48x 27x 27x 27x 27x 27x 8x 8x 8x 19x 19x 1x 1x 1x 6x 6x 6x 5x 5x 4x 4x 5x 6x 6x 1x 1x 1x    
/**
# fastest-writable   [![Build Status](https://github.com/davedoesdev/fastest-writable/actions/workflows/ci.yml/badge.svg)](https://github.com/davedoesdev/fastest-writable/actions/workflows/ci.yml) [![Coverage Status](https://coveralls.io/repos/davedoesdev/fastest-writable/badge.png?branch=master)](https://coveralls.io/r/davedoesdev/fastest-writable?branch=master) [![NPM version](https://badge.fury.io/js/fastest-writable.png)](http://badge.fury.io/js/fastest-writable)
 
Node.js [`Writable`](http://nodejs.org/docs/v0.11.13/api/stream.html#stream_class_stream_writable) which goes at the speed of its fastest peer `Writable` and ends peers which can't keep up.
 
- Alternative to [`readable.pipe`](http://nodejs.org/docs/v0.11.13/api/stream.html#stream_readable_pipe_destination_options) which goes at the rate of the slowest peer.
- Peers which aren't drained when a write occurs are ended.
- With no peers added, `fastest-writable` consumes data as fast as it is written and throws it away.
- Full set of unit tests with 100% coverage.
 
Example:
 
```javascript
var stream = require('stream'),
    assert = require('assert'),
    FastestWritable = require('fastest-writable').FastestWritable,
    source = new stream.PassThrough(),
    fw = new FastestWritable(),
    dest1 = new stream.PassThrough({ highWaterMark: 1 }),
    dest2 = new stream.PassThrough({ highWaterMark: 1 });
 
source.pipe(fw);
fw.add_peer(dest1);
fw.add_peer(dest2);
 
source.write('foo');
assert.equal(dest1.read().toString(), 'foo');
source.write('bar');
// drain emitted next tick
process.nextTick(function ()
{
    assert(dest2._writableState.ended);
});
```
 
The API is described [here](#tableofcontents).
 
## Installation
 
```shell
npm install fastest-writable
```
 
## Licence
 
[MIT](LICENCE)
 
## Test
 
```shell
grunt test
```
 
## Code Coverage
 
```shell
grunt coverage
```
 
[c8](https://github.com/bcoe/c8) results are available [here](http://rawgit.davedoesdev.com/davedoesdev/fastest-writable/master/coverage/lcov-report/index.html).
 
Coveralls page is [here](https://coveralls.io/r/davedoesdev/fastest-writable).
 
## Lint
 
```shell
grunt lint
```
 
# API
*/
"use strict";
 
var stream = require('stream'),
    util = require('util');
 
/**
Creates a new `FastestWritable` object which can write to multiple peer [`stream.Writable`](http://nodejs.org/docs/v0.11.13/api/stream.html#stream_class_stream_writable) objects. 
 
Inherits from [`stream.Writable`](http://nodejs.org/docs/v0.11.13/api/stream.html#stream_class_stream_writable) so you can use any `Writable` method or event in addition to those described here.
 
@constructor
@extends Writable
 
@param {Object} [options] Configuration options. This is passed onto `Writable`'s constructor and can contain the following extra property:
- `{Boolean} [end_peers_on_finish]` Whether to call [`writable.end`](http://nodejs.org/docs/v0.11.13/api/stream.html#stream_writable_end_chunk_encoding_callback) on all peers when this `FastestWritable` object emits a [`finish`](http://nodejs.org/docs/v0.11.13/api/stream.html#stream_event_finish) event. Defaults to `true`.
 
- `{Boolean} [destroy_peers_on_destroy]` Whether to call [`writable.destroy`](https://nodejs.org/dist/latest-v14.x/docs/api/stream.html#stream_writable_destroy_error) on all peers when this `FastestWritable` is destroyed. Defaults to `true`.
 
- `{Boolean} [emit_laggard]` Whether to emit an event named `laggard` on any peers which can't keep up _instead of_ ending them. Defaults to `false`.
*/
 
function FastestWritable(options)
{
    stream.Writable.call(this, options);
 
    options = options || {};
    this._options = options;
 
    this._peers = new Map();
 
    var ths = this;
 
    this.on('finish', function ()
    {
        for (var peer of ths._peers.keys())
        {
            ths._end_peer(peer, options.end_peers_on_finish, false);
        }
    });
 
    this.on('pipe', function (src)
    {
        for (var peer of ths._peers.keys())
        {
            peer.emit('pipe', src);
        }
    });
 
    this.on('unpipe', function (src)
    {
        for (var peer of ths._peers.keys())
        {
            peer.emit('unpipe', src);
        }
    });
 
    this._finish = function ()
    {
        ths.remove_peer(this, false);
    };
 
    this._orig_emit = this.emit;
    this.emit = function (type)
    {
        if (type === 'error')
        {
            for (var peer of ths._peers.keys())
            {
                peer._fastest_writable_orig_emit.apply(peer, arguments);
            }
        }
 
        return this._orig_emit.apply(this, arguments);
    };
}
 
util.inherits(FastestWritable, stream.Writable);
 
/**
Add a peer [`Writable`](http://nodejs.org/docs/v0.11.13/api/stream.html#stream_class_stream_writable) to the list of peers to which data will be written.
 
When [`writable.write`](http://nodejs.org/docs/v0.11.13/api/stream.html#stream_writable_write_chunk_encoding_callback) is called on this `FastestWritable` object, the data is written to every peer. `FastestWritable` drains when _at least one_ of its peers drains. When `writable.write` is called again, [`writable.end`](http://nodejs.org/docs/v0.11.13/api/stream.html#stream_writable_end_chunk_encoding_callback) is called on any peer which hasn't drained from the previous `writable.write` call.
 
If this `FastestWritable` object has no peer `Writable`s then it drains immediately.
 
@param {stream.Writable} peer Peer `Writable` to add.
*/
FastestWritable.prototype.add_peer = function (peer)
{
    var ths = this;
 
    this._peers.set(peer, false);
    
    peer.on('finish', this._finish);
    peer.on('close', this._finish);
 
    peer._fastest_writable_orig_emit = peer.emit;
    peer.emit = function (type)
    {
        if (type === 'error')
        {
            ths._orig_emit.apply(ths, arguments);
        }
 
        return this._fastest_writable_orig_emit.apply(this, arguments);
    };
 
    this.emit('peer_added', peer);
};
 
/**
Remove a peer [`Writable`](http://nodejs.org/docs/v0.11.13/api/stream.html#stream_class_stream_writable) from the list of peers to which data will be written.
 
@param {stream.Writable} peer Peer `Writable` to remove.
 
@param {Boolean} end Whether to call [`writable.end`](http://nodejs.org/docs/v0.11.13/api/stream.html#stream_writable_end_chunk_encoding_callback) on the peer once it's been removed from the list. Defaults to `true`.
*/
FastestWritable.prototype.remove_peer = function (peer, end)
{
    // From Node 14, 'finish' event is not emitted synchronously.
    // So when we end() the peer in _end_peer(), _remove_peer() isn't called
    // because we remove the 'finish' listener before the event is emitted.
    // istanbul ignore else
    if (this._peers.has(peer))
    {
        this._end_peer(peer, end, false);
    }
};
 
FastestWritable.prototype._end_peer = function (peer, end, laggard)
{
    this._peers.delete(peer);
 
    if (laggard)
    {
        this.emit('laggard', peer);
        peer.emit('laggard');
    }
    else if (end !== false)
    {
        peer.end();
    }
 
    peer.removeListener('finish', this._finish);
    peer.emit = peer._fastest_writable_orig_emit;
 
    this.emit('peer_removed', peer);
 
    if (this._peers.size === 0)
    {
        this.emit('empty');
    }
};
 
FastestWritable.prototype._write = function (chunk, encoding, cb)
{
    var ths = this, num_waiting = 0;
 
    function callback()
    {
        if (cb)
        {
            var f = cb;
            cb = null;
            f();
        }
    }
 
    function ready()
    {
        if (cb && !ths.emit('ready', num_waiting, num_peers, callback))
        {
            callback();
        }
    }
 
    function drain()
    {
        num_waiting -= 1;
 
        /*jshint validthis: true */
        this.removeListener('drain', drain);
        this.removeListener('finish', drain);
 
        if (!ths._peers.has(this))
        {
            if (num_waiting === 0)
            {
                ready();
            }
 
            return; // doesn't mean it's ready for data!
        }
 
        ths._peers.set(this, false);
        ready();
    }
 
    for (var info of this._peers)
    {
        var peer = info[0], waiting = info[1];
 
        if (waiting)
        {
            this._end_peer(peer, true, this._options.emit_laggard);
        }
        else if (!peer.write(chunk, encoding))
        {
            num_waiting += 1;
            this._peers.set(peer, true);
            peer.on('drain', drain);
            peer.on('finish', drain);
        }
    }
 
    var num_peers = this._peers.size;
 
    if ((num_peers === 0) || (num_waiting < num_peers))
    {
        // at least one peer is ready or there are no peers
        return process.nextTick(ready);
    }
 
    this.emit('waiting', callback);
};
 
FastestWritable.prototype._destroy = function (err, cb)
{
    if (this._options.destroy_peers_on_destroy !== false)
    {
        for (var peer of this._peers.keys())
        {
            peer.destroy(err);
        }
    }
 
    cb(err);
};
 
exports.FastestWritable = FastestWritable;