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/*
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 7c1bd325..dbd8c517 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()
@@ -25,6 +26,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 +37,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++
+Nan::MaybeLocal 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..4ffcd6c6 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,6 +2419,33 @@ enum Encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER};
# include "nan_string_bytes.h" // NOLINT(build/include)
#endif
+#if NODE_MAJOR_VERSION >= 24
+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);
+
+ if (encoding == UCS2) {
+ return node::TryEncode(
+ isolate
+ , reinterpret_cast(buf)
+ , len / 2);
+ } else {
+ return node::TryEncode(
+ isolate
+ , reinterpret_cast(buf)
+ , len
+ , node_enc);
+ }
+}
+
+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) {
#if (NODE_MODULE_VERSION >= ATOM_0_21_MODULE_VERSION)
@@ -2445,6 +2477,7 @@ inline v8::Local Encode(
# endif
#endif
}
+#endif
inline ssize_t DecodeBytes(
v8::Local val, enum Encoding encoding = BINARY) {