# Code coverage report for lib\chunkstream.js

## Statements: 85% (68 / 80) Branches: 77.27% (34 / 44) Functions: 100% (8 / 8) Lines: 87.01% (67 / 77) Ignored: none

All files » lib/ » chunkstream.js

    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

    1

    1
    500

    500
    500

    500
    500

    500
    500

    1

    1

    9460

    9460
    9460

    9460

    1

    503

    503

    503
    503

    503

    503
    162

    503

    1

    323

    323

    323

    162

    162
    162

    1

    1

    162

    162

    1

    175

    175
    175
    175

    175

    1

    10125

    9773

    9773

    491

    491

    491

    491
    491

    491

    9282

    8806

    8806

    8806

    8786

    8786
    8786

    8786
    8296

    8806
    393

    8806

    8806

    476

    10125
    162

     // Copyright (c) 2012 Kuba Niegowski
    //
    // Permission is hereby granted, free of charge, to any person obtaining a copy
    // of this software and associated documentation files (the "Software"), to deal
    // in the Software without restriction, including without limitation the rights
    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    // copies of the Software, and to permit persons to whom the Software is
    // furnished to do so, subject to the following conditions:
    //
    // The above copyright notice and this permission notice shall be included in
    // all copies or substantial portions of the Software.
    //
    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    // THE SOFTWARE.

    'use strict';

    var util = require('util'),
        Stream = require('stream');

    var ChunkStream = module.exports = function() {
        Stream.call(this);

        this._buffers = [];
        this._buffered = 0;

        this._reads = [];
        this._paused = false;

        this._encoding = 'utf8';
        this.writable = true;
    };
    util.inherits(ChunkStream, Stream);

    ChunkStream.prototype.read = function(length, callback) {

        this._reads.push({
            length: Math.abs(length),  // if length < 0 then at most this length
            allowLess: length < 0,
            func: callback
        });

        process.nextTick(function() {
          this._process();

          // its paused and there is not enought data then ask for more
          Iif (this._paused && this._reads.length > 0) {
              this._paused = false;

              this.emit('drain');
          }
        }.bind(this));
    };

    ChunkStream.prototype.write = function(data, encoding) {

        Iif (!this.writable) {
            this.emit('error', new Error('Stream not writable'));
            return false;
        }

        Iif (!Buffer.isBuffer(data))
            data = new Buffer(data, encoding || this._encoding);

        this._buffers.push(data);
        this._buffered += data.length;

        this._process();

        // ok if there are no more read requests
        if (this._reads && this._reads.length == 0)
            this._paused = true;

        return this.writable && !this._paused;
    };

    ChunkStream.prototype.end = function(data, encoding) {

        Iif (data) this.write(data, encoding);

        this.writable = false;

        // already destroyed
        if (!this._buffers) return;

        // enqueue or handle end
        Iif (this._buffers.length == 0) {
            this._end();
        } else {
            this._buffers.push(null);
            this._process();
        }
    };

    ChunkStream.prototype.destroySoon = ChunkStream.prototype.end;

    ChunkStream.prototype._end = function() {

        Iif (this._reads.length > 0) {
            this.emit('error',
                new Error('There are some read requests waitng on finished stream')
            );
        }

        this.destroy();
    };

    ChunkStream.prototype.destroy = function() {

        Iif (!this._buffers) return;

        this.writable = false;
        this._reads = null;
        this._buffers = null;

        this.emit('close');
    };

    ChunkStream.prototype._process = function() {

        // as long as there is any data and read requests
        while (this._buffered > 0 && this._reads && this._reads.length > 0) {

            var read = this._reads[0];

            // read any data (but no more than length)
            if (read.allowLess) {

                // ok there is any data so that we can satisfy this request
                this._reads.shift(); // == read

                // first we need to peek into first buffer
                var buf = this._buffers[0];

                // ok there is more data than we need
                Eif (buf.length > read.length) {

                    this._buffered -= read.length;
                    this._buffers[0] = buf.slice(read.length);

                    read.func.call(this, buf.slice(0, read.length));

                } else {
                    // ok this is less than maximum length so use it all
                    this._buffered -= buf.length;
                    this._buffers.shift(); // == buf

                    read.func.call(this, buf);
                }

            } else if (this._buffered >= read.length) {
                // ok we can meet some expectations

                this._reads.shift(); // == read

                var pos = 0,
                    count = 0,
                    data = new Buffer(read.length);

                // create buffer for all data
                while (pos < read.length) {

                    var buf = this._buffers[count++],
                        len = Math.min(buf.length, read.length - pos);

                    buf.copy(data, pos, 0, len);
                    pos += len;

                    // last buffer wasn't used all so just slice it and leave
                    if (len != buf.length)
                        this._buffers[--count] = buf.slice(len);
                }

                // remove all used buffers
                if (count > 0)
                    this._buffers.splice(0, count);

                this._buffered -= read.length;

                read.func.call(this, data);

            } else {
                // not enought data to satisfy first request in queue
                // so we need to wait for more
                break;
            }
        }

        if (this._buffers && this._buffers.length > 0 && this._buffers[0] == null) {
            this._end();
        }
    };