close
Skip to main content

Built and signed on GitHub Actions

Works with
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score100%
Downloads50/wk
Published11 months ago (0.1.6)

A tiny (741b) utility for Server Sent Event (SSE) streaming via `fetch` and Web Streams API

function stream
stream(
input: RequestInfo | URL,
): Promise<AsyncGenerator<ServerSentEventMessage, void, unknown>>

Convenience function that will fetch with the given arguments and, if ok, will return the events async iterator.

If the response is not ok (status 200-299), the Response is thrown.

Examples

Example 1

// NOTE: throws `Response` if not 2xx status
let events = await stream('https://api.openai.com/...', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer <token>',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    stream: true,
    // ...
  })
});

for await (let event of events) {
  console.log('<<', JSON.parse(event.data));
}

Parameters

Return Type

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@lukeed/fetch-event-stream

Import symbol

import { stream } from "@lukeed/fetch-event-stream";
or

Import directly with a jsr specifier

import { stream } from "jsr:@lukeed/fetch-event-stream@^0.1.6";