@ -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 || expor ts. Z_DEFAULT_CHUNK ;
this . _ chunkSize = opts . chunkSize || constan ts. 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 < expor ts. Z_MIN_CHUNK ||
opts . chunkSize > expor ts. Z_MAX_CHUNK ) {
if ( opts . chunkSize < constan ts. Z_MIN_CHUNK ||
opts . chunkSize > constan ts. Z_MAX_CHUNK ) {
throw new Error ( 'Invalid chunk size: ' + opts . chunkSize ) ;
}
}
if ( opts . windowBits ) {
if ( opts . windowBits < expor ts. Z_MIN_WINDOWBITS ||
opts . windowBits > expor ts. Z_MAX_WINDOWBITS ) {
if ( opts . windowBits < constan ts. Z_MIN_WINDOWBITS ||
opts . windowBits > constan ts. Z_MAX_WINDOWBITS ) {
throw new Error ( 'Invalid windowBits: ' + opts . windowBits ) ;
}
}
if ( opts . level ) {
if ( opts . level < expor ts. Z_MIN_LEVEL ||
opts . level > expor ts. Z_MAX_LEVEL ) {
if ( opts . level < constan ts. Z_MIN_LEVEL ||
opts . level > constan ts. Z_MAX_LEVEL ) {
throw new Error ( 'Invalid compression level: ' + opts . level ) ;
}
}
if ( opts . memLevel ) {
if ( opts . memLevel < expor ts. Z_MIN_MEMLEVEL ||
opts . memLevel > expor ts. Z_MAX_MEMLEVEL ) {
if ( opts . memLevel < constan ts. Z_MIN_MEMLEVEL ||
opts . memLevel > constan ts. Z_MAX_MEMLEVEL ) {
throw new Error ( 'Invalid memLevel: ' + opts . memLevel ) ;
}
}
if ( opts . strategy ) {
if ( opts . strategy != expor ts. Z_FILTERED &&
opts . strategy != expor ts. Z_HUFFMAN_ONLY &&
opts . strategy != expor ts. Z_RLE &&
opts . strategy != expor ts. Z_FIXED &&
opts . strategy != expor ts. Z_DEFAULT_STRATEGY ) {
if ( opts . strategy != constan ts. Z_FILTERED &&
opts . strategy != constan ts. Z_HUFFMAN_ONLY &&
opts . strategy != constan ts. Z_RLE &&
opts . strategy != constan ts. Z_FIXED &&
opts . strategy != constan ts. Z_DEFAULT_STRATEGY ) {
throw new Error ( 'Invalid strategy: ' + opts . strategy ) ;
}
}
@ -373,15 +359,15 @@ function Zlib(opts, mode) {
self . emit ( 'error' , error ) ;
} ;
var level = expor ts. Z_DEFAULT_COMPRESSION ;
var level = constan ts. Z_DEFAULT_COMPRESSION ;
if ( typeof opts . level === 'number' ) level = opts . level ;
var strategy = expor ts. Z_DEFAULT_STRATEGY ;
var strategy = constan ts. Z_DEFAULT_STRATEGY ;
if ( typeof opts . strategy === 'number' ) strategy = opts . strategy ;
this . _ handle . init ( opts . windowBits || expor ts. Z_DEFAULT_WINDOWBITS ,
this . _ handle . init ( opts . windowBits || constan ts. Z_DEFAULT_WINDOWBITS ,
level ,
opts . memLevel || expor ts. Z_DEFAULT_MEMLEVEL ,
opts . memLevel || constan ts. 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 < expor ts. Z_MIN_LEVEL ||
level > expor ts. Z_MAX_LEVEL ) {
if ( level < constan ts. Z_MIN_LEVEL ||
level > constan ts. Z_MAX_LEVEL ) {
throw new RangeError ( 'Invalid compression level: ' + level ) ;
}
if ( strategy != expor ts. Z_FILTERED &&
strategy != expor ts. Z_HUFFMAN_ONLY &&
strategy != expor ts. Z_RLE &&
strategy != expor ts. Z_FIXED &&
strategy != expor ts. Z_DEFAULT_STRATEGY ) {
if ( strategy != constan ts. Z_FILTERED &&
strategy != constan ts. Z_HUFFMAN_ONLY &&
strategy != constan ts. Z_RLE &&
strategy != constan ts. Z_FIXED &&
strategy != constan ts. 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 ;
}
}