From 23563dfae92cec9223804311a16636477ea6d7ab Mon Sep 17 00:00:00 2001 From: Vitaly Puzrin Date: Sat, 7 Nov 2020 17:42:50 +0300 Subject: [PATCH] Removed binary strings support --- CHANGELOG.md | 1 + lib/deflate.js | 31 +++++++++---------------------- lib/inflate.js | 28 +++++++++++----------------- lib/utils/strings.js | 18 +----------------- test/strings.js | 13 ------------- 5 files changed, 22 insertions(+), 69 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae5c80d..5e2a5f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [2.0.0] - WIP ### Changed +- Removed binary strings support. - Removed support of `Inflate` & `Deflate` instance create without `new`. - Upgrade build tools to modern ones. diff --git a/lib/deflate.js b/lib/deflate.js index 472eaa4..96a1762 100644 --- a/lib/deflate.js +++ b/lib/deflate.js @@ -89,8 +89,6 @@ var Z_DEFLATED = 8; * - `chunkSize` - size of generated data chunks (16K by default) * - `raw` (Boolean) - do raw deflate * - `gzip` (Boolean) - create gzip wrapper - * - `to` (String) - if equal to 'string', then result will be "binary string" - * (each char code [0..255]) * - `header` (Object) - custom header for gzip * - `text` (Boolean) - true if compressed data believed to be text * - `time` (Number) - modification time, unix timestamp @@ -104,8 +102,8 @@ var Z_DEFLATED = 8; * * ```javascript * var pako = require('pako') - * , chunk1 = Uint8Array([1,2,3,4,5,6,7,8,9]) - * , chunk2 = Uint8Array([10,11,12,13,14,15,16,17,18,19]); + * , chunk1 = new Uint8Array([1,2,3,4,5,6,7,8,9]) + * , chunk2 = new Uint8Array([10,11,12,13,14,15,16,17,18,19]); * * var deflate = new pako.Deflate({ level: 3}); * @@ -124,8 +122,7 @@ function Deflate(options) { chunkSize: 16384, windowBits: 15, memLevel: 8, - strategy: Z_DEFAULT_STRATEGY, - to: '' + strategy: Z_DEFAULT_STRATEGY }, options || {}); var opt = this.options; @@ -250,11 +247,7 @@ Deflate.prototype.push = function (data, mode) { return false; } if (strm.avail_out === 0 || (strm.avail_in === 0 && (_mode === Z_FINISH || _mode === Z_SYNC_FLUSH))) { - if (this.options.to === 'string') { - this.onData(strings.buf2binstring(utils.shrinkBuf(strm.output, strm.next_out))); - } else { - this.onData(utils.shrinkBuf(strm.output, strm.next_out)); - } + this.onData(utils.shrinkBuf(strm.output, strm.next_out)); } } while ((strm.avail_in > 0 || strm.avail_out === 0) && status !== Z_STREAM_END); @@ -304,11 +297,7 @@ Deflate.prototype.onData = function (chunk) { Deflate.prototype.onEnd = function (status) { // On success - join if (status === Z_OK) { - if (this.options.to === 'string') { - this.result = this.chunks.join(''); - } else { - this.result = utils.flattenChunks(this.chunks); - } + this.result = utils.flattenChunks(this.chunks); } this.chunks = []; this.err = status; @@ -317,7 +306,7 @@ Deflate.prototype.onEnd = function (status) { /** - * deflate(data[, options]) -> Uint8Array|Array|String + * deflate(data[, options]) -> Uint8Array|Array * - data (Uint8Array|Array|String): input data to compress. * - options (Object): zlib deflate options. * @@ -338,14 +327,12 @@ Deflate.prototype.onEnd = function (status) { * * - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify * negative windowBits implicitly. - * - `to` (String) - if equal to 'string', then result will be "binary string" - * (each char code [0..255]) * * ##### Example: * * ```javascript * var pako = require('pako') - * , data = Uint8Array([1,2,3,4,5,6,7,8,9]); + * , data = new Uint8Array([1,2,3,4,5,6,7,8,9]); * * console.log(pako.deflate(data)); * ``` @@ -363,7 +350,7 @@ function deflate(input, options) { /** - * deflateRaw(data[, options]) -> Uint8Array|Array|String + * deflateRaw(data[, options]) -> Uint8Array|Array * - data (Uint8Array|Array|String): input data to compress. * - options (Object): zlib deflate options. * @@ -378,7 +365,7 @@ function deflateRaw(input, options) { /** - * gzip(data[, options]) -> Uint8Array|Array|String + * gzip(data[, options]) -> Uint8Array|Array * - data (Uint8Array|Array|String): input data to compress. * - options (Object): zlib deflate options. * diff --git a/lib/inflate.js b/lib/inflate.js index 3549a2f..ab50ba0 100644 --- a/lib/inflate.js +++ b/lib/inflate.js @@ -77,8 +77,8 @@ var toString = Object.prototype.toString; * * ```javascript * var pako = require('pako') - * , chunk1 = Uint8Array([1,2,3,4,5,6,7,8,9]) - * , chunk2 = Uint8Array([10,11,12,13,14,15,16,17,18,19]); + * , chunk1 = new Uint8Array([1,2,3,4,5,6,7,8,9]) + * , chunk2 = new Uint8Array([10,11,12,13,14,15,16,17,18,19]); * * var inflate = new pako.Inflate({ level: 3}); * @@ -162,7 +162,7 @@ function Inflate(options) { /** * Inflate#push(data[, mode]) -> Boolean - * - data (Uint8Array|Array|ArrayBuffer|String): input data + * - data (Uint8Array|Array|ArrayBuffer): 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` means Z_FINISH. * @@ -174,9 +174,8 @@ function Inflate(options) { * * On fail call [[Inflate#onEnd]] with error code and return false. * - * We strongly recommend to use `Uint8Array` on input for best speed (output - * format is detected automatically). Also, don't skip last param and always - * use the same type in your code (boolean or number). That will improve JS speed. + * Note. Don't skip last param and always use the same type in your code + * (boolean or number). That will improve JS speed. * * For regular `Array`-s make sure all elements are [0..255]. * @@ -203,10 +202,7 @@ Inflate.prototype.push = function (data, mode) { _mode = (mode === ~~mode) ? mode : ((mode === true) ? c.Z_FINISH : c.Z_NO_FLUSH); // Convert data if needed - if (typeof data === 'string') { - // Only binary strings can be decompressed on practice - strm.input = strings.binstring2buf(data); - } else if (toString.call(data) === '[object ArrayBuffer]') { + if (toString.call(data) === '[object ArrayBuffer]') { strm.input = new Uint8Array(data); } else { strm.input = data; @@ -300,7 +296,7 @@ Inflate.prototype.push = function (data, mode) { /** * Inflate#onData(chunk) -> Void - * - chunk (Uint8Array|Array|String): output data. Type of array depends + * - chunk (Uint8Array|Array): output data. Type of array depends * on js engine support. When string output requested, each chunk * will be string. * @@ -326,8 +322,6 @@ Inflate.prototype.onEnd = function (status) { // On success - join if (status === c.Z_OK) { if (this.options.to === 'string') { - // Glue & convert here, until we teach pako to send - // utf8 aligned strings to onData this.result = this.chunks.join(''); } else { this.result = utils.flattenChunks(this.chunks); @@ -341,7 +335,7 @@ Inflate.prototype.onEnd = function (status) { /** * inflate(data[, options]) -> Uint8Array|Array|String - * - data (Uint8Array|Array|String): input data to decompress. + * - data (Uint8Array|Array): input data to decompress. * - options (Object): zlib inflate options. * * Decompress `data` with inflate/ungzip and `options`. Autodetect @@ -368,7 +362,7 @@ Inflate.prototype.onEnd = function (status) { * * ```javascript * var pako = require('pako') - * , input = pako.deflate([1,2,3,4,5,6,7,8,9]) + * , input = pako.deflate(new Uint8Array([1,2,3,4,5,6,7,8,9])) * , output; * * try { @@ -392,7 +386,7 @@ function inflate(input, options) { /** * inflateRaw(data[, options]) -> Uint8Array|Array|String - * - data (Uint8Array|Array|String): input data to decompress. + * - data (Uint8Array|Array): input data to decompress. * - options (Object): zlib inflate options. * * The same as [[inflate]], but creates raw data, without wrapper @@ -407,7 +401,7 @@ function inflateRaw(input, options) { /** * ungzip(data[, options]) -> Uint8Array|Array|String - * - data (Uint8Array|Array|String): input data to decompress. + * - data (Uint8Array|Array): input data to decompress. * - options (Object): zlib inflate options. * * Just shortcut to [[inflate]], because it autodetects format diff --git a/lib/utils/strings.js b/lib/utils/strings.js index b07a732..cf2606b 100644 --- a/lib/utils/strings.js +++ b/lib/utils/strings.js @@ -81,7 +81,7 @@ exports.string2buf = function (str) { return buf; }; -// Helper (used in 2 places) +// Helper function buf2binstring(buf, len) { // On Chrome, the arguments in a function call that are allowed is `65534`. // If the length of the buffer is smaller than that, we can use this optimization, @@ -100,22 +100,6 @@ function buf2binstring(buf, len) { } -// Convert byte array to binary string -exports.buf2binstring = function (buf) { - return buf2binstring(buf, buf.length); -}; - - -// Convert binary string (typed, when possible) -exports.binstring2buf = function (str) { - var buf = new utils.Buf8(str.length); - for (var i = 0, len = buf.length; i < len; i++) { - buf[i] = str.charCodeAt(i); - } - return buf; -}; - - // convert array to string exports.buf2string = function (buf, max) { var i, out, c, c_len; diff --git a/test/strings.js b/test/strings.js index 0ddd2ae..4365ea2 100644 --- a/test/strings.js +++ b/test/strings.js @@ -102,19 +102,6 @@ describe('Deflate/Inflate strings', function () { )); }); - it('Deflate with binary string output', function () { - var data = pako.deflate(sampleArray, { to: 'string', chunkSize: 99 }); - - assert.equal(typeof data, 'string'); - assert.ok(cmp(b(data, 'binary'), pako.deflate(sampleArray))); - }); - - it('Inflate binary string input', function () { - var deflatedString = pako.deflate(sampleArray, { to: 'string' }); - var deflatedArray = pako.deflate(sampleArray); - assert.ok(cmp(pako.inflate(deflatedString), pako.inflate(deflatedArray))); - }); - it('Inflate with javascript string (utf16) output', function () { var deflatedArray = pako.deflate(sampleArray); var data = pako.inflate(deflatedArray, { to: 'string', chunkSize: 99 });