1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-09-16 12:51:44 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee

Compare commits

...

1 Commits

Author SHA1 Message Date
sunnylqm
a564770597 v9.2.0 2024-07-27 21:59:25 +08:00
5 changed files with 74 additions and 36 deletions

View File

@@ -1,13 +1,21 @@
import { logger } from './utils'; import { logger } from './utils';
let currentEndpoint = 'https://update.react-native.cn/api'; let currentEndpoint = 'https://update.react-native.cn/api';
let backupEndpoints: string[] = ['https://update.reactnative.cn/api']; let backupEndpoints: string[] = [
let backupEndpointsQueryUrl: string | null = null; '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() { export async function updateBackupEndpoints() {
if (backupEndpointsQueryUrl) { if (backupEndpointsQueryUrls) {
try { try {
const resp = await fetch(backupEndpointsQueryUrl); const resp = await Promise.race(
backupEndpointsQueryUrls.map((queryUrl) => fetch(queryUrl)),
);
const remoteEndpoints = await resp.json(); const remoteEndpoints = await resp.json();
if (Array.isArray(remoteEndpoints)) { if (Array.isArray(remoteEndpoints)) {
backupEndpoints = Array.from( backupEndpoints = Array.from(
@@ -36,18 +44,17 @@ export function getCheckUrl(APPKEY, endpoint = currentEndpoint) {
export function setCustomEndpoints({ export function setCustomEndpoints({
main, main,
backups, backups,
backupQueryUrl, backupQueryUrls,
}: { }: {
main: string; main: string;
backups?: string[]; backups?: string[];
backupQueryUrl?: string; backupQueryUrls?: string[];
}) { }) {
currentEndpoint = main; currentEndpoint = main;
backupEndpointsQueryUrl = null;
if (Array.isArray(backups) && backups.length > 0) { if (Array.isArray(backups) && backups.length > 0) {
backupEndpoints = backups; backupEndpoints = backups;
} }
if (typeof backupQueryUrl === 'string') { if (Array.isArray(backupQueryUrls) && backupQueryUrls.length > 0) {
backupEndpointsQueryUrl = backupQueryUrl; backupEndpointsQueryUrls = backupQueryUrls;
} }
} }

View File

@@ -16,7 +16,7 @@ import {
UpdateAvailableResult, UpdateAvailableResult,
UpdateEventsListener, UpdateEventsListener,
} from './type'; } from './type';
import { assertRelease, logger } from './utils'; import { assertRelease, logger, testUrls } from './utils';
export { setCustomEndpoints }; export { setCustomEndpoints };
const { const {
version: v, version: v,
@@ -260,11 +260,12 @@ export async function downloadUpdate(
} }
let succeeded = false; let succeeded = false;
report({ type: 'downloading' }); report({ type: 'downloading' });
if (options.diffUrl) { const diffUrl = (await testUrls(options.diffUrls)) || options.diffUrl;
if (diffUrl) {
logger('downloading diff'); logger('downloading diff');
try { try {
await PushyModule.downloadPatchFromPpk({ await PushyModule.downloadPatchFromPpk({
updateUrl: options.diffUrl, updateUrl: diffUrl,
hash: options.hash, hash: options.hash,
originHash: currentVersion, originHash: currentVersion,
}); });
@@ -273,28 +274,34 @@ export async function downloadUpdate(
logger(`diff error: ${e.message}, try pdiff`); logger(`diff error: ${e.message}, try pdiff`);
} }
} }
if (!succeeded && options.pdiffUrl) { if (!succeeded) {
logger('downloading pdiff'); const pdiffUrl = (await testUrls(options.pdiffUrls)) || options.pdiffUrl;
try { if (pdiffUrl) {
await PushyModule.downloadPatchFromPackage({ logger('downloading pdiff');
updateUrl: options.pdiffUrl, try {
hash: options.hash, await PushyModule.downloadPatchFromPackage({
}); updateUrl: pdiffUrl,
succeeded = true; hash: options.hash,
} catch (e) { });
logger(`pdiff error: ${e.message}, try full patch`); succeeded = true;
} catch (e) {
logger(`pdiff error: ${e.message}, try full patch`);
}
} }
} }
if (!succeeded && options.updateUrl) { if (!succeeded) {
logger('downloading full patch'); const updateUrl = (await testUrls(options.updateUrls)) || options.updateUrl;
try { if (updateUrl) {
await PushyModule.downloadFullUpdate({ logger('downloading full patch');
updateUrl: options.updateUrl, try {
hash: options.hash, await PushyModule.downloadFullUpdate({
}); updateUrl: updateUrl,
succeeded = true; hash: options.hash,
} catch (e) { });
logger(`full patch error: ${e.message}`); succeeded = true;
} catch (e) {
logger(`full patch error: ${e.message}`);
}
} }
} }
progressHandler && progressHandler.remove(); progressHandler && progressHandler.remove();
@@ -322,12 +329,10 @@ function assertHash(hash: string) {
return true; return true;
} }
let applyingUpdate = false;
export function switchVersion(hash: string) { export function switchVersion(hash: string) {
assertRelease(); assertRelease();
if (assertHash(hash) && !applyingUpdate) { if (assertHash(hash)) {
logger('switchVersion: ' + hash); logger('switchVersion: ' + hash);
applyingUpdate = true;
PushyModule.reloadUpdate({ hash }); PushyModule.reloadUpdate({ hash });
} }
} }

View File

@@ -19,8 +19,11 @@ export interface UpdateAvailableResult {
description: string; description: string;
metaInfo: string; metaInfo: string;
pdiffUrl: string; pdiffUrl: string;
pdiffUrls?: string[];
diffUrl?: string; diffUrl?: string;
diffUrls?: string[];
updateUrl?: string; updateUrl?: string;
updateUrls?: string[];
} }
export type CheckResult = export type CheckResult =

View File

@@ -1,3 +1,5 @@
import { Platform } from "react-native";
export function logger(...args: any[]) { export function logger(...args: any[]) {
console.log('Pushy: ', ...args); console.log('Pushy: ', ...args);
} }
@@ -7,3 +9,24 @@ export function assertRelease() {
throw new Error('react-native-update 只能在 RELEASE 版本中运行.'); 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);
};

View File

@@ -1,6 +1,6 @@
{ {
"name": "react-native-update", "name": "react-native-update",
"version": "9.1.6", "version": "9.2.0",
"description": "react-native hot update", "description": "react-native hot update",
"main": "lib/index.ts", "main": "lib/index.ts",
"scripts": { "scripts": {