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:
|
---|
5 |
|
---|
6 | ```shell
|
---|
7 | git branch -m master main
|
---|
8 | git fetch origin
|
---|
9 | git branch -u origin/main main
|
---|
10 | ```
|
---|
11 |
|
---|
12 | # **node-addon-api module**
|
---|
13 | This module contains **header-only C++ wrapper classes** which simplify
|
---|
14 | the use of the C based [Node-API](https://nodejs.org/dist/latest/docs/api/n-api.html)
|
---|
15 | provided by Node.js when using C++. It provides a C++ object model
|
---|
16 | and exception handling semantics with low overhead.
|
---|
17 |
|
---|
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.
|
---|
54 |
|
---|
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**.
|
---|
58 |
|
---|
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.
|
---|
62 |
|
---|
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.1.1**
|
---|
74 |
|
---|
75 | (See [CHANGELOG.md](CHANGELOG.md) for complete Changelog)
|
---|
76 |
|
---|
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 | node-addon-api is based on [Node-API](https://nodejs.org/api/n-api.html) and supports using different Node-API versions.
|
---|
82 | This allows addons built with it to run with Node.js versions which support the targeted Node-API version.
|
---|
83 | **However** the node-addon-api support model is to support only the active LTS Node.js versions. This means that
|
---|
84 | every year there will be a new major which drops support for the Node.js LTS version which has gone out of service.
|
---|
85 |
|
---|
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 | - [SyntaxError](doc/syntax_error.md)
|
---|
136 | - [Object Lifetime Management](doc/object_lifetime_management.md)
|
---|
137 | - [HandleScope](doc/handle_scope.md)
|
---|
138 | - [EscapableHandleScope](doc/escapable_handle_scope.md)
|
---|
139 | - [Memory Management](doc/memory_management.md)
|
---|
140 | - [Async Operations](doc/async_operations.md)
|
---|
141 | - [AsyncWorker](doc/async_worker.md)
|
---|
142 | - [AsyncContext](doc/async_context.md)
|
---|
143 | - [AsyncWorker Variants](doc/async_worker_variants.md)
|
---|
144 | - [Thread-safe Functions](doc/threadsafe.md)
|
---|
145 | - [ThreadSafeFunction](doc/threadsafe_function.md)
|
---|
146 | - [TypedThreadSafeFunction](doc/typed_threadsafe_function.md)
|
---|
147 | - [Promises](doc/promises.md)
|
---|
148 | - [Version management](doc/version_management.md)
|
---|
149 |
|
---|
150 | <a name="examples"></a>
|
---|
151 |
|
---|
152 | ### **Examples**
|
---|
153 |
|
---|
154 | Are you new to **node-addon-api**? Take a look at our **[examples](https://github.com/nodejs/node-addon-examples)**
|
---|
155 |
|
---|
156 | - **[Hello World](https://github.com/nodejs/node-addon-examples/tree/main/src/1-getting-started/1_hello_world)**
|
---|
157 | - **[Pass arguments to a function](https://github.com/nodejs/node-addon-examples/tree/main/src/1-getting-started/2_function_arguments/node-addon-api)**
|
---|
158 | - **[Callbacks](https://github.com/nodejs/node-addon-examples/tree/main/src/1-getting-started/3_callbacks/node-addon-api)**
|
---|
159 | - **[Object factory](https://github.com/nodejs/node-addon-examples/tree/main/src/1-getting-started/4_object_factory/node-addon-api)**
|
---|
160 | - **[Function factory](https://github.com/nodejs/node-addon-examples/tree/main/src/1-getting-started/5_function_factory/node-addon-api)**
|
---|
161 | - **[Wrapping C++ Object](https://github.com/nodejs/node-addon-examples/tree/main/src/1-getting-started/6_object_wrap/node-addon-api)**
|
---|
162 | - **[Factory of wrapped object](https://github.com/nodejs/node-addon-examples/tree/main/src/1-getting-started/7_factory_wrap/node-addon-api)**
|
---|
163 | - **[Passing wrapped object around](https://github.com/nodejs/node-addon-examples/tree/main/src/2-js-to-native-conversion/8_passing_wrapped/node-addon-api)**
|
---|
164 |
|
---|
165 | <a name="tests"></a>
|
---|
166 |
|
---|
167 | ### **Tests**
|
---|
168 |
|
---|
169 | To run the **node-addon-api** tests do:
|
---|
170 |
|
---|
171 | ```
|
---|
172 | npm install
|
---|
173 | npm test
|
---|
174 | ```
|
---|
175 |
|
---|
176 | To avoid testing the deprecated portions of the API run
|
---|
177 | ```
|
---|
178 | npm install
|
---|
179 | npm test --disable-deprecated
|
---|
180 | ```
|
---|
181 |
|
---|
182 | To run the tests targeting a specific version of Node-API run
|
---|
183 | ```
|
---|
184 | npm install
|
---|
185 | export NAPI_VERSION=X
|
---|
186 | npm test --NAPI_VERSION=X
|
---|
187 | ```
|
---|
188 |
|
---|
189 | where X is the version of Node-API you want to target.
|
---|
190 |
|
---|
191 | To run a specific unit test, filter conditions are available
|
---|
192 |
|
---|
193 | **Example:**
|
---|
194 | compile and run only tests on objectwrap.cc and objectwrap.js
|
---|
195 | ```
|
---|
196 | npm run unit --filter=objectwrap
|
---|
197 | ```
|
---|
198 |
|
---|
199 | Multiple unit tests cane be selected with wildcards
|
---|
200 |
|
---|
201 | **Example:**
|
---|
202 | compile and run all test files ending with "reference" -> function_reference.cc, object_reference.cc, reference.cc
|
---|
203 | ```
|
---|
204 | npm run unit --filter=*reference
|
---|
205 | ```
|
---|
206 |
|
---|
207 | Multiple filter conditions can be joined to broaden the test selection
|
---|
208 |
|
---|
209 | **Example:**
|
---|
210 | compile and run all tests under folders threadsafe_function and typed_threadsafe_function and also the objectwrap.cc file
|
---|
211 | npm run unit --filter='*function objectwrap'
|
---|
212 |
|
---|
213 | ### **Debug**
|
---|
214 |
|
---|
215 | To run the **node-addon-api** tests with `--debug` option:
|
---|
216 |
|
---|
217 | ```
|
---|
218 | npm run-script dev
|
---|
219 | ```
|
---|
220 |
|
---|
221 | If you want a faster build, you might use the following option:
|
---|
222 |
|
---|
223 | ```
|
---|
224 | npm run-script dev:incremental
|
---|
225 | ```
|
---|
226 |
|
---|
227 | Take a look and get inspired by our **[test suite](https://github.com/nodejs/node-addon-api/tree/HEAD/test)**
|
---|
228 |
|
---|
229 | ### **Benchmarks**
|
---|
230 |
|
---|
231 | You can run the available benchmarks using the following command:
|
---|
232 |
|
---|
233 | ```
|
---|
234 | npm run-script benchmark
|
---|
235 | ```
|
---|
236 |
|
---|
237 | See [benchmark/README.md](benchmark/README.md) for more details about running and adding benchmarks.
|
---|
238 |
|
---|
239 | <a name="resources"></a>
|
---|
240 |
|
---|
241 | ### **More resource and info about native Addons**
|
---|
242 | - **[C++ Addons](https://nodejs.org/dist/latest/docs/api/addons.html)**
|
---|
243 | - **[Node-API](https://nodejs.org/dist/latest/docs/api/n-api.html)**
|
---|
244 | - **[Node-API - Next Generation Node API for Native Modules](https://youtu.be/-Oniup60Afs)**
|
---|
245 | - **[How We Migrated Realm JavaScript From NAN to Node-API](https://developer.mongodb.com/article/realm-javascript-nan-to-n-api)**
|
---|
246 |
|
---|
247 | As node-addon-api's core mission is to expose the plain C Node-API as C++
|
---|
248 | wrappers, tools that facilitate n-api/node-addon-api providing more
|
---|
249 | convenient patterns for developing a Node.js add-on with n-api/node-addon-api
|
---|
250 | can be published to NPM as standalone packages. It is also recommended to tag
|
---|
251 | such packages with `node-addon-api` to provide more visibility to the community.
|
---|
252 |
|
---|
253 | Quick links to NPM searches: [keywords:node-addon-api](https://www.npmjs.com/search?q=keywords%3Anode-addon-api).
|
---|
254 |
|
---|
255 | <a name="other-bindings"></a>
|
---|
256 |
|
---|
257 | ### **Other bindings**
|
---|
258 |
|
---|
259 | - **[napi-rs](https://napi.rs)** - (`Rust`)
|
---|
260 |
|
---|
261 | <a name="badges"></a>
|
---|
262 |
|
---|
263 | ### **Badges**
|
---|
264 |
|
---|
265 | The use of badges is recommended to indicate the minimum version of Node-API
|
---|
266 | required for the module. This helps to determine which Node.js major versions are
|
---|
267 | supported. Addon maintainers can consult the [Node-API support matrix][] to determine
|
---|
268 | which Node.js versions provide a given Node-API version. The following badges are
|
---|
269 | available:
|
---|
270 |
|
---|
271 | ![Node-API v1 Badge](https://github.com/nodejs/abi-stable-node/blob/doc/assets/Node-API%20v1%20Badge.svg)
|
---|
272 | ![Node-API v2 Badge](https://github.com/nodejs/abi-stable-node/blob/doc/assets/Node-API%20v2%20Badge.svg)
|
---|
273 | ![Node-API v3 Badge](https://github.com/nodejs/abi-stable-node/blob/doc/assets/Node-API%20v3%20Badge.svg)
|
---|
274 | ![Node-API v4 Badge](https://github.com/nodejs/abi-stable-node/blob/doc/assets/Node-API%20v4%20Badge.svg)
|
---|
275 | ![Node-API v5 Badge](https://github.com/nodejs/abi-stable-node/blob/doc/assets/Node-API%20v5%20Badge.svg)
|
---|
276 | ![Node-API v6 Badge](https://github.com/nodejs/abi-stable-node/blob/doc/assets/Node-API%20v6%20Badge.svg)
|
---|
277 | ![Node-API v7 Badge](https://github.com/nodejs/abi-stable-node/blob/doc/assets/Node-API%20v7%20Badge.svg)
|
---|
278 | ![Node-API v8 Badge](https://github.com/nodejs/abi-stable-node/blob/doc/assets/Node-API%20v8%20Badge.svg)
|
---|
279 | ![Node-API v9 Badge](https://github.com/nodejs/abi-stable-node/blob/doc/assets/Node-API%20v9%20Badge.svg)
|
---|
280 | ![Node-API Experimental Version Badge](https://github.com/nodejs/abi-stable-node/blob/doc/assets/Node-API%20Experimental%20Version%20Badge.svg)
|
---|
281 |
|
---|
282 | ## **Contributing**
|
---|
283 |
|
---|
284 | We love contributions from the community to **node-addon-api**!
|
---|
285 | See [CONTRIBUTING.md](CONTRIBUTING.md) for more details on our philosophy around extending this module.
|
---|
286 |
|
---|
287 | <a name="contributors"></a>
|
---|
288 |
|
---|
289 | ## Team members
|
---|
290 |
|
---|
291 | ### Active
|
---|
292 | | Name | GitHub Link |
|
---|
293 | | ------------------- | ----------------------------------------------------- |
|
---|
294 | | Anna Henningsen | [addaleax](https://github.com/addaleax) |
|
---|
295 | | Chengzhong Wu | [legendecas](https://github.com/legendecas) |
|
---|
296 | | Jack Xia | [JckXia](https://github.com/JckXia) |
|
---|
297 | | Kevin Eady | [KevinEady](https://github.com/KevinEady) |
|
---|
298 | | Michael Dawson | [mhdawson](https://github.com/mhdawson) |
|
---|
299 | | Nicola Del Gobbo | [NickNaso](https://github.com/NickNaso) |
|
---|
300 | | Vladimir Morozov | [vmoroz](https://github.com/vmoroz) |
|
---|
301 |
|
---|
302 | ### Emeritus
|
---|
303 | | Name | GitHub Link |
|
---|
304 | | ------------------- | ----------------------------------------------------- |
|
---|
305 | | Arunesh Chandra | [aruneshchandra](https://github.com/aruneshchandra) |
|
---|
306 | | Benjamin Byholm | [kkoopa](https://github.com/kkoopa) |
|
---|
307 | | Gabriel Schulhof | [gabrielschulhof](https://github.com/gabrielschulhof) |
|
---|
308 | | Hitesh Kanwathirtha | [digitalinfinity](https://github.com/digitalinfinity) |
|
---|
309 | | Jason Ginchereau | [jasongin](https://github.com/jasongin) |
|
---|
310 | | Jim Schlight | [jschlight](https://github.com/jschlight) |
|
---|
311 | | Sampson Gao | [sampsongao](https://github.com/sampsongao) |
|
---|
312 | | Taylor Woll | [boingoing](https://github.com/boingoing) |
|
---|
313 |
|
---|
314 | <a name="license"></a>
|
---|
315 |
|
---|
316 | Licensed under [MIT](./LICENSE.md)
|
---|
317 |
|
---|
318 | [ABI stability guide]: https://nodejs.org/en/docs/guides/abi-stability/
|
---|
319 | [Node-API support matrix]: https://nodejs.org/dist/latest/docs/api/n-api.html#n_api_n_api_version_matrix
|
---|