Throttle HTTP Requests with `request`
throttled-request is a Node.js module designed to apply rate-limiting to outgoing HTTP requests by wrapping the now-deprecated `request` library. It provides functionality to configure request throttling based on a fixed rate (e.g., N requests within M milliseconds) or a dynamic delay determined by a provided function. The package is currently at version 0.1.1, indicating an early development stage and a lack of active maintenance. Its release cadence is effectively inactive, as its last update was long ago, and its core dependency, `request`, has been officially deprecated and archived. This module's primary differentiator was its simple integration with the `request` API, making it easy for users of that library to add throttling. However, its strong reliance on an unmaintained dependency makes it unsuitable for modern applications, which should seek alternatives built on actively maintained HTTP clients.
Common errors
-
TypeError: throttledRequest is not a function
cause The `throttled-request` package exports a factory function that needs to be called with an instance of the `request` library, but it was used directly or called without the `request` dependency.fixEnsure you `require('request')` separately and then pass that instance to `throttled-request` to initialize it: `var request = require('request'); var throttledRequest = require('throttled-request')(request);` -
TypeError: throttledRequest.get is not a function
cause Attempting to use a shortcut method like `.get()` or `.post()` which `throttled-request` does not implement; it only provides the main function signature.fixUse the main `throttledRequest(options, callback)` signature for all HTTP requests, specifying the method in the `options` object (e.g., `throttledRequest({ method: 'GET', url: '...' }, callback);`). -
npm WARN deprecated request@x.y.z: request has been deprecated
cause This warning indicates that `throttled-request` depends on the `request` library, which is officially deprecated and unmaintained.fixWhile this error highlights the underlying problem, the ultimate solution for `throttled-request` is to migrate away from this package entirely due to its dependency on deprecated software. Consider modern HTTP clients and throttling solutions.
Warnings
- breaking The underlying `request` library, which `throttled-request` wraps, has been officially deprecated and is no longer maintained. This poses significant security risks (unpatched vulnerabilities) and stability concerns for any application using this package, as it will not receive updates or bug fixes.
- gotcha This package does not expose the convenient HTTP verb shortcut methods (e.g., `.get()`, `.post()`, `.put()`) that the original `request` library provided. All requests must be made via the primary `throttledRequest(options, callback)` function or as a stream.
- gotcha The package version 0.1.1 indicates an early, potentially unstable, and unmaintained state. It has not seen updates in a long time, suggesting potential compatibility issues with newer Node.js versions, unaddressed bugs, or missing modern features.
Install
-
npm install throttled-request -
yarn add throttled-request -
pnpm add throttled-request
Imports
- throttledRequest
import throttledRequest from 'throttled-request';
var request = require('request'); var throttledRequest = require('throttled-request')(request); - throttledRequest.configure
throttledRequest.configure(5, 1000);
throttledRequest.configure({ requests: 5, milliseconds: 1000 }); - throttledRequest.on('request')
throttledRequest.addListener('request', function () {});throttledRequest.on('request', function () { console.log('Making a request'); });
Quickstart
var request = require('request')
, throttledRequest = require('throttled-request')(request)
, startedAt = Date.now();
throttledRequest.configure({
requests: 2,
milliseconds: 1000
});
throttledRequest.on('request', function () {
console.log('Making a request. Elapsed time: %d ms', Date.now() - startedAt);
});
//Throttle 10 requests in parallel to 'https://www.google.com/'
// Replace 'https://www.google.com/' with a safe, public URL for testing.
// In a real application, handle errors and ensure proper resource cleanup.
for (var i = 0; i < 10; i++) {
throttledRequest('https://www.google.com/', function (error, response, body) {
if (error) {
console.error('Request failed:', error.message);
return;
}
console.log('Got response for request %d. Status: %d. Elapsed time: %d ms', i, response.statusCode, Date.now() - startedAt);
});
}