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

Compare commits

..

1 Commits

Author SHA1 Message Date
sunnylqm
10ac7073a8 v8.4.0 2023-10-28 18:30:28 +08:00
7 changed files with 42 additions and 97 deletions

View File

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

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 onPushyEvents = noop;
export const onEvents = noop;

View File

@@ -16,7 +16,7 @@ import {
UpdateAvailableResult,
UpdateEventsListener,
} from './type';
import { assertRelease, logger, promiseAny, testUrls } from './utils';
import { assertRelease, logger } from './utils';
export { setCustomEndpoints };
const {
version: v,
@@ -73,7 +73,7 @@ if (!uuid) {
const noop = () => {};
let reporter: UpdateEventsListener = noop;
export function onPushyEvents(customReporter: UpdateEventsListener) {
export function onEvents(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 promiseAny(
resp = await Promise.race(
backupEndpoints.map((endpoint) =>
fetch(getCheckUrl(APPKEY, endpoint), fetchPayload),
),
@@ -255,12 +255,11 @@ export async function downloadUpdate(
}
let succeeded = false;
report({ type: 'downloading' });
const diffUrl = (await testUrls(options.diffUrls)) || options.diffUrl;
if (diffUrl) {
if (options.diffUrl) {
logger('downloading diff');
try {
await PushyModule.downloadPatchFromPpk({
updateUrl: diffUrl,
updateUrl: options.diffUrl,
hash: options.hash,
originHash: currentVersion,
});
@@ -269,34 +268,28 @@ export async function downloadUpdate(
logger(`diff error: ${e.message}, try pdiff`);
}
}
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.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 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}`);
}
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}`);
}
}
progressHandler && progressHandler.remove();

View File

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

View File

@@ -19,11 +19,8 @@ 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,22 +1,3 @@
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);
}
@@ -26,21 +7,3 @@ 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.5.2",
"version": "8.4.0",
"description": "react-native hot update",
"main": "lib/index.ts",
"scripts": {
@@ -36,6 +36,5 @@
"@types/react": "^18.2.33",
"react-native": "^0.72.6",
"typescript": "^5.2.2"
},
"packageManager": "yarn@1.22.21+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72"
}
}