From a5647705975285fe61034dc5bfb5da000f4abe9e Mon Sep 17 00:00:00 2001 From: sunnylqm Date: Sat, 27 Jul 2024 21:59:25 +0800 Subject: [PATCH] v9.2.0 --- lib/endpoint.ts | 25 ++++++++++++++-------- lib/main.ts | 57 +++++++++++++++++++++++++++---------------------- lib/type.ts | 3 +++ lib/utils.ts | 23 ++++++++++++++++++++ package.json | 2 +- 5 files changed, 74 insertions(+), 36 deletions(-) diff --git a/lib/endpoint.ts b/lib/endpoint.ts index 98a14c9..98db211 100644 --- a/lib/endpoint.ts +++ b/lib/endpoint.ts @@ -1,13 +1,21 @@ import { logger } 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 Promise.race( + 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; } } diff --git a/lib/main.ts b/lib/main.ts index b7fa3b7..66cd813 100644 --- a/lib/main.ts +++ b/lib/main.ts @@ -16,7 +16,7 @@ import { UpdateAvailableResult, UpdateEventsListener, } from './type'; -import { assertRelease, logger } from './utils'; +import { assertRelease, logger, testUrls } from './utils'; export { setCustomEndpoints }; const { version: v, @@ -260,11 +260,12 @@ export async function downloadUpdate( } let succeeded = false; report({ type: 'downloading' }); - if (options.diffUrl) { + const diffUrl = (await testUrls(options.diffUrls)) || options.diffUrl; + if (diffUrl) { logger('downloading diff'); try { await PushyModule.downloadPatchFromPpk({ - updateUrl: options.diffUrl, + updateUrl: diffUrl, hash: options.hash, originHash: currentVersion, }); @@ -273,28 +274,34 @@ export async function downloadUpdate( logger(`diff error: ${e.message}, try pdiff`); } } - if (!succeeded && options.pdiffUrl) { - logger('downloading pdiff'); - try { - await PushyModule.downloadPatchFromPackage({ - updateUrl: options.pdiffUrl, - hash: options.hash, - }); - succeeded = true; - } catch (e) { - logger(`pdiff error: ${e.message}, try full patch`); + if (!succeeded) { + const pdiffUrl = (await testUrls(options.pdiffUrls)) || options.pdiffUrl; + if (pdiffUrl) { + logger('downloading pdiff'); + try { + await PushyModule.downloadPatchFromPackage({ + updateUrl: pdiffUrl, + hash: options.hash, + }); + succeeded = true; + } catch (e) { + logger(`pdiff error: ${e.message}, try full patch`); + } } } - if (!succeeded && options.updateUrl) { - logger('downloading full patch'); - try { - await PushyModule.downloadFullUpdate({ - updateUrl: options.updateUrl, - hash: options.hash, - }); - succeeded = true; - } catch (e) { - logger(`full patch error: ${e.message}`); + if (!succeeded) { + const updateUrl = (await testUrls(options.updateUrls)) || options.updateUrl; + if (updateUrl) { + logger('downloading full patch'); + try { + await PushyModule.downloadFullUpdate({ + updateUrl: updateUrl, + hash: options.hash, + }); + succeeded = true; + } catch (e) { + logger(`full patch error: ${e.message}`); + } } } progressHandler && progressHandler.remove(); @@ -322,12 +329,10 @@ function assertHash(hash: string) { return true; } -let applyingUpdate = false; export function switchVersion(hash: string) { assertRelease(); - if (assertHash(hash) && !applyingUpdate) { + if (assertHash(hash)) { logger('switchVersion: ' + hash); - applyingUpdate = true; PushyModule.reloadUpdate({ hash }); } } diff --git a/lib/type.ts b/lib/type.ts index 00ec197..d8c8ad0 100644 --- a/lib/type.ts +++ b/lib/type.ts @@ -19,8 +19,11 @@ export interface UpdateAvailableResult { description: string; metaInfo: string; pdiffUrl: string; + pdiffUrls?: string[]; diffUrl?: string; + diffUrls?: string[]; updateUrl?: string; + updateUrls?: string[]; } export type CheckResult = diff --git a/lib/utils.ts b/lib/utils.ts index 9b024de..f7a3c99 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -1,3 +1,5 @@ +import { Platform } from "react-native"; + export function logger(...args: any[]) { console.log('Pushy: ', ...args); } @@ -7,3 +9,24 @@ export function assertRelease() { throw new Error('react-native-update 只能在 RELEASE 版本中运行.'); } } + +const ping = + Platform.OS === 'web' + ? Promise.resolve + : async (url: string) => + Promise.race([ + fetch(url, { + method: 'HEAD', + }) + .then(({ status }) => (status === 200 ? url : null)) + .catch(() => 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); +}; diff --git a/package.json b/package.json index a85c035..ace53a8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-update", - "version": "9.1.6", + "version": "9.2.0", "description": "react-native hot update", "main": "lib/index.ts", "scripts": {