{"library":"parse-sse","title":"Parse Server-Sent Events","description":"`parse-sse` is a lightweight, spec-compliant JavaScript library designed for parsing Server-Sent Events (SSE) from a standard Web `Response` object, typically obtained via the native Fetch API. Currently at version 0.1.0, it is a new release focused on providing a robust and efficient way to consume streaming APIs. The library differentiates itself by being fully compatible with native `ReadableStream` and `TransformStream` interfaces, enabling maximum composability with other stream-based operations. It is particularly well-suited for integrating with modern streaming services like OpenAI and Anthropic, which extensively utilize SSE for real-time data delivery. As a relatively new package, a specific release cadence has not yet been established, but its focus on web platform standards and minimal dependencies suggests a stable foundation.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install parse-sse"],"cli":null},"imports":["import { parseServerSentEvents } from 'parse-sse';","import { ServerSentEventTransformStream } from 'parse-sse';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { parseServerSentEvents } from 'parse-sse';\n\nconst apiKey = process.env.OPENAI_API_KEY ?? 'YOUR_OPENAI_API_KEY_HERE';\n\nasync function getStreamingCompletion() {\n  const response = await fetch('https://api.openai.com/v1/chat/completions', {\n    method: 'POST',\n    headers: {\n      'Content-Type': 'application/json',\n      'Authorization': `Bearer ${apiKey}`,\n    },\n    body: JSON.stringify({\n      model: 'gpt-4',\n      messages: [{ role: 'user', content: 'Tell me a story about a futuristic city.' }],\n      stream: true,\n    }),\n  });\n\n  if (!response.body) {\n    throw new Error('Response body is null, expected a ReadableStream.');\n  }\n\n  console.log('--- Streaming Chat Completion ---\\n');\n  for await (const event of parseServerSentEvents(response)) {\n    if (event.data === '[DONE]') {\n      console.log('\\n--- Stream Ended ---');\n      break;\n    }\n\n    try {\n      const data = JSON.parse(event.data);\n      process.stdout.write(data.choices[0]?.delta?.content || '');\n    } catch (error) {\n      console.error('Error parsing event data:', event.data, error);\n    }\n  }\n}\n\ngetStreamingCompletion().catch(console.error);","lang":"typescript","description":"Demonstrates how to consume a streaming chat completion from OpenAI using `parse-sse` with the Fetch API, showing real-time output and handling the `[DONE]` signal.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}