zlib: migrate to internal/errors

PR-URL: https://github.com/nodejs/node/pull/15618
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
This commit is contained in:
James M Snell 2017-09-25 16:48:57 -07:00
parent 1f8d527e94
commit 74891412f1
9 changed files with 229 additions and 99 deletions

View File

@ -609,6 +609,12 @@ also able to define their own types when using the public embedder API.
Used when attempting to perform an operation outside the bounds of a `Buffer`. Used when attempting to perform an operation outside the bounds of a `Buffer`.
<a id="ERR_BUFFER_TOO_LARGE"></a>
### ERR_BUFFER_TOO_LARGE
Used when an attempt has been made to create a `Buffer` larger than the
maximum allowed size.
<a id="ERR_CHILD_CLOSED_BEFORE_REPLY"></a> <a id="ERR_CHILD_CLOSED_BEFORE_REPLY"></a>
### ERR_CHILD_CLOSED_BEFORE_REPLY ### ERR_CHILD_CLOSED_BEFORE_REPLY
@ -879,6 +885,12 @@ Used when a given index is out of the accepted range (e.g. negative offsets).
Used generically to identify that an argument of the wrong type has been passed Used generically to identify that an argument of the wrong type has been passed
to a Node.js API. to a Node.js API.
<a id="ERR_INVALID_ARG_VALUE"></a>
### ERR_INVALID_ARG_VALUE
Used generically to identify that an invalid or unsupported value has been
passed for a given argument.
<a id="ERR_INVALID_ARRAY_LENGTH"></a> <a id="ERR_INVALID_ARRAY_LENGTH"></a>
### ERR_INVALID_ARRAY_LENGTH ### ERR_INVALID_ARRAY_LENGTH
@ -1277,6 +1289,12 @@ entry types were found.
Used when a given value is out of the accepted range. Used when a given value is out of the accepted range.
<a id="ERR_ZLIB_BINDING_CLOSED"></a>
### ERR_ZLIB_BINDING_CLOSED
Used when an attempt is made to use a `zlib` object after it has already been
closed.
[`ERR_INVALID_ARG_TYPE`]: #ERR_INVALID_ARG_TYPE [`ERR_INVALID_ARG_TYPE`]: #ERR_INVALID_ARG_TYPE
[`subprocess.kill()`]: child_process.html#child_process_subprocess_kill_signal [`subprocess.kill()`]: child_process.html#child_process_subprocess_kill_signal
[`subprocess.send()`]: child_process.html#child_process_subprocess_send_message_sendhandle_options_callback [`subprocess.send()`]: child_process.html#child_process_subprocess_send_message_sendhandle_options_callback

View File

