@2060.io/ref-napi

中文文档
3.0.6发布于 2023/09/06MITTypeScriptCommonJS近 30 天下载 5685

Turn Buffer instances into "pointers"

原始使用文档
文档目录8 节
  1. Turn Buffer instances into "pointers"
  2. Features:
  3. Installation
  4. Examples
  5. The "type" interface
  6. Build the docs
  7. Incompatible packages
  8. License

ref-napi

Turn Buffer instances into "pointers"

Greenkeeper badge

NPM Version NPM Downloads Build Status Coverage Status Dependency Status

This module is inspired by the old Pointer class from node-ffi, but with the intent of using Node's fast Buffer instances instead of a slow C++ Pointer class. These two concepts were previously very similar, but now this module brings over the functionality that Pointers had and Buffers are missing, so now Buffers are a lot more powerful.

Features:

  • Get the memory address of any Buffer instance
  • Read/write references to JavaScript Objects into Buffer instances
  • Read/write Buffer instances' memory addresses to other Buffer instances
  • Read/write int64_t and uint64_t data values (Numbers or Strings)
  • A "type" convention, so that you can specify a buffer as an int *, and reference/dereference at will.
  • Offers a buffer instance representing the NULL pointer

Installation

Install with npm:

$ npm install ref-napi

Examples

referencing and derefencing

var ref = require('ref-napi')

// so we can all agree that a buffer with the int value written
// to it could be represented as an "int *"
var buf = new Buffer(4)
buf.writeInt32LE(12345, 0)

// first, what is the memory address of the buffer?
console.log(buf.hexAddress())  // ← '7FA89D006FD8'

// using `ref`, you can set the "type", and gain magic abilities!
buf.type = ref.types.int

// now we can dereference to get the "meaningful" value
console.log(buf.deref())  // ← 12345


// you can also get references to the original buffer if you need it.
// this buffer could be thought of as an "int **"
var one = buf.ref()

// and you can dereference all the way back down to an int
console.log(one.deref().deref())  // ← 12345

See the full API Docs for more examples.

The "type" interface

You can easily define your own "type" objects at attach to Buffer instances. It just needs to be a regular JavaScript Object that contains the following properties:

NameData TypeDescription
sizeNumberThe size in bytes required to hold this type.
indirectionNumberThe current level of indirection of the buffer. Usually this would be 1, and gets incremented on Buffers from ref() calls. A value of less than or equal to 0 is invalid.
getFunction (buffer, offset)The function to invoke when dereferencing this type when the indirection level is 1.
setFunction (buffer, offset, value)The function to invoke when setting a value to a buffer instance.
nameString(optional) The name to use during debugging for this type.
alignmentNumber(optional) The alignment of this type when placed in a struct. Defaults to the type's size.

Be sure to check out the Wiki page of "Known Types", for the list of built-in ref types, as well as known external type implementations.

For example, you could define a "bigint" type that dereferences into a bigint instance:

var ref = require('ref-napi')
var bigint = require('bigint')

// define the "type" instance according to the spec
var BigintType = {
    size: ref.sizeof.int64
  , indirection: 1
  , get: function (buffer, offset) {
      // return a bigint instance from the buffer
      return bigint.fromBuffer(buffer)
    }
  , set: function (buffer, offset, value) {
      // 'value' would be a bigint instance
      var val = value.toString()
      return ref.writeInt64(buffer, offset || 0, val)
    }
}

// now we can create instances of the type from existing buffers.
// "buf" is some Buffer instance returned from some external data
// source, which should contain "bigint" binary data.
buf.type = BigintType

// and now you can create "bigint" instances using this generic "types" API
var val = buf.deref()
            .add('1234')
            .sqrt()
            .shiftLeft(5)

Build the docs

Install the dev dependencies:

$ npm install

Generate the docs:

$ npm run docs

Incompatible packages

The ref-struct-napi and ref-array-napi packages have names that sound like they are compatible with this module.

They are not, and your application will experience crashes if you use them together with ref-napi. Use ref-struct-di or ref-array-di instead.

License

(The MIT License)

Copyright (c) 2012 Nathan Rajlich <[email protected]> Copyright (c) 2017 Anna Henningsen <[email protected]> (N-API port)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

版本与发布记录

@2060.io/ref-napi 共发布 3 个版本,首次发布于 2023/02/07;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.6latest2023/09/063 年前
3.0.52023/09/063 年前
3.0.42023/02/073 年前

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

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

Turn Buffer instances into "pointers"

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

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

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

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

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

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

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

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

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

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

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

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

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

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