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

@forivall/wyt

v2.0.4

Published

A time-based rate limiter using promises

Downloads

1,886

Readme

wyt - time-based rate limiter

Build Status Codebeat badge Codacy Badge Maintainability Coverage Status

NPM

wyt (pronounced wait) is a time-based rate limiter that uses promises.

Use Cases

  • Limiting calls to APIs that only allow a limited number of requests within a certain period. Just call it before your HTTP requests.

  • As an alternative to setInterval or setTimeout in polling loops.

    If you want to run run a function every second and you use setInterval, your function might run multiple times at the same time if your function occasionally takes more than 1 second to run. This might cause problems.

    Alternatively, if you use setTimeout(update, 1000) in a recursive update loop, your update interval will be 1000ms plus the time it takes for your update function to run. You'd have to measure the time and dynamically set the timeout.

    wyt takes care of this for you. It'll make sure that your update function runs every second unless the update function takes more than 1s to run, in which case it will run it again immediately.

Features

  • Promise-based API
  • Full test coverage
  • TypeScript support
  • Easy to use

Installation

npm install --save wyt

Usage

Rate-limiting API calls

Just put it in front of your HTTP requests.

import wyt from 'wyt';

// 5 API calls per second
const rateLimit = wyt(5, 1000);

async function getStuff() {
  await rateLimit();
  const response = await fetch('/stuff');
  return response.json();
}

await getStuff();
await getStuff();
await getStuff();
await getStuff();
await getStuff();
await getStuff(); // has to wait unless the previous 5 calls together took longer than 1000ms

Update loops

import wyt from 'wyt';

// Once per second
const rateLimit = wyt(1, 1000);

async function update() {
  await rateLimit();
  await updateMyStuff();
  update();
}

update();

If, for example, updateMyStuff() takes 900ms then rateLimit() will wait only 100ms.

API

const rateLimit = wyt(requestsPerInterval: number, interval: number)

  • requestsPerInterval (number): The number of requests that can be executed within a certain interval.
  • interval (number): The interval within which requestsPerInterval can be executed.

Returns a function (requests?: number) => Promise<number> that can be called before before requesting a rate-limited resource in order to not exceed the limit.

const timeWaited = await rateLimit(requests?: number)

  • requests (number, optional, default: 1): The number of requests that will be used at the same time. For example, if your code fetches two resources in parallel. You can not use more requests at the same time as requestsPerInterval.

Returns a promise Promise<number> that will resolve with the time waited as soon as another request can be made. If more than requestsPerInterval are requested at the same time the promise will reject.

License

Copyright (c) 2017 - 2020 Max Kueng and contributors

MIT License