@2060.io/ffi-napi

中文文档
4.0.9发布于 2024/04/06MITTypeScriptCommonJS近 30 天下载 6215

A foreign function interface (FFI) for Node.js, N-API style

原始使用文档
文档目录9 节
  1. Node.js Foreign Function Interface for N-API
  2. Example
  3. Requirements
  4. Installation
  5. Source Install / Manual Compilation
  6. Types
  7. V8 and 64-bit Types
  8. Call Overhead
  9. License

node-ffi-napi

Node.js Foreign Function Interface for N-API

Greenkeeper badge

NPM Version NPM Downloads Build Status Coverage Status Dependency Status

node-ffi-napi is a Node.js addon for loading and calling dynamic libraries using pure JavaScript. It can be used to create bindings to native libraries without writing any C++ code.

It also simplifies the augmentation of node.js with C code as it takes care of handling the translation of types across JavaScript and C, which can add reams of boilerplate code to your otherwise simple C. See the example/factorial for an example of this use case.

NOTE: This fork uses a patched version of ref-napi due to serious performance issues found when using it intensively. This patch has been proven to be unstable in older node versions. As a result, node >=18 is required.

WARNING: node-ffi-napi assumes you know what you're doing. You can pretty easily create situations where you will segfault the interpreter and unless you've got C debugger skills, you probably won't know what's going on.

WARNING: The original API of node-ffi is left mostly untouched in the N-API wrapper. However, the API did not have very well-defined properties in the context of garbage collection and multi-threaded execution. It is recommended to avoid any multi-threading usage of this library if possible.

Example

var ffi = require('ffi-napi');

var libm = ffi.Library('libm', {
  'ceil': [ 'double', [ 'double' ] ]
});
libm.ceil(1.5); // 2

// You can also access just functions in the current process by passing a null
var current = ffi.Library(null, {
  'atoi': [ 'int', [ 'string' ] ]
});
current.atoi('1234'); // 1234

For a more detailed introduction, see the node-ffi tutorial page.

Requirements

  • Linux, OS X, Windows, or Solaris.
  • libffi comes bundled with node-ffi-napi; it does not need to be installed on your system.
  • The current version is tested to run on Node 6 and above.

Installation

Make sure you've installed all the necessary build tools for your platform, then invoke:

$ npm install ffi-napi

Source Install / Manual Compilation

To compile from source it's easiest to use node-gyp:

$ npm install -g node-gyp

Now you can compile node-ffi-napi:

$ git clone git://github.com/node-ffi-napi/node-ffi-napi.git
$ cd node-ffi
$ node-gyp rebuild

Types

The types that you specify in function declarations correspond to ref's types system. So see its docs for a reference if you are unfamiliar.

V8 and 64-bit Types

Internally, V8 stores integers that will fit into a 32-bit space in a 32-bit integer, and those that fall outside of this get put into double-precision floating point numbers. This is problematic because FP numbers are imprecise. To get around this, the methods in node-ffi that deal with 64-bit integers return strings and can accept strings as parameters.

Call Overhead

There is non-trivial overhead associated with FFI calls. Comparing a hard-coded binding version of strtoul() to an FFI version of strtoul() shows that the native hard-coded binding is orders of magnitude faster. So don't just use the C version of a function just because it's faster. There's a significant cost in FFI calls, so make them worth it.

License

MIT License. See the LICENSE file.

版本与发布记录

@2060.io/ffi-napi 共发布 6 个版本,首次发布于 2023/08/13;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.0.9latest2024/04/062 年前
4.0.82023/09/063 年前
4.0.72023/09/063 年前
4.0.62023/09/063 年前
4.0.52023/08/133 年前
4.0.42023/08/133 年前

关于 @2060.io/ffi-napi 的常见问题

@2060.io/ffi-napi 是什么?

A foreign function interface (FFI) for Node.js, N-API style

如何安装 @2060.io/ffi-napi?

在项目目录运行 npm install @2060.io/ffi-napi,或使用 pnpm add / yarn add / bun add。

@2060.io/ffi-napi 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@2060.io/ffi-napi 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@2060.io/ffi-napi 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@2060.io/ffi-napi 可以商用吗?

@2060.io/ffi-napi 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@2060.io/ffi-napi 的下载量有多少?

根据 npm 官方下载统计,@2060.io/ffi-napi 在 2026/10/05 – 2026/11/03 这 30 天内被下载 6,215 次,日均约 207 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@2060.io/ffi-napi 还在维护吗?

最新版本 4.0.9 发布于 2024/04/06(2 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/08/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。