Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: Lazily detect HTTP protocol #7644

Merged
merged 1 commit into from
Apr 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions ui-v2/app/utils/get-environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
Expand Down