@endo/hex

中文文档
1.1.1发布于 2026/06/12Apache-2.0ES Module近 30 天下载 3.8万

Transcodes hexadecimal

原始使用文档
文档目录6 节
  1. Install
  2. Usage
  3. API
  4. encodeHex(bytes) -> string
  5. decodeHex(string, name?) -> Uint8Array
  6. Hardened JavaScript

@endo/hex

@endo/hex encodes and decodes between Uint8Array and hexadecimal strings. It is a ponyfill for the TC39 Uint8Array.prototype.toHex and Uint8Array.fromHex intrinsics (proposal-arraybuffer-base64, Stage 4), but for errors provides a consistent diagnostic optionally including a name to identify the file the source material came from, which can be instrumental for debugging.

On engines that ship the native intrinsics, encodeHex and decodeHex dispatch to them at module load time. On older engines, and in SES-locked-down compartments where a realm has removed the intrinsics, the package falls through to a portable pure-JavaScript implementation.

Install

npm install @endo/hex

Usage

import { encodeHex } from '@endo/hex/encode.js';
import { decodeHex } from '@endo/hex/decode.js';

// Or, if you genuinely need to entrain both implementations:
// import { encodeHex, decodeHex } from '@endo/hex';

encodeHex(new Uint8Array([0xb0, 0xb5, 0xc4, 0xfe])); // 'b0b5c4fe'
decodeHex('b0b5c4fe'); // Uint8Array(4) [0xb0, 0xb5, 0xc4, 0xfe]

API

encodeHex(bytes) -> string

Encodes a Uint8Array as a lowercase hex string. Callers that need uppercase can call .toUpperCase() on the result.

decodeHex(string, name?) -> Uint8Array

Decodes a hex string to a Uint8Array. Accepts both upper- and lowercase input. Throws on odd-length strings and on characters outside [0-9a-fA-F]. The optional name parameter is included in error messages for diagnostic context.

Hardened JavaScript

The native intrinsic reference is captured once at module load, before any caller can reach the exported functions and before SES lockdown freezes Uint8Array. Post-lockdown mutation of Uint8Array cannot redirect the dispatched bindings.

In a post-lockdown environment, this module hardens its interfaces to reduce supply chain attack exposure.

版本与发布记录

@endo/hex 共发布 2 个版本,首次发布于 2026/05/28;过去 12 个月发布了 2 个版本。

版本发布日期距今
1.1.1latest2026/06/125 个月前
1.1.02026/05/285 个月前

关于 @endo/hex 的常见问题

@endo/hex 是什么?

Transcodes hexadecimal

如何安装 @endo/hex?

在项目目录运行 npm install @endo/hex,或使用 pnpm add / yarn add / bun add。

@endo/hex 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/endo__hex 是否存在。

@endo/hex 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@endo/hex 可以商用吗?

@endo/hex 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@endo/hex 的下载量有多少?

根据 npm 官方下载统计,@endo/hex 在 2026/10/14 – 2026/11/12 这 30 天内被下载 37,755 次,日均约 1,259 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@endo/hex 还在维护吗?

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