Removed Array support

This commit is contained in:
Vitaly Puzrin 2020-11-07 19:52:58 +03:00
parent 23563dfae9
commit f5c3c29848
17 changed files with 94 additions and 230 deletions

View file

@ -8,7 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [2.0.0] - WIP ## [2.0.0] - WIP
### Changed ### 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`. - Removed support of `Inflate` & `Deflate` instance create without `new`.
- Upgrade build tools to modern ones. - Upgrade build tools to modern ones.

View file

@ -1,10 +0,0 @@
'use strict';
var pako = require('../../../');
exports.run = function (data, level) {
pako.deflate(data.string, {
level: level,
to: 'string'
});
};

View file

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

View file

@ -1,9 +0,0 @@
'use strict';
var pako = require('../../../');
exports.run = function (data) {
return pako.inflate(data.deflateString, {
to: 'string'
});
};

View file

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

View file

@ -43,7 +43,7 @@ var Z_DEFLATED = 8;
**/ **/
/** /**
* Deflate.result -> Uint8Array|Array * Deflate.result -> Uint8Array
* *
* Compressed result, generated by default [[Deflate#onData]] * Compressed result, generated by default [[Deflate#onData]]
* and [[Deflate#onEnd]] handlers. Filled after you push last chunk * and [[Deflate#onEnd]] handlers. Filled after you push last chunk
@ -184,7 +184,7 @@ function Deflate(options) {
/** /**
* Deflate#push(data[, mode]) -> Boolean * 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. * converted to utf8 byte sequence.
* - mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes. * - 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. * 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. * On fail call [[Deflate#onEnd]] with error code and return false.
* *
* We strongly recommend to use `Uint8Array` on input for best speed (output * Note. Don't skip last param and always use the same type in your code
* array format is detected automatically). Also, don't skip last param and always * (boolean or number). That will improve JS speed.
* 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 * ##### Example
* *
@ -235,7 +232,7 @@ Deflate.prototype.push = function (data, mode) {
do { do {
if (strm.avail_out === 0) { if (strm.avail_out === 0) {
strm.output = new utils.Buf8(chunkSize); strm.output = new Uint8Array(chunkSize);
strm.next_out = 0; strm.next_out = 0;
strm.avail_out = chunkSize; strm.avail_out = chunkSize;
} }
@ -247,7 +244,7 @@ Deflate.prototype.push = function (data, mode) {
return false; return false;
} }
if (strm.avail_out === 0 || (strm.avail_in === 0 && (_mode === Z_FINISH || _mode === Z_SYNC_FLUSH))) { 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); } 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 * Deflate#onData(chunk) -> Void
* - chunk (Uint8Array|Array|String): output data. Type of array depends * - chunk (Uint8Array): output data.
* on js engine support. When string output requested, each chunk
* will be string.
* *
* By default, stores data blocks in `chunks[]` property and glue * By default, stores data blocks in `chunks[]` property and glue
* those in `onEnd`. Override this handler, if you need another behaviour. * 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 * deflate(data[, options]) -> Uint8Array
* - data (Uint8Array|Array|String): input data to compress. * - data (Uint8Array|String): input data to compress.
* - options (Object): zlib deflate options. * - options (Object): zlib deflate options.
* *
* Compress `data` with deflate algorithm and `options`. * Compress `data` with deflate algorithm and `options`.
@ -350,8 +345,8 @@ function deflate(input, options) {
/** /**
* deflateRaw(data[, options]) -> Uint8Array|Array * deflateRaw(data[, options]) -> Uint8Array
* - data (Uint8Array|Array|String): input data to compress. * - data (Uint8Array|String): input data to compress.
* - options (Object): zlib deflate options. * - options (Object): zlib deflate options.
* *
* The same as [[deflate]], but creates raw data, without wrapper * The same as [[deflate]], but creates raw data, without wrapper
@ -365,8 +360,8 @@ function deflateRaw(input, options) {
/** /**
* gzip(data[, options]) -> Uint8Array|Array * gzip(data[, options]) -> Uint8Array
* - data (Uint8Array|Array|String): input data to compress. * - data (Uint8Array|String): input data to compress.
* - options (Object): zlib deflate options. * - options (Object): zlib deflate options.
* *
* The same as [[deflate]], but create gzip wrapper instead of * The same as [[deflate]], but create gzip wrapper instead of

View file

@ -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]] * Uncompressed result, generated by default [[Inflate#onData]]
* and [[Inflate#onEnd]] handlers. Filled after you push last chunk * and [[Inflate#onEnd]] handlers. Filled after you push last chunk
@ -162,7 +162,7 @@ function Inflate(options) {
/** /**
* Inflate#push(data[, mode]) -> Boolean * 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. * - 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. * 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 * Note. Don't skip last param and always use the same type in your code
* (boolean or number). That will improve JS speed. * (boolean or number). That will improve JS speed.
* *
* For regular `Array`-s make sure all elements are [0..255].
*
* ##### Example * ##### Example
* *
* ```javascript * ```javascript
@ -213,7 +211,7 @@ Inflate.prototype.push = function (data, mode) {
do { do {
if (strm.avail_out === 0) { if (strm.avail_out === 0) {
strm.output = new utils.Buf8(chunkSize); strm.output = new Uint8Array(chunkSize);
strm.next_out = 0; strm.next_out = 0;
strm.avail_out = chunkSize; strm.avail_out = chunkSize;
} }
@ -248,12 +246,12 @@ Inflate.prototype.push = function (data, mode) {
// move tail // move tail
strm.next_out = tail; strm.next_out = tail;
strm.avail_out = chunkSize - 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); this.onData(utf8str);
} else { } 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 * Inflate#onData(chunk) -> Void
* - chunk (Uint8Array|Array): output data. Type of array depends * - chunk (Uint8Array|String): output data. When string output requested,
* on js engine support. When string output requested, each chunk * each chunk will be string.
* will be string.
* *
* By default, stores data blocks in `chunks[]` property and glue * By default, stores data blocks in `chunks[]` property and glue
* those in `onEnd`. Override this handler, if you need another behaviour. * 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 * inflate(data[, options]) -> Uint8Array|String
* - data (Uint8Array|Array): input data to decompress. * - data (Uint8Array): input data to decompress.
* - options (Object): zlib inflate options. * - options (Object): zlib inflate options.
* *
* Decompress `data` with inflate/ungzip and `options`. Autodetect * Decompress `data` with inflate/ungzip and `options`. Autodetect
@ -385,8 +382,8 @@ function inflate(input, options) {
/** /**
* inflateRaw(data[, options]) -> Uint8Array|Array|String * inflateRaw(data[, options]) -> Uint8Array|String
* - data (Uint8Array|Array): input data to decompress. * - data (Uint8Array): input data to decompress.
* - options (Object): zlib inflate options. * - options (Object): zlib inflate options.
* *
* The same as [[inflate]], but creates raw data, without wrapper * The same as [[inflate]], but creates raw data, without wrapper
@ -400,8 +397,8 @@ function inflateRaw(input, options) {
/** /**
* ungzip(data[, options]) -> Uint8Array|Array|String * ungzip(data[, options]) -> Uint8Array|String
* - data (Uint8Array|Array): input data to decompress. * - data (Uint8Array): input data to decompress.
* - options (Object): zlib inflate options. * - options (Object): zlib inflate options.
* *
* Just shortcut to [[inflate]], because it autodetects format * Just shortcut to [[inflate]], because it autodetects format

View file

@ -1,15 +1,11 @@
'use strict'; 'use strict';
var TYPED_OK = (typeof Uint8Array !== 'undefined') &&
(typeof Uint16Array !== 'undefined') &&
(typeof Int32Array !== 'undefined');
function _has(obj, key) { function _has(obj, key) {
return Object.prototype.hasOwnProperty.call(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); var sources = Array.prototype.slice.call(arguments, 1);
while (sources.length) { while (sources.length) {
var source = sources.shift(); var source = sources.shift();
@ -30,76 +26,24 @@ exports.assign = function (obj /*from1, from2, from3, ...*/) {
}; };
// reduce buffer size, avoiding mem copy // Join array of chunks to single array.
exports.shrinkBuf = function (buf, size) { module.exports.flattenChunks = function (chunks) {
if (buf.length === size) { return buf; } var i, l, len, pos, chunk, result;
if (buf.subarray) { return buf.subarray(0, size); }
buf.length = size;
return buf;
};
// calculate data length
var fnTyped = { len = 0;
arraySet: function (dest, src, src_offs, len, dest_offs) { for (i = 0, l = chunks.length; i < l; i++) {
if (src.subarray && dest.subarray) { len += chunks[i].length;
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;
} }
};
var fnUntyped = { // join chunks
arraySet: function (dest, src, src_offs, len, dest_offs) { result = new Uint8Array(len);
for (var i = 0; i < len; i++) { pos = 0;
dest[dest_offs + i] = src[src_offs + i]; for (i = 0, l = chunks.length; i < l; i++) {
} chunk = chunks[i];
}, result.set(chunk, pos);
// Join array of chunks to single array. pos += chunk.length;
flattenChunks: function (chunks) {
return [].concat.apply([], chunks);
} }
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);

View file

@ -2,25 +2,20 @@
'use strict'; 'use strict';
var utils = require('./common');
// Quick check if we can use fast array to bin string conversion // Quick check if we can use fast array to bin string conversion
// //
// - apply(Array) can fail on Android 2.2 // - apply(Array) can fail on Android 2.2
// - apply(Uint8Array) can fail on iOS 5.1 Safari // - apply(Uint8Array) can fail on iOS 5.1 Safari
// //
var STR_APPLY_OK = true;
var STR_APPLY_UIA_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; } try { String.fromCharCode.apply(null, new Uint8Array(1)); } catch (__) { STR_APPLY_UIA_OK = false; }
// Table with utf8 lengths (calculated by first byte of sequence) // 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, // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,
// because max possible codepoint is 0x10ffff // because max possible codepoint is 0x10ffff
var _utf8len = new utils.Buf8(256); var _utf8len = new Uint8Array(256);
for (var q = 0; q < 256; q++) { 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); _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 // allocate buffer
buf = new utils.Buf8(buf_len); buf = new Uint8Array(buf_len);
// convert // convert
for (i = 0, m_pos = 0; i < buf_len; m_pos++) { 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, // If the length of the buffer is smaller than that, we can use this optimization,
// otherwise we will take a slower path. // otherwise we will take a slower path.
if (len < 65534) { if (len < 65534) {
if ((buf.subarray && STR_APPLY_UIA_OK) || (!buf.subarray && STR_APPLY_OK)) { if (buf.subarray && STR_APPLY_UIA_OK) {
return String.fromCharCode.apply(null, utils.shrinkBuf(buf, len)); return String.fromCharCode.apply(null, buf.length === len ? buf : buf.subarray(0, len));
} }
} }

View file

@ -19,7 +19,6 @@
// misrepresented as being the original software. // misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution. // 3. This notice may not be removed or altered from any source distribution.
var utils = require('../utils/common');
var trees = require('./trees'); var trees = require('./trees');
var adler32 = require('./adler32'); var adler32 = require('./adler32');
var crc32 = require('./crc32'); var crc32 = require('./crc32');
@ -150,7 +149,7 @@ function flush_pending(strm) {
} }
if (len === 0) { return; } 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; strm.next_out += len;
s.pending_out += len; s.pending_out += len;
strm.total_out += len; strm.total_out += len;
@ -203,7 +202,7 @@ function read_buf(strm, buf, start, size) {
strm.avail_in -= len; strm.avail_in -= len;
// zmemcpy(buf, strm->next_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) { if (strm.state.wrap === 1) {
strm.adler = adler32(strm.adler, buf, len, start); 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)) { 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.match_start -= _w_size;
s.strstart -= _w_size; s.strstart -= _w_size;
/* we now have strstart >= MAX_DIST */ /* we now have strstart >= MAX_DIST */
@ -1192,9 +1191,9 @@ function DeflateState() {
// Use flat array of DOUBLE size, with interleaved fata, // Use flat array of DOUBLE size, with interleaved fata,
// because JS does not support effective // because JS does not support effective
this.dyn_ltree = new utils.Buf16(HEAP_SIZE * 2); this.dyn_ltree = new Uint16Array(HEAP_SIZE * 2);
this.dyn_dtree = new utils.Buf16((2 * D_CODES + 1) * 2); this.dyn_dtree = new Uint16Array((2 * D_CODES + 1) * 2);
this.bl_tree = new utils.Buf16((2 * BL_CODES + 1) * 2); this.bl_tree = new Uint16Array((2 * BL_CODES + 1) * 2);
zero(this.dyn_ltree); zero(this.dyn_ltree);
zero(this.dyn_dtree); zero(this.dyn_dtree);
zero(this.bl_tree); zero(this.bl_tree);
@ -1204,11 +1203,11 @@ function DeflateState() {
this.bl_desc = null; /* desc. for bit length tree */ this.bl_desc = null; /* desc. for bit length tree */
//ush bl_count[MAX_BITS+1]; //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 */ /* number of codes at each bit length for an optimal tree */
//int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */ //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); zero(this.heap);
this.heap_len = 0; /* number of elements in the 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. * 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); zero(this.depth);
/* Depth of each subtree used as tie breaker for trees of equal frequency /* 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_mask = s.hash_size - 1;
s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH); s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH);
s.window = new utils.Buf8(s.w_size * 2); s.window = new Uint8Array(s.w_size * 2);
s.head = new utils.Buf16(s.hash_size); s.head = new Uint16Array(s.hash_size);
s.prev = new utils.Buf16(s.w_size); s.prev = new Uint16Array(s.w_size);
// Don't need mem init magic for JS. // Don't need mem init magic for JS.
//s.high_water = 0; /* nothing written to s->window yet */ //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); //overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
//s->pending_buf = (uchf *) overlay; //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`) // It is offset from `s.pending_buf` (size is `s.lit_bufsize * 2`)
//s->d_buf = overlay + s->lit_bufsize/sizeof(ush); //s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
@ -1811,8 +1810,8 @@ function deflateSetDictionary(strm, dictionary) {
} }
/* use the tail */ /* use the tail */
// dictionary = dictionary.slice(dictLength - s.w_size); // dictionary = dictionary.slice(dictLength - s.w_size);
tmpDict = new utils.Buf8(s.w_size); tmpDict = new Uint8Array(s.w_size);
utils.arraySet(tmpDict, dictionary, dictLength - s.w_size, s.w_size, 0); tmpDict.set(dictionary.subarray(dictLength - s.w_size, dictLength), 0);
dictionary = tmpDict; dictionary = tmpDict;
dictLength = s.w_size; dictLength = s.w_size;
} }

