# @2060\.io/ref-napi 中文文档

规范地址：https://npmcn.com/package/%402060.io/ref-napi/

版本：3\.0\.6
npm 元数据更新时间：2023-09-06T12:43:19.054Z
中文译文更新时间：2026-11-06T03:24:41.324Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%402060.io/ref-napi
原文对照：https://npmcn.com/package/%402060.io/ref-napi/?lang=original
项目主页：https://github.com/node-ffi-napi/ref-napi#readme
源码仓库：https://github.com/node-ffi-napi/ref-napi

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：472 kB，43 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：\>= 18\.0
- 运行依赖：4 个
- 首次发布：2023-02-06T21:49:17.542Z
- 最新版本发布：2023-09-06T12:43:18.874Z
- 近 30 天下载量：5685（2026-10-05 至 2026-11-03，npm 官方下载统计）
- 已发布版本数：3（近 12 个月 0 个）

## 中文简介

将缓冲实例转换为“ 指针 ”

## 安装

```sh
npm install @2060.io/ref-napi
```

也可以使用：`pnpm add @2060.io/ref-napi`、`yarn add @2060.io/ref-napi`、`bun add @2060.io/ref-napi`

## README

# 参考

### 将缓冲实例转换为“ 指针 ”

[![Greenkeeper badge](https://badges.greenkeeper.io/node-ffi-napi/ref-napi.svg)](https://greenkeeper.io/)

[![NPM Version](https://img.shields.io/npm/v/ref-napi.svg?style=flat)](https://npmjs.org/package/ref-napi) [![NPM Downloads](https://img.shields.io/npm/dm/ref-napi.svg?style=flat)](https://npmjs.org/package/ref-napi) [![Build Status](https://travis-ci.org/node-ffi-napi/ref-napi.svg?style=flat&branch=latest)](https://travis-ci.org/node-ffi-napi/ref-napi?branch=latest) [![Coverage Status](https://coveralls.io/repos/node-ffi-napi/ref-napi/badge.svg?branch=latest)](https://coveralls.io/r/node-ffi-napi/ref-napi?branch=latest) [![Dependency Status](https://david-dm.org/node-ffi-napi/ref-napi.svg?style=flat)](https://david-dm.org/node-ffi-napi/ref-napi)

这个模块的灵感来自旧的 `Pointer` 课程从节点-ffi,但与 使用节点快的意向 `Buffer` 实例而不是慢 C++ `Pointer` 课。 这两个概念以前非常相似,但现在这个单元 把指针和缓冲器的功能带过来 所以 现在缓冲器更强大了.

### 特征 :

-   获取任意的内存地址 `Buffer` 实例
-   读取/写入 JavaScript 对象的引用 `Buffer` 实例
-   读取/写作 `Buffer` 实例的内存地址到其它 `Buffer` 实例
-   读取/写作 `int64_t` 财务报告和审定财务报表 `uint64_t` 数据值(数字或字符串)
-   “类型”公约,这样您就可以指定缓冲器作为 `int *`, (中文(简体) ), 和随意参考/参考.
-   提供缓冲实例 `NULL` 指针

## 安装

安装方式 `npm`数字 :

```bash
$ npm install ref-napi
```

## 实例

#### 参考和疏导

```js
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
```

见 [完整 API 文档](http://tootallnate.github.com/ref) 需要更多的实例.

## "类型"接口

您可以在附件上轻松定义自己的“类型”对象 `Buffer` 实例. 它只需要是一个包含以下内容的普通 JavaScript 对象 属性 :

| **名称** | **数据类型** | **说明** |
| :-- | :-- | :-- |
| `size` | 数目 | 持有此类型所需的字节大小 . |
| `indirection` | 数目 | 缓冲的当前间接水平. 通常这样 _1个_,然后从 `ref()` 打电话 数值小于或等于 _0 个_ 无效. |
| `get` | 职能(缓冲、冲抵) | 当间接级别是时, 引用该类型的函数 _1个_。 。 。 . |
| `set` | 函数(缓冲、冲抵、值) | 设置缓冲实例值时引用的函数 . |
| `name` | 字符串 | _(可选)_ 此类型调试时使用的名称 . |
| `alignment` | 数目 | _(可选)_ 此类型在被放入结构时的对齐 。 类型默认值 `size`。 。 。 . |

请检查date=中的日期值 (帮助) Wiki页面 [已经知道 类型"](https://github.com/TooTallNate/ref/wiki/Known-%22types%22),用于列表 以及已知的外部类型.

例如,您可以定义一种“ bigint” 类型,该类型可以将缩写为 [`bigint`](https://github.com/substack/node-bigint) 实例:

```js
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)
```

## 构建文件

安装非依赖性 :

```bash
$ npm install
```

生成文件 :

```bash
$ npm run docs
```

## 不兼容的软件包

这个 [`ref-struct-napi`](https://www.npmjs.com/package/ref-struct-napi) 财务报告和审定财务报表 [`ref-array-napi`](https://www.npmjs.com/package/ref-array-napi) 软件包 拥有与本模块相容的名字.

他们不是,如果你使用,你的应用程序会遇到崩溃 他们与 `ref-napi`。 。 。 . 使用 [`ref-struct-di`](https://www.npmjs.com/package/ref-struct-di) 或者说 [`ref-array-di`](https://www.npmjs.com/package/ref-array-di) 换句话说.

## 许可证

(麻省理工学院的执照)

版权 (c) 2012 Nathan Rajlich <nathan@tootallnate.net> 版权 (c) 2017 Anna Henningsen <anna@addaleax.net>(N-API端口)

特此免费准许任何人获得 该软件和相关文件文件的副本(第2条) “软件”,在软件中不受限制地处理,包括 不限制使用、复制、修改、合并、出版、, 软件的分发、分版和/或销售,以及 允许获得软件的人这样做,但须符合下列条件: 下述条件:

上述版权通知和本许可通知应为: 列入软件的所有副本或大部分.

软件是提供的"是", 没有任何的警告, ,包括但不受限制的 紧迫性,特别是目的和非目的的证明. 绝对不能让警察或警察 (B) 合同行动、合同或其他责任, 产生、退出或与 软件或软件的使用或其他交易.

## 常见问题

### @2060\.io/ref-napi 是什么？

将缓冲实例转换为“ 指针 ”

### 如何安装 @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 和提交记录判断。

