# etag 中文文档

规范地址：https://npmcn.com/package/etag/

版本：1\.8\.1
npm 元数据更新时间：2025-05-14T14:56:17.076Z
中文译文更新时间：2026-11-05T05:01:24.639Z
软件包许可：MIT

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

原始 npm 页面：https://www.npmjs.com/package/etag
原文对照：https://npmcn.com/package/etag/?lang=original
项目主页：https://github.com/jshttp/etag#readme
源码仓库：https://github.com/jshttp/etag

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

- TypeScript 类型：未声明
- Node.js 要求：\>= 0\.6
- 运行依赖：0 个
- 首次发布：2014-05-18T11:14:58.281Z
- 最新版本发布：2017-09-13T02:43:44.422Z
- 近 30 天下载量：498605217（2026-10-05 至 2026-11-03，npm 官方下载统计）
- 已发布版本数：14（近 12 个月 0 个）

## 中文简介

创建简单的HTTP ETags

## 安装

```sh
npm install etag
```

也可以使用：`pnpm add etag`、`yarn add etag`、`bun add etag`

## README

# etag

[![NPM Version](https://img.shields.io/npm/v/etag.svg)](https://npmjs.org/package/etag) [![NPM Downloads](https://img.shields.io/npm/dm/etag.svg)](https://npmjs.org/package/etag) [![Node.js Version](https://img.shields.io/node/v/etag.svg)](https://nodejs.org/en/download/) [![Build Status](https://img.shields.io/travis/jshttp/etag/master.svg)](https://travis-ci.org/jshttp/etag) [![Test Coverage](https://img.shields.io/coveralls/jshttp/etag/master.svg)](https://coveralls.io/r/jshttp/etag?branch=master)

创建简单的HTTP ETags

该模块生成HTTP ETags（如RFC 7232中定义）用于 HTTP响应。

## 安装

这是一个 [node . js](https://nodejs.org/en/) 模块可通过 [npm注册表](https://www.npmjs.com/)． 安装是使用 [`npm install` 命令](https://docs.npmjs.com/getting-started/installing-npm-packages-locally)：

```sh
$ npm install etag
```

## API

```js
var etag = require('etag')
```

### etag(实体,\[选项\])

为给定实体生成一个强ETag。这应该是完整的 实体的主体。字符串, `Buffer`年代, `fs.Stats` 被接受。通过 默认情况下，生成一个强ETag，除了 `fs.Stats`，这将 生成一个弱ETag(它可以被 `options.weak`）.

```js
res.setHeader('ETag', etag(body))
```

#### 选项

`etag` 在options对象中接受这些属性。

##### 弱

指定生成的ETag是否包含弱验证器标记(即 是领头的 `W/`）. 实际的实体标记是相同的。默认值 是 `false`，除非 `entity` 是 `fs.Stats`，在这种情况下，它是 `true`。

## 测试

```sh
$ npm test
```

## 基准

```bash
$ npm run-script bench

> etag@1.8.1 bench nodejs-etag
> node benchmark/index.js

  http_parser@2.7.0
  node@6.11.1
  v8@5.1.281.103
  uv@1.11.0
  zlib@1.2.11
  ares@1.10.1-DEV
  icu@58.2
  modules@48
  openssl@1.0.2k

> node benchmark/body0-100b.js

  100B body

  4 tests completed.

  buffer - strong x 258,647 ops/sec ±1.07% (180 runs sampled)
  buffer - weak   x 263,812 ops/sec ±0.61% (184 runs sampled)
  string - strong x 259,955 ops/sec ±1.19% (185 runs sampled)
  string - weak   x 264,356 ops/sec ±1.09% (184 runs sampled)

> node benchmark/body1-1kb.js

  1KB body

  4 tests completed.

  buffer - strong x 189,018 ops/sec ±1.12% (182 runs sampled)
  buffer - weak   x 190,586 ops/sec ±0.81% (186 runs sampled)
  string - strong x 144,272 ops/sec ±0.96% (188 runs sampled)
  string - weak   x 145,380 ops/sec ±1.43% (187 runs sampled)

> node benchmark/body2-5kb.js

  5KB body

  4 tests completed.

  buffer - strong x 92,435 ops/sec ±0.42% (188 runs sampled)
  buffer - weak   x 92,373 ops/sec ±0.58% (189 runs sampled)
  string - strong x 48,850 ops/sec ±0.56% (186 runs sampled)
  string - weak   x 49,380 ops/sec ±0.56% (190 runs sampled)

> node benchmark/body3-10kb.js

  10KB body

  4 tests completed.

  buffer - strong x 55,989 ops/sec ±0.93% (188 runs sampled)
  buffer - weak   x 56,148 ops/sec ±0.55% (190 runs sampled)
  string - strong x 27,345 ops/sec ±0.43% (188 runs sampled)
  string - weak   x 27,496 ops/sec ±0.45% (190 runs sampled)

> node benchmark/body4-100kb.js

  100KB body

  4 tests completed.

  buffer - strong x 7,083 ops/sec ±0.22% (190 runs sampled)
  buffer - weak   x 7,115 ops/sec ±0.26% (191 runs sampled)
  string - strong x 3,068 ops/sec ±0.34% (190 runs sampled)
  string - weak   x 3,096 ops/sec ±0.35% (190 runs sampled)

> node benchmark/stats.js

  stat

  4 tests completed.

  real - strong x 871,642 ops/sec ±0.34% (189 runs sampled)
  real - weak   x 867,613 ops/sec ±0.39% (190 runs sampled)
  fake - strong x 401,051 ops/sec ±0.40% (189 runs sampled)
  fake - weak   x 400,100 ops/sec ±0.47% (188 runs sampled)
```

## 许可证

[麻省理工学院](https://npmcn.com/package/etag/LICENSE)

## 常见问题

### etag 是什么？

创建简单的HTTP ETags

### 如何安装 etag？

在项目目录运行 npm install etag，或使用 pnpm add / yarn add / bun add。

### etag 支持 TypeScript 吗？

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

### etag 需要什么版本的 Node\.js？

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

### etag 可以商用吗？

etag 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### etag 的下载量有多少？

根据 npm 官方下载统计，etag 在 2026/10/05 – 2026/11/03 这 30 天内被下载 498,605,217 次，日均约 16,620,174 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### etag 还在维护吗？

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

