Skip to content

feat: Node-API 10 binding with prebuilds (unblock Node.js upgrades) - #1

Merged
lyesadmi merged 6 commits into
masterfrom
feat/napi
Nov 12, 2026
Merged

lyesadmi merged 6 commits into
masterfrom
feat/napi

Conversation

@lyesadmi

@lyesadmi lyesadmi commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Why

Main goal: stop this addon from blocking Node.js upgrades. 4.0.1 is a NAN addon: its binary only works for one Node ABI, so on any other Node version npm install recompiles it on the installing machine, against that machine's glibc. Built on a recent workstation, it then fails to load on an older glibc such as the AWS Lambda one (GLIBC_2.38 not found), and every Node upgrade means rebuilding it.

5.0.0 is a Node-API 10 addon with prebuilds: one binary per platform that loads on every Node >= 22.14 (24, 26...), built in the AWS Lambda nodejs:22 image and needing at most glibc 2.17. No compatibility with Node < 22.14 is kept: the goal is to keep working on newer runtimes.

Secondary gains: linux-arm64, bit-identical results on x64 and arm64, index memory reported to V8, typed-array vectors, TypeScript declarations, no more leveldown installed with the package.

What

  • N-API binding (node-addon-api 8, NAPI_VERSION 10) replacing the NAN wrapper; engines.node >= 22.14.0.
  • Prebuilds, all committed: linux-x64 and linux-arm64 built in the Lambda nodejs:22 image (Dockerfile), darwin-arm64 and darwin-x64 on macOS runners (.github/workflows/prebuild.yml). scripts/check-prebuild-symbols.sh fails if a Linux binary needs more than glibc 2.34.
  • Same results on x64 and arm64: compiled with -ffp-contract=off (arm64 fused a*b+c into FMA, which changed distances and trees).
  • Memory: the index memory (heap nodes or mapped file) is reported to V8 with napi_adjust_external_memory, so dropped indexes are collected: 1000 loads of a 4 MB index keep ~20 alive instead of ~360.
  • Typed arrays: addItem and getNNsByVector read numeric typed arrays straight from their backing store (4.0.1 ignored them).
  • Fewer items than requested: returns only the real neighbours. 4.0.1 padded with garbage, including item 0 at distance 0.
  • Dependencies: level (and the native leveldown) and bindings move to devDependencies; tools/ is no longer published.
  • TypeScript declarations (index.d.ts), checked under --strict by tests/types.ts.
  • Tests: equivalence, leak, external memory, typed array, undefined behaviour and wrong-input tests against the 4.0.1 binding.

Validation

result
Equivalence with 4.0.1 same results 60/60, x64 (Node 22, 24) and arm64
arm64 vs x64 0 differences on 1,161,936 values, .annoy files byte-identical
Node versions, same prebuilds Node 22.17.1, 24.12.0, Lambda nodejs:24 image (x64, arm64), Node 26.12.0

Downstream validation (production replays, arm64 deployment) is in the internal pull requests that consume the canary 5.0.0-canary.2.

Not in this PR

  • Release: after merge, tag v5.0.0 and publish from a checkout of master (npm publish). The prebuild workflow only builds and tests: it does not publish and uses no secrets. All four prebuilds are committed (the darwin ones come from the workflow artifact; its linux ones are byte-identical to the committed ones). The canary 5.0.0-canary.2 lacks the darwin prebuilds.

Work in progress, saved before review. Rewrites the wrapper on N-API,
builds the Linux prebuilds in the Lambda nodejs:22 image for x64 and
arm64, and adds equivalence, leak, undefined-behaviour and wrong-input
tests against the 4.0.1 NAN binding.
…d arrays

- Report the index memory (heap nodes or mapped file) to V8 with
  napi_adjust_external_memory, so dropped indexes are collected: 1000
  loads of a 4 MB index keep ~20 alive instead of ~360 (4.0.1).
- Compile with -ffp-contract=off: arm64 fused a*b+c into FMA and gave
  different distances and trees than x86-64; results are now
  bit-identical across architectures.
- Accept numeric typed arrays as vectors in addItem and getNNsByVector,
  read straight from their backing store (4.0.1 ignored them).
- Tests: external-memory-test.js, typed-array-test.js; README updated.
The binding only loads node-gyp-build. level (and through it the native
leveldown) is only used by the tools/ scripts that build test data, and
bindings by nothing: every consumer installed and shipped a second
native module it never loaded. tools/ is no longer published.
The package only serves bot-api, which follows the AWS Lambda Node.js
runtime: target the latest stable Node-API instead of keeping Node 16
compatibility. engines: node >= 22.14.0.

- NAPI_VERSION 10: node-addon-api reports napi_cannot_run_js when an
  exception is thrown while the environment shuts down.
- The result keys (neighbors, distances) are created with
  node_api_create_property_key_utf8: -3% per query on a small index,
  no measurable change on a real one (3631, 1024 dimensions).

Prebuilds rebuilt in the Lambda nodejs:22 image. Same results as 4.0.1
(60/60) on x64 (Node 22, 24) and arm64, every suite passing.
index.d.ts declares the Annoy class (export =) with its metric, vector
(arrays and numeric typed arrays) and result types; getNNsByVector and
getNNsByItem are overloaded on includeDistances. tests/types.ts must
compile under --strict (without skipLibCheck, so the declarations are
checked too) and its misuses must not; it runs in make test.
Taken from the prebuild workflow artifact (run 43196701137, built on the
macOS runners from 5b8020e), so macOS installs no longer compile the
addon. The linux prebuilds of the same run are byte-identical to the
committed ones.
@lyesadmi
lyesadmi merged commit 1497341 into master Nov 12, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants