Skip to content

Commit fd5ff3f

Browse files
authored
Adding new Nan::TryEncode() wrapper for node::TryEncode() (#1005)
Adding new method `Nan::TryEncode()` to serve as a wrapper for `Node::TryEncode()`
1 parent 4f2ac29 commit fd5ff3f

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
os: [windows-latest]
2727
include:
2828
- node-version: lts/*
29-
os: macos-13 # macOS on Intel
29+
os: macos-15-intel # macOS on Intel
3030
- node-version: lts/*
3131
os: macos-latest # macOS on arm64
3232
- node-version: lts/*

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ Miscellaneous string & byte encoding and decoding functionality provided for com
272272

273273
- <a href="doc/string_bytes.md#api_nan_encoding"><b><code>Nan::Encoding</code></b></a>
274274
- <a href="doc/string_bytes.md#api_nan_encode"><b><code>Nan::Encode()</code></b></a>
275+
- <a href="doc/string_bytes.md#api_nan_try_encode"><b><code>Nan::TryEncode()</code></b></a>
275276
- <a href="doc/string_bytes.md#api_nan_decode_bytes"><b><code>Nan::DecodeBytes()</code></b></a>
276277
- <a href="doc/string_bytes.md#api_nan_decode_write"><b><code>Nan::DecodeWrite()</code></b></a>
277278

doc/string_bytes.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Miscellaneous string & byte encoding and decoding functionality provided for com
44

55
- <a href="#api_nan_encoding"><b><code>Nan::Encoding</code></b></a>
66
- <a href="#api_nan_encode"><b><code>Nan::Encode()</code></b></a>
7+
- <a href="#api_nan_try_encode"><b><code>Nan::TryEncode()</code></b></a>
78
- <a href="#api_nan_decode_bytes"><b><code>Nan::DecodeBytes()</code></b></a>
89
- <a href="#api_nan_decode_write"><b><code>Nan::DecodeWrite()</code></b></a>
910

@@ -25,6 +26,8 @@ enum Nan::Encoding { ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER }
2526
2627
A wrapper around `node::Encode()` that provides a consistent implementation across supported versions of Node.
2728
29+
**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).
30+
2831
Signature:
2932
3033
```c++
@@ -34,6 +37,22 @@ v8::Local<v8::Value> Nan::Encode(const void *buf,
3437
```
3538

3639

40+
<a name="api_nan_try_encode"></a>
41+
### Nan::TryEncode()
42+
43+
A wrapper around `node::TryEncode()` that provides a consistent implementation across supported versions of Node.
44+
45+
**Note** Only available in Node 24 and higher. For earlier versions use [`Nan::Encode()`](#api_nan_encode).
46+
47+
Signature:
48+
49+
```c++
50+
Nan::MaybeLocal<v8::Value> Nan::TryEncode(const void *buf,
51+
size_t len,
52+
enum Nan::Encoding encoding = BINARY);
53+
```
54+
55+
3756
<a name="api_nan_decode_bytes"></a>
3857
### Nan::DecodeBytes()
3958

nan.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
#define NODE_18_0_MODULE_VERSION 108
4848
#define NODE_19_0_MODULE_VERSION 111
4949
#define NODE_20_0_MODULE_VERSION 115
50+
#define NODE_21_0_MODULE_VERSION 120
51+
#define NODE_22_0_MODULE_VERSION 127
52+
#define NODE_23_0_MODULE_VERSION 131
53+
#define NODE_24_0_MODULE_VERSION 137
54+
#define NODE_25_0_MODULE_VERSION 141
5055

5156
#ifdef _MSC_VER
5257
# define NAN_HAS_CPLUSPLUS_11 (_MSC_VER >= 1800)
@@ -2414,6 +2419,33 @@ enum Encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER};
24142419
# include "nan_string_bytes.h" // NOLINT(build/include)
24152420
#endif
24162421

2422+
#if NODE_MAJOR_VERSION >= 24
2423+
inline MaybeLocal<v8::Value> TryEncode(
2424+
const void *buf, size_t len, enum Encoding encoding = BINARY) {
2425+
v8::Isolate* isolate = v8::Isolate::GetCurrent();
2426+
node::encoding node_enc = static_cast<node::encoding>(encoding);
2427+
2428+
if (encoding == UCS2) {
2429+
return node::TryEncode(
2430+
isolate
2431+
, reinterpret_cast<const uint16_t *>(buf)
2432+
, len / 2);
2433+
} else {
2434+
return node::TryEncode(
2435+
isolate
2436+
, reinterpret_cast<const char *>(buf)
2437+
, len
2438+
, node_enc);
2439+
}
2440+
}
2441+
2442+
inline v8::Local<v8::Value> Encode(
2443+
const void *buf, size_t len, enum Encoding encoding = BINARY) {
2444+
return TryEncode(buf, len, encoding).ToLocalChecked();
2445+
}
2446+
2447+
#else
2448+
24172449
inline v8::Local<v8::Value> Encode(
24182450
const void *buf, size_t len, enum Encoding encoding = BINARY) {
24192451
#if (NODE_MODULE_VERSION >= ATOM_0_21_MODULE_VERSION)
@@ -2445,6 +2477,7 @@ inline v8::Local<v8::Value> Encode(
24452477
# endif
24462478
#endif
24472479
}
2480+
#endif
24482481

24492482
inline ssize_t DecodeBytes(
24502483
v8::Local<v8::Value> val, enum Encoding encoding = BINARY) {

0 commit comments

Comments
 (0)