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

http-headers

v3.0.2

Published

Parse http headers

Downloads

2,497,584

Readme

http-headers

Build status js-standard-style

Parse the start-line and headers from an HTTP request or reponse.

Converts:

HTTP/1.1 200 OK
Date: Tue, 10 Jun 2014 07:19:27 GMT
Connection: keep-alive
Transfer-Encoding: chunked

Hello World

To this:

{
  version: { major: 1, minor: 1 },
  statusCode: 200,
  statusMessage: 'OK',
  headers: {
    date: 'Tue, 10 Jun 2014 07:19:27 GMT',
    connection: 'keep-alive',
    'transfer-encoding': 'chunked'
  }
}

Features:

  • Auto-detects and ignores body if present
  • Fully RFC 2068 compliant (please open an issue if you find a discrepancy)
  • Support multi-line headers (lines will be joined with a space)
  • Support repeating headers

Installation

npm install http-headers --save

Usage

var net = require('net')
var httpHeaders = require('http-headers')

// create TCP server
net.createServer(function (c) {
  var buffers = []
  c.on('data', buffers.push.bind(buffers))
  c.on('end', function () {
    var data = Buffer.concat(buffers)

    // parse incoming data as an HTTP request and extra HTTP headers
    console.log(httpHeaders(data))
  })
}).listen(8080)

http.ServerReponse support

If given an instance of http.ServerResponse, the reponse headers is automatically extracted, parsed and returned:

var http = require('http')
var httpHeaders = require('http-headers')

http.createServer(function (req, res) {
  res.end('Hello World')
  console.log(httpHeaders(res))
}).listen(8080)

Why?

If you've ever needed to log or in another way access the headers sent to the client on a http.ServerResponse in Node.js, you know it's not as easy as with the http.IncomingMessage headers (which you just access via request.headers['content-type']).

Response headers are not directly available on the response object. Instead all headers are preprocessed as a string on the private response._header property and needs to be processed in order to be available as an object.

This module makes the task super simple.

API

The http-headers module exposes a single parser function:

httpHeaders(data[, onlyHeaders])

Arguments:

  • data - A string, buffer or instance of http.ServerReponse
  • onlyHeaders - An optional boolean. If true, only the headers object will be returned. Defaults to false

Request example

If given a request as input:

GET /foo HTTP/1.1
Date: Tue, 10 Jun 2014 07:19:27 GMT
Connection: keep-alive
Transfer-Encoding: chunked

Hello World

Returns:

{
  method: 'GET',
  url: '/foo',
  version: { major: 1, minor: 1 },
  headers: {
    date: 'Tue, 10 Jun 2014 07:19:27 GMT',
    connection: 'keep-alive',
    'transfer-encoding': 'chunked'
  }
}

Response example

If given a request as input:

HTTP/1.1 200 OK
Date: Tue, 10 Jun 2014 07:19:27 GMT
Connection: keep-alive
Transfer-Encoding: chunked

Hello World

Returns:

{
  version: { major: 1, minor: 1 },
  statusCode: 200,
  statusMessage: 'OK',
  headers: {
    date: 'Tue, 10 Jun 2014 07:19:27 GMT',
    connection: 'keep-alive',
    'transfer-encoding': 'chunked'
  }
}

onlyHeaders example

If the optional second argument is set to true, only headers are returned no matter the type of input:

{
  date: 'Tue, 10 Jun 2014 07:19:27 GMT',
  connection: 'keep-alive',
  'transfer-encoding': 'chunked'
}

No Start-Line

If the data given does not contain an HTTP Start-Line, only the headers are returned, even if the onlyHeaders argument is false:

Date: Tue, 10 Jun 2014 07:19:27 GMT
Connection: keep-alive
Transfer-Encoding: chunked

Hello World

Returns:

{
  date: 'Tue, 10 Jun 2014 07:19:27 GMT',
  connection: 'keep-alive',
  'transfer-encoding': 'chunked'
}

License

MIT