forked from google/santa-tracker-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransport.js
More file actions
88 lines (78 loc) · 2.46 KB
/
Copy pathtransport.js
File metadata and controls
88 lines (78 loc) · 2.46 KB
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
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const MAX_RETRIES = 3;
const TIMEOUT = 5000;
import {build} from '../lib/params.js';
/**
* Performs a cross-domain XHR to fetch JSON.
*
* @param {string} url
* @return {!Promise<!Object<string, *>>}
*/
function fetchJSON(url) {
const xhrPromise = new Promise((resolve) => xhrRequest(url, resolve));
return xhrPromise.then((xhr) => {
if (xhr instanceof Error) {
throw xhr; // we only pass resolve to xhrRequest; rethrow with Error
}
let out;
try {
out = JSON.parse(xhr.responseText);
} catch (e) {
console.warn('invalid JSON from API', url);
console.debug('raw response', this.responseText)
throw e;
}
if (!out || typeof out !== 'object') {
console.warn('non-object JSON return', out);
return {'status': out};
}
return out;
});
}
/**
* @param {string} url
* @param {function(!XMLHttpRequest)} resolve to call with loaded XHR
* @param {number=} requestNo request count, for retries
*/
function xhrRequest(url, resolve, requestNo = 0) {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.timeout = TIMEOUT; // IE needs this >open but <send
xhr.onload = () => resolve(xhr);
xhr.onerror = () => {
if (requestNo >= MAX_RETRIES) {
return resolve(new Error(`exceeded retry limit for: ${url}`));
}
++requestNo;
// Retry with exponential backoff (625ms, ~1.5s, ~4s, ~10s, ...).
const at = Math.pow(2.5, requestNo) * 250;
console.debug('xhr failure, retry', requestNo, 'delay', at, 'url', url);
self.setTimeout(() => xhrRequest(url, resolve, requestNo), at);
};
xhr.send(null);
}
/**
* Performs a cross-domain AJAX request to the Santa API.
*
* @param {string} url
* @param {?Object<string, (string|number)>=} data
* @return {!Promise<!Object<string, *>>}
*/
export function request(url, data = null) {
const query = build(data);
return fetchJSON(url + query);
}