forked from DevExpress/testcafe
/
index.js
181 lines (140 loc) · 5.35 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
169
170
171
172
173
174
175
176
177
178
179
180
181
// 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 COMMAND from '../../browser/connection/command';
import HeartbeatStatus from '../../browser/connection/heartbeat-status';
import { HEARTBEAT_INTERVAL } from '../../utils/browser-connection-timeouts';
import SERVICE_ROUTES from '../../browser/connection/service-routes';
/*eslint-disable no-restricted-properties*/
const LOCATION_HREF = document.location.href;
const LOCATION_ORIGIN = document.location.origin;
/*eslint-enable no-restricted-properties*/
const STATUS_RETRY_DELAY = 1000;
const MAX_STATUS_RETRY = 5;
const SERVICE_WORKER_LOCATION = LOCATION_ORIGIN + SERVICE_ROUTES.serviceWorker;
let allowInitScriptExecution = false;
let heartbeatIntervalId = null;
const evaluate = eval; // eslint-disable-line no-eval
//Utils
export function delay (ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// 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);
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) {
return LOCATION_HREF.toLowerCase() === url.toLowerCase();
}
//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: JSON.stringify(evaluate(res.code)) }); //eslint-disable-line no-restricted-globals
})
.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;
}
async function getStatus (statusUrl, createXHR, { manualRedirect } = {}) {
const result = await sendXHR(statusUrl, createXHR);
const redirecting = (result.cmd === COMMAND.run || result.cmd === COMMAND.idle) && !isCurrentLocation(result.url);
if (redirecting && !manualRedirect)
redirect(result);
return { command: result, redirecting };
}
async function tryGetStatus (...args) {
try {
return await getStatus(...args);
}
catch (error) {
// eslint-disable-next-line no-console
console.error(error);
return { error };
}
}
export async function checkStatus (...args) {
let error = null;
let result = null;
for (let i = 0; i < MAX_STATUS_RETRY; i++) {
({ error, ...result } = await tryGetStatus(...args));
if (!error)
return result;
await delay(STATUS_RETRY_DELAY);
}
throw error;
}
export async function enableRetryingTestPages () {
if (!navigator.serviceWorker)
return;
try {
await navigator.serviceWorker.register(SERVICE_WORKER_LOCATION, { scope: LOCATION_ORIGIN });
await navigator.serviceWorker.ready;
}
catch (error) {
// eslint-disable-next-line no-console
console.error(error);
}
}
export function getActiveWindowId (activeWindowIdUrl, createXHR) {
return sendXHR(activeWindowIdUrl, createXHR);
}
export function setActiveWindowId (activeWindowIdUrl, createXHR, windowId) {
return sendXHR(activeWindowIdUrl, createXHR, {
method: 'POST',
data: JSON.stringify({ windowId }), //eslint-disable-line no-restricted-globals
});
}
export function closeWindow (closeWindowUrl, createXHR, windowId) {
return sendXHR(closeWindowUrl, createXHR, {
method: 'POST',
data: JSON.stringify({ windowId }), //eslint-disable-line no-restricted-globals
});
}