Browse Source

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>
v7.x
James M Snell 9 years ago
parent
commit
197a465280
  1. 96
      doc/api/zlib.md
  2. 142
      lib/zlib.js
  3. 84
      src/node_constants.cc
  4. 38
      src/node_zlib.cc
  5. 6
      test/parallel/test-zlib-const.js
  6. 4
      test/parallel/test-zlib-flush-flags.js
  7. 2
      test/parallel/test-zlib-flush.js
  8. 4
      test/parallel/test-zlib-params.js
  9. 4
      test/parallel/test-zlib-sync-no-event.js
  10. 2
      test/parallel/test-zlib-truncated.js
  11. 2
      test/parallel/test-zlib-write-after-flush.js

96
doc/api/zlib.md

@ -123,7 +123,9 @@ method that is used to compressed the last chunk of input data:
// This is a truncated version of the buffer from the above examples // This is a truncated version of the buffer from the above examples
const buffer = Buffer.from('eJzT0yMA', 'base64'); const buffer = Buffer.from('eJzT0yMA', 'base64');
zlib.unzip(buffer, { finishFlush: zlib.Z_SYNC_FLUSH }, (err, buffer) => { zlib.unzip(buffer,
{finishFlush: zlib.constants.Z_SYNC_FLUSH},
(err, buffer) => {
if (!err) { if (!err) {
console.log(buffer.toString()); console.log(buffer.toString());
} else { } else {
@ -221,65 +223,56 @@ added: v0.5.8
<!--type=misc--> <!--type=misc-->
All of the constants defined in `zlib.h` are also defined on `require('zlib')`. All of the constants defined in `zlib.h` are also defined on
In the normal course of operations, it will not be necessary to use these `require('zlib').constants`. In the normal course of operations, it will not be
constants. They are documented so that their presence is not surprising. This necessary to use these constants. They are documented so that their presence is
section is taken almost directly from the [zlib documentation][]. See not surprising. This section is taken almost directly from the
<http://zlib.net/manual.html#Constants> for more details. [zlib documentation][]. See <http://zlib.net/manual.html#Constants> for more
details.
*Note*: Previously, the constants were available directly from
`require('zlib')`, for instance `zlib.Z_NO_FLUSH`. Accessing the constants
directly from the module is currently still possible but should be considered
deprecated.
Allowed flush values. Allowed flush values.
* `zlib.Z_NO_FLUSH` * `zlib.constants.Z_NO_FLUSH`
* `zlib.Z_PARTIAL_FLUSH` * `zlib.constants.Z_PARTIAL_FLUSH`
* `zlib.Z_SYNC_FLUSH` * `zlib.constants.Z_SYNC_FLUSH`
* `zlib.Z_FULL_FLUSH` * `zlib.constants.Z_FULL_FLUSH`
* `zlib.Z_FINISH` * `zlib.constants.Z_FINISH`
* `zlib.Z_BLOCK` * `zlib.constants.Z_BLOCK`
* `zlib.Z_TREES` * `zlib.constants.Z_TREES`
Return codes for the compression/decompression functions. Negative Return codes for the compression/decompression functions. Negative
values are errors, positive values are used for special but normal values are errors, positive values are used for special but normal
events. events.
* `zlib.Z_OK` * `zlib.constants.Z_OK`
* `zlib.Z_STREAM_END` * `zlib.constants.Z_STREAM_END`
* `zlib.Z_NEED_DICT` * `zlib.constants.Z_NEED_DICT`
* `zlib.Z_ERRNO` * `zlib.constants.Z_ERRNO`
* `zlib.Z_STREAM_ERROR` * `zlib.constants.Z_STREAM_ERROR`
* `zlib.Z_DATA_ERROR` * `zlib.constants.Z_DATA_ERROR`
* `zlib.Z_MEM_ERROR` * `zlib.constants.Z_MEM_ERROR`
* `zlib.Z_BUF_ERROR` * `zlib.constants.Z_BUF_ERROR`
* `zlib.Z_VERSION_ERROR` * `zlib.constants.Z_VERSION_ERROR`
Compression levels. Compression levels.
* `zlib.Z_NO_COMPRESSION` * `zlib.constants.Z_NO_COMPRESSION`
* `zlib.Z_BEST_SPEED` * `zlib.constants.Z_BEST_SPEED`
* `zlib.Z_BEST_COMPRESSION` * `zlib.constants.Z_BEST_COMPRESSION`
* `zlib.Z_DEFAULT_COMPRESSION` * `zlib.constants.Z_DEFAULT_COMPRESSION`
Compression strategy. Compression strategy.
* `zlib.Z_FILTERED` * `zlib.constants.Z_FILTERED`
* `zlib.Z_HUFFMAN_ONLY` * `zlib.constants.Z_HUFFMAN_ONLY`
* `zlib.Z_RLE` * `zlib.constants.Z_RLE`
* `zlib.Z_FIXED` * `zlib.constants.Z_FIXED`
* `zlib.Z_DEFAULT_STRATEGY` * `zlib.constants.Z_DEFAULT_STRATEGY`
Possible values of the data_type field.
* `zlib.Z_BINARY`
* `zlib.Z_TEXT`
* `zlib.Z_ASCII`
* `zlib.Z_UNKNOWN`
The deflate compression method (the only one supported in this version).
* `zlib.Z_DEFLATED`
For initializing zalloc, zfree, opaque.
* `zlib.Z_NULL`
## Class Options ## Class Options
<!-- YAML <!-- YAML
@ -293,8 +286,8 @@ Each class takes an `options` object. All options are optional.
Note that some options are only relevant when compressing, and are Note that some options are only relevant when compressing, and are
ignored by the decompression classes. ignored by the decompression classes.
* `flush` (default: `zlib.Z_NO_FLUSH`) * `flush` (default: `zlib.constants.Z_NO_FLUSH`)
* `finishFlush` (default: `zlib.Z_FINISH`) * `finishFlush` (default: `zlib.constants.Z_FINISH`)
* `chunkSize` (default: 16*1024) * `chunkSize` (default: 16*1024)
* `windowBits` * `windowBits`
* `level` (compression only) * `level` (compression only)
@ -368,7 +361,7 @@ class of the compressor/decompressor classes.
added: v0.5.8 added: v0.5.8
--> -->
`kind` defaults to `zlib.Z_FULL_FLUSH`. `kind` defaults to `zlib.constants.Z_FULL_FLUSH`.
Flush pending data. Don't call this frivolously, premature flushes negatively Flush pending data. Don't call this frivolously, premature flushes negatively
impact the effectiveness of the compression algorithm. impact the effectiveness of the compression algorithm.
@ -391,6 +384,10 @@ Only applicable to deflate algorithm.
added: v0.7.0 added: v0.7.0
--> -->
## zlib.constants
Provides an object enumerating Zlib-related [constants][].
Reset the compressor/decompressor to factory defaults. Only applicable to Reset the compressor/decompressor to factory defaults. Only applicable to
the inflate and deflate algorithms. the inflate and deflate algorithms.
@ -545,3 +542,4 @@ Decompress a Buffer or string with Unzip.
[Unzip]: #zlib_class_zlib_unzip [Unzip]: #zlib_class_zlib_unzip
[`.flush()`]: #zlib_zlib_flush_kind_callback [`.flush()`]: #zlib_zlib_flush_kind_callback
[Buffer]: buffer.html [Buffer]: buffer.html
[constants]: #constants_constants

142
lib/zlib.js

@ -9,49 +9,35 @@ const kMaxLength = require('buffer').kMaxLength;
const kRangeErrorMessage = 'Cannot create final Buffer. It would be larger ' + const kRangeErrorMessage = 'Cannot create final Buffer. It would be larger ' +
'than 0x' + kMaxLength.toString(16) + ' bytes'; 'than 0x' + kMaxLength.toString(16) + ' bytes';
// zlib doesn't provide these, so kludge them in following the same const constants = process.binding('constants').zlib;
// 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;
// These should be considered deprecated
// expose all the zlib constants // expose all the zlib constants
const bkeys = Object.keys(binding); const bkeys = Object.keys(constants);
for (var bk = 0; bk < bkeys.length; bk++) { for (var bk = 0; bk < bkeys.length; bk++) {
var bkey = bkeys[bk]; var bkey = bkeys[bk];
if (bkey.match(/^Z/)) { Object.defineProperty(exports, bkey, {
Object.defineProperty(exports, bkey, { enumerable: true, value: constants[bkey], writable: false
enumerable: true, value: binding[bkey], writable: false });
});
}
} }
Object.defineProperty(exports, 'constants', {
configurable: false,
enumerable: true,
value: constants
});
// translation table for return codes. // translation table for return codes.
const codes = { const codes = {
Z_OK: binding.Z_OK, Z_OK: constants.Z_OK,
Z_STREAM_END: binding.Z_STREAM_END, Z_STREAM_END: constants.Z_STREAM_END,
Z_NEED_DICT: binding.Z_NEED_DICT, Z_NEED_DICT: constants.Z_NEED_DICT,
Z_ERRNO: binding.Z_ERRNO, Z_ERRNO: constants.Z_ERRNO,
Z_STREAM_ERROR: binding.Z_STREAM_ERROR, Z_STREAM_ERROR: constants.Z_STREAM_ERROR,
Z_DATA_ERROR: binding.Z_DATA_ERROR, Z_DATA_ERROR: constants.Z_DATA_ERROR,
Z_MEM_ERROR: binding.Z_MEM_ERROR, Z_MEM_ERROR: constants.Z_MEM_ERROR,
Z_BUF_ERROR: binding.Z_BUF_ERROR, Z_BUF_ERROR: constants.Z_BUF_ERROR,
Z_VERSION_ERROR: binding.Z_VERSION_ERROR Z_VERSION_ERROR: constants.Z_VERSION_ERROR
}; };
const ckeys = Object.keys(codes); const ckeys = Object.keys(codes);
@ -243,52 +229,52 @@ function zlibBufferSync(engine, buffer) {
// minimal 2-byte header // minimal 2-byte header
function Deflate(opts) { function Deflate(opts) {
if (!(this instanceof Deflate)) return new 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) { function Inflate(opts) {
if (!(this instanceof Inflate)) return new 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 // gzip - bigger header, same deflate compression
function Gzip(opts) { function Gzip(opts) {
if (!(this instanceof Gzip)) return new 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) { function Gunzip(opts) {
if (!(this instanceof Gunzip)) return new 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 // raw - no header
function DeflateRaw(opts) { function DeflateRaw(opts) {
if (!(this instanceof DeflateRaw)) return new 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) { function InflateRaw(opts) {
if (!(this instanceof InflateRaw)) return new 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. // auto-detect header.
function Unzip(opts) { function Unzip(opts) {
if (!(this instanceof Unzip)) return new 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) { function isValidFlushFlag(flag) {
return flag === binding.Z_NO_FLUSH || return flag === constants.Z_NO_FLUSH ||
flag === binding.Z_PARTIAL_FLUSH || flag === constants.Z_PARTIAL_FLUSH ||
flag === binding.Z_SYNC_FLUSH || flag === constants.Z_SYNC_FLUSH ||
flag === binding.Z_FULL_FLUSH || flag === constants.Z_FULL_FLUSH ||
flag === binding.Z_FINISH || flag === constants.Z_FINISH ||
flag === binding.Z_BLOCK; flag === constants.Z_BLOCK;
} }
// the Zlib class they all inherit from // the Zlib class they all inherit from
@ -298,7 +284,7 @@ function isValidFlushFlag(flag) {
function Zlib(opts, mode) { function Zlib(opts, mode) {
this._opts = opts = opts || {}; this._opts = opts = opts || {};
this._chunkSize = opts.chunkSize || exports.Z_DEFAULT_CHUNK; this._chunkSize = opts.chunkSize || constants.Z_DEFAULT_CHUNK;
Transform.call(this, opts); Transform.call(this, opts);
@ -309,44 +295,44 @@ function Zlib(opts, mode) {
throw new Error('Invalid flush flag: ' + opts.finishFlush); 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' ? this._finishFlushFlag = typeof opts.finishFlush !== 'undefined' ?
opts.finishFlush : binding.Z_FINISH; opts.finishFlush : constants.Z_FINISH;
if (opts.chunkSize) { if (opts.chunkSize) {
if (opts.chunkSize < exports.Z_MIN_CHUNK || if (opts.chunkSize < constants.Z_MIN_CHUNK ||
opts.chunkSize > exports.Z_MAX_CHUNK) { opts.chunkSize > constants.Z_MAX_CHUNK) {
throw new Error('Invalid chunk size: ' + opts.chunkSize); throw new Error('Invalid chunk size: ' + opts.chunkSize);
} }
} }
if (opts.windowBits) { if (opts.windowBits) {
if (opts.windowBits < exports.Z_MIN_WINDOWBITS || if (opts.windowBits < constants.Z_MIN_WINDOWBITS ||
opts.windowBits > exports.Z_MAX_WINDOWBITS) { opts.windowBits > constants.Z_MAX_WINDOWBITS) {
throw new Error('Invalid windowBits: ' + opts.windowBits); throw new Error('Invalid windowBits: ' + opts.windowBits);
} }
} }
if (opts.level) { if (opts.level) {
if (opts.level < exports.Z_MIN_LEVEL || if (opts.level < constants.Z_MIN_LEVEL ||
opts.level > exports.Z_MAX_LEVEL) { opts.level > constants.Z_MAX_LEVEL) {
throw new Error('Invalid compression level: ' + opts.level); throw new Error('Invalid compression level: ' + opts.level);
} }
} }
if (opts.memLevel) { if (opts.memLevel) {
if (opts.memLevel < exports.Z_MIN_MEMLEVEL || if (opts.memLevel < constants.Z_MIN_MEMLEVEL ||
opts.memLevel > exports.Z_MAX_MEMLEVEL) { opts.memLevel > constants.Z_MAX_MEMLEVEL) {
throw new Error('Invalid memLevel: ' + opts.memLevel); throw new Error('Invalid memLevel: ' + opts.memLevel);
} }
} }
if (opts.strategy) { if (opts.strategy) {
if (opts.strategy != exports.Z_FILTERED && if (opts.strategy != constants.Z_FILTERED &&
opts.strategy != exports.Z_HUFFMAN_ONLY && opts.strategy != constants.Z_HUFFMAN_ONLY &&
opts.strategy != exports.Z_RLE && opts.strategy != constants.Z_RLE &&
opts.strategy != exports.Z_FIXED && opts.strategy != constants.Z_FIXED &&
opts.strategy != exports.Z_DEFAULT_STRATEGY) { opts.strategy != constants.Z_DEFAULT_STRATEGY) {
throw new Error('Invalid strategy: ' + opts.strategy); throw new Error('Invalid strategy: ' + opts.strategy);
} }
} }
@ -373,15 +359,15 @@ function Zlib(opts, mode) {
self.emit('error', error); self.emit('error', error);
}; };
var level = exports.Z_DEFAULT_COMPRESSION; var level = constants.Z_DEFAULT_COMPRESSION;
if (typeof opts.level === 'number') level = opts.level; 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; 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, level,
opts.memLevel || exports.Z_DEFAULT_MEMLEVEL, opts.memLevel || constants.Z_DEFAULT_MEMLEVEL,
strategy, strategy,
opts.dictionary); opts.dictionary);
@ -402,21 +388,21 @@ function Zlib(opts, mode) {
util.inherits(Zlib, Transform); util.inherits(Zlib, Transform);
Zlib.prototype.params = function(level, strategy, callback) { Zlib.prototype.params = function(level, strategy, callback) {
if (level < exports.Z_MIN_LEVEL || if (level < constants.Z_MIN_LEVEL ||
level > exports.Z_MAX_LEVEL) { level > constants.Z_MAX_LEVEL) {
throw new RangeError('Invalid compression level: ' + level); throw new RangeError('Invalid compression level: ' + level);
} }
if (strategy != exports.Z_FILTERED && if (strategy != constants.Z_FILTERED &&
strategy != exports.Z_HUFFMAN_ONLY && strategy != constants.Z_HUFFMAN_ONLY &&
strategy != exports.Z_RLE && strategy != constants.Z_RLE &&
strategy != exports.Z_FIXED && strategy != constants.Z_FIXED &&
strategy != exports.Z_DEFAULT_STRATEGY) { strategy != constants.Z_DEFAULT_STRATEGY) {
throw new TypeError('Invalid strategy: ' + strategy); throw new TypeError('Invalid strategy: ' + strategy);
} }
if (this._level !== level || this._strategy !== strategy) { if (this._level !== level || this._strategy !== strategy) {
var self = this; var self = this;
this.flush(binding.Z_SYNC_FLUSH, function() { this.flush(constants.Z_SYNC_FLUSH, function() {
assert(self._handle, 'zlib binding closed'); assert(self._handle, 'zlib binding closed');
self._handle.params(level, strategy); self._handle.params(level, strategy);
if (!self._hadError) { if (!self._hadError) {
@ -446,7 +432,7 @@ Zlib.prototype.flush = function(kind, callback) {
if (typeof kind === 'function' || (kind === undefined && !callback)) { if (typeof kind === 'function' || (kind === undefined && !callback)) {
callback = kind; callback = kind;
kind = binding.Z_FULL_FLUSH; kind = constants.Z_FULL_FLUSH;
} }
if (ws.ended) { 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 // once we've flushed the last of the queue, stop flushing and
// go back to the normal behavior. // go back to the normal behavior.
if (chunk.length >= ws.length) { if (chunk.length >= ws.length) {
this._flushFlag = this._opts.flush || binding.Z_NO_FLUSH; this._flushFlag = this._opts.flush || constants.Z_NO_FLUSH;
} }
} }

84
src/node_constants.cc

@ -3,6 +3,7 @@
#include "env-inl.h" #include "env-inl.h"
#include "uv.h" #include "uv.h"
#include "zlib.h"
#include <errno.h> #include <errno.h>
#if !defined(_MSC_VER) #if !defined(_MSC_VER)
@ -12,6 +13,7 @@
#include <signal.h> #include <signal.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <limits>
#if HAVE_OPENSSL #if HAVE_OPENSSL
# include <openssl/ec.h> # include <openssl/ec.h>
@ -1142,12 +1144,92 @@ void DefineCryptoConstants(Local<Object> target) {
#endif #endif
} }
void DefineZlibConstants(Local<Object> target) {
NODE_DEFINE_CONSTANT(target, Z_NO_FLUSH);
NODE_DEFINE_CONSTANT(target, Z_PARTIAL_FLUSH);
NODE_DEFINE_CONSTANT(target, Z_SYNC_FLUSH);
NODE_DEFINE_CONSTANT(target, Z_FULL_FLUSH);
NODE_DEFINE_CONSTANT(target, Z_FINISH);
NODE_DEFINE_CONSTANT(target, Z_BLOCK);
// return/error codes
NODE_DEFINE_CONSTANT(target, Z_OK);
NODE_DEFINE_CONSTANT(target, Z_STREAM_END);
NODE_DEFINE_CONSTANT(target, Z_NEED_DICT);
NODE_DEFINE_CONSTANT(target, Z_ERRNO);
NODE_DEFINE_CONSTANT(target, Z_STREAM_ERROR);
NODE_DEFINE_CONSTANT(target, Z_DATA_ERROR);
NODE_DEFINE_CONSTANT(target, Z_MEM_ERROR);
NODE_DEFINE_CONSTANT(target, Z_BUF_ERROR);
NODE_DEFINE_CONSTANT(target, Z_VERSION_ERROR);
NODE_DEFINE_CONSTANT(target, Z_NO_COMPRESSION);
NODE_DEFINE_CONSTANT(target, Z_BEST_SPEED);
NODE_DEFINE_CONSTANT(target, Z_BEST_COMPRESSION);
NODE_DEFINE_CONSTANT(target, Z_DEFAULT_COMPRESSION);
NODE_DEFINE_CONSTANT(target, Z_FILTERED);
NODE_DEFINE_CONSTANT(target, Z_HUFFMAN_ONLY);
NODE_DEFINE_CONSTANT(target, Z_RLE);
NODE_DEFINE_CONSTANT(target, Z_FIXED);
NODE_DEFINE_CONSTANT(target, Z_DEFAULT_STRATEGY);
NODE_DEFINE_CONSTANT(target, ZLIB_VERNUM);
enum node_zlib_mode {
NONE,
DEFLATE,
INFLATE,
GZIP,
GUNZIP,
DEFLATERAW,
INFLATERAW,
UNZIP
};
NODE_DEFINE_CONSTANT(target, DEFLATE);
NODE_DEFINE_CONSTANT(target, INFLATE);
NODE_DEFINE_CONSTANT(target, GZIP);
NODE_DEFINE_CONSTANT(target, GUNZIP);
NODE_DEFINE_CONSTANT(target, DEFLATERAW);
NODE_DEFINE_CONSTANT(target, INFLATERAW);
NODE_DEFINE_CONSTANT(target, UNZIP);
#define Z_MIN_WINDOWBITS 8
#define Z_MAX_WINDOWBITS 15
#define Z_DEFAULT_WINDOWBITS 15
// Fewer than 64 bytes per chunk is not recommended.
// Technically it could work with as few as 8, but even 64 bytes
// is low. Usually a MB or more is best.
#define Z_MIN_CHUNK 64
#define Z_MAX_CHUNK std::numeric_limits<double>::infinity()
#define Z_DEFAULT_CHUNK (16 * 1024)
#define Z_MIN_MEMLEVEL 1
#define Z_MAX_MEMLEVEL 9
#define Z_DEFAULT_MEMLEVEL 8
#define Z_MIN_LEVEL -1
#define Z_MAX_LEVEL 9
#define Z_DEFAULT_LEVEL Z_DEFAULT_COMPRESSION
NODE_DEFINE_CONSTANT(target, Z_MIN_WINDOWBITS);
NODE_DEFINE_CONSTANT(target, Z_MAX_WINDOWBITS);
NODE_DEFINE_CONSTANT(target, Z_DEFAULT_WINDOWBITS);
NODE_DEFINE_CONSTANT(target, Z_MIN_CHUNK);
NODE_DEFINE_CONSTANT(target, Z_MAX_CHUNK);
NODE_DEFINE_CONSTANT(target, Z_DEFAULT_CHUNK);
NODE_DEFINE_CONSTANT(target, Z_MIN_MEMLEVEL);
NODE_DEFINE_CONSTANT(target, Z_MAX_MEMLEVEL);
NODE_DEFINE_CONSTANT(target, Z_DEFAULT_MEMLEVEL);
NODE_DEFINE_CONSTANT(target, Z_MIN_LEVEL);
NODE_DEFINE_CONSTANT(target, Z_MAX_LEVEL);
NODE_DEFINE_CONSTANT(target, Z_DEFAULT_LEVEL);
}
void DefineConstants(v8::Isolate* isolate, Local<Object> target) { void DefineConstants(v8::Isolate* isolate, Local<Object> target) {
Local<Object> os_constants = Object::New(isolate); Local<Object> os_constants = Object::New(isolate);
Local<Object> err_constants = Object::New(isolate); Local<Object> err_constants = Object::New(isolate);
Local<Object> sig_constants = Object::New(isolate); Local<Object> sig_constants = Object::New(isolate);
Local<Object> fs_constants = Object::New(isolate); Local<Object> fs_constants = Object::New(isolate);
Local<Object> crypto_constants = Object::New(isolate); Local<Object> crypto_constants = Object::New(isolate);
Local<Object> zlib_constants = Object::New(isolate);
DefineErrnoConstants(err_constants); DefineErrnoConstants(err_constants);
DefineWindowsErrorConstants(err_constants); DefineWindowsErrorConstants(err_constants);
@ -1156,12 +1238,14 @@ void DefineConstants(v8::Isolate* isolate, Local<Object> target) {
DefineSystemConstants(fs_constants); DefineSystemConstants(fs_constants);
DefineOpenSSLConstants(crypto_constants); DefineOpenSSLConstants(crypto_constants);
DefineCryptoConstants(crypto_constants); DefineCryptoConstants(crypto_constants);
DefineZlibConstants(zlib_constants);
os_constants->Set(OneByteString(isolate, "errno"), err_constants); os_constants->Set(OneByteString(isolate, "errno"), err_constants);
os_constants->Set(OneByteString(isolate, "signals"), sig_constants); os_constants->Set(OneByteString(isolate, "signals"), sig_constants);
target->Set(OneByteString(isolate, "os"), os_constants); target->Set(OneByteString(isolate, "os"), os_constants);
target->Set(OneByteString(isolate, "fs"), fs_constants); target->Set(OneByteString(isolate, "fs"), fs_constants);
target->Set(OneByteString(isolate, "crypto"), crypto_constants); target->Set(OneByteString(isolate, "crypto"), crypto_constants);
target->Set(OneByteString(isolate, "zlib"), zlib_constants);
} }
} // namespace node } // namespace node

38
src/node_zlib.cc

@ -666,44 +666,6 @@ void InitZlib(Local<Object> target,
z->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Zlib")); z->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Zlib"));
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Zlib"), z->GetFunction()); target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Zlib"), z->GetFunction());
// valid flush values.
NODE_DEFINE_CONSTANT(target, Z_NO_FLUSH);
NODE_DEFINE_CONSTANT(target, Z_PARTIAL_FLUSH);
NODE_DEFINE_CONSTANT(target, Z_SYNC_FLUSH);
NODE_DEFINE_CONSTANT(target, Z_FULL_FLUSH);
NODE_DEFINE_CONSTANT(target, Z_FINISH);
NODE_DEFINE_CONSTANT(target, Z_BLOCK);
// return/error codes
NODE_DEFINE_CONSTANT(target, Z_OK);
NODE_DEFINE_CONSTANT(target, Z_STREAM_END);
NODE_DEFINE_CONSTANT(target, Z_NEED_DICT);
NODE_DEFINE_CONSTANT(target, Z_ERRNO);
NODE_DEFINE_CONSTANT(target, Z_STREAM_ERROR);
NODE_DEFINE_CONSTANT(target, Z_DATA_ERROR);
NODE_DEFINE_CONSTANT(target, Z_MEM_ERROR);
NODE_DEFINE_CONSTANT(target, Z_BUF_ERROR);
NODE_DEFINE_CONSTANT(target, Z_VERSION_ERROR);
NODE_DEFINE_CONSTANT(target, Z_NO_COMPRESSION);
NODE_DEFINE_CONSTANT(target, Z_BEST_SPEED);
NODE_DEFINE_CONSTANT(target, Z_BEST_COMPRESSION);
NODE_DEFINE_CONSTANT(target, Z_DEFAULT_COMPRESSION);
NODE_DEFINE_CONSTANT(target, Z_FILTERED);
NODE_DEFINE_CONSTANT(target, Z_HUFFMAN_ONLY);
NODE_DEFINE_CONSTANT(target, Z_RLE);
NODE_DEFINE_CONSTANT(target, Z_FIXED);
NODE_DEFINE_CONSTANT(target, Z_DEFAULT_STRATEGY);
NODE_DEFINE_CONSTANT(target, ZLIB_VERNUM);
NODE_DEFINE_CONSTANT(target, DEFLATE);
NODE_DEFINE_CONSTANT(target, INFLATE);
NODE_DEFINE_CONSTANT(target, GZIP);
NODE_DEFINE_CONSTANT(target, GUNZIP);
NODE_DEFINE_CONSTANT(target, DEFLATERAW);
NODE_DEFINE_CONSTANT(target, INFLATERAW);
NODE_DEFINE_CONSTANT(target, UNZIP);
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "ZLIB_VERSION"), target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "ZLIB_VERSION"),
FIXED_ONE_BYTE_STRING(env->isolate(), ZLIB_VERSION)); FIXED_ONE_BYTE_STRING(env->isolate(), ZLIB_VERSION));
} }

6
test/parallel/test-zlib-const.js

@ -4,9 +4,9 @@ var assert = require('assert');
var zlib = require('zlib'); var zlib = require('zlib');
assert.equal(zlib.Z_OK, 0, 'Z_OK should be 0'); assert.equal(zlib.constants.Z_OK, 0, 'Z_OK should be 0');
zlib.Z_OK = 1; zlib.constants.Z_OK = 1;
assert.equal(zlib.Z_OK, 0, 'Z_OK should be 0'); assert.equal(zlib.constants.Z_OK, 0, 'Z_OK should be 0');
assert.equal(zlib.codes.Z_OK, 0, 'Z_OK should be 0'); assert.equal(zlib.codes.Z_OK, 0, 'Z_OK should be 0');
zlib.codes.Z_OK = 1; zlib.codes.Z_OK = 1;

4
test/parallel/test-zlib-flush-flags.js

@ -4,7 +4,7 @@ const assert = require('assert');
const zlib = require('zlib'); const zlib = require('zlib');
assert.doesNotThrow(() => { assert.doesNotThrow(() => {
zlib.createGzip({ flush: zlib.Z_SYNC_FLUSH }); zlib.createGzip({ flush: zlib.constants.Z_SYNC_FLUSH });
}); });
assert.throws(() => { assert.throws(() => {
@ -16,7 +16,7 @@ assert.throws(() => {
}, /Invalid flush flag: 10000/); }, /Invalid flush flag: 10000/);
assert.doesNotThrow(() => { assert.doesNotThrow(() => {
zlib.createGzip({ finishFlush: zlib.Z_SYNC_FLUSH }); zlib.createGzip({ finishFlush: zlib.constants.Z_SYNC_FLUSH });
}); });
assert.throws(() => { assert.throws(() => {

2
test/parallel/test-zlib-flush.js

@ -19,7 +19,7 @@ let actualNone;
let actualFull; let actualFull;
deflater.write(chunk, function() { deflater.write(chunk, function() {
deflater.flush(zlib.Z_NO_FLUSH, function() { deflater.flush(zlib.constants.Z_NO_FLUSH, function() {
actualNone = deflater.read(); actualNone = deflater.read();
deflater.flush(function() { deflater.flush(function() {
var bufs = [], buf; var bufs = [], buf;

4
test/parallel/test-zlib-params.js

@ -7,7 +7,7 @@ var fs = require('fs');
const file = fs.readFileSync(path.resolve(common.fixturesDir, 'person.jpg')); const file = fs.readFileSync(path.resolve(common.fixturesDir, 'person.jpg'));
const chunkSize = 12 * 1024; const chunkSize = 12 * 1024;
const opts = { level: 9, strategy: zlib.Z_DEFAULT_STRATEGY }; const opts = { level: 9, strategy: zlib.constants.Z_DEFAULT_STRATEGY };
const deflater = zlib.createDeflate(opts); const deflater = zlib.createDeflate(opts);
const chunk1 = file.slice(0, chunkSize); const chunk1 = file.slice(0, chunkSize);
@ -17,7 +17,7 @@ const expected = Buffer.concat([blkhdr, chunk2]);
let actual; let actual;
deflater.write(chunk1, function() { deflater.write(chunk1, function() {
deflater.params(0, zlib.Z_DEFAULT_STRATEGY, function() { deflater.params(0, zlib.constants.Z_DEFAULT_STRATEGY, function() {
while (deflater.read()); while (deflater.read());
deflater.end(chunk2, function() { deflater.end(chunk2, function() {
var bufs = [], buf; var bufs = [], buf;

4
test/parallel/test-zlib-sync-no-event.js

@ -11,11 +11,11 @@ const zipper = new zlib.Gzip();
zipper.on('close', shouldNotBeCalled); zipper.on('close', shouldNotBeCalled);
const buffer = new Buffer(message); const buffer = new Buffer(message);
const zipped = zipper._processChunk(buffer, zlib.Z_FINISH); const zipped = zipper._processChunk(buffer, zlib.constants.Z_FINISH);
const unzipper = new zlib.Gunzip(); const unzipper = new zlib.Gunzip();
unzipper.on('close', shouldNotBeCalled); unzipper.on('close', shouldNotBeCalled);
const unzipped = unzipper._processChunk(zipped, zlib.Z_FINISH); const unzipped = unzipper._processChunk(zipped, zlib.constants.Z_FINISH);
assert.notEqual(zipped.toString(), message); assert.notEqual(zipped.toString(), message);
assert.strictEqual(unzipped.toString(), message); assert.strictEqual(unzipped.toString(), message);

2
test/parallel/test-zlib-truncated.js

@ -47,7 +47,7 @@ const inputString = 'ΩΩLorem ipsum dolor sit amet, consectetur adipiscing eli'
assert(/unexpected end of file/.test(err.message)); assert(/unexpected end of file/.test(err.message));
}); });
const syncFlushOpt = { finishFlush: zlib.Z_SYNC_FLUSH }; const syncFlushOpt = { finishFlush: zlib.constants.Z_SYNC_FLUSH };
// sync truncated input test, finishFlush = Z_SYNC_FLUSH // sync truncated input test, finishFlush = Z_SYNC_FLUSH
assert.doesNotThrow(function() { assert.doesNotThrow(function() {

2
test/parallel/test-zlib-write-after-flush.js

@ -19,7 +19,7 @@ process.on('exit', function() {
assert.equal(output, input); assert.equal(output, input);
// Make sure that the flush flag was set back to normal // Make sure that the flush flag was set back to normal
assert.equal(gzip._flushFlag, zlib.Z_NO_FLUSH); assert.equal(gzip._flushFlag, zlib.constants.Z_NO_FLUSH);
console.log('ok'); console.log('ok');
}); });

Loading…
Cancel
Save