node-addon-api 7.0.0 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md CHANGED
@@ -1,13 +1,9 @@
1
1
  The MIT License (MIT)
2
- =====================
3
2
 
4
- Copyright (c) 2017 Node.js API collaborators
5
- -----------------------------------
6
-
7
- *Node.js API collaborators listed at <https://github.com/nodejs/node-addon-api#collaborators>*
3
+ Copyright (c) 2017 [Node.js API collaborators](https://github.com/nodejs/node-addon-api#collaborators)
8
4
 
9
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
6
 
11
7
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12
8
 
13
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,265 +1,33 @@
1
- NOTE: The default branch has been renamed!
2
- master is now named main
3
-
4
- If you have a local clone, you can update it by running:
1
+ # **node-addon-api module**
5
2
 
6
- ```shell
7
- git branch -m master main
8
- git fetch origin
9
- git branch -u origin/main main
10
- ```
3
+ [![NPM](https://nodei.co/npm/node-addon-api.png?downloads=true&downloadRank=true)](https://nodei.co/npm/node-addon-api/) [![NPM](https://nodei.co/npm-dl/node-addon-api.png?months=6&height=1)](https://nodei.co/npm/node-addon-api/)
11
4
 
12
- # **node-addon-api module**
13
5
  This module contains **header-only C++ wrapper classes** which simplify
14
6
  the use of the C based [Node-API](https://nodejs.org/dist/latest/docs/api/n-api.html)
15
7
  provided by Node.js when using C++. It provides a C++ object model
16
8
  and exception handling semantics with low overhead.
17
9
 
18
- There are three options for implementing addons: Node-API, nan, or direct
19
- use of internal V8, libuv, and Node.js libraries. Unless there is a need for
20
- direct access to functionality that is not exposed by Node-API as outlined
21
- in [C/C++ addons](https://nodejs.org/dist/latest/docs/api/addons.html)
22
- in Node.js core, use Node-API. Refer to
23
- [C/C++ addons with Node-API](https://nodejs.org/dist/latest/docs/api/n-api.html)
24
- for more information on Node-API.
25
-
26
- Node-API is an ABI stable C interface provided by Node.js for building native
27
- addons. It is independent of the underlying JavaScript runtime (e.g. V8 or ChakraCore)
28
- and is maintained as part of Node.js itself. It is intended to insulate
29
- native addons from changes in the underlying JavaScript engine and allow
30
- modules compiled for one version to run on later versions of Node.js without
31
- recompilation.
32
-
33
- The `node-addon-api` module, which is not part of Node.js, preserves the benefits
34
- of the Node-API as it consists only of inline code that depends only on the stable API
35
- provided by Node-API. As such, modules built against one version of Node.js
36
- using node-addon-api should run without having to be rebuilt with newer versions
37
- of Node.js.
38
-
39
- It is important to remember that *other* Node.js interfaces such as
40
- `libuv` (included in a project via `#include <uv.h>`) are not ABI-stable across
41
- Node.js major versions. Thus, an addon must use Node-API and/or `node-addon-api`
42
- exclusively and build against a version of Node.js that includes an
43
- implementation of Node-API (meaning an active LTS version of Node.js) in
44
- order to benefit from ABI stability across Node.js major versions. Node.js
45
- provides an [ABI stability guide][] containing a detailed explanation of ABI
46
- stability in general, and the Node-API ABI stability guarantee in particular.
47
-
48
- As new APIs are added to Node-API, node-addon-api must be updated to provide
49
- wrappers for those new APIs. For this reason, node-addon-api provides
50
- methods that allow callers to obtain the underlying Node-API handles so
51
- direct calls to Node-API and the use of the objects/methods provided by
52
- node-addon-api can be used together. For example, in order to be able
53
- to use an API for which the node-addon-api does not yet provide a wrapper.
10
+ - [API References](doc/README.md)
11
+ - [Badges](#badges)
12
+ - [Contributing](#contributing)
13
+ - [License](#license)
54
14
 
55
- APIs exposed by node-addon-api are generally used to create and
56
- manipulate JavaScript values. Concepts and operations generally map
57
- to ideas specified in the **ECMA262 Language Specification**.
15
+ ## API References
58
16
 
59
- The [Node-API Resource](https://nodejs.github.io/node-addon-examples/) offers an
60
- excellent orientation and tips for developers just getting started with Node-API
61
- and node-addon-api.
17
+ API references are available in the [doc](doc/README.md) directory.
62
18
 
63
- - **[Setup](#setup)**
64
- - **[API Documentation](#api)**
65
- - **[Examples](#examples)**
66
- - **[Tests](#tests)**
67
- - **[More resource and info about native Addons](#resources)**
68
- - **[Badges](#badges)**
69
- - **[Code of Conduct](CODE_OF_CONDUCT.md)**
70
- - **[Contributors](#contributors)**
71
- - **[License](#license)**
72
-
73
- ## **Current version: 7.0.0**
19
+ ## Current version: 8.0.0
74
20
 
75
21
  (See [CHANGELOG.md](CHANGELOG.md) for complete Changelog)
76
22
 
77
- [![NPM](https://nodei.co/npm/node-addon-api.png?downloads=true&downloadRank=true)](https://nodei.co/npm/node-addon-api/) [![NPM](https://nodei.co/npm-dl/node-addon-api.png?months=6&height=1)](https://nodei.co/npm/node-addon-api/)
78
-
79
- <a name="setup"></a>
80
-
81
23
  node-addon-api is based on [Node-API](https://nodejs.org/api/n-api.html) and supports using different Node-API versions.
82
24
  This allows addons built with it to run with Node.js versions which support the targeted Node-API version.
83
25
  **However** the node-addon-api support model is to support only the active LTS Node.js versions. This means that
84
26
  every year there will be a new major which drops support for the Node.js LTS version which has gone out of service.
85
27
 
86
- The oldest Node.js version supported by the current version of node-addon-api is Node.js 16.x.
87
-
88
- ## Setup
89
- - [Installation and usage](doc/setup.md)
90
- - [node-gyp](doc/node-gyp.md)
91
- - [cmake-js](doc/cmake-js.md)
92
- - [Conversion tool](doc/conversion-tool.md)
93
- - [Checker tool](doc/checker-tool.md)
94
- - [Generator](doc/generator.md)
95
- - [Prebuild tools](doc/prebuild_tools.md)
96
-
97
- <a name="api"></a>
98
-
99
- ### **API Documentation**
100
-
101
- The following is the documentation for node-addon-api.
102
-
103
- - [Full Class Hierarchy](doc/hierarchy.md)
104
- - [Addon Structure](doc/addon.md)
105
- - Data Types:
106
- - [Env](doc/env.md)
107
- - [CallbackInfo](doc/callbackinfo.md)
108
- - [Reference](doc/reference.md)
109
- - [Value](doc/value.md)
110
- - [Name](doc/name.md)
111
- - [Symbol](doc/symbol.md)
112
- - [String](doc/string.md)
113
- - [Number](doc/number.md)
114
- - [Date](doc/date.md)
115
- - [BigInt](doc/bigint.md)
116
- - [Boolean](doc/boolean.md)
117
- - [External](doc/external.md)
118
- - [Object](doc/object.md)
119
- - [Array](doc/array.md)
120
- - [ObjectReference](doc/object_reference.md)
121
- - [PropertyDescriptor](doc/property_descriptor.md)
122
- - [Function](doc/function.md)
123
- - [FunctionReference](doc/function_reference.md)
124
- - [ObjectWrap](doc/object_wrap.md)
125
- - [ClassPropertyDescriptor](doc/class_property_descriptor.md)
126
- - [Buffer](doc/buffer.md)
127
- - [ArrayBuffer](doc/array_buffer.md)
128
- - [TypedArray](doc/typed_array.md)
129
- - [TypedArrayOf](doc/typed_array_of.md)
130
- - [DataView](doc/dataview.md)
131
- - [Error Handling](doc/error_handling.md)
132
- - [Error](doc/error.md)
133
- - [TypeError](doc/type_error.md)
134
- - [RangeError](doc/range_error.md)
135
- - [Object Lifetime Management](doc/object_lifetime_management.md)
136
- - [HandleScope](doc/handle_scope.md)
137
- - [EscapableHandleScope](doc/escapable_handle_scope.md)
138
- - [Memory Management](doc/memory_management.md)
139
- - [Async Operations](doc/async_operations.md)
140
- - [AsyncWorker](doc/async_worker.md)
141
- - [AsyncContext](doc/async_context.md)
142
- - [AsyncWorker Variants](doc/async_worker_variants.md)
143
- - [Thread-safe Functions](doc/threadsafe.md)
144
- - [ThreadSafeFunction](doc/threadsafe_function.md)
145
- - [TypedThreadSafeFunction](doc/typed_threadsafe_function.md)
146
- - [Promises](doc/promises.md)
147
- - [Version management](doc/version_management.md)
148
-
149
- <a name="examples"></a>
150
-
151
- ### **Examples**
152
-
153
- Are you new to **node-addon-api**? Take a look at our **[examples](https://github.com/nodejs/node-addon-examples)**
154
-
155
- - **[Hello World](https://github.com/nodejs/node-addon-examples/tree/HEAD/1_hello_world/node-addon-api)**
156
- - **[Pass arguments to a function](https://github.com/nodejs/node-addon-examples/tree/HEAD/2_function_arguments/node-addon-api)**
157
- - **[Callbacks](https://github.com/nodejs/node-addon-examples/tree/HEAD/3_callbacks/node-addon-api)**
158
- - **[Object factory](https://github.com/nodejs/node-addon-examples/tree/HEAD/4_object_factory/node-addon-api)**
159
- - **[Function factory](https://github.com/nodejs/node-addon-examples/tree/HEAD/5_function_factory/node-addon-api)**
160
- - **[Wrapping C++ Object](https://github.com/nodejs/node-addon-examples/tree/HEAD/6_object_wrap/node-addon-api)**
161
- - **[Factory of wrapped object](https://github.com/nodejs/node-addon-examples/tree/HEAD/7_factory_wrap/node-addon-api)**
162
- - **[Passing wrapped object around](https://github.com/nodejs/node-addon-examples/tree/HEAD/8_passing_wrapped/node-addon-api)**
163
-
164
- <a name="tests"></a>
165
-
166
- ### **Tests**
167
-
168
- To run the **node-addon-api** tests do:
169
-
170
- ```
171
- npm install
172
- npm test
173
- ```
174
-
175
- To avoid testing the deprecated portions of the API run
176
- ```
177
- npm install
178
- npm test --disable-deprecated
179
- ```
180
-
181
- To run the tests targeting a specific version of Node-API run
182
- ```
183
- npm install
184
- export NAPI_VERSION=X
185
- npm test --NAPI_VERSION=X
186
- ```
187
-
188
- where X is the version of Node-API you want to target.
189
-
190
- To run a specific unit test, filter conditions are available
28
+ The oldest Node.js version supported by the current version of node-addon-api is Node.js 18.x.
191
29
 
192
- **Example:**
193
- compile and run only tests on objectwrap.cc and objectwrap.js
194
- ```
195
- npm run unit --filter=objectwrap
196
- ```
197
-
198
- Multiple unit tests cane be selected with wildcards
199
-
200
- **Example:**
201
- compile and run all test files ending with "reference" -> function_reference.cc, object_reference.cc, reference.cc
202
- ```
203
- npm run unit --filter=*reference
204
- ```
205
-
206
- Multiple filter conditions can be joined to broaden the test selection
207
-
208
- **Example:**
209
- compile and run all tests under folders threadsafe_function and typed_threadsafe_function and also the objectwrap.cc file
210
- npm run unit --filter='*function objectwrap'
211
-
212
- ### **Debug**
213
-
214
- To run the **node-addon-api** tests with `--debug` option:
215
-
216
- ```
217
- npm run-script dev
218
- ```
219
-
220
- If you want a faster build, you might use the following option:
221
-
222
- ```
223
- npm run-script dev:incremental
224
- ```
225
-
226
- Take a look and get inspired by our **[test suite](https://github.com/nodejs/node-addon-api/tree/HEAD/test)**
227
-
228
- ### **Benchmarks**
229
-
230
- You can run the available benchmarks using the following command:
231
-
232
- ```
233
- npm run-script benchmark
234
- ```
235
-
236
- See [benchmark/README.md](benchmark/README.md) for more details about running and adding benchmarks.
237
-
238
- <a name="resources"></a>
239
-
240
- ### **More resource and info about native Addons**
241
- - **[C++ Addons](https://nodejs.org/dist/latest/docs/api/addons.html)**
242
- - **[Node-API](https://nodejs.org/dist/latest/docs/api/n-api.html)**
243
- - **[Node-API - Next Generation Node API for Native Modules](https://youtu.be/-Oniup60Afs)**
244
- - **[How We Migrated Realm JavaScript From NAN to Node-API](https://developer.mongodb.com/article/realm-javascript-nan-to-n-api)**
245
-
246
- As node-addon-api's core mission is to expose the plain C Node-API as C++
247
- wrappers, tools that facilitate n-api/node-addon-api providing more
248
- convenient patterns for developing a Node.js add-on with n-api/node-addon-api
249
- can be published to NPM as standalone packages. It is also recommended to tag
250
- such packages with `node-addon-api` to provide more visibility to the community.
251
-
252
- Quick links to NPM searches: [keywords:node-addon-api](https://www.npmjs.com/search?q=keywords%3Anode-addon-api).
253
-
254
- <a name="other-bindings"></a>
255
-
256
- ### **Other bindings**
257
-
258
- - **[napi-rs](https://napi.rs)** - (`Rust`)
259
-
260
- <a name="badges"></a>
261
-
262
- ### **Badges**
30
+ ## Badges
263
31
 
264
32
  The use of badges is recommended to indicate the minimum version of Node-API
265
33
  required for the module. This helps to determine which Node.js major versions are
@@ -278,16 +46,15 @@ available:
278
46
  ![Node-API v9 Badge](https://github.com/nodejs/abi-stable-node/blob/doc/assets/Node-API%20v9%20Badge.svg)
279
47
  ![Node-API Experimental Version Badge](https://github.com/nodejs/abi-stable-node/blob/doc/assets/Node-API%20Experimental%20Version%20Badge.svg)
280
48
 
281
- ## **Contributing**
49
+ ## Contributing
282
50
 
283
51
  We love contributions from the community to **node-addon-api**!
284
52
  See [CONTRIBUTING.md](CONTRIBUTING.md) for more details on our philosophy around extending this module.
285
53
 
286
- <a name="contributors"></a>
287
-
288
54
  ## Team members
289
55
 
290
56
  ### Active
57
+
291
58
  | Name | GitHub Link |
292
59
  | ------------------- | ----------------------------------------------------- |
293
60
  | Anna Henningsen | [addaleax](https://github.com/addaleax) |
@@ -298,7 +65,12 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for more details on our philosophy around
298
65
  | Nicola Del Gobbo | [NickNaso](https://github.com/NickNaso) |
299
66
  | Vladimir Morozov | [vmoroz](https://github.com/vmoroz) |
300
67
 
68
+ <details>
69
+
70
+ <summary>Emeritus</summary>
71
+
301
72
  ### Emeritus
73
+
302
74
  | Name | GitHub Link |
303
75
  | ------------------- | ----------------------------------------------------- |
304
76
  | Arunesh Chandra | [aruneshchandra](https://github.com/aruneshchandra) |
@@ -310,9 +82,10 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for more details on our philosophy around
310
82
  | Sampson Gao | [sampsongao](https://github.com/sampsongao) |
311
83
  | Taylor Woll | [boingoing](https://github.com/boingoing) |
312
84
 
313
- <a name="license"></a>
85
+ </details>
86
+
87
+ ## License
314
88
 
315
89
  Licensed under [MIT](./LICENSE.md)
316
90
 
317
- [ABI stability guide]: https://nodejs.org/en/docs/guides/abi-stability/
318
91
  [Node-API support matrix]: https://nodejs.org/dist/latest/docs/api/n-api.html#n_api_n_api_version_matrix
package/common.gypi CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  'variables': {
3
- 'NAPI_VERSION%': "<!(node -p \"process.versions.napi\")",
3
+ 'NAPI_VERSION%': "<!(node -p \"process.env.NAPI_VERSION || process.versions.napi\")",
4
4
  'disable_deprecated': "<!(node -p \"process.env['npm_config_disable_deprecated']\")"
5
5
  },
6
6
  'conditions': [
@@ -15,7 +15,6 @@
15
15
  }
16
16
  }]
17
17
  ],
18
- 'include_dirs': ["<!(node -p \"require('../').include_dir\")"],
19
18
  'cflags': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ],
20
19
  'cflags_cc': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ]
21
20
  }
package/index.js CHANGED
@@ -5,7 +5,8 @@ const includeDir = path.relative('.', __dirname);
5
5
  module.exports = {
6
6
  include: `"${__dirname}"`, // deprecated, can be removed as part of 4.0.0
7
7
  include_dir: includeDir,
8
- gyp: path.join(includeDir, 'node_api.gyp:nothing'),
8
+ gyp: path.join(includeDir, 'node_api.gyp:nothing'), // deprecated.
9
+ targets: path.join(includeDir, 'node_addon_api.gyp'),
9
10
  isNodeApiBuiltin: true,
10
11
  needsFlag: false
11
12
  };
package/napi-inl.h CHANGED
@@ -31,22 +31,22 @@ namespace details {
31
31
  // Node.js releases. Only necessary when they are used in napi.h and napi-inl.h.
32
32
  constexpr int napi_no_external_buffers_allowed = 22;
33
33
 
34
+ template <typename FreeType>
35
+ inline void default_finalizer(napi_env /*env*/, void* data, void* /*hint*/) {
36
+ delete static_cast<FreeType*>(data);
37
+ }
38
+
34
39
  // Attach a data item to an object and delete it when the object gets
35
40
  // garbage-collected.
36
41
  // TODO: Replace this code with `napi_add_finalizer()` whenever it becomes
37
42
  // available on all supported versions of Node.js.
38
- template <typename FreeType>
43
+ template <typename FreeType,
44
+ napi_finalize finalizer = default_finalizer<FreeType>>
39
45
  inline napi_status AttachData(napi_env env,
40
46
  napi_value obj,
41
47
  FreeType* data,
42
- napi_finalize finalizer = nullptr,
43
48
  void* hint = nullptr) {
44
49
  napi_status status;
45
- if (finalizer == nullptr) {
46
- finalizer = [](napi_env /*env*/, void* data, void* /*hint*/) {
47
- delete static_cast<FreeType*>(data);
48
- };
49
- }
50
50
  #if (NAPI_VERSION < 5)
51
51
  napi_value symbol, external;
52
52
  status = napi_create_symbol(env, nullptr, &symbol);
@@ -263,10 +263,12 @@ struct ThreadSafeFinalize {
263
263
  template <typename ContextType, typename DataType, typename CallJs, CallJs call>
264
264
  inline typename std::enable_if<call != static_cast<CallJs>(nullptr)>::type
265
265
  CallJsWrapper(napi_env env, napi_value jsCallback, void* context, void* data) {
266
- call(env,
267
- Function(env, jsCallback),
268
- static_cast<ContextType*>(context),
269
- static_cast<DataType*>(data));
266
+ details::WrapVoidCallback([&]() {
267
+ call(env,
268
+ Function(env, jsCallback),
269
+ static_cast<ContextType*>(context),
270
+ static_cast<DataType*>(data));
271
+ });
270
272
  }
271
273
 
272
274
  template <typename ContextType, typename DataType, typename CallJs, CallJs call>
@@ -275,9 +277,11 @@ CallJsWrapper(napi_env env,
275
277
  napi_value jsCallback,
276
278
  void* /*context*/,
277
279
  void* /*data*/) {
278
- if (jsCallback != nullptr) {
279
- Function(env, jsCallback).Call(0, nullptr);
280
- }
280
+ details::WrapVoidCallback([&]() {
281
+ if (jsCallback != nullptr) {
282
+ Function(env, jsCallback).Call(0, nullptr);
283
+ }
284
+ });
281
285
  }
282
286
 
283
287
  #if NAPI_VERSION > 4
@@ -572,6 +576,14 @@ void Env::DefaultFiniWithHint(Env, DataType* data, HintType*) {
572
576
  }
573
577
  #endif // NAPI_VERSION > 5
574
578
 
579
+ #if NAPI_VERSION > 8
580
+ inline const char* Env::GetModuleFileName() const {
581
+ const char* result;
582
+ napi_status status = node_api_get_module_file_name(_env, &result);
583
+ NAPI_THROW_IF_FAILED(*this, status, nullptr);
584
+ return result;
585
+ }
586
+ #endif // NAPI_VERSION > 8
575
587
  ////////////////////////////////////////////////////////////////////////////////
576
588
  // Value class
577
589
  ////////////////////////////////////////////////////////////////////////////////
@@ -1624,11 +1636,8 @@ inline void Object::AddFinalizer(Finalizer finalizeCallback, T* data) const {
1624
1636
  new details::FinalizeData<T, Finalizer>(
1625
1637
  {std::move(finalizeCallback), nullptr});
1626
1638
  napi_status status =
1627
- details::AttachData(_env,
1628
- *this,
1629
- data,
1630
- details::FinalizeData<T, Finalizer>::Wrapper,
1631
- finalizeData);
1639
+ details::AttachData<T, details::FinalizeData<T, Finalizer>::Wrapper>(
1640
+ _env, *this, data, finalizeData);
1632
1641
  if (status != napi_ok) {
1633
1642
  delete finalizeData;
1634
1643
  NAPI_THROW_IF_FAILED_VOID(_env, status);
@@ -1642,12 +1651,9 @@ inline void Object::AddFinalizer(Finalizer finalizeCallback,
1642
1651
  details::FinalizeData<T, Finalizer, Hint>* finalizeData =
1643
1652
  new details::FinalizeData<T, Finalizer, Hint>(
1644
1653
  {std::move(finalizeCallback), finalizeHint});
1645
- napi_status status = details::AttachData(
1646
- _env,
1647
- *this,
1648
- data,
1649
- details::FinalizeData<T, Finalizer, Hint>::WrapperWithHint,
1650
- finalizeData);
1654
+ napi_status status = details::
1655
+ AttachData<T, details::FinalizeData<T, Finalizer, Hint>::WrapperWithHint>(
1656
+ _env, *this, data, finalizeData);
1651
1657
  if (status != napi_ok) {
1652
1658
  delete finalizeData;
1653
1659
  NAPI_THROW_IF_FAILED_VOID(_env, status);
@@ -2623,7 +2629,7 @@ inline Buffer<T> Buffer<T>::New(napi_env env, size_t length) {
2623
2629
  napi_status status =
2624
2630
  napi_create_buffer(env, length * sizeof(T), &data, &value);
2625
2631
  NAPI_THROW_IF_FAILED(env, status, Buffer<T>());
2626
- return Buffer(env, value, length, static_cast<T*>(data));
2632
+ return Buffer(env, value);
2627
2633
  }
2628
2634
 
2629
2635
  #ifndef NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
@@ -2633,7 +2639,7 @@ inline Buffer<T> Buffer<T>::New(napi_env env, T* data, size_t length) {
2633
2639
  napi_status status = napi_create_external_buffer(
2634
2640
  env, length * sizeof(T), data, nullptr, nullptr, &value);
2635
2641
  NAPI_THROW_IF_FAILED(env, status, Buffer<T>());
2636
- return Buffer(env, value, length, data);
2642
+ return Buffer(env, value);
2637
2643
  }
2638
2644
 
2639
2645
  template <typename T>
@@ -2657,7 +2663,7 @@ inline Buffer<T> Buffer<T>::New(napi_env env,
2657
2663
  delete finalizeData;
2658
2664
  NAPI_THROW_IF_FAILED(env, status, Buffer());
2659
2665
  }
2660
- return Buffer(env, value, length, data);
2666
+ return Buffer(env, value);
2661
2667
  }
2662
2668
 
2663
2669
  template <typename T>
@@ -2682,7 +2688,7 @@ inline Buffer<T> Buffer<T>::New(napi_env env,
2682
2688
  delete finalizeData;
2683
2689
  NAPI_THROW_IF_FAILED(env, status, Buffer());
2684
2690
  }
2685
- return Buffer(env, value, length, data);
2691
+ return Buffer(env, value);
2686
2692
  }
2687
2693
  #endif // NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
2688
2694
 
@@ -2699,7 +2705,7 @@ inline Buffer<T> Buffer<T>::NewOrCopy(napi_env env, T* data, size_t length) {
2699
2705
  #ifndef NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
2700
2706
  }
2701
2707
  NAPI_THROW_IF_FAILED(env, status, Buffer<T>());
2702
- return Buffer(env, value, length, data);
2708
+ return Buffer(env, value);
2703
2709
  #endif // NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
2704
2710
  }
2705
2711
 
@@ -2733,7 +2739,7 @@ inline Buffer<T> Buffer<T>::NewOrCopy(napi_env env,
2733
2739
  delete finalizeData;
2734
2740
  NAPI_THROW_IF_FAILED(env, status, Buffer());
2735
2741
  }
2736
- return Buffer(env, value, length, data);
2742
+ return Buffer(env, value);
2737
2743
  #endif // NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
2738
2744
  }
2739
2745
 
@@ -2769,7 +2775,7 @@ inline Buffer<T> Buffer<T>::NewOrCopy(napi_env env,
2769
2775
  delete finalizeData;
2770
2776
  NAPI_THROW_IF_FAILED(env, status, Buffer());
2771
2777
  }
2772
- return Buffer(env, value, length, data);
2778
+ return Buffer(env, value);
2773
2779
  #endif
2774
2780
  }
2775
2781
 
@@ -2793,42 +2799,20 @@ inline void Buffer<T>::CheckCast(napi_env env, napi_value value) {
2793
2799
  }
2794
2800
 
2795
2801
  template <typename T>
2796
- inline Buffer<T>::Buffer() : Uint8Array(), _length(0), _data(nullptr) {}
2802
+ inline Buffer<T>::Buffer() : Uint8Array() {}
2797
2803
 
2798
2804
  template <typename T>
2799
2805
  inline Buffer<T>::Buffer(napi_env env, napi_value value)
2800
- : Uint8Array(env, value), _length(0), _data(nullptr) {}
2801
-
2802
- template <typename T>
2803
- inline Buffer<T>::Buffer(napi_env env, napi_value value, size_t length, T* data)
2804
- : Uint8Array(env, value), _length(length), _data(data) {}
2806
+ : Uint8Array(env, value) {}
2805
2807
 
2806
2808
  template <typename T>
2807
2809
  inline size_t Buffer<T>::Length() const {
2808
- EnsureInfo();
2809
- return _length;
2810
+ return ByteLength() / sizeof(T);
2810
2811
  }
2811
2812
 
2812
2813
  template <typename T>
2813
2814
  inline T* Buffer<T>::Data() const {
2814
- EnsureInfo();
2815
- return _data;
2816
- }
2817
-
2818
- template <typename T>
2819
- inline void Buffer<T>::EnsureInfo() const {
2820
- // The Buffer instance may have been constructed from a napi_value whose
2821
- // length/data are not yet known. Fetch and cache these values just once,
2822
- // since they can never change during the lifetime of the Buffer.
2823
- if (_data == nullptr) {
2824
- size_t byteLength;
2825
- void* voidData;
2826
- napi_status status =
2827
- napi_get_buffer_info(_env, _value, &voidData, &byteLength);
2828
- NAPI_THROW_IF_FAILED_VOID(_env, status);
2829
- _length = byteLength / sizeof(T);
2830
- _data = static_cast<T*>(voidData);
2831
- }
2815
+ return reinterpret_cast<T*>(const_cast<uint8_t*>(Uint8Array::Data()));
2832
2816
  }
2833
2817
 
2834
2818
  ////////////////////////////////////////////////////////////////////////////////
@@ -3157,6 +3141,23 @@ inline RangeError::RangeError() : Error() {}
3157
3141
  inline RangeError::RangeError(napi_env env, napi_value value)
3158
3142
  : Error(env, value) {}
3159
3143
 
3144
+ #if NAPI_VERSION > 8
3145
+ inline SyntaxError SyntaxError::New(napi_env env, const char* message) {
3146
+ return Error::New<SyntaxError>(
3147
+ env, message, std::strlen(message), node_api_create_syntax_error);
3148
+ }
3149
+
3150
+ inline SyntaxError SyntaxError::New(napi_env env, const std::string& message) {
3151
+ return Error::New<SyntaxError>(
3152
+ env, message.c_str(), message.size(), node_api_create_syntax_error);
3153
+ }
3154
+
3155
+ inline SyntaxError::SyntaxError() : Error() {}
3156
+
3157
+ inline SyntaxError::SyntaxError(napi_env env, napi_value value)
3158
+ : Error(env, value) {}
3159
+ #endif // NAPI_VERSION > 8
3160
+
3160
3161
  ////////////////////////////////////////////////////////////////////////////////
3161
3162
  // Reference<T> class
3162
3163
  ////////////////////////////////////////////////////////////////////////////////
@@ -6110,13 +6111,15 @@ inline void ThreadSafeFunction::CallJS(napi_env env,
6110
6111
  return;
6111
6112
  }
6112
6113
 
6113
- if (data != nullptr) {
6114
- auto* callbackWrapper = static_cast<CallbackWrapper*>(data);
6115
- (*callbackWrapper)(env, Function(env, jsCallback));
6116
- delete callbackWrapper;
6117
- } else if (jsCallback != nullptr) {
6118
- Function(env, jsCallback).Call({});
6119
- }
6114
+ details::WrapVoidCallback([&]() {
6115
+ if (data != nullptr) {
6116
+ auto* callbackWrapper = static_cast<CallbackWrapper*>(data);
6117
+ (*callbackWrapper)(env, Function(env, jsCallback));
6118
+ delete callbackWrapper;
6119
+ } else if (jsCallback != nullptr) {
6120
+ Function(env, jsCallback).Call({});
6121
+ }
6122
+ });
6120
6123
  }
6121
6124
 
6122
6125
  ////////////////////////////////////////////////////////////////////////////////
package/napi.h CHANGED
@@ -367,6 +367,10 @@ class Env {
367
367
  } * data;
368
368
  };
369
369
  #endif // NAPI_VERSION > 2
370
+
371
+ #if NAPI_VERSION > 8
372
+ const char* GetModuleFileName() const;
373
+ #endif // NAPI_VERSION > 8
370
374
  };
371
375
 
372
376
  /// A JavaScript value of unknown type.
@@ -1531,11 +1535,6 @@ class Buffer : public Uint8Array {
1531
1535
  T* Data() const;
1532
1536
 
1533
1537
  private:
1534
- mutable size_t _length;
1535
- mutable T* _data;
1536
-
1537
- Buffer(napi_env env, napi_value value, size_t length, T* data);
1538
- void EnsureInfo() const;
1539
1538
  };
1540
1539
 
1541
1540
  /// Holds a counted reference to a value; initially a weak reference unless
@@ -1853,6 +1852,17 @@ class RangeError : public Error {
1853
1852
  RangeError(napi_env env, napi_value value);
1854
1853
  };
1855
1854
 
1855
+ #if NAPI_VERSION > 8
1856
+ class SyntaxError : public Error {
1857
+ public:
1858
+ static SyntaxError New(napi_env env, const char* message);
1859
+ static SyntaxError New(napi_env env, const std::string& message);
1860
+
1861
+ SyntaxError();
1862
+ SyntaxError(napi_env env, napi_value value);
1863
+ };
1864
+ #endif // NAPI_VERSION > 8
1865
+
1856
1866
  class CallbackInfo {
1857
1867
  public:
1858
1868
  CallbackInfo(napi_env env, napi_callback_info info);
@@ -0,0 +1,32 @@
1
+ {
2
+ 'targets': [
3
+ {
4
+ 'target_name': 'node_addon_api',
5
+ 'type': 'none',
6
+ 'sources': [ 'napi.h', 'napi-inl.h' ],
7
+ 'direct_dependent_settings': {
8
+ 'include_dirs': [ '.' ],
9
+ 'includes': ['noexcept.gypi'],
10
+ }
11
+ },
12
+ {
13
+ 'target_name': 'node_addon_api_except',
14
+ 'type': 'none',
15
+ 'sources': [ 'napi.h', 'napi-inl.h' ],
16
+ 'direct_dependent_settings': {
17
+ 'include_dirs': [ '.' ],
18
+ 'includes': ['except.gypi'],
19
+ }
20
+ },
21
+ {
22
+ 'target_name': 'node_addon_api_maybe',
23
+ 'type': 'none',
24
+ 'sources': [ 'napi.h', 'napi-inl.h' ],
25
+ 'direct_dependent_settings': {
26
+ 'include_dirs': [ '.' ],
27
+ 'includes': ['noexcept.gypi'],
28
+ 'defines': ['NODE_ADDON_API_ENABLE_MAYBE']
29
+ }
30
+ },
31
+ ]
32
+ }
package/package.json CHANGED
@@ -407,6 +407,14 @@
407
407
  {
408
408
  "name": "Caleb Hearon",
409
409
  "url": "https://github.com/chearon"
410
+ },
411
+ {
412
+ "name": "Marx",
413
+ "url": "https://github.com/MarxJiao"
414
+ },
415
+ {
416
+ "name": "Ömer AKGÜL",
417
+ "url": "https://github.com/tuhalf"
410
418
  }
411
419
  ],
412
420
  "description": "Node.js API (Node-API)",
@@ -420,7 +428,7 @@
420
428
  "eslint-plugin-import": "^2.24.2",
421
429
  "eslint-plugin-node": "^11.1.0",
422
430
  "eslint-plugin-promise": "^5.1.0",
423
- "fs-extra": "^9.0.1",
431
+ "fs-extra": "^11.1.1",
424
432
  "path": "^0.12.7",
425
433
  "pre-commit": "^1.2.2",
426
434
  "safe-buffer": "^5.1.1"
@@ -467,6 +475,9 @@
467
475
  "lint:fix": "node tools/clang-format --fix && node tools/eslint-format --fix"
468
476
  },
469
477
  "pre-commit": "lint",
470
- "version": "7.0.0",
471
- "support": true
478
+ "version": "8.0.0",
479
+ "support": true,
480
+ "engines": {
481
+ "node": "^18 || ^20 || >= 21"
482
+ }
472
483
  }