# @c4312/eventsource-umd 中文文档

规范地址：https://npmcn.com/package/%40c4312/eventsource-umd/

版本：3\.0\.5
npm 元数据更新时间：2025-03-15T03:37:22.519Z
中文译文更新时间：2026-11-08T22:41:42.541Z
软件包许可：MIT

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

原始 npm 页面：https://www.npmjs.com/package/%40c4312/eventsource-umd
原文对照：https://npmcn.com/package/%40c4312/eventsource-umd/?lang=original
项目主页：https://github.com/EventSource/eventsource#readme

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

- 包体积（解压后）：183 kB，14 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=18\.0\.0
- 运行依赖：1 个
- 首次发布：2025-03-15T03:37:21.945Z
- 最新版本发布：2025-03-15T03:37:22.228Z
- 近 30 天下载量：68480（2026-10-09 至 2026-11-07，npm 官方下载统计）
- 已发布版本数：1（近 12 个月 0 个）

## 中文简介

节点和浏览器的WhatWG/W3C兼容事件源客户端

## 安装

```sh
npm install @c4312/eventsource-umd
```

也可以使用：`pnpm add @c4312/eventsource-umd`、`yarn add @c4312/eventsource-umd`、`bun add @c4312/eventsource-umd`

## README

# 事件源

[![npm version](https://img.shields.io/npm/v/eventsource.svg?style=flat-square)](https://www.npmjs.com/package/eventsource)[![npm bundle size](https://img.shields.io/bundlephobia/minzip/eventsource?style=flat-square)](https://bundlephobia.com/result?p=eventsource)[![npm weekly downloads](https://img.shields.io/npm/dw/eventsource.svg?style=flat-square)](https://www.npmjs.com/package/eventsource)

兼容什么WG/W3C [服务器发送事件/事件源](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) 客户端。 该模块试图执行超出规格的绝对最低的特性/变化.

如果你在寻找一个现代的替代品, 限制较少的API, 检查一下 [`eventsource-client` 软件包](https://www.npmjs.com/package/eventsource-client)。 。 。 .

## 安装

```bash
npm install --save eventsource
```

## 支持的引擎

-   节点.js 18
-   克罗姆语 >= 63
-   萨法里岛
-   Firefox 第65页
-   边 上 79
-   德诺 · 1.30
-   (单位:千美元)

基本上,任何支持以下内容的环境:

-   [获取](https://developer.mozilla.org/en-US/docs/Web/API/fetch)
-   [可读结构](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream)
-   [文本解析器结构](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoderStream)
-   [網址](https://developer.mozilla.org/en-US/docs/Web/API/URL)
-   [活动](https://developer.mozilla.org/en-US/docs/Web/API/Event), (中文(简体) ), [信件事件](https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent), (中文(简体) ), [事件目标](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget)

如果您需要支持旧的运行时间,请尝试 `2.x` 分支/版本范围(注: 2.x分支主要针对Node.js,而非浏览器).

## 使用量

```ts
import {EventSource} from 'eventsource'

const es = new EventSource('https://my-server.com/sse')

/*
 * This will listen for events with the field `event: notice`.
 */
es.addEventListener('notice', (event) => {
  console.log(event.data)
})

/*
 * This will listen for events with the field `event: update`.
 */
es.addEventListener('update', (event) => {
  console.log(event.data)
})

/*
 * The event "message" is a special case, as it will capture events _without_ an
 * event field, as well as events that have the specific type `event: message`.
 * It will not trigger on any other event type.
 */
es.addEventListener('message', (event) => {
  console.log(event.data)
})

/**
 * To explicitly close the connection, call the `close` method.
 * This will prevent any reconnection from happening.
 */
setTimeout(() => {
  es.close()
}, 10_000)
```

### 类型脚本

确定您已经配置了您的 TSConfig , 这样它就能匹配您的目标环境 。 如果你是目标浏览器,这将是 `dom`数字 :

```jsonc
{
  "compilerOptions": {
    "lib": ["dom"],
  },
}
```

如果你使用Node.js,确保你有 `@types/node` 安装(并且是第18版或以上)。 云旗工人拥有 `@cloudflare/workers-types` 包括:.

以下出错是由于瞄准一个不具备必要类型的环境:

```
error TS2304: Cannot find name 'Event'.
error TS2304: Cannot find name 'EventTarget'.
error TS2304: Cannot find name 'MessageEvent'.
```

## 从 v1 / v2 移动

见 [移民.md](https://npmcn.com/package/%40c4312/eventsource-umd/MIGRATION.md#v2-to-v3) 详细移民指南.

## 扩展到 WhatWG/W3C API

### 错误时的信件和代码属性

这个 `error` 事件有一个 `message` 财务报告和审定财务报表 `code` 属性,用于获取更多关于错误的信息。 在规格中,事件

```ts
es.addEventListener('error', (err) => {
  if (err.code === 401 || err.code === 403) {
    console.log('not authorized')
  }
})
```

### 指定 `fetch` 执行

这个 `EventSource` 构造器接受可选 `fetch` 属性,用于指定 `fetch` 用于执行.

这在全球化环境中可能有用 `fetch` 函数不可用,但也可以用来改变请求/响应行为.

#### 设置 HTTP 请求头

```ts
const es = new EventSource('https://my-server.com/sse', {
  fetch: (input, init) =>
    fetch(input, {
      ...init,
      headers: {
        ...init.headers,
        Authorization: 'Bearer myToken',
      },
    }),
})
```

#### HTTP/ HTTPS 代理服务器

使用类似 [`node-fetch-native`](https://github.com/unjs/node-fetch-native) 通过环境变量或明确的配置,添加代理支持.

```ts
// npm install node-fetch-native --save
import {fetch} from 'node-fetch-native/proxy'

const es = new EventSource('https://my-server.com/sse', {
  fetch: (input, init) => fetch(input, init),
})
```

#### 允许未经授权的 HTTPS 请求

使用类似 [`undici`](https://github.com/nodejs/undici) 通过使用一个 [`Agent`](https://undici.nodejs.org/#/docs/api/Agent.md)。 。 。 .

```ts
// npm install undici --save
import {fetch, Agent} from 'undici'

await fetch('https://my-server.com/sse', {
  dispatcher: new Agent({
    connect: {
      rejectUnauthorized: false,
    },
  }),
})
```

## 许可证

麻省理工学院有执照 见 [自由主义](https://npmcn.com/package/%40c4312/eventsource-umd/LICENSE)。 。 。 .

## 常见问题

### @c4312/eventsource-umd 是什么？

节点和浏览器的WhatWG/W3C兼容事件源客户端

### 如何安装 @c4312/eventsource-umd？

在项目目录运行 npm install @c4312/eventsource-umd，或使用 pnpm add / yarn add / bun add。

### @c4312/eventsource-umd 支持 TypeScript 吗？

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

### @c4312/eventsource-umd 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @c4312/eventsource-umd 需要什么版本的 Node\.js？

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

### @c4312/eventsource-umd 可以商用吗？

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

### @c4312/eventsource-umd 的下载量有多少？

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

### @c4312/eventsource-umd 还在维护吗？

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