@ -11,6 +11,10 @@
const kCode = Symbol('code'); const kCode = Symbol('code');
const messages = new Map(); const messages = new Map();
const {
kMaxLength
} = process.binding('buffer');
// Lazily loaded // Lazily loaded
var util = null; var util = null;
@ -121,6 +125,8 @@ E('ERR_ASSERTION', '%s');
E('ERR_ASYNC_CALLBACK', (name) => `${name} must be a function`); E('ERR_ASYNC_CALLBACK', (name) => `${name} must be a function`);
E('ERR_ASYNC_TYPE', (s) => `Invalid name for async "type": ${s}`); E('ERR_ASYNC_TYPE', (s) => `Invalid name for async "type": ${s}`);
E('ERR_BUFFER_OUT_OF_BOUNDS', bufferOutOfBounds); E('ERR_BUFFER_OUT_OF_BOUNDS', bufferOutOfBounds);
E('ERR_BUFFER_TOO_LARGE',
`Cannot create a Buffer larger than 0x${kMaxLength.toString(16)} bytes`);
E('ERR_CHILD_CLOSED_BEFORE_REPLY', 'Child closed before reply received'); E('ERR_CHILD_CLOSED_BEFORE_REPLY', 'Child closed before reply received');
E('ERR_CONSOLE_WRITABLE_STREAM', E('ERR_CONSOLE_WRITABLE_STREAM',
'Console expects a writable stream instance for %s'); 'Console expects a writable stream instance for %s');
@ -206,6 +212,10 @@ E('ERR_HTTP_TRAILER_INVALID',
'Trailers are invalid with this transfer encoding'); 'Trailers are invalid with this transfer encoding');
E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range'); E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range');
E('ERR_INVALID_ARG_TYPE', invalidArgType); E('ERR_INVALID_ARG_TYPE', invalidArgType);
E('ERR_INVALID_ARG_VALUE',
(name, value) => {
return `The value "${String(value)}" is invalid for argument "${name}"`;
});
E('ERR_INVALID_ARRAY_LENGTH', E('ERR_INVALID_ARRAY_LENGTH',
(name, len, actual) => { (name, len, actual) => {
internalAssert(typeof actual === 'number', 'actual must be a number'); internalAssert(typeof actual === 'number', 'actual must be a number');
@ -303,6 +313,7 @@ E('ERR_VALID_PERFORMANCE_ENTRY_TYPE',
E('ERR_VALUE_OUT_OF_RANGE', (start, end, value) => { E('ERR_VALUE_OUT_OF_RANGE', (start, end, value) => {
return `The value of "${start}" must be ${end}. Received "${value}"`; return `The value of "${start}" must be ${end}. Received "${value}"`;
}); });
E('ERR_ZLIB_BINDING_CLOSED', 'zlib binding closed');
function invalidArgType(name, expected, actual) { function invalidArgType(name, expected, actual) {
internalAssert(name, 'name is required'); internalAssert(name, 'name is required');

View File

@ -21,15 +21,16 @@
'use strict'; 'use strict';
const Buffer = require('buffer').Buffer; const errors = require('internal/errors');
const Transform = require('_stream_transform'); const Transform = require('_stream_transform');
const { _extend } = require('util'); const { _extend } = require('util');
const { isArrayBufferView } = require('internal/util/types'); const { isArrayBufferView } = require('internal/util/types');
const binding = process.binding('zlib'); const binding = process.binding('zlib');
const assert = require('assert').ok; const assert = require('assert').ok;
const kMaxLength = require('buffer').kMaxLength; const {
const kRangeErrorMessage = 'Cannot create final Buffer. It would be larger ' + Buffer,
`than 0x${kMaxLength.toString(16)} bytes`; kMaxLength
} = require('buffer');
const constants = process.binding('constants').zlib; const constants = process.binding('constants').zlib;
const { const {
@ -93,7 +94,7 @@ function zlibBufferOnEnd() {
var buf; var buf;
var err; var err;
if (this.nread >= kMaxLength) { if (this.nread >= kMaxLength) {
err = new RangeError(kRangeErrorMessage); err = new errors.RangeError('ERR_BUFFER_TOO_LARGE');
} else { } else {
var bufs = this.buffers; var bufs = this.buffers;
buf = (bufs.length === 1 ? bufs[0] : Buffer.concat(bufs, this.nread)); buf = (bufs.length === 1 ? bufs[0] : Buffer.concat(bufs, this.nread));
@ -111,8 +112,9 @@ function zlibBufferSync(engine, buffer) {
if (typeof buffer === 'string') { if (typeof buffer === 'string') {
buffer = Buffer.from(buffer); buffer = Buffer.from(buffer);
} else if (!isArrayBufferView(buffer)) { } else if (!isArrayBufferView(buffer)) {
throw new TypeError('"buffer" argument must be a string, Buffer, ' + throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'TypedArray, or DataView'); 'buffer',
['string', 'Buffer', 'TypedArray', 'DataView']);
} }
buffer = processChunkSync(engine, buffer, engine._finishFlushFlag); buffer = processChunkSync(engine, buffer, engine._finishFlushFlag);
if (engine._info) if (engine._info)
@ -128,7 +130,7 @@ function zlibOnError(message, errno) {
_close(self); _close(self);
self._hadError = true; self._hadError = true;
var error = new Error(message); const error = new Error(message);
error.errno = errno; error.errno = errno;
error.code = codes[errno]; error.code = codes[errno];
self.emit('error', error); self.emit('error', error);
@ -163,7 +165,9 @@ function Zlib(opts, mode) {
chunkSize = opts.chunkSize; chunkSize = opts.chunkSize;
if (chunkSize !== undefined && chunkSize === chunkSize) { if (chunkSize !== undefined && chunkSize === chunkSize) {
if (chunkSize < Z_MIN_CHUNK || !Number.isFinite(chunkSize)) if (chunkSize < Z_MIN_CHUNK || !Number.isFinite(chunkSize))
throw new RangeError('Invalid chunk size: ' + chunkSize); throw new errors.RangeError('ERR_INVALID_OPT_VALUE',
'chunkSize',
chunkSize);
} else { } else {
chunkSize = Z_DEFAULT_CHUNK; chunkSize = Z_DEFAULT_CHUNK;
} }
@ -171,7 +175,7 @@ function Zlib(opts, mode) {
flush = opts.flush; flush = opts.flush;
if (flush !== undefined && flush === flush) { if (flush !== undefined && flush === flush) {
if (flush < Z_NO_FLUSH || flush > Z_BLOCK || !Number.isFinite(flush)) if (flush < Z_NO_FLUSH || flush > Z_BLOCK || !Number.isFinite(flush))
throw new RangeError('Invalid flush flag: ' + flush); throw new errors.RangeError('ERR_INVALID_OPT_VALUE', 'flush', flush);
} else { } else {
flush = Z_NO_FLUSH; flush = Z_NO_FLUSH;
} }
@ -180,7 +184,9 @@ function Zlib(opts, mode) {
if (finishFlush !== undefined && finishFlush === finishFlush) { if (finishFlush !== undefined && finishFlush === finishFlush) {
if (finishFlush < Z_NO_FLUSH || finishFlush > Z_BLOCK || if (finishFlush < Z_NO_FLUSH || finishFlush > Z_BLOCK ||
!Number.isFinite(finishFlush)) { !Number.isFinite(finishFlush)) {
throw new RangeError('Invalid flush flag: ' + finishFlush); throw new errors.RangeError('ERR_INVALID_OPT_VALUE',
'finishFlush',
finishFlush);
} }
} else { } else {
finishFlush = Z_FINISH; finishFlush = Z_FINISH;
@ -190,7 +196,9 @@ function Zlib(opts, mode) {
if (windowBits !== undefined && windowBits === windowBits) { if (windowBits !== undefined && windowBits === windowBits) {
if (windowBits < Z_MIN_WINDOWBITS || windowBits > Z_MAX_WINDOWBITS || if (windowBits < Z_MIN_WINDOWBITS || windowBits > Z_MAX_WINDOWBITS ||
!Number.isFinite(windowBits)) { !Number.isFinite(windowBits)) {
throw new RangeError('Invalid windowBits: ' + windowBits); throw new errors.RangeError('ERR_INVALID_OPT_VALUE',
'windowBits',
windowBits);
} }
} else { } else {
windowBits = Z_DEFAULT_WINDOWBITS; windowBits = Z_DEFAULT_WINDOWBITS;
@ -200,7 +208,8 @@ function Zlib(opts, mode) {
if (level !== undefined && level === level) { if (level !== undefined && level === level) {
if (level < Z_MIN_LEVEL || level > Z_MAX_LEVEL || if (level < Z_MIN_LEVEL || level > Z_MAX_LEVEL ||
!Number.isFinite(level)) { !Number.isFinite(level)) {
throw new RangeError('Invalid compression level: ' + level); throw new errors.RangeError('ERR_INVALID_OPT_VALUE',
'level', level);
} }
} else { } else {
level = Z_DEFAULT_COMPRESSION; level = Z_DEFAULT_COMPRESSION;
@ -210,7 +219,8 @@ function Zlib(opts, mode) {
if (memLevel !== undefined && memLevel === memLevel) { if (memLevel !== undefined && memLevel === memLevel) {
if (memLevel < Z_MIN_MEMLEVEL || memLevel > Z_MAX_MEMLEVEL || if (memLevel < Z_MIN_MEMLEVEL || memLevel > Z_MAX_MEMLEVEL ||
!Number.isFinite(memLevel)) { !Number.isFinite(memLevel)) {
throw new RangeError('Invalid memLevel: ' + memLevel); throw new errors.RangeError('ERR_INVALID_OPT_VALUE',
'memLevel', memLevel);
} }
} else { } else {
memLevel = Z_DEFAULT_MEMLEVEL; memLevel = Z_DEFAULT_MEMLEVEL;
@ -220,7 +230,8 @@ function Zlib(opts, mode) {
if (strategy !== undefined && strategy === strategy) { if (strategy !== undefined && strategy === strategy) {
if (strategy < Z_DEFAULT_STRATEGY || strategy > Z_FIXED || if (strategy < Z_DEFAULT_STRATEGY || strategy > Z_FIXED ||
!Number.isFinite(strategy)) { !Number.isFinite(strategy)) {
throw new TypeError('Invalid strategy: ' + strategy); throw new errors.TypeError('ERR_INVALID_OPT_VALUE',
'strategy', strategy);
} }
} else { } else {
strategy = Z_DEFAULT_STRATEGY; strategy = Z_DEFAULT_STRATEGY;
@ -228,8 +239,9 @@ function Zlib(opts, mode) {
dictionary = opts.dictionary; dictionary = opts.dictionary;
if (dictionary !== undefined && !isArrayBufferView(dictionary)) { if (dictionary !== undefined && !isArrayBufferView(dictionary)) {
throw new TypeError( throw new errors.TypeError('ERR_INVALID_OPT_VALUE',
'Invalid dictionary: it should be a Buffer, TypedArray, or DataView'); 'dictionary',
dictionary);
} }
if (opts.encoding || opts.objectMode || opts.writableObjectMode) { if (opts.encoding || opts.objectMode || opts.writableObjectMode) {
@ -273,12 +285,12 @@ Object.defineProperty(Zlib.prototype, '_closed', {
Zlib.prototype.params = function params(level, strategy, callback) { Zlib.prototype.params = function params(level, strategy, callback) {
if (level < Z_MIN_LEVEL || level > Z_MAX_LEVEL) if (level < Z_MIN_LEVEL || level > Z_MAX_LEVEL)
throw new RangeError('Invalid compression level: ' + level); throw new errors.RangeError('ERR_INVALID_ARG_VALUE', 'level', level);
if (strategy !== undefined && if (strategy !== undefined &&
(strategy < Z_DEFAULT_STRATEGY || strategy > Z_FIXED || (strategy < Z_DEFAULT_STRATEGY || strategy > Z_FIXED ||
!Number.isFinite(strategy))) { !Number.isFinite(strategy))) {
throw new TypeError('Invalid strategy: ' + strategy); throw new errors.TypeError('ERR_INVALID_ARG_VALUE', 'strategy', strategy);
} }
if (this._level !== level || this._strategy !== strategy) { if (this._level !== level || this._strategy !== strategy) {
@ -455,7 +467,7 @@ function processChunkSync(self, chunk, flushFlag) {
if (nread >= kMaxLength) { if (nread >= kMaxLength) {
_close(self); _close(self);
throw new RangeError(kRangeErrorMessage); throw new errors.RangeError('ERR_BUFFER_TOO_LARGE');
} }
_close(self); _close(self);
@ -466,7 +478,7 @@ function processChunkSync(self, chunk, flushFlag) {
function processChunk(self, chunk, flushFlag, cb) { function processChunk(self, chunk, flushFlag, cb) {
var handle = self._handle; var handle = self._handle;
if (!handle) if (!handle)
return cb(new Error('zlib binding closed')); return cb(new errors.Error('ERR_ZLIB_BINDING_CLOSED'));
handle.buffer = chunk; handle.buffer = chunk;
handle.cb = cb; handle.cb = cb;

View File

@ -284,3 +284,20 @@ assert.strictEqual(
assert.strictEqual( assert.strictEqual(
errors.message('ERR_INVALID_ASYNC_ID', ['asyncId', undefined]), errors.message('ERR_INVALID_ASYNC_ID', ['asyncId', undefined]),
'Invalid asyncId value: undefined'); 'Invalid asyncId value: undefined');
{
const { kMaxLength } = process.binding('buffer');
const error = new errors.Error('ERR_BUFFER_TOO_LARGE');
assert.strictEqual(
error.message,
`Cannot create a Buffer larger than 0x${kMaxLength.toString(16)} bytes`
);
}
{
const error = new errors.Error('ERR_INVALID_ARG_VALUE', 'foo', 'bar');
assert.strictEqual(
error.message,
'The value "bar" is invalid for argument "foo"'
);
}

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
require('../common'); const common = require('../common');
const zlib = require('zlib'); const zlib = require('zlib');
const assert = require('assert'); const assert = require('assert');
@ -13,56 +13,83 @@ assert.ok(zlib.DeflateRaw() instanceof zlib.DeflateRaw);
assert.ok(new zlib.DeflateRaw() instanceof zlib.DeflateRaw); assert.ok(new zlib.DeflateRaw() instanceof zlib.DeflateRaw);
// Throws if `opts.chunkSize` is invalid // Throws if `opts.chunkSize` is invalid
assert.throws( common.expectsError(
() => { new zlib.Deflate({ chunkSize: -Infinity }); }, () => new zlib.Deflate({ chunkSize: -Infinity }),
/^RangeError: Invalid chunk size: -Infinity$/ {
code: 'ERR_INVALID_OPT_VALUE',
type: RangeError
}
); );
// Confirm that maximum chunk size cannot be exceeded because it is `Infinity`. // Confirm that maximum chunk size cannot be exceeded because it is `Infinity`.
assert.strictEqual(zlib.constants.Z_MAX_CHUNK, Infinity); assert.strictEqual(zlib.constants.Z_MAX_CHUNK, Infinity);
// Throws if `opts.windowBits` is invalid // Throws if `opts.windowBits` is invalid
assert.throws( common.expectsError(
() => { new zlib.Deflate({ windowBits: -Infinity }); }, () => new zlib.Deflate({ windowBits: -Infinity }),
/^RangeError: Invalid windowBits: -Infinity$/ {
code: 'ERR_INVALID_OPT_VALUE',
type: RangeError
}
); );
assert.throws( common.expectsError(
() => { new zlib.Deflate({ windowBits: Infinity }); }, () => new zlib.Deflate({ windowBits: Infinity }),
/^RangeError: Invalid windowBits: Infinity$/ {
code: 'ERR_INVALID_OPT_VALUE',
type: RangeError
}
); );
// Throws if `opts.level` is invalid // Throws if `opts.level` is invalid
assert.throws( common.expectsError(
() => { new zlib.Deflate({ level: -Infinity }); }, () => new zlib.Deflate({ level: -Infinity }),
/^RangeError: Invalid compression level: -Infinity$/ {
code: 'ERR_INVALID_OPT_VALUE',
type: RangeError
}
); );
assert.throws( common.expectsError(
() => { new zlib.Deflate({ level: Infinity }); }, () => new zlib.Deflate({ level: Infinity }),
/^RangeError: Invalid compression level: Infinity$/ {
code: 'ERR_INVALID_OPT_VALUE',
type: RangeError
}
); );
// Throws a RangeError if `level` invalid in `Deflate.prototype.params()` // Throws a RangeError if `level` invalid in `Deflate.prototype.params()`
assert.throws( common.expectsError(
() => { new zlib.Deflate().params(-Infinity); }, () => new zlib.Deflate().params(-Infinity),
/^RangeError: Invalid compression level: -Infinity$/ {
code: 'ERR_INVALID_ARG_VALUE',
type: RangeError
}
); );
assert.throws( common.expectsError(
() => { new zlib.Deflate().params(Infinity); }, () => new zlib.Deflate().params(Infinity),
/^RangeError: Invalid compression level: Infinity$/ {
code: 'ERR_INVALID_ARG_VALUE',
type: RangeError
}
); );
// Throws if `opts.memLevel` is invalid // Throws if `opts.memLevel` is invalid
assert.throws( common.expectsError(
() => { new zlib.Deflate({ memLevel: -Infinity }); }, () => new zlib.Deflate({ memLevel: -Infinity }),
/^RangeError: Invalid memLevel: -Infinity$/ {
code: 'ERR_INVALID_OPT_VALUE',
type: RangeError
}
); );
assert.throws( common.expectsError(
() => { new zlib.Deflate({ memLevel: Infinity }); }, () => new zlib.Deflate({ memLevel: Infinity }),
/^RangeError: Invalid memLevel: Infinity$/ {
code: 'ERR_INVALID_OPT_VALUE',
type: RangeError
}
); );
// Does not throw if opts.strategy is valid // Does not throw if opts.strategy is valid
@ -87,25 +114,37 @@ assert.doesNotThrow(
); );
// Throws if opt.strategy is the wrong type. // Throws if opt.strategy is the wrong type.
assert.throws( common.expectsError(
() => { new zlib.Deflate({ strategy: String(zlib.constants.Z_RLE) }); }, () => new zlib.Deflate({ strategy: String(zlib.constants.Z_RLE) }),
/^TypeError: Invalid strategy: 3$/ {
code: 'ERR_INVALID_OPT_VALUE',
type: TypeError
}
); );
// Throws if opts.strategy is invalid // Throws if opts.strategy is invalid
assert.throws( common.expectsError(
() => { new zlib.Deflate({ strategy: 'this is a bogus strategy' }); }, () => new zlib.Deflate({ strategy: 'this is a bogus strategy' }),
/^TypeError: Invalid strategy: this is a bogus strategy$/ {
code: 'ERR_INVALID_OPT_VALUE',
type: TypeError
}
); );
// Throws TypeError if `strategy` is invalid in `Deflate.prototype.params()` // Throws TypeError if `strategy` is invalid in `Deflate.prototype.params()`
assert.throws( common.expectsError(
() => { new zlib.Deflate().params(0, 'I am an invalid strategy'); }, () => new zlib.Deflate().params(0, 'I am an invalid strategy'),
/^TypeError: Invalid strategy: I am an invalid strategy$/ {
code: 'ERR_INVALID_ARG_VALUE',
type: TypeError
}
); );
// Throws if opts.dictionary is not a Buffer // Throws if opts.dictionary is not a Buffer
assert.throws( common.expectsError(
() => { new zlib.Deflate({ dictionary: 'not a buffer' }); }, () => new zlib.Deflate({ dictionary: 'not a buffer' }),
/^TypeError: Invalid dictionary: it should be a Buffer, TypedArray, or DataView$/ {
code: 'ERR_INVALID_OPT_VALUE',
type: TypeError
}
); );

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const zlib = require('zlib'); const zlib = require('zlib');
@ -18,17 +18,29 @@ if (!/^1\.2\.[0-8]$/.test(process.versions.zlib)) {
// Regression tests for bugs in the validation logic. // Regression tests for bugs in the validation logic.
assert.throws(() => { common.expectsError(
zlib.createGzip({ chunkSize: 0 }); () => zlib.createGzip({ chunkSize: 0 }),
}, /^RangeError: Invalid chunk size: 0$/); {
code: 'ERR_INVALID_OPT_VALUE',
type: RangeError
}
);
assert.throws(() => { common.expectsError(
zlib.createGzip({ windowBits: 0 }); () => zlib.createGzip({ windowBits: 0 }),
}, /^RangeError: Invalid windowBits: 0$/); {
code: 'ERR_INVALID_OPT_VALUE',
type: RangeError
}
);
assert.throws(() => { common.expectsError(
zlib.createGzip({ memLevel: 0 }); () => zlib.createGzip({ memLevel: 0 }),
}, /^RangeError: Invalid memLevel: 0$/); {
code: 'ERR_INVALID_OPT_VALUE',
type: RangeError
}
);
{ {
const stream = zlib.createGzip({ level: NaN }); const stream = zlib.createGzip({ level: NaN });

View File

@ -1,5 +1,5 @@
'use strict'; 'use strict';
require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const zlib = require('zlib'); const zlib = require('zlib');
@ -7,22 +7,38 @@ assert.doesNotThrow(() => {
zlib.createGzip({ flush: zlib.constants.Z_SYNC_FLUSH }); zlib.createGzip({ flush: zlib.constants.Z_SYNC_FLUSH });
}); });
assert.throws(() => { common.expectsError(
zlib.createGzip({ flush: 'foobar' }); () => zlib.createGzip({ flush: 'foobar' }),
}, /^RangeError: Invalid flush flag: foobar$/); {
code: 'ERR_INVALID_OPT_VALUE',
type: RangeError
}
);
assert.throws(() => { common.expectsError(
zlib.createGzip({ flush: 10000 }); () => zlib.createGzip({ flush: 10000 }),
}, /^RangeError: Invalid flush flag: 10000$/); {
code: 'ERR_INVALID_OPT_VALUE',
type: RangeError
}
);
assert.doesNotThrow(() => { assert.doesNotThrow(() => {
zlib.createGzip({ finishFlush: zlib.constants.Z_SYNC_FLUSH }); zlib.createGzip({ finishFlush: zlib.constants.Z_SYNC_FLUSH });
}); });
assert.throws(() => { common.expectsError(
zlib.createGzip({ finishFlush: 'foobar' }); () => zlib.createGzip({ finishFlush: 'foobar' }),
}, /^RangeError: Invalid flush flag: foobar$/); {
code: 'ERR_INVALID_OPT_VALUE',
type: RangeError
}
);
assert.throws(() => { common.expectsError(
zlib.createGzip({ finishFlush: 10000 }); () => zlib.createGzip({ finishFlush: 10000 }),
}, /^RangeError: Invalid flush flag: 10000$/); {
code: 'ERR_INVALID_OPT_VALUE',
type: RangeError
}
);

View File

@ -3,18 +3,17 @@
// Check the error condition testing for passing something other than a string // Check the error condition testing for passing something other than a string
// or buffer. // or buffer.
require('../common'); const common = require('../common');
const assert = require('assert');
const zlib = require('zlib'); const zlib = require('zlib');
const expected = [undefined, null, true, false, 0, 1, [1, 2, 3], { foo: 'bar' }].forEach((i) => {
/^TypeError: "buffer" argument must be a string, Buffer, TypedArray, or DataView$/; common.expectsError(
() => zlib.deflateSync(i),
assert.throws(() => { zlib.deflateSync(undefined); }, expected); {
assert.throws(() => { zlib.deflateSync(null); }, expected); code: 'ERR_INVALID_ARG_TYPE',
assert.throws(() => { zlib.deflateSync(true); }, expected); type: TypeError,
assert.throws(() => { zlib.deflateSync(false); }, expected); message: 'The "buffer" argument must be one of type string, Buffer, ' +
assert.throws(() => { zlib.deflateSync(0); }, expected); 'TypedArray, or DataView'
assert.throws(() => { zlib.deflateSync(1); }, expected); }
assert.throws(() => { zlib.deflateSync([1, 2, 3]); }, expected); );
assert.throws(() => { zlib.deflateSync({ foo: 'bar' }); }, expected); });

View File

@ -21,11 +21,17 @@
'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
const assert = require('assert');
const zlib = require('zlib'); const zlib = require('zlib');
zlib.gzip('hello', common.mustCall(function(err, out) { zlib.gzip('hello', common.mustCall(function(err, out) {
const unzip = zlib.createGunzip(); const unzip = zlib.createGunzip();
unzip.close(common.mustCall()); unzip.close(common.mustCall());
assert.throws(() => unzip.write(out), /^Error: zlib binding closed$/); common.expectsError(
() => unzip.write(out),
{
code: 'ERR_ZLIB_BINDING_CLOSED',
type: Error,
message: 'zlib binding closed'
}
);
})); }));