Skip to content

Commit

Permalink
fixup! zlib: add zstd support
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrems committed Sep 10, 2024
1 parent 8957036 commit 720bf6c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
12 changes: 6 additions & 6 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down
1 change: 1 addition & 0 deletions src/node_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
17 changes: 11 additions & 6 deletions src/node_zlib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -881,11 +884,13 @@ class ZstdStream final : public CompressionStream<CompressionContext> {
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;
Expand All @@ -895,7 +900,8 @@ class ZstdStream final : public CompressionStream<CompressionContext> {
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;
}

Expand All @@ -909,12 +915,11 @@ class ZstdStream final : public CompressionStream<CompressionContext> {
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<Value>& args) {
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-zlib-zstd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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'
});
}

Expand Down

0 comments on commit 720bf6c

Please sign in to comment.