mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-09-18 02:16:11 +08:00
Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
baebeff7cb | ||
![]() |
c7b78f0d46 | ||
![]() |
21f2c3918e |
@@ -1,13 +1,21 @@
|
||||
import { logger } from './utils';
|
||||
import { logger, promiseAny } from './utils';
|
||||
|
||||
let currentEndpoint = 'https://update.react-native.cn/api';
|
||||
let backupEndpoints: string[] = ['https://update.reactnative.cn/api'];
|
||||
let backupEndpointsQueryUrl: string | null = null;
|
||||
let backupEndpoints: string[] = [
|
||||
'https://pushy-koa-qgbgqmcpis.cn-beijing.fcapp.run',
|
||||
'https://update.reactnative.cn/api',
|
||||
];
|
||||
let backupEndpointsQueryUrls = [
|
||||
'https://gitee.com/sunnylqm/react-native-pushy/raw/master/endpoints.json',
|
||||
'https://cdn.jsdelivr.net/gh/reactnativecn/react-native-pushy@master/endpoints.json',
|
||||
];
|
||||
|
||||
export async function updateBackupEndpoints() {
|
||||
if (backupEndpointsQueryUrl) {
|
||||
if (backupEndpointsQueryUrls) {
|
||||
try {
|
||||
const resp = await fetch(backupEndpointsQueryUrl);
|
||||
const resp = await promiseAny(
|
||||
backupEndpointsQueryUrls.map((queryUrl) => fetch(queryUrl)),
|
||||
);
|
||||
const remoteEndpoints = await resp.json();
|
||||
if (Array.isArray(remoteEndpoints)) {
|
||||
backupEndpoints = Array.from(
|
||||
@@ -36,18 +44,17 @@ export function getCheckUrl(APPKEY, endpoint = currentEndpoint) {
|
||||
export function setCustomEndpoints({
|
||||
main,
|
||||
backups,
|
||||
backupQueryUrl,
|
||||
backupQueryUrls,
|
||||
}: {
|
||||
main: string;
|
||||
backups?: string[];
|
||||
backupQueryUrl?: string;
|
||||
backupQueryUrls?: string[];
|
||||
}) {
|
||||
currentEndpoint = main;
|
||||
backupEndpointsQueryUrl = null;
|
||||
if (Array.isArray(backups) && backups.length > 0) {
|
||||
backupEndpoints = backups;
|
||||
}
|
||||
if (typeof backupQueryUrl === 'string') {
|
||||
backupEndpointsQueryUrl = backupQueryUrl;
|
||||
if (Array.isArray(backupQueryUrls) && backupQueryUrls.length > 0) {
|
||||
backupEndpointsQueryUrls = backupQueryUrls;
|
||||
}
|
||||
}
|
||||
|
@@ -16,7 +16,7 @@ import {
|
||||
UpdateAvailableResult,
|
||||
UpdateEventsListener,
|
||||
} from './type';
|
||||
import { assertRelease, logger, testUrls } from './utils';
|
||||
import { assertRelease, logger, promiseAny, testUrls } from './utils';
|
||||
export { setCustomEndpoints };
|
||||
const {
|
||||
version: v,
|
||||
@@ -162,7 +162,7 @@ export async function checkUpdate(APPKEY: string) {
|
||||
const backupEndpoints = await updateBackupEndpoints();
|
||||
if (backupEndpoints) {
|
||||
try {
|
||||
resp = await Promise.race(
|
||||
resp = await promiseAny(
|
||||
backupEndpoints.map((endpoint) =>
|
||||
fetch(getCheckUrl(APPKEY, endpoint), fetchPayload),
|
||||
),
|
||||
|
26
lib/utils.ts
26
lib/utils.ts
@@ -1,4 +1,21 @@
|
||||
import { Platform } from "react-native";
|
||||
import { Platform } from 'react-native';
|
||||
|
||||
export function promiseAny<T>(promises: Promise<T>[]) {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
let count = 0;
|
||||
|
||||
promises.forEach(promise => {
|
||||
Promise.resolve(promise)
|
||||
.then(resolve)
|
||||
.catch(() => {
|
||||
count++;
|
||||
if (count === promises.length) {
|
||||
reject(new Error('All promises were rejected'));
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function logger(...args: any[]) {
|
||||
console.log('Pushy: ', ...args);
|
||||
@@ -17,16 +34,13 @@ const ping =
|
||||
Promise.race([
|
||||
fetch(url, {
|
||||
method: 'HEAD',
|
||||
})
|
||||
.then(({ status }) => (status === 200 ? url : null))
|
||||
.catch(() => null),
|
||||
}).then(({ status }) => (status === 200 ? url : null)),
|
||||
new Promise(r => setTimeout(() => r(null), 2000)),
|
||||
]);
|
||||
|
||||
|
||||
export const testUrls = async (urls?: string[]) => {
|
||||
if (!urls?.length) {
|
||||
return null;
|
||||
}
|
||||
return Promise.race(urls.map(ping)).catch(() => null);
|
||||
return promiseAny(urls.map(ping)).catch(() => null);
|
||||
};
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "react-native-update",
|
||||
"version": "8.5.1",
|
||||
"version": "8.5.2",
|
||||
"description": "react-native hot update",
|
||||
"main": "lib/index.ts",
|
||||
"scripts": {
|
||||
@@ -36,5 +36,6 @@
|
||||
"@types/react": "^18.2.33",
|
||||
"react-native": "^0.72.6",
|
||||
"typescript": "^5.2.2"
|
||||
}
|
||||
},
|
||||
"packageManager": "yarn@1.22.21+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72"
|
||||
}
|
||||
|
Reference in New Issue
Block a user