Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
172 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,88 @@ | ||
'use strict'; | ||
const http = require('http'); | ||
const https = require('https'); | ||
const resolveALPN = require('resolve-alpn'); | ||
const QuickLRU = require('quick-lru'); | ||
const Http2ClientRequest = require('./client-request'); | ||
const httpResolveALPN = require('./utils/http-resolve-alpn'); | ||
const calculateServerName = require('./utils/calculate-server-name'); | ||
const urlToOptions = require('./utils/url-to-options'); | ||
|
||
const cache = new QuickLRU({maxSize: 100}); | ||
|
||
const prepareRequest = async options => { | ||
const resolveProtocol = async options => { | ||
const name = `${options.host}:${options.port}:${options.ALPNProtocols.sort()}`; | ||
|
||
if (!cache.has(name)) { | ||
const result = (await resolveALPN(options)).alpnProtocol; | ||
cache.set(name, result); | ||
|
||
return result; | ||
} | ||
|
||
return cache.get(name); | ||
}; | ||
|
||
module.exports = async (input, options, callback) => { | ||
if (typeof input === 'string' || input instanceof URL) { | ||
input = urlToOptions(new URL(input)); | ||
} | ||
|
||
if (typeof options === 'function') { | ||
callback = options; | ||
} | ||
|
||
options = { | ||
ALPNProtocols: ['h2', 'http/1.1'], | ||
protocol: 'https:', | ||
...input, | ||
...options, | ||
resolveSocket: false | ||
}; | ||
|
||
options.host = options.hostname || options.host || 'localhost'; | ||
options.session = options.tlsSession; | ||
options.servername = options.servername || calculateServerName(options); | ||
|
||
if (options.protocol === 'https:') { | ||
const host = options.hostname || options.host || 'localhost'; | ||
const port = options.port || 443; | ||
options.port = options.port || 443; | ||
|
||
const ALPNProtocols = options.ALPNProtocols || ['h2', 'http/1.1']; | ||
const name = `${host}:${port}:${ALPNProtocols.sort()}`; | ||
const {path} = options; | ||
options.path = options.socketPath; | ||
|
||
let alpnProtocol = cache.get(name); | ||
const protocol = await resolveProtocol(options); | ||
|
||
if (typeof alpnProtocol === 'undefined') { | ||
alpnProtocol = (await httpResolveALPN(options)).alpnProtocol; | ||
cache.set(name, alpnProtocol); | ||
} | ||
options.path = path; | ||
|
||
if (alpnProtocol === 'h2') { | ||
return (options, callback) => { | ||
if (options.agent && options.agent.http2) { | ||
options = { | ||
...options, | ||
agent: options.agent.http2 | ||
}; | ||
} | ||
|
||
return Http2ClientRequest.request(options, callback); | ||
}; | ||
} | ||
if (protocol === 'h2') { | ||
if (options.agent && options.agent.http2) { | ||
options.agent = options.agent.http2; | ||
} | ||
|
||
return (options, callback) => { | ||
options = { | ||
...options, | ||
_defaultAgent: https.globalAgent, | ||
session: options.tlsSession | ||
}; | ||
return new Http2ClientRequest(options, callback); | ||
} | ||
|
||
if (protocol === 'http/1.1') { | ||
if (options.agent && options.agent.https) { | ||
options.agent = options.agent.https; | ||
} | ||
|
||
return http.request(options, callback); | ||
}; | ||
} | ||
|
||
return (options, callback) => { | ||
options = { | ||
...options, | ||
_defaultAgent: http.globalAgent | ||
}; | ||
options._defaultAgent = https.globalAgent; | ||
|
||
if (options.agent && options.agent.http) { | ||
options.agent = options.agent.http; | ||
return http.request(options, callback); | ||
} | ||
|
||
return http.request(options, callback); | ||
}; | ||
}; | ||
|
||
module.exports = async (input, options, callback) => { | ||
if (typeof input === 'string' || input instanceof URL) { | ||
input = urlToOptions(new URL(input)); | ||
throw new Error('Unknown ALPN protocol'); | ||
} | ||
|
||
options = { | ||
protocol: 'https:', | ||
...input, | ||
...options | ||
}; | ||
options.port = options.port || 80; | ||
|
||
if (options.agent && options.agent.http) { | ||
options.agent = options.agent.http; | ||
} | ||
|
||
const request = await prepareRequest(options); | ||
options._defaultAgent = http.globalAgent; | ||
|
||
return request(options, callback); | ||
return http.request(options, callback); | ||
}; | ||
|
||
module.exports.resolveALPN = httpResolveALPN; | ||
module.exports.prepareRequest = prepareRequest; | ||
module.exports.protocolCache = cache; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.