index.js 2.0 KB

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