From 720bf6c4db96d9f10d2467d3af42095c82d8e86c Mon Sep 17 00:00:00 2001 From: Jan Martin Date: Tue, 10 Sep 2024 11:03:32 -0700 Subject: [PATCH] fixup! zlib: add zstd support --- lib/zlib.js | 12 ++++++------ src/node_errors.h | 1 + src/node_zlib.cc | 17 +++++++++++------ test/parallel/test-zlib-zstd.js | 4 ++-- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/zlib.js b/lib/zlib.js index 61c5d6d6a8f702..a20bbeb2b30aba 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -887,12 +887,12 @@ function Zstd(opts, mode, initParamsArray, maxParam) { const pledgedSrcSize = opts?.pledgedSrcSize ?? undefined; this._writeState = new Uint32Array(2); - if (!handle.init(initParamsArray, - pledgedSrcSize, - this._writeState, - processCallback)) { - throw new ERR_ZLIB_INITIALIZATION_FAILED(); - } + handle.init( + initParamsArray, + pledgedSrcSize, + this._writeState, + processCallback + ); ReflectApply(ZlibBase, this, [opts, mode, handle, zstdDefaultOpts]); } diff --git a/src/node_errors.h b/src/node_errors.h index 171e2a2e09011c..f4f6e98814cea0 100644 --- a/src/node_errors.h +++ b/src/node_errors.h @@ -108,6 +108,7 @@ void OOMErrorHandler(const char* location, const v8::OOMDetails& details); V(ERR_VM_MODULE_CACHED_DATA_REJECTED, Error) \ V(ERR_VM_MODULE_LINK_FAILURE, Error) \ V(ERR_WASI_NOT_STARTED, Error) \ + V(ERR_ZLIB_INITIALIZATION_FAILED, Error) \ V(ERR_WORKER_INIT_FAILED, Error) \ V(ERR_PROTO_ACCESS, Error) diff --git a/src/node_zlib.cc b/src/node_zlib.cc index 8bb2cd1a39e812..dd3a9a99dd024f 100644 --- a/src/node_zlib.cc +++ b/src/node_zlib.cc @@ -25,6 +25,7 @@ #include "async_wrap-inl.h" #include "env-inl.h" +#include "node_errors.h" #include "node_external_reference.h" #include "threadpoolwork-inl.h" #include "util-inl.h" @@ -386,6 +387,8 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork { CHECK_EQ(unreported_allocations_, 0); } + Environment* env() const { return this->ThreadPoolWork::env(); } + void Close() { if (write_in_progress_) { pending_close_ = true; @@ -881,11 +884,13 @@ class ZstdStream final : public CompressionStream { if (args[1]->IsNumber()) { int64_t signed_pledged_src_size; if (!args[1]->IntegerValue(context).To(&signed_pledged_src_size)) { - args.GetReturnValue().Set(false); + THROW_ERR_INVALID_ARG_VALUE(wrap->env(), + "pledgedSrcSize should be an integer"); return; } if (signed_pledged_src_size < 0) { - args.GetReturnValue().Set(false); + THROW_ERR_INVALID_ARG_VALUE(wrap->env(), + "pledgedSrcSize may not be negative"); return; } pledged_src_size = signed_pledged_src_size; @@ -895,7 +900,8 @@ class ZstdStream final : public CompressionStream { CompressionError err = wrap->context()->Init(pledged_src_size); if (err.IsError()) { wrap->EmitError(err); - args.GetReturnValue().Set(false); + THROW_ERR_ZLIB_INITIALIZATION_FAILED(wrap->env(), + err.message); return; } @@ -909,12 +915,11 @@ class ZstdStream final : public CompressionStream { CompressionError err = wrap->context()->SetParameter(i, data[i]); if (err.IsError()) { wrap->EmitError(err); - args.GetReturnValue().Set(false); + THROW_ERR_ZLIB_INITIALIZATION_FAILED(wrap->env(), + err.message); return; } } - - args.GetReturnValue().Set(true); } static void Params(const FunctionCallbackInfo& args) { diff --git a/test/parallel/test-zlib-zstd.js b/test/parallel/test-zlib-zstd.js index 9aad22b206e196..194ed98bf940ab 100644 --- a/test/parallel/test-zlib-zstd.js +++ b/test/parallel/test-zlib-zstd.js @@ -68,7 +68,7 @@ const sampleBuffer = fixtures.readSync('/pss-vectors.json'); }, { code: 'ERR_ZLIB_INITIALIZATION_FAILED', name: 'Error', - message: 'Initialization failed' + message: 'Setting parameter failed' }); // Test that setting out-of-bounds option values or keys fails. @@ -108,7 +108,7 @@ const sampleBuffer = fixtures.readSync('/pss-vectors.json'); }, { code: 'ERR_ZLIB_INITIALIZATION_FAILED', name: 'Error', - message: 'Initialization failed' + message: 'Setting parameter failed' }); }