node/lib/zlib.js

453 lines
12 KiB
JavaScript
Raw Normal View History

2011-09-06 23:13:05 +00:00
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2012-10-02 23:15:39 +00:00
var Transform = require('_stream_transform');
2011-09-06 23:13:05 +00:00
var binding = process.binding('zlib');
var util = require('util');
var assert = require('assert').ok;
2011-09-06 23:13:05 +00:00
// 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;
// expose all the zlib constants
Object.keys(binding).forEach(function(k) {
if (k.match(/^Z/)) exports[k] = binding[k];
});
// translation table for return codes.
exports.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
};
Object.keys(exports.codes).forEach(function(k) {
exports.codes[exports.codes[k]] = k;
});
2011-09-06 23:13:05 +00:00
exports.Deflate = Deflate;
exports.Inflate = Inflate;
exports.Gzip = Gzip;
exports.Gunzip = Gunzip;
exports.DeflateRaw = DeflateRaw;
exports.InflateRaw = InflateRaw;
exports.Unzip = Unzip;
2011-09-18 06:03:36 +00:00
exports.createDeflate = function(o) {
return new Deflate(o);
};
exports.createInflate = function(o) {
return new Inflate(o);
};
exports.createDeflateRaw = function(o) {
return new DeflateRaw(o);
};
exports.createInflateRaw = function(o) {
return new InflateRaw(o);
};
exports.createGzip = function(o) {
return new Gzip(o);
};
exports.createGunzip = function(o) {
return new Gunzip(o);
};
exports.createUnzip = function(o) {
return new Unzip(o);
};
2011-09-06 23:13:05 +00:00
// Convenience methods.
// compress/decompress a string or buffer in one step.
exports.deflate = function(buffer, callback) {
zlibBuffer(new Deflate(), buffer, callback);
};
exports.gzip = function(buffer, callback) {
zlibBuffer(new Gzip(), buffer, callback);
};
exports.deflateRaw = function(buffer, callback) {
zlibBuffer(new DeflateRaw(), buffer, callback);
};
exports.unzip = function(buffer, callback) {
zlibBuffer(new Unzip(), buffer, callback);
};
exports.inflate = function(buffer, callback) {
zlibBuffer(new Inflate(), buffer, callback);
};
exports.gunzip = function(buffer, callback) {
zlibBuffer(new Gunzip(), buffer, callback);
};
exports.inflateRaw = function(buffer, callback) {
zlibBuffer(new InflateRaw(), buffer, callback);
};
function zlibBuffer(engine, buffer, callback) {
var buffers = [];
var nread = 0;
2012-10-02 23:15:39 +00:00
engine.on('error', onError);
engine.on('end', onEnd);
engine.end(buffer);
flow();
function flow() {
var chunk;
while (null !== (chunk = engine.read())) {
buffers.push(chunk);
nread += chunk.length;
}
engine.once('readable', flow);
}
function onError(err) {
engine.removeListener('end', onEnd);
2012-10-02 23:15:39 +00:00
engine.removeListener('readable', flow);
callback(err);
}
function onEnd() {
var buf = Buffer.concat(buffers, nread);
buffers = [];
callback(null, buf);
}
}
2011-09-06 23:13:05 +00:00
// generic zlib
// minimal 2-byte header
function Deflate(opts) {
2011-09-26 18:50:20 +00:00
if (!(this instanceof Deflate)) return new Deflate(opts);
Zlib.call(this, opts, binding.DEFLATE);
2011-09-06 23:13:05 +00:00
}
function Inflate(opts) {
if (!(this instanceof Inflate)) return new Inflate(opts);
Zlib.call(this, opts, binding.INFLATE);
2011-09-06 23:13:05 +00:00
}
// gzip - bigger header, same deflate compression
function Gzip(opts) {
if (!(this instanceof Gzip)) return new Gzip(opts);
Zlib.call(this, opts, binding.GZIP);
2011-09-06 23:13:05 +00:00
}
function Gunzip(opts) {
if (!(this instanceof Gunzip)) return new Gunzip(opts);
Zlib.call(this, opts, binding.GUNZIP);
2011-09-06 23:13:05 +00:00
}
// raw - no header
function DeflateRaw(opts) {
if (!(this instanceof DeflateRaw)) return new DeflateRaw(opts);
Zlib.call(this, opts, binding.DEFLATERAW);
2011-09-06 23:13:05 +00:00
}
function InflateRaw(opts) {
if (!(this instanceof InflateRaw)) return new InflateRaw(opts);
Zlib.call(this, opts, binding.INFLATERAW);
2011-09-06 23:13:05 +00:00
}
// auto-detect header.
function Unzip(opts) {
if (!(this instanceof Unzip)) return new Unzip(opts);
Zlib.call(this, opts, binding.UNZIP);
2011-09-06 23:13:05 +00:00
}
// the Zlib class they all inherit from
// This thing manages the queue of requests, and returns
// true or false if there is anything in the queue when
// you call the .write() method.
function Zlib(opts, mode) {
2011-09-06 23:13:05 +00:00
this._opts = opts = opts || {};
2012-10-02 23:15:39 +00:00
this._chunkSize = opts.chunkSize || exports.Z_DEFAULT_CHUNK;
Transform.call(this, opts);
// means a different thing there.
this._readableState.chunkSize = null;
2011-09-06 23:13:05 +00:00
if (opts.chunkSize) {
if (opts.chunkSize < exports.Z_MIN_CHUNK ||
opts.chunkSize > exports.Z_MAX_CHUNK) {
2011-09-18 06:03:36 +00:00
throw new Error('Invalid chunk size: ' + opts.chunkSize);
2011-09-06 23:13:05 +00:00
}
}
if (opts.windowBits) {
if (opts.windowBits < exports.Z_MIN_WINDOWBITS ||
opts.windowBits > exports.Z_MAX_WINDOWBITS) {
2011-09-18 06:03:36 +00:00
throw new Error('Invalid windowBits: ' + opts.windowBits);
2011-09-06 23:13:05 +00:00
}
}
if (opts.level) {
if (opts.level < exports.Z_MIN_LEVEL ||
opts.level > exports.Z_MAX_LEVEL) {
2011-09-18 06:03:36 +00:00
throw new Error('Invalid compression level: ' + opts.level);
2011-09-06 23:13:05 +00:00
}
}
if (opts.memLevel) {
if (opts.memLevel < exports.Z_MIN_MEMLEVEL ||
opts.memLevel > exports.Z_MAX_MEMLEVEL) {
2011-09-18 06:03:36 +00:00
throw new Error('Invalid memLevel: ' + opts.memLevel);
2011-09-06 23:13:05 +00:00
}
}
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) {
2011-09-18 06:03:36 +00:00
throw new Error('Invalid strategy: ' + opts.strategy);
2011-09-06 23:13:05 +00:00
}
}
2011-12-02 07:53:56 +00:00
if (opts.dictionary) {
if (!Buffer.isBuffer(opts.dictionary)) {
throw new Error('Invalid dictionary: it should be a Buffer instance');
}
}
this._binding = new binding.Zlib(mode);
var self = this;
2012-10-02 23:15:39 +00:00
this._hadError = false;
this._binding.onerror = function(message, errno) {
// there is no way to cleanly recover.
// continuing only obscures problems.
self._binding = null;
self._hadError = true;
var error = new Error(message);
error.errno = errno;
error.code = exports.codes[errno];
self.emit('error', error);
};
2011-09-06 23:13:05 +00:00
this._binding.init(opts.windowBits || exports.Z_DEFAULT_WINDOWBITS,
2011-09-18 06:03:36 +00:00
opts.level || exports.Z_DEFAULT_COMPRESSION,
opts.memLevel || exports.Z_DEFAULT_MEMLEVEL,
2011-12-02 07:53:56 +00:00
opts.strategy || exports.Z_DEFAULT_STRATEGY,
opts.dictionary);
2011-09-06 23:13:05 +00:00
this._buffer = new Buffer(this._chunkSize);
this._offset = 0;
this._closed = false;
this.once('end', this.close);
2011-09-06 23:13:05 +00:00
}
2012-10-02 23:15:39 +00:00
util.inherits(Zlib, Transform);
2011-09-06 23:13:05 +00:00
Zlib.prototype.reset = function reset() {
return this._binding.reset();
};
2012-10-02 23:15:39 +00:00
Zlib.prototype._flush = function(output, callback) {
var rs = this._readableState;
var self = this;
this._transform(null, output, function(er) {
if (er)
return callback(er);
// now a weird thing happens... it could be that you called flush
// but everything had already actually been consumed, but it wasn't
// enough to get over the Readable class's lowWaterMark.
// In that case, we emit 'readable' now to make sure it's consumed.
if (rs.length &&
rs.length < rs.lowWaterMark &&
!rs.ended &&
rs.needReadable)
self.emit('readable');
callback();
});
2011-09-06 23:13:05 +00:00
};
2012-10-02 23:15:39 +00:00
Zlib.prototype.flush = function(callback) {
var ws = this._writableState;
var ts = this._transformState;
2012-10-02 23:15:39 +00:00
if (ws.writing) {
ws.needDrain = true;
var self = this;
this.once('drain', function() {
self._flush(ts.output, callback);
});
return;
}
this._flush(ts.output, callback || function() {});
2011-09-06 23:13:05 +00:00
};
Zlib.prototype.close = function(callback) {
if (callback)
process.nextTick(callback);
if (this._closed)
return;
this._closed = true;
this._binding.close();
2012-10-02 23:15:39 +00:00
var self = this;
process.nextTick(function() {
self.emit('close');
});
};
2012-10-02 23:15:39 +00:00
Zlib.prototype._transform = function(chunk, output, cb) {
var flushFlag;
var ws = this._writableState;
var ending = ws.ending || ws.ended;
var last = ending && (!chunk || ws.length === chunk.length);
if (chunk !== null && !Buffer.isBuffer(chunk))
return cb(new Error('invalid input'));
// If it's the last chunk, or a final flush, we use the Z_FINISH flush flag.
// If it's explicitly flushing at some other time, then we use
// Z_FULL_FLUSH. Otherwise, use Z_NO_FLUSH for maximum compression
// goodness.
if (last)
flushFlag = binding.Z_FINISH;
else if (chunk === null)
flushFlag = binding.Z_FULL_FLUSH;
else
flushFlag = binding.Z_NO_FLUSH;
2011-09-06 23:13:05 +00:00
var availInBefore = chunk && chunk.length;
var availOutBefore = this._chunkSize - this._offset;
var inOff = 0;
2012-10-02 23:15:39 +00:00
var req = this._binding.write(flushFlag,
2011-09-06 23:13:05 +00:00
chunk, // in
inOff, // in_off
availInBefore, // in_len
this._buffer, // out
this._offset, //out_off
availOutBefore); // out_len
req.buffer = chunk;
req.callback = callback;
2012-10-02 23:15:39 +00:00
var self = this;
function callback(availInAfter, availOutAfter, buffer) {
2012-10-02 23:15:39 +00:00
if (self._hadError)
return;
var have = availOutBefore - availOutAfter;
assert(have >= 0, 'have should not go down');
2011-09-06 23:13:05 +00:00
if (have > 0) {
var out = self._buffer.slice(self._offset, self._offset + have);
self._offset += have;
2012-10-02 23:15:39 +00:00
// serve some output to the consumer.
output(out);
2011-09-06 23:13:05 +00:00
}
2012-10-02 23:15:39 +00:00
// exhausted the output buffer, or used all the input create a new one.
2011-09-06 23:13:05 +00:00
if (availOutAfter === 0 || self._offset >= self._chunkSize) {
availOutBefore = self._chunkSize;
2011-09-06 23:13:05 +00:00
self._offset = 0;
self._buffer = new Buffer(self._chunkSize);
}
if (availOutAfter === 0) {
// Not actually done. Need to reprocess.
// Also, update the availInBefore to the availInAfter value,
// so that if we have to hit it a third (fourth, etc.) time,
// it'll have the correct byte counts.
2011-09-06 23:13:05 +00:00
inOff += (availInBefore - availInAfter);
availInBefore = availInAfter;
2012-10-02 23:15:39 +00:00
var newReq = self._binding.write(flushFlag,
2011-09-06 23:13:05 +00:00
chunk,
inOff,
availInBefore,
2011-09-06 23:13:05 +00:00
self._buffer,
self._offset,
self._chunkSize);
newReq.callback = callback; // this same function
newReq.buffer = chunk;
return;
}
// finished with the chunk.
2012-10-02 23:15:39 +00:00
cb();
2011-09-06 23:13:05 +00:00
}
};
util.inherits(Deflate, Zlib);
util.inherits(Inflate, Zlib);
util.inherits(Gzip, Zlib);
util.inherits(Gunzip, Zlib);
util.inherits(DeflateRaw, Zlib);
util.inherits(InflateRaw, Zlib);
util.inherits(Unzip, Zlib);