Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
e17c737
tools: copyedit `build-tarball.yml`
aduh95 Sep 10, 2025
43e6e54
build: do not include custom ESLint rules testing in tarball
aduh95 Sep 8, 2025
f0c20cc
src: remove unnecessary `Environment::GetCurrent()` calls
iknoom Sep 11, 2025
0d23fd5
tools: skip test-internet workflow for draft PRs
targos Sep 11, 2025
b200cd8
lib,src: refactor assert to load error source from memory
legendecas Sep 2, 2025
d785929
lib: add source map support for assert messages
legendecas Sep 3, 2025
ff13d1d
lib,src: cache ModuleWrap.hasAsyncGraph
legendecas Sep 11, 2025
8a5325d
src: ensure `v8::Eternal` is empty before setting it
addaleax Sep 11, 2025
b711256
deps: update undici to 7.16.0
nodejs-github-bot Sep 11, 2025
e9cb986
doc: rephrase dynamic import() description
meteorqz6 Sep 12, 2025
e4f5716
doc: deprecate closing `fs.Dir` on garbage collection
LiviaMedeiros Sep 12, 2025
6458867
url: add type checking to urlToHttpOptions()
simon-id Sep 12, 2025
9a4bbdc
benchmark: calibrate config cluster/echo.js
meteorqz6 Sep 12, 2025
bb051c5
crypto: avoid calls to `promise.catch()`
Renegade334 Sep 9, 2025
8ed4587
crypto: use async functions for non-stub Promise-returning functions
Renegade334 Sep 9, 2025
647c332
crypto: use `return await` when returning Promises from async functions
Renegade334 Sep 9, 2025
f006a14
node-api: make napi_delete_reference use node_api_basic_env
JeetuSuthar Sep 12, 2025
03116a7
src: remove `std::array` overload of `FIXED_ONE_BYTE_STRING`
addaleax Aug 4, 2025
2bb1525
src: create strings in `FIXED_ONE_BYTE_STRING` as internalized
addaleax Aug 4, 2025
026d4e3
doc,crypto: update subtle.generateKey and subtle.importKey
panva Sep 13, 2025
0f46c1c
repl: fix cpu overhead pasting big strings to the REPL
BridgeAR Sep 14, 2025
6133a82
util: fix debuglog.enabled not being present with callback logger
BridgeAR Sep 14, 2025
3c62b38
util: inspect objects with throwing Symbol.toStringTag
BridgeAR Sep 14, 2025
9df91e5
doc: type improvement of file `http.md`
samuel871211 Sep 14, 2025
8b29bbc
url: replaced slice with at
Connormiha Sep 14, 2025
cbec4fd
benchmark: calibrate config dgram multi-buffer
brunocroh Sep 14, 2025
fed1dac
lib: update isDeepStrictEqual to support options
miguelmarcondesf Sep 14, 2025
9782ca2
zlib: implement fast path for crc32
gurgunday Sep 14, 2025
83ae610
http: optimize checkIsHttpToken for short strings
JinhyeokFang Sep 14, 2025
acada1f
inspector: ensure adequate memory allocation for `Binary::toBase64`
Renegade334 Sep 14, 2025
bd767c5
doc: add security escalation policy
UlisesGascon Sep 15, 2025
97c4e1b
typings: remove unused imports
meteorqz6 Sep 15, 2025
be48760
node-api: added SharedArrayBuffer api
mertcanaltin Sep 16, 2025
039ac19
crypto: expose signatureAlgorithm on X509Certificate
patrickrbc Sep 16, 2025
213188e
stream: use new AsyncResource instead of bind
mcollina Sep 16, 2025
abecdcb
src: replace FIXED_ONE_BYTE_STRING with Environment-cached strings
iknoom Sep 17, 2025
ac78491
src: reduce the nearest parent package JSON cache size
michaelsmithxyz Sep 17, 2025
993f05d
fs,win: do not add a second trailing slash in readdir
Flarna Sep 17, 2025
728472a
module: only put directly require-d ESM into require.cache
joyeecheung Sep 17, 2025
8f32746
test: guard write to proxy client if proxy connection is ended
joyeecheung Sep 17, 2025
d17f299
src: use DictionaryTemplate more in URLPattern
jasnell Sep 17, 2025
87bbaa2
tools: fix `tools/make-v8.sh` for clang
richardlau Sep 17, 2025
7aec53b
http: add shouldUpgradeCallback to let servers control HTTP upgrades
pimterry Sep 17, 2025
9a3e58a
sqlite: add tagged template
0hmX Sep 17, 2025
e94c573
meta: add .npmrc with ignore-scripts=true
joyeecheung Sep 17, 2025
8c85570
deps: upgrade openssl sources to openssl-3.5.3
nodejs-github-bot Sep 16, 2025
fa40d3a
deps: update archs files for openssl-3.5.3
nodejs-github-bot Sep 16, 2025
f5d3f91
tls: only do off-thread certificate loading on loading tls
joyeecheung Sep 11, 2025
6790093
tls: load bundled and extra certificates off-thread
joyeecheung Sep 11, 2025
2b2591d
esm: make hasAsyncGraph non-enumerable
joyeecheung Sep 18, 2025
b0c1e67
diagnostics_channel: fix race condition with diagnostics_channel and GC
uurien Sep 19, 2025
6e586a1
vm: expose hasTopLevelAwait on SourceTextModule
legendecas Sep 19, 2025
c5d910a
http2: fix allowHttp1+Upgrade, broken by shouldUpgradeCallback
pimterry Sep 20, 2025
86150f3
src: rename private fields to follow naming convention
iknoom Sep 20, 2025
69b5607
src: simplify is_callable by making it a concept
tniessen Sep 20, 2025
396cc8e
lib: update inspect output format for subclasses
miguelmarcondesf Sep 21, 2025
ce8435b
test: testcase demonstrating issue 59541
ericrannaud Sep 7, 2025
44ce971
vm: "afterEvaluate", evaluate() return a promise from the outer context
ericrannaud Sep 10, 2025
9347ddd
vm: explain how to share promises between contexts w/ afterEvaluate
ericrannaud Sep 19, 2025
dea5dd7
dgram: restore buffer optimization in fixBufferList
ri7116 Sep 21, 2025
a28962a
doc: update V8 fast API guidance
Renegade334 Sep 21, 2025
0b284d8
build: add the missing macro definitions for OpenHarmony
hqzing Sep 22, 2025
e330f03
src: update crypto objects to use DictionaryTemplate
jasnell Sep 22, 2025
b970c0b
zlib: reduce code duplication
jhofstee Sep 22, 2025
6695067
http,https: handle IPv6 with proxies
joyeecheung Sep 22, 2025
05e560d
deps: update googletest to 50b8600
nodejs-github-bot Sep 23, 2025
1e723f9
doc: fix typo in section on microtask order
tniessen Sep 23, 2025
49747a5
worker: add heap profile API
theanarkh Sep 23, 2025
f14ed5a
src: simplify watchdog instantiations via `std::optional`
addaleax Sep 23, 2025
0b37b59
doc: use "WebAssembly" instead of "Web Assembly"
tniessen Sep 24, 2025
0fe5337
sqlite: cleanup ERM support and export Session class
jasnell May 18, 2025
3eeb7b4
sqlite: fix crash session extension callbacks with workers
louwers Sep 23, 2025
31c6e70
2025-09-25, Version 24.9.0 (Current)
nodejs-github-bot Sep 24, 2025
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
Prev Previous commit
Next Next commit
node-api: added SharedArrayBuffer api
PR-URL: #59071
Reviewed-By: Chengzhong Wu <[email protected]>
  • Loading branch information
