From 0068e0d51ad268961cb9b7363c8bee836fb5da06 Mon Sep 17 00:00:00 2001 From: Bartel Eerdekens Date: Thu, 24 Nov 2022 13:02:13 +0100 Subject: [PATCH] Fix for Electron v21 and up. Fixes https://github.com/zeromq/zeromq.js/issues/514 --- binding.gyp | 5 ++++- src/incoming_msg.cc | 4 ++++ src/prefix.h | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/binding.gyp b/binding.gyp index c4b431bd..b51b8d1a 100644 --- a/binding.gyp +++ b/binding.gyp @@ -5,6 +5,7 @@ 'zmq_no_sync_resolve%': 'false', 'sanitizers%': 'false', 'openssl_fips': '', + 'runtime%': 'node', }, 'targets': [ @@ -51,7 +52,6 @@ ], 'defines': [ - 'NAPI_VERSION=3', 'ZMQ_STATIC', ], @@ -96,6 +96,9 @@ }], ], }], + ['runtime=="electron"', { + "defines": ["NODE_RUNTIME_ELECTRON=1"] + }], ], 'configurations': { diff --git a/src/incoming_msg.cc b/src/incoming_msg.cc index c089c7ea..a52b7895 100644 --- a/src/incoming_msg.cc +++ b/src/incoming_msg.cc @@ -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. */ @@ -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(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 @@ -43,6 +46,7 @@ Napi::Value IncomingMsg::IntoBuffer(const Napi::Env& env) { return Napi::Buffer::New(env, data, length, release, ref); } +#endif if (length > 0) { return Napi::Buffer::Copy(env, data, length); diff --git a/src/prefix.h b/src/prefix.h index 405f88b6..edd26dc1 100644 --- a/src/prefix.h +++ b/src/prefix.h @@ -2,6 +2,7 @@ #pragma once #include +#include #define NAPI_BUILD_VERSION NAPI_VERSION #include