ui: Lazily detect HTTP protocol (#7644)

This commit includes 2 things:

1. Sometimes (seemingly due to client caching), performance entries
aren't available, even for the currently executing script. This waits
until the first retrieval of 'CONSUL_HTTP_PROTOCOL' before using the
performance entries to decide this. This means that the entries aren't
inspected until ember has initialized, which means that the entries are
always available.

2. getCurrentResource/getResourceFor could potentially return undefined
if the correct entry could not be found. This adds a default {} return
value if the resource cannot be found. This means that if for whatever
reason the correct resource cannot be found at least we don't fail with
an error and just drop back to HTTP/1 functionality.
This commit is contained in:
John Cowen 2020-04-15 14:42:55 +01:00 committed by GitHub
parent 2746a1272d
commit 310d35e69c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 8 deletions

View File

@ -9,19 +9,20 @@ export default function(config = {}, win = window, doc = document) {
const item = win.localStorage.getItem(str);
return item === null ? undefined : item;
};
const getCurrentResource = function(scripts) {
const current = scripts[scripts.length - 1];
const getResourceFor = function(src) {
try {
return win.performance.getEntriesByType('resource').find(item => {
// current is based on the assumption that whereever this script is it's
// likely to be the same as the xmlhttprequests
return item.initiatorType === 'script' && current.src === item.name;
});
return (
win.performance.getEntriesByType('resource').find(item => {
return item.initiatorType === 'script' && src === item.name;
}) || {}
);
} catch (e) {
return {};
}
};
const resource = getCurrentResource(doc.getElementsByTagName('script'));
const scripts = doc.getElementsByTagName('script');
const currentSrc = scripts[scripts.length - 1].src;
let resource;
// TODO: Look to see if we can pull in HTTP headers here
// so we can let things be controlled via HTTP proxies, for example
@ -30,6 +31,16 @@ export default function(config = {}, win = window, doc = document) {
let protocol;
switch (str) {
case 'CONSUL_HTTP_PROTOCOL':
if (typeof resource === 'undefined') {
// resource needs to be retrieved lazily as entries aren't guaranteed
// to be available at script execution time (caching seems to affect this)
// waiting until we retrieve this value lazily at runtime means that
// the entries are always available as these values are only retrieved
// after initialization
// current is based on the assumption that whereever this script is it's
// likely to be the same as the xmlhttprequests
resource = getResourceFor(currentSrc);
}
return resource.nextHopProtocol || 'http/1.1';
case 'CONSUL_HTTP_MAX_CONNECTIONS':
protocol = env('CONSUL_HTTP_PROTOCOL');