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

Compare commits

..

7 Commits

Author SHA1 Message Date
sunnylqm
baebeff7cb v8.5.2 2024-11-13 19:58:12 +08:00
sunnylqm
c7b78f0d46 fix testurl 2024-11-13 19:57:44 +08:00
sunnylqm
21f2c3918e v8.5.1 2024-07-27 21:54:15 +08:00
sunnylqm
c3689e560c v8.5.0 2024-07-27 16:55:00 +08:00
sunnylqm
ab1e62dba5 test cdn urls 2024-07-26 15:37:32 +08:00
sunnylqm
fcfc8c3dbb add testurls 2024-07-26 15:28:13 +08:00
sunnylqm
41af560a39 v8.4.0 2023-10-28 18:30:51 +08:00
7 changed files with 97 additions and 42 deletions

View File

@@ -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;
}
}

View File

@@ -15,4 +15,4 @@ export const downloadAndInstallApk = noop;
export const setCustomEndpoints = noop;
export const getCurrentVersionInfo = noop;
export const simpleUpdate = (app) => app;
export const onEvents = noop;
export const onPushyEvents = noop;

View File

@@ -16,7 +16,7 @@ import {
UpdateAvailableResult,
UpdateEventsListener,
} from './type';
import { assertRelease, logger } from './utils';
import { assertRelease, logger, promiseAny, testUrls } from './utils';
export { setCustomEndpoints };
const {
version: v,
@@ -73,7 +73,7 @@ if (!uuid) {
const noop = () => {};
let reporter: UpdateEventsListener = noop;
export function onEvents(customReporter: UpdateEventsListener) {
export function onPushyEvents(customReporter: UpdateEventsListener) {
reporter = customReporter;
if (isRolledBack) {
report({
@@ -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),
),
@@ -255,11 +255,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,
});
@@ -268,28 +269,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();

View File

@@ -16,20 +16,20 @@ import {
switchVersionLater,
markSuccess,
downloadAndInstallApk,
onEvents,
onPushyEvents,
} from './main';
import { UpdateEventsListener } from './type';
export function simpleUpdate(
WrappedComponent: ComponentType,
options: { appKey?: string; onEvents?: UpdateEventsListener } = {},
options: { appKey?: string; onPushyEvents?: UpdateEventsListener } = {},
) {
const { appKey, onEvents: eventListeners } = options;
const { appKey, onPushyEvents: eventListeners } = options;
if (!appKey) {
throw new Error('appKey is required for simpleUpdate()');
}
if (typeof eventListeners === 'function') {
onEvents(eventListeners);
onPushyEvents(eventListeners);
}
return __DEV__
? WrappedComponent

View File

@@ -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 =

View File

@@ -1,3 +1,22 @@
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);
}
@@ -7,3 +26,21 @@ 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)),
new Promise(r => setTimeout(() => r(null), 2000)),
]);
export const testUrls = async (urls?: string[]) => {
if (!urls?.length) {
return null;
}
return promiseAny(urls.map(ping)).catch(() => null);
};

View File

@@ -1,6 +1,6 @@
{
"name": "react-native-update",
"version": "8.4.0",
"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"
}