{"_id":"@alibsp/event-stream","_rev":"5-cb4309a78e96cbf046c212f29eb3f23a","name":"@alibsp/event-stream","dist-tags":{"latest":"1.0.2"},"versions":{"1.0.0":{"name":"@alibsp/event-stream","version":"1.0.0","keywords":["ice","react","component"],"license":"MIT","_id":"@alibsp/event-stream@1.0.0","maintainers":[{"name":"mo.zhou","email":"bjfuzm@163.com"},{"name":"sususuper","email":"544074170@qq.com"},{"name":"jwellying","email":"2428227179@qq.com"},{"name":"jasoncapricorn","email":"17682318001@163.com"}],"homepage":"https://unpkg.com/@alibsp/event-stream@1.0.0/build/index.html","dist":{"shasum":"23a9a62c9b304065aac06c1353f8330d8a0bcac7","tarball":"https://registry.npmjs.org/@alibsp/event-stream/-/event-stream-1.0.0.tgz","fileCount":18,"integrity":"sha512-MCJpVjNw3wHHc4Iiv86OHfnfeq++ds6BoLviqnN1L/ogTBe27tKpXFZ1rotK5CF+7nhwzKJk6QwZiZsC0QCFPA==","signatures":[{"sig":"MEQCIAbm0Q2JWQv1DHRqqJpUzYOmNRB/Ln40h/kxrDUepgqLAiB6egjbq9UjBmIJo7/9SOtrM5hylq6M3TtYZxFjfnuXIw==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":550200,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJkOMcAACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmqx4A/9EeGDI62JngGnXqyJtziM38Ew8zEoi2RtodrXhXXV57xDIXdG\r\nHmCKxAn88691JtvuV4ttHjcxS41ZywjMCt5mLVS3r6kE2UUxf7+Vlr0tsxzy\r\nh6hTt0AnyKo8KpjPgoyqQBCgA6Ftm8hWKDyYW0pfKt6qjNvF7Wn6jJoPFJfK\r\n+6WRdWoDiqcAojEolT8V/6wdH78eFHBiQ/cwewrmROkzKCFOzw0or9Jj2roD\r\n3sRieeEric+GsAxnypuMgIzmg5U5HQVauKMcqFR8S+omtWHL/jfl2cqfXk25\r\nd21C5lGpfMvSzcAx07V6V9RhU3EvyOueC3DzOYUaIEG4MN/49MB5xgEnYuBu\r\nT2uxlagzHhgqEUbY7XUJSQ4y2PJJH/5/sluKUzNh6+t5v2C5n5cwN63EYm4e\r\nfHtSZgelHfhZ1HQeo40eDZDrrdmUXBdDbx/Ko2hTtnCCtMgpDKQsnqUnb8Qv\r\nBLHr9jVpRnTF1kFqYU3U6xrRDH5F8WiGcnUDAAewZeEUADSwlheKC23p8KGO\r\na3my+YWqdsE8cNowpjm1w77JUv9ErG8CcaGnZZ4vH5Pi31BmBdx4iIhXSTE/\r\nDRhFUXnS8ivbku6TZtJlpz2H4+MITKzUhjdaYIAazzDh0MAMedPhMSodY0GZ\r\nQOzH+jVfbEepu1B507Zbp1J0Ue1gi3FeHFM=\r\n=BXKv\r\n-----END PGP SIGNATURE-----\r\n"},"main":"lib/index.js","module":"es/index.js","scripts":{"lint":"npm run eslint && npm run stylelint","test":"build-scripts test","build":"build-scripts build","start":"build-scripts start","eslint":"eslint --cache --ext .js,.jsx,.ts,.tsx ./","stylelint":"stylelint \"**/*.{css,scss,less}\"","eslint:fix":"npm run eslint -- --fix","prepublishOnly":"npm run build"},"_npmUser":{"name":"jasoncapricorn","email":"17682318001@163.com"},"_npmVersion":"6.14.13","description":"对话式流请求库","directories":{},"sideEffects":["dist/*","*.scss","*.less","*.css"],"_nodeVersion":"14.17.0","dependencies":{},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"componentConfig":{"name":"ApiDebug","title":"api调试功能","category":"Others"},"devDependencies":{"react":"^17.0.2","enzyme":"^3.10.0","eslint":"^7.0.0","react-dom":"^17.0.2","stylelint":"^13.7.2","@types/react":"^17.0.2","build-scripts":"^1.1.1","@iceworks/spec":"^1.0.0","@types/react-dom":"^17.0.2","build-plugin-component":"^1.0.0","enzyme-adapter-react-16":"^1.14.0"},"peerDependencies":{"react":"^16 || ^17"},"_npmOperationalInternal":{"tmp":"tmp/event-stream_1.0.0_1681442560167_0.19195183835554852","host":"s3://npm-registry-packages"}},"1.0.1":{"name":"@alibsp/event-stream","version":"1.0.1","keywords":["ice","react","component"],"license":"MIT","_id":"@alibsp/event-stream@1.0.1","maintainers":[{"name":"mo.zhou","email":"bjfuzm@163.com"},{"name":"sususuper","email":"544074170@qq.com"},{"name":"jwellying","email":"2428227179@qq.com"},{"name":"jasoncapricorn","email":"17682318001@163.com"}],"homepage":"https://unpkg.com/@alibsp/event-stream@1.0.1/build/index.html","dist":{"shasum":"69d4bb80e50532d13f49c3dc6acb07d953f98c1a","tarball":"https://registry.npmjs.org/@alibsp/event-stream/-/event-stream-1.0.1.tgz","fileCount":18,"integrity":"sha512-0soKgu0lbMbrGPrCM7rYbLXEoFhrAaFayPlRBkv+7F7NIMf8MOKLZoVtn1KTN9MnUTjtSS7F7vZZaqf4mWY45Q==","signatures":[{"sig":"MEUCIQDJ5/Qf0s1LYZq4IOavURUV6gn+I1ZBcYxffsnGKW2oLgIgGSObwvp6FbLYKELkMYLC1SVkW9GFjdKEIaARpwsAPQI=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":550003,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJkONoSACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmpcOQ/7BjYn5AZABpF5fqghGrcaWgBBGhONLqjhAOBhUmO40Z7wjRpa\r\nCl0L6WGAgHrCOK4ysDnmFJd7qixkssiDsm2W0qLPkf2RHrj27/2ApT9kOkUE\r\nQBkuhOsMVYWwuERmH4ZhY6O558vUJinHBlEMQE8ufw5U4+divf+18EKSgFMU\r\niS+vkKOeK3LMm3hrmgbOwzGCA2W1AdIzbiavSsrT81weEIrtLrAvqeM6Vt6R\r\nF4DNN/2lW75i2lEDbBzkPnVEEToaDnKQD1hdM8y1LIqNLE12slvDcA7lUIEw\r\ngauxHQvWtwn19KO89gZLwTxKyGbd30Yw1+nO0LxX/ISm/HD1TbWqoOFI7ODq\r\nW3iXsVnvSb1zjsvSB+Jje9XQhADMsayFz6rRNsWBR3AddZeSmD18vejrAq1a\r\nCfq67dA+6+2q76+asnf1WgAfzPaJotp20537X+qCTPPOQozl/Uio/Mv52ACl\r\nV/+W6NG6ROkRbYzl20ulwk0mPejgQ8adizC8xPrygwccSnnmjU3be6oKmwyC\r\ndEvojiKYr2I6pXqEzrQvh3vm+3Loqr0YEB4qktKftfG5sIqzdVNOyAP3DKwb\r\nyZFQJjNvs1bOzEeu2VHFyEYdGu6fgIe67zD21W+IBWQxyJZdr2kpvEiME4Lo\r\n7oXbx1T6mLNd0qQIahnEuij15n0hEWUlb9U=\r\n=bBJI\r\n-----END PGP SIGNATURE-----\r\n"},"main":"lib/index.js","module":"es/index.js","scripts":{"lint":"npm run eslint && npm run stylelint","test":"build-scripts test","build":"build-scripts build","start":"build-scripts start","eslint":"eslint --cache --ext .js,.jsx,.ts,.tsx ./","stylelint":"stylelint \"**/*.{css,scss,less}\"","eslint:fix":"npm run eslint -- --fix","prepublishOnly":"npm run build"},"_npmUser":{"name":"jasoncapricorn","email":"17682318001@163.com"},"_npmVersion":"6.14.13","description":"对话式流请求库","directories":{},"sideEffects":["dist/*","*.scss","*.less","*.css"],"_nodeVersion":"14.17.0","dependencies":{},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"componentConfig":{"name":"ApiDebug","title":"api调试功能","category":"Others"},"devDependencies":{"react":"^17.0.2","enzyme":"^3.10.0","eslint":"^7.0.0","react-dom":"^17.0.2","stylelint":"^13.7.2","@types/react":"^17.0.2","build-scripts":"^1.1.1","@iceworks/spec":"^1.0.0","@types/react-dom":"^17.0.2","build-plugin-component":"^1.0.0","enzyme-adapter-react-16":"^1.14.0"},"peerDependencies":{"react":"^16 || ^17"},"_npmOperationalInternal":{"tmp":"tmp/event-stream_1.0.1_1681447442343_0.8461779559050471","host":"s3://npm-registry-packages"}},"1.0.2":{"name":"@alibsp/event-stream","version":"1.0.2","keywords":["ice","react","component"],"license":"MIT","_id":"@alibsp/event-stream@1.0.2","maintainers":[{"name":"mo.zhou","email":"bjfuzm@163.com"},{"name":"sususuper","email":"544074170@qq.com"},{"name":"jwellying","email":"2428227179@qq.com"},{"name":"jasoncapricorn","email":"17682318001@163.com"}],"homepage":"https://unpkg.com/@alibsp/event-stream@1.0.2/build/index.html","dist":{"shasum":"83ab725a86c5092d66613e087e694afc0cd81864","tarball":"https://registry.npmjs.org/@alibsp/event-stream/-/event-stream-1.0.2.tgz","fileCount":18,"integrity":"sha512-SO+xsEeKmD/vNFYsytKacFAuVR0Ue0t92p99TdLwPYX1o/nNY/ibrGt5wGozt2pPll48OXJ3dOlxQGumsmWcjg==","signatures":[{"sig":"MEYCIQCkLR+YYgXYK8HhvcvZARmNfQ6ZNSWmBtYQPcgEq/MhTgIhALwr6kGTYvGC5M9FMUXxZ2G29YMZNlkD86Kw2GTCWAiL","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":550112,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJkOPF1ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmrQTg/+Pjr5DHQI91vQYROKljYdR8J6WYaj1qqH0lXU8AZHxcZVJRAc\r\njRqWCGkOqOMhNaEgbCbeEQWjd+ijmQtTxgVseOMIZfxsiLsomVzWTWu5HVnm\r\n67TySSvdvZbymBidE+bSDX8M90xLozAx+H8AnSSr/Z8Z8byqJ83AIhIUbqn4\r\nX6rdobruOsD/u84OdQSk4GMejBrqh7cpH2zXOZR0Catsg9Yf009g74GFZ+xA\r\nz49Clubw4yX6CVIh0WwfETO7d8n+qYp3/upwZJ14Ob8h1+A1kMull5OIe/5K\r\nFP0iEffvfUgLJUr8n1hCsFZDPrriW+TWScnfHqCeZ6IpAQBmoq/GoGMJSsmK\r\nG939s/c2y+uP2EbEbhVnPS15oCqi3P76p7JZMa4NH4+/ZOWKZRnBl7qIy6VS\r\n5Z4f4nIQ0VHWtwMdxwxbKC6t3kRNspEEOLJJACC7b00SK7VxcPpqLCoU3jU6\r\n1M9+iNngOoWkpMzagHEEUBofJy9PQwAEmUyLo4OxHJl4tRiNVinUz4zoW4M3\r\nQhB3TSIObJSy6pctFASDGSq3NZQM39OcPF0HWCe8DHSuQg+IbsSbuEoqUvtA\r\nlhAChMYMwD3f3zVW3a2+Gn3BAenGhxZU6s90RT8PblwMM89IM9NZTEDz9M/3\r\nXohzAo2Ub9EpdiaImYs8WH9alVnPCZKnvM8=\r\n=GNlx\r\n-----END PGP SIGNATURE-----\r\n"},"main":"lib/index.js","module":"es/index.js","scripts":{"lint":"npm run eslint && npm run stylelint","test":"build-scripts test","build":"build-scripts build","start":"build-scripts start","eslint":"eslint --cache --ext .js,.jsx,.ts,.tsx ./","stylelint":"stylelint \"**/*.{css,scss,less}\"","eslint:fix":"npm run eslint -- --fix","prepublishOnly":"npm run build"},"_npmUser":{"name":"jasoncapricorn","email":"17682318001@163.com"},"_npmVersion":"6.14.13","description":"对话式流请求库","directories":{},"sideEffects":["dist/*","*.scss","*.less","*.css"],"_nodeVersion":"14.17.0","dependencies":{},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"componentConfig":{"name":"ApiDebug","title":"api调试功能","category":"Others"},"devDependencies":{"react":"^17.0.2","enzyme":"^3.10.0","eslint":"^7.0.0","react-dom":"^17.0.2","stylelint":"^13.7.2","@types/react":"^17.0.2","build-scripts":"^1.1.1","@iceworks/spec":"^1.0.0","@types/react-dom":"^17.0.2","build-plugin-component":"^1.0.0","enzyme-adapter-react-16":"^1.14.0"},"peerDependencies":{"react":"^16 || ^17"},"_npmOperationalInternal":{"tmp":"tmp/event-stream_1.0.2_1681453429149_0.4741831230255138","host":"s3://npm-registry-packages"}}},"time":{"created":"2023-04-14T03:22:40.100Z","modified":"2024-12-12T03:49:26.867Z","1.0.0":"2023-04-14T03:22:40.451Z","1.0.1":"2023-04-14T04:44:02.581Z","1.0.2":"2023-04-14T06:23:49.489Z"},"license":"MIT","homepage":"https://unpkg.com/@alibsp/event-stream@1.0.2/build/index.html","keywords":["ice","react","component"],"description":"对话式流请求库","maintainers":[{"email":"17682318001@163.com","name":"jasoncapricorn"},{"email":"2428227179@qq.com","name":"jwellying"},{"email":"544074170@qq.com","name":"sususuper"},{"email":"xiazhiqiangcuc@126.com","name":"xiazhiqiang"},{"email":"wanyi.css@alibaba-inc.com","name":"wanyi.css"},{"email":"bjfuzm@163.com","name":"mo.zhou"},{"email":"chris_jun@aliyun.com","name":"qingkaili"}],"readme":"# Fetch Event Source\n\nThis package provides a better API for making [Event Source requests](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events) - also known as server-sent events - with all the features available in the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API).\n\nThe [default browser EventSource API](https://developer.mozilla.org/en-US/docs/Web/API/EventSource) imposes several restrictions on the type of request you're allowed to make: the [only parameters](https://developer.mozilla.org/en-US/docs/Web/API/EventSource/EventSource#Parameters) you're allowed to pass in are the `url` and `withCredentials`, so:\n\n- You cannot pass in a request body: you have to encode all the information necessary to execute the request inside the URL, which is [limited to 2000 characters](https://stackoverflow.com/questions/417142) in most browsers.\n- You cannot pass in custom request headers\n- You can only make GET requests - there is no way to specify another method.\n- If the connection is cut, you don't have any control over the retry strategy: the browser will silently retry for you a few times and then stop, which is not good enough for any sort of robust application.\n\nThis library provides an alternate interface for consuming server-sent events, based on the Fetch API. It is fully compatible with the [Event Stream format](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format), so if you already have a server emitting these events, you can consume it just like before. However, you now have greater control over the request and response so:\n\n- You can use any request method/headers/body, plus all the other functionality exposed by fetch(). You can even provide an alternate fetch() implementation, if the default browser implementation doesn't work for you.\n- You have access to the response object if you want to do some custom validation/processing before parsing the event source. This is useful in case you have API gateways (like nginx) in front of your application server: if the gateway returns an error, you might want to handle it correctly.\n- If the connection gets cut or an error occurs, you have full control over the retry strategy.\n\nIn addition, this library also plugs into the browser's [Page Visibility API](https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API) so the connection closes if the document is hidden (e.g., the user minimizes the window), and automatically retries with the last event ID when it becomes visible again. This reduces the load on your server by not having open connections unnecessarily (but you can opt out of this behavior if you want.)\n\n# Install\n\n```sh\nnpm install @alibsp/event-stream\n```\n\n# Usage\n\n```ts\n// BEFORE:\nconst sse = new EventSource(\"/api/sse\");\nsse.onmessage = (ev) => {\n  console.log(ev.data);\n};\n// AFTER:\nimport { fetchEventSource } from \"@alibsp/event-stream\";\nawait fetchEventSource(\"/api/sse\", {\n  onmessage(ev) {\n    console.log(ev.data);\n  },\n});\n```\n\nYou can pass in all the [other parameters](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters) exposed by the default fetch API, for example:\n\n```ts\nconst ctrl = new AbortController();\nfetchEventSource(\"/api/sse\", {\n  method: \"POST\",\n  headers: {\n    \"Content-Type\": \"application/json\",\n  },\n  body: JSON.stringify({\n    foo: \"bar\",\n  }),\n  signal: ctrl.signal,\n});\n```\n\nYou can add better error handling, for example:\n\n```ts\nclass RetriableError extends Error {}\nclass FatalError extends Error {}\nfetchEventSource(\"/api/sse\", {\n  async onopen(response) {\n    if (\n      response.ok &&\n      response.headers.get(\"content-type\") === EventStreamContentType\n    ) {\n      return; // everything's good\n    } else if (\n      response.status >= 400 &&\n      response.status < 500 &&\n      response.status !== 429\n    ) {\n      // client-side errors are usually non-retriable:\n      throw new FatalError();\n    } else {\n      throw new RetriableError();\n    }\n  },\n  onmessage(msg) {\n    // if the server emits an error message, throw an exception\n    // so it gets handled by the onerror callback below:\n    if (msg.event === \"FatalError\") {\n      throw new FatalError(msg.data);\n    }\n  },\n  onclose() {\n    // if the server closes the connection unexpectedly, retry:\n    throw new RetriableError();\n  },\n  onerror(err) {\n    if (err instanceof FatalError) {\n      throw err; // rethrow to stop the operation\n    } else {\n      // do nothing to automatically retry. You can also\n      // return a specific retry interval here.\n    }\n  },\n});\n```\n","readmeFilename":"README.md"}