summaryrefslogtreecommitdiff
path: root/lib/zlib.js
diff options
context:
space:
mode:
authorJames M Snell <jasnell@gmail.com>2017-02-14 17:58:15 -0800
committerJames M Snell <jasnell@gmail.com>2017-02-17 10:37:46 -0800
commitb514bd231ed8926a0037b78e85d267a602c2e7cd (patch)
tree9a73b57ca3453f813e92e377b32650d4bd83d5ee /lib/zlib.js
parent8e69f7e38552a4c65064f2829d9ca973ad9b05ba (diff)
downloadandroid-node-v8-b514bd231ed8926a0037b78e85d267a602c2e7cd.tar.gz
android-node-v8-b514bd231ed8926a0037b78e85d267a602c2e7cd.tar.bz2
android-node-v8-b514bd231ed8926a0037b78e85d267a602c2e7cd.zip
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>
Diffstat (limited to 'lib/zlib.js')
-rw-r--r--lib/zlib.js30
1 files changed, 16 insertions, 14 deletions
diff --git a/lib/zlib.js b/lib/zlib.js
index 19328d381a..476503b17f 100644
--- a/lib/zlib.js
+++ b/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'));