Skip to content
This repository was archived by the owner on Aug 31, 2018. It is now read-only.

Commit 72792fb

Browse files
jasnellQard
authored andcommitted
zlib: finish migrating to internal/errors
PR-URL: nodejs/node#16540 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Khaidi Chu <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent b9d7269 commit 72792fb

File tree

5 files changed

+40
-18
lines changed

5 files changed

+40
-18
lines changed

doc/api/errors.md

+6
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,11 @@ Used when a given value is out of the accepted range.
14561456
Used when an attempt is made to use a `zlib` object after it has already been
14571457
closed.
14581458

1459+
<a id="ERR_ZLIB_INITIALIZATION_FAILED"></a>
1460+
### ERR_ZLIB_INITIALIZATION_FAILED
1461+
1462+
Used when creation of a [`zlib`][] object fails due to incorrect configuration.
1463+
14591464
[`--force-fips`]: cli.html#cli_force_fips
14601465
[`crypto.timingSafeEqual()`]: crypto.html#crypto_crypto_timingsafeequal_a_b
14611466
[`dgram.createSocket()`]: dgram.html#dgram_dgram_createsocket_options_callback
@@ -1494,3 +1499,4 @@ closed.
14941499
[try-catch]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
14951500
[vm]: vm.html
14961501
[WHATWG Supported Encodings]: util.html#util_whatwg_supported_encodings
1502+
[`zlib`]: zlib.html

lib/internal/errors.js

+1
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ E('ERR_WORKER_UNSERIALIZABLE_ERROR',
369369
E('ERR_WORKER_UNSUPPORTED_EXTENSION',
370370
'The worker script extension must be ".js" or ".mjs". Received "%s"');
371371
E('ERR_ZLIB_BINDING_CLOSED', 'zlib binding closed');
372+
E('ERR_ZLIB_INITIALIZATION_FAILED', 'Initialization failed');
372373

373374
function invalidArgType(name, expected, actual) {
374375
internalAssert(name, 'name is required');

lib/zlib.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ function Zlib(opts, mode) {
161161
var memLevel = Z_DEFAULT_MEMLEVEL;
162162
var strategy = Z_DEFAULT_STRATEGY;
163163
var dictionary;
164+
165+
if (typeof mode !== 'number')
166+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'number');
167+
if (mode < DEFLATE || mode > UNZIP)
168+
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'mode');
169+
164170
if (opts) {
165171
chunkSize = opts.chunkSize;
166172
if (chunkSize !== undefined && chunkSize === chunkSize) {
@@ -258,8 +264,15 @@ function Zlib(opts, mode) {
258264
this._hadError = false;
259265
this._writeState = new Uint32Array(2);
260266

261-
this._handle.init(windowBits, level, memLevel, strategy, this._writeState,
262-
processCallback, dictionary);
267+
if (!this._handle.init(windowBits,
268+
level,
269+
memLevel,
270+
strategy,
271+
this._writeState,
272+
processCallback,
273+
dictionary)) {
274+
throw new errors.Error('ERR_ZLIB_INITIALIZATION_FAILED');
275+
}
263276

264277
this._outBuffer = Buffer.allocUnsafe(chunkSize);
265278
this._outOffset = 0;

src/node_zlib.cc

+11-13
Original file line numberDiff line numberDiff line change
@@ -425,16 +425,8 @@ class ZCtx : public AsyncWrap {
425425

426426
static void New(const FunctionCallbackInfo<Value>& args) {
427427
Environment* env = Environment::GetCurrent(args);
428-
429-
if (args.Length() < 1 || !args[0]->IsInt32()) {
430-
return env->ThrowTypeError("Bad argument");
431-
}
428+
CHECK(args[0]->IsInt32());
432429
node_zlib_mode mode = static_cast<node_zlib_mode>(args[0]->Int32Value());
433-
434-
if (mode < DEFLATE || mode > UNZIP) {
435-
return env->ThrowTypeError("Bad argument");
436-
}
437-
438430
new ZCtx(env, args.This(), mode);
439431
}
440432

@@ -483,9 +475,14 @@ class ZCtx : public AsyncWrap {
483475
memcpy(dictionary, dictionary_, dictionary_len);
484476
}
485477

486-
Init(ctx, level, windowBits, memLevel, strategy, write_result,
487-
write_js_callback, dictionary, dictionary_len);
478+
bool ret = Init(ctx, level, windowBits, memLevel, strategy, write_result,
479+
write_js_callback, dictionary, dictionary_len);
480+
if (!ret) goto end;
481+
488482
SetDictionary(ctx);
483+
484+
end:
485+
return args.GetReturnValue().Set(ret);
489486
}
490487

491488
static void Params(const FunctionCallbackInfo<Value>& args) {
@@ -502,7 +499,7 @@ class ZCtx : public AsyncWrap {
502499
SetDictionary(ctx);
503500
}
504501

505-
static void Init(ZCtx *ctx, int level, int windowBits, int memLevel,
502+
static bool Init(ZCtx *ctx, int level, int windowBits, int memLevel,
506503
int strategy, uint32_t* write_result,
507504
Local<Function> write_js_callback, char* dictionary,
508505
size_t dictionary_len) {
@@ -568,11 +565,12 @@ class ZCtx : public AsyncWrap {
568565
ctx->dictionary_ = nullptr;
569566
}
570567
ctx->mode_ = NONE;
571-
ctx->env()->ThrowError("Init error");
568+
return false;
572569
}
573570

574571
ctx->write_result_ = write_result;
575572
ctx->write_js_callback_.Reset(ctx->env()->isolate(), write_js_callback);
573+
return true;
576574
}
577575

578576
static void SetDictionary(ZCtx* ctx) {

test/parallel/test-zlib-failed-init.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ const zlib = require('zlib');
1111
// no such rejection which is the reason for the version check below
1212
// (http://zlib.net/ChangeLog.txt).
1313
if (!/^1\.2\.[0-8]$/.test(process.versions.zlib)) {
14-
assert.throws(() => {
15-
zlib.createDeflateRaw({ windowBits: 8 });
16-
}, /^Error: Init error$/);
14+
common.expectsError(
15+
() => zlib.createDeflateRaw({ windowBits: 8 }),
16+
{
17+
code: 'ERR_ZLIB_INITIALIZATION_FAILED',
18+
type: Error,
19+
message: 'Initialization failed'
20+
});
1721
}
1822

1923
// Regression tests for bugs in the validation logic.

0 commit comments

Comments
 (0)