npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fetch-stream-parser

v0.2.1

Published

A super lightweight lib for parsing ReadableStream which is the type of Response.body of fetch api.

Downloads

3

Readme

fetch-stream-parser

This lib is moved to new position.

Fetch api part is moved to @async-util/fetch

Websocket api part is moved to @async-util/websocket

A super lightweight lib for parsing ReadableStream which is the type of Response.body of fetch api, implement by async generator fuction, so we can write code in AsyncIterableIterator style.

Parser & fetchStreamParser

The parser ctor accepts a ReadableStream.

import { Parser } from 'fetch-stream-parser';

async function foo() {
  const resp = await fetch('https://your/stream/url', opts);
  const parser = new Parser(resp.body);
}

If you are parsing data from fetch response, use the fetchStreamParser, the arg is exactly the same as fetch.

imoport fetchStreamParser from 'fetch-stream-parser';
// or
imoport { fetchStreamParser } from 'fetch-stream-parser';

async function foo() {
  const parser = await fetchStreamParser('https://your/stream/url', opts);
}

Read data from stream

Here's a exmaple of read the data of OPENAI chat completions api with stream = true.

import fetchStreamParser from 'fetch-stream-parser';

const openAiKey = process.env.OPENAI_KEY;

(async function () {
  const fsp = await fetchStreamParser('https://api.openai.com/v1/chat/completions', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${openAiKey}`
    },
    body: JSON.stringify({
      stream: true,
      model: 'gpt-3.5-turbo',
      messages: [
        { role: 'system', content: 'You are a helpful assistant.' },
        { role: 'user', content: 'Hello, how are you?' }
      ]
    })
  });

  for await (const { data } of fsp.sse(true /* event.data is json, except the last one '[DONE]' */)) {
    console.log(data.choices?.[0] || data);
  }
})().catch(console.error);

Results will looks like:

{
  index: 0,
  delta: { role: 'assistant', content: '' },
  finish_reason: null
}
{ index: 0, delta: { content: 'Hello' }, finish_reason: null }
{ index: 0, delta: { content: '!' }, finish_reason: null }
{ index: 0, delta: { content: ' I' }, finish_reason: null }
{ index: 0, delta: { content: "'m" }, finish_reason: null }
{ index: 0, delta: { content: ' an' }, finish_reason: null }
{ index: 0, delta: { content: ' AI' }, finish_reason: null }
{ index: 0, delta: { content: ' assistant' }, finish_reason: null }
{ index: 0, delta: { content: ',' }, finish_reason: null }
{ index: 0, delta: { content: ' so' }, finish_reason: null }
{ index: 0, delta: { content: ' I' }, finish_reason: null }
{ index: 0, delta: { content: ' don' }, finish_reason: null }
{ index: 0, delta: { content: "'t" }, finish_reason: null }
{ index: 0, delta: { content: ' have' }, finish_reason: null }
{ index: 0, delta: { content: ' feelings' }, finish_reason: null }
{ index: 0, delta: { content: ',' }, finish_reason: null }
{ index: 0, delta: { content: ' but' }, finish_reason: null }
{ index: 0, delta: { content: ' I' }, finish_reason: null }
{ index: 0, delta: { content: "'m" }, finish_reason: null }
{ index: 0, delta: { content: ' here' }, finish_reason: null }
{ index: 0, delta: { content: ' to' }, finish_reason: null }
{ index: 0, delta: { content: ' help' }, finish_reason: null }
{ index: 0, delta: { content: ' you' }, finish_reason: null }
{ index: 0, delta: { content: '.' }, finish_reason: null }
{ index: 0, delta: { content: ' How' }, finish_reason: null }
{ index: 0, delta: { content: ' can' }, finish_reason: null }
{ index: 0, delta: { content: ' I' }, finish_reason: null }
{ index: 0, delta: { content: ' assist' }, finish_reason: null }
{ index: 0, delta: { content: ' you' }, finish_reason: null }
{ index: 0, delta: { content: ' today' }, finish_reason: null }
{ index: 0, delta: { content: '?' }, finish_reason: null }
{ index: 0, delta: {}, finish_reason: 'stop' }
[DONE]

Or write data to stdout, you will feel it is typing.

  for await (const { data } of fsp.sse(true)) {
    const delta = data.choices?.[0].delta?.content;
    if (delta) process.stdout.write(delta);
  }

Read data from websocket.

import { getWsEvents } from 'fetch-stream-parser';

(async function () {
  const ws = new WebSocket('wss://gateway.discord.gg/?v=9&encoding=json');
  setTimeout(() => ws.send('{"op":1,"d":6}'), 1000);
  setTimeout(() => ws.close(), 5000);

  for await (const event of getWsEvents(ws)) {
    console.log(event);
  }

  console.log('socket closed.');
})(console.error);

sse

If the data source is Server Sent Events

async function foo() {
  const parser = await fetchStreamParser('https://your/stream/url', opts);
  for await (const evt of paser.sse(true /* if the data of event is json format. */)) {
    console.log(evt.event, evt.id, evt.data);
  }
}

lines

async function foo() {
  const parser = await fetchStreamParser('https://your/stream/url', opts);
  for await (const line of paser.lines()) {
    console.log(line);
  }
}

json data

If every non-empty line is valid json:

async function foo() {
  const parser = await fetchStreamParser('https://your/stream/url', opts);
  for await (const j of paser.json()) {
    console.log(j);
  }
}

chunks

async function foo() {
  const parser = await fetchStreamParser('https://your/stream/url', opts);
  for await (const chunk of paser.chunks()) {
    console.log(chunk);
  }
}