1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee
react-native-pushy/lib/endpoint.ts

104 lines
2.8 KiB
TypeScript
Raw Normal View History

2020-08-26 00:44:34 +08:00
let currentEndpoint = 'https://update.react-native.cn/api';
2020-07-28 23:15:42 +08:00
2023-08-31 19:24:00 +08:00
function ping(url: string, rejectImmediate?: boolean) {
2020-07-28 23:15:42 +08:00
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;
});
}
2023-08-31 19:24:00 +08:00
function logger(...args: any[]) {
console.log('Pushy: ', ...args);
2020-07-28 23:15:42 +08:00
}
2023-08-31 19:24:00 +08:00
let backupEndpoints: string[] = [];
let backupEndpointsQueryUrl: string | null =
2020-07-28 23:15:42 +08:00
'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);
2023-08-31 19:24:00 +08:00
logger('current endpoint ok', currentEndpoint);
2020-07-28 23:15:42 +08:00
return;
} catch (e) {
2023-08-31 19:24:00 +08:00
logger('current endpoint failed', currentEndpoint);
2020-07-28 23:15:42 +08:00
}
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}`;
}
2023-08-31 19:24:00 +08:00
/**
* @param {string} main - The main api endpoint
* @param {string[]} [backups] - The back up endpoints.
* @param {string} [backupQueryUrl] - An url that return a json file containing an array of endpoint.
* like: ["https://backup.api/1", "https://backup.api/2"]
*/
export function setCustomEndpoints({
main,
backups,
backupQueryUrl,
}: {
main: string;
backups?: string[];
backupQueryUrl?: string;
}) {
2020-07-28 23:34:06 +08:00
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
}