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 🙏

© 2025 – Pkg Stats / Ryan Hefner

multithread-superagent

v1.0.10

Published

Support for multithreaded superagent.

Readme

multithread-superagent

A Node.js library for executing and managing concurrent HTTP requests with real-time feedback on progress and results.

Features

  • Real-time Progress Updates: Get immediate feedback for each request as it's completed.
  • Concurrent Request Handling: Control the maximum number of requests processed simultaneously using worker threads.
  • Customizable Requests: Supports GET, POST with JSON, form-encoded, and file uploads.
  • Error Handling: Gracefully handles request errors and provides detailed feedback.

How It Works

The multithread-superagent uses Node.js worker_threads to handle concurrent HTTP requests efficiently. Each worker processes a chunk of requests, sending individual results and progress updates back to the main thread.


Installation

# Clone this repository
git clone https://github.com/Apipost-Team/multithread-superagent.git

# Navigate into the project directory
cd multithread-superagent

# Install dependencies
npm install

Usage

Here is an example of using the multithread-superagent to perform multiple HTTP requests with real-time progress feedback.

Example

  1. Prepare request configurations: Define the URLs, methods, headers, and bodies for your requests.
  2. Initialize the multithread-superagent: Create an instance of the library and define the maximum number of concurrent workers.
  3. Listen for events: Use event listeners for progress updates (progress), individual results (result), and when all requests are completed (finished).
const RequestManager = require('./request-lib');

async function main() {
    const requestManager = new RequestManager();

    const requests = [
        {
            url: 'https://httpbin.org/post',
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: { message: 'Hello JSON' },
        },
        {
            url: 'https://httpbin.org/get',
            method: 'GET',
            headers: {},
        },
        {
            url: 'https://httpbin.org/post',
            method: 'POST',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            body: { key1: 'value1', key2: 'value2' },
        },
    ];

    const maxWorkers = 2;

    // Listen for individual request results
    requestManager.on('result', result => {
        console.log('Request completed:', result);
    });

    // Listen for progress updates
    requestManager.on('progress', ({ completed, total }) => {
        console.log(`Progress: ${completed}/${total}`);
    });

    // Listen for when all requests are finished
    requestManager.on('finished', ({ completed }) => {
        console.log(`All requests completed. Total: ${completed}`);
    });

    // Start sending requests
    try {
        await requestManager.sendRequests(requests, maxWorkers);
    } catch (err) {
        console.error('Error:', err);
    }
}

main();

Output

The above script will produce the following output:

Request completed: { success: true, statusCode: 200, duration: 123, body: {...} }
Progress: 1/3
Request completed: { success: true, statusCode: 200, duration: 456, body: {...} }
Progress: 2/3
Request completed: { success: true, statusCode: 200, duration: 312, body: {...} }
Progress: 3/3
All requests completed. Total: 3

API Documentation

Class: RequestManager

Manages and executes concurrent HTTP requests.

Constructor

new RequestManager(config)
  • config (Object):
    • maxWorkers (Number): Maximum number of concurrent worker threads (default: 5).

Method: sendRequests(requestConfigs, maxWorkers)

Executes a list of HTTP requests concurrently.

  • Parameters:
    • requestConfigs (Array): Array of request configuration objects. See the Request Configuration section below for details.
    • maxWorkers (Number): Maximum number of concurrent workers (default: value in config).
  • Returns:
    A promise that resolves when all requests are completed.

Supported Events

result

Emitted when an individual request is completed.

  • Data (Object):
    • success (Boolean): Whether the request was successful.
    • statusCode (Number): HTTP status code of the response (if successful).
    • duration (Number): Time taken for the request to complete (in milliseconds).
    • body (Object): Parsed body of the HTTP response, if applicable.
    • error (String): Error message, if the request failed.

progress

Emitted when a request is completed, providing the overall progress.

  • Data (Object):
    • completed (Number): Number of requests completed so far.
    • total (Number): Total number of requests.

finished

Emitted when all requests are completed.

  • Data (Object):
    • completed (Number): Total number of requests completed.

Request Configuration

Each request configuration object should include the following properties:

  • url (String): The URL to send the request to (required).
  • method (String): HTTP method (e.g., GET, POST). Default: GET.
  • headers (Object): Key-value pairs of request headers (optional).
  • body (Object|String): Request body, applicable for POST/PUT requests (optional).

Examples:

  1. GET Request:

    {
        url: 'https://httpbin.org/get',
        method: 'GET',
        headers: { Authorization: 'Bearer token' }
    }
  2. POST Request with JSON:

    {
        url: 'https://httpbin.org/post',
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: { key1: 'value1', key2: 'value2' }
    }
  3. POST Request with Form Data:

    {
        url: 'https://httpbin.org/post',
        method: 'POST',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        body: { key1: 'value1', key2: 'value2' }
    }

Dependencies

  • superagent: HTTP request library for handling requests and responses.
  • worker_threads: Native Node.js module for multithreading.

To install dependencies:

npm install

Contributing

Contributions, issues, and feature requests are welcome! Feel free to check the issues page.

  1. Fork the repository.
  2. Create your feature branch (git checkout -b feature/YourFeature).
  3. Commit your changes (git commit -am 'Add some feature').
  4. Push to the branch (git push origin feature/YourFeature).
  5. Open a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.


Acknowledgments

  • Inspired by real-world scenarios where real-time feedback and concurrency are critical.
  • Special thanks to OpenAI's ChatGPT for assistance with documentation and implementation guidance.