# @git-stunts/trailer-codec 中文文档

规范地址：https://npmcn.com/package/%40git-stunts/trailer-codec/

版本：4\.0\.0
npm 元数据更新时间：2026-07-30T06:50:00.233Z
中文译文更新时间：2026-11-16T10:38:10.299Z
软件包许可：Apache-2\.0

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

原始 npm 页面：https://www.npmjs.com/package/%40git-stunts/trailer-codec
原文对照：https://npmcn.com/package/%40git-stunts/trailer-codec/?lang=original
项目主页：https://github.com/git-stunts/trailer-codec#readme
源码仓库：https://github.com/git-stunts/trailer-codec

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

- 包体积（解压后）：104 kB，39 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- Node.js 要求：\>=20\.0\.0
- 运行依赖：1 个
- 首次发布：2026-01-12T09:05:31.844Z
- 最新版本发布：2026-07-30T06:49:59.911Z
- 近 30 天下载量：7057（2026-10-17 至 2026-11-15，npm 官方下载统计）
- 已发布版本数：4（近 12 个月 4 个）

## 中文简介

Git 中结构化元数据强大的编码器/解码器

## 安装

```sh
npm install @git-stunts/trailer-codec
```

也可以使用：`pnpm add @git-stunts/trailer-codec`、`yarn add @git-stunts/trailer-codec`、`bun add @git-stunts/trailer-codec`

## README

# @git-stunts/拖车编码c

[![npm version](https://img.shields.io/npm/v/@git-stunts/trailer-codec.svg)](https://www.npmjs.com/package/@git-stunts/trailer-codec) [![License](https://img.shields.io/npm/l/@git-stunts/trailer-codec.svg)](https://npmcn.com/package/%40git-stunts/trailer-codec/LICENSE) [![CI](https://github.com/git-stunts/trailer-codec/actions/workflows/ci.yml/badge.svg)](https://github.com/git-stunts/trailer-codec/actions/workflows/ci.yml)

> **强力, Git Commissions 的 Async- First 元数据编码 .**

`@git-stunts/trailer-codec` 是一个专业级编码器/解码器,用于 Git 传输信件中的结构化元数据(拖车)。 它遵循了标准的Git拖车公约(RFC 822 / Email headers),同时提供工业安全特性和类型安全域模型.

![](https://github.com/user-attachments/assets/0a3800d9-12c4-4639-b6c3-b1782bf28c96)

## 关键特性

-   **Async- 第一个 API**: 为v1.0实现现代化,支持非屏蔽编码/解码.
-   **DoS 保护**:内建5MB消息尺寸限制和ReDoS-hardened regex.
-   **结构化域**: 正规实体和价值对象(GitCommitMessage, GitTrailer).
-   **Zod 验证**: 有诊断错误对象的Schema驱动验证.
-   **纯域逻辑**:零 I/O,零子进程. 任何运行时间(节点、Bun、Deno、Web)的安全.
-   **大小写规范化**: 拖车密钥自动正常化,用于一致查询.

## QQ 快速启动

### 基本编码

```javascript
import { createDefaultTrailerCodec } from '@git-stunts/trailer-codec';

const codec = createDefaultTrailerCodec();

const message = await codec.encode({
  title: 'feat: add user authentication',
  body: 'Implemented OAuth2 flow with JWT tokens.',
  trailers: {
    'Signed-off-by': 'James Ross',
    'Reviewed-by': 'Alice Smith'
  }
});
```

### 基本解码

```javascript
const payload = await codec.decode(message);

console.log(payload.title);    // "feat: add user authentication"
console.log(payload.trailers); // { "signed-off-by": "James Ross", ... }
```

## 页:1

-   [**标准指南**](https://npmcn.com/package/%40git-stunts/trailer-codec/GUIDE.md) - 共同模式和先进配置.
-   [**API 参考**](https://npmcn.com/package/%40git-stunts/trailer-codec/API_REFERENCE.md) - 耗尽出口目录.
-   [**担保模式**](https://npmcn.com/package/%40git-stunts/trailer-codec/SECURITY.md) -DoS和ReDoS保护的细节.
-   [**融合食谱**](https://npmcn.com/package/%40git-stunts/trailer-codec/docs/INTEGRATION.md) - 怎样使用 `git log` 和CI管道.

## 审定规则

| 规则 | 约束 | 抛出错误 |
| --- | --- | --- |
| **信件大小** | • 5MB | `TrailerTooLargeError` |
| **拖车键** | 字母、连字符、下划线 | `TrailerInvalidError` |
| **拖车价值** | 无换行, 无空 | `TrailerValueInvalidError` |
| **结构** | 空行必须把车和拖车分开 | `TrailerNoSeparatorError` |

## 许可证

阿帕奇 - 2.0

## 常见问题

### @git-stunts/trailer-codec 是什么？

Git 中结构化元数据强大的编码器/解码器

### 如何安装 @git-stunts/trailer-codec？

在项目目录运行 npm install @git-stunts/trailer-codec，或使用 pnpm add / yarn add / bun add。

### @git-stunts/trailer-codec 支持 TypeScript 吗？

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

### @git-stunts/trailer-codec 支持 ESM 还是 CommonJS？

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

### @git-stunts/trailer-codec 需要什么版本的 Node\.js？

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

### @git-stunts/trailer-codec 可以商用吗？

@git-stunts/trailer-codec 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @git-stunts/trailer-codec 的下载量有多少？

根据 npm 官方下载统计，@git-stunts/trailer-codec 在 2026/10/17 – 2026/11/15 这 30 天内被下载 7,057 次，日均约 235 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @git-stunts/trailer-codec 还在维护吗？

最新版本 4\.0\.0 发布于 2026/07/30（3 个月前）；至今共发布 4 个版本，其中过去 12 个月发布了 4 个；首次发布于 2026/01/12。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

