diff options
author | James M Snell <jasnell@gmail.com> | 2016-06-07 14:38:00 -0700 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2016-06-11 17:24:35 -0700 |
commit | 197a4652807bf1d830d67c1d8a4aecdf35fda6a4 (patch) | |
tree | f1066a00879074d5c3d6a03b85965f65251ae57a /lib/zlib.js | |
parent | a173483619cc47999e5016e968ec807a55ccd53c (diff) | |
download | android-node-v8-197a4652807bf1d830d67c1d8a4aecdf35fda6a4.tar.gz android-node-v8-197a4652807bf1d830d67c1d8a4aecdf35fda6a4.tar.bz2 android-node-v8-197a4652807bf1d830d67c1d8a4aecdf35fda6a4.zip |
zlib: move constants into zlib.constants
zlib constants were previously being added to binding in node_zlib.cc.
This moves the zlib constants to node_constants.cc for consistency with
the recent constants refactoring:
https://github.com/nodejs/node/pull/6534
Adds require('zlib').constants to expose the constants
Docs-only deprecates the constants hung directly off require('zlib')
Removes a couple constants from the docs that apparently no longer
exist in the code
PR-URL: https://github.com/nodejs/node/pull/7203
Reviewed-By: Rod Vagg <rod@vagg.org>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Diffstat (limited to 'lib/zlib.js')
-rw-r--r-- | lib/zlib.js | 142 |
1 files changed, 64 insertions, 78 deletions
diff --git a/lib/zlib.js b/lib/zlib.js index ffb7ba6c69..42dd561623 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -9,49 +9,35 @@ const kMaxLength = require('buffer').kMaxLength; const kRangeErrorMessage = 'Cannot create final Buffer. It would be larger ' + 'than 0x' + kMaxLength.toString(16) + ' bytes'; -// zlib doesn't provide these, so kludge them in following the same -// const naming scheme zlib uses. -binding.Z_MIN_WINDOWBITS = 8; -binding.Z_MAX_WINDOWBITS = 15; -binding.Z_DEFAULT_WINDOWBITS = 15; - -// fewer than 64 bytes per chunk is stupid. -// technically it could work with as few as 8, but even 64 bytes -// is absurdly low. Usually a MB or more is best. -binding.Z_MIN_CHUNK = 64; -binding.Z_MAX_CHUNK = Infinity; -binding.Z_DEFAULT_CHUNK = (16 * 1024); - -binding.Z_MIN_MEMLEVEL = 1; -binding.Z_MAX_MEMLEVEL = 9; -binding.Z_DEFAULT_MEMLEVEL = 8; - -binding.Z_MIN_LEVEL = -1; -binding.Z_MAX_LEVEL = 9; -binding.Z_DEFAULT_LEVEL = binding.Z_DEFAULT_COMPRESSION; +const constants = process.binding('constants').zlib; +// These should be considered deprecated // expose all the zlib constants -const bkeys = Object.keys(binding); +const bkeys = Object.keys(constants); for (var bk = 0; bk < bkeys.length; bk++) { var bkey = bkeys[bk]; - if (bkey.match(/^Z/)) { - Object.defineProperty(exports, bkey, { - enumerable: true, value: binding[bkey], writable: false - }); - } + Object.defineProperty(exports, bkey, { + enumerable: true, value: constants[bkey], writable: false + }); } +Object.defineProperty(exports, 'constants', { + configurable: false, + enumerable: true, + value: constants +}); + // translation table for return codes. const codes = { - Z_OK: binding.Z_OK, - Z_STREAM_END: binding.Z_STREAM_END, - Z_NEED_DICT: binding.Z_NEED_DICT, - Z_ERRNO: binding.Z_ERRNO, - Z_STREAM_ERROR: binding.Z_STREAM_ERROR, - Z_DATA_ERROR: binding.Z_DATA_ERROR, - Z_MEM_ERROR: binding.Z_MEM_ERROR, - Z_BUF_ERROR: binding.Z_BUF_ERROR, - Z_VERSION_ERROR: binding.Z_VERSION_ERROR + Z_OK: constants.Z_OK, + Z_STREAM_END: constants.Z_STREAM_END, + Z_NEED_DICT: constants.Z_NEED_DICT, + Z_ERRNO: constants.Z_ERRNO, + Z_STREAM_ERROR: constants.Z_STREAM_ERROR, + Z_DATA_ERROR: constants.Z_DATA_ERROR, + Z_MEM_ERROR: constants.Z_MEM_ERROR, + Z_BUF_ERROR: constants.Z_BUF_ERROR, + Z_VERSION_ERROR: constants.Z_VERSION_ERROR }; const ckeys = Object.keys(codes); @@ -243,52 +229,52 @@ function zlibBufferSync(engine, buffer) { // minimal 2-byte header function Deflate(opts) { if (!(this instanceof Deflate)) return new Deflate(opts); - Zlib.call(this, opts, binding.DEFLATE); + Zlib.call(this, opts, constants.DEFLATE); } function Inflate(opts) { if (!(this instanceof Inflate)) return new Inflate(opts); - Zlib.call(this, opts, binding.INFLATE); + Zlib.call(this, opts, constants.INFLATE); } // gzip - bigger header, same deflate compression function Gzip(opts) { if (!(this instanceof Gzip)) return new Gzip(opts); - Zlib.call(this, opts, binding.GZIP); + Zlib.call(this, opts, constants.GZIP); } function Gunzip(opts) { if (!(this instanceof Gunzip)) return new Gunzip(opts); - Zlib.call(this, opts, binding.GUNZIP); + Zlib.call(this, opts, constants.GUNZIP); } // raw - no header function DeflateRaw(opts) { if (!(this instanceof DeflateRaw)) return new DeflateRaw(opts); - Zlib.call(this, opts, binding.DEFLATERAW); + Zlib.call(this, opts, constants.DEFLATERAW); } function InflateRaw(opts) { if (!(this instanceof InflateRaw)) return new InflateRaw(opts); - Zlib.call(this, opts, binding.INFLATERAW); + Zlib.call(this, opts, constants.INFLATERAW); } // auto-detect header. function Unzip(opts) { if (!(this instanceof Unzip)) return new Unzip(opts); - Zlib.call(this, opts, binding.UNZIP); + Zlib.call(this, opts, constants.UNZIP); } function isValidFlushFlag(flag) { - return flag === binding.Z_NO_FLUSH || - flag === binding.Z_PARTIAL_FLUSH || - flag === binding.Z_SYNC_FLUSH || - flag === binding.Z_FULL_FLUSH || - flag === binding.Z_FINISH || - flag === binding.Z_BLOCK; + return flag === constants.Z_NO_FLUSH || + flag === constants.Z_PARTIAL_FLUSH || + flag === constants.Z_SYNC_FLUSH || + flag === constants.Z_FULL_FLUSH || + flag === constants.Z_FINISH || + flag === constants.Z_BLOCK; } // the Zlib class they all inherit from @@ -298,7 +284,7 @@ function isValidFlushFlag(flag) { function Zlib(opts, mode) { this._opts = opts = opts || {}; - this._chunkSize = opts.chunkSize || exports.Z_DEFAULT_CHUNK; + this._chunkSize = opts.chunkSize || constants.Z_DEFAULT_CHUNK; Transform.call(this, opts); @@ -309,44 +295,44 @@ function Zlib(opts, mode) { throw new Error('Invalid flush flag: ' + opts.finishFlush); } - this._flushFlag = opts.flush || binding.Z_NO_FLUSH; + this._flushFlag = opts.flush || constants.Z_NO_FLUSH; this._finishFlushFlag = typeof opts.finishFlush !== 'undefined' ? - opts.finishFlush : binding.Z_FINISH; + opts.finishFlush : constants.Z_FINISH; if (opts.chunkSize) { - if (opts.chunkSize < exports.Z_MIN_CHUNK || - opts.chunkSize > exports.Z_MAX_CHUNK) { + if (opts.chunkSize < constants.Z_MIN_CHUNK || + opts.chunkSize > constants.Z_MAX_CHUNK) { throw new Error('Invalid chunk size: ' + opts.chunkSize); } } if (opts.windowBits) { - if (opts.windowBits < exports.Z_MIN_WINDOWBITS || - opts.windowBits > exports.Z_MAX_WINDOWBITS) { + if (opts.windowBits < constants.Z_MIN_WINDOWBITS || + opts.windowBits > constants.Z_MAX_WINDOWBITS) { throw new Error('Invalid windowBits: ' + opts.windowBits); } } if (opts.level) { - if (opts.level < exports.Z_MIN_LEVEL || - opts.level > exports.Z_MAX_LEVEL) { + if (opts.level < constants.Z_MIN_LEVEL || + opts.level > constants.Z_MAX_LEVEL) { throw new Error('Invalid compression level: ' + opts.level); } } if (opts.memLevel) { - if (opts.memLevel < exports.Z_MIN_MEMLEVEL || - opts.memLevel > exports.Z_MAX_MEMLEVEL) { + if (opts.memLevel < constants.Z_MIN_MEMLEVEL || + opts.memLevel > constants.Z_MAX_MEMLEVEL) { throw new Error('Invalid memLevel: ' + opts.memLevel); } } if (opts.strategy) { - if (opts.strategy != exports.Z_FILTERED && - opts.strategy != exports.Z_HUFFMAN_ONLY && - opts.strategy != exports.Z_RLE && - opts.strategy != exports.Z_FIXED && - opts.strategy != exports.Z_DEFAULT_STRATEGY) { + if (opts.strategy != constants.Z_FILTERED && + opts.strategy != constants.Z_HUFFMAN_ONLY && + opts.strategy != constants.Z_RLE && + opts.strategy != constants.Z_FIXED && + opts.strategy != constants.Z_DEFAULT_STRATEGY) { throw new Error('Invalid strategy: ' + opts.strategy); } } @@ -373,15 +359,15 @@ function Zlib(opts, mode) { self.emit('error', error); }; - var level = exports.Z_DEFAULT_COMPRESSION; + var level = constants.Z_DEFAULT_COMPRESSION; if (typeof opts.level === 'number') level = opts.level; - var strategy = exports.Z_DEFAULT_STRATEGY; + var strategy = constants.Z_DEFAULT_STRATEGY; if (typeof opts.strategy === 'number') strategy = opts.strategy; - this._handle.init(opts.windowBits || exports.Z_DEFAULT_WINDOWBITS, + this._handle.init(opts.windowBits || constants.Z_DEFAULT_WINDOWBITS, level, - opts.memLevel || exports.Z_DEFAULT_MEMLEVEL, + opts.memLevel || constants.Z_DEFAULT_MEMLEVEL, strategy, opts.dictionary); @@ -402,21 +388,21 @@ function Zlib(opts, mode) { util.inherits(Zlib, Transform); Zlib.prototype.params = function(level, strategy, callback) { - if (level < exports.Z_MIN_LEVEL || - level > exports.Z_MAX_LEVEL) { + if (level < constants.Z_MIN_LEVEL || + level > constants.Z_MAX_LEVEL) { throw new RangeError('Invalid compression level: ' + level); } - if (strategy != exports.Z_FILTERED && - strategy != exports.Z_HUFFMAN_ONLY && - strategy != exports.Z_RLE && - strategy != exports.Z_FIXED && - strategy != exports.Z_DEFAULT_STRATEGY) { + if (strategy != constants.Z_FILTERED && + strategy != constants.Z_HUFFMAN_ONLY && + strategy != constants.Z_RLE && + strategy != constants.Z_FIXED && + strategy != constants.Z_DEFAULT_STRATEGY) { throw new TypeError('Invalid strategy: ' + strategy); } if (this._level !== level || this._strategy !== strategy) { var self = this; - this.flush(binding.Z_SYNC_FLUSH, function() { + this.flush(constants.Z_SYNC_FLUSH, function() { assert(self._handle, 'zlib binding closed'); self._handle.params(level, strategy); if (!self._hadError) { @@ -446,7 +432,7 @@ Zlib.prototype.flush = function(kind, callback) { if (typeof kind === 'function' || (kind === undefined && !callback)) { callback = kind; - kind = binding.Z_FULL_FLUSH; + kind = constants.Z_FULL_FLUSH; } if (ws.ended) { @@ -510,7 +496,7 @@ Zlib.prototype._transform = function(chunk, encoding, cb) { // once we've flushed the last of the queue, stop flushing and // go back to the normal behavior. if (chunk.length >= ws.length) { - this._flushFlag = this._opts.flush || binding.Z_NO_FLUSH; + this._flushFlag = this._opts.flush || constants.Z_NO_FLUSH; } } |