Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/*
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ Miscellaneous string & byte encoding and decoding functionality provided for com

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

Expand Down
19 changes: 19 additions & 0 deletions doc/string_bytes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Miscellaneous string & byte encoding and decoding functionality provided for com

- <a href="#api_nan_encoding"><b><code>Nan::Encoding</code></b></a>
- <a href="#api_nan_encode"><b><code>Nan::Encode()</code></b></a>
- <a href="#api_nan_try_encode"><b><code>Nan::TryEncode()</code></b></a>
- <a href="#api_nan_decode_bytes"><b><code>Nan::DecodeBytes()</code></b></a>
- <a href="#api_nan_decode_write"><b><code>Nan::DecodeWrite()</code></b></a>

Expand All @@ -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++
Expand All @@ -34,6 +37,22 @@ v8::Local<v8::Value> Nan::Encode(const void *buf,
```


<a name="api_nan_try_encode"></a>
### 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<v8::Value> Nan::TryEncode(const void *buf,
size_t len,
enum Nan::Encoding encoding = BINARY);
```


<a name="api_nan_decode_bytes"></a>
### Nan::DecodeBytes()

Expand Down
33 changes: 33 additions & 0 deletions nan.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<v8::Value> TryEncode(
const void *buf, size_t len, enum Encoding encoding = BINARY) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
node::encoding node_enc = static_cast<node::encoding>(encoding);

if (encoding == UCS2) {
return node::TryEncode(
isolate
, reinterpret_cast<const uint16_t *>(buf)
, len / 2);
} else {
return node::TryEncode(
isolate
, reinterpret_cast<const char *>(buf)
, len
, node_enc);
}
}

inline v8::Local<v8::Value> Encode(
const void *buf, size_t len, enum Encoding encoding = BINARY) {
return TryEncode(buf, len, encoding).ToLocalChecked();
}

#else

inline v8::Local<v8::Value> Encode(
const void *buf, size_t len, enum Encoding encoding = BINARY) {
#if (NODE_MODULE_VERSION >= ATOM_0_21_MODULE_VERSION)
Expand Down Expand Up @@ -2445,6 +2477,7 @@ inline v8::Local<v8::Value> Encode(
# endif
#endif
}
#endif

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