Allow ArrayBuffer input, closes #49

master
Vitaly Puzrin 2015-03-24 03:46:30 +03:00
rodzic cab7e77d06
commit c6f03f4522
3 zmienionych plików z 36 dodań i 3 usunięć

Wyświetl plik

@ -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;
}

Wyświetl plik

@ -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;
}

27
test/misc.js 100644
Wyświetl plik

@ -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)));
});
});