Skip to content

Commit

Permalink
zlib: throw brotli initialization error from c++
Browse files Browse the repository at this point in the history
PR-URL: #54698
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
anonrig authored and targos committed Oct 4, 2024
1 parent a679669 commit a9fa2da
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
1 change: 0 additions & 1 deletion lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1889,4 +1889,3 @@ E('ERR_WORKER_UNSERIALIZABLE_ERROR',
'Serializing an uncaught exception failed', Error);
E('ERR_WORKER_UNSUPPORTED_OPERATION',
'%s is not supported in workers', TypeError);
E('ERR_ZLIB_INITIALIZATION_FAILED', 'Initialization failed', Error);
10 changes: 1 addition & 9 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const {
ERR_BUFFER_TOO_LARGE,
ERR_INVALID_ARG_TYPE,
ERR_OUT_OF_RANGE,
ERR_ZLIB_INITIALIZATION_FAILED,
},
genericNodeError,
} = require('internal/errors');
Expand Down Expand Up @@ -815,14 +814,7 @@ function Brotli(opts, mode) {
new binding.BrotliDecoder(mode) : new binding.BrotliEncoder(mode);

this._writeState = new Uint32Array(2);
// TODO(addaleax): Sometimes we generate better error codes in C++ land,
// e.g. ERR_BROTLI_PARAM_SET_FAILED -- it's hard to access them with
// the current bindings setup, though.
if (!handle.init(brotliInitParamsArray,
this._writeState,
processCallback)) {
throw new ERR_ZLIB_INITIALIZATION_FAILED();
}
handle.init(brotliInitParamsArray, this._writeState, processCallback);

ReflectApply(ZlibBase, this, [opts, mode, handle, brotliDefaultOpts]);
}
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
14 changes: 10 additions & 4 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 @@ -271,6 +272,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 @@ -694,7 +697,11 @@ class BrotliCompressionStream final :
static_cast<CompressionStream<CompressionContext>*>(wrap));
if (err.IsError()) {
wrap->EmitError(err);
args.GetReturnValue().Set(false);
// TODO(addaleax): Sometimes we generate better error codes in C++ land,
// e.g. ERR_BROTLI_PARAM_SET_FAILED -- it's hard to access them with
// the current bindings setup, though.
THROW_ERR_ZLIB_INITIALIZATION_FAILED(wrap->env(),
"Initialization failed");
return;
}

Expand All @@ -708,12 +715,11 @@ class BrotliCompressionStream final :
err = wrap->context()->SetParams(i, data[i]);
if (err.IsError()) {
wrap->EmitError(err);
args.GetReturnValue().Set(false);
THROW_ERR_ZLIB_INITIALIZATION_FAILED(wrap->env(),
"Initialization failed");
return;
}
}

args.GetReturnValue().Set(true);
}

static void Params(const FunctionCallbackInfo<Value>& args) {
Expand Down

0 comments on commit a9fa2da

Please sign in to comment.