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-js

v1.0.10

Published

Http constants for mime-types, headers, tracking-statuses, methods, content-encodings, csp, response codes and cache-control instructions

Downloads

2,495

Readme

Http constants for mime-types, headers, tracking-statuses, methods, content-encodings, csp, response codes and cache-control instructions

Current version 1.0.10

Provides standard and common non-standard http headers, mime-types, methods, content codings, response statuses, CSP directives and sources, cache-control instructions and tracking statuses.

All headers are capitalized, hence Access-Control-Request-Method

All mime-types are lowercase, hence video/mpeg

All content codings are lowercase, hence deflate

All methods are uppercase, hence POST

All response codes are integer numbers, hence 404

All Cache-Control instructions are lowercase, hence min-fresh

All CSP directives and sources are lowercase, hence unsafe-eval

Key names constructed from uppercase values, -, + and . substituted with _, hence

Headers.STRICT_TRANSPORT_SECURITY //Strict-Transport-Security
MimeTypes.Application.XOP_XML     //application/xop+xml
MimeTypes.Model.X3D_VRML          //model/x3d+vrml
ContentEncodings.X_GZIP           //x-gzip
MimeTypes.Image.VND_WAP_WBMP      //image/vnd.wap.wbmp
ResponseCodes.I_AM_A_TEAPOT       //418
CacheControls.MAX_AGE             //max-age
CSPS.UNSAFE_EVAL                  //unsafe-eval

Usage

Install local

npm install http-headers-js --save

Available at the moment

let { 
    Headers, 
    TrackingStatuses, 
    MimeTypes, 
    Methods,
    ContentEncodings,
    ResponseCodes,
    CacheControls,
    RespCodeIs,
    CSPD,
    CSPS
} = require('http-headers-js');
  • Headers - headers
  • TrackingStatuses - tracking statuses
  • Methods - http methods
  • ContentEncodings - http content coding registry
  • ResponseCodes - response codes
  • CacheControls - Cache-Control instructions
  • CSPD - CSP directives
  • CSPS - some CSP source values
  • MimeTypes - yes, mime types, fair and square
  • RespCodeIs - set of functions to get type of response code

Constants

Import and usage, as exemplified by express js middleware

let { Headers } = require('http-headers-js');
/* code */
isLoggedIn: function (req, res, next) {    
    let token = req.get(Headers.AUTHORIZATION);
    if (token == null) {
        return res.status(401).send("Not authorized");
    }
    OAuth.get(token) //some library to check token
    .then(function (user) {
        if (user == null) {
            return res.status(401).send("Not authorized");
        }            
        next();
    })
}

/*code*/
const { MimeTypes } = require('http-headers-js');

class ExtensionFilter {

    static getExtensionByContentType (contentType) {            
        switch (contentType) {
        case MimeTypes.Application.PDF:
            return "PDF";
        case MimeTypes.Vnd.MS.WORD_2007:
            return "DOCX";
        defaut:
            return null;
        }
    }
    
}

To use mime types first access type, then subtype:

MimeTypes.Application.SOAP_XML              //application/soap+xml
MimeTypes.Application.X.WWW_FORM_URLENCODED //application/x-www-form-urlencoded
MimeTypes.X.WWW_FORM_URLENCODED             //application/x-www-form-urlencoded
MimeTypes.Vnd.Od.TEXT                       //application/vnd.oasis.opendocument.text
MimeTypes.Vnd.MS.EXCEL                      //application/vnd.ms-excel
MimeTypes.Vnd.MS.EXCEL_2007                 //application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
MimeTypes.Pkcs.X_PKCS7_CERTREQRESP          //application/x-pkcs7-certreqresp

There are also two duplicates for mime types. One is MimeTypes.X and MimeType.Application.X, another is MimeTypes.Pkcs and MimeTypes.Application.Pkcs

CSPS contains two sets of values - common and with QTD suffix. QTD values are quoted:

CSPS.SELF     //string "self"
CSPS.SELF_QTD //string "'self'"

Functions

There are 5 functions to check response code:

  • RespCodeIs.Info
  • RespCodeIs.Success
  • RespCodeIs.Redirect
  • RespCodeIs.ClientErr
  • RespCodeIs.ServerErr

All functions take number argument, like

RespCodeIs.ServerErr(500) //return true
RespCodeIs.ServerErr(ResponseCodes.INTERNAL_SERVER_ERROR) //return true
RespCodeIs.ClientErr(ResponseCodes.USE_PROXY) //return false

Dependencies

No additional dependencies for production, mocha and chai for testing.

Test

npm test

Development

See bitbucket repository

Direct Contacts

Issues? Write to [email protected]

What's new

1.0.10

  • Added more testing

1.0.9

  • Added CSP directives and common sources

1.0.8

  • Added CacheControls
  • Support older engines

1.0.7

  • Added RespCodeIs
  • Headers and Methods sorted in src

1.0.6

  • Added ResponseCodes

1.0.5

  • Added Early-Data, Downlink, Save-Data, Referrer-Policy, Content-Security-Policy-Report-Only, Expect-CT, Public-Key-Pins-Report-Only, X-Download-Options, X-Permitted-Cross-Domain-Policies, Sec-WebSocket-..., Large-Allocation, Server-Timing, SourceMap, X-DNS-Prefetch-Control, X-Robots-Tag
  • Fixed VIEWPORT-WIDTH to VIEWPORT_WIDTH, 3GPP to _3GPP (_3GPP2), VND.RN_REALAUDIO to VND_RN_REALAUDIO, VND.WAVE to VND_WAVE, VND.MICROSOFT.ICON to VND_MICROSOFT_ICON, VND.WAP.WBMP to VND_WAP_WBMP
  • Autotest key names

1.0.3

  • Added Content-Version, Derived-From, Alternates, MIME-Version, Public, Title headers
  • Added ContentEncodings

1.0.2

  • Added Methods
  • Added Last-Event-ID, DPR, Content-DPR, Accept-CH, Accept-CH-Lifetime, Width, Viewport-Width, Keep-Alive headers