| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import {Transform} from 'stream'; |
| import * as _binding from './zlib-lib/binding'; |
| import {inherits} from 'util'; |
| function assert (a, msg) { |
| if (!a) { |
| throw new Error(msg); |
| } |
| } |
| var binding = {}; |
| Object.keys(_binding).forEach(function (key) { |
| binding[key] = _binding[key]; |
| }); |
| |
| |
| binding.Z_MIN_WINDOWBITS = 8; |
| binding.Z_MAX_WINDOWBITS = 15; |
| binding.Z_DEFAULT_WINDOWBITS = 15; |
|
|
| |
| |
| |
| 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; |
|
|
|
|
| |
| export var 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(codes).forEach(function(k) { |
| codes[codes[k]] = k; |
| }); |
|
|
| export function createDeflate(o) { |
| return new Deflate(o); |
| } |
|
|
| export function createInflate(o) { |
| return new Inflate(o); |
| } |
|
|
| export function createDeflateRaw(o) { |
| return new DeflateRaw(o); |
| } |
|
|
| export function createInflateRaw(o) { |
| return new InflateRaw(o); |
| } |
|
|
| export function createGzip(o) { |
| return new Gzip(o); |
| } |
|
|
| export function createGunzip(o) { |
| return new Gunzip(o); |
| } |
|
|
| export function createUnzip(o) { |
| return new Unzip(o); |
| } |
|
|
|
|
| |
| |
| export function deflate(buffer, opts, callback) { |
| if (typeof opts === 'function') { |
| callback = opts; |
| opts = {}; |
| } |
| return zlibBuffer(new Deflate(opts), buffer, callback); |
| } |
|
|
| export function deflateSync(buffer, opts) { |
| return zlibBufferSync(new Deflate(opts), buffer); |
| } |
|
|
| export function gzip(buffer, opts, callback) { |
| if (typeof opts === 'function') { |
| callback = opts; |
| opts = {}; |
| } |
| return zlibBuffer(new Gzip(opts), buffer, callback); |
| } |
|
|
| export function gzipSync(buffer, opts) { |
| return zlibBufferSync(new Gzip(opts), buffer); |
| } |
|
|
| export function deflateRaw(buffer, opts, callback) { |
| if (typeof opts === 'function') { |
| callback = opts; |
| opts = {}; |
| } |
| return zlibBuffer(new DeflateRaw(opts), buffer, callback); |
| } |
|
|
| export function deflateRawSync(buffer, opts) { |
| return zlibBufferSync(new DeflateRaw(opts), buffer); |
| } |
|
|
| export function unzip(buffer, opts, callback) { |
| if (typeof opts === 'function') { |
| callback = opts; |
| opts = {}; |
| } |
| return zlibBuffer(new Unzip(opts), buffer, callback); |
| } |
|
|
| export function unzipSync(buffer, opts) { |
| return zlibBufferSync(new Unzip(opts), buffer); |
| } |
|
|
| export function inflate(buffer, opts, callback) { |
| if (typeof opts === 'function') { |
| callback = opts; |
| opts = {}; |
| } |
| return zlibBuffer(new Inflate(opts), buffer, callback); |
| } |
|
|
| export function inflateSync(buffer, opts) { |
| return zlibBufferSync(new Inflate(opts), buffer); |
| } |
|
|
| export function gunzip(buffer, opts, callback) { |
| if (typeof opts === 'function') { |
| callback = opts; |
| opts = {}; |
| } |
| return zlibBuffer(new Gunzip(opts), buffer, callback); |
| } |
|
|
| export function gunzipSync(buffer, opts) { |
| return zlibBufferSync(new Gunzip(opts), buffer); |
| } |
|
|
| export function inflateRaw(buffer, opts, callback) { |
| if (typeof opts === 'function') { |
| callback = opts; |
| opts = {}; |
| } |
| return zlibBuffer(new InflateRaw(opts), buffer, callback); |
| } |
|
|
| export function inflateRawSync(buffer, opts) { |
| return zlibBufferSync(new InflateRaw(opts), buffer); |
| } |
|
|
| function zlibBuffer(engine, buffer, callback) { |
| var buffers = []; |
| var nread = 0; |
|
|
| 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); |
| engine.removeListener('readable', flow); |
| callback(err); |
| } |
|
|
| function onEnd() { |
| var buf = Buffer.concat(buffers, nread); |
| buffers = []; |
| callback(null, buf); |
| engine.close(); |
| } |
| } |
|
|
| function zlibBufferSync(engine, buffer) { |
| if (typeof buffer === 'string') |
| buffer = new Buffer(buffer); |
| if (!Buffer.isBuffer(buffer)) |
| throw new TypeError('Not a string or buffer'); |
|
|
| var flushFlag = binding.Z_FINISH; |
|
|
| return engine._processChunk(buffer, flushFlag); |
| } |
|
|
| |
| |
| export function Deflate(opts) { |
| if (!(this instanceof Deflate)) return new Deflate(opts); |
| Zlib.call(this, opts, binding.DEFLATE); |
| } |
|
|
| export function Inflate(opts) { |
| if (!(this instanceof Inflate)) return new Inflate(opts); |
| Zlib.call(this, opts, binding.INFLATE); |
| } |
|
|
|
|
|
|
| |
| export function Gzip(opts) { |
| if (!(this instanceof Gzip)) return new Gzip(opts); |
| Zlib.call(this, opts, binding.GZIP); |
| } |
|
|
| export function Gunzip(opts) { |
| if (!(this instanceof Gunzip)) return new Gunzip(opts); |
| Zlib.call(this, opts, binding.GUNZIP); |
| } |
|
|
|
|
|
|
| |
| export function DeflateRaw(opts) { |
| if (!(this instanceof DeflateRaw)) return new DeflateRaw(opts); |
| Zlib.call(this, opts, binding.DEFLATERAW); |
| } |
|
|
| export function InflateRaw(opts) { |
| if (!(this instanceof InflateRaw)) return new InflateRaw(opts); |
| Zlib.call(this, opts, binding.INFLATERAW); |
| } |
|
|
|
|
| |
| export function Unzip(opts) { |
| if (!(this instanceof Unzip)) return new Unzip(opts); |
| Zlib.call(this, opts, binding.UNZIP); |
| } |
|
|
|
|
| |
| |
| |
| |
|
|
| export function Zlib(opts, mode) { |
| this._opts = opts = opts || {}; |
| this._chunkSize = opts.chunkSize || binding.Z_DEFAULT_CHUNK; |
|
|
| Transform.call(this, opts); |
|
|
| if (opts.flush) { |
| if (opts.flush !== binding.Z_NO_FLUSH && |
| opts.flush !== binding.Z_PARTIAL_FLUSH && |
| opts.flush !== binding.Z_SYNC_FLUSH && |
| opts.flush !== binding.Z_FULL_FLUSH && |
| opts.flush !== binding.Z_FINISH && |
| opts.flush !== binding.Z_BLOCK) { |
| throw new Error('Invalid flush flag: ' + opts.flush); |
| } |
| } |
| this._flushFlag = opts.flush || binding.Z_NO_FLUSH; |
|
|
| if (opts.chunkSize) { |
| if (opts.chunkSize < binding.Z_MIN_CHUNK || |
| opts.chunkSize > binding.Z_MAX_CHUNK) { |
| throw new Error('Invalid chunk size: ' + opts.chunkSize); |
| } |
| } |
|
|
| if (opts.windowBits) { |
| if (opts.windowBits < binding.Z_MIN_WINDOWBITS || |
| opts.windowBits > binding.Z_MAX_WINDOWBITS) { |
| throw new Error('Invalid windowBits: ' + opts.windowBits); |
| } |
| } |
|
|
| if (opts.level) { |
| if (opts.level < binding.Z_MIN_LEVEL || |
| opts.level > binding.Z_MAX_LEVEL) { |
| throw new Error('Invalid compression level: ' + opts.level); |
| } |
| } |
|
|
| if (opts.memLevel) { |
| if (opts.memLevel < binding.Z_MIN_MEMLEVEL || |
| opts.memLevel > binding.Z_MAX_MEMLEVEL) { |
| throw new Error('Invalid memLevel: ' + opts.memLevel); |
| } |
| } |
|
|
| if (opts.strategy) { |
| if (opts.strategy != binding.Z_FILTERED && |
| opts.strategy != binding.Z_HUFFMAN_ONLY && |
| opts.strategy != binding.Z_RLE && |
| opts.strategy != binding.Z_FIXED && |
| opts.strategy != binding.Z_DEFAULT_STRATEGY) { |
| throw new Error('Invalid strategy: ' + opts.strategy); |
| } |
| } |
|
|
| 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; |
| this._hadError = false; |
| this._binding.onerror = function(message, errno) { |
| |
| |
| self._binding = null; |
| self._hadError = true; |
|
|
| var error = new Error(message); |
| error.errno = errno; |
| error.code = binding.codes[errno]; |
| self.emit('error', error); |
| }; |
|
|
| var level = binding.Z_DEFAULT_COMPRESSION; |
| if (typeof opts.level === 'number') level = opts.level; |
|
|
| var strategy = binding.Z_DEFAULT_STRATEGY; |
| if (typeof opts.strategy === 'number') strategy = opts.strategy; |
|
|
| this._binding.init(opts.windowBits || binding.Z_DEFAULT_WINDOWBITS, |
| level, |
| opts.memLevel || binding.Z_DEFAULT_MEMLEVEL, |
| strategy, |
| opts.dictionary); |
|
|
| this._buffer = new Buffer(this._chunkSize); |
| this._offset = 0; |
| this._closed = false; |
| this._level = level; |
| this._strategy = strategy; |
|
|
| this.once('end', this.close); |
| } |
|
|
| inherits(Zlib, Transform); |
|
|
| Zlib.prototype.params = function(level, strategy, callback) { |
| if (level < binding.Z_MIN_LEVEL || |
| level > binding.Z_MAX_LEVEL) { |
| throw new RangeError('Invalid compression level: ' + level); |
| } |
| if (strategy != binding.Z_FILTERED && |
| strategy != binding.Z_HUFFMAN_ONLY && |
| strategy != binding.Z_RLE && |
| strategy != binding.Z_FIXED && |
| strategy != binding.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() { |
| self._binding.params(level, strategy); |
| if (!self._hadError) { |
| self._level = level; |
| self._strategy = strategy; |
| if (callback) callback(); |
| } |
| }); |
| } else { |
| process.nextTick(callback); |
| } |
| }; |
|
|
| Zlib.prototype.reset = function() { |
| return this._binding.reset(); |
| }; |
|
|
| |
| |
| Zlib.prototype._flush = function(callback) { |
| this._transform(new Buffer(0), '', callback); |
| }; |
|
|
| Zlib.prototype.flush = function(kind, callback) { |
| var ws = this._writableState; |
|
|
| if (typeof kind === 'function' || (kind === void 0 && !callback)) { |
| callback = kind; |
| kind = binding.Z_FULL_FLUSH; |
| } |
|
|
| if (ws.ended) { |
| if (callback) |
| process.nextTick(callback); |
| } else if (ws.ending) { |
| if (callback) |
| this.once('end', callback); |
| } else if (ws.needDrain) { |
| var self = this; |
| this.once('drain', function() { |
| self.flush(callback); |
| }); |
| } else { |
| this._flushFlag = kind; |
| this.write(new Buffer(0), '', callback); |
| } |
| }; |
|
|
| Zlib.prototype.close = function(callback) { |
| if (callback) |
| process.nextTick(callback); |
|
|
| if (this._closed) |
| return; |
|
|
| this._closed = true; |
|
|
| this._binding.close(); |
|
|
| var self = this; |
| process.nextTick(function() { |
| self.emit('close'); |
| }); |
| }; |
|
|
| Zlib.prototype._transform = function(chunk, encoding, 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 (last) |
| flushFlag = binding.Z_FINISH; |
| else { |
| flushFlag = this._flushFlag; |
| |
| |
| if (chunk.length >= ws.length) { |
| this._flushFlag = this._opts.flush || binding.Z_NO_FLUSH; |
| } |
| } |
|
|
| this._processChunk(chunk, flushFlag, cb); |
| }; |
|
|
| Zlib.prototype._processChunk = function(chunk, flushFlag, cb) { |
| var availInBefore = chunk && chunk.length; |
| var availOutBefore = this._chunkSize - this._offset; |
| var inOff = 0; |
|
|
| var self = this; |
|
|
| var async = typeof cb === 'function'; |
|
|
| if (!async) { |
| var buffers = []; |
| var nread = 0; |
|
|
| var error; |
| this.on('error', function(er) { |
| error = er; |
| }); |
|
|
| do { |
| var res = this._binding.writeSync(flushFlag, |
| chunk, |
| inOff, |
| availInBefore, |
| this._buffer, |
| this._offset, |
| availOutBefore); |
| } while (!this._hadError && callback(res[0], res[1])); |
|
|
| if (this._hadError) { |
| throw error; |
| } |
|
|
| var buf = Buffer.concat(buffers, nread); |
| this.close(); |
|
|
| return buf; |
| } |
|
|
| var req = this._binding.write(flushFlag, |
| chunk, |
| inOff, |
| availInBefore, |
| this._buffer, |
| this._offset, |
| availOutBefore); |
|
|
| req.buffer = chunk; |
| req.callback = callback; |
|
|
| function callback(availInAfter, availOutAfter) { |
| if (self._hadError) |
| return; |
|
|
| var have = availOutBefore - availOutAfter; |
| assert(have >= 0, 'have should not go down'); |
|
|
| if (have > 0) { |
| var out = self._buffer.slice(self._offset, self._offset + have); |
| self._offset += have; |
| |
| if (async) { |
| self.push(out); |
| } else { |
| buffers.push(out); |
| nread += out.length; |
| } |
| } |
|
|
| |
| if (availOutAfter === 0 || self._offset >= self._chunkSize) { |
| availOutBefore = self._chunkSize; |
| self._offset = 0; |
| self._buffer = new Buffer(self._chunkSize); |
| } |
|
|
| if (availOutAfter === 0) { |
| |
| |
| |
| |
| inOff += (availInBefore - availInAfter); |
| availInBefore = availInAfter; |
|
|
| if (!async) |
| return true; |
|
|
| var newReq = self._binding.write(flushFlag, |
| chunk, |
| inOff, |
| availInBefore, |
| self._buffer, |
| self._offset, |
| self._chunkSize); |
| newReq.callback = callback; |
| newReq.buffer = chunk; |
| return; |
| } |
|
|
| if (!async) |
| return false; |
|
|
| |
| cb(); |
| } |
| }; |
|
|
| inherits(Deflate, Zlib); |
| inherits(Inflate, Zlib); |
| inherits(Gzip, Zlib); |
| inherits(Gunzip, Zlib); |
| inherits(DeflateRaw, Zlib); |
| inherits(InflateRaw, Zlib); |
| inherits(Unzip, Zlib); |
| export default { |
| codes: codes, |
| createDeflate: createDeflate, |
| createInflate: createInflate, |
| createDeflateRaw: createDeflateRaw, |
| createInflateRaw: createInflateRaw, |
| createGzip: createGzip, |
| createGunzip: createGunzip, |
| createUnzip: createUnzip, |
| deflate: deflate, |
| deflateSync: deflateSync, |
| gzip: gzip, |
| gzipSync: gzipSync, |
| deflateRaw: deflateRaw, |
| deflateRawSync: deflateRawSync, |
| unzip: unzip, |
| unzipSync: unzipSync, |
| inflate: inflate, |
| inflateSync: inflateSync, |
| gunzip: gunzip, |
| gunzipSync: gunzipSync, |
| inflateRaw: inflateRaw, |
| inflateRawSync: inflateRawSync, |
| Deflate: Deflate, |
| Inflate: Inflate, |
| Gzip: Gzip, |
| Gunzip: Gunzip, |
| DeflateRaw: DeflateRaw, |
| InflateRaw: InflateRaw, |
| Unzip: Unzip, |
| Zlib: Zlib |
| }; |
|
|