react / wstein / node_modules / browserify / node_modules / browserify-zlib / test / test-zlib-random-byte-pipes.js
80529 views// Copyright Joyent, Inc. and other Node contributors.1//2// Permission is hereby granted, free of charge, to any person obtaining a3// copy of this software and associated documentation files (the4// "Software"), to deal in the Software without restriction, including5// without limitation the rights to use, copy, modify, merge, publish,6// distribute, sublicense, and/or sell copies of the Software, and to permit7// persons to whom the Software is furnished to do so, subject to the8// following conditions:9//10// The above copyright notice and this permission notice shall be included11// in all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF15// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN16// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,17// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR18// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE19// USE OR OTHER DEALINGS IN THE SOFTWARE.2021var crypto = require('crypto');22var stream = require('stream');23var Stream = stream.Stream;24var util = require('util');25var tape = require('tape');26var zlib = require('../');27282930// emit random bytes, and keep a shasum31function RandomReadStream(opt) {32Stream.call(this);3334this.readable = true;35this._paused = false;36this._processing = false;3738this._hasher = crypto.createHash('sha1');39opt = opt || {};4041// base block size.42opt.block = opt.block || 256 * 1024;4344// total number of bytes to emit45opt.total = opt.total || 256 * 1024 * 1024;46this._remaining = opt.total;4748// how variable to make the block sizes49opt.jitter = opt.jitter || 1024;5051this._opt = opt;5253this._process = this._process.bind(this);5455process.nextTick(this._process);56}5758util.inherits(RandomReadStream, Stream);5960RandomReadStream.prototype.pause = function() {61this._paused = true;62this.emit('pause');63};6465RandomReadStream.prototype.resume = function() {66this._paused = false;67this.emit('resume');68this._process();69};7071RandomReadStream.prototype._process = function() {72if (this._processing) return;73if (this._paused) return;7475this._processing = true;7677if (!this._remaining) {78this._hash = this._hasher.digest('hex').toLowerCase().trim();79this._processing = false;8081this.emit('end');82return;83}8485// figure out how many bytes to output86// if finished, then just emit end.87var block = this._opt.block;88var jitter = this._opt.jitter;89if (jitter) {90block += Math.ceil(Math.random() * jitter - (jitter / 2));91}92block = Math.min(block, this._remaining);93var buf = new Buffer(block);94for (var i = 0; i < block; i++) {95buf[i] = Math.random() * 256;96}9798this._hasher.update(buf);99100this._remaining -= block;101102this._processing = false;103104this.emit('data', buf);105process.nextTick(this._process);106};107108109// a filter that just verifies a shasum110function HashStream() {111Stream.call(this);112113this.readable = this.writable = true;114this._hasher = crypto.createHash('sha1');115}116117util.inherits(HashStream, Stream);118119HashStream.prototype.write = function(c) {120// Simulate the way that an fs.ReadStream returns false121// on *every* write like a jerk, only to resume a122// moment later.123this._hasher.update(c);124process.nextTick(this.resume.bind(this));125return false;126};127128HashStream.prototype.resume = function() {129this.emit('resume');130process.nextTick(this.emit.bind(this, 'drain'));131};132133HashStream.prototype.end = function(c) {134if (c) {135this.write(c);136}137this._hash = this._hasher.digest('hex').toLowerCase().trim();138this.emit('data', this._hash);139this.emit('end');140};141142tape('random byte pipes', function(t) {143var inp = new RandomReadStream({ total: 1024, block: 256, jitter: 16 });144var out = new HashStream();145var gzip = zlib.createGzip();146var gunz = zlib.createGunzip();147148inp.pipe(gzip).pipe(gunz).pipe(out);149150inp.on('data', function(c) {151t.ok(c.length);152});153154gzip.on('data', function(c) {155t.ok(c.length);156});157158gunz.on('data', function(c) {159t.ok(c.length);160});161162out.on('data', function(c) {163t.ok(c.length);164});165166out.on('data', function(c) {167t.ok(c.length);168t.equal(c, inp._hash, 'hashes should match');169});170171out.on('end', function() {172t.end();173})174});175176177178