httpstreambuffer

Module that buffers incoming HTTP requests' data, allowing parallel treatments

npm install httpstreambuffer
24 downloads in the last month

node-HttpStreamBuffer

Introduction

When building an HTTP-based application, our lovely node.js needs us to define as soon as possible callback for "data" and "end" events, refering to, respectively, the received HTTP request's body's chunk, and the end of this body.

However, this can lead to some issue. The fact is, like in almost all event-programming langages, if you don't define a gesture to a given event, then, when this event is fired, it will be ignored by your application. Thus, if you process some operations like authentication or whatever on your headers before defining your callbacks to "data" and "end" events, the data (that is the received HTTP request's body) will be received by your application, but ignored, as no callback are defined. Annoying isn't it ?

On the other hand, it's pretty ugly and borring to, first, receive your HTTP body, and then, go through all your functions with this data passed as argument. Moreover, your code will spend time to received the data first, and then, continue for your pre-operations. Also, it's stupid to receive a data, and then to authenticate an user and finally realize that this user doesn't have access to your services right ? Well.

This module

This module adds an object to the http.ServerRequest object received through the 'request' callback. This particular object, "streamBuffer", will stream the incoming HTTP body "in background", letting you process all your pre-operations.

As an example is sometimes better than explanation...

var HttpStreamBuffer = require("httpstreambuffer");
var http = require("http");

var server = http.createServer(function(req, res)
{
    // Adds the streamBuffer object to req
    new HttpStreamBuffer(req);

    // You can process all your pre-operation here
    // The incoming HTTP data are received through the streamBuffer obj
    // ...

    // This will keep buffering data if not done yet
    // or will call directly the callback as many times as the number of
    // chunks it has received
    // Total transparency !
    req.streamBuffer.onData(function(chunk)
    {
        console.log("Receiving data: " + chunk);
    });

    // Same as the method above
    req.streamBuffer.onEnd(function()
    {
        console.log("Request ended.");
    });
});
server.listen(8080, function()
{
    console.log("Listening on port 8080");
});

How to install

Feel free to clone this repo, or install it through the well-known npm

npm install httpstreambuffer

This repo and the package uploaded on NPM are normally always both up-to-date

npm loves you