mertcanaltin authored and targos committed Sep 24, 2025
commit be48760b93e8272b94e128b28202c148ef7c9bd1
73 changes: 67 additions & 6 deletions doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3299,6 +3299,10 @@ Specification.
<!-- YAML
added: v8.0.0
napiVersion: 1
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/59071
description: Added support for `SharedArrayBuffer`.
-->

```c
Expand All @@ -3309,21 +3313,20 @@ napi_status napi_get_arraybuffer_info(napi_env env,
```

* `[in] env`: The environment that the API is invoked under.
* `[in] arraybuffer`: `napi_value` representing the `ArrayBuffer` being queried.
* `[out] data`: The underlying data buffer of the `ArrayBuffer`. If byte\_length
* `[in] arraybuffer`: `napi_value` representing the `ArrayBuffer` or `SharedArrayBuffer` being queried.
* `[out] data`: The underlying data buffer of the `ArrayBuffer` or `SharedArrayBuffer`
is `0`, this may be `NULL` or any other pointer value.
* `[out] byte_length`: Length in bytes of the underlying data buffer.

Returns `napi_ok` if the API succeeded.

This API is used to retrieve the underlying data buffer of an `ArrayBuffer` and
its length.
This API is used to retrieve the underlying data buffer of an `ArrayBuffer` or `SharedArrayBuffer` and its length.

