From f5c3c29848d4b4c4610740d98dd1d3de1cfb1398 Mon Sep 17 00:00:00 2001 From: Vitaly Puzrin Date: Sat, 7 Nov 2020 19:52:58 +0300 Subject: [PATCH] Removed `Array` support --- CHANGELOG.md | 3 +- .../deflate-pako-string/index.js | 10 --- .../deflate-pako-untyped/index.js | 12 --- .../inflate-pako-string/index.js | 9 -- .../inflate-pako-untyped/index.js | 11 --- lib/deflate.js | 31 +++---- lib/inflate.js | 29 +++--- lib/utils/common.js | 90 ++++--------------- lib/utils/strings.js | 13 +-- lib/zlib/deflate.js | 31 ++++--- lib/zlib/inflate.js | 43 +++++---- lib/zlib/inftrees.js | 6 +- lib/zlib/trees.js | 4 +- test/chunks.js | 7 +- test/gzip_specials.js | 5 +- test/helpers.js | 18 ---- test/inflate_cover_ported.js | 2 +- 17 files changed, 94 insertions(+), 230 deletions(-) delete mode 100644 benchmark/implementations/deflate-pako-string/index.js delete mode 100644 benchmark/implementations/deflate-pako-untyped/index.js delete mode 100644 benchmark/implementations/inflate-pako-string/index.js delete mode 100644 benchmark/implementations/inflate-pako-untyped/index.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e2a5f5..e075715 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [2.0.0] - WIP ### Changed -- Removed binary strings support. +- Removed binary strings and `Array` support. +- Removed fallbacks for unsupported TypedArray methods (`.set()`, `.subarray()`). - Removed support of `Inflate` & `Deflate` instance create without `new`. - Upgrade build tools to modern ones. diff --git a/benchmark/implementations/deflate-pako-string/index.js b/benchmark/implementations/deflate-pako-string/index.js deleted file mode 100644 index ef86626..0000000 --- a/benchmark/implementations/deflate-pako-string/index.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict'; - -var pako = require('../../../'); - -exports.run = function (data, level) { - pako.deflate(data.string, { - level: level, - to: 'string' - }); -}; diff --git a/benchmark/implementations/deflate-pako-untyped/index.js b/benchmark/implementations/deflate-pako-untyped/index.js deleted file mode 100644 index 27a2722..0000000 --- a/benchmark/implementations/deflate-pako-untyped/index.js +++ /dev/null @@ -1,12 +0,0 @@ -'use strict'; - -var pako = require('../../../'); -var utils = require('../../../lib/utils/common'); - -exports.run = function (data, level) { - utils.setTyped(false); - pako.deflate(data.typed, { - level: level - }); - utils.setTyped(true); -}; diff --git a/benchmark/implementations/inflate-pako-string/index.js b/benchmark/implementations/inflate-pako-string/index.js deleted file mode 100644 index f067d7d..0000000 --- a/benchmark/implementations/inflate-pako-string/index.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict'; - -var pako = require('../../../'); - -exports.run = function (data) { - return pako.inflate(data.deflateString, { - to: 'string' - }); -}; diff --git a/benchmark/implementations/inflate-pako-untyped/index.js b/benchmark/implementations/inflate-pako-untyped/index.js deleted file mode 100644 index 0917c97..0000000 --- a/benchmark/implementations/inflate-pako-untyped/index.js +++ /dev/null @@ -1,11 +0,0 @@ -'use strict'; - -var pako = require('../../../'); -var utils = require('../../../lib/utils/common'); - -exports.run = function (data/*, level*/) { - utils.setTyped(false); - pako.inflate(data.deflateTyped, { - }); - utils.setTyped(true); -}; diff --git a/lib/deflate.js b/lib/deflate.js index 96a1762..569481d 100644 --- a/lib/deflate.js +++ b/lib/deflate.js @@ -43,7 +43,7 @@ var Z_DEFLATED = 8; **/ /** - * Deflate.result -> Uint8Array|Array + * Deflate.result -> Uint8Array * * Compressed result, generated by default [[Deflate#onData]] * and [[Deflate#onEnd]] handlers. Filled after you push last chunk @@ -184,7 +184,7 @@ function Deflate(options) { /** * Deflate#push(data[, mode]) -> Boolean - * - data (Uint8Array|Array|ArrayBuffer|String): input data. Strings will be + * - data (Uint8Array|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` means Z_FINISH. @@ -197,11 +197,8 @@ function Deflate(options) { * * On fail call [[Deflate#onEnd]] with error code and return false. * - * We strongly recommend to use `Uint8Array` on input for best speed (output - * array 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. - * - * For regular `Array`-s make sure all elements are [0..255]. + * Note. Don't skip last param and always use the same type in your code + * (boolean or number). That will improve JS speed. * * ##### Example * @@ -235,7 +232,7 @@ Deflate.prototype.push = function (data, mode) { do { if (strm.avail_out === 0) { - strm.output = new utils.Buf8(chunkSize); + strm.output = new Uint8Array(chunkSize); strm.next_out = 0; strm.avail_out = chunkSize; } @@ -247,7 +244,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))) { - this.onData(utils.shrinkBuf(strm.output, strm.next_out)); + this.onData(strm.output.length === strm.next_out ? strm.output : strm.output.subarray(0, strm.next_out)); } } while ((strm.avail_in > 0 || strm.avail_out === 0) && status !== Z_STREAM_END); @@ -272,9 +269,7 @@ Deflate.prototype.push = function (data, mode) { /** * Deflate#onData(chunk) -> Void - * - chunk (Uint8Array|Array|String): output data. Type of array depends - * on js engine support. When string output requested, each chunk - * will be string. + * - chunk (Uint8Array): output data. * * By default, stores data blocks in `chunks[]` property and glue * those in `onEnd`. Override this handler, if you need another behaviour. @@ -306,8 +301,8 @@ Deflate.prototype.onEnd = function (status) { /** - * deflate(data[, options]) -> Uint8Array|Array - * - data (Uint8Array|Array|String): input data to compress. + * deflate(data[, options]) -> Uint8Array + * - data (Uint8Array|String): input data to compress. * - options (Object): zlib deflate options. * * Compress `data` with deflate algorithm and `options`. @@ -350,8 +345,8 @@ function deflate(input, options) { /** - * deflateRaw(data[, options]) -> Uint8Array|Array - * - data (Uint8Array|Array|String): input data to compress. + * deflateRaw(data[, options]) -> Uint8Array + * - data (Uint8Array|String): input data to compress. * - options (Object): zlib deflate options. * * The same as [[deflate]], but creates raw data, without wrapper @@ -365,8 +360,8 @@ function deflateRaw(input, options) { /** - * gzip(data[, options]) -> Uint8Array|Array - * - data (Uint8Array|Array|String): input data to compress. + * gzip(data[, options]) -> Uint8Array + * - data (Uint8Array|String): input data to compress. * - options (Object): zlib deflate options. * * The same as [[deflate]], but create gzip wrapper instead of diff --git a/lib/inflate.js b/lib/inflate.js index ab50ba0..18af9eb 100644 --- a/lib/inflate.js +++ b/lib/inflate.js @@ -26,7 +26,7 @@ var toString = Object.prototype.toString; **/ /** - * Inflate.result -> Uint8Array|Array|String + * Inflate.result -> Uint8Array|String * * Uncompressed result, generated by default [[Inflate#onData]] * and [[Inflate#onEnd]] handlers. Filled after you push last chunk @@ -162,7 +162,7 @@ function Inflate(options) { /** * Inflate#push(data[, mode]) -> Boolean - * - data (Uint8Array|Array|ArrayBuffer): input data + * - data (Uint8Array|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. * @@ -177,8 +177,6 @@ function Inflate(options) { * 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]. - * * ##### Example * * ```javascript @@ -213,7 +211,7 @@ Inflate.prototype.push = function (data, mode) { do { if (strm.avail_out === 0) { - strm.output = new utils.Buf8(chunkSize); + strm.output = new Uint8Array(chunkSize); strm.next_out = 0; strm.avail_out = chunkSize; } @@ -248,12 +246,12 @@ Inflate.prototype.push = function (data, mode) { // move tail strm.next_out = tail; strm.avail_out = chunkSize - tail; - if (tail) { utils.arraySet(strm.output, strm.output, next_out_utf8, tail, 0); } + if (tail) { strm.output.set(strm.output.subarray(next_out_utf8, next_out_utf8 + tail), 0); } this.onData(utf8str); } else { - this.onData(utils.shrinkBuf(strm.output, strm.next_out)); + this.onData(strm.output.length === strm.next_out ? strm.output : strm.output.subarray(0, strm.next_out)); } } } @@ -296,9 +294,8 @@ Inflate.prototype.push = function (data, mode) { /** * Inflate#onData(chunk) -> Void - * - chunk (Uint8Array|Array): output data. Type of array depends - * on js engine support. When string output requested, each chunk - * will be string. + * - chunk (Uint8Array|String): output data. When string output requested, + * each chunk will be string. * * By default, stores data blocks in `chunks[]` property and glue * those in `onEnd`. Override this handler, if you need another behaviour. @@ -334,8 +331,8 @@ Inflate.prototype.onEnd = function (status) { /** - * inflate(data[, options]) -> Uint8Array|Array|String - * - data (Uint8Array|Array): input data to decompress. + * inflate(data[, options]) -> Uint8Array|String + * - data (Uint8Array): input data to decompress. * - options (Object): zlib inflate options. * * Decompress `data` with inflate/ungzip and `options`. Autodetect @@ -385,8 +382,8 @@ function inflate(input, options) { /** - * inflateRaw(data[, options]) -> Uint8Array|Array|String - * - data (Uint8Array|Array): input data to decompress. + * inflateRaw(data[, options]) -> Uint8Array|String + * - data (Uint8Array): input data to decompress. * - options (Object): zlib inflate options. * * The same as [[inflate]], but creates raw data, without wrapper @@ -400,8 +397,8 @@ function inflateRaw(input, options) { /** - * ungzip(data[, options]) -> Uint8Array|Array|String - * - data (Uint8Array|Array): input data to decompress. + * ungzip(data[, options]) -> Uint8Array|String + * - data (Uint8Array): input data to decompress. * - options (Object): zlib inflate options. * * Just shortcut to [[inflate]], because it autodetects format diff --git a/lib/utils/common.js b/lib/utils/common.js index 58be4be..4d885af 100644 --- a/lib/utils/common.js +++ b/lib/utils/common.js @@ -1,15 +1,11 @@ 'use strict'; -var TYPED_OK = (typeof Uint8Array !== 'undefined') && - (typeof Uint16Array !== 'undefined') && - (typeof Int32Array !== 'undefined'); - function _has(obj, key) { return Object.prototype.hasOwnProperty.call(obj, key); } -exports.assign = function (obj /*from1, from2, from3, ...*/) { +module.exports.assign = function (obj /*from1, from2, from3, ...*/) { var sources = Array.prototype.slice.call(arguments, 1); while (sources.length) { var source = sources.shift(); @@ -30,76 +26,24 @@ exports.assign = function (obj /*from1, from2, from3, ...*/) { }; -// reduce buffer size, avoiding mem copy -exports.shrinkBuf = function (buf, size) { - if (buf.length === size) { return buf; } - if (buf.subarray) { return buf.subarray(0, size); } - buf.length = size; - return buf; -}; +// Join array of chunks to single array. +module.exports.flattenChunks = function (chunks) { + var i, l, len, pos, chunk, result; - -var fnTyped = { - arraySet: function (dest, src, src_offs, len, dest_offs) { - if (src.subarray && dest.subarray) { - dest.set(src.subarray(src_offs, src_offs + len), dest_offs); - return; - } - // Fallback to ordinary array - for (var i = 0; i < len; i++) { - dest[dest_offs + i] = src[src_offs + i]; - } - }, - // Join array of chunks to single array. - flattenChunks: function (chunks) { - var i, l, len, pos, chunk, result; - - // calculate data length - len = 0; - for (i = 0, l = chunks.length; i < l; i++) { - len += chunks[i].length; - } - - // join chunks - result = new Uint8Array(len); - pos = 0; - for (i = 0, l = chunks.length; i < l; i++) { - chunk = chunks[i]; - result.set(chunk, pos); - pos += chunk.length; - } - - return result; + // calculate data length + len = 0; + for (i = 0, l = chunks.length; i < l; i++) { + len += chunks[i].length; } -}; -var fnUntyped = { - arraySet: function (dest, src, src_offs, len, dest_offs) { - for (var i = 0; i < len; i++) { - dest[dest_offs + i] = src[src_offs + i]; - } - }, - // Join array of chunks to single array. - flattenChunks: function (chunks) { - return [].concat.apply([], chunks); + // join chunks + result = new Uint8Array(len); + pos = 0; + for (i = 0, l = chunks.length; i < l; i++) { + chunk = chunks[i]; + result.set(chunk, pos); + pos += chunk.length; } + + return result; }; - - -// Enable/Disable typed arrays use, for testing -// -exports.setTyped = function (on) { - if (on) { - exports.Buf8 = Uint8Array; - exports.Buf16 = Uint16Array; - exports.Buf32 = Int32Array; - exports.assign(exports, fnTyped); - } else { - exports.Buf8 = Array; - exports.Buf16 = Array; - exports.Buf32 = Array; - exports.assign(exports, fnUntyped); - } -}; - -exports.setTyped(TYPED_OK); diff --git a/lib/utils/strings.js b/lib/utils/strings.js index cf2606b..75e5ad6 100644 --- a/lib/utils/strings.js +++ b/lib/utils/strings.js @@ -2,25 +2,20 @@ 'use strict'; -var utils = require('./common'); - - // Quick check if we can use fast array to bin string conversion // // - apply(Array) can fail on Android 2.2 // - apply(Uint8Array) can fail on iOS 5.1 Safari // -var STR_APPLY_OK = true; var STR_APPLY_UIA_OK = true; -try { String.fromCharCode.apply(null, [ 0 ]); } catch (__) { STR_APPLY_OK = false; } try { String.fromCharCode.apply(null, new Uint8Array(1)); } catch (__) { STR_APPLY_UIA_OK = false; } // Table with utf8 lengths (calculated by first byte of sequence) // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS, // because max possible codepoint is 0x10ffff -var _utf8len = new utils.Buf8(256); +var _utf8len = new Uint8Array(256); for (var q = 0; q < 256; q++) { _utf8len[q] = (q >= 252 ? 6 : q >= 248 ? 5 : q >= 240 ? 4 : q >= 224 ? 3 : q >= 192 ? 2 : 1); } @@ -45,7 +40,7 @@ exports.string2buf = function (str) { } // allocate buffer - buf = new utils.Buf8(buf_len); + buf = new Uint8Array(buf_len); // convert for (i = 0, m_pos = 0; i < buf_len; m_pos++) { @@ -87,8 +82,8 @@ function buf2binstring(buf, len) { // If the length of the buffer is smaller than that, we can use this optimization, // otherwise we will take a slower path. if (len < 65534) { - if ((buf.subarray && STR_APPLY_UIA_OK) || (!buf.subarray && STR_APPLY_OK)) { - return String.fromCharCode.apply(null, utils.shrinkBuf(buf, len)); + if (buf.subarray && STR_APPLY_UIA_OK) { + return String.fromCharCode.apply(null, buf.length === len ? buf : buf.subarray(0, len)); } } diff --git a/lib/zlib/deflate.js b/lib/zlib/deflate.js index 3145698..3fbab27 100644 --- a/lib/zlib/deflate.js +++ b/lib/zlib/deflate.js @@ -19,7 +19,6 @@ // misrepresented as being the original software. // 3. This notice may not be removed or altered from any source distribution. -var utils = require('../utils/common'); var trees = require('./trees'); var adler32 = require('./adler32'); var crc32 = require('./crc32'); @@ -150,7 +149,7 @@ function flush_pending(strm) { } if (len === 0) { return; } - utils.arraySet(strm.output, s.pending_buf, s.pending_out, len, strm.next_out); + strm.output.set(s.pending_buf.subarray(s.pending_out, s.pending_out + len), strm.next_out); strm.next_out += len; s.pending_out += len; strm.total_out += len; @@ -203,7 +202,7 @@ function read_buf(strm, buf, start, size) { strm.avail_in -= len; // zmemcpy(buf, strm->next_in, len); - utils.arraySet(buf, strm.input, strm.next_in, len, start); + buf.set(strm.input.subarray(strm.next_in, strm.next_in + len), start); if (strm.state.wrap === 1) { strm.adler = adler32(strm.adler, buf, len, start); } @@ -370,7 +369,7 @@ function fill_window(s) { */ if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) { - utils.arraySet(s.window, s.window, _w_size, _w_size, 0); + s.window.set(s.window.subarray(_w_size, _w_size + _w_size), 0); s.match_start -= _w_size; s.strstart -= _w_size; /* we now have strstart >= MAX_DIST */ @@ -1192,9 +1191,9 @@ function DeflateState() { // Use flat array of DOUBLE size, with interleaved fata, // because JS does not support effective - this.dyn_ltree = new utils.Buf16(HEAP_SIZE * 2); - this.dyn_dtree = new utils.Buf16((2 * D_CODES + 1) * 2); - this.bl_tree = new utils.Buf16((2 * BL_CODES + 1) * 2); + this.dyn_ltree = new Uint16Array(HEAP_SIZE * 2); + this.dyn_dtree = new Uint16Array((2 * D_CODES + 1) * 2); + this.bl_tree = new Uint16Array((2 * BL_CODES + 1) * 2); zero(this.dyn_ltree); zero(this.dyn_dtree); zero(this.bl_tree); @@ -1204,11 +1203,11 @@ function DeflateState() { this.bl_desc = null; /* desc. for bit length tree */ //ush bl_count[MAX_BITS+1]; - this.bl_count = new utils.Buf16(MAX_BITS + 1); + this.bl_count = new Uint16Array(MAX_BITS + 1); /* number of codes at each bit length for an optimal tree */ //int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */ - this.heap = new utils.Buf16(2 * L_CODES + 1); /* heap used to build the Huffman trees */ + this.heap = new Uint16Array(2 * L_CODES + 1); /* heap used to build the Huffman trees */ zero(this.heap); this.heap_len = 0; /* number of elements in the heap */ @@ -1217,7 +1216,7 @@ function DeflateState() { * The same heap array is used to build all trees. */ - this.depth = new utils.Buf16(2 * L_CODES + 1); //uch depth[2*L_CODES+1]; + this.depth = new Uint16Array(2 * L_CODES + 1); //uch depth[2*L_CODES+1]; zero(this.depth); /* Depth of each subtree used as tie breaker for trees of equal frequency */ @@ -1373,9 +1372,9 @@ function deflateInit2(strm, level, method, windowBits, memLevel, strategy) { s.hash_mask = s.hash_size - 1; s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH); - s.window = new utils.Buf8(s.w_size * 2); - s.head = new utils.Buf16(s.hash_size); - s.prev = new utils.Buf16(s.w_size); + s.window = new Uint8Array(s.w_size * 2); + s.head = new Uint16Array(s.hash_size); + s.prev = new Uint16Array(s.w_size); // Don't need mem init magic for JS. //s.high_water = 0; /* nothing written to s->window yet */ @@ -1386,7 +1385,7 @@ function deflateInit2(strm, level, method, windowBits, memLevel, strategy) { //overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); //s->pending_buf = (uchf *) overlay; - s.pending_buf = new utils.Buf8(s.pending_buf_size); + s.pending_buf = new Uint8Array(s.pending_buf_size); // It is offset from `s.pending_buf` (size is `s.lit_bufsize * 2`) //s->d_buf = overlay + s->lit_bufsize/sizeof(ush); @@ -1811,8 +1810,8 @@ function deflateSetDictionary(strm, dictionary) { } /* use the tail */ // dictionary = dictionary.slice(dictLength - s.w_size); - tmpDict = new utils.Buf8(s.w_size); - utils.arraySet(tmpDict, dictionary, dictLength - s.w_size, s.w_size, 0); + tmpDict = new Uint8Array(s.w_size); + tmpDict.set(dictionary.subarray(dictLength - s.w_size, dictLength), 0); dictionary = tmpDict; dictLength = s.w_size; } diff --git a/lib/zlib/inflate.js b/lib/zlib/inflate.js index 6384844..56f53b6 100644 --- a/lib/zlib/inflate.js +++ b/lib/zlib/inflate.js @@ -19,7 +19,6 @@ // misrepresented as being the original software. // 3. This notice may not be removed or altered from any source distribution. -var utils = require('../utils/common'); var adler32 = require('./adler32'); var crc32 = require('./crc32'); var inflate_fast = require('./inffast'); @@ -161,14 +160,14 @@ function InflateState() { this.have = 0; /* number of code lengths in lens[] */ this.next = null; /* next available space in codes[] */ - this.lens = new utils.Buf16(320); /* temporary storage for code lengths */ - this.work = new utils.Buf16(288); /* work area for code table building */ + this.lens = new Uint16Array(320); /* temporary storage for code lengths */ + this.work = new Uint16Array(288); /* work area for code table building */ /* because we don't have pointers in js, we use lencode and distcode directly as buffers so we don't need codes */ - //this.codes = new utils.Buf32(ENOUGH); /* space for code tables */ + //this.codes = new Int32Array(ENOUGH); /* space for code tables */ this.lendyn = null; /* dynamic table for length/literal codes (JS specific) */ this.distdyn = null; /* dynamic table for distance codes (JS specific) */ this.sane = 0; /* if false, allow invalid distance too far */ @@ -194,8 +193,8 @@ function inflateResetKeep(strm) { state.hold = 0; state.bits = 0; //state.lencode = state.distcode = state.next = state.codes; - state.lencode = state.lendyn = new utils.Buf32(ENOUGH_LENS); - state.distcode = state.distdyn = new utils.Buf32(ENOUGH_DISTS); + state.lencode = state.lendyn = new Int32Array(ENOUGH_LENS); + state.distcode = state.distdyn = new Int32Array(ENOUGH_DISTS); state.sane = 1; state.back = -1; @@ -293,8 +292,8 @@ function fixedtables(state) { if (virgin) { var sym; - lenfix = new utils.Buf32(512); - distfix = new utils.Buf32(32); + lenfix = new Int32Array(512); + distfix = new Int32Array(32); /* literal/length table */ sym = 0; @@ -346,12 +345,12 @@ function updatewindow(strm, src, end, copy) { state.wnext = 0; state.whave = 0; - state.window = new utils.Buf8(state.wsize); + state.window = new Uint8Array(state.wsize); } /* copy state->wsize or less output bytes into the circular window */ if (copy >= state.wsize) { - utils.arraySet(state.window, src, end - state.wsize, state.wsize, 0); + state.window.set(src.subarray(end - state.wsize, end), 0); state.wnext = 0; state.whave = state.wsize; } @@ -361,11 +360,11 @@ function updatewindow(strm, src, end, copy) { dist = copy; } //zmemcpy(state->window + state->wnext, end - copy, dist); - utils.arraySet(state.window, src, end - copy, dist, state.wnext); + state.window.set(src.subarray(end - copy, end - copy + dist), state.wnext); copy -= dist; if (copy) { //zmemcpy(state->window, end - copy, copy); - utils.arraySet(state.window, src, end - copy, copy, 0); + state.window.set(src.subarray(end - copy, end), 0); state.wnext = copy; state.whave = state.wsize; } @@ -396,7 +395,7 @@ function inflate(strm, flush) { var last_bits, last_op, last_val; // paked "last" denormalized (JS specific) var len; /* length to copy for repeats, bits to drop */ var ret; /* return code */ - var hbuf = new utils.Buf8(4); /* buffer for gzip header crc calculation */ + var hbuf = new Uint8Array(4); /* buffer for gzip header crc calculation */ var opts; var n; // temporary var for NEED_BITS @@ -626,15 +625,15 @@ function inflate(strm, flush) { len = state.head.extra_len - state.length; if (!state.head.extra) { // Use untyped array for more convenient processing later - state.head.extra = new Array(state.head.extra_len); + state.head.extra = new Uint8Array(state.head.extra_len); } - utils.arraySet( - state.head.extra, - input, - next, - // extra field is limited to 65536 bytes - // - no need for additional size check - copy, + state.head.extra.set( + input.subarray( + next, + // extra field is limited to 65536 bytes + // - no need for additional size check + next + copy + ), /*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/ len ); @@ -860,7 +859,7 @@ function inflate(strm, flush) { if (copy > left) { copy = left; } if (copy === 0) { break inf_leave; } //--- zmemcpy(put, next, copy); --- - utils.arraySet(output, input, next, copy, put); + output.set(input.subarray(next, next + copy), put); //---// have -= copy; next += copy; diff --git a/lib/zlib/inftrees.js b/lib/zlib/inftrees.js index ec4e4e9..f29aa31 100644 --- a/lib/zlib/inftrees.js +++ b/lib/zlib/inftrees.js @@ -19,8 +19,6 @@ // misrepresented as being the original software. // 3. This notice may not be removed or altered from any source distribution. -var utils = require('../utils/common'); - var MAXBITS = 15; var ENOUGH_LENS = 852; var ENOUGH_DISTS = 592; @@ -75,8 +73,8 @@ module.exports = function inflate_table(type, lens, lens_index, codes, table, ta var base_index = 0; // var shoextra; /* extra bits table to use */ var end; /* use base and extra for symbol > end */ - var count = new utils.Buf16(MAXBITS + 1); //[MAXBITS+1]; /* number of codes of each length */ - var offs = new utils.Buf16(MAXBITS + 1); //[MAXBITS+1]; /* offsets in table for each length */ + var count = new Uint16Array(MAXBITS + 1); //[MAXBITS+1]; /* number of codes of each length */ + var offs = new Uint16Array(MAXBITS + 1); //[MAXBITS+1]; /* offsets in table for each length */ var extra = null; var extra_index = 0; diff --git a/lib/zlib/trees.js b/lib/zlib/trees.js index aaf15db..02ce472 100644 --- a/lib/zlib/trees.js +++ b/lib/zlib/trees.js @@ -21,8 +21,6 @@ /* eslint-disable space-unary-ops */ -var utils = require('../utils/common'); - /* Public constants ==========================================================*/ /* ===========================================================================*/ @@ -560,7 +558,7 @@ function copy_block(s, buf, len, header) // while (len--) { // put_byte(s, *buf++); // } - utils.arraySet(s.pending_buf, s.window, buf, len, s.pending); + s.pending_buf.set(s.window.subarray(buf, buf + len), s.pending); s.pending += len; } diff --git a/test/chunks.js b/test/chunks.js index 3bcb3e4..a987464 100644 --- a/test/chunks.js +++ b/test/chunks.js @@ -8,7 +8,6 @@ var assert = require('assert'); var helpers = require('./helpers'); -var pako_utils = require('../lib/utils/common'); var pako = require('../index'); @@ -16,7 +15,7 @@ var samples = helpers.loadSamples(); function randomBuf(size) { - var buf = new pako_utils.Buf8(size); + var buf = new Uint8Array(size); for (var i = 0; i < size; i++) { buf[i] = Math.round(Math.random() * 256); } @@ -38,8 +37,8 @@ function testChunk(buf, expected, packer, chunkSize) { pos = 0; for (i = 0; i < count; i++) { size = (buf.length - pos) < chunkSize ? buf.length - pos : chunkSize; - _in = new pako_utils.Buf8(size); - pako_utils.arraySet(_in, buf, pos, size, 0); + _in = new Uint8Array(size); + _in.set(buf.subarray(pos, pos + size), 0); packer.push(_in, i === count - 1); pos += chunkSize; } diff --git a/test/gzip_specials.js b/test/gzip_specials.js index 90cc8bc..6ca6a63 100644 --- a/test/gzip_specials.js +++ b/test/gzip_specials.js @@ -8,7 +8,6 @@ var fs = require('fs'); var path = require('path'); var assert = require('assert'); -var pako_utils = require('../lib/utils/common'); var pako = require('../index'); var cmp = require('./helpers').cmpBuf; @@ -66,8 +65,8 @@ describe('Gzip special cases', function () { do { len = data.length - pos; - _in = new pako_utils.Buf8(len); - pako_utils.arraySet(_in, data, pos, len, 0); + _in = new Uint8Array(len); + _in.set(data.subarray(pos, pos + len), 0); inflator = new pako.Inflate(); strm = inflator.strm; diff --git a/test/helpers.js b/test/helpers.js index 23f9acb..633f1d2 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -6,7 +6,6 @@ var path = require('path'); var assert = require('assert'); var b = require('buffer-from'); -var pako_utils = require('../lib/utils/common'); var pako = require('../index'); // Load fixtures to test @@ -75,12 +74,6 @@ function testSamples(zlib_method, pako_method, samples, options) { Object.keys(samples).forEach(function (name) { var data = samples[name]; - // with untyped arrays - pako_utils.setTyped(false); - testSingle(zlib_method, pako_method, data, options); - - // with typed arrays - pako_utils.setTyped(true); testSingle(zlib_method, pako_method, data, options); }); } @@ -95,18 +88,7 @@ function testInflate(samples, inflateOptions, deflateOptions) { if (!samples.hasOwnProperty(name)) continue; data = samples[name]; - // always use the same data type to generate sample - pako_utils.setTyped(true); deflated = pako.deflate(data, deflateOptions); - - // with untyped arrays - pako_utils.setTyped(false); - inflated = pako.inflate(deflated, inflateOptions); - pako_utils.setTyped(true); - - assert.deepEqual(new Uint8Array(inflated), data); - - // with typed arrays inflated = pako.inflate(deflated, inflateOptions); assert.deepEqual(inflated, data); diff --git a/test/inflate_cover_ported.js b/test/inflate_cover_ported.js index 51c49e4..55b8c7c 100644 --- a/test/inflate_cover_ported.js +++ b/test/inflate_cover_ported.js @@ -32,7 +32,7 @@ function testInflate(hex, wbits, status) { assert(e === msg[status]); return; } - inflator.push(h2b(hex), true); + inflator.push(new Uint8Array(h2b(hex)), true); assert.equal(inflator.err, status); }