@ -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 || expor ts. Z_DEFAULT_CHUNK ;
this . _ chunkSize = opts . chunkSize || constan ts. 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 < expor ts. Z_MIN_CHUNK ||
if ( opts . chunkSize < constan ts. Z_MIN_CHUNK ||
opts . chunkSize > expor ts. Z_MAX_CHUNK ) {
opts . chunkSize > constan ts. 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 < expor ts. Z_MIN_WINDOWBITS ||
if ( opts . windowBits < constan ts. Z_MIN_WINDOWBITS ||
opts . windowBits > expor ts. Z_MAX_WINDOWBITS ) {
opts . windowBits > constan ts. 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 < expor ts. Z_MIN_LEVEL ||
if ( opts . level < constan ts. Z_MIN_LEVEL ||
opts . level > expor ts. Z_MAX_LEVEL ) {
opts . level > constan ts. 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 < expor ts. Z_MIN_MEMLEVEL ||
if ( opts . memLevel < constan ts. Z_MIN_MEMLEVEL ||
opts . memLevel > expor ts. Z_MAX_MEMLEVEL ) {
opts . memLevel > constan ts. 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 != expor ts. Z_FILTERED &&
if ( opts . strategy != constan ts. Z_FILTERED &&
opts . strategy != expor ts. Z_HUFFMAN_ONLY &&
opts . strategy != constan ts. Z_HUFFMAN_ONLY &&
opts . strategy != expor ts. Z_RLE &&
opts . strategy != constan ts. Z_RLE &&
opts . strategy != expor ts. Z_FIXED &&
opts . strategy != constan ts. Z_FIXED &&
opts . strategy != expor ts. Z_DEFAULT_STRATEGY ) {
opts . strategy != constan ts. 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 = expor ts. Z_DEFAULT_COMPRESSION ;
var level = constan ts. Z_DEFAULT_COMPRESSION ;
if ( typeof opts . level === 'number' ) level = opts . level ;
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 ;
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 ,
level ,
opts . memLevel || expor ts. Z_DEFAULT_MEMLEVEL ,
opts . memLevel || constan ts. 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 < expor ts. Z_MIN_LEVEL ||
if ( level < constan ts. Z_MIN_LEVEL ||
level > expor ts. Z_MAX_LEVEL ) {
level > constan ts. Z_MAX_LEVEL ) {
throw new RangeError ( 'Invalid compression level: ' + level ) ;
throw new RangeError ( 'Invalid compression level: ' + level ) ;
}
}
if ( strategy != expor ts. Z_FILTERED &&
if ( strategy != constan ts. Z_FILTERED &&
strategy != expor ts. Z_HUFFMAN_ONLY &&
strategy != constan ts. Z_HUFFMAN_ONLY &&
strategy != expor ts. Z_RLE &&
strategy != constan ts. Z_RLE &&
strategy != expor ts. Z_FIXED &&
strategy != constan ts. Z_FIXED &&
strategy != expor ts. Z_DEFAULT_STRATEGY ) {
strategy != constan ts. 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 ;
}
}
}
}