{"name":"http2-wrapper","version":"0.8.0","description":"HTTP2 client, just with the familiar `https` API","main":"source","engines":{"node":">=8.16.0"},"scripts":{"test":"xo && nyc ava","coveralls":"nyc report --reporter=text-lcov | coveralls"},"keywords":["http2","https","http","request"],"repository":{"type":"git","url":"git+https://github.com/szmarczak/http2-wrapper.git"},"author":{"name":"Szymon Marczak"},"license":"MIT","bugs":{"url":"https://github.com/szmarczak/http2-wrapper/issues"},"homepage":"https://github.com/szmarczak/http2-wrapper#readme","dependencies":{"resolve-alpn":"^1.0.0"},"devDependencies":{"@sindresorhus/is":"^1.0.0","ava":"^2.2.0","benchmark":"^2.1.4","coveralls":"^3.0.5","delay":"^4.3.0","get-stream":"^5.1.0","nyc":"^14.1.1","p-event":"^4.1.0","pem":"^1.14.2","tempy":"^0.3.0","timekeeper":"^2.2.0","to-readable-stream":"^2.1.0","xo":"^0.24.0"},"ava":{"timeout":"2m","concurrency":"4","helpers":["test/helpers/*"]},"gitHead":"b3ed08d24b1aeb0b356ac0b331a5b3adbb749d92","_id":"http2-wrapper@0.8.0","_nodeVersion":"11.13.0","_npmVersion":"6.7.0","dist":{"integrity":"sha512-uB4tzeT2HMpoVijZc3qaEpbdCnRN2oRPW2bGsG6MYtCAmRnv2bfR3dow8fNDGiNFN9aJkV4vkEdI+M+SXTcDug==","shasum":"ca2b758edb53440efd9c78388475bf268441713b","tarball":"http://registry-npm.ywwl.com/http2-wrapper/download/http2-wrapper-0.8.0.tgz","fileCount":14,"unpackedSize":32377,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdKvU/CRA9TVsSAnZWagAAtB4P/AloZGUaF6NNA228L2HV\nMVwHpJA7/S6xha63MBmKRScbgNuKUAlN6imnTjp2yTFVIlTcrHdRq3ZPtzvV\nbXo5kMp65rsW8rnKpNuQX0z66A4/aaorJOPvc8Cz30uXFEp5inNj6M6mzbYg\no+NgFtyPX22wmcjpTXNp86/k550mjSKIWEMto6797qS97mPCq1M/z4WOBtAs\nFTVoZ1SGi5Yk3XRlPp4fKLf0oQITZ01CAC2lcX15Svft9nYJ0S1n9g7j9Z5x\nYXW/rRqsA0+gW7t/Nzn7ZK4365DsTPy0J+P8U+iWCOO8OMo/NkUeVLk9TlPh\noM22IPddpVSiYj7W2o4S0Y7ZzL3kDQuuhr0xQae+knWNyUVEAv/FCQBciqKU\nYsDf0p2z2+FSx9orvfva7+Ya5f/F/rWfY23Wgm+7+oVo+X1ORXyfc+CJ0SVW\nVTrm7p1+zFEh2nNmyowNEQgUO+y4JL6MWXxFYcIEwcJEgqJdwzM1Qo98q13Y\nJaRlA60UrOseB3OAAie3dVGFw6+cA2EXFAhh1vCqPXF6+XBRomCyfMlk9Lke\nMa7MiEZVYYzZZ9/FAbCc37/PQmv1/aLio/vRDCwa3ylGdpRb0GEkBfN65DBd\nxIsH9fWBSqS+KdUfS1mlxwdaesrOdXhpIQEEdS6VcUMhzM2lGPumlegH2OY0\nLSia\r\n=wu+C\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBgnv4NxuyAE1bgNI1IYH0GBs3weVykogcfH+iqaPuRxAiB52fb1hAebp3q5Fu0v910tfECjMxIkVI4A4M4RG2Wf7A=="}],"size":10188,"noattachment":false,"key":"/http2-wrapper/-/http2-wrapper-0.8.0.tgz"},"maintainers":[{"name":"szmarczak","email":"sz.marczak@gmail.com"}],"_npmUser":{"name":"szmarczak","email":"sz.marczak@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/http2-wrapper_0.8.0_1563096382975_0.8549122962500226"},"_hasShrinkwrap":false,"readme":"# http2-wrapper\n> HTTP/2 client, just with the familiar `https` API\n\n[![Node CI](https://github.com/szmarczak/http2-wrapper/workflows/Node%20CI/badge.svg)](https://github.com/szmarczak/http2-wrapper/actions)\n[![codecov](https://codecov.io/gh/szmarczak/http2-wrapper/branch/master/graph/badge.svg)](https://codecov.io/gh/szmarczak/http2-wrapper)\n[![npm](https://img.shields.io/npm/dm/http2-wrapper.svg)](https://www.npmjs.com/package/http2-wrapper)\n[![install size](https://packagephobia.now.sh/badge?p=http2-wrapper)](https://packagephobia.now.sh/result?p=http2-wrapper)\n\nThis package was created to support HTTP/2 without the need to rewrite your code.<br>\nI 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!\n\n**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.\n\n**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.\n\n## Installation\n\n> `$ npm install http2-wrapper`<br>\n> `$ yarn add http2-wrapper`\n\n## Usage\n\n```js\nconst http2 = require('http2-wrapper');\n\nconst options = {\n\thostname: 'nghttp2.org',\n\tprotocol: 'https:',\n\tpath: '/httpbin/post',\n\tmethod: 'POST',\n\theaders: {\n\t\t'content-length': 6\n\t}\n};\n\nconst request = http2.request(options, response => {\n\tconsole.log('statusCode:', response.statusCode);\n\tconsole.log('headers:', response.headers);\n\n\tconst body = [];\n\tresponse.on('data', chunk => {\n\t\tbody.push(chunk);\n\t});\n\tresponse.on('end', () => {\n\t\tconsole.log('body:', Buffer.concat(body).toString());\n\t});\n});\n\nrequest.on('error', console.error);\n\nrequest.write('123');\nrequest.end('456');\n\n// statusCode: 200\n// headers: [Object: null prototype] {\n//   ':status': 200,\n//   date: 'Fri, 27 Sep 2019 19:45:46 GMT',\n//   'content-type': 'application/json',\n//   'access-control-allow-origin': '*',\n//   'access-control-allow-credentials': 'true',\n//   'content-length': '239',\n//   'x-backend-header-rtt': '0.002516',\n//   'strict-transport-security': 'max-age=31536000',\n//   server: 'nghttpx',\n//   via: '1.1 nghttpx',\n//   'alt-svc': 'h3-23=\":4433\"; ma=3600',\n//   'x-frame-options': 'SAMEORIGIN',\n//   'x-xss-protection': '1; mode=block',\n//   'x-content-type-options': 'nosniff'\n// }\n// body: {\n//   \"args\": {},\n//   \"data\": \"123456\",\n//   \"files\": {},\n//   \"form\": {},\n//   \"headers\": {\n//     \"Content-Length\": \"6\",\n//     \"Host\": \"nghttp2.org\"\n//   },\n//   \"json\": 123456,\n//   \"origin\": \"xxx.xxx.xxx.xxx\",\n//   \"url\": \"https://nghttp2.org/httpbin/post\"\n// }\n```\n\n## API\n\n**Note:** The `session` option was renamed to `tlsSession` for better readability.\n\n**Note:** The `timeout` option applies to HTTP/2 streams only. In order to set session timeout, pass an Agent with custom `timeout` option set.\n\n### http2.auto(url, options, callback)\n\nPerforms [ALPN](https://nodejs.org/api/tls.html#tls_alpn_and_sni) negotiation.\nReturns a Promise giving proper `ClientRequest` instance (depending on the ALPN).\n\n**Note**: The `agent` option represents an object with `http`, `https` and `http2` properties.\n\n```js\nconst http2 = require('http2-wrapper');\n\nconst options = {\n\thostname: 'httpbin.org',\n\tprotocol: 'http:', // Try changing this to https:\n\tpath: '/post',\n\tmethod: 'POST',\n\theaders: {\n\t\t'content-length': 6\n\t}\n};\n\n(async () => {\n\ttry {\n\t\tconst request = await http2.auto(options, response => {\n\t\t\tconsole.log('statusCode:', response.statusCode);\n\t\t\tconsole.log('headers:', response.headers);\n\n\t\t\tconst body = [];\n\t\t\tresponse.on('data', chunk => body.push(chunk));\n\t\t\tresponse.on('end', () => {\n\t\t\t\tconsole.log('body:', Buffer.concat(body).toString());\n\t\t\t});\n\t\t});\n\n\t\trequest.on('error', console.error);\n\n\t\trequest.write('123');\n\t\trequest.end('456');\n\t} catch (error) {\n\t\tconsole.error(error);\n\t}\n})();\n\n// statusCode: 200\n// headers: { connection: 'close',\n//   server: 'gunicorn/19.9.0',\n//   date: 'Sat, 15 Dec 2018 18:19:32 GMT',\n//   'content-type': 'application/json',\n//   'content-length': '259',\n//   'access-control-allow-origin': '*',\n//   'access-control-allow-credentials': 'true',\n//   via: '1.1 vegur' }\n// body: {\n//   \"args\": {},\n//   \"data\": \"123456\",\n//   \"files\": {},\n//   \"form\": {},\n//   \"headers\": {\n//     \"Connection\": \"close\",\n//     \"Content-Length\": \"6\",\n//     \"Host\": \"httpbin.org\"\n//   },\n//   \"json\": 123456,\n//   \"origin\": \"xxx.xxx.xxx.xxx\",\n//   \"url\": \"http://httpbin.org/post\"\n// }\n```\n\n### http2.auto.protocolCache\n\nAn instance of [`quick-lru`](https://github.com/sindresorhus/quick-lru) used for ALPN cache.\n\nThere is a maximum of 100 entries. You can modify the limit through `protocolCache.maxSize` - note that the change will be visible globally.\n\n### http2.auto.createResolveProtocol(cache, queue, connect)\n\n#### cache\n\nType: `Map<string, string>`\n\nThis is the store where cached ALPN protocols are put into.\n\n#### queue\n\nType: `Map<string, Promise>`\n\nThis is the store that contains pending ALPN negotiation promises.\n\n#### connect\n\nType: `(options, callback) => TLSSocket | Promise<TLSSocket>`\n\nSee https://github.com/szmarczak/resolve-alpn#connect\n\n### http2.auto.resolveProtocol(options)\n\nReturns a `Promise<{alpnProtocol: string}>`.\n\n### http2.request(url, options, callback)\n\nSame as [`https.request`](https://nodejs.org/api/https.html#https_https_request_options_callback).\n\n##### options.h2session\n\nType: `Http2Session`<br>\n\nThe session used to make the actual request. If none provided, it will use `options.agent` to get one.\n\n### http2.get(url, options, callback)\n\nSame as [`https.get`](https://nodejs.org/api/https.html#https_https_get_options_callback).\n\n### new http2.ClientRequest(url, options, callback)\n\nSame as [`https.ClientRequest`](https://nodejs.org/api/https.html#https_class_https_clientrequest).\n\n### new http2.IncomingMessage(socket)\n\nSame as [`https.IncomingMessage`](https://nodejs.org/api/https.html#https_class_https_incomingmessage).\n\n### new http2.Agent(options)\n\n**Note:** this is **not** compatible with the classic `http.Agent`.\n\nUsage example:\n\n```js\nconst http2 = require('http2-wrapper');\n\nclass MyAgent extends http2.Agent {\n\tcreateConnection(origin, options) {\n\t\tconsole.log(`Connecting to ${http2.Agent.normalizeOrigin(origin)}`);\n\t\treturn http2.Agent.connect(origin, options);\n\t}\n}\n\nhttp2.get({\n\thostname: 'google.com',\n\tagent: new MyAgent()\n}, response => {\n\tresponse.on('data', chunk => console.log(`Received chunk of ${chunk.length} bytes`));\n});\n```\n\n#### options\n\nEach option is an `Agent` property and can be changed later.\n\n##### timeout\n\nType: `number`<br>\nDefault: `0`\n\nIf there's no activity after `timeout` milliseconds, the session will be closed. If `0`, no timeout is applied.\n\n##### maxSessions\n\nType: `number`<br>\nDefault: `Infinity`\n\nThe maximum amount of sessions in total.\n\n##### maxEmptySessions\n\nType: `number`<br>\nDefault: `10`\n\nThe maximum amount of empty sessions in total. An empty session is a session with no pending requests.\n\n##### maxCachedTlsSessions\n\nType: `number`<br>\nDefault: `100`\n\nThe maximum amount of cached TLS sessions.\n\n#### agent.protocol\n\nType: `string`<br>\nDefault: `https:`\n\n#### agent.settings\n\nType: `object`<br>\nDefault: `{enablePush: false}`\n\n[Settings](https://nodejs.org/api/http2.html#http2_settings_object) used by the current agent instance.\n\n#### agent.normalizeOptions([options](https://github.com/szmarczak/http2-wrapper/blob/master/source/agent.js))\n\nReturns a string representing normalized options.\n\n```js\nAgent.normalizeOptions({servername: 'example.com'});\n// => ':::::::::::::::::::::::::::::::::::::'\n```\n\n#### agent.getSession(origin, options)\n\n##### [origin](https://nodejs.org/api/http2.html#http2_http2_connect_authority_options_listener)\n\nType: `string` `URL` `object`\n\nOrigin used to create new session.\n\n##### [options](https://nodejs.org/api/http2.html#http2_http2_connect_authority_options_listener)\n\nType: `object`\n\nOptions used to create new session.\n\nReturns a Promise giving free `Http2Session`. If no free sessions are found, a new one is created.\n\nA session is considered free when pending streams count is less than max concurrent streams settings.\n\n#### agent.getSession([origin](#origin), [options](options-1), listener)\n\n##### listener\n\nType: `object`\n\n```\n{\n\treject: error => void,\n\tresolve: session => void\n}\n```\n\nIf the `listener` argument is present, the Promise will resolve immediately. It will use the `resolve` function to pass the session.\n\n#### agent.request([origin](#origin), [options](#options-1), [headers](https://nodejs.org/api/http2.html#http2_headers_object), [streamOptions](https://nodejs.org/api/http2.html#http2_clienthttp2session_request_headers_options))\n\nReturns a Promise giving `Http2Stream`.\n\n#### agent.createConnection([origin](#origin), [options](#options-1))\n\nReturns a new `TLSSocket`. It defaults to `Agent.connect(origin, options)`.\n\n#### agent.closeEmptySessions(count)\n\n##### count\n\nType: `number`\nDefault: `Number.POSITIVE_INFINITY`\n\nMakes an attempt to close empty sessions. Only sessions with 0 concurrent streams will be closed.\n\n#### agent.destroy(reason)\n\nDestroys **all** sessions.\n\n#### agent.emptySessionCount\n\nType: `number`\n\nA number of empty sessions.\n\n#### agent.pendingSessionCount\n\nType: `number`\n\nA number of pending sessions.\n\n#### agent.sessionCount\n\nType: `number`\n\nA number of all sessions held by the Agent.\n\n#### Event: 'session'\n\n```js\nagent.on('session', session => {\n\t// A new session has been created by the Agent.\n});\n```\n\n## Proxy support\n\nCurrently `http2-wrapper` provides support for these proxies:\n\n- `HttpOverHttp2`\n- `HttpsOverHttp2`\n- `Http2OverHttp2`\n- `Http2OverHttp`\n- `Http2OverHttps`\n\nAny of the above can be accessed via `http2wrapper.proxies`. Check out the [`examples/proxies`](examples/proxies) directory to learn more.\n\n**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:\n\n```js\nconst resolveAlpnProxy = new URL('https://username:password@localhost:8000');\nconst connect = async (options, callback) => new Promise((resolve, reject) => {\n\tconst host = `${options.host}:${options.port}`;\n\n\t(async () => {\n\t\ttry {\n\t\t\tconst request = await http2.auto(resolveAlpnProxy, {\n\t\t\t\tmethod: 'CONNECT',\n\t\t\t\theaders: {\n\t\t\t\t\thost\n\t\t\t\t},\n\t\t\t\tpath: host,\n\n\t\t\t\t// For demo purposes only!\n\t\t\t\trejectUnauthorized: false,\n\t\t\t});\n\n\t\t\trequest.end();\n\n\t\t\trequest.once('error', reject);\n\n\t\t\trequest.once('connect', (response, socket, head) => {\n\t\t\t\tif (head.length > 0) {\n\t\t\t\t\treject(new Error(`Unexpected data before CONNECT tunnel: ${head.length} bytes`));\n\n\t\t\t\t\tsocket.destroy();\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tconst tlsSocket = tls.connect({\n\t\t\t\t\t...options,\n\t\t\t\t\tsocket\n\t\t\t\t}, callback);\n\n\t\t\t\tresolve(tlsSocket);\n\t\t\t});\n\t\t} catch (error) {\n\t\t\treject(error);\n\t\t}\n\t})();\n});\n\n// This is required to prevent leaking real IP address on ALPN negotiation\nconst resolveProtocol = http2.auto.createResolveProtocol(new Map(), new Map(), connect);\n\nconst request = await http2.auto('https://httpbin.org/anything', {\n\tagent: {…},\n\tresolveProtocol\n}, response => {\n\t// Read the response here\n});\n\nrequest.end();\n```\n\nSee [`unknown-over-unknown.js`](examples/proxies/unknown-over-unknown.js) to learn more.\n\n## Mirroring another server\n\nSee [`examples/proxies/mirror.js`](examples/proxies/mirror.js) for an example.\n\n## [WebSockets over HTTP/2](https://tools.ietf.org/html/rfc8441)\n\nSee [`examples/ws`](examples/ws) for an example.\n\n## Push streams\n\nSee [`examples/push-stream`](examples/push-stream) for an example.\n\n## Related\n\n- [`got`](https://github.com/sindresorhus/got) - Simplified HTTP requests\n- [`http2-proxy`](https://github.com/nxtedition/node-http2-proxy) - A simple http/2 & http/1.1 spec compliant proxy helper for Node.\n\n## License\n\nMIT\n","publish_time":1563096383143,"_cnpm_publish_time":1563096383143,"dist-tags":{"latest":"2.2.4"}}