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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@agen/fetch

v0.5.10

Published

Tranforms fetch to async generators of blocks

Readme

@agen/fetch

This package contains methods transforming standard fetch method to an async generator for data blocks. In browsers it uses the standard fetch method and AbortController class. In NodeJS environment the node-fetch and the node-abort-controller polyfills should be used (see example below).

This package contains the following methods:

  • fetchData - returns an async iterator over binary blocks fetched from the specified URL
  • fetchWithAbort - launches a globally available fetch method and returns the result with added abort method; it is implemented using globally available AbortController class (see below)
  • handleFetchResults - handles results returned by the fetchWithAbort method and transforms them to an async iterator

fetchData

This method retrieves binary data from the specified HTTP resource and yields them as an async generator over binary chunks. This method accepts the same parameters as the standard fetch method.

Example: get binary data from the specified URL:


import { fetchData } from '@agen/fetch';

(async () => {
  const url = 'http://localhost:8080/my/images/img.jpg';
  const gen = fetchData(url);
  for await (let chunk of gen) {
    console.log('*', chunk);
  }
})();

Basically this method calls the fetchWithAbort and the handleFetchResults methods:

async function* fetchData(url, params = {}) {
  const res = await fetchWithAbort(url, params);
  yield* handleFetchResults(res);
}

See below how to use this method in the NodeJS environment.

fetchWithAbort

This method launches the standard fetch method and adds a abort method on the resulting object. To do so it uses the AbortController class.

Example:


import { fetchWithAbort } from '@agen/fetch';

(async () => {
  const url = 'http://localhost:8080/my/images/img.jpg';
  const res = await fetchWithAbort(url);
  ...
  await res.abort(); // Stop data loading

})();

See below how to use this method in the NodeJS environment.

handleFetchResults

This method transforms the response returned by the fetchWithAbort method to an AnyncGenerator. The example below shows the implementation of the fetchData method:


import { fetchWithAbort, handleFetchResults } from '@agen/fetch';

(async () => {

  const url = 'http://localhost:8080/README.txt';
  const res = await fetchWithAbort(url);
  const it = handleFetchResults(res);
  let text = '';
  for await (let block of it) {
    text += block.toString('UTF-8');
  }
  console.log(text);

})();

Overloading fetch and AbortController

In NodeJS environment the packages node-fetch and node-abort-controller should be used in the following way:


const { getGlobal } = require('@agen/ns');

setGlobal('fetch', require('node-fetch'));
setGlobal('AbortController', require('node-abort-controller'));

(async () => {
  const url = 'http://localhost:8080/my/images/img.jpg';
  const gen = fetchData(url);
  for await (let chunk of gen) {
    console.log('*', chunk);
  }
})();