zlib: use RangeError/TypeError consistently
PR-URL: https://github.com/nodejs/node/pull/11391 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
This commit is contained in:
parent
8e69f7e385
commit
b514bd231e
30
lib/zlib.js
30
lib/zlib.js
@ -31,9 +31,10 @@ for (var ck = 0; ck < ckeys.length; ck++) {
|
||||
codes[codes[ckey]] = ckey;
|
||||
}
|
||||
|
||||
function isValidFlushFlag(flag) {
|
||||
return flag >= constants.Z_NO_FLUSH &&
|
||||
flag <= constants.Z_BLOCK;
|
||||
function isInvalidFlushFlag(flag) {
|
||||
return typeof flag !== 'number' ||
|
||||
flag < constants.Z_NO_FLUSH ||
|
||||
flag > constants.Z_BLOCK;
|
||||
|
||||
// Covers: constants.Z_NO_FLUSH (0),
|
||||
// constants.Z_PARTIAL_FLUSH (1),
|
||||
@ -141,11 +142,11 @@ class Zlib extends Transform {
|
||||
this._opts = opts;
|
||||
this._chunkSize = opts.chunkSize || constants.Z_DEFAULT_CHUNK;
|
||||
|
||||
if (opts.flush && !isValidFlushFlag(opts.flush)) {
|
||||
throw new Error('Invalid flush flag: ' + opts.flush);
|
||||
if (opts.flush && isInvalidFlushFlag(opts.flush)) {
|
||||
throw new RangeError('Invalid flush flag: ' + opts.flush);
|
||||
}
|
||||
if (opts.finishFlush && !isValidFlushFlag(opts.finishFlush)) {
|
||||
throw new Error('Invalid flush flag: ' + opts.finishFlush);
|
||||
if (opts.finishFlush && isInvalidFlushFlag(opts.finishFlush)) {
|
||||
throw new RangeError('Invalid flush flag: ' + opts.finishFlush);
|
||||
}
|
||||
|
||||
this._flushFlag = opts.flush || constants.Z_NO_FLUSH;
|
||||
@ -154,37 +155,38 @@ class Zlib extends Transform {
|
||||
|
||||
if (opts.chunkSize) {
|
||||
if (opts.chunkSize < constants.Z_MIN_CHUNK) {
|
||||
throw new Error('Invalid chunk size: ' + opts.chunkSize);
|
||||
throw new RangeError('Invalid chunk size: ' + opts.chunkSize);
|
||||
}
|
||||
}
|
||||
|
||||
if (opts.windowBits) {
|
||||
if (opts.windowBits < constants.Z_MIN_WINDOWBITS ||
|
||||
opts.windowBits > constants.Z_MAX_WINDOWBITS) {
|
||||
throw new Error('Invalid windowBits: ' + opts.windowBits);
|
||||
throw new RangeError('Invalid windowBits: ' + opts.windowBits);
|
||||
}
|
||||
}
|
||||
|
||||
if (opts.level) {
|
||||
if (opts.level < constants.Z_MIN_LEVEL ||
|
||||
opts.level > constants.Z_MAX_LEVEL) {
|
||||
throw new Error('Invalid compression level: ' + opts.level);
|
||||
throw new RangeError('Invalid compression level: ' + opts.level);
|
||||
}
|
||||
}
|
||||
|
||||
if (opts.memLevel) {
|
||||
if (opts.memLevel < constants.Z_MIN_MEMLEVEL ||
|
||||
opts.memLevel > constants.Z_MAX_MEMLEVEL) {
|
||||
throw new Error('Invalid memLevel: ' + opts.memLevel);
|
||||
throw new RangeError('Invalid memLevel: ' + opts.memLevel);
|
||||
}
|
||||
}
|
||||
|
||||
if (opts.strategy && isInvalidStrategy(opts.strategy))
|
||||
throw new Error('Invalid strategy: ' + opts.strategy);
|
||||
throw new TypeError('Invalid strategy: ' + opts.strategy);
|
||||
|
||||
if (opts.dictionary) {
|
||||
if (!(opts.dictionary instanceof Buffer)) {
|
||||
throw new Error('Invalid dictionary: it should be a Buffer instance');
|
||||
throw new TypeError(
|
||||
'Invalid dictionary: it should be a Buffer instance');
|
||||
}
|
||||
}
|
||||
|
||||
@ -280,7 +282,7 @@ class Zlib extends Transform {
|
||||
var last = ending && (!chunk || ws.length === chunk.length);
|
||||
|
||||
if (chunk !== null && !(chunk instanceof Buffer))
|
||||
return cb(new Error('invalid input'));
|
||||
return cb(new TypeError('invalid input'));
|
||||
|
||||
if (!this._handle)
|
||||
return cb(new Error('zlib binding closed'));
|
||||
|
@ -15,7 +15,7 @@ assert.ok(new zlib.DeflateRaw() instanceof zlib.DeflateRaw);
|
||||
// Throws if `opts.chunkSize` is invalid
|
||||
assert.throws(
|
||||
() => { new zlib.Deflate({chunkSize: -Infinity}); },
|
||||
/^Error: Invalid chunk size: -Infinity$/
|
||||
/^RangeError: Invalid chunk size: -Infinity$/
|
||||
);
|
||||
|
||||
// Confirm that maximum chunk size cannot be exceeded because it is `Infinity`.
|
||||
@ -24,23 +24,23 @@ assert.strictEqual(zlib.constants.Z_MAX_CHUNK, Infinity);
|
||||
// Throws if `opts.windowBits` is invalid
|
||||
assert.throws(
|
||||
() => { new zlib.Deflate({windowBits: -Infinity}); },
|
||||
/^Error: Invalid windowBits: -Infinity$/
|
||||
/^RangeError: Invalid windowBits: -Infinity$/
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => { new zlib.Deflate({windowBits: Infinity}); },
|
||||
/^Error: Invalid windowBits: Infinity$/
|
||||
/^RangeError: Invalid windowBits: Infinity$/
|
||||
);
|
||||
|
||||
// Throws if `opts.level` is invalid
|
||||
assert.throws(
|
||||
() => { new zlib.Deflate({level: -Infinity}); },
|
||||
/^Error: Invalid compression level: -Infinity$/
|
||||
/^RangeError: Invalid compression level: -Infinity$/
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => { new zlib.Deflate({level: Infinity}); },
|
||||
/^Error: Invalid compression level: Infinity$/
|
||||
/^RangeError: Invalid compression level: Infinity$/
|
||||
);
|
||||
|
||||
// Throws a RangeError if `level` invalid in `Deflate.prototype.params()`
|
||||
@ -57,12 +57,12 @@ assert.throws(
|
||||
// Throws if `opts.memLevel` is invalid
|
||||
assert.throws(
|
||||
() => { new zlib.Deflate({memLevel: -Infinity}); },
|
||||
/^Error: Invalid memLevel: -Infinity$/
|
||||
/^RangeError: Invalid memLevel: -Infinity$/
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => { new zlib.Deflate({memLevel: Infinity}); },
|
||||
/^Error: Invalid memLevel: Infinity$/
|
||||
/^RangeError: Invalid memLevel: Infinity$/
|
||||
);
|
||||
|
||||
// Does not throw if opts.strategy is valid
|
||||
@ -89,13 +89,13 @@ assert.doesNotThrow(
|
||||
// Throws if opt.strategy is the wrong type.
|
||||
assert.throws(
|
||||
() => { new zlib.Deflate({strategy: '' + zlib.constants.Z_RLE }); },
|
||||
/^Error: Invalid strategy: 3$/
|
||||
/^TypeError: Invalid strategy: 3$/
|
||||
);
|
||||
|
||||
// Throws if opts.strategy is invalid
|
||||
assert.throws(
|
||||
() => { new zlib.Deflate({strategy: 'this is a bogus strategy'}); },
|
||||
/^Error: Invalid strategy: this is a bogus strategy$/
|
||||
/^TypeError: Invalid strategy: this is a bogus strategy$/
|
||||
);
|
||||
|
||||
// Throws TypeError if `strategy` is invalid in `Deflate.prototype.params()`
|
||||
@ -107,5 +107,5 @@ assert.throws(
|
||||
// Throws if opts.dictionary is not a Buffer
|
||||
assert.throws(
|
||||
() => { new zlib.Deflate({dictionary: 'not a buffer'}); },
|
||||
/^Error: Invalid dictionary: it should be a Buffer instance$/
|
||||
/^TypeError: Invalid dictionary: it should be a Buffer instance$/
|
||||
);
|
||||
|
@ -9,11 +9,11 @@ assert.doesNotThrow(() => {
|
||||
|
||||
assert.throws(() => {
|
||||
zlib.createGzip({ flush: 'foobar' });
|
||||
}, /Invalid flush flag: foobar/);
|
||||
}, /^RangeError: Invalid flush flag: foobar$/);
|
||||
|
||||
assert.throws(() => {
|
||||
zlib.createGzip({ flush: 10000 });
|
||||
}, /Invalid flush flag: 10000/);
|
||||
}, /^RangeError: Invalid flush flag: 10000$/);
|
||||
|
||||
assert.doesNotThrow(() => {
|
||||
zlib.createGzip({ finishFlush: zlib.constants.Z_SYNC_FLUSH });
|
||||
@ -21,8 +21,8 @@ assert.doesNotThrow(() => {
|
||||
|
||||
assert.throws(() => {
|
||||
zlib.createGzip({ finishFlush: 'foobar' });
|
||||
}, /Invalid flush flag: foobar/);
|
||||
}, /^RangeError: Invalid flush flag: foobar$/);
|
||||
|
||||
assert.throws(() => {
|
||||
zlib.createGzip({ finishFlush: 10000 });
|
||||
}, /Invalid flush flag: 10000/);
|
||||
}, /^RangeError: Invalid flush flag: 10000$/);
|
||||
|
Loading…
x
Reference in New Issue
Block a user