react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / browserify-zlib / test / test-zlib.js
80728 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 assert = require('assert');22var zlib = require('../');23var path = require('path');2425var zlibPairs =26[[zlib.Deflate, zlib.Inflate],27[zlib.Gzip, zlib.Gunzip],28[zlib.Deflate, zlib.Unzip],29[zlib.Gzip, zlib.Unzip],30[zlib.DeflateRaw, zlib.InflateRaw]];3132// how fast to trickle through the slowstream33var trickle = [128, 1024, 1024 * 1024];3435// tunable options for zlib classes.3637// several different chunk sizes38var chunkSize = [128, 1024, 1024 * 16, 1024 * 1024];3940// this is every possible value.41var level = [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];42var windowBits = [8, 9, 10, 11, 12, 13, 14, 15];43var memLevel = [1, 2, 3, 4, 5, 6, 7, 8, 9];44var strategy = [0, 1, 2, 3, 4];4546// it's nice in theory to test every combination, but it47// takes WAY too long. Maybe a pummel test could do this?48if (!process.env.PUMMEL) {49trickle = [1024];50chunkSize = [1024 * 16];51level = [6];52memLevel = [8];53windowBits = [15];54strategy = [0];55}5657var fs = require('fs');5859if (process.env.FAST) {60zlibPairs = [[zlib.Gzip, zlib.Unzip]];61}6263var tests = {64'person.jpg': fs.readFileSync(__dirname + '/fixtures/person.jpg'),65'elipses.txt': fs.readFileSync(__dirname + '/fixtures/elipses.txt'),66'empty.txt': fs.readFileSync(__dirname + '/fixtures/empty.txt')67};6869var util = require('util');70var stream = require('stream');717273// stream that saves everything74function BufferStream() {75this.chunks = [];76this.length = 0;77this.writable = true;78this.readable = true;79}8081util.inherits(BufferStream, stream.Stream);8283BufferStream.prototype.write = function(c) {84this.chunks.push(c);85this.length += c.length;86return true;87};8889BufferStream.prototype.end = function(c) {90if (c) this.write(c);91// flatten92var buf = new Buffer(this.length);93var i = 0;94this.chunks.forEach(function(c) {95c.copy(buf, i);96i += c.length;97});98this.emit('data', buf);99this.emit('end');100return true;101};102103104function SlowStream(trickle) {105this.trickle = trickle;106this.offset = 0;107this.readable = this.writable = true;108}109110util.inherits(SlowStream, stream.Stream);111112SlowStream.prototype.write = function() {113throw new Error('not implemented, just call ss.end(chunk)');114};115116SlowStream.prototype.pause = function() {117this.paused = true;118this.emit('pause');119};120121SlowStream.prototype.resume = function() {122var self = this;123if (self.ended) return;124self.emit('resume');125if (!self.chunk) return;126self.paused = false;127emit();128function emit() {129if (self.paused) return;130if (self.offset >= self.length) {131self.ended = true;132return self.emit('end');133}134var end = Math.min(self.offset + self.trickle, self.length);135var c = self.chunk.slice(self.offset, end);136self.offset += c.length;137self.emit('data', c);138process.nextTick(emit);139}140};141142SlowStream.prototype.end = function(chunk) {143// walk over the chunk in blocks.144var self = this;145self.chunk = chunk;146self.length = chunk.length;147self.resume();148return self.ended;149};150151152153// for each of the files, make sure that compressing and154// decompressing results in the same data, for every combination155// of the options set above.156var tape = require('tape');157158Object.keys(tests).forEach(function(file) {159var test = tests[file];160chunkSize.forEach(function(chunkSize) {161trickle.forEach(function(trickle) {162windowBits.forEach(function(windowBits) {163level.forEach(function(level) {164memLevel.forEach(function(memLevel) {165strategy.forEach(function(strategy) {166zlibPairs.forEach(function(pair) {167var Def = pair[0];168var Inf = pair[1];169var opts = {170level: level,171windowBits: windowBits,172memLevel: memLevel,173strategy: strategy174};175176var msg = file + ' ' +177chunkSize + ' ' +178JSON.stringify(opts) + ' ' +179Def.name + ' -> ' + Inf.name;180181tape('zlib ' + msg, function(t) {182t.plan(1);183184var def = new Def(opts);185var inf = new Inf(opts);186var ss = new SlowStream(trickle);187var buf = new BufferStream();188189// verify that the same exact buffer comes out the other end.190buf.on('data', function(c) {191t.deepEqual(c, test);192});193194// the magic happens here.195ss.pipe(def).pipe(inf).pipe(buf);196ss.end(test);197});198});199});200});201});202});203});204});205});206207208