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

Compare commits

..

4 Commits

Author SHA1 Message Date
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
6 changed files with 63 additions and 30 deletions

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, 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({
@@ -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,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);
};

View File

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