Home

Awesome

HTTP2 client

Greenkeeper badge

NPM Version Build Status Known Vulnerabilities

Drop-in replacement for Nodes http and https that transparently make http request to both http1 / http2 server. Currently, it's the only http2/https compatible API for clients.

Motivation

http2 in Node.JS works entirely differently, while in browsers the experience is the same. http2-client was created to enable http2 / http1.1 requests with the same interface as http1.1.

The reason is that many NPM modules cannot upgrade to use http2.0 as these are coupled into http1.1 interface. With http2-client it should be very straight forward.

Meaning you don't need to know which protocol the destination supports before making the request http2-client will chose the one that works.

If the Node.js version you are using is not supporting http2 http2-client will automatically fallback to http.

Features

Transparently supports all http protocol.

In case of http1.1

In case of http2.0

Usage - Same interface

request()

const {request} = require('http2-client');
const h1Target = 'http://www.example.com/';
const h2Target = 'https://www.example.com/';
const req1 = request(h1Target, (res)=>{
    console.log(`
Url : ${h1Target}
Status : ${res.statusCode}
HttpVersion : ${res.httpVersion}
    `);
});
req1.end();

const req2 = request(h2Target, (res)=>{
    console.log(`
Url : ${h2Target}
Status : ${res.statusCode}
HttpVersion : ${res.httpVersion}
    `);
});
req2.end();

get()

const {get} = require('http2-client');
const h1Target = 'http://www.example.com/';
const h2Target = 'https://www.example.com/';
get(h1Target, (res)=>{
    console.log(`
Url : ${h1Target}
Status : ${res.statusCode}
HttpVersion : ${res.httpVersion}
    `);
});

get(h2Target, (res)=>{
    console.log(`
Url : ${h2Target}
Status : ${res.statusCode}
HttpVersion : ${res.httpVersion}
    `);
});

API

The module mimics the nodejs http module interface of ClientRequest, get() and request(). Same API as regular http/s modules. Different options will be used depending on the destination this method will get.

HttpRequestManager

By default this module exports a default request method the will try to detect the currect protocol to use (http2/http1.1/https1.1). However, you can always create different request manager with your specfic defaults and seperated cache.

//Use the default
const {request} = require('http2-client');
//Make a request
const req = request(/*....*/);
req.end();

//Alternatively create a new request
const {HttpRequestManager} = require('http2-client');
const httpRequestManager = new HttpRequestManager();
//Make a request
const req = httpRequestManager.request(/*....*/);
req.end();

Http/1.1 - request(options[, callback]) | request(url [,options][, callback])

All http protocols - get(options[, callback]) | get(url [,options][, callback])

Https/1.1 - request(options[, callback]) | request(url [,options][, callback])

Https/2.0 - request(options[, callback]) | request(url [,options][, callback])

How?

http2-client implements 'Application-Layer Protocol Negotiation (ALPN)'. Which means it first creates TCP connection, after successful ALPN negotiation the supported protocol is known.

If the supported protocol is http2.0 http2-client will re-use the same connection. After the http2.0 connection won't be used for keepH2ConnectionFor which defaults to 100 ms, it will be automatically closed.

If the supported protocol is http1.x http2-client will only cache the identification result and not the actual socket for keepH1IdentificationCacheFor which defaults to 30000 ms. Any socket configuration is manged by the http agent. If none is defined the node globalAgent will be used.

License

MIT