diff --git a/lib/deflate.js b/lib/deflate.js index 168e8cb..bec7c57 100644 --- a/lib/deflate.js +++ b/lib/deflate.js @@ -7,6 +7,7 @@ var strings = require('./utils/strings'); var msg = require('./zlib/messages'); var zstream = require('./zlib/zstream'); +var toString = Object.prototype.toString; /* Public constants ==========================================================*/ /* ===========================================================================*/ @@ -162,8 +163,8 @@ var Deflate = function(options) { /** * Deflate#push(data[, mode]) -> Boolean - * - data (Uint8Array|Array|String): input data. Strings will be converted to - * utf8 byte sequence. + * - data (Uint8Array|Array|ArrayBuffer|String): input data. Strings will be + * converted to utf8 byte sequence. * - mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes. * See constants. Skipped or `false` means Z_NO_FLUSH, `true` meansh Z_FINISH. * @@ -201,6 +202,8 @@ Deflate.prototype.push = function(data, mode) { if (typeof data === 'string') { // If we need to compress text, change encoding to utf8. strm.input = strings.string2buf(data); + } else if (toString.call(data) === '[object ArrayBuffer]') { + strm.input = new Uint8Array(data); } else { strm.input = data; } diff --git a/lib/inflate.js b/lib/inflate.js index c32941b..96915bc 100644 --- a/lib/inflate.js +++ b/lib/inflate.js @@ -9,6 +9,7 @@ var msg = require('./zlib/messages'); var zstream = require('./zlib/zstream'); var gzheader = require('./zlib/gzheader'); +var toString = Object.prototype.toString; /** * class Inflate @@ -143,7 +144,7 @@ var Inflate = function(options) { /** * Inflate#push(data[, mode]) -> Boolean - * - data (Uint8Array|Array|String): input data + * - data (Uint8Array|Array|ArrayBuffer|String): input data * - mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes. * See constants. Skipped or `false` means Z_NO_FLUSH, `true` meansh Z_FINISH. * @@ -181,6 +182,8 @@ Inflate.prototype.push = function(data, mode) { if (typeof data === 'string') { // Only binary strings can be decompressed on practice strm.input = strings.binstring2buf(data); + } else if (toString.call(data) === '[object ArrayBuffer]') { + strm.input = new Uint8Array(data); } else { strm.input = data; } diff --git a/test/misc.js b/test/misc.js new file mode 100644 index 0000000..d5c2f93 --- /dev/null +++ b/test/misc.js @@ -0,0 +1,27 @@ +/*global describe, it*/ + + +'use strict'; + + +var fs = require('fs'); +var path = require('path'); +var assert = require('assert'); + +var pako = require('../index'); +var cmp = require('./helpers').cmpBuf; + +describe('ArrayBuffer', function () { + + var file = path.join(__dirname, 'fixtures/samples/lorem_utf_100k.txt'); + var sample = new Uint8Array(fs.readFileSync(file)); + var deflated = pako.deflate(sample); + + it('Deflate ArrayBuffer', function () { + assert.ok(cmp(deflated, pako.deflate(sample.buffer))); + }); + + it('Inflate ArrayBuffer', function () { + assert.ok(cmp(sample, pako.inflate(deflated.buffer))); + }); +});