View file

@ -19,7 +19,6 @@
// misrepresented as being the original software. // misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution. // 3. This notice may not be removed or altered from any source distribution.
var utils = require('../utils/common');
var adler32 = require('./adler32'); var adler32 = require('./adler32');
var crc32 = require('./crc32'); var crc32 = require('./crc32');
var inflate_fast = require('./inffast'); var inflate_fast = require('./inffast');
@ -161,14 +160,14 @@ function InflateState() {
this.have = 0; /* number of code lengths in lens[] */ this.have = 0; /* number of code lengths in lens[] */
this.next = null; /* next available space in codes[] */ this.next = null; /* next available space in codes[] */
this.lens = new utils.Buf16(320); /* temporary storage for code lengths */ this.lens = new Uint16Array(320); /* temporary storage for code lengths */
this.work = new utils.Buf16(288); /* work area for code table building */ 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 because we don't have pointers in js, we use lencode and distcode directly
as buffers so we don't need codes 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.lendyn = null; /* dynamic table for length/literal codes (JS specific) */
this.distdyn = null; /* dynamic table for distance codes (JS specific) */ this.distdyn = null; /* dynamic table for distance codes (JS specific) */
this.sane = 0; /* if false, allow invalid distance too far */ this.sane = 0; /* if false, allow invalid distance too far */
@ -194,8 +193,8 @@ function inflateResetKeep(strm) {
state.hold = 0; state.hold = 0;
state.bits = 0; state.bits = 0;
//state.lencode = state.distcode = state.next = state.codes; //state.lencode = state.distcode = state.next = state.codes;
state.lencode = state.lendyn = new utils.Buf32(ENOUGH_LENS); state.lencode = state.lendyn = new Int32Array(ENOUGH_LENS);
state.distcode = state.distdyn = new utils.Buf32(ENOUGH_DISTS); state.distcode = state.distdyn = new Int32Array(ENOUGH_DISTS);
state.sane = 1; state.sane = 1;
state.back = -1; state.back = -1;
@ -293,8 +292,8 @@ function fixedtables(state) {
if (virgin) { if (virgin) {
var sym; var sym;
lenfix = new utils.Buf32(512); lenfix = new Int32Array(512);
distfix = new utils.Buf32(32); distfix = new Int32Array(32);
/* literal/length table */ /* literal/length table */
sym = 0; sym = 0;
@ -346,12 +345,12 @@ function updatewindow(strm, src, end, copy) {
state.wnext = 0; state.wnext = 0;
state.whave = 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 */ /* copy state->wsize or less output bytes into the circular window */
if (copy >= state.wsize) { 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.wnext = 0;
state.whave = state.wsize; state.whave = state.wsize;
} }
@ -361,11 +360,11 @@ function updatewindow(strm, src, end, copy) {
dist = copy; dist = copy;
} }
//zmemcpy(state->window + state->wnext, end - copy, dist); //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; copy -= dist;
if (copy) { if (copy) {
//zmemcpy(state->window, end - copy, 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.wnext = copy;
state.whave = state.wsize; 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 last_bits, last_op, last_val; // paked "last" denormalized (JS specific)
var len; /* length to copy for repeats, bits to drop */ var len; /* length to copy for repeats, bits to drop */
var ret; /* return code */ 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 opts;
var n; // temporary var for NEED_BITS var n; // temporary var for NEED_BITS
@ -626,15 +625,15 @@ function inflate(strm, flush) {
len = state.head.extra_len - state.length; len = state.head.extra_len - state.length;
if (!state.head.extra) { if (!state.head.extra) {
// Use untyped array for more convenient processing later // 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.set(
state.head.extra, input.subarray(
input, next,
next, // extra field is limited to 65536 bytes
// extra field is limited to 65536 bytes // - no need for additional size check
// - no need for additional size check next + copy
copy, ),
/*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/ /*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/
len len
); );
@ -860,7 +859,7 @@ function inflate(strm, flush) {
if (copy > left) { copy = left; } if (copy > left) { copy = left; }
if (copy === 0) { break inf_leave; } if (copy === 0) { break inf_leave; }
//--- zmemcpy(put, next, copy); --- //--- zmemcpy(put, next, copy); ---
utils.arraySet(output, input, next, copy, put); output.set(input.subarray(next, next + copy), put);
//---// //---//
have -= copy; have -= copy;
next += copy; next += copy;

View file

@ -19,8 +19,6 @@
// misrepresented as being the original software. // misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution. // 3. This notice may not be removed or altered from any source distribution.
var utils = require('../utils/common');
var MAXBITS = 15; var MAXBITS = 15;
var ENOUGH_LENS = 852; var ENOUGH_LENS = 852;
var ENOUGH_DISTS = 592; 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 base_index = 0;
// var shoextra; /* extra bits table to use */ // var shoextra; /* extra bits table to use */
var end; /* use base and extra for symbol > end */ 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 count = new Uint16Array(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 offs = new Uint16Array(MAXBITS + 1); //[MAXBITS+1]; /* offsets in table for each length */
var extra = null; var extra = null;
var extra_index = 0; var extra_index = 0;

View file

@ -21,8 +21,6 @@
/* eslint-disable space-unary-ops */ /* eslint-disable space-unary-ops */
var utils = require('../utils/common');
/* Public constants ==========================================================*/ /* Public constants ==========================================================*/
/* ===========================================================================*/ /* ===========================================================================*/
@ -560,7 +558,7 @@ function copy_block(s, buf, len, header)
// while (len--) { // while (len--) {
// put_byte(s, *buf++); // 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; s.pending += len;
} }

View file

@ -8,7 +8,6 @@ var assert = require('assert');
var helpers = require('./helpers'); var helpers = require('./helpers');
var pako_utils = require('../lib/utils/common');
var pako = require('../index'); var pako = require('../index');
@ -16,7 +15,7 @@ var samples = helpers.loadSamples();
function randomBuf(size) { function randomBuf(size) {
var buf = new pako_utils.Buf8(size); var buf = new Uint8Array(size);
for (var i = 0; i < size; i++) { for (var i = 0; i < size; i++) {
buf[i] = Math.round(Math.random() * 256); buf[i] = Math.round(Math.random() * 256);
} }
@ -38,8 +37,8 @@ function testChunk(buf, expected, packer, chunkSize) {
pos = 0; pos = 0;
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
size = (buf.length - pos) < chunkSize ? buf.length - pos : chunkSize; size = (buf.length - pos) < chunkSize ? buf.length - pos : chunkSize;
_in = new pako_utils.Buf8(size); _in = new Uint8Array(size);
pako_utils.arraySet(_in, buf, pos, size, 0); _in.set(buf.subarray(pos, pos + size), 0);
packer.push(_in, i === count - 1); packer.push(_in, i === count - 1);
pos += chunkSize; pos += chunkSize;
} }

View file

@ -8,7 +8,6 @@ var fs = require('fs');
var path = require('path'); var path = require('path');
var assert = require('assert'); var assert = require('assert');
var pako_utils = require('../lib/utils/common');
var pako = require('../index'); var pako = require('../index');
var cmp = require('./helpers').cmpBuf; var cmp = require('./helpers').cmpBuf;
@ -66,8 +65,8 @@ describe('Gzip special cases', function () {
do { do {
len = data.length - pos; len = data.length - pos;
_in = new pako_utils.Buf8(len); _in = new Uint8Array(len);
pako_utils.arraySet(_in, data, pos, len, 0); _in.set(data.subarray(pos, pos + len), 0);
inflator = new pako.Inflate(); inflator = new pako.Inflate();
strm = inflator.strm; strm = inflator.strm;

View file

@ -6,7 +6,6 @@ var path = require('path');
var assert = require('assert'); var assert = require('assert');
var b = require('buffer-from'); var b = require('buffer-from');
var pako_utils = require('../lib/utils/common');
var pako = require('../index'); var pako = require('../index');
// Load fixtures to test // Load fixtures to test
@ -75,12 +74,6 @@ function testSamples(zlib_method, pako_method, samples, options) {
Object.keys(samples).forEach(function (name) { Object.keys(samples).forEach(function (name) {
var data = samples[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); testSingle(zlib_method, pako_method, data, options);
}); });
} }
@ -95,18 +88,7 @@ function testInflate(samples, inflateOptions, deflateOptions) {
if (!samples.hasOwnProperty(name)) continue; if (!samples.hasOwnProperty(name)) continue;
data = samples[name]; data = samples[name];
// always use the same data type to generate sample
pako_utils.setTyped(true);
deflated = pako.deflate(data, deflateOptions); 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); inflated = pako.inflate(deflated, inflateOptions);
assert.deepEqual(inflated, data); assert.deepEqual(inflated, data);

View file

@ -32,7 +32,7 @@ function testInflate(hex, wbits, status) {
assert(e === msg[status]); assert(e === msg[status]);
return; return;
} }
inflator.push(h2b(hex), true); inflator.push(new Uint8Array(h2b(hex)), true);
assert.equal(inflator.err, status); assert.equal(inflator.err, status);
} }