Skip to content

Commit

Permalink
Merge pull request #532 from Bartel-C8/v6fixElectron21
Browse files Browse the repository at this point in the history
Fixes #514
  • Loading branch information
aminya committed Nov 27, 2022
2 parents 1c08cfd + 0068e0d commit 16ec524
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
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',
'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

0 comments on commit 16ec524

Please sign in to comment.