Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Electron v21 and up. #532

Merged
merged 1 commit into from
Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'zmq_no_sync_resolve%': 'false',
'sanitizers%': 'false',
'openssl_fips': '',
'runtime%': 'node',
},

'targets': [
Expand Down Expand Up @@ -51,7 +52,6 @@
],

'defines': [
'NAPI_VERSION=3',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't removing this cause issues?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, as now <node_version.h> is included, which defines NAPI_VERSION...

If I didn't remove this, I got a compilation error that NAPI_VERSION was being redefined.

'ZMQ_STATIC',
],

Expand Down Expand Up @@ -96,6 +96,9 @@
}],
],
}],
['runtime=="electron"', {
"defines": ["NODE_RUNTIME_ELECTRON=1"]
}],
],

'configurations': {
Expand Down
4 changes: 4 additions & 0 deletions src/incoming_msg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ IncomingMsg::~IncomingMsg() {
}

Napi::Value IncomingMsg::IntoBuffer(const Napi::Env& env) {
#if !(NODE_RUNTIME_ELECTRON && NODE_MODULE_VERSION >= 109) // 109 is Electron v21 and up
if (moved) {
/* If ownership has been transferred, do not attempt to read the buffer
again in any case. This should not happen of course. */
Expand All @@ -22,10 +23,12 @@ Napi::Value IncomingMsg::IntoBuffer(const Napi::Env& env) {
}

static auto constexpr zero_copy_threshold = 1 << 7;
#endif

auto data = reinterpret_cast<uint8_t*>(zmq_msg_data(*ref));
auto length = zmq_msg_size(*ref);

#if !(NODE_RUNTIME_ELECTRON && NODE_MODULE_VERSION >= 109) // 109 is Electron v21 and up
if (length > zero_copy_threshold) {
/* Reuse existing buffer for external storage. This avoids copying but
does include an overhead in having to call a finalizer when the
Expand All @@ -43,6 +46,7 @@ Napi::Value IncomingMsg::IntoBuffer(const Napi::Env& env) {

return Napi::Buffer<uint8_t>::New(env, data, length, release, ref);
}
#endif

if (length > 0) {
return Napi::Buffer<uint8_t>::Copy(env, data, length);
Expand Down
1 change: 1 addition & 0 deletions src/prefix.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma once

#include <napi.h>
#include <node_version.h>
#define NAPI_BUILD_VERSION NAPI_VERSION

#include <zmq.h>
Expand Down