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

advancedrequest

v1.0.15

Published

ES6 JS class for intervaled requests with retries and timeouts

Downloads

6

Readme

advancedrequest

ES6 JS class for intervaled requests with retries and timeouts

Installation

npm install advancedrequest --save

Usage

Manager for sending HTTP requests with multiple tries, wait intervals, and timeouts
Uses node module 'request'

This can be configured by adding names as keys and values with requiredInterval
in order to WAIT before sending another request of that name. If you wanted to
only send one request with name DevAPI_SendFriendRequest per minute, set the
requiredInterval to 1000*60 and the minute will automatically be enforced

All 'lastruns' start with the moment of script start
to make sure script restarts don't accidentally disobey the interval

Simple GET request example

let advancedrequest = require('advancedrequest');

let requestData = await new advancedrequest.AdvancedRequest({
  url: "http://api.somefriendsite.com/addFriend"
}).runAsync();

console.log("[+] API response received!", requestData);

Inheritance example

Here you can define our own logic to determine if a request was successful
or needs to be retried by subclassing advancedrequest.AdvancedRequest
Calling this.fail(timeoutInMs, messageToLog); will retry the request up to
10 times. This max can be passed in as an integer argument named maxRetries.
Call this.onFinish(this.data); in order to complete the request

class DevApiRequest extends advancedrequest.AdvancedRequest {
  constructor (args) {
    super(args); // always call superclass constructor!
  }
  // Override the postProcess function to provide your own checks
  postProcess () {
    // this.data is the body of the request.
    if (!this.data) {
      // If the response was blank, call this.fail to retry the same request in 10 seconds
      this.fail(10, "Request was blank! Is connection to internet broken?");
    } else if (this.data.indexOf('Too many requests in the last hour!') != -1) {
      // If the API has a limit and tells you about it, you can call this.fail and retry in 1 hour
      this.fail(60*60, "Hit rate limit. Waiting 1 hour to retry");
    } else {
      // All was clear! MAKE SURE YOU HAVE this line at the end of your request!
      this.onFinish(this.data);
    }
  }
};


// Then once you've written your DevApiRequest class, maybe you'd call it so:
function sendFriendRequestTo(targetId, callback) {
  let sendFriendRequestReq = new DevApiRequest({
      url: "http://api.somefriendsite.com/addFriend",
      method: "POST",
      name: "sendFriendRequest", // NOTE: used as identifier for interval
      postData: { targetId: targetId, auth_token: "24t4534token42i5h2"},
      callback: callback
  });

  sendFriendRequestReq.runAsync().then(callback);
}

sendFriendRequestTo(252452, function(json) {
  console.log("[+] API response received!", json);
});

Intervaled request example

Enforce a defined interval between requests with the same 'name' passed into them. Using the DevApiRequest class, we first set lastRunHash with the proper interval

// Set interval timeouts for the requests of a particular name
advancedrequest.setLastRunHash({
  'sendFriendRequest': { requiredInterval: 1000*15 }, // 15 seconds
  'removeFriend':      { requiredInterval: 1000*5, lastReqTime: new Date().getTime() }
});

sendFriendRequestTo(34, function(json) {
  // 15 seconds will be waited before next request is sent out
  sendFriendRequestTo(35, function(json) {
    // 15 seconds will be waited before next request is sent out
    sendFriendRequestTo(36, function(json) {
      console.log("[+] 3 friend requests sent successfully with 15 seconds between them");
    });
  });
});

Credits

http://x64projects.tk/