zlib: revert back to Functions
Using ES6 Classes broke userland code. Revert back to functions. PR-URL: https://github.com/nodejs/node/pull/13374 Fixes: https://github.com/nodejs/node/issues/13358 Ref: https://github.com/nodejs/node/pull/13370 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
parent
e6dcc3dfa9
commit
7024c5a302
649
lib/zlib.js
649
lib/zlib.js
@ -22,7 +22,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const Buffer = require('buffer').Buffer;
|
const Buffer = require('buffer').Buffer;
|
||||||
const internalUtil = require('internal/util');
|
|
||||||
const Transform = require('_stream_transform');
|
const Transform = require('_stream_transform');
|
||||||
const binding = process.binding('zlib');
|
const binding = process.binding('zlib');
|
||||||
const assert = require('assert').ok;
|
const assert = require('assert').ok;
|
||||||
@ -31,7 +30,7 @@ const kRangeErrorMessage = 'Cannot create final Buffer. It would be larger ' +
|
|||||||
`than 0x${kMaxLength.toString(16)} bytes`;
|
`than 0x${kMaxLength.toString(16)} bytes`;
|
||||||
|
|
||||||
const constants = process.binding('constants').zlib;
|
const constants = process.binding('constants').zlib;
|
||||||
const createClassWrapper = internalUtil.createClassWrapper;
|
const { inherits } = require('util');
|
||||||
|
|
||||||
// translation table for return codes.
|
// translation table for return codes.
|
||||||
const codes = {
|
const codes = {
|
||||||
@ -170,319 +169,322 @@ function flushCallback(level, strategy, callback) {
|
|||||||
// This thing manages the queue of requests, and returns
|
// This thing manages the queue of requests, and returns
|
||||||
// true or false if there is anything in the queue when
|
// true or false if there is anything in the queue when
|
||||||
// you call the .write() method.
|
// you call the .write() method.
|
||||||
class Zlib extends Transform {
|
function Zlib(opts, mode) {
|
||||||
constructor(opts, mode) {
|
opts = opts || {};
|
||||||
opts = opts || {};
|
Transform.call(this, opts);
|
||||||
super(opts);
|
|
||||||
|
|
||||||
this.bytesRead = 0;
|
this.bytesRead = 0;
|
||||||
|
|
||||||
this._opts = opts;
|
this._opts = opts;
|
||||||
this._chunkSize = opts.chunkSize || constants.Z_DEFAULT_CHUNK;
|
this._chunkSize = opts.chunkSize || constants.Z_DEFAULT_CHUNK;
|
||||||
|
|
||||||
if (opts.flush && isInvalidFlushFlag(opts.flush)) {
|
if (opts.flush && isInvalidFlushFlag(opts.flush)) {
|
||||||
throw new RangeError('Invalid flush flag: ' + opts.flush);
|
throw new RangeError('Invalid flush flag: ' + opts.flush);
|
||||||
}
|
}
|
||||||
if (opts.finishFlush && isInvalidFlushFlag(opts.finishFlush)) {
|
if (opts.finishFlush && isInvalidFlushFlag(opts.finishFlush)) {
|
||||||
throw new RangeError('Invalid flush flag: ' + opts.finishFlush);
|
throw new RangeError('Invalid flush flag: ' + opts.finishFlush);
|
||||||
}
|
|
||||||
|
|
||||||
this._flushFlag = opts.flush || constants.Z_NO_FLUSH;
|
|
||||||
this._finishFlushFlag = opts.finishFlush !== undefined ?
|
|
||||||
opts.finishFlush : constants.Z_FINISH;
|
|
||||||
|
|
||||||
if (opts.chunkSize !== undefined) {
|
|
||||||
if (opts.chunkSize < constants.Z_MIN_CHUNK) {
|
|
||||||
throw new RangeError('Invalid chunk size: ' + opts.chunkSize);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (opts.windowBits !== undefined) {
|
|
||||||
if (opts.windowBits < constants.Z_MIN_WINDOWBITS ||
|
|
||||||
opts.windowBits > constants.Z_MAX_WINDOWBITS) {
|
|
||||||
throw new RangeError('Invalid windowBits: ' + opts.windowBits);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (opts.level !== undefined) {
|
|
||||||
if (opts.level < constants.Z_MIN_LEVEL ||
|
|
||||||
opts.level > constants.Z_MAX_LEVEL) {
|
|
||||||
throw new RangeError('Invalid compression level: ' + opts.level);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (opts.memLevel !== undefined) {
|
|
||||||
if (opts.memLevel < constants.Z_MIN_MEMLEVEL ||
|
|
||||||
opts.memLevel > constants.Z_MAX_MEMLEVEL) {
|
|
||||||
throw new RangeError('Invalid memLevel: ' + opts.memLevel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (opts.strategy !== undefined && isInvalidStrategy(opts.strategy))
|
|
||||||
throw new TypeError('Invalid strategy: ' + opts.strategy);
|
|
||||||
|
|
||||||
if (opts.dictionary !== undefined) {
|
|
||||||
if (!ArrayBuffer.isView(opts.dictionary)) {
|
|
||||||
throw new TypeError(
|
|
||||||
'Invalid dictionary: it should be a Buffer, TypedArray, or DataView');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this._handle = new binding.Zlib(mode);
|
|
||||||
this._handle.onerror = zlibOnError.bind(this);
|
|
||||||
this._hadError = false;
|
|
||||||
|
|
||||||
var level = constants.Z_DEFAULT_COMPRESSION;
|
|
||||||
if (Number.isFinite(opts.level)) {
|
|
||||||
level = opts.level;
|
|
||||||
}
|
|
||||||
|
|
||||||
var strategy = constants.Z_DEFAULT_STRATEGY;
|
|
||||||
if (Number.isFinite(opts.strategy)) {
|
|
||||||
strategy = opts.strategy;
|
|
||||||
}
|
|
||||||
|
|
||||||
var windowBits = constants.Z_DEFAULT_WINDOWBITS;
|
|
||||||
if (Number.isFinite(opts.windowBits)) {
|
|
||||||
windowBits = opts.windowBits;
|
|
||||||
}
|
|
||||||
|
|
||||||
var memLevel = constants.Z_DEFAULT_MEMLEVEL;
|
|
||||||
if (Number.isFinite(opts.memLevel)) {
|
|
||||||
memLevel = opts.memLevel;
|
|
||||||
}
|
|
||||||
|
|
||||||
this._handle.init(windowBits,
|
|
||||||
level,
|
|
||||||
memLevel,
|
|
||||||
strategy,
|
|
||||||
opts.dictionary);
|
|
||||||
|
|
||||||
this._buffer = Buffer.allocUnsafe(this._chunkSize);
|
|
||||||
this._offset = 0;
|
|
||||||
this._level = level;
|
|
||||||
this._strategy = strategy;
|
|
||||||
|
|
||||||
this.once('end', this.close);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get _closed() {
|
this._flushFlag = opts.flush || constants.Z_NO_FLUSH;
|
||||||
|
this._finishFlushFlag = opts.finishFlush !== undefined ?
|
||||||
|
opts.finishFlush : constants.Z_FINISH;
|
||||||
|
|
||||||
|
if (opts.chunkSize !== undefined) {
|
||||||
|
if (opts.chunkSize < constants.Z_MIN_CHUNK) {
|
||||||
|
throw new RangeError('Invalid chunk size: ' + opts.chunkSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (opts.windowBits !== undefined) {
|
||||||
|
if (opts.windowBits < constants.Z_MIN_WINDOWBITS ||
|
||||||
|
opts.windowBits > constants.Z_MAX_WINDOWBITS) {
|
||||||
|
throw new RangeError('Invalid windowBits: ' + opts.windowBits);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (opts.level !== undefined) {
|
||||||
|
if (opts.level < constants.Z_MIN_LEVEL ||
|
||||||
|
opts.level > constants.Z_MAX_LEVEL) {
|
||||||
|
throw new RangeError('Invalid compression level: ' + opts.level);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (opts.memLevel !== undefined) {
|
||||||
|
if (opts.memLevel < constants.Z_MIN_MEMLEVEL ||
|
||||||
|
opts.memLevel > constants.Z_MAX_MEMLEVEL) {
|
||||||
|
throw new RangeError('Invalid memLevel: ' + opts.memLevel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (opts.strategy !== undefined && isInvalidStrategy(opts.strategy))
|
||||||
|
throw new TypeError('Invalid strategy: ' + opts.strategy);
|
||||||
|
|
||||||
|
if (opts.dictionary !== undefined) {
|
||||||
|
if (!ArrayBuffer.isView(opts.dictionary)) {
|
||||||
|
throw new TypeError(
|
||||||
|
'Invalid dictionary: it should be a Buffer, TypedArray, or DataView');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this._handle = new binding.Zlib(mode);
|
||||||
|
this._handle.onerror = zlibOnError.bind(this);
|
||||||
|
this._hadError = false;
|
||||||
|
|
||||||
|
var level = constants.Z_DEFAULT_COMPRESSION;
|
||||||
|
if (Number.isFinite(opts.level)) {
|
||||||
|
level = opts.level;
|
||||||
|
}
|
||||||
|
|
||||||
|
var strategy = constants.Z_DEFAULT_STRATEGY;
|
||||||
|
if (Number.isFinite(opts.strategy)) {
|
||||||
|
strategy = opts.strategy;
|
||||||
|
}
|
||||||
|
|
||||||
|
var windowBits = constants.Z_DEFAULT_WINDOWBITS;
|
||||||
|
if (Number.isFinite(opts.windowBits)) {
|
||||||
|
windowBits = opts.windowBits;
|
||||||
|
}
|
||||||
|
|
||||||
|
var memLevel = constants.Z_DEFAULT_MEMLEVEL;
|
||||||
|
if (Number.isFinite(opts.memLevel)) {
|
||||||
|
memLevel = opts.memLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._handle.init(windowBits,
|
||||||
|
level,
|
||||||
|
memLevel,
|
||||||
|
strategy,
|
||||||
|
opts.dictionary);
|
||||||
|
|
||||||
|
this._buffer = Buffer.allocUnsafe(this._chunkSize);
|
||||||
|
this._offset = 0;
|
||||||
|
this._level = level;
|
||||||
|
this._strategy = strategy;
|
||||||
|
|
||||||
|
this.once('end', this.close);
|
||||||
|
}
|
||||||
|
inherits(Zlib, Transform);
|
||||||
|
|
||||||
|
Object.defineProperty(Zlib.prototype, '_closed', {
|
||||||
|
configurable: true,
|
||||||
|
enumerable: true,
|
||||||
|
get() {
|
||||||
return !this._handle;
|
return !this._handle;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
params(level, strategy, callback) {
|
Zlib.prototype.params = function params(level, strategy, callback) {
|
||||||
if (level < constants.Z_MIN_LEVEL ||
|
if (level < constants.Z_MIN_LEVEL ||
|
||||||
level > constants.Z_MAX_LEVEL) {
|
level > constants.Z_MAX_LEVEL) {
|
||||||
throw new RangeError('Invalid compression level: ' + level);
|
throw new RangeError('Invalid compression level: ' + level);
|
||||||
}
|
}
|
||||||
if (isInvalidStrategy(strategy))
|
if (isInvalidStrategy(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) {
|
||||||
this.flush(constants.Z_SYNC_FLUSH,
|
this.flush(constants.Z_SYNC_FLUSH,
|
||||||
flushCallback.bind(this, level, strategy, callback));
|
flushCallback.bind(this, level, strategy, callback));
|
||||||
} else {
|
} else {
|
||||||
|
process.nextTick(callback);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Zlib.prototype.reset = function reset() {
|
||||||
|
assert(this._handle, 'zlib binding closed');
|
||||||
|
return this._handle.reset();
|
||||||
|
};
|
||||||
|
|
||||||
|
// This is the _flush function called by the transform class,
|
||||||
|
// internally, when the last chunk has been written.
|
||||||
|
Zlib.prototype._flush = function _flush(callback) {
|
||||||
|
this._transform(Buffer.alloc(0), '', callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
Zlib.prototype.flush = function flush(kind, callback) {
|
||||||
|
var ws = this._writableState;
|
||||||
|
|
||||||
|
if (typeof kind === 'function' || (kind === undefined && !callback)) {
|
||||||
|
callback = kind;
|
||||||
|
kind = constants.Z_FULL_FLUSH;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ws.ended) {
|
||||||
|
if (callback)
|
||||||
process.nextTick(callback);
|
process.nextTick(callback);
|
||||||
|
} else if (ws.ending) {
|
||||||
|
if (callback)
|
||||||
|
this.once('end', callback);
|
||||||
|
} else if (ws.needDrain) {
|
||||||
|
if (callback) {
|
||||||
|
const drainHandler = () => this.flush(kind, callback);
|
||||||
|
this.once('drain', drainHandler);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this._flushFlag = kind;
|
||||||
|
this.write(Buffer.alloc(0), '', callback);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Zlib.prototype.close = function close(callback) {
|
||||||
|
_close(this, callback);
|
||||||
|
process.nextTick(emitCloseNT, this);
|
||||||
|
};
|
||||||
|
|
||||||
|
Zlib.prototype._transform = function _transform(chunk, encoding, cb) {
|
||||||
|
var flushFlag;
|
||||||
|
var ws = this._writableState;
|
||||||
|
var ending = ws.ending || ws.ended;
|
||||||
|
var last = ending && (!chunk || ws.length === chunk.byteLength);
|
||||||
|
|
||||||
|
if (chunk !== null && !ArrayBuffer.isView(chunk))
|
||||||
|
return cb(new TypeError('invalid input'));
|
||||||
|
|
||||||
|
if (!this._handle)
|
||||||
|
return cb(new Error('zlib binding closed'));
|
||||||
|
|
||||||
|
// If it's the last chunk, or a final flush, we use the Z_FINISH flush flag
|
||||||
|
// (or whatever flag was provided using opts.finishFlush).
|
||||||
|
// 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 = this._finishFlushFlag;
|
||||||
|
else {
|
||||||
|
flushFlag = this._flushFlag;
|
||||||
|
// once we've flushed the last of the queue, stop flushing and
|
||||||
|
// go back to the normal behavior.
|
||||||
|
if (chunk.byteLength >= ws.length) {
|
||||||
|
this._flushFlag = this._opts.flush || constants.Z_NO_FLUSH;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
reset() {
|
this._processChunk(chunk, flushFlag, cb);
|
||||||
|
};
|
||||||
|
|
||||||
|
Zlib.prototype._processChunk = function _processChunk(chunk, flushFlag, cb) {
|
||||||
|
var availInBefore = chunk && chunk.byteLength;
|
||||||
|
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;
|
||||||
|
});
|
||||||
|
|
||||||
assert(this._handle, 'zlib binding closed');
|
assert(this._handle, 'zlib binding closed');
|
||||||
return this._handle.reset();
|
do {
|
||||||
}
|
var res = this._handle.writeSync(flushFlag,
|
||||||
|
chunk, // in
|
||||||
|
inOff, // in_off
|
||||||
|
availInBefore, // in_len
|
||||||
|
this._buffer, // out
|
||||||
|
this._offset, //out_off
|
||||||
|
availOutBefore); // out_len
|
||||||
|
} while (!this._hadError && callback(res[0], res[1]));
|
||||||
|
|
||||||
// This is the _flush function called by the transform class,
|
if (this._hadError) {
|
||||||
// internally, when the last chunk has been written.
|
throw error;
|
||||||
_flush(callback) {
|
|
||||||
this._transform(Buffer.alloc(0), '', callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
flush(kind, callback) {
|
|
||||||
var ws = this._writableState;
|
|
||||||
|
|
||||||
if (typeof kind === 'function' || (kind === undefined && !callback)) {
|
|
||||||
callback = kind;
|
|
||||||
kind = constants.Z_FULL_FLUSH;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ws.ended) {
|
if (nread >= kMaxLength) {
|
||||||
if (callback)
|
|
||||||
process.nextTick(callback);
|
|
||||||
} else if (ws.ending) {
|
|
||||||
if (callback)
|
|
||||||
this.once('end', callback);
|
|
||||||
} else if (ws.needDrain) {
|
|
||||||
if (callback) {
|
|
||||||
const drainHandler = () => this.flush(kind, callback);
|
|
||||||
this.once('drain', drainHandler);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
this._flushFlag = kind;
|
|
||||||
this.write(Buffer.alloc(0), '', callback);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
close(callback) {
|
|
||||||
_close(this, callback);
|
|
||||||
process.nextTick(emitCloseNT, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
_transform(chunk, encoding, cb) {
|
|
||||||
var flushFlag;
|
|
||||||
var ws = this._writableState;
|
|
||||||
var ending = ws.ending || ws.ended;
|
|
||||||
var last = ending && (!chunk || ws.length === chunk.byteLength);
|
|
||||||
|
|
||||||
if (chunk !== null && !ArrayBuffer.isView(chunk))
|
|
||||||
return cb(new TypeError('invalid input'));
|
|
||||||
|
|
||||||
if (!this._handle)
|
|
||||||
return cb(new Error('zlib binding closed'));
|
|
||||||
|
|
||||||
// If it's the last chunk, or a final flush, we use the Z_FINISH flush flag
|
|
||||||
// (or whatever flag was provided using opts.finishFlush).
|
|
||||||
// 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 = this._finishFlushFlag;
|
|
||||||
else {
|
|
||||||
flushFlag = this._flushFlag;
|
|
||||||
// once we've flushed the last of the queue, stop flushing and
|
|
||||||
// go back to the normal behavior.
|
|
||||||
if (chunk.byteLength >= ws.length) {
|
|
||||||
this._flushFlag = this._opts.flush || constants.Z_NO_FLUSH;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this._processChunk(chunk, flushFlag, cb);
|
|
||||||
}
|
|
||||||
|
|
||||||
_processChunk(chunk, flushFlag, cb) {
|
|
||||||
var availInBefore = chunk && chunk.byteLength;
|
|
||||||
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;
|
|
||||||
});
|
|
||||||
|
|
||||||
assert(this._handle, 'zlib binding closed');
|
|
||||||
do {
|
|
||||||
var res = this._handle.writeSync(flushFlag,
|
|
||||||
chunk, // in
|
|
||||||
inOff, // in_off
|
|
||||||
availInBefore, // in_len
|
|
||||||
this._buffer, // out
|
|
||||||
this._offset, //out_off
|
|
||||||
availOutBefore); // out_len
|
|
||||||
} while (!this._hadError && callback(res[0], res[1]));
|
|
||||||
|
|
||||||
if (this._hadError) {
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nread >= kMaxLength) {
|
|
||||||
_close(this);
|
|
||||||
throw new RangeError(kRangeErrorMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
var buf = Buffer.concat(buffers, nread);
|
|
||||||
_close(this);
|
_close(this);
|
||||||
|
throw new RangeError(kRangeErrorMessage);
|
||||||
return buf;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(this._handle, 'zlib binding closed');
|
var buf = Buffer.concat(buffers, nread);
|
||||||
var req = this._handle.write(flushFlag,
|
_close(this);
|
||||||
chunk, // in
|
|
||||||
inOff, // in_off
|
|
||||||
availInBefore, // in_len
|
|
||||||
this._buffer, // out
|
|
||||||
this._offset, //out_off
|
|
||||||
availOutBefore); // out_len
|
|
||||||
|
|
||||||
req.buffer = chunk;
|
return buf;
|
||||||
req.callback = callback;
|
}
|
||||||
|
|
||||||
function callback(availInAfter, availOutAfter) {
|
assert(this._handle, 'zlib binding closed');
|
||||||
// When the callback is used in an async write, the callback's
|
var req = this._handle.write(flushFlag,
|
||||||
// context is the `req` object that was created. The req object
|
chunk, // in
|
||||||
// is === this._handle, and that's why it's important to null
|
inOff, // in_off
|
||||||
// out the values after they are done being used. `this._handle`
|
availInBefore, // in_len
|
||||||
// can stay in memory longer than the callback and buffer are needed.
|
this._buffer, // out
|
||||||
if (this) {
|
this._offset, //out_off
|
||||||
this.buffer = null;
|
availOutBefore); // out_len
|
||||||
this.callback = null;
|
|
||||||
|
req.buffer = chunk;
|
||||||
|
req.callback = callback;
|
||||||
|
|
||||||
|
function callback(availInAfter, availOutAfter) {
|
||||||
|
// When the callback is used in an async write, the callback's
|
||||||
|
// context is the `req` object that was created. The req object
|
||||||
|
// is === this._handle, and that's why it's important to null
|
||||||
|
// out the values after they are done being used. `this._handle`
|
||||||
|
// can stay in memory longer than the callback and buffer are needed.
|
||||||
|
if (this) {
|
||||||
|
this.buffer = null;
|
||||||
|
this.callback = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self._hadError)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var have = availOutBefore - availOutAfter;
|
||||||
|
assert(have >= 0, 'have should not go down');
|
||||||
|
|
||||||
|
self.bytesRead += availInBefore - availInAfter;
|
||||||
|
|
||||||
|
if (have > 0) {
|
||||||
|
var out = self._buffer.slice(self._offset, self._offset + have);
|
||||||
|
self._offset += have;
|
||||||
|
// serve some output to the consumer.
|
||||||
|
if (async) {
|
||||||
|
self.push(out);
|
||||||
|
} else {
|
||||||
|
buffers.push(out);
|
||||||
|
nread += out.byteLength;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (self._hadError)
|
// exhausted the output buffer, or used all the input create a new one.
|
||||||
return;
|
if (availOutAfter === 0 || self._offset >= self._chunkSize) {
|
||||||
|
availOutBefore = self._chunkSize;
|
||||||
|
self._offset = 0;
|
||||||
|
self._buffer = Buffer.allocUnsafe(self._chunkSize);
|
||||||
|
}
|
||||||
|
|
||||||
var have = availOutBefore - availOutAfter;
|
if (availOutAfter === 0) {
|
||||||
assert(have >= 0, 'have should not go down');
|
// Not actually done. Need to reprocess.
|
||||||
|
// Also, update the availInBefore to the availInAfter value,
|
||||||
self.bytesRead += availInBefore - availInAfter;
|
// so that if we have to hit it a third (fourth, etc.) time,
|
||||||
|
// it'll have the correct byte counts.
|
||||||
if (have > 0) {
|
inOff += (availInBefore - availInAfter);
|
||||||
var out = self._buffer.slice(self._offset, self._offset + have);
|
availInBefore = availInAfter;
|
||||||
self._offset += have;
|
|
||||||
// serve some output to the consumer.
|
|
||||||
if (async) {
|
|
||||||
self.push(out);
|
|
||||||
} else {
|
|
||||||
buffers.push(out);
|
|
||||||
nread += out.byteLength;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// exhausted the output buffer, or used all the input create a new one.
|
|
||||||
if (availOutAfter === 0 || self._offset >= self._chunkSize) {
|
|
||||||
availOutBefore = self._chunkSize;
|
|
||||||
self._offset = 0;
|
|
||||||
self._buffer = Buffer.allocUnsafe(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.
|
|
||||||
inOff += (availInBefore - availInAfter);
|
|
||||||
availInBefore = availInAfter;
|
|
||||||
|
|
||||||
if (!async)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
var newReq = self._handle.write(flushFlag,
|
|
||||||
chunk,
|
|
||||||
inOff,
|
|
||||||
availInBefore,
|
|
||||||
self._buffer,
|
|
||||||
self._offset,
|
|
||||||
self._chunkSize);
|
|
||||||
newReq.callback = callback; // this same function
|
|
||||||
newReq.buffer = chunk;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!async)
|
if (!async)
|
||||||
return false;
|
return true;
|
||||||
|
|
||||||
// finished with the chunk.
|
var newReq = self._handle.write(flushFlag,
|
||||||
cb();
|
chunk,
|
||||||
|
inOff,
|
||||||
|
availInBefore,
|
||||||
|
self._buffer,
|
||||||
|
self._offset,
|
||||||
|
self._chunkSize);
|
||||||
|
newReq.callback = callback; // this same function
|
||||||
|
newReq.buffer = chunk;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!async)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// finished with the chunk.
|
||||||
|
cb();
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
function _close(engine, callback) {
|
function _close(engine, callback) {
|
||||||
if (callback)
|
if (callback)
|
||||||
@ -502,47 +504,54 @@ function emitCloseNT(self) {
|
|||||||
|
|
||||||
// generic zlib
|
// generic zlib
|
||||||
// minimal 2-byte header
|
// minimal 2-byte header
|
||||||
class Deflate extends Zlib {
|
function Deflate(opts) {
|
||||||
constructor(opts) {
|
if (!(this instanceof Deflate))
|
||||||
super(opts, constants.DEFLATE);
|
return new Deflate(opts);
|
||||||
}
|
Zlib.call(this, opts, constants.DEFLATE);
|
||||||
}
|
}
|
||||||
|
inherits(Deflate, Zlib);
|
||||||
|
|
||||||
class Inflate extends Zlib {
|
function Inflate(opts) {
|
||||||
constructor(opts) {
|
if (!(this instanceof Inflate))
|
||||||
super(opts, constants.INFLATE);
|
return new Inflate(opts);
|
||||||
}
|
Zlib.call(this, opts, constants.INFLATE);
|
||||||
}
|
}
|
||||||
|
inherits(Inflate, Zlib);
|
||||||
|
|
||||||
class Gzip extends Zlib {
|
function Gzip(opts) {
|
||||||
constructor(opts) {
|
if (!(this instanceof Gzip))
|
||||||
super(opts, constants.GZIP);
|
return new Gzip(opts);
|
||||||
}
|
Zlib.call(this, opts, constants.GZIP);
|
||||||
}
|
}
|
||||||
|
inherits(Gzip, Zlib);
|
||||||
|
|
||||||
class Gunzip extends Zlib {
|
function Gunzip(opts) {
|
||||||
constructor(opts) {
|
if (!(this instanceof Gunzip))
|
||||||
super(opts, constants.GUNZIP);
|
return new Gunzip(opts);
|
||||||
}
|
Zlib.call(this, opts, constants.GUNZIP);
|
||||||
}
|
}
|
||||||
|
inherits(Gunzip, Zlib);
|
||||||
|
|
||||||
class DeflateRaw extends Zlib {
|
function DeflateRaw(opts) {
|
||||||
constructor(opts) {
|
if (!(this instanceof DeflateRaw))
|
||||||
super(opts, constants.DEFLATERAW);
|
return new DeflateRaw(opts);
|
||||||
}
|
Zlib.call(this, opts, constants.DEFLATERAW);
|
||||||
}
|
}
|
||||||
|
inherits(DeflateRaw, Zlib);
|
||||||
|
|
||||||
class InflateRaw extends Zlib {
|
function InflateRaw(opts) {
|
||||||
constructor(opts) {
|
if (!(this instanceof InflateRaw))
|
||||||
super(opts, constants.INFLATERAW);
|
return new InflateRaw(opts);
|
||||||
}
|
Zlib.call(this, opts, constants.INFLATERAW);
|
||||||
}
|
}
|
||||||
|
inherits(InflateRaw, Zlib);
|
||||||
|
|
||||||
class Unzip extends Zlib {
|
function Unzip(opts) {
|
||||||
constructor(opts) {
|
if (!(this instanceof Unzip))
|
||||||
super(opts, constants.UNZIP);
|
return new Unzip(opts);
|
||||||
}
|
Zlib.call(this, opts, constants.UNZIP);
|
||||||
}
|
}
|
||||||
|
inherits(Unzip, Zlib);
|
||||||
|
|
||||||
function createConvenienceMethod(type, sync) {
|
function createConvenienceMethod(type, sync) {
|
||||||
if (sync) {
|
if (sync) {
|
||||||
@ -569,13 +578,13 @@ function createProperty(type) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
Deflate: createClassWrapper(Deflate),
|
Deflate,
|
||||||
Inflate: createClassWrapper(Inflate),
|
Inflate,
|
||||||
Gzip: createClassWrapper(Gzip),
|
Gzip,
|
||||||
Gunzip: createClassWrapper(Gunzip),
|
Gunzip,
|
||||||
DeflateRaw: createClassWrapper(DeflateRaw),
|
DeflateRaw,
|
||||||
InflateRaw: createClassWrapper(InflateRaw),
|
InflateRaw,
|
||||||
Unzip: createClassWrapper(Unzip),
|
Unzip,
|
||||||
|
|
||||||
// Convenience methods.
|
// Convenience methods.
|
||||||
// compress/decompress a string or buffer in one step.
|
// compress/decompress a string or buffer in one step.
|
||||||
|
27
test/parallel/test-zlib-deflate-raw-inherits.js
Normal file
27
test/parallel/test-zlib-deflate-raw-inherits.js
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
require('../common');
|
||||||
|
const { DeflateRaw } = require('zlib');
|
||||||
|
const { inherits } = require('util');
|
||||||
|
const { Readable } = require('stream');
|
||||||
|
|
||||||
|
// validates that zlib.DeflateRaw can be inherited
|
||||||
|
// with util.inherits
|
||||||
|
|
||||||
|
function NotInitialized(options) {
|
||||||
|
DeflateRaw.call(this, options);
|
||||||
|
this.prop = true;
|
||||||
|
}
|
||||||
|
inherits(NotInitialized, DeflateRaw);
|
||||||
|
|
||||||
|
const dest = new NotInitialized();
|
||||||
|
|
||||||
|
const read = new Readable({
|
||||||
|
read() {
|
||||||
|
this.push(Buffer.from('a test string'));
|
||||||
|
this.push(null);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
read.pipe(dest);
|
||||||
|
dest.resume();
|
Loading…
x
Reference in New Issue
Block a user