Code coverage report for lib\common\streams\chunkstream.js

Statements: 80.92% (106 / 131)      Branches: 66.07% (37 / 56)      Functions: 87.5% (14 / 16)      Lines: 82.81% (106 / 128)      Ignored: none     

All files » lib/common/streams/ » 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 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                                1 1 1   1 1                   1 40 40   40 1     40   40   40 40 40 40 40 40 40 40   40 18       1         1             1 38       38         38       38 38   38     38     1 37   37     1           1 37 37           1 1 1     1   1           1 36 16     36 36 36 36 36   36   35 35 1 1   1           1                     1               1 36 36           36   36           1 35 35 35 35 35   35 35   35               1 38 38 35             1 38 38 3 35     35     38 38   38           1 35 35     35 35             1 17 17     17 17 17   17                   1 36           1 36 36   36       1
// 
// Copyright (c) Microsoft and contributors.  All rights reserved.
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//   http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// 
// See the License for the specific language governing permissions and
// limitations under the License.
// 
 
var stream = require('stream');
var crypto = require('crypto');
var util = require('util');
 
var Constants = require('../util/constants');
var bufferSize = Constants.BlobConstants.DEFAULT_WRITE_BLOCK_SIZE_IN_BYTES;
 
/**
*  Chunk stream
*  1. Calculate md5
*  2. Track reading offset
*  3. Work with customize memory allocator
*  4. Buffer data from stream.
*  @param {object} options stream.Readable options
*/
function ChunkStream(options) {
  stream.Stream.call(this);
  this.writable = this.readable = true;
 
  if (!options) {
    options = {};
  }
 
  this._highWaterMark = options.highWaterMark || bufferSize;
 
  this._paused = undefined; //True/false is the external status from users.
 
  this._isStreamOpened = false;
  this._offset = 0;
  this._allocator = options.allocator;
  this._streamEnded = false;
  this._md5hash = null;
  this._buffer = null;
  this._internalBufferSize = 0;
  this._md5sum = undefined;
 
  if (options.calcContentMd5) {
    this._md5hash = crypto.createHash('md5');
  }
}
 
util.inherits(ChunkStream, stream.Stream);
 
/**
* Set the memory allocator.
*/
ChunkStream.prototype.setMemoryAllocator = function(allocator) {
  this._allocator = allocator;
};
 
/**
* Internal stream ended
*/
ChunkStream.prototype.end = function (chunk, encoding, cb) {
  Iif (typeof chunk === 'function') {
    cb = chunk;
    chunk = null;
    encoding = null;
  } else Iif (typeof encoding === 'function') {
    cb = encoding;
    encoding = null;
  }
 
  Iif (chunk) {
    this.write(chunk, encoding);
  }
 
  this._streamEnded = true;
  this._flushInternalBuffer();
  
  Iif (cb) {
    this.once('end', cb);
  }
  this.emit('end');
};
 
ChunkStream.prototype.finish = function () {
  this.emit('finish');
 
  this.destroy();
};
 
ChunkStream.prototype.error = function () {
  this.emit('error');
 
  this.destroy();
};
 
ChunkStream.prototype.destroy = function () {
  this.writable = this.readable = false;
  this.emit('close');
};
 
/**
* Add event listener
*/
ChunkStream.prototype.write = function (chunk, encoding) {
  Eif (!this._isStreamOpened) {
    this._isStreamOpened = true;
  }
 
  this._buildChunk(chunk, encoding);
 
  return !this._paused;
};
 
