New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

rapid-responder

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rapid-responder

Provides a comprehensive utility for handling responses across multiple communication protocols (HTTP, IPC, and Socket).

latest
Source
npmnpm
Version
1.0.5
Version published
Maintainers
0
Created
Source

Adaptive Response Handler

A versatile JavaScript library designed for adaptive response handling across multiple communication protocols, including HTTP, IPC, and SOCKET. This library supports stream processing, MIME type detection, and advanced error handling to provide a robust solution for server-side or middleware applications.

Features

  • Protocol Support: Handles HTTP, IPC, and SOCKET responses seamlessly.
  • Stream Processing: Detects and processes various stream types with built-in destruction and timeout handling.
  • MIME Type Detection: Automatically identifies content types for responses.
  • Error Handling: Customizable error handling with fallback mechanisms.
  • Status Management: Built-in support for standard HTTP status codes.

Installation

To install this package, use:

npm install rapid-responder

Usage

Importing the Module

const { ResponseHandler, STATUS_CODES, PROTOCOLS, httpResponder, ipcResponder, socketResponder } = require('rapid-responder');

Example: HTTP Responder

// Send an HTTP 200 OK response
httpResponder.ok({ message: 'Success' }, { headers: { 'Content-Type': 'application/json' } });

// Send an HTTP 404 Not Found response
httpResponder.notFound({ error: 'Resource not found' });

Example: IPC Responder

// Send an IPC 201 Created response
ipcResponder.created({ id: 12345 });

// Send an IPC 500 Internal Server Error response
ipcResponder.internalServerError({ error: 'Unexpected error occurred' });

Example: SOCKET Responder

// Send a SOCKET 503 Service Unavailable response
socketResponder.serviceUnavailable({ message: 'Service is temporarily down' });

Creating a Response Handler

const handler = new ResponseHandler({
  protocol: PROTOCOLS.HTTP,
  headers: { 'Content-Type': 'application/json' },
  streamTimeout: 30000,
  maxStreamSize: 50 * 1024 * 1024,
});

Setting Status Code

handler.status(STATUS_CODES.ok);

Handling Responses

Non-Stream Response

const response = handler.send({ message: 'Hello, World!' });
console.log(response);

Stream Response

const fs = require('fs');
const readableStream = fs.createReadStream('./example.txt');

handler.send(readableStream).then(data => {
  console.log(data);
}).catch(error => {
  console.error(error);
});

API Reference

Classes

ResponseHandler

Constructor
new ResponseHandler(options)
  • options: Configuration object with the following properties:
    • protocol (string): Communication protocol (default: PROTOCOLS.HTTP).
    • headers (object): Response headers.
    • streamTimeout (number): Timeout for stream processing (default: 30000ms).
    • maxStreamSize (number): Maximum allowable size for streams (default: 50MB).
Methods
  • status(code)

    • Sets the HTTP status code for the response.
    • Returns the ResponseHandler instance for chaining.
  • send(body)

    • Processes the response body and returns a formatted object or Promise.

Contributing

Contributions are welcome! Please follow the guidelines:

  • Fork the repository.
  • Create a feature branch.
  • Submit a pull request with a detailed description of your changes.

License

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

Acknowledgments

  • Built with ❤️ for the JavaScript community.
  • Inspired by common challenges in handling adaptive responses.

Contact

For issues or inquiries, contact the maintainer at xuan.0211@gmail.com.

FAQs

Package last updated on 13 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts