-
Notifications
You must be signed in to change notification settings - Fork 660
/
index.js
168 lines (129 loc) · 5.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// TODO: once we'll have client commons load it from there instead of node modules (currently it's leads to two copies of this packages on client)
// TODO: Get rid of Pinkie after dropping IE11
import Promise from 'pinkie';
import COMMAND from '../../browser/connection/command';
import HeartbeatStatus from '../../browser/connection/heartbeat-status';
import { UNSTABLE_NETWORK_MODE_HEADER } from '../../browser/connection/unstable-network-mode';
import { HEARTBEAT_INTERVAL } from '../../utils/browser-connection-timeouts';
let allowInitScriptExecution = false;
let retryTestPages = false;
let heartbeatIntervalId = null;
const noop = () => void 0;
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const evaluate = eval; // eslint-disable-line no-eval
const FETCH_PAGE_TO_CACHE_RETRY_DELAY = 300;
const FETCH_PAGE_TO_CACHE_RETRY_COUNT = 5;
//Utils
// NOTE: the window.XMLHttpRequest may have been wrapped by Hammerhead, while we should send a request to
// the original URL. That's why we need the XMLHttpRequest argument to send the request via native methods.
export function sendXHR (url, createXHR, { method = 'GET', data = null, parseResponse = true } = {}) {
return new Promise((resolve, reject) => {
const xhr = createXHR();
xhr.open(method, url, true);
if (isRetryingTestPagesEnabled()) {
xhr.setRequestHeader(UNSTABLE_NETWORK_MODE_HEADER, 'true');
xhr.setRequestHeader('accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
}
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
let responseText = xhr.responseText || '';
if (responseText && parseResponse)
responseText = JSON.parse(xhr.responseText); //eslint-disable-line no-restricted-globals
resolve(responseText);
}
else
reject('disconnected');
}
};
xhr.send(data);
});
}
function isCurrentLocation (url) {
/*eslint-disable no-restricted-properties*/
return document.location.href.toLowerCase() === url.toLowerCase();
/*eslint-enable no-restricted-properties*/
}
//API
export function startHeartbeat (heartbeatUrl, createXHR) {
function heartbeat () {
sendXHR(heartbeatUrl, createXHR)
.then(status => {
if (status.code === HeartbeatStatus.closing && !isCurrentLocation(status.url)) {
stopInitScriptExecution();
document.location = status.url;
}
});
}
heartbeatIntervalId = window.setInterval(heartbeat, HEARTBEAT_INTERVAL);
heartbeat();
}
export function stopHeartbeat () {
window.clearInterval(heartbeatIntervalId);
}
function executeInitScript (initScriptUrl, createXHR) {
if (!allowInitScriptExecution)
return;
sendXHR(initScriptUrl, createXHR)
.then(res => {
if (!res.code)
return null;
return sendXHR(initScriptUrl, createXHR, { method: 'POST', data: evaluate(res.code) });
})
.then(() => {
window.setTimeout(() => executeInitScript(initScriptUrl, createXHR), 1000);
});
}
export function startInitScriptExecution (initScriptUrl, createXHR) {
allowInitScriptExecution = true;
executeInitScript(initScriptUrl, createXHR);
}
export function stopInitScriptExecution () {
allowInitScriptExecution = false;
}
export function redirect (command) {
stopInitScriptExecution();
document.location = command.url;
}
export function fetchPageToCache (pageUrl, createXHR) {
const requestAttempt = () => sendXHR(pageUrl, createXHR, { parseResponse: false });
const retryRequest = () => delay(FETCH_PAGE_TO_CACHE_RETRY_DELAY).then(requestAttempt);
let fetchPagePromise = requestAttempt();
for (let i = 0; i < FETCH_PAGE_TO_CACHE_RETRY_COUNT; i++)
fetchPagePromise = fetchPagePromise.catch(retryRequest);
return fetchPagePromise.catch(noop);
}
export function checkStatus (statusUrl, createXHR, opts) {
const { manualRedirect } = opts || {};
return sendXHR(statusUrl, createXHR)
.then(result => {
let ensurePagePromise = Promise.resolve();
if (result.url && isRetryingTestPagesEnabled())
ensurePagePromise = fetchPageToCache(result.url, createXHR);
return ensurePagePromise.then(() => result);
})
.then(result => {
const redirecting = (result.cmd === COMMAND.run || result.cmd === COMMAND.idle) && !isCurrentLocation(result.url);
if (redirecting && !manualRedirect)
redirect(result);
return { command: result, redirecting };
});
}
export function enableRetryingTestPages () {
retryTestPages = true;
}
export function disableRetryingTestPages () {
retryTestPages = false;
}
export function isRetryingTestPagesEnabled () {
return retryTestPages;
}
export function getActiveWindowId (activeWindowIdUrl, createXHR) {
return sendXHR(activeWindowIdUrl, createXHR);
}
export function setActiveWindowId (activeWindowIdUrl, createXHR, windowId) {
return sendXHR(activeWindowIdUrl, createXHR, {
method: 'POST',
data: { windowId }
});
}