/**
* Buffer the data into a chunk and emit it
*/
ChunkStream.prototype._buildChunk = function (data) {
  if(this._md5hash) {
    this._md5hash.update(data);
  }
 
  var dataSize = data.length;
  var dataOffset = 0;
  do {
    var buffer = null;
    var targetSize = this._internalBufferSize + dataSize;
 
    if (targetSize < this._highWaterMark) {
      // add the data to the internal buffer and return as it is not yet full
      this._copyToInternalBuffer(data, dataOffset, data.length);
      return;
    } else Eif (targetSize == this._highWaterMark){
      Eif(this._internalBufferSize === 0 && data.length === this._highWaterMark) {
        // set the buffer to the data passed in to avoid creating a new buffer
        buffer = data;
      } else {
        // add the data to the internal buffer and pop that buffer
        this._copyToInternalBuffer(data, dataOffset, data.length);
        buffer = this._popInternalBuffer();
      }
      dataSize = 0;
    } else {
      // add data to the internal buffer until its full, then return it
      // set the dataSize parameter so that additional data is not lost
      var copySize = this._highWaterMark - this._internalBufferSize;
      this._copyToInternalBuffer(data, dataOffset, dataOffset + copySize);
      dataSize -= copySize;
      dataOffset += copySize;
      buffer = this._popInternalBuffer();
    }
 
    this._emitBufferData(buffer);
  } while(dataSize > 0);
};
 
 
/**
* Emit the buffer
*/
ChunkStream.prototype._emitBufferData = function(buffer) {
  var newOffset = this._offset + buffer.length;
  var range = {
    start : this._offset,
    end : newOffset - 1,
    size : buffer.length
  };
 
  this._offset = newOffset;
 
  this.emit('data', buffer, range);
};
 
/**
* Copy data into internal buffer
*/
ChunkStream.prototype._copyToInternalBuffer = function(data, start, end) {
  Iif(start === undefined) start = 0;
  Iif(end === undefined) end = data.length;
  Eif (!this._buffer) {
    this._buffer = this._allocateNewBuffer();
    this._internalBufferSize = 0;
  }
  var copied = data.copy(this._buffer, this._internalBufferSize, start, end);
  this._internalBufferSize += copied;
 
  Iif(copied != (end - start)) {
    throw new Error('Can not copy entire data to buffer');
  }
};
 
/**
* Flush internal buffer
*/
ChunkStream.prototype._flushInternalBuffer = function() {
  var buffer = this._popInternalBuffer();
  if (buffer) {
    this._emitBufferData(buffer);
  }
};
 
/**
* Pop internal buffer
*/
ChunkStream.prototype._popInternalBuffer = function () {
  var buf = null;
  if (!this._buffer || this._internalBufferSize === 0) {
    buf = null;
  } else Iif(this._internalBufferSize == this._highWaterMark) {
    buf = this._buffer;
  } else {
    buf = this._buffer.slice(0, this._internalBufferSize);
  }
 
  this._buffer = null;
  this._internalBufferSize = 0;
 
  return buf;
};
 
/**
* Allocate a buffer
*/
ChunkStream.prototype._allocateNewBuffer = function() {
  var size = this._highWaterMark;
  Iif(this._allocator && this._allocator.getBuffer) {
    return this._allocator.getBuffer(size);
  } else {
    var buffer = new Buffer(size);
    return buffer;
  }
};
 
/**
* Get file content md5 when read completely.
*/
ChunkStream.prototype.getContentMd5 = function(encoding) {
  Iif (!encoding) encoding = 'base64';
  Iif(!this._md5hash) {
    throw new Error('Can\'t get content md5, please set the calcContentMd5 option for FileReadStream.');
  } else {
    Eif (this._streamEnded) {
      Eif (!this._md5sum) {
        this._md5sum = this._md5hash.digest(encoding);
      }
      return this._md5sum;
    } else {
      throw new Error('Stream has not ended.');
    }
  }
};
 
/**
* Pause chunk stream
*/
ChunkStream.prototype.pause = function() {
  this._paused = true;
};
 
/**
* Resume read stream
*/
ChunkStream.prototype.resume = function() {
  Eif (this._paused) {
    this._paused = false;
 
    this.emit('drain');
  }
};
 
module.exports = ChunkStream;