├── .gitignore ├── .gitmodules ├── .github ├── dependabot.yml └── workflows │ └── rust.yml ├── Cargo.toml ├── src └── lib.rs ├── update-node-submodule.sh ├── README.md └── CHANGELOG.md /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | 5 | .idea -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "node"] 2 | path = node 3 | url = https://github.com/nodejs/node.git 4 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "cargo" 4 | directory: "/" 5 | schedule: 6 | interval: "daily" 7 | commit-message: 8 | prefix: ":arrow_up:" 9 | -------------------------------------------------------------------------------- /.github/workflows/rust.yml: -------------------------------------------------------------------------------- 1 | name: Rust 2 | 3 | on: 4 | push: 5 | branches: [master] 6 | pull_request: 7 | branches: [master] 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v2 15 | with: 16 | submodules: true 17 | fetch-depth: 0 18 | - name: Install llvm 19 | run: sudo apt-get install -y llvm 20 | - name: Build 21 | run: cargo build --verbose 22 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "nodejs-sys" 3 | version = "0.15.0" 4 | authors = ["Elmar Athmer "] 5 | edition = "2018" 6 | build = "build.rs" 7 | categories = ["external-ffi-bindings"] 8 | keywords = ["ffi", "node", "nodejs", "napi", "n-api"] 9 | repository = "https://github.com/elmarx/nodejs-sys" 10 | license = "MIT" 11 | description = "Native bindings to the nodejs' n-api" 12 | include = [ 13 | "node/src/node_api.h", 14 | "node/src/js_native_api.h", 15 | "node/src/js_native_api_types.h", 16 | "node/src/node_api_types.h", 17 | "**/*.rs", 18 | "Cargo.toml", 19 | ] 20 | readme = "README.md" 21 | 22 | [dependencies] 23 | 24 | [build-dependencies] 25 | bindgen = "0.69.1" 26 | 27 | [features] 28 | # no features by default 29 | default = [] 30 | 31 | # enable the experimental API https://nodejs.org/dist/latest-v14.x/docs/api/n-api.html#n_api_usage 32 | experimental = [] 33 | 34 | # select API version of supported versions 35 | napi_v5 = [] 36 | napi_v6 = [] 37 | napi_v7 = [] 38 | napi_v8 = [] 39 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! Low level bindings to nodejs' [N-API](https://nodejs.org/dist/latest-v14.x/docs/api/n-api.html) 2 | //! 3 | //! 4 | //! # Features 5 | //! 6 | //! Different API versions may be selected via feature-flag. See the [N-API Version Matrix](https://nodejs.org/dist/latest-v14.x/docs/api/n-api.html#n_api_n_api_version_matrix) for details. 7 | //! 8 | //! - `napi_v5` supported by all [actively maintained](https://nodejs.org/en/about/releases/) NodeJS releases 9 | //! - `napi_v6` supported by all [actively maintained](https://nodejs.org/en/about/releases/) NodeJS releases 10 | //! - `napi_v7` supported by all [actively maintained](https://nodejs.org/en/about/releases/) NodeJS releases 11 | //! - `napi_v8` requires at least `15.12.0`, this is the **default** (if no flag is given) 12 | //! - the experimental N-API features may be enabled via feature flag `experimental` (*off* by default) 13 | 14 | #![allow(non_upper_case_globals)] 15 | #![allow(non_camel_case_types)] 16 | #![allow(non_snake_case)] 17 | 18 | include!(concat!(env!("OUT_DIR"), "/bindings.rs")); 19 | -------------------------------------------------------------------------------- /update-node-submodule.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | set -o errexit 4 | set -o nounset 5 | 6 | cd node 7 | git fetch 8 | MOST_RECENT_VERSION=${1:-$(git tag | sort -V | tail -n 1 )} 9 | CURRENT_VERSION=$(git describe --tags HEAD) 10 | 11 | if [ "$MOST_RECENT_VERSION" = "$CURRENT_VERSION" ]; then 12 | echo "$CURRENT_VERSION is up to date, nothing to do" 13 | exit 0 14 | fi 15 | 16 | CHANGED_FILES=$(git diff --name-only "$CURRENT_VERSION".."$MOST_RECENT_VERSION" src/node_api.h src/js_native_api.h src/js_native_api_types.h src/node_api_types.h) 17 | 18 | if [ -z "$CHANGED_FILES" ]; then 19 | echo "Relevant files did not change between $CURRENT_VERSION..$MOST_RECENT_VERSION" 20 | exit 0 21 | else 22 | CHANGELOG=$(mktemp) 23 | echo "# Node ${MOST_RECENT_VERSION}\n" >> $CHANGELOG 24 | echo "affected files:" >> $CHANGELOG 25 | for i in $CHANGED_FILES; do 26 | echo "* [$i](https://github.com/nodejs/node/blob/$MOST_RECENT_VERSION/$i)" >> $CHANGELOG 27 | done 28 | 29 | echo "\nupstream changelog:" >> $CHANGELOG 30 | # get a list of (space-separated) commit-ids we can easily loop over 31 | for i in $(git log --format='%H' "$CURRENT_VERSION".."$MOST_RECENT_VERSION" src/node_api.h src/js_native_api.h src/js_native_api_types.h src/node_api_types.h); do 32 | # now get the actual text of the commit (which contains multiple spaces) 33 | echo "* $(git log -1 --format='[%h](https://github.com/nodejs/node/commit/%H) %s' $i)" >> $CHANGELOG 34 | done 35 | 36 | echo "" >> $CHANGELOG 37 | fi 38 | 39 | # for dry run mode: 40 | # cat $CHANGELOG 41 | # exit 0; 42 | 43 | git checkout "$MOST_RECENT_VERSION" 44 | 45 | if ! grep "NAPI_VERSION 8" src/node_version.h; then 46 | echo "Detected new NAPI_VERSION. Refusing to auto-update. Please add feature-flag etc." 47 | exit 1 48 | fi 49 | 50 | # back to src-root 51 | cd .. 52 | cat CHANGELOG.md >> $CHANGELOG 53 | cat $CHANGELOG > CHANGELOG.md 54 | git commit -m ":arrow_up: update node $CURRENT_VERSION -> $MOST_RECENT_VERSION" node CHANGELOG.md 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Rust build](https://github.com/elmarx/nodejs-sys/workflows/Rust/badge.svg)](https://github.com/elmarx/nodejs-sys/actions?query=workflow%3ARust) [![crates.io badge](https://img.shields.io/crates/v/nodejs-sys.svg)](https://crates.io/crates/nodejs-sys) [![docs.rs badge](https://docs.rs/nodejs-sys/badge.svg)](https://docs.rs/nodejs-sys) 2 | 3 | nodejs-sys 4 | ---------- 5 | 6 | Bindings for NodeJS' [N-API](https://nodejs.org/dist/latest-v14.x/docs/api/n-api.html). 7 | 8 | Requirements 9 | ============ 10 | 11 | This crate needs `llvm` at build-time, since it generates bindings at build-time (by using [bindgen](https://docs.rs/bindgen/)). 12 | 13 | For Debian/Ubuntu that's a simple `apt install llvm libclang-dev`. 14 | 15 | Features 16 | ======== 17 | 18 | Different API versions may be selected via feature-flag. See the [N-API Version Matrix](https://nodejs.org/dist/latest-v14.x/docs/api/n-api.html#n_api_n_api_version_matrix) for details. 19 | 20 | - `napi_v5` supported by all [actively maintained](https://nodejs.org/en/about/releases/) NodeJS releases 21 | - `napi_v6` supported by all [actively maintained](https://nodejs.org/en/about/releases/) NodeJS releases 22 | - `napi_v7` supported by all [actively maintained](https://nodejs.org/en/about/releases/) NodeJS releases 23 | - `napi_v8` requires at least `15.12.0`, this is the **default** (if no flag is given) 24 | - the experimental N-API features may be enabled via feature flag `experimental` (*off* by default) 25 | 26 | Updates 27 | ======= 28 | 29 | Not all NodeJS Versions change the N-API, so `nodejs-sys` gets an update only if the relevant headers have been changed between node versions. 30 | 31 | See the (autogenerated) changelog for links to detailed upstream commits. 32 | 33 | Usage 34 | ===== 35 | 36 | This crate is very low-level. See [neon](https://neon-bindings.com/), they provide [N-API Support](https://github.com/neon-bindings/neon/issues/444). 37 | 38 | But of course you may also use this crate directly, [LogRocket](https://logrocket.com/) has a nice blog post: 39 | [Rust and Node.js: A match made in heaven](https://blog.logrocket.com/rust-and-node-js-a-match-made-in-heaven/). -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Node v19.9.0\n 2 | affected files: 3 | * [src/node_api.h](https://github.com/nodejs/node/blob/v19.9.0/src/node_api.h) 4 | \nupstream changelog: 5 | * [ca981be2b9](https://github.com/nodejs/node/commit/ca981be2b9c4797af1a00c2ec74d51c4f2fac4b2) node-api: deprecate napi_module_register 6 | 7 | # Node v19.8.0\n 8 | affected files: 9 | * [src/node_api.h](https://github.com/nodejs/node/blob/v19.8.0/src/node_api.h) 10 | \nupstream changelog: 11 | * [58b1f33bd7](https://github.com/nodejs/node/commit/58b1f33bd7ac5a71b791e7f180de7d77084fb008) node-api: add __wasm32__ guards on async works 12 | 13 | # Node v19.5.0\n 14 | affected files: 15 | * [src/js_native_api.h](https://github.com/nodejs/node/blob/v19.5.0/src/js_native_api.h) 16 | \nupstream changelog: 17 | * [bd98e5baba](https://github.com/nodejs/node/commit/bd98e5baba78b2fb0b3eb02f6468a4f2090363da) node-api: disambiguate napi_add_finalizer 18 | 19 | # Node v19.2.0\n 20 | affected files: 21 | * [src/node_api.h](https://github.com/nodejs/node/blob/v19.2.0/src/node_api.h) 22 | * [src/node_api_types.h](https://github.com/nodejs/node/blob/v19.2.0/src/node_api_types.h) 23 | \nupstream changelog: 24 | * [4a4f2802ec](https://github.com/nodejs/node/commit/4a4f2802ec4cb22a111363ea027ebb9be4bc8f6b) node-api: declare type napi_cleanup_hook 25 | 26 | # Node v19.1.0\n 27 | affected files: 28 | * [src/js_native_api.h](https://github.com/nodejs/node/blob/v19.1.0/src/js_native_api.h) 29 | * [src/js_native_api_types.h](https://github.com/nodejs/node/blob/v19.1.0/src/js_native_api_types.h) 30 | * [src/node_api.h](https://github.com/nodejs/node/blob/v19.1.0/src/node_api.h) 31 | \nupstream changelog: 32 | * [aaca54c5c0](https://github.com/nodejs/node/commit/aaca54c5c0093353afb5fbaf998014dd7e7dab3c) node-api: handle no support for external buffers 33 | * [472edc775d](https://github.com/nodejs/node/commit/472edc775d683aed2d9ed39ca7cf381f3e7e3ce2) src: disambiguate terms used to refer to builtins and addons 34 | * [fb744749e2](https://github.com/nodejs/node/commit/fb744749e204c349f76df79b3c513c7e0df7e4c6) node-api: explicitly set __cdecl for API functions 35 | 36 | # Node v18.10.0 37 | affected files: 38 | * [src/js_native_api.h](https://github.com/nodejs/node/blob/v18.10.0/src/js_native_api.h) 39 | * [src/js_native_api_types.h](https://github.com/nodejs/node/blob/v18.10.0/src/js_native_api_types.h) 40 | * [src/node_api.h](https://github.com/nodejs/node/blob/v18.10.0/src/node_api.h) 41 | * [src/node_api_types.h](https://github.com/nodejs/node/blob/v18.10.0/src/node_api_types.h) 42 | \nupstream changelog: 43 | * [ab73cc8706](https://github.com/nodejs/node/commit/ab73cc8706b0fc5716d3b798d86a732edd5972a1) src: disambiguate terms used to refer to builtins and addons 44 | * [037ff3da6d](https://github.com/nodejs/node/commit/037ff3da6d65954e3d3106b40a84d50851c1fbfb) node-api: explicitly set __cdecl for API functions 45 | * [44fdf953ba](https://github.com/nodejs/node/commit/44fdf953ba435a46a3525bc877069044a3157e7d) node-api,src: fix module registration in MSVC C++ 46 | * [718be08686](https://github.com/nodejs/node/commit/718be08686312d19645861b9715491a49ee3c9cb) node-api: format Node-API related code 47 | * [726711fe4e](https://github.com/nodejs/node/commit/726711fe4e2de7992b2ecc6921d3b083be41f950) node-api: add node_api_symbol_for() 48 | * [4265f2769b](https://github.com/nodejs/node/commit/4265f2769bf91c0edb002da675fce98bbf61de04) src,doc: add SyntaxError napi support 49 | 50 | # Node v17.2.0 51 | 52 | affected files: 53 | * [src/js_native_api.h](https://github.com/nodejs/node/blob/v17.2.0/src/js_native_api.h) 54 | 55 | upstream changelog: 56 | * [88b57bc9d3](https://github.com/nodejs/node/commit/88b57bc9d3bab7998c3de0aa787b88233cf46ed4) src,doc: add SyntaxError napi support 57 | * [d15475578a](https://github.com/nodejs/node/commit/d15475578abfcc7e0a420719b8d55a6b3ab4f198) node-api: define version 8 58 | * [ad3ebed046](https://github.com/nodejs/node/commit/ad3ebed046ef457530b046f2a62313a7e16b7e29) node-api: allow retrieval of add-on file name 59 | 60 | # Node v15.12.0 61 | 62 | affected files: 63 | * [src/js_native_api.h](https://github.com/nodejs/node/blob/v15.12.0/src/js_native_api.h) 64 | * [src/js_native_api_types.h](https://github.com/nodejs/node/blob/v15.12.0/src/js_native_api_types.h) 65 | * [src/node_api.h](https://github.com/nodejs/node/blob/v15.12.0/src/node_api.h) 66 | * [src/node_api_types.h](https://github.com/nodejs/node/blob/v15.12.0/src/node_api_types.h) 67 | 68 | upstream changelog: 69 | * [a86334fbb9](https://github.com/nodejs/node/commit/a86334fbb92e3776d0055563f49fad4a0f728554) node-api: define version 8 70 | 71 | # Node v15.9.0 72 | 73 | affected files: 74 | * [src/node_api.h](https://github.com/nodejs/node/blob/v15.9.0/src/node_api.h) 75 | 76 | upstream changelog: 77 | * [061939d2f6](https://github.com/nodejs/node/commit/061939d2f6fbc86ee854481dbfa0aa762a2f591f) node-api: allow retrieval of add-on file name 78 | 79 | # Node v15.0.1 80 | 81 | affected files: 82 | * [src/js_native_api.h](https://github.com/nodejs/node/blob/v15.0.1/src/js_native_api.h) 83 | 84 | upstream changelog: 85 | * [19f14517c7](https://github.com/nodejs/node/commit/19f14517c7637b0e277b4fc3fdfe4473e5cc1262) n-api: support for object freeze/seal 86 | * [ff38165820](https://github.com/nodejs/node/commit/ff38165820da2a9eaddabbce23f3e75aa502900b) src: allow N-API addon in `AddLinkedBinding()` 87 | * [31b3202d59](https://github.com/nodejs/node/commit/31b3202d5902e7fa105949758677ea09643d8c5f) n-api: create N-API version 7 88 | * [c9506a8f3e](https://github.com/nodejs/node/commit/c9506a8f3e9bc5c679151feb39198023154464ab) n-api: add more property defaults 89 | * [0848f56cb3](https://github.com/nodejs/node/commit/0848f56cb39432090cdb99af9b8541fbc1a2849c) n-api: re-implement async env cleanup hooks 90 | * [22cbbcf9d9](https://github.com/nodejs/node/commit/22cbbcf9d9374d4b663bf1409f292212fa57623a) n-api,src: provide asynchronous cleanup hooks 91 | * [cc7ec889e8](https://github.com/nodejs/node/commit/cc7ec889e863433c248bc4b5c8e33f61ccc40f29) n-api: support type-tagging objects 92 | * [b327d335ff](https://github.com/nodejs/node/commit/b327d335ff6bc48c3c2aaedccfa9c40522f6b32f) n-api: add version to wasm registration 93 | * [b4ede54a7d](https://github.com/nodejs/node/commit/b4ede54a7d235cfe58265b2b8c455298698460a8) napi: add __wasm32__ guards 94 | * [b18d8dde84](https://github.com/nodejs/node/commit/b18d8dde847e1bff188c6cfb2d65a96209146c2c) Revert "n-api: detect deadlocks in thread-safe function" 95 | * [d26ca06c16](https://github.com/nodejs/node/commit/d26ca06c16f497ffa5ac4845a27922d5058a9318) n-api: detect deadlocks in thread-safe function 96 | * [d3d5eca657](https://github.com/nodejs/node/commit/d3d5eca657474f25fab47036fef9469efc211d8a) Revert "n-api: detect deadlocks in thread-safe function" 97 | * [aeb7084fe6](https://github.com/nodejs/node/commit/aeb7084fe6446350ec032e9819746126811bf44f) n-api: detect deadlocks in thread-safe function 98 | 99 | # Node v14.14.0 100 | 101 | affected files: 102 | * [src/js_native_api.h](https://github.com/nodejs/node/blob/v14.14.0/src/js_native_api.h) 103 | 104 | upstream changelog: 105 | * [c995242068](https://github.com/nodejs/node/commit/c995242068f364292bf90c6f5a5fa6bda662896d) n-api: support for object freeze/seal 106 | 107 | # Node v14.13.0 108 | 109 | affected files: 110 | * [src/node_api.h](https://github.com/nodejs/node/blob/v14.13.0/src/node_api.h) 111 | 112 | upstream changelog: 113 | * [0d8eaa3942](https://github.com/nodejs/node/commit/0d8eaa3942f289874ed8c5d2a9468ba9c9ec45c8) src: allow N-API addon in `AddLinkedBinding()` 114 | 115 | # v0.8.0 N-API version 7 116 | 117 | Node.js v 14.12.0 [introduced N-API version 7](https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V14.md#14.12.0). 118 | So N-API version 7 is the default, but v6 (and v5) may be selected via feature-flag. 119 | 120 | # Node v14.12.0 121 | 122 | affected files: 123 | * [src/js_native_api.h](https://github.com/nodejs/node/blob/v14.12.0/src/js_native_api.h) 124 | * [src/js_native_api_types.h](https://github.com/nodejs/node/blob/v14.12.0/src/js_native_api_types.h) 125 | 126 | upstream changelog: 127 | * [ca1181615e](https://github.com/nodejs/node/commit/ca1181615e961ec948587aa6f8b7e46efd7cbd71) n-api: create N-API version 7 128 | * [7f3b2b2a1f](https://github.com/nodejs/node/commit/7f3b2b2a1f2b2fa25adf9c4ea261f2a99ddd74aa) n-api: add more property defaults 129 | 130 | # v0.7.0 `key_filter` and `property_attributes` are now bitfields 131 | 132 | These enums are supposed to be combined using `|`, but the "rustified" 133 | enum configuration doesn't allow it. Bindgen has a bitfield enum style 134 | which _does_ allow `|`-ing values. 135 | 136 | N-API currently includes two enums used as bitfields: 137 | [`napi_key_filter`](https://nodejs.org/api/n-api.html#n_api_napi_key_filter) and 138 | [`napi_property_attributes`](https://nodejs.org/api/n-api.html#n_api_napi_property_attributes). 139 | 140 | # Node v14.11.0 141 | 142 | affected files: 143 | * [src/node_api.h](https://github.com/nodejs/node/blob/v14.11.0/src/node_api.h) 144 | * [src/node_api_types.h](https://github.com/nodejs/node/blob/v14.11.0/src/node_api_types.h) 145 | 146 | upstream changelog: 147 | * [3c32fe09e9](https://github.com/nodejs/node/commit/3c32fe09e9354479a2527bdd7484d6efab39f864) n-api: re-implement async env cleanup hooks 148 | 149 | # v0.6.0 breaking change — `size_t` is now `usize` 150 | 151 | `bindgen` switched the default output for parameters with the `size_t` C type to `u32`/`u64` depending on the target platform. 152 | Per [rust-lang/rust-bindgen#1671](https://github.com/rust-lang/rust-bindgen/issues/1671), the old behaviour where 153 | `size_t` would just output `usize` is not _technically_ correct, because the C standard has a slightly different definition for `size_t`. 154 | 155 | It should be correct for all platforms that Node.js supports though. bindgen added a toggle to opt back in to the old behaviour. 156 | Since bindgen had been doing this for years without issue, I think we're safe. 157 | 158 | # Node v14.8.0 159 | 160 | affected files: 161 | * [src/js_native_api.h](https://github.com/nodejs/node/blob/v14.8.0/src/js_native_api.h) 162 | * [src/js_native_api_types.h](https://github.com/nodejs/node/blob/v14.8.0/src/js_native_api_types.h) 163 | * [src/node_api.h](https://github.com/nodejs/node/blob/v14.8.0/src/node_api.h) 164 | * [src/node_api_types.h](https://github.com/nodejs/node/blob/v14.8.0/src/node_api_types.h) 165 | 166 | upstream changelog: 167 | * [8630f34776](https://github.com/nodejs/node/commit/8630f3477697835719df93dbc49d03f60cdf2b31) n-api,src: provide asynchronous cleanup hooks 168 | * [8cc9e5eb52](https://github.com/nodejs/node/commit/8cc9e5eb52dbbff49a594c2c8c07032d0b8f6d98) n-api: support type-tagging objects 169 | 170 | # Node v14.5.0 171 | 172 | affected files: 173 | * [src/js_native_api.h](https://github.com/nodejs/node/blob/v14.5.0/src/js_native_api.h) 174 | * [src/js_native_api_types.h](https://github.com/nodejs/node/blob/v14.5.0/src/js_native_api_types.h) 175 | * [src/node_api.h](https://github.com/nodejs/node/blob/v14.5.0/src/node_api.h) 176 | 177 | upstream changelog: 178 | * [ac41bf03fa](https://github.com/nodejs/node/commit/ac41bf03fa6b8f1d78d8ec150481553d765ac290) n-api: add version to wasm registration 179 | * [9148e01e76](https://github.com/nodejs/node/commit/9148e01e7612f886a6fe6563e1ad7bb20e7beac1) napi: add __wasm32__ guards 180 | * [f4cfe94d90](https://github.com/nodejs/node/commit/f4cfe94d90c59e0e6b3cdbdad333f71c9ef20216) Revert "n-api: detect deadlocks in thread-safe function" 181 | 182 | # Node v14.1.0 183 | 184 | affected files: 185 | * [src/js_native_api_types.h](https://github.com/nodejs/node/blob/v14.1.0/src/js_native_api_types.h) 186 | 187 | upstream changelog: 188 | * [861eb39307](https://github.com/nodejs/node/commit/861eb39307d68640305ad8cb456ecfa8ed25ffa3) n-api: detect deadlocks in thread-safe function 189 | 190 | --------------------------------------------------------------------------------