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

Compare commits

..

4 Commits

Author SHA1 Message Date
sunnylqm
1614a21968 v9.2.2 2024-11-13 20:00:06 +08:00
sunnylqm
d7879ac2fe v9.2.1 2024-08-21 20:06:36 +08:00
sunnylqm
a564770597 v9.2.0 2024-07-27 21:59:25 +08:00
sunnylqm
7229f8847a chore: indent 2023-12-31 17:05:37 +08:00
7 changed files with 97 additions and 45 deletions

View File

@@ -57,7 +57,7 @@ public class UpdateModule extends NativeUpdateSpec {
@Override @Override
public void downloadFullUpdate(ReadableMap options, final Promise promise) { public void downloadFullUpdate(ReadableMap options, final Promise promise) {
UpdateModuleImpl.downloadFullUpdate(this.updateContext,options,promise); UpdateModuleImpl.downloadFullUpdate(this.updateContext,options,promise);
} }
@Override @Override

View File

@@ -349,8 +349,7 @@ RCT_EXPORT_METHOD(reloadUpdate:(NSDictionary *)options
} }
} }
RCT_EXPORT_METHOD(markSuccess: RCT_EXPORT_METHOD(markSuccess:(RCTPromiseResolveBlock)resolve
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) rejecter:(RCTPromiseRejectBlock)reject)
{ {

View File

@@ -1,13 +1,21 @@
import { logger } from './utils'; import { logger, promiseAny } 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 promiseAny(
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, promiseAny, testUrls } from './utils';
export { setCustomEndpoints }; export { setCustomEndpoints };
const { const {
version: v, version: v,
@@ -167,8 +167,8 @@ export async function checkUpdate(APPKEY: string) {
const backupEndpoints = await updateBackupEndpoints(); const backupEndpoints = await updateBackupEndpoints();
if (backupEndpoints) { if (backupEndpoints) {
try { try {
resp = await Promise.race( resp = await promiseAny(
backupEndpoints.map((endpoint) => backupEndpoints.map(endpoint =>
fetch(getCheckUrl(APPKEY, endpoint), fetchPayload), fetch(getCheckUrl(APPKEY, endpoint), fetchPayload),
), ),
); );
@@ -205,7 +205,7 @@ function checkOperation(
if (!Array.isArray(op)) { if (!Array.isArray(op)) {
return; return;
} }
op.forEach((action) => { op.forEach(action => {
if (action.type === 'block') { if (action.type === 'block') {
blockUpdate = { blockUpdate = {
reason: action.reason, reason: action.reason,
@@ -250,7 +250,7 @@ export async function downloadUpdate(
const downloadCallback = eventListeners.onDownloadProgress; const downloadCallback = eventListeners.onDownloadProgress;
progressHandler = eventEmitter.addListener( progressHandler = eventEmitter.addListener(
'RCTPushyDownloadProgress', 'RCTPushyDownloadProgress',
(progressData) => { progressData => {
if (progressData.hash === options.hash) { if (progressData.hash === options.hash) {
downloadCallback(progressData); downloadCallback(progressData);
} }
@@ -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,9 +1,46 @@
import { Platform } from 'react-native';
export function logger(...args: any[]) { export function logger(...args: any[]) {
console.log('Pushy: ', ...args); console.log('Pushy: ', ...args);
} }
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 assertRelease() { export function assertRelease() {
if (__DEV__) { if (__DEV__) {
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)),
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", "name": "react-native-update",
"version": "9.1.6", "version": "9.2.2",
"description": "react-native hot update", "description": "react-native hot update",
"main": "lib/index.ts", "main": "lib/index.ts",
"scripts": { "scripts": {
@@ -67,5 +67,6 @@
"react-native": "^0.72.6", "react-native": "^0.72.6",
"ts-jest": "^29.0.3", "ts-jest": "^29.0.3",
"typescript": "^5.2.2" "typescript": "^5.2.2"
} },
"packageManager": "yarn@1.22.21+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72"
} }