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

@aegenet/belt-fetch

v1.5.0

Published

bFetch is fetch with a default timeout and an asynchronous DNS resolver (node only). +fetch utility: ensureFetch response

Downloads

15

Readme

npm version

@aegenet/belt-fetch

Belt fetch

💾 Installation

yarn add @aegenet/belt-fetch@^1.4.0
# or
npm i @aegenet/belt-fetch@^1.4.0

bFetch

Why?

bFetch is fetch with:

  • a configurable timeout (default 10 seconds)
  • (node only) an asynchronous DNS resolver: by default, native fetch uses a synchronous DNS resolver*, which can potentially block your entire Node.js process.

Though the call to dns.lookup() will be asynchronous from JavaScript's perspective, it is implemented as a synchronous call to getaddrinfo(3) that runs on libuv's threadpool. This can have surprising negative performance implications for some applications, see the UV_THREADPOOL_SIZE documentation for more information.

Important: there is a drawback; the DNS is not resolved in the same way as dns.lookup. For example, the host file is not taken into account.

For more informations: https://nodejs.org/api/dns.html#implementation-considerations

Basic usage

import { bFetch, fetchEnsure } from '@aegenet/belt-fetch';

const response = await bFetch('https://jsonplaceholder.typicode.com/todos/1');
const results = await fetchEnsure(response);
// ->
{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
}

DNS Cache

import { bFetch, fetchEnsure } from '@aegenet/belt-fetch';

const response = await bFetch('https://jsonplaceholder.typicode.com/todos/1', {
  dnsCacheTTL: 10000
});
const results = await fetchEnsure(response);
// ->
{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
}

Custom DNS resolution

import { bFetch, fetchEnsure } from '@aegenet/belt-fetch';

const resp = await bFetch(
  'http://dontexistbutmapped:3030/text',
  {},
  {
    timeout: 200,
    replaceDNSByIP: true,
    dnsMapAsFallback: true,
    dnsMap: {
      dontexistbutmapped: '127.0.0.1',
    },
  }
);
// resp.status 200 (Hello World!)
import { bFetch, fetchEnsure } from '@aegenet/belt-fetch';

const resp = await bFetch(
  'http://github.com:3030/text',
  {},
  {
    timeout: 200,
    replaceDNSByIP: true,
    /** Not as a fallback, but as a primary resolver */
    dnsMapAsFallback: false,
    dnsMap: {
      'github.com': '127.0.0.1',
    },
  }
);
// resp.status 200 (Hello World!)

API

export type bFetchOptions = {
  /**
   * Timeout in milliseconds
   *
   * @default 10000
   *
   * @kind Browser/Node.js
   */
  timeout?: number;
  /**
   * Make the request using the IP address instead of the DNS name
   *
   * @kind Browser/Node.js
   */
  replaceDNSByIP?: boolean;
  /**
   * DNS Map { 'github.com': '127.0.0.1' }
   *
   * @kind Browser/Node.js
   */
  dnsMap?: Record<string, string>;
  /**
   * DNS Map as fallback or as a primary resolver
   *
   * @default true
   *
   * @kind Node.js
   */
  dnsMapAsFallback?: boolean;
  /**
   * DNS Cache TTL (ms)
   *
   * @default 0
   *
   * @kind Node.js
   */
  dnsCacheTTL?: number;
};

fetchEnsure

The purpose of this function is simply to ensure that the fetch response is OK and then format the response according to the content-type.

import { fetchEnsure } from '@aegenet/belt-fetch';

const response = await fetch('https://jsonplaceholder.typicode.com/todos/1');
const results = await fetchEnsure(response);
// ->
{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
}