{"_id":"@buletu/fetch-event-source","_rev":"5-626d7ac9c19ddc5ab8eb5220da790bb3","name":"@buletu/fetch-event-source","dist-tags":{"latest":"1.0.4"},"versions":{"1.0.0":{"name":"@buletu/fetch-event-source","version":"1.0.0","keywords":["fetch","javascript","EventSource","FetchEventSource"],"author":{"name":"buletu"},"license":"ISC","_id":"@buletu/fetch-event-source@1.0.0","maintainers":[{"name":"buletu","email":"949362805@qq.com"}],"homepage":"https://github.com/buletunice/fetch-event-source#readme","bugs":{"url":"https://github.com/buletunice/fetch-event-source/issues"},"dist":{"shasum":"881c465e11505a14f6dbeb850a5dba6e2d0ee89c","tarball":"https://registry.npmjs.org/@buletu/fetch-event-source/-/fetch-event-source-1.0.0.tgz","fileCount":5,"integrity":"sha512-mHP1wBrOwY/yQqOeekudDXWi6wCYjRCUWV/FHV6xuSm7hBoYdK5tsUsxDjGbGK0jz0NtxEK2Zkrm3/9MOV1+Fw==","signatures":[{"sig":"MEQCIB9iVDAM0CmDTINagaKui0vD/86a7gOWju/IhYc2EhluAiBjiQqj7tMQOyKej8Bf4REhC2qVpxwc0BMPF0mbKehyfw==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":10756},"main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1","build":"npx webpack"},"_npmUser":{"name":"buletu","actor":{"name":"buletu","type":"user","email":"949362805@qq.com"},"email":"949362805@qq.com"},"repository":{"url":"git://github.com/buletunice/fetch-event-source.git","type":"git"},"_npmVersion":"9.8.1","description":"A JavaScript library using EventSource and fetch to quickly construct AI assistant applications.","directories":{},"_nodeVersion":"18.18.0","publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"webpack":"^5.99.9","webpack-cli":"^6.0.1"},"_npmOperationalInternal":{"tmp":"tmp/fetch-event-source_1.0.0_1750483585641_0.26493745309844585","host":"s3://npm-registry-packages-npm-production"}},"1.0.1":{"name":"@buletu/fetch-event-source","version":"1.0.1","keywords":["fetch","javascript","EventSource","FetchEventSource"],"author":{"name":"buletu"},"license":"ISC","_id":"@buletu/fetch-event-source@1.0.1","maintainers":[{"name":"buletu","email":"949362805@qq.com"}],"homepage":"https://github.com/buletunice/fetch-event-source#readme","bugs":{"url":"https://github.com/buletunice/fetch-event-source/issues"},"dist":{"shasum":"b1174d7664caf96a51abe9784b7aeb11b37c7c3f","tarball":"https://registry.npmjs.org/@buletu/fetch-event-source/-/fetch-event-source-1.0.1.tgz","fileCount":6,"integrity":"sha512-mrXMSlFTg5BO+bwg0OGQUGLU4fOlVgfs5BwMuA6fX9GtsaEesmSIXV2oeggB80d6NsdQuMQVuUDhNf7tuda6Yg==","signatures":[{"sig":"MEUCIQDQ95CIgOqivDouC8+CcufFruo+/WEgyO0aPE5xi0CZ9wIgcBLPa9YHCVlSzPf/Zi3xLFciyB65Qxwo7hAGWUQgNgI=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":10828},"main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1","build":"npx webpack"},"_npmUser":{"name":"buletu","actor":{"name":"buletu","type":"user","email":"949362805@qq.com"},"email":"949362805@qq.com"},"repository":{"url":"git://github.com/buletunice/fetch-event-source.git","type":"git"},"_npmVersion":"9.8.1","description":"A JavaScript library using EventSource and fetch to quickly construct AI assistant applications.","directories":{},"_nodeVersion":"18.18.0","publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"webpack":"^5.99.9","webpack-cli":"^6.0.1"},"_npmOperationalInternal":{"tmp":"tmp/fetch-event-source_1.0.1_1750556771175_0.5642862315263715","host":"s3://npm-registry-packages-npm-production"}},"1.0.2":{"name":"@buletu/fetch-event-source","version":"1.0.2","keywords":["fetch","javascript","EventSource","FetchEventSource"],"author":{"name":"buletu"},"license":"ISC","_id":"@buletu/fetch-event-source@1.0.2","maintainers":[{"name":"buletu","email":"949362805@qq.com"}],"homepage":"https://github.com/buletunice/fetch-event-source#readme","bugs":{"url":"https://github.com/buletunice/fetch-event-source/issues"},"dist":{"shasum":"821697b212c096763c03041c43b8a9e856303001","tarball":"https://registry.npmjs.org/@buletu/fetch-event-source/-/fetch-event-source-1.0.2.tgz","fileCount":6,"integrity":"sha512-t2LiIgZ3YJTog+Af4m2yY15y0Taa5d9GzRBoLHa4s6F+btC2oI9/Aavr7aL5jNFTQPd6mPjkOL2IlDVxWDHn7w==","signatures":[{"sig":"MEUCICDfo/uDBoBhE1SuI2qWyozWKmJSubAAs8nrm5cg7fx7AiEAgVwICPnLy3FKBofeMNVy7MaH1MvVSRlwnkJnGcov3o4=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":10967},"main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1","build":"npx webpack"},"_npmUser":{"name":"buletu","actor":{"name":"buletu","type":"user","email":"949362805@qq.com"},"email":"949362805@qq.com"},"repository":{"url":"git://github.com/buletunice/fetch-event-source.git","type":"git"},"_npmVersion":"9.8.1","description":"A JavaScript library using EventSource and fetch to quickly construct AI assistant applications.","directories":{},"_nodeVersion":"18.18.0","publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"webpack":"^5.99.9","webpack-cli":"^6.0.1"},"_npmOperationalInternal":{"tmp":"tmp/fetch-event-source_1.0.2_1750559349122_0.35522499786504524","host":"s3://npm-registry-packages-npm-production"}},"1.0.3":{"name":"@buletu/fetch-event-source","version":"1.0.3","keywords":["fetch","javascript","EventSource","FetchEventSource"],"author":{"name":"buletu"},"license":"ISC","_id":"@buletu/fetch-event-source@1.0.3","maintainers":[{"name":"buletu","email":"949362805@qq.com"}],"homepage":"https://github.com/buletunice/fetch-event-source#readme","bugs":{"url":"https://github.com/buletunice/fetch-event-source/issues"},"dist":{"shasum":"9b445d6047b40229fdc36537e563f2bcfa4c59f9","tarball":"https://registry.npmjs.org/@buletu/fetch-event-source/-/fetch-event-source-1.0.3.tgz","fileCount":6,"integrity":"sha512-id159PZUo3PqmEzmQRySXqELffyYqUmgvZVw8eOhV//Bp8fZRT9PEBClpNFiF03oiqSVA1Sq+E86cukE+dTbuw==","signatures":[{"sig":"MEUCIQDht7I2S81g8akAIBD/kDNfV3X2QjQB3qBER+Z7SN007QIgCz2Iqdw+4W7Dp09RWxnjb+0A8Y5Trq9WI6jYyQKd+kY=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":10986},"main":"index.js","gitHead":"d24ee630d20db9b35cb49e5b95978618220fc0c6","scripts":{"test":"echo \"Error: no test specified\" && exit 1","build":"npx webpack"},"_npmUser":{"name":"buletu","actor":{"name":"buletu","type":"user","email":"949362805@qq.com"},"email":"949362805@qq.com"},"repository":{"url":"git://github.com/buletunice/fetch-event-source.git","type":"git"},"_npmVersion":"9.8.1","description":"A JavaScript library using EventSource and fetch to quickly construct AI assistant applications.","directories":{},"_nodeVersion":"18.18.0","publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"webpack":"^5.99.9","webpack-cli":"^6.0.1"},"_npmOperationalInternal":{"tmp":"tmp/fetch-event-source_1.0.3_1750560319024_0.3913348061886628","host":"s3://npm-registry-packages-npm-production"}},"1.0.4":{"name":"@buletu/fetch-event-source","version":"1.0.4","description":"A JavaScript library using EventSource and fetch to quickly construct AI assistant applications.","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1","build":"npx webpack"},"repository":{"type":"git","url":"git+https://github.com/bluetunice/fetch-event-source.git"},"keywords":["fetch","javascript","EventSource","FetchEventSource"],"author":{"name":"buletu"},"license":"ISC","publishConfig":{"access":"public"},"devDependencies":{"webpack":"^5.99.9","webpack-cli":"^6.0.1"},"_id":"@buletu/fetch-event-source@1.0.4","gitHead":"6e87c171b66728abff5f77f9941486c8213e904e","bugs":{"url":"https://github.com/bluetunice/fetch-event-source/issues"},"homepage":"https://github.com/bluetunice/fetch-event-source#readme","_nodeVersion":"18.18.0","_npmVersion":"9.8.1","dist":{"integrity":"sha512-UQPv+6mWW6k2jECDLJUXbZcLVYiZGTNjGDzqRaxum9tDh0E4Cc8SjqNZiqZ+QjBhgRNJII5D80PYHLpOpGsYAg==","shasum":"99a529276e341a337d341d438a8b325bb4afff48","tarball":"https://registry.npmjs.org/@buletu/fetch-event-source/-/fetch-event-source-1.0.4.tgz","fileCount":6,"unpackedSize":11003,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQC7M8Edgql22x3oRsWz8FQToA+nLTYgVZrv77IxPHmhzQIhAO9r6IZT7h8J2iRi1LfMifPdqUrkcvYQEMJx3Yr+u/Hl"}]},"_npmUser":{"name":"buletu","email":"949362805@qq.com","actor":{"name":"buletu","email":"949362805@qq.com","type":"user"}},"directories":{},"maintainers":[{"name":"buletu","email":"949362805@qq.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/fetch-event-source_1.0.4_1750562660666_0.7016753382489771"},"_hasShrinkwrap":false}},"time":{"created":"2025-06-21T05:26:25.493Z","modified":"2025-06-22T03:24:21.037Z","1.0.0":"2025-06-21T05:26:25.867Z","1.0.1":"2025-06-22T01:46:11.340Z","1.0.2":"2025-06-22T02:29:09.328Z","1.0.3":"2025-06-22T02:45:19.190Z","1.0.4":"2025-06-22T03:24:20.844Z"},"bugs":{"url":"https://github.com/bluetunice/fetch-event-source/issues"},"author":{"name":"buletu"},"license":"ISC","homepage":"https://github.com/bluetunice/fetch-event-source#readme","keywords":["fetch","javascript","EventSource","FetchEventSource"],"repository":{"type":"git","url":"git+https://github.com/bluetunice/fetch-event-source.git"},"description":"A JavaScript library using EventSource and fetch to quickly construct AI assistant applications.","maintainers":[{"name":"buletu","email":"949362805@qq.com"}],"readme":"# @buletu/fetch-event-source - 基于 Fetch 的 EventSource 实现\n\n## 简介\n\n`@buletu/fetch-event-source` 是一个用于通过 Fetch API 实现 EventSource（服务器发送事件）的 JavaScript 类。它允许你以一种简单的方式与支持 SSE（Server-Sent Events）的服务器进行通信，接收实时更新的数据。该类提供了事件监听、自动重连等功能，方便开发者快速构建基于实时数据交互的应用程序，例如 AI 助手等场景。\n\n## 安装\n\n你可以通过 npm 安装此库（假设你已将其发布到 npm 或从本地路径安装）：\n\n```bash\nnpm install @buletu/fetch-event-source\n```\n\n或者，如果你是直接在浏览器中使用，可以通过以下方式引入：\n\n```html\n<script src=\"path/to/dist/fetch-event-source.js\"></script>\n```\n\n## 使用方法\n\n### 基本用法\n\n1. **引入库**\n\n    - 如果使用 npm 安装，在项目中引入：\n    ```javascript\n    import { FetchEventSource } from '@buletu/fetch-event-source';\n    ```\n    - 如果是直接在浏览器中使用，通过 `<script>` 标签引入后，可以直接使用 `FetchEventSource`。\n\n2. **创建实例并连接**\n\n    创建一个 `FetchEventSource` 实例，并指定目标 URL 和可选的配置选项。然后调用 `connect` 方法建立连接。\n\n    ```javascript\n    const eventSource = new FetchEventSource('https://example.com/events', {\n      method: 'POST',\n      headers: {\n        'Content-Type': 'application/json',\n        Accept: 'text/event-stream'\n      },\n      body: JSON.stringify({\n        key: 'value'\n      }),\n      retryInterval: 5000 // 自定义重连间隔时间，单位为毫秒，默认为 3000\n    });\n\n    eventSource.connect();\n    ```\n\n3. **监听事件**\n\n    通过 `addEventListener` 方法为特定事件添加监听器。当服务器发送事件时，相应的监听器会被触发。\n\n    ```javascript\n    eventSource.addEventListener('message', (event) => {\n      console.log('接收到消息事件：', event);\n      console.log('事件数据：', event.data);\n    });\n\n    eventSource.addEventListener('open', () => {\n      console.log('连接已打开');\n    });\n\n    eventSource.addEventListener('error', (err) => {\n      console.error('发生错误：', err);\n    });\n\n    eventSource.addEventListener('close', () => {\n      console.log('连接已关闭');\n    });\n    ```\n\n4. **关闭连接**\n\n    当不再需要接收事件时，可以通过调用 `close` 方法关闭连接。\n\n    ```javascript\n    eventSource.close();\n    ```\n\n### 自定义事件处理\n\n除了默认的 `message`、`open`、`error` 和 `close` 事件外，你还可以监听服务器发送的自定义事件类型。只需在服务器端发送带有自定义事件类型的事件，然后在客户端为该事件类型添加监听器即可。\n\n例如，服务器发送以下事件：\n\n```\nevent: custom-event\ndata: {\"key\":\"value\"}\n```\n\n客户端可以这样监听：\n\n```javascript\neventSource.addEventListener('custom-event', (event) => {\n  console.log('接收到自定义事件：', event);\n});\n```\n\n### 自动重连机制\n\n`FetchEventSource` 提供了自动重连功能。如果连接意外中断，它会在指定的重连间隔时间后尝试重新建立连接。你可以通过 `retryInterval` 选项自定义重连间隔时间。\n\n## 示例代码\n\n以下是一个完整的示例，展示了如何使用 `FetchEventSource` 与服务器进行通信并处理事件：\n\n```javascript\nimport { FetchEventSource } from '@buletu/fetch-event-source';\n\nconst eventSource = new FetchEventSource('https://example.com/events', {\n  method: 'POST',\n  headers: {\n    'Content-Type': 'application/json',\n    Accept: 'text/event-stream'\n  },\n  body: JSON.stringify({\n    key: 'value'\n  }),\n  retryInterval: 5000 // 自定义重连间隔时间\n});\n\neventSource.addEventListener('message', (event) => {\n  console.log('接收到消息事件：', event);\n});\n\neventSource.addEventListener('open', () => {\n  console.log('连接已打开');\n});\n\neventSource.addEventListener('error', (err) => {\n  console.error('发生错误：', err);\n});\n\neventSource.addEventListener('close', () => {\n  console.log('连接已关闭');\n});\n\neventSource.connect();\n\n// 在适当的时候关闭连接\n// eventSource.close();\n```\n\n## 注意事项\n\n- 确保服务器支持 SSE 并正确配置了响应头，例如 `Content-Type: text/event-stream`。\n- 根据实际需求合理设置 `retryInterval`，避免过短的重连间隔导致频繁请求服务器。\n- 在浏览器环境中使用时，需要注意跨域问题，确保服务器允许跨域请求。\n\n## 贡献\n\n欢迎贡献代码或提出改进建议！你可以通过提交 issue 或 pull request 参与项目。\n\n## 许可证\n\n本项目采用 [MIT License](LICENSE) 许可证。\n\n希望这个教程对你有帮助！如果有任何问题或需要进一步的说明，请随时告诉我。","readmeFilename":"readme.md"}