All files / node-unzipper/lib PullStream.js

100% Statements 139/139
97.67% Branches 42/43
100% Functions 9/9
100% Lines 139/139

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 14028x 28x 28x 28x 28x 221x 221x 221x 129x 129x 129x 129x 129x 51x 51x 129x 221x 28x 28x 28x 28x 984x 984x 984x 28x 28x 28x 28x 28x 28x 387x 387x 387x 387x 908x 907x 907x 907x 907x 908x 387x 387x 1373x 1373x 1220x 805x 805x 805x 805x 1218x 415x 415x 27x 27x 27x 27x 27x 27x 27x 415x 388x 388x 7x 387x 381x 381x 381x 388x 415x 1220x 1211x 1213x 1220x 1373x 1373x 992x 4x 4x 4x 4x 992x 1373x 381x 381x 381x 1373x 387x 387x 387x 387x 28x 28x 28x 1545x 1225x 1225x 1225x 1545x 986x 986x 986x 986x 239x 239x 239x 239x 239x 239x 239x 631x 631x 239x 239x 239x 239x 239x 239x 239x 2x 2x 2x 239x 239x 239x 239x 239x 239x 239x 239x 239x 239x 239x 239x 239x 28x 28x 28x 28x 28x  
var Stream = require('stream');
var Promise = require('bluebird');
var util = require('util');
var strFunction = 'function';
 
function PullStream() {
  if (!(this instanceof PullStream))
    return new PullStream();
 
  Stream.Duplex.call(this,{decodeStrings:false, objectMode:true});
  this.buffer = Buffer.from('');
  var self = this;
  self.on('finish',function() {
    self.finished = true;
    self.emit('chunk',false);
  });
}
 
util.inherits(PullStream,Stream.Duplex);
 
PullStream.prototype._write = function(chunk,e,cb) {
  this.buffer = Buffer.concat([this.buffer,chunk]);
  this.cb = cb;
  this.emit('chunk');
};
 
 
// The `eof` parameter is interpreted as `file_length` if the type is number
// otherwise (i.e. buffer) it is interpreted as a pattern signaling end of stream
PullStream.prototype.stream = function(eof,includeEof) {
  var p = Stream.PassThrough();
  var done,self= this;
 
  function cb() {
    if (typeof self.cb === strFunction) {
      var callback = self.cb;
      self.cb = undefined;
      return callback();
    }
  }
 
  function pull() {
    var packet;
    if (self.buffer && self.buffer.length) {
      if (typeof eof === 'number') {
        packet = self.buffer.slice(0,eof);
        self.buffer = self.buffer.slice(eof);
        eof -= packet.length;
        done = !eof;
      } else {
        var match = self.buffer.indexOf(eof);
        if (match !== -1) {
          // store signature match byte offset to allow us to reference
          // this for zip64 offset
          self.match = match
          if (includeEof) match = match + eof.length;
          packet = self.buffer.slice(0,match);
          self.buffer = self.buffer.slice(match);
          done = true;
        } else {
          var len = self.buffer.length - eof.length;
          if (len <= 0) {
            cb();
          } else {
            packet = self.buffer.slice(0,len);
            self.buffer = self.buffer.slice(len);
          }
        }
      }
      if (packet) p.write(packet,function() {
        if (self.buffer.length === 0 || (eof.length && self.buffer.length <= eof.length)) cb();
      });
    }
    
    if (!done) {
      if (self.finished) {
        self.removeListener('chunk',pull);
        self.emit('error', new Error('FILE_ENDED'));
        return;
      }
      
    } else {
      self.removeListener('chunk',pull);
      p.end();
    }
  }
 
  self.on('chunk',pull);
  pull();
  return p;
};
 
PullStream.prototype.pull = function(eof,includeEof) {
  if (eof === 0) return Promise.resolve('');
 
  // If we already have the required data in buffer
  // we can resolve the request immediately
  if (!isNaN(eof) && this.buffer.length > eof) {
    var data = this.buffer.slice(0,eof);
    this.buffer = this.buffer.slice(eof);
    return Promise.resolve(data);
  }
 
  // Otherwise we stream until we have it
  var buffer = Buffer.from(''),
      self = this;
 
  var concatStream = Stream.Transform();
  concatStream._transform = function(d,e,cb) {
    buffer = Buffer.concat([buffer,d]);
    cb();
  };
  
  var rejectHandler;
  var pullStreamRejectHandler;
  return new Promise(function(resolve,reject) {
    rejectHandler = reject;
    pullStreamRejectHandler = function(e) {
      self.__emittedError = e;
      reject(e);
    }
    if (self.finished)
      return reject(new Error('FILE_ENDED'));
    self.once('error',pullStreamRejectHandler);  // reject any errors from pullstream itself
    self.stream(eof,includeEof)
      .on('error',reject)
      .pipe(concatStream)
      .on('finish',function() {resolve(buffer);})
      .on('error',reject);
  })
  .finally(function() {
    self.removeListener('error',rejectHandler);
    self.removeListener('error',pullStreamRejectHandler);
  });
};
 
PullStream.prototype._read = function(){};
 
module.exports = PullStream;