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

@codejamboree/web-request-queue

v3.2.0

Published

Throttle web requests

Downloads

7

Readme

web-request-queue

Simple utility to throttle web requests to reduce heavy traffic on your website.

Queing

You can call queue with the same arguments as https.request. However, it returns a promise that eventually resolves into a request, rather than the request itself.

import { webRequest } from '@codejamboree/web-request-queue';

const callback = res => {
  let buffers = [];
  res.on('data', data => buffers.push(data));
  res.on('error', err => console.error(err));
  res.on('end', () => {
    console.log(Buffer.concat(buffers).toString());
  });
};

// same args as https.request
webRequest.queue(`https://localhost/api?id=${i}`, { method: 'GET' }, callback)
  .then(req => {
    req.on('error', err => console.error(err));
    req.end();
  })
  .catch(err => console.error(err));

Callbacks

If you prefer to work with callbacks, you can pass them as an object, where the original arguments are passed as the args key.

const onRequested = req => {
  req.on('error', (err) => console.error(err));
  req.write("Posted form data");
  req.end();
}
webRequest.queueWithCallbacks({
  args: [url, {method: 'POST'}, callback],
  onRequested
});

Canceling Requests

To cancel all requests currently in the queue, call the method

const onCancel = err => console.log(err);
webRequest.queueWithCallbacks({args: ["https://localhost"], onCancel});
webRequest.cancel("I have my reasons");
// Output: I have my reasons

Configuration

Configuration options can be passed to adjust the behavior. Each key is optional.

webRequest.configure({
  requestsPerPeriod: 10,
  secondsPerPeriod: 60,
  pause: false
});
  • requestsPerPeriod: The total number of requests permitted per period.
  • secondsPerPeriod: The number of seconds within a period.
  • pause: Start/Stop the requests without clearing/canceling the queue

Request Rate

You may set the number of requests allowed within a given period.

// Limit to 10 requests per minute
webRequest.configure({ reqeustsPerPeriod: 10, secondsPerPeriod: 60 });

Info

You can request information about the current state of the queue.

console.log(webRequest.info());
// {
//  "requested": 32,
//  "queued": 12000,
//  "firstAt": 2024-08-30T23:51:22.818Z,
//  "paused": false
// }

With it, you can work out the progress of a batch of requests.


const label = 'Queue';
console.time(label);

let interval = setInterval(() => {
  const { requested, queued } = webRequest.info();
  console.timeLog(label, 'Requested', requested, 'of', requested + queued);
}, 1000);

const queueId = id => webRequest.queue(`https://localhost/${id}`);

Promise.all([1,2,3].map(queueId))
    .finally(() => {
      clearInterval(interval);
      console.timeEnd(label);
    });
// Queue: 1.002s Requested 1 of 3
// Queue: 2.004s Requested 1 of 3
// Queue: 3.005s Requested 2 of 3
// Queue: 4.007s Requested 2 of 3

Helper Functions

A few helper functions are included to simplify working with websites.

Saving Files

webRequest.toFile(filePath, url);

Getting a string

const html = await webRequest.asString(url);
console.log(html);

Send to a stream

webRequest.toStream(process.stdout, "https://localhost").then(stream => stream.end());

Parse JSON responses

const url = "https://localhost/api/user/id";

const user = webRequest.parseJson(url);
console.log('Date', user.date, typeof user.date);
// Output: Date 2024-09-05T00:00:00Z string

const reviver = (key, value) => {
  return key === 'date' ? new Date(value) : value;
}

const user = webRequest.parseJsonWithRevivor(reviver, url);
console.log('Date', user.date, typeof user.date);
// Output: Date 2024-09-05T00:00:00Z object

// Generic Types supported
const user = webRequest.parseJson<User>(url);
const user = webRequest.parseJsonWithRevivor<User>(reviver, url);