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

reshandler

v0.1.8

Published

A response handler that makes HTTP response processing simpler.

Downloads

3

Readme

reshandler

A response handler that makes HTTP response processing simpler. It's desinged to facilitate the work with modules like http and request that require a callback function for the response event.

By using chaining filters, reshandler helps you to handle things like chunks concatenation, gzip decompression and charset conversion in a beautiful way.

Example with http module

The example below gets and processes the HTTP response according to its content type. It tries to decompress the response data, convert the response data to UTF-8 if necessary, and outputs the processed response data at last.

var http = require('http');
var reshandler = require('reshandler');				
http.get("http://www.example.com/?q=123", 
	reshandler.new()								// create a new response handler
    .onType('text/html', reshandler.decompress())	// add a pre-defined filter for HTML
    .onType('text/html', reshandler.transcode())	// add another pre-defined filter for HTML
	.done(function(err, res, buffer){				// add the last callback
		console.log("The processed response: " + buffer.toString());
	})
).on('error', function(e) {
	console.log("Got error: " + e.message);
});

APIs

  • reshandler.new()

    Create a response handler. Same as new reshandler.Handler()

  • reshandler.Handler()

    The main Handler class.

  • Handler.onType(contentType, function (res, buffer, callback) {...})

    Register a pre-defined or custom filter function for the specified contentType. The filter function will only be called when contentType matches(is a substring of) 'Content-Type' header of the response. Multiple filter functions can be registered and will be called in the order they were registered.

    Please note that callback(function(err, res, buffer)) must be called in the end of each filter function for chaining filters to work. Unless you are sure that filter function is the last one to execute.

      //...
      .onType('image/png', function (res, buffer, callback) {	// the first filter
      	try{
      		console.log("Let's make some change to the buffer.");
      		callback(null, res, buffer);	// the result is passed to the next filter
      	}catch(err){
      		callback(err, res, buffer);		// the result/error is passed to the last callback
      	}
      })
      .onType('image/png', function (res, buffer, callback) {	// the next filter
      	console.log("This is the end.");	// the last callback won't be called
      })
      .done(function(err, res, buffer){		// the last callback
      	if(err) console.log("Got error: " + err.message);
      	else console.log("Got response: " + buffer.toString());
      })
      //...
  • Handler.done([function(err, res, buffer, contentType){...}])

    Build the callback for the response event. The only argument is a optional function. If provided, it will be registered as the last callback function to receive the processed response.

Pre-defined filters

  • reshandler.decompress()

    Build the callback function that automatically unzip or inflate response buffer.

  • reshandler.transcode()

    Build the callback function that automatically convert the charset of response buffer to UTF-8.