mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-11-01 05:43:11 +08:00
Backup domains
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
printWidth: 120,
|
|
||||||
trailingComma: 'all',
|
trailingComma: 'all',
|
||||||
singleQuote: true,
|
singleQuote: true,
|
||||||
};
|
};
|
||||||
|
|||||||
57
lib/getHost.js
Normal file
57
lib/getHost.js
Normal 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
1
lib/index.d.ts
vendored
@@ -19,7 +19,6 @@ export interface UpdateAvailableResult {
|
|||||||
hash: string;
|
hash: string;
|
||||||
description: string;
|
description: string;
|
||||||
metaInfo: string;
|
metaInfo: string;
|
||||||
updateUrl: string;
|
|
||||||
pdiffUrl: string;
|
pdiffUrl: string;
|
||||||
diffUrl: string;
|
diffUrl: string;
|
||||||
}
|
}
|
||||||
|
|||||||
40
lib/index.js
40
lib/index.js
@@ -1,7 +1,7 @@
|
|||||||
|
import getHost, { tryBackupDomains } from './getHost';
|
||||||
import { NativeAppEventEmitter, NativeModules } from 'react-native';
|
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 downloadRootDir = Pushy.downloadRootDir;
|
||||||
export const packageVersion = Pushy.packageVersion;
|
export const packageVersion = Pushy.packageVersion;
|
||||||
@@ -28,7 +28,6 @@ There is available update:
|
|||||||
hash: 'hash',
|
hash: 'hash',
|
||||||
description: '添加聊天功能\n修复商城页面BUG',
|
description: '添加聊天功能\n修复商城页面BUG',
|
||||||
metaInfo: '{"silent":true}',
|
metaInfo: '{"silent":true}',
|
||||||
updateUrl: 'http://update-packages.reactnative.cn/hash',
|
|
||||||
pdiffUrl: 'http://update-packages.reactnative.cn/hash',
|
pdiffUrl: 'http://update-packages.reactnative.cn/hash',
|
||||||
diffUrl: '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();
|
assertRelease();
|
||||||
const resp = await fetch(`${host}/checkUpdate/${APPKEY}`, {
|
let resp;
|
||||||
method: 'POST',
|
try {
|
||||||
headers: {
|
resp = await fetch(`${getHost()}/checkUpdate/${APPKEY}`, {
|
||||||
Accept: 'application/json',
|
method: 'POST',
|
||||||
'Content-Type': 'application/json',
|
headers: {
|
||||||
},
|
Accept: 'application/json',
|
||||||
body: JSON.stringify({
|
'Content-Type': 'application/json',
|
||||||
packageVersion,
|
},
|
||||||
hash: currentVersion,
|
body: JSON.stringify({
|
||||||
buildTime,
|
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) {
|
if (resp.status !== 200) {
|
||||||
throw new Error((await resp.json()).message);
|
throw new Error((await resp.json()).message);
|
||||||
|
|||||||
Reference in New Issue
Block a user