1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee

Backup domains

This commit is contained in:
sunnylqm 2020-01-18 23:55:10 +08:00
parent bdd97ac6ac
commit 1d0fb7d260
4 changed files with 81 additions and 18 deletions

View File

@ -1,5 +1,4 @@
module.exports = {
printWidth: 120,
trailingComma: 'all',
singleQuote: true,
};

57
lib/getHost.js Normal file
View File

@ -0,0 +1,57 @@
let availableDomain = 'update.reactnative.cn';
function ping(domain) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = e => {
if (xhr.readyState !== 4) {
return;
}
if (xhr.status === 200) {
resolve(domain);
} else {
setTimeout(reject, 5000);
}
};
xhr.open('HEAD', `https://${domain}`);
xhr.send();
xhr.timeout = 5000;
xhr.ontimeout = reject;
});
}
function logger(...args) {
// console.warn('pushy', ...args);
}
export async function tryBackupDomains() {
try {
await ping(availableDomain);
logger('main domain ok');
return;
} catch (e) {
logger('main domain failed');
}
let backupDomains = [];
try {
const resp = await fetch(
'https://cdn.jsdelivr.net/gh/reactnativecn/react-native-pushy@master/domains.json',
);
backupDomains = await resp.json();
logger('get remote domains:', backupDomains);
} catch (e) {
logger('get remote domains failed');
return;
}
const fastestDomain = await Promise.race(backupDomains.map(ping));
if (typeof fastestDomain === 'string') {
logger(`pick domain: ${fastestDomain}`);
availableDomain = fastestDomain;
} else {
logger('all remote domains failed');
}
}
export default function getHost() {
return `https://${availableDomain}/api`;
}

1
lib/index.d.ts vendored
View File

@ -19,7 +19,6 @@ export interface UpdateAvailableResult {
hash: string;
description: string;
metaInfo: string;
updateUrl: string;
pdiffUrl: string;
diffUrl: string;
}

View File

@ -1,7 +1,7 @@
import getHost, { tryBackupDomains } from './getHost';
import { NativeAppEventEmitter, NativeModules } from 'react-native';
const Pushy = NativeModules.Pushy || {};
const host = 'https://update.reactnative.cn/api';
const Pushy = NativeModules.Pushy || {};
export const downloadRootDir = Pushy.downloadRootDir;
export const packageVersion = Pushy.packageVersion;
@ -28,7 +28,6 @@ There is available update:
hash: 'hash',
description: '添加聊天功能\n修复商城页面BUG',
metaInfo: '{"silent":true}',
updateUrl: 'http://update-packages.reactnative.cn/hash',
pdiffUrl: 'http://update-packages.reactnative.cn/hash',
diffUrl: 'http://update-packages.reactnative.cn/hash',
}
@ -40,20 +39,29 @@ function assertRelease() {
}
}
export async function checkUpdate(APPKEY) {
export async function checkUpdate(APPKEY, isRetry) {
assertRelease();
const resp = await fetch(`${host}/checkUpdate/${APPKEY}`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
packageVersion,
hash: currentVersion,
buildTime,
}),
});
let resp;
try {
resp = await fetch(`${getHost()}/checkUpdate/${APPKEY}`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
packageVersion,
hash: currentVersion,
buildTime,
}),
});
} catch (e) {
if (isRetry) {
throw new Error('Could not connect to pushy server');
}
await tryBackupDomains();
return checkUpdate(APPKEY, true);
}
if (resp.status !== 200) {
throw new Error((await resp.json()).message);