zlib: finish migrating to internal/errors
PR-URL: https://github.com/nodejs/node/pull/16540 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Khaidi Chu <i@2333.moe> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
parent
1cdcab09f2
commit
896eaf6820
@ -1474,6 +1474,11 @@ Used when a given value is out of the accepted range.
|
||||
Used when an attempt is made to use a `zlib` object after it has already been
|
||||
closed.
|
||||
|
||||
<a id="ERR_ZLIB_INITIALIZATION_FAILED"></a>
|
||||
### ERR_ZLIB_INITIALIZATION_FAILED
|
||||
|
||||
Used when creation of a [`zlib`][] object fails due to incorrect configuration.
|
||||
|
||||
[`--force-fips`]: cli.html#cli_force_fips
|
||||
[`crypto.timingSafeEqual()`]: crypto.html#crypto_crypto_timingsafeequal_a_b
|
||||
[`dgram.createSocket()`]: dgram.html#dgram_dgram_createsocket_options_callback
|
||||
@ -1515,3 +1520,4 @@ closed.
|
||||
[try-catch]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
|
||||
[vm]: vm.html
|
||||
[WHATWG Supported Encodings]: util.html#util_whatwg_supported_encodings
|
||||
[`zlib`]: zlib.html
|
||||
|
@ -364,6 +364,7 @@ E('ERR_VALUE_OUT_OF_RANGE', (start, end, value) => {
|
||||
return `The value of "${start}" must be ${end}. Received "${value}"`;
|
||||
});
|
||||
E('ERR_ZLIB_BINDING_CLOSED', 'zlib binding closed');
|
||||
E('ERR_ZLIB_INITIALIZATION_FAILED', 'Initialization failed');
|
||||
|
||||
function invalidArgType(name, expected, actual) {
|
||||
internalAssert(name, 'name is required');
|
||||
|
17
lib/zlib.js
17
lib/zlib.js
@ -161,6 +161,12 @@ function Zlib(opts, mode) {
|
||||
var memLevel = Z_DEFAULT_MEMLEVEL;
|
||||
var strategy = Z_DEFAULT_STRATEGY;
|
||||
var dictionary;
|
||||
|
||||
if (typeof mode !== 'number')
|
||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'number');
|
||||
if (mode < DEFLATE || mode > UNZIP)
|
||||
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'mode');
|
||||
|
||||
if (opts) {
|
||||
chunkSize = opts.chunkSize;
|
||||
if (chunkSize !== undefined && chunkSize === chunkSize) {
|
||||
@ -258,8 +264,15 @@ function Zlib(opts, mode) {
|
||||
this._hadError = false;
|
||||
this._writeState = new Uint32Array(2);
|
||||
|
||||
this._handle.init(windowBits, level, memLevel, strategy, this._writeState,
|
||||
processCallback, dictionary);
|
||||
if (!this._handle.init(windowBits,
|
||||
level,
|
||||
memLevel,
|
||||
strategy,
|
||||
this._writeState,
|
||||
processCallback,
|
||||
dictionary)) {
|
||||
throw new errors.Error('ERR_ZLIB_INITIALIZATION_FAILED');
|
||||
}
|
||||
|
||||
this._outBuffer = Buffer.allocUnsafe(chunkSize);
|
||||
this._outOffset = 0;
|
||||
|
@ -418,16 +418,8 @@ class ZCtx : public AsyncWrap {
|
||||
|
||||
static void New(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
|
||||
if (args.Length() < 1 || !args[0]->IsInt32()) {
|
||||
return env->ThrowTypeError("Bad argument");
|
||||
}
|
||||
CHECK(args[0]->IsInt32());
|
||||
node_zlib_mode mode = static_cast<node_zlib_mode>(args[0]->Int32Value());
|
||||
|
||||
if (mode < DEFLATE || mode > UNZIP) {
|
||||
return env->ThrowTypeError("Bad argument");
|
||||
}
|
||||
|
||||
new ZCtx(env, args.This(), mode);
|
||||
}
|
||||
|
||||
@ -476,9 +468,14 @@ class ZCtx : public AsyncWrap {
|
||||
memcpy(dictionary, dictionary_, dictionary_len);
|
||||
}
|
||||
|
||||
Init(ctx, level, windowBits, memLevel, strategy, write_result,
|
||||
write_js_callback, dictionary, dictionary_len);
|
||||
bool ret = Init(ctx, level, windowBits, memLevel, strategy, write_result,
|
||||
write_js_callback, dictionary, dictionary_len);
|
||||
if (!ret) goto end;
|
||||
|
||||
SetDictionary(ctx);
|
||||
|
||||
end:
|
||||
return args.GetReturnValue().Set(ret);
|
||||
}
|
||||
|
||||
static void Params(const FunctionCallbackInfo<Value>& args) {
|
||||
@ -495,7 +492,7 @@ class ZCtx : public AsyncWrap {
|
||||
SetDictionary(ctx);
|
||||
}
|
||||
|
||||
static void Init(ZCtx *ctx, int level, int windowBits, int memLevel,
|
||||
static bool Init(ZCtx *ctx, int level, int windowBits, int memLevel,
|
||||
int strategy, uint32_t* write_result,
|
||||
Local<Function> write_js_callback, char* dictionary,
|
||||
size_t dictionary_len) {
|
||||
@ -561,11 +558,12 @@ class ZCtx : public AsyncWrap {
|
||||
ctx->dictionary_ = nullptr;
|
||||
}
|
||||
ctx->mode_ = NONE;
|
||||
ctx->env()->ThrowError("Init error");
|
||||
return false;
|
||||
}
|
||||
|
||||
ctx->write_result_ = write_result;
|
||||
ctx->write_js_callback_.Reset(ctx->env()->isolate(), write_js_callback);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void SetDictionary(ZCtx* ctx) {
|
||||
|
@ -11,9 +11,13 @@ const zlib = require('zlib');
|
||||
// no such rejection which is the reason for the version check below
|
||||
// (http://zlib.net/ChangeLog.txt).
|
||||
if (!/^1\.2\.[0-8]$/.test(process.versions.zlib)) {
|
||||
assert.throws(() => {
|
||||
zlib.createDeflateRaw({ windowBits: 8 });
|
||||
}, /^Error: Init error$/);
|
||||
common.expectsError(
|
||||
() => zlib.createDeflateRaw({ windowBits: 8 }),
|
||||
{
|
||||
code: 'ERR_ZLIB_INITIALIZATION_FAILED',
|
||||
type: Error,
|
||||
message: 'Initialization failed'
|
||||
});
|
||||
}
|
||||
|
||||
// Regression tests for bugs in the validation logic.
|
||||
|
Loading…
x
Reference in New Issue
Block a user