@@ -418,16 +418,8 @@ class ZCtx : public AsyncWrap {
418418
419419 static void New(const FunctionCallbackInfo<Value>& args) {
420420 Environment* env = Environment::GetCurrent(args);
421-
422- if (args.Length() < 1 || !args[0]->IsInt32()) {
423- return env->ThrowTypeError("Bad argument");
424- }
421+ CHECK(args[0]->IsInt32());
425422 node_zlib_mode mode = static_cast<node_zlib_mode>(args[0]->Int32Value());
426-
427- if (mode < DEFLATE || mode > UNZIP) {
428- return env->ThrowTypeError("Bad argument");
429- }
430-
431423 new ZCtx(env, args.This(), mode);
432424 }
433425
@@ -476,9 +468,14 @@ class ZCtx : public AsyncWrap {
476468 memcpy(dictionary, dictionary_, dictionary_len);
477469 }
478470
479- Init(ctx, level, windowBits, memLevel, strategy, write_result,
480- write_js_callback, dictionary, dictionary_len);
471+ bool ret = Init(ctx, level, windowBits, memLevel, strategy, write_result,
472+ write_js_callback, dictionary, dictionary_len);
473+ if (!ret) goto end;
474+
481475 SetDictionary(ctx);
476+
477+ end:
478+ return args.GetReturnValue().Set(ret);
482479 }
483480
484481 static void Params(const FunctionCallbackInfo<Value>& args) {
@@ -495,7 +492,7 @@ class ZCtx : public AsyncWrap {
495492 SetDictionary(ctx);
496493 }
497494
498- static void Init(ZCtx *ctx, int level, int windowBits, int memLevel,
495+ static bool Init(ZCtx *ctx, int level, int windowBits, int memLevel,
499496 int strategy, uint32_t* write_result,
500497 Local<Function> write_js_callback, char* dictionary,
501498 size_t dictionary_len) {
@@ -561,11 +558,12 @@ class ZCtx : public AsyncWrap {
561558 ctx->dictionary_ = nullptr;
562559 }
563560 ctx->mode_ = NONE;
564- ctx->env()->ThrowError("Init error") ;
561+ return false ;
565562 }
566563
567564 ctx->write_result_ = write_result;
568565 ctx->write_js_callback_.Reset(ctx->env()->isolate(), write_js_callback);
566+ return true;
569567 }
570568
571569 static void SetDictionary(ZCtx* ctx) {
0 commit comments