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
121
lib/zlib.js
121
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,10 +169,9 @@ 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 || {};
|
||||||
super(opts);
|
Transform.call(this, opts);
|
||||||
|
|
||||||
this.bytesRead = 0;
|
this.bytesRead = 0;
|
||||||
|
|
||||||
@ -265,12 +263,17 @@ class Zlib extends Transform {
|
|||||||
|
|
||||||
this.once('end', this.close);
|
this.once('end', this.close);
|
||||||
}
|
}
|
||||||
|
inherits(Zlib, Transform);
|
||||||
|
|
||||||
get _closed() {
|
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);
|
||||||
@ -284,20 +287,20 @@ class Zlib extends Transform {
|
|||||||
} else {
|
} else {
|
||||||
process.nextTick(callback);
|
process.nextTick(callback);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
reset() {
|
Zlib.prototype.reset = function reset() {
|
||||||
assert(this._handle, 'zlib binding closed');
|
assert(this._handle, 'zlib binding closed');
|
||||||
return this._handle.reset();
|
return this._handle.reset();
|
||||||
}
|
};
|
||||||
|
|
||||||
// This is the _flush function called by the transform class,
|
// This is the _flush function called by the transform class,
|
||||||
// internally, when the last chunk has been written.
|
// internally, when the last chunk has been written.
|
||||||
_flush(callback) {
|
Zlib.prototype._flush = function _flush(callback) {
|
||||||
this._transform(Buffer.alloc(0), '', callback);
|
this._transform(Buffer.alloc(0), '', callback);
|
||||||
}
|
};
|
||||||
|
|
||||||
flush(kind, callback) {
|
Zlib.prototype.flush = function flush(kind, callback) {
|
||||||
var ws = this._writableState;
|
var ws = this._writableState;
|
||||||
|
|
||||||
if (typeof kind === 'function' || (kind === undefined && !callback)) {
|
if (typeof kind === 'function' || (kind === undefined && !callback)) {
|
||||||
@ -320,14 +323,14 @@ class Zlib extends Transform {
|
|||||||
this._flushFlag = kind;
|
this._flushFlag = kind;
|
||||||
this.write(Buffer.alloc(0), '', callback);
|
this.write(Buffer.alloc(0), '', callback);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
close(callback) {
|
Zlib.prototype.close = function close(callback) {
|
||||||
_close(this, callback);
|
_close(this, callback);
|
||||||
process.nextTick(emitCloseNT, this);
|
process.nextTick(emitCloseNT, this);
|
||||||
}
|
};
|
||||||
|
|
||||||
_transform(chunk, encoding, cb) {
|
Zlib.prototype._transform = function _transform(chunk, encoding, cb) {
|
||||||
var flushFlag;
|
var flushFlag;
|
||||||
var ws = this._writableState;
|
var ws = this._writableState;
|
||||||
var ending = ws.ending || ws.ended;
|
var ending = ws.ending || ws.ended;
|
||||||
@ -356,9 +359,9 @@ class Zlib extends Transform {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this._processChunk(chunk, flushFlag, cb);
|
this._processChunk(chunk, flushFlag, cb);
|
||||||
}
|
};
|
||||||
|
|
||||||
_processChunk(chunk, flushFlag, cb) {
|
Zlib.prototype._processChunk = function _processChunk(chunk, flushFlag, cb) {
|
||||||
var availInBefore = chunk && chunk.byteLength;
|
var availInBefore = chunk && chunk.byteLength;
|
||||||
var availOutBefore = this._chunkSize - this._offset;
|
var availOutBefore = this._chunkSize - this._offset;
|
||||||
var inOff = 0;
|
var inOff = 0;
|
||||||
@ -481,8 +484,7 @@ class Zlib extends Transform {
|
|||||||
// finished with the chunk.
|
// finished with the chunk.
|
||||||
cb();
|
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