From 94cccca1fada932b443cb7e786c745a0216a8fee Mon Sep 17 00:00:00 2001 From: agracio Date: Tue, 2 Dec 2025 13:56:14 +0000 Subject: [PATCH 1/6] Adding new method `Nan::TryEncode()` to serve as a wrapper for `Node::TryEncode()` --- doc/string_bytes.md | 18 ++++++++++++++++++ nan.h | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/doc/string_bytes.md b/doc/string_bytes.md index 7c1bd325..0c551d33 100644 --- a/doc/string_bytes.md +++ b/doc/string_bytes.md @@ -25,6 +25,8 @@ enum Nan::Encoding { ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER } A wrapper around `node::Encode()` that provides a consistent implementation across supported versions of Node. +**Note** `node::Encode()` was deprecated in Node 24 but will remain to maintain backwards compatibility. For Node 24 and higher consider using [`Nan::TryEncode()`](#api_nan_try_encode). + Signature: ```c++ @@ -34,6 +36,22 @@ v8::Local Nan::Encode(const void *buf, ``` + +### Nan::TryEncode() + +A wrapper around `node::TryEncode()` that provides a consistent implementation across supported versions of Node. + +**Note** Only available in Node 24 and higher. For earlier versions use [`Nan::Encode()`](#api_nan_encode). + +Signature: + +```c++ +v8::Local Nan::TryEncode(const void *buf, + size_t len, + enum Nan::Encoding encoding = BINARY); +``` + + ### Nan::DecodeBytes() diff --git a/nan.h b/nan.h index 227b0692..1b682e68 100644 --- a/nan.h +++ b/nan.h @@ -47,6 +47,11 @@ #define NODE_18_0_MODULE_VERSION 108 #define NODE_19_0_MODULE_VERSION 111 #define NODE_20_0_MODULE_VERSION 115 +#define NODE_21_0_MODULE_VERSION 120 +#define NODE_22_0_MODULE_VERSION 127 +#define NODE_23_0_MODULE_VERSION 131 +#define NODE_24_0_MODULE_VERSION 137 +#define NODE_25_0_MODULE_VERSION 141 #ifdef _MSC_VER # define NAN_HAS_CPLUSPLUS_11 (_MSC_VER >= 1800) @@ -2414,8 +2419,34 @@ enum Encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER}; # include "nan_string_bytes.h" // NOLINT(build/include) #endif -inline v8::Local Encode( +#if NODE_MAJOR_VERSION >= 24 +inline v8::Local TryEncode( + const char *buf, size_t len, enum Encoding encoding = BINARY) { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + node::encoding node_enc = static_cast(encoding); + + if (encoding == UCS2) { + return node::TryEncode( + isolate + , reinterpret_cast(buf) + , len / 2).FromMaybe(v8::Local()); + } else { + return node::TryEncode( + isolate + , reinterpret_cast(buf) + , len + , node_enc).FromMaybe(v8::Local()); + } +} +#endif + +#if NODE_MAJOR_VERSION >= 24 +NAN_DEPRECATED inline v8::Local Encode( const void *buf, size_t len, enum Encoding encoding = BINARY) { +#else +inline v8::Local Encode( + const char *buf, size_t len, enum Encoding encoding = BINARY) { +#endif #if (NODE_MODULE_VERSION >= ATOM_0_21_MODULE_VERSION) v8::Isolate* isolate = v8::Isolate::GetCurrent(); node::encoding node_enc = static_cast(encoding); From 1dad900855a3303ed6d5197c2e341e33c3554623 Mon Sep 17 00:00:00 2001 From: agracio Date: Tue, 2 Dec 2025 13:59:07 +0000 Subject: [PATCH 2/6] Updating docs for new method `Nan::TryEncode()` --- README.md | 1 + doc/string_bytes.md | 1 + 2 files changed, 2 insertions(+) diff --git a/README.md b/README.md index e9130454..bcb72ba3 100644 --- a/README.md +++ b/README.md @@ -272,6 +272,7 @@ Miscellaneous string & byte encoding and decoding functionality provided for com - Nan::Encoding - Nan::Encode() + - Nan::TryEncode() - Nan::DecodeBytes() - Nan::DecodeWrite() diff --git a/doc/string_bytes.md b/doc/string_bytes.md index 0c551d33..0a710bba 100644 --- a/doc/string_bytes.md +++ b/doc/string_bytes.md @@ -4,6 +4,7 @@ Miscellaneous string & byte encoding and decoding functionality provided for com - Nan::Encoding - Nan::Encode() + - Nan::TryEncode() - Nan::DecodeBytes() - Nan::DecodeWrite() From 290ae803eeab18935630fe5143657e8ce74c16b2 Mon Sep 17 00:00:00 2001 From: agracio Date: Tue, 2 Dec 2025 14:50:36 +0000 Subject: [PATCH 3/6] updating #if condition --- nan.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/nan.h b/nan.h index 1b682e68..fdfce0a2 100644 --- a/nan.h +++ b/nan.h @@ -2442,11 +2442,12 @@ inline v8::Local TryEncode( #if NODE_MAJOR_VERSION >= 24 NAN_DEPRECATED inline v8::Local Encode( - const void *buf, size_t len, enum Encoding encoding = BINARY) { + const void *buf, size_t len, enum Encoding encoding = BINARY) #else inline v8::Local Encode( - const char *buf, size_t len, enum Encoding encoding = BINARY) { + const void *buf, size_t len, enum Encoding encoding = BINARY) #endif +{ #if (NODE_MODULE_VERSION >= ATOM_0_21_MODULE_VERSION) v8::Isolate* isolate = v8::Isolate::GetCurrent(); node::encoding node_enc = static_cast(encoding); From 203eeba72f7e264dfca135fc44e8c74ae404d2b8 Mon Sep 17 00:00:00 2001 From: agracio Date: Tue, 2 Dec 2025 15:00:09 +0000 Subject: [PATCH 4/6] updating macOS x64 ci runner --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9532f184..e12a2c95 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,7 +26,7 @@ jobs: os: [windows-latest] include: - node-version: lts/* - os: macos-13 # macOS on Intel + os: macos-15-intel # macOS on Intel - node-version: lts/* os: macos-latest # macOS on arm64 - node-version: lts/* From 0c3d86dd798c2f51379b4a8f1df348c0c6fcf2e6 Mon Sep 17 00:00:00 2001 From: agracio Date: Tue, 2 Dec 2025 19:50:49 +0000 Subject: [PATCH 5/6] changed `TryEncode()` signature to be consistent with `Encode()`, removed deprecation for `Encode()` in Node >= 24 --- doc/string_bytes.md | 2 +- nan.h | 20 +++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/doc/string_bytes.md b/doc/string_bytes.md index 0a710bba..dbd8c517 100644 --- a/doc/string_bytes.md +++ b/doc/string_bytes.md @@ -47,7 +47,7 @@ A wrapper around `node::TryEncode()` that provides a consistent implementation a Signature: ```c++ -v8::Local Nan::TryEncode(const void *buf, +Nan::MaybeLocal Nan::TryEncode(const void *buf, size_t len, enum Nan::Encoding encoding = BINARY); ``` diff --git a/nan.h b/nan.h index fdfce0a2..b2d49dd0 100644 --- a/nan.h +++ b/nan.h @@ -2420,8 +2420,8 @@ enum Encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER}; #endif #if NODE_MAJOR_VERSION >= 24 -inline v8::Local TryEncode( - const char *buf, size_t len, enum Encoding encoding = BINARY) { +inline MaybeLocal TryEncode( + const void *buf, size_t len, enum Encoding encoding = BINARY) { v8::Isolate* isolate = v8::Isolate::GetCurrent(); node::encoding node_enc = static_cast(encoding); @@ -2429,24 +2429,25 @@ inline v8::Local TryEncode( return node::TryEncode( isolate , reinterpret_cast(buf) - , len / 2).FromMaybe(v8::Local()); + , len / 2); } else { return node::TryEncode( isolate , reinterpret_cast(buf) , len - , node_enc).FromMaybe(v8::Local()); + , node_enc); } } -#endif -#if NODE_MAJOR_VERSION >= 24 -NAN_DEPRECATED inline v8::Local Encode( - const void *buf, size_t len, enum Encoding encoding = BINARY) +inline v8::Local Encode( + const void *buf, size_t len, enum Encoding encoding = BINARY){ + return TryEncode(buf, len, encoding).ToLocalChecked(); +} + #else + inline v8::Local Encode( const void *buf, size_t len, enum Encoding encoding = BINARY) -#endif { #if (NODE_MODULE_VERSION >= ATOM_0_21_MODULE_VERSION) v8::Isolate* isolate = v8::Isolate::GetCurrent(); @@ -2477,6 +2478,7 @@ inline v8::Local Encode( # endif #endif } +#endif inline ssize_t DecodeBytes( v8::Local val, enum Encoding encoding = BINARY) { From c0f5157c6679e2e5c190207f743dcbaaff9064b9 Mon Sep 17 00:00:00 2001 From: agracio Date: Tue, 2 Dec 2025 20:00:55 +0000 Subject: [PATCH 6/6] cpplint fixes --- nan.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/nan.h b/nan.h index b2d49dd0..4ffcd6c6 100644 --- a/nan.h +++ b/nan.h @@ -2440,15 +2440,14 @@ inline MaybeLocal TryEncode( } inline v8::Local Encode( - const void *buf, size_t len, enum Encoding encoding = BINARY){ + const void *buf, size_t len, enum Encoding encoding = BINARY) { return TryEncode(buf, len, encoding).ToLocalChecked(); } #else inline v8::Local Encode( - const void *buf, size_t len, enum Encoding encoding = BINARY) -{ + const void *buf, size_t len, enum Encoding encoding = BINARY) { #if (NODE_MODULE_VERSION >= ATOM_0_21_MODULE_VERSION) v8::Isolate* isolate = v8::Isolate::GetCurrent(); node::encoding node_enc = static_cast(encoding);