index.js
1 var inflate = require('../'); 2 var zlib = require('zlib'); 3 var fs = require('fs'); 4 var assert = require('assert'); 5 var uncompressed = fs.readFileSync(__dirname + '/lorem.txt'); 6 7 describe('tiny-inflate', function() { 8 var compressed, noCompression, fixed; 9 10 function deflate(buf, options, fn) { 11 var chunks = []; 12 zlib.createDeflateRaw(options) 13 .on('data', function(chunk) { 14 chunks.push(chunk); 15 }) 16 .on('error', fn) 17 .on('end', function() { 18 fn(null, Buffer.concat(chunks)); 19 }) 20 .end(buf); 21 } 22 23 before(function(done) { 24 zlib.deflateRaw(uncompressed, function(err, data) { 25 compressed = data; 26 done(); 27 }); 28 }); 29 30 before(function(done) { 31 deflate(uncompressed, { level: zlib.Z_NO_COMPRESSION }, function(err, data) { 32 noCompression = data; 33 done(); 34 }); 35 }); 36 37 before(function(done) { 38 deflate(uncompressed, { strategy: zlib.Z_FIXED }, function(err, data) { 39 fixed = data; 40 done(); 41 }); 42 }); 43 44 it('should inflate some data', function() { 45 var out = Buffer.alloc(uncompressed.length); 46 inflate(compressed, out); 47 assert.deepEqual(out, uncompressed); 48 }); 49 50 it('should slice output buffer', function() { 51 var out = Buffer.alloc(uncompressed.length + 1024); 52 var res = inflate(compressed, out); 53 assert.deepEqual(res, uncompressed); 54 assert.equal(res.length, uncompressed.length); 55 }); 56 57 it('should handle uncompressed blocks', function() { 58 var out = Buffer.alloc(uncompressed.length); 59 inflate(noCompression, out); 60 assert.deepEqual(out, uncompressed); 61 }); 62 63 it('should handle fixed huffman blocks', function() { 64 var out = Buffer.alloc(uncompressed.length); 65 inflate(fixed, out); 66 assert.deepEqual(out, uncompressed); 67 }); 68 69 it('should handle typed arrays', function() { 70 var input = new Uint8Array(compressed); 71 var out = new Uint8Array(uncompressed.length); 72 inflate(input, out); 73 assert.deepEqual(out, new Uint8Array(uncompressed)); 74 }); 75 });