Skip to content

Commit

Permalink
zlib: use RangeError/TypeError consistently
Browse files Browse the repository at this point in the history
PR-URL: #11391
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
  • Loading branch information
jasnell committed Feb 17, 2017
1 parent 8e69f7e commit b514bd2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
30 changes: 16 additions & 14 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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;
Expand All @@ -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');
}
}

Expand Down Expand Up @@ -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'));
Expand Down
20 changes: 10 additions & 10 deletions test/parallel/test-zlib-deflate-constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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()`
Expand All @@ -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
Expand All @@ -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()`
Expand All @@ -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$/
);
8 changes: 4 additions & 4 deletions test/parallel/test-zlib-flush-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ 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 });
});

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$/);

0 comments on commit b514bd2

Please sign in to comment.