_WARNING_: Use caution while using this API. The lifetime of the underlying data
buffer is managed by the `ArrayBuffer` even after it's returned. A
buffer is managed by the `ArrayBuffer` or `SharedArrayBuffer` even after it's returned. A
possible safe way to use this API is in conjunction with
[`napi_create_reference`][], which can be used to guarantee control over the
lifetime of the `ArrayBuffer`. It's also safe to use the returned data buffer
lifetime of the `ArrayBuffer` or `SharedArrayBuffer`. It's also safe to use the returned data buffer
within the same callback as long as there are no calls to other APIs that might
trigger a GC.

Expand Down Expand Up @@ -4278,6 +4281,63 @@ This API represents the invocation of the `ArrayBuffer` `IsDetachedBuffer`
operation as defined in [Section isDetachedBuffer][] of the ECMAScript Language
Specification.

### `node_api_is_sharedarraybuffer`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental

```c
napi_status node_api_is_sharedarraybuffer(napi_env env, napi_value value, bool* result)
```

* `[in] env`: The environment that the API is invoked under.
* `[in] value`: The JavaScript value to check.
* `[out] result`: Whether the given `napi_value` represents a `SharedArrayBuffer`.

Returns `napi_ok` if the API succeeded.

This API checks if the Object passed in is a `SharedArrayBuffer`.

### `node_api_create_sharedarraybuffer`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental

```c
napi_status node_api_create_sharedarraybuffer(napi_env env,
size_t byte_length,
void** data,
napi_value* result)
```

* `[in] env`: The environment that the API is invoked under.
* `[in] byte_length`: The length in bytes of the shared array buffer to create.
* `[out] data`: Pointer to the underlying byte buffer of the `SharedArrayBuffer`.
`data` can optionally be ignored by passing `NULL`.
* `[out] result`: A `napi_value` representing a JavaScript `SharedArrayBuffer`.

Returns `napi_ok` if the API succeeded.

This API returns a Node-API value corresponding to a JavaScript `SharedArrayBuffer`.
`SharedArrayBuffer`s are used to represent fixed-length binary data buffers that
can be shared across multiple workers.

The `SharedArrayBuffer` allocated will have an underlying byte buffer whose size is
determined by the `byte_length` parameter that's passed in.
The underlying buffer is optionally returned back to the caller in case the
caller wants to directly manipulate the buffer. This buffer can only be
written to directly from native code. To write to this buffer from JavaScript,
a typed array or `DataView` object would need to be created.

JavaScript `SharedArrayBuffer` objects are described in
[Section SharedArrayBuffer objects][] of the ECMAScript Language Specification.

## Working with JavaScript properties

Node-API exposes a set of APIs to get and set properties on JavaScript
Expand Down Expand Up @@ -6791,6 +6851,7 @@ the add-on's file name during loading.
[Section IsArray]: https://tc39.es/ecma262/#sec-isarray
[Section IsStrctEqual]: https://tc39.es/ecma262/#sec-strict-equality-comparison
[Section Promise objects]: https://tc39.es/ecma262/#sec-promise-objects
[Section SharedArrayBuffer objects]: https://tc39.es/ecma262/#sec-sharedarraybuffer-objects
[Section ToBoolean]: https://tc39.es/ecma262/#sec-toboolean
[Section ToNumber]: https://tc39.es/ecma262/#sec-tonumber
[Section ToObject]: https://tc39.es/ecma262/#sec-toobject
Expand Down
8 changes: 8 additions & 0 deletions src/js_native_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,14 @@ napi_get_dataview_info(napi_env env,
napi_value* arraybuffer,
size_t* byte_offset);

#ifdef NAPI_EXPERIMENTAL
#define NODE_API_EXPERIMENTAL_HAS_SHAREDARRAYBUFFER
NAPI_EXTERN napi_status NAPI_CDECL
node_api_is_sharedarraybuffer(napi_env env, napi_value value, bool* result);
NAPI_EXTERN napi_status NAPI_CDECL node_api_create_sharedarraybuffer(
napi_env env, size_t byte_length, void** data, napi_value* result);
#endif // NAPI_EXPERIMENTAL

