Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

http2-wrapper

Package Overview
Dependencies
Maintainers
1
Versions
59
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

http2-wrapper - npm Package Compare versions

Comparing version 0.3.2 to 0.4.0

source/auto.js

27

package.json
{
"name": "http2-wrapper",
"version": "0.3.2",
"description": "Use HTTP2 the same way like HTTP1",
"version": "0.4.0",
"description": "HTTP2 client, but with the HTTP1 API",
"main": "source",
"engines": {
"node": ">=10.8.0"
"node": ">=10.10.0"
},

@@ -29,23 +29,16 @@ "scripts": {

"homepage": "https://github.com/szmarczak/http2-wrapper#readme",
"xo": {
"rules": {
"unicorn/filename-case": "camelCase"
}
},
"devDependencies": {
"@sindresorhus/is": "^0.11.0",
"ava": "^0.25.0",
"@sindresorhus/is": "^0.14.0",
"ava": "^1.0.1",
"coveralls": "^3.0.2",
"delay": "^3.0.0",
"delay": "^4.1.0",
"get-stream": "^4.0.0",
"nyc": "^12.0.2",
"nyc": "^13.1.0",
"p-event": "^2.1.0",
"pem": "^1.12.5",
"resolve-alpn": "^1.0.0",
"tempy": "^0.2.1",
"to-readable-stream": "^1.0.0",
"xo": "^0.22.0"
},
"dependencies": {
"defer-to-connect": "^1.0.0",
"url-parse-lax": "^3.0.0"
"xo": "^0.23.0"
}
}
# http2-wrapper
> Use HTTP2 the same way like HTTP1
> HTTP2 client, but with the HTTP1 API

@@ -9,4 +9,4 @@ [![Build Status](https://travis-ci.org/szmarczak/http2-wrapper.svg?branch=master)](https://travis-ci.org/szmarczak/http2-wrapper)

This package was created for the purpose of supporting HTTP2 without rewriting your code.<br>
I recommend adapting to the `http2` module if possible, because it's much simpler to use and has many cool features.
This package was created for the purpose of supporting HTTP2 without the need to rewrite your code.<br>
I recommend adapting to the [`http2`](https://nodejs.org/api/http2.html) module if possible - it's much simpler to use and has many cool features!

@@ -78,51 +78,141 @@ **Tip**: `http2-wrapper` is very useful when you rely on other modules that use the HTTP1 API and you want to support HTTP2.

## Differences between the HTTP1 API
## API
This wrapper may work a bit different than the HTTP1 API:
### http2.auto(url, options)
- 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`](https://nodejs.org/api/http2.html#http2_http2_connect_authority_options_listener)) instead of the `agent` option.
### HTTP2IncomingMessage
**Note**: [`resolve-alpn`](https://github.com/szmarczak/resolve-alpn) package required.
* [x] Event: 'aborted'
* [x] Event: 'close'
* [x] message.destroy([error])
* [x] message.headers
* [x] message.httpVersion
* [x] message.rawHeaders
* [x] message.rawTrailers
* [x] message.setTimeout(msecs, callback)
* [x] message.socket
* [x] message.statusCode
* [x] message.statusMessage
* [x] message.trailers
Performs [ALPN](https://nodejs.org/api/tls.html#tls_alpn_and_sni) negotiation.
Returns a Promise giving [`HTTP2ClientRequest`](https://github.com/szmarczak/http2-wrapper#http2http2clientrequest) instance or [`ClientRequest`](https://nodejs.org/api/https.html#https_class_https_clientrequest) instance (depending on the ALPN).<br>
Usage example:
### HTTP2ClientRequest
* [x] Event: 'abort'
* [x] Event: 'connect'
* [x] Event: 'continue'
* [x] Event: 'information'
* [x] Event: 'response'
* [x] Event: 'socket'
* [x] Event: 'timeout'
* [ ] Event: 'upgrade'
* [x] request.abort()
* [x] request.aborted
* [x] request.connection
* [x] request.end([data[, encoding]][, callback])
* [x] request.flushHeaders()
* [x] request.getHeader(name)
* [x] request.removeHeader(name)
* [x] request.setHeader(name, value)
* [x] request.setNoDelay([noDelay])
* [ ] ~~request.setSocketKeepAlive([enable][, initialDelay])~~
* [x] request.setTimeout(timeout[, callback])
* [x] request.socket
* [x] request.write(chunk[, encoding][, callback])
```js
'use strict';
const http2 = require('http2-wrapper');
const options = {
hostname: 'httpbin.org',
protocol: 'http:', // Note the `http:` protocol here
path: '/post',
method: 'POST',
headers: {
'content-length': 6
}
};
(async () => {
try {
const req = await http2.auto(options);
req.on('response', 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', console.error);
req.write('123');
req.end('456');
} catch (error) {
console.error(error);
}
})();
// statusCode: 200
// headers: { connection: 'close',
// server: 'gunicorn/19.9.0',
// date: 'Sat, 15 Dec 2018 18:19:32 GMT',
// 'content-type': 'application/json',
// 'content-length': '259',
// 'access-control-allow-origin': '*',
// 'access-control-allow-credentials': 'true',
// via: '1.1 vegur' }
// body: {
// "args": {},
// "data": "123456",
// "files": {},
// "form": {},
// "headers": {
// "Connection": "close",
// "Content-Length": "6",
// "Host": "httpbin.org"
// },
// "json": 123456,
// "origin": "xxx.xxx.xxx.xxx",
// "url": "http://httpbin.org/post"
// }
```
#### url
Type: `string` `URL`
#### options
Type: `object`
#### callback(error, clientRequestInstance)
Type: `Function`
### http2.request(url, options, callback)
Same as [https.request](https://nodejs.org/api/https.html#https_https_request_options_callback).
**Note**: `session` accepts [HTTP2 sessions](https://nodejs.org/api/http2.html#http2_http2_connect_authority_options_listener). To pass TLS session, you need to use `socketSession` instead.
### http2.get(url, options, callback)
Same as [https.get](https://nodejs.org/api/https.html#https_https_get_options_callback).
### http2.HTTP2ClientRequest
Same as [https.ClientRequest](https://nodejs.org/api/https.html#https_class_https_clientrequest).
### http2.HTTP2IncomingMessage
Same as [https.IncomingMessage](https://nodejs.org/api/https.html#https_class_https_incomingmessage).
## Tips
### Reusing sessions
Due to the lack of HTTP2 session pools, you have to manage them by yourself. To reuse a session, you need to pass it through the `session` option:
```js
const http2 = require('http2-wrapper');
const session = http2.connect('https://google.com');
session.unref(); // The session will destroy automatically when the process exits
const options = {
hostname: 'google.com',
session
};
// Reuses the same session
const makeRequest = () => {
http2.request(options, res => {
res.on('data', chunk => {
console.log(`Received ${chunk.length} bytes of data`);
});
}).end();
};
makeRequest(); // Received 220 bytes of data
makeRequest(); // Received 220 bytes of data
```
## Notes
- [WebSockets over HTTP2 is not supported](https://github.com/nodejs/node/issues/15230).
- [HTTP2 sockets cannot be malformed](https://github.com/nodejs/node/blob/cc8250fab86486632fdeb63892be735d7628cd13/lib/internal/http2/core.js#L725).
- There's no `pool` (`agent` equivalent) option [yet](https://github.com/nodejs/node/issues/17746).
## License
MIT
'use strict';
const HTTP2ClientRequest = require('./HTTP2ClientRequest');
const http2 = require('http2');
const HTTP2ClientRequest = require('./client-request');
const HTTP2IncomingMessage = require('./incoming-message');
const auto = require('./auto');
const request = (url, options, cb) => {
return new HTTP2ClientRequest(url, options, cb);
};
const get = (url, options, cb) => {
const req = request(url, options, cb);
req.end();
return req;
};
module.exports = {
request: (options, cb) => {
return new HTTP2ClientRequest(options, cb);
}
...http2,
auto,
request,
get,
HTTP2ClientRequest,
HTTP2IncomingMessage
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc