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:
James M Snell 2017-06-01 11:23:59 -07:00
parent e6dcc3dfa9
commit 7024c5a302
2 changed files with 356 additions and 320 deletions

View File

@ -22,7 +22,6 @@
'use strict';
const Buffer = require('buffer').Buffer;
const internalUtil = require('internal/util');
const Transform = require('_stream_transform');
const binding = process.binding('zlib');
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`;
const constants = process.binding('constants').zlib;
const createClassWrapper = internalUtil.createClassWrapper;
const { inherits } = require('util');
// translation table for return codes.
const codes = {
@ -170,10 +169,9 @@ function flushCallback(level, strategy, callback) {
// 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.
class Zlib extends Transform {
constructor(opts, mode) {
function Zlib(opts, mode) {
opts = opts || {};
super(opts);
Transform.call(this, opts);
this.bytesRead = 0;
@ -264,13 +262,18 @@ class Zlib extends Transform {
this._strategy = strategy;
this.once('end', this.close);
}
}
inherits(Zlib, Transform);
get _closed() {
Object.defineProperty(Zlib.prototype, '_closed', {
configurable: true,
enumerable: true,
get() {
return !this._handle;
}
});
params(level, strategy, callback) {
Zlib.prototype.params = function params(level, strategy, callback) {
if (level < constants.Z_MIN_LEVEL ||
level > constants.Z_MAX_LEVEL) {
throw new RangeError('Invalid compression level: ' + level);
@ -284,20 +287,20 @@ class Zlib extends Transform {
} else {
process.nextTick(callback);
}
}
};
reset() {
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.
_flush(callback) {
// 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);
}
};
flush(kind, callback) {
Zlib.prototype.flush = function flush(kind, callback) {
var ws = this._writableState;
if (typeof kind === 'function' || (kind === undefined && !callback)) {
@ -320,14 +323,14 @@ class Zlib extends Transform {
this._flushFlag = kind;
this.write(Buffer.alloc(0), '', callback);
}
}
};
close(callback) {
Zlib.prototype.close = function close(callback) {
_close(this, callback);
process.nextTick(emitCloseNT, this);
}
};
_transform(chunk, encoding, cb) {
Zlib.prototype._transform = function _transform(chunk, encoding, cb) {
var flushFlag;
var ws = this._writableState;
var ending = ws.ending || ws.ended;
@ -356,9 +359,9 @@ class Zlib extends Transform {
}
this._processChunk(chunk, flushFlag, cb);
}
};
_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;
@ -481,8 +484,7 @@ class Zlib extends Transform {
// finished with the chunk.
cb();
}
}
}
};
function _close(engine, callback) {
if (callback)
@ -502,47 +504,54 @@ function emitCloseNT(self) {
// generic zlib
// minimal 2-byte header
class Deflate extends Zlib {
constructor(opts) {
super(opts, constants.DEFLATE);
}
function Deflate(opts) {
if (!(this instanceof Deflate))
return new Deflate(opts);
Zlib.call(this, opts, constants.DEFLATE);
}
inherits(Deflate, Zlib);
class Inflate extends Zlib {
constructor(opts) {
super(opts, constants.INFLATE);
}
function Inflate(opts) {
if (!(this instanceof Inflate))
return new Inflate(opts);
Zlib.call(this, opts, constants.INFLATE);
}
inherits(Inflate, Zlib);
class Gzip extends Zlib {
constructor(opts) {
super(opts, constants.GZIP);
}
function Gzip(opts) {
if (!(this instanceof Gzip))
return new Gzip(opts);
Zlib.call(this, opts, constants.GZIP);
}
inherits(Gzip, Zlib);
class Gunzip extends Zlib {
constructor(opts) {
super(opts, constants.GUNZIP);
}
function Gunzip(opts) {
if (!(this instanceof Gunzip))
return new Gunzip(opts);
Zlib.call(this, opts, constants.GUNZIP);
}
inherits(Gunzip, Zlib);
class DeflateRaw extends Zlib {
constructor(opts) {
super(opts, constants.DEFLATERAW);
}
function DeflateRaw(opts) {
if (!(this instanceof DeflateRaw))
return new DeflateRaw(opts);
Zlib.call(this, opts, constants.DEFLATERAW);
}
inherits(DeflateRaw, Zlib);
class InflateRaw extends Zlib {
constructor(opts) {
super(opts, constants.INFLATERAW);
}
function InflateRaw(opts) {
if (!(this instanceof InflateRaw))
return new InflateRaw(opts);
Zlib.call(this, opts, constants.INFLATERAW);
}
inherits(InflateRaw, Zlib);
class Unzip extends Zlib {
constructor(opts) {
super(opts, constants.UNZIP);
}
function Unzip(opts) {
if (!(this instanceof Unzip))
return new Unzip(opts);
Zlib.call(this, opts, constants.UNZIP);
}
inherits(Unzip, Zlib);
function createConvenienceMethod(type, sync) {
if (sync) {
@ -569,13 +578,13 @@ function createProperty(type) {
}
module.exports = {
Deflate: createClassWrapper(Deflate),
Inflate: createClassWrapper(Inflate),
Gzip: createClassWrapper(Gzip),
Gunzip: createClassWrapper(Gunzip),
DeflateRaw: createClassWrapper(DeflateRaw),
InflateRaw: createClassWrapper(InflateRaw),
Unzip: createClassWrapper(Unzip),
Deflate,
Inflate,
Gzip,
Gunzip,
DeflateRaw,
InflateRaw,
Unzip,
// Convenience methods.
// compress/decompress a string or buffer in one step.

View 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();