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

@fightmegg/riot-rate-limiter

v0.0.22

Published

Rate Limiter for Riot Games API

Downloads

584

Readme

Riot Rate Limiter

Version Downloads CircleCI

Node.JS Rate Limiter for Riot Games API

Wiki

Features

Contents

Installation

$ npm install @fightmegg/riot-rate-limiter

Usage

import { RiotRateLimiter } from "@fightmegg/riot-rate-limiter";

const limiter = new RiotRateLimiter();

// async await
const response = await limiter.execute({
  url: "https://euw1.api.riotgames.com/lol/some/method",
  options: {
    headers: {
      "X-Riot-Token": "...",
    },
  },
});

// Promises
limiter
  .execute({
    url: "https://euw1.api.riotgames.com/lol/some/method",
    options: {
      headers: {
        "X-Riot-Token": "...",
      },
    },
  })
  .then((response) => {
    // ...
  });

API

constructor

new RiotRateLimiter({
  debug: boolean = false,
  concurrency: number = 1,
  retryAfterDefault: number = 5000,
  retryCount: number = 4,
  datastore: 'local' | 'ioredis' = 'local'
  redis?: RedisConnectionOptions | RedisConnectionString = null
});

.execute

This library uses node-fetch underneath the hood for making requests. The .execute method literally passes everything given to node-fetch, therefore you can change things like method, headers etc..

Any responses that are not 2xx or 429 will be thrown, and must be caught.

We will auto-retry 429 responses, up until the retryCount limit is hit (defaults to 4), utilising the Retry-After header to respect the API.

limiter.execute({
  url: RequestInfo,
  options: RequestInit,
},
  jobOptions?: Bottleneck.JobOptions
);

The second argument is optional, which allows you to specify JobOptions such as job priority and unique ID for log identification. The weight of a job cannot be changed from the value of 1 no matter what you pass in.

.rateLimiters

This is a map of all of the rate-limiters created, we create at least 1 rate-limiter for the region of the request, and then at least 1 rate-limiter per method underneath that region.

We use the library Bottleneck as our rate-limiter, which supports chaining rate-limiters, meaning that the parents rate-limiter is always respected by its children.

You can access the region rate-limiters via: limiter.rateLimiters[region].main and you can access the method rate-limiters via: limiter.rateLimiters[region][method].main

Helpers

We also provided some helper functions & objects.

extractRegion

This can extract the region or platformId from your URL:

import { extractRegion } from "@fightmegg/riot-rate-limiter";

extractRegion("https://na1.api.riotgames.com/method"); // returns na1

extractMethod

This can extract the method from your URL:

import { extractMethod } from "@fightmegg/riot-rate-limiter";

extractMethod(
  "https://na1.api.riotgames.com/lol/champion-mastery/v4/scores/by-summoner/12345"
); // returns 'CHAMPION_MASTERY.GET_CHAMPION_MASTERY_SCORE'

METHODS & HOST

For those looking to build a RiotGamesAPI library ontop of this rate limiter, we export two object called METHODS & HOST. You can use these exports to create the URLs for you, as seen below:

import { compile } from "path-to-regexp";
import { HOST, METHODS } from "@fightmegg/riot-rate-limiter";

const createHost = compile(HOST, { encode: encodeURIComponent });
const createPath = compile(METHODS.ACCOUNT.GET_BY_PUUID, {
  encode: encodeURIComponent,
});

const url = `https://${createHost({ platformId: "euw1" })}${createPath({
  puuid: "12345",
})}`;

Debugging

If you want to see want the rate-limiter is currently doing, we use the debug module for logging. Simply run your app with:

DEBUG=riotratelimiter* node ...

Testing

Unit tests: npm test

E2E tests: npm run test:e2e

Maintainers

@olliejennings