// version management
NAPI_EXTERN napi_status NAPI_CDECL napi_get_version(node_api_basic_env env,
uint32_t* result);
Expand Down
61 changes: 54 additions & 7 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3077,21 +3077,68 @@ napi_status NAPI_CDECL napi_get_arraybuffer_info(napi_env env,
CHECK_ARG(env, arraybuffer);

v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue(arraybuffer);
RETURN_STATUS_IF_FALSE(env, value->IsArrayBuffer(), napi_invalid_arg);

v8::Local<v8::ArrayBuffer> ab = value.As<v8::ArrayBuffer>();
if (value->IsArrayBuffer()) {
v8::Local<v8::ArrayBuffer> ab = value.As<v8::ArrayBuffer>();

if (data != nullptr) {
*data = ab->Data();
}
if (data != nullptr) {
*data = ab->Data();
}

if (byte_length != nullptr) {
*byte_length = ab->ByteLength();
if (byte_length != nullptr) {
*byte_length = ab->ByteLength();
}
} else if (value->IsSharedArrayBuffer()) {
v8::Local<v8::SharedArrayBuffer> sab = value.As<v8::SharedArrayBuffer>();

if (data != nullptr) {
*data = sab->Data();
}

if (byte_length != nullptr) {
*byte_length = sab->ByteLength();
}
} else {
return napi_set_last_error(env, napi_invalid_arg);
}

return napi_clear_last_error(env);
}

napi_status NAPI_CDECL node_api_is_sharedarraybuffer(napi_env env,
napi_value value,
bool* result) {
CHECK_ENV_NOT_IN_GC(env);
CHECK_ARG(env, value);
CHECK_ARG(env, result);

v8::Local<v8::Value> val = v8impl::V8LocalValueFromJsValue(value);
*result = val->IsSharedArrayBuffer();

return napi_clear_last_error(env);
}

napi_status NAPI_CDECL node_api_create_sharedarraybuffer(napi_env env,
size_t byte_length,
void** data,
napi_value* result) {
NAPI_PREAMBLE(env);
CHECK_ARG(env, result);

v8::Isolate* isolate = env->isolate;
v8::Local<v8::SharedArrayBuffer> buffer =
v8::SharedArrayBuffer::New(isolate, byte_length);

// Optionally return a pointer to the buffer's data, to avoid another call to
// retrieve it.
if (data != nullptr) {
*data = buffer->Data();
}

*result = v8impl::JsValueFromV8LocalValue(buffer);
return GET_RETURN_STATUS(env);
}

napi_status NAPI_CDECL napi_is_typedarray(napi_env env,
napi_value value,
bool* result) {
Expand Down
8 changes: 8 additions & 0 deletions test/js-native-api/test_sharedarraybuffer/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"targets": [
{
"target_name": "test_sharedarraybuffer",
"sources": [ "test_sharedarraybuffer.c" ]
}
]
}
67 changes: 67 additions & 0 deletions test/js-native-api/test_sharedarraybuffer/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict';

const common = require('../../common');
const assert = require('assert');
const test_sharedarraybuffer = require(`./build/${common.buildType}/test_sharedarraybuffer`);

{
const sab = new SharedArrayBuffer(16);
const ab = new ArrayBuffer(16);
const obj = {};
const arr = [];

assert.strictEqual(test_sharedarraybuffer.TestIsSharedArrayBuffer(sab), true);
assert.strictEqual(test_sharedarraybuffer.TestIsSharedArrayBuffer(ab), false);
assert.strictEqual(test_sharedarraybuffer.TestIsSharedArrayBuffer(obj), false);
assert.strictEqual(test_sharedarraybuffer.TestIsSharedArrayBuffer(arr), false);
assert.strictEqual(test_sharedarraybuffer.TestIsSharedArrayBuffer(null), false);
assert.strictEqual(test_sharedarraybuffer.TestIsSharedArrayBuffer(undefined), false);
}

// Test node_api_create_sharedarraybuffer
{
const sab = test_sharedarraybuffer.TestCreateSharedArrayBuffer(16);
assert(sab instanceof SharedArrayBuffer);
assert.strictEqual(sab.byteLength, 16);
}

// Test node_api_create_get_sharedarraybuffer_info
{
const sab = new SharedArrayBuffer(32);
const byteLength = test_sharedarraybuffer.TestGetSharedArrayBufferInfo(sab);
assert.strictEqual(byteLength, 32);
}

// Test data access
{
const sab = new SharedArrayBuffer(8);
const result = test_sharedarraybuffer.TestSharedArrayBufferData(sab);
assert.strictEqual(result, true);

// Check if data was written correctly
const view = new Uint8Array(sab);
for (let i = 0; i < 8; i++) {
assert.strictEqual(view[i], i % 256);
}
}

// Test data pointer from existing SharedArrayBuffer
{
const sab = new SharedArrayBuffer(16);
const result = test_sharedarraybuffer.TestSharedArrayBufferData(sab);
assert.strictEqual(result, true);
}

// Test zero-length SharedArrayBuffer
{
const sab = test_sharedarraybuffer.TestCreateSharedArrayBuffer(0);
assert(sab instanceof SharedArrayBuffer);
assert.strictEqual(sab.byteLength, 0);
}

// Test invalid arguments
{
assert.throws(() => {
test_sharedarraybuffer.TestGetSharedArrayBufferInfo({});
}, { name: 'Error', message: 'Invalid argument' });
}
Loading