http2-wrapper
Advanced tools
Comparing version 0.5.2 to 0.6.0
{ | ||
"name": "http2-wrapper", | ||
"version": "0.5.2", | ||
"version": "0.6.0", | ||
"description": "HTTP2 client, just with the familiar `https` API", | ||
@@ -5,0 +5,0 @@ "main": "source", |
@@ -86,3 +86,3 @@ # http2-wrapper | ||
**Note**: the `session` option accepts an instance of [`Http2Session`](https://nodejs.org/api/http2.html#http2_class_http2session). To pass a SSL session, use `socketSession` instead. | ||
**Note:** the `session` option accepts an instance of [`Http2Session`](https://nodejs.org/api/http2.html#http2_class_http2session). To pass a SSL session, use `socketSession` instead. | ||
@@ -94,3 +94,3 @@ ### http2.auto(url, options, callback) | ||
**Note**: the `agent` option also accepts an object with `http`, `https` and `http2` properties. | ||
**Tip**: the `agent` option also accepts an object with `http`, `https` and `http2` properties. | ||
@@ -158,11 +158,10 @@ ```js | ||
### http2.auto.prepareRequest(url, options) | ||
### http2.auto.prepareRequest(options) | ||
Performs [ALPN](https://nodejs.org/api/tls.html#tls_alpn_and_sni) negotiation. | ||
Returns a Promise giving an object with `options` and `request`. | ||
Returns a Promise giving proper request function depending on the ALPN protocol. | ||
Depending on the ALPN protocol, `request` is either `http2.request` or `http.request`.<br> | ||
Options are normalized. | ||
**Note:** the request function takes only two arguments: `options` and `callback`. | ||
**Note**: the `agent` option also accepts an object with `http`, `https` and `http2` properties. | ||
**Tip:** the `agent` option also accepts an object with `http`, `https` and `http2` properties. | ||
@@ -203,3 +202,3 @@ ### http2.auto.resolveALPN(options) | ||
**Note**: this is **not** compatible with the classic `http.Agent`. | ||
**Note:** this is **not** compatible with the classic `http.Agent`. | ||
@@ -206,0 +205,0 @@ Usage example: |
@@ -10,13 +10,3 @@ 'use strict'; | ||
const prepareRequest = async (input, options) => { | ||
if (typeof input === 'string' || input instanceof URL) { | ||
input = urlToOptions(new URL(input)); | ||
} | ||
options = { | ||
protocol: 'https:', | ||
...input, | ||
...options | ||
}; | ||
const prepareRequest = async options => { | ||
if (options.protocol === 'https:') { | ||
@@ -42,27 +32,40 @@ const host = options.hostname || options.host || 'localhost'; | ||
if (alpnProtocol === 'h2') { | ||
if (options.agent && options.agent.http2) { | ||
options.agent = options.agent.http2; | ||
} | ||
return (options, callback) => { | ||
if (options.agent && options.agent.http2) { | ||
options = { | ||
...options, | ||
agent: options.agent.http2 | ||
}; | ||
} | ||
return { | ||
options, | ||
request: Http2ClientRequest.request | ||
return Http2ClientRequest.request(options, callback); | ||
}; | ||
} | ||
if (options.agent && options.agent.https) { | ||
options.agent = options.agent.https; | ||
} | ||
return (options, callback) => { | ||
options = { | ||
...options, | ||
_defaultAgent: https.globalAgent, | ||
session: options.socketSession | ||
}; | ||
options._defaultAgent = https.globalAgent; | ||
} else if (options.agent && options.agent.http) { | ||
options.agent = options.agent.http; | ||
options._defaultAgent = http.globalAgent; | ||
if (options.agent && options.agent.https) { | ||
options.agent = options.agent.https; | ||
} | ||
return http.request(options, callback); | ||
}; | ||
} | ||
options.session = options.socketSession; | ||
return (options, callback) => { | ||
options = { | ||
...options, | ||
_defaultAgent: http.globalAgent | ||
}; | ||
return { | ||
options, | ||
request: http.request | ||
if (options.agent && options.agent.http) { | ||
options.agent = options.agent.http; | ||
} | ||
return http.request(options, callback); | ||
}; | ||
@@ -72,5 +75,15 @@ }; | ||
module.exports = async (input, options, callback) => { | ||
const {options: preparedOptions, request} = await prepareRequest(input, options); | ||
if (typeof input === 'string' || input instanceof URL) { | ||
input = urlToOptions(new URL(input)); | ||
} | ||
return request(preparedOptions, callback); | ||
options = { | ||
protocol: 'https:', | ||
...input, | ||
...options | ||
}; | ||
const request = await prepareRequest(options); | ||
return request(options, callback); | ||
}; | ||
@@ -77,0 +90,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31856
723
326