Replace Koffi with ffi-rs in the Node SDK - #2643
Conversation
Migrate the in-process runtime transport to ffi-rs, preserve callback and cleanup lifecycle behavior, and add coverage for callback pointer handling and partial startup rollback. Co-authored-by: Copilot App <[email protected]>
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Synchronous non-void FFI calls are memory-unsafe in the pinned dependency, and the samples lockfile is incomplete.
Get a fresh assessment by requesting another Copilot review.
Review tier: Balanced
Findings: 1
Open findings (2)
What changed in this PR
Replaces Koffi with ffi-rs for the Node SDK’s in-process native transport.
Changes:
- Reworks native calls, callbacks, pointer handling, and cleanup.
- Adds focused FFI lifecycle tests.
- Updates dependencies and lockfiles.
| File | Description |
|---|---|
nodejs/src/ffiRuntimeHost.ts |
Implements the ffi-rs transport. |
nodejs/src/client.ts |
Updates lazy-loading documentation. |
nodejs/test/ffiRuntimeHost.test.ts |
Tests callback and cleanup behavior. |
nodejs/package.json |
Replaces the FFI dependency. |
nodejs/package-lock.json |
Locks ffi-rs platform packages. |
nodejs/samples/package-lock.json |
Partially updates the sample lock graph. |
Files not reviewed (2)
- nodejs/package-lock.json: Generated file
- nodejs/samples/package-lock.json: Generated file
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Run every non-void ffi-rs call on its worker-thread path, preserve serialized writes, add regression coverage, and complete the samples lock graph. Co-authored-by: Copilot App <[email protected]>
SDK Consistency Review — PR #2643Scope of change: This PR only modifies the Node.js SDK's internal in-process FFI runtime host implementation, replacing the Findings:
Conclusion: This PR maintains cross-SDK consistency. It is a Node.js-only internal dependency/implementation change with no observable API or behavioral difference, so no other SDK requires updates.
|


Why
The Node in-process runtime transport uses a narrow C ABI, but Koffi contributes a relatively large native dependency.
ffi-rssupports all runtime targets used by the SDK while reducing the observed Windows x64 installed footprint from approximately 2.73 MB to 1.47 MB and loading its addon faster.What changed
ffi-rs1.3.7.ffi-rs.Validation
Tradeoffs and review notes
A trivial native call takes approximately 2.8 microseconds through
ffi-rs, compared with approximately 43 nanoseconds through a pre-bound Koffi function. The relative difference is large, but the absolute overhead is negligible compared with JSON-RPC and runtime processing.ffi-rs1.3.7 also requires two callback compatibility measures: pointer arguments are transported as losslessBigIntvalues because native-threadExternalconversion aborts, and callbacks use an ignoredU64return becauseVoidcallbacks corrupt callback-owned data. These paths have targeted tests but need careful cross-platform CI coverage.One upstream limitation remains: if native close fails permanently or
freePointerthrows,ffi-rsprovides no safe callback unref API, so its referenced thread-safe callback can keep Node alive. The relevant lines are annotated for review.Generated by Copilot