JSPM

  • Created
  • Published
  • Downloads 13887996
  • Score
    100M100P100Q222593F
  • License MIT

Use HTTP2 the same way like HTTP1

Package Exports

  • http2-wrapper

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (http2-wrapper) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

http2-wrapper

Use HTTP2 the same way like HTTP1

Build Status Coverage Status npm install size

This package was created for the purpose of supporting HTTP2 without rewriting your code.
I recommend adapting to the http2 module if possible, because it's much simpler to use and has many cool features.

Tip: http2-wrapper is very useful when you rely on other modules that use the HTTP1 API and you want to support HTTP2.

Usage

'use strict';
const http2 = require('http2-wrapper');

const options = {
    hostname: 'nghttp2.org',
    protocol: 'https:',
    path: '/httpbin/post',
    method: 'POST',
    headers: {
        'content-length': 6
    }
};

const req = http2.request(options, res => {
    console.log('statusCode:', res.statusCode);
    console.log('headers:', res.headers);

    const body = [];
    res.on('data', chunk => {
        body.push(chunk);
    });
    res.on('end', () => {
        console.log('body:', Buffer.concat(body).toString());
    });
});

req.on('error', e => console.error(e));

req.write('123');
req.end('456');

// statusCode: 200
// headers: { ':status': 200,
//   date: 'Sat, 11 Aug 2018 09:37:41 GMT',
//   'content-type': 'application/json',
//   'content-length': '264',
//   'access-control-allow-origin': '*',
//   'access-control-allow-credentials': 'true',
//   'x-backend-header-rtt': '0.002997',
//   'strict-transport-security': 'max-age=31536000',
//   server: 'nghttpx',
//   via: '1.1 nghttpx',
//   'x-frame-options': 'SAMEORIGIN',
//   'x-xss-protection': '1; mode=block',
//   'x-content-type-options': 'nosniff' }
// body: {
//   "args": {},
//   "data": "123456",
//   "files": {},
//   "form": {},
//   "headers": {
//     "Content-Length": "6",
//     "Host": "nghttp2.org:443",
//     "Via": "2 nghttpx"
//   },
//   "json": 123456,
//   "origin": "xxx.xxx.xxx.xxx",
//   "url": "https://nghttp2.org:443/httpbin/post"
// }

Differences between the HTTP1 API

This wrapper may work a bit different than the HTTP1 API:

  • the upgrade event is not supported (yet),
  • the HTTP2ClientRequest class has no setSocketKeepAlive function,
  • close events inform that the underlying request is no longer usable (instead of: the socket has been closed),
  • there's a session option (accepts ClientHttp2Session) instead of the agent option.

HTTP2IncomingMessage

  • Event: 'aborted'
  • Event: 'close'
  • message.destroy([error])
  • message.headers
  • message.httpVersion
  • message.rawHeaders
  • message.rawTrailers
  • message.setTimeout(msecs, callback)
  • message.socket
  • message.statusCode
  • message.statusMessage
  • message.trailers

HTTP2ClientRequest

  • Event: 'abort'
  • Event: 'connect'
  • Event: 'continue'
  • Event: 'information'
  • Event: 'response'
  • Event: 'socket'
  • Event: 'timeout'
  • Event: 'upgrade'
  • request.abort()
  • request.aborted
  • request.connection
  • request.end([data[, encoding]][, callback])
  • request.flushHeaders()
  • request.getHeader(name)
  • request.removeHeader(name)
  • request.setHeader(name, value)
  • request.setNoDelay([noDelay])
  • request.setSocketKeepAlive([enable][, initialDelay])
  • request.setTimeout(timeout[, callback])
  • request.socket
  • request.write(chunk[, encoding][, callback])

License

MIT