1.1.2 • Published 3 months ago

eventsource-parser v1.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

eventsource-parser

npm versionnpm bundle sizenpm weekly downloads

A streaming parser for server-sent events/eventsource, without any assumptions about how the actual stream of data is retrieved. It is intended to be a building block for clients and polyfills in javascript environments such as browsers, node.js and deno.

You create an instance of the parser, and feed it chunks of data - partial or complete, and the parse emits parsed messages once it receives a complete message. A TransformStream variant is also available for environments that support it (modern browsers, Node 18 and higher).

Installation

npm install --save eventsource-parser

Usage

import {createParser, type ParsedEvent, type ReconnectInterval} from 'eventsource-parser'

function onParse(event: ParsedEvent | ReconnectInterval) {
  if (event.type === 'event') {
    console.log('Received event!')
    console.log('id: %s', event.id || '<none>')
    console.log('name: %s', event.name || '<none>')
    console.log('data: %s', event.data)
  } else if (event.type === 'reconnect-interval') {
    console.log('We should set reconnect interval to %d milliseconds', event.value)
  }
}

const parser = createParser(onParse)
const sseStream = getSomeReadableStream()

for await (const chunk of sseStream) {
  parser.feed(chunk)
}

// If you want to re-use the parser for a new stream of events, make sure to reset it!
parser.reset()
console.log('Done!')

Stream usage

import {EventSourceParserStream} from 'eventsource-parser/stream'

const eventStream = response.body
  .pipeThrough(new TextDecoderStream())
  .pipeThrough(new EventSourceParserStream())

Note that the TransformStream is exposed under a separate export (eventsource-parser/stream), in order to maximize compatibility with environments that do not have the TransformStream constructor available.

License

MIT © Espen Hovlandsdal

@chat-bots/commoncustom-gpt-api@trelent/openai-streamsryan-test1ryan-test2mingdao-web-mo@everything-registry/sub-chunk-1623custom-gpt-user@hackdance/agents-coresupanovel@helixai/chat-widget@sshkeda/ai@isnolan/bodhi-adapter@isnolan/bodhi-puppet@dust-tt/types@ericlewis/langchain@jonathan143/chatgpt@juukee/chatgpt@evertech/chatgpt-patched@johannlai/gptcli@locallm/api@fal-ai/serverless-client@logunify/chatgptcboarding-note-editor@justkahdri/ai@makay/rpccgpt@hackdance/agentsblazity-chatgpt-cjs@hotease/chatgptblocktrust-chatbotbiye-novel@gitbook/api@helixml/chat-widget@halfwork.cn/js@kjain0073/ollamachatgptapichatgpt-text-davinci-003chatgpt-turbochatgpt-v3chatgpt-vscodechatgpt-webchatgptkchatgptxchatgptx-browserchatgptzcwchatjptchatgpt-zpfchatgpt-lchatjet-aichatgpt-kodyfirechatgpt-mgchatgpt-minimumchatgpt-minuschatgpt-nextchatgpt-next-webchatgpt-nine-aichatgpt-nodejschatgpt-openai-cjschatgpt-p@kluai/gateway@kloblic/chatgpt-kchatmax-doc@lgrammel/ai-utils@leikeduntech/leiai-js@leapchat.io/js@libeilong/chatgpt@mirari/chatgpt-api@inkeep/ai-sdk@markapiado/novelchatgptchatgpt-ai-webchatgpt-api-aidbchatgpt-api-arnoldschatgpt-api-cjschatgpt-api-lawrencechatgpt-artemchatgpt-betterchatgpt-browserchatgpt-browser-apichatgpt-bschatgpt-cjschatgpt-cjs-jhchatgpt-commonjschatgpt-custom-headerschatgpt-devchatgpt-dooychatgpt-finedustchatgpt-fix-versionchatgpt-hashkeychatgpt-incjs@khulnasoft/ai-sdk@khulnasoft/ai-sdk-provide-utils@khulnasoft/ai-sdk-provider-utils@kittenbot/kitten_extensionchatxu-fix@markprompt/core@patikadev/novel@paotuan/api2d@ryen/jscopilot-studio-direct-to-engine-chat-adapter
1.1.2

3 months ago

1.1.1

8 months ago

1.1.0

8 months ago

1.0.0

1 year ago

0.1.0

1 year ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago