2020-08-26 00:44:34 +08:00
|
|
|
let currentEndpoint = 'https://update.react-native.cn/api';
|
2020-07-28 23:15:42 +08:00
|
|
|
|
|
|
|
function ping(url, rejectImmediate) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.onreadystatechange = (e) => {
|
|
|
|
if (xhr.readyState !== 4) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (xhr.status === 200) {
|
|
|
|
resolve(url);
|
|
|
|
} else {
|
|
|
|
rejectImmediate ? reject() : setTimeout(reject, 5000);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.open('HEAD', url);
|
|
|
|
xhr.send();
|
|
|
|
xhr.timeout = 5000;
|
|
|
|
xhr.ontimeout = reject;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function logger(...args) {
|
|
|
|
// console.warn('pushy', ...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
let backupEndpoints = [];
|
|
|
|
let backupEndpointsQueryUrl =
|
|
|
|
'https://cdn.jsdelivr.net/gh/reactnativecn/react-native-pushy@master/endpoints.json';
|
|
|
|
|
|
|
|
export async function tryBackupEndpoints() {
|
2020-07-28 23:34:06 +08:00
|
|
|
if (!backupEndpoints.length && !backupEndpointsQueryUrl) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-28 23:15:42 +08:00
|
|
|
try {
|
|
|
|
await ping(getStatusUrl(), true);
|
|
|
|
logger('current endpoint ok');
|
|
|
|
return;
|
|
|
|
} catch (e) {
|
|
|
|
logger('current endpoint failed');
|
|
|
|
}
|
|
|
|
if (!backupEndpoints.length && backupEndpointsQueryUrl) {
|
|
|
|
try {
|
|
|
|
const resp = await fetch(backupEndpointsQueryUrl);
|
|
|
|
backupEndpoints = await resp.json();
|
|
|
|
logger('get remote endpoints:', backupEndpoints);
|
|
|
|
} catch (e) {
|
|
|
|
logger('get remote endpoints failed');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await pickFatestAvailableEndpoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function pickFatestAvailableEndpoint(endpoints = backupEndpoints) {
|
|
|
|
const fastestEndpoint = await Promise.race(
|
|
|
|
endpoints.map(pingAndReturnEndpoint),
|
|
|
|
);
|
|
|
|
if (typeof fastestEndpoint === 'string') {
|
|
|
|
logger(`pick endpoint: ${fastestEndpoint}`);
|
|
|
|
currentEndpoint = fastestEndpoint;
|
|
|
|
} else {
|
|
|
|
logger('all remote endpoints failed');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function pingAndReturnEndpoint(endpoint = currentEndpoint) {
|
|
|
|
return ping(getStatusUrl(endpoint)).then(() => endpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getStatusUrl(endpoint = currentEndpoint) {
|
|
|
|
return `${endpoint}/status`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getCheckUrl(APPKEY, endpoint = currentEndpoint) {
|
|
|
|
return `${endpoint}/checkUpdate/${APPKEY}`;
|
|
|
|
}
|
|
|
|
|
2021-10-21 09:29:37 +08:00
|
|
|
export function getReportUrl(endpoint = currentEndpoint) {
|
|
|
|
return `${endpoint}/report`;
|
|
|
|
}
|
|
|
|
|
2020-07-28 23:34:06 +08:00
|
|
|
export function setCustomEndpoints({ main, backups, backupQueryUrl }) {
|
|
|
|
currentEndpoint = main;
|
|
|
|
backupEndpointsQueryUrl = null;
|
2020-07-28 23:15:42 +08:00
|
|
|
if (Array.isArray(backups) && backups.length > 0) {
|
|
|
|
backupEndpoints = backups;
|
|
|
|
pickFatestAvailableEndpoint();
|
|
|
|
}
|
2020-07-28 23:34:06 +08:00
|
|
|
if (typeof backupQueryUrl === 'string') {
|
|
|
|
backupEndpointsQueryUrl = backupQueryUrl;
|
|
|
|
}
|
2020-07-28 23:15:42 +08:00
|
|
|
}
|