This package was created to support HTTP/2 without the need to rewrite your code.
I recommend adapting to the http2 module if possible - 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 HTTP/1 API and you want to support HTTP/2.
Pro Tip: While the native http2 doesn't have agents yet, you can use http2-wrapper Agents and still operate on the native HTTP/2 streams.
Returns a new TLSSocket. It defaults to Agent.connect(origin, options).
agent.closeEmptySessions(count)
count
Type: number
Default: Number.POSITIVE_INFINITY
Makes an attempt to close empty sessions. Only sessions with 0 concurrent streams will be closed.
agent.destroy(reason)
Destroys all sessions.
agent.emptySessionCount
Type: number
A number of empty sessions.
agent.pendingSessionCount
Type: number
A number of pending sessions.
agent.sessionCount
Type: number
A number of all sessions held by the Agent.
Event: 'session'
agent.on('session', session => { // A new session has been created by the Agent.});
Proxy support
Currently http2-wrapper provides support for these proxies:
HttpOverHttp2
HttpsOverHttp2
Http2OverHttp2
Http2OverHttp
Http2OverHttps
Any of the above can be accessed via http2wrapper.proxies. Check out the examples/proxies directory to learn more.
Note: If you use the http2.auto function, the real IP address will leak. http2wrapper is not aware of the context. It will create a connection to the end server using your real IP address to get the ALPN protocol. Then it will create another connection using proxy. To migitate this, you need to pass a custom resolveProtocol function as an option:
const resolveAlpnProxy = new URL('https://username:password@localhost:8000');const connect = async (options, callback) => new Promise((resolve, reject) => { const host = `${options.host}:${options.port}`; (async () => { try { const request = await http2.auto(resolveAlpnProxy, { method: 'CONNECT', headers: { host }, path: host, // For demo purposes only! rejectUnauthorized: false, }); request.end(); request.once('error', reject); request.once('connect', (response, socket, head) => { if (head.length > 0) { reject(new Error(`Unexpected data before CONNECT tunnel: ${head.length} bytes`)); socket.destroy(); return; } const tlsSocket = tls.connect({ ...options, socket }, callback); resolve(tlsSocket); }); } catch (error) { reject(error); } })();});// This is required to prevent leaking real IP address on ALPN negotiationconst resolveProtocol = http2.auto.createResolveProtocol(new Map(), new Map(), connect);const request = await http2.auto('https://httpbin.org/anything', { agent: {…}, resolveProtocol}, response => { // Read the response here});request.end();