Add custom endpoints
This commit is contained in:
parent
d17eac48f0
commit
f110df1206
1
endpoints.json
Normal file
1
endpoints.json
Normal file
@ -0,0 +1 @@
|
||||
["https://update.react-native.cn/api", "https://update.reactnative.cn/api"]
|
82
lib/endpoint.js
Normal file
82
lib/endpoint.js
Normal file
@ -0,0 +1,82 @@
|
||||
let currentEndpoint = 'https://update.reactnative.cn/api';
|
||||
|
||||
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() {
|
||||
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}`;
|
||||
}
|
||||
|
||||
export function setCustomEndpoints(mainEndpoint, backups) {
|
||||
currentEndpoint = mainEndpoint;
|
||||
if (Array.isArray(backups) && backups.length > 0) {
|
||||
backupEndpoints = backups;
|
||||
pickFatestAvailableEndpoint();
|
||||
}
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
let availableDomain = 'update.react-native.cn';
|
||||
|
||||
function ping(domain, rejectImmediate) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.onreadystatechange = e => {
|
||||
if (xhr.readyState !== 4) {
|
||||
return;
|
||||
}
|
||||
if (xhr.status === 200) {
|
||||
resolve(domain);
|
||||
} else {
|
||||
rejectImmediate ? reject() : 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, true);
|
||||
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`;
|
||||
}
|
18
lib/index.d.ts
vendored
18
lib/index.d.ts
vendored
@ -20,17 +20,27 @@ export interface UpdateAvailableResult {
|
||||
description: string;
|
||||
metaInfo: string;
|
||||
pdiffUrl: string;
|
||||
diffUrl: string;
|
||||
}
|
||||
diffUrl?: string;
|
||||
};
|
||||
|
||||
export type CheckResult = Partial<ExpiredResult & UpTodateResult & UpdateAvailableResult>;
|
||||
export type CheckResult =
|
||||
| ExpiredResult
|
||||
| UpTodateResult
|
||||
| UpdateAvailableResult;
|
||||
|
||||
export function checkUpdate(appkey: string): Promise<CheckResult>;
|
||||
|
||||
export function downloadUpdate(options: UpdateAvailableResult): Promise<undefined | string>;
|
||||
export function downloadUpdate(
|
||||
options: UpdateAvailableResult,
|
||||
): Promise<undefined | string>;
|
||||
|
||||
export function switchVersion(hash: string): void;
|
||||
|
||||
export function switchVersionLater(hash: string): void;
|
||||
|
||||
export function markSuccess(): void;
|
||||
|
||||
export function setCustomEndpoints(
|
||||
mainEndpoint: string,
|
||||
backupEndpoints?: string[],
|
||||
): void;
|
||||
|
12
lib/index.js
12
lib/index.js
@ -1,5 +1,6 @@
|
||||
import getHost, { tryBackupDomains } from './getHost';
|
||||
import { tryBackupEndpoints, getCheckUrl, setCustomEndpoints } from './endpoint';
|
||||
import { NativeAppEventEmitter, NativeModules } from 'react-native';
|
||||
export { setCustomEndpoints };
|
||||
|
||||
let Pushy = NativeModules.Pushy;
|
||||
|
||||
@ -47,7 +48,7 @@ export async function checkUpdate(APPKEY, isRetry) {
|
||||
assertRelease();
|
||||
let resp;
|
||||
try {
|
||||
resp = await fetch(`${getHost()}/checkUpdate/${APPKEY}`, {
|
||||
resp = await fetch(getCheckUrl(APPKEY), {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
@ -63,7 +64,7 @@ export async function checkUpdate(APPKEY, isRetry) {
|
||||
if (isRetry) {
|
||||
throw new Error('Could not connect to pushy server');
|
||||
}
|
||||
await tryBackupDomains();
|
||||
await tryBackupEndpoints(APPKEY);
|
||||
return checkUpdate(APPKEY, true);
|
||||
}
|
||||
|
||||
@ -91,11 +92,6 @@ export async function downloadUpdate(options) {
|
||||
updateUrl: options.pdiffUrl,
|
||||
hashName: options.hash,
|
||||
});
|
||||
} else {
|
||||
await Pushy.downloadUpdate({
|
||||
updateUrl: options.updateUrl,
|
||||
hashName: options.hash,
|
||||
});
|
||||
}
|
||||
return options.hash;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user