mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-09-18 01:06:11 +08:00
Compare commits
19 Commits
v9.0.0-bet
...
v9.1.2
Author | SHA1 | Date | |
---|---|---|---|
![]() |
23346a5f1d | ||
![]() |
5aca2104c2 | ||
![]() |
fe0a05db3d | ||
![]() |
2b287786ff | ||
![]() |
7d128900cd | ||
![]() |
189e3ec78e | ||
![]() |
821722165a | ||
![]() |
6cb53ac655 | ||
![]() |
7b9a24168a | ||
![]() |
c6354bbedc | ||
![]() |
b53878c291 | ||
![]() |
ab01312f8d | ||
![]() |
44784b6d3e | ||
![]() |
30c21fed91 | ||
![]() |
15af7802ad | ||
![]() |
8bed6ef979 | ||
![]() |
7d03dc24b7 | ||
![]() |
18849ee441 | ||
![]() |
3ba1df2020 |
@@ -10,7 +10,7 @@
|
||||
|
||||
1. 基于阿里云高速 CDN 分发,对比其他服务器在国外的热更新服务,分发更稳定,更新成功率极高。
|
||||
2. 基于 bsdiff/hdiff 算法创建的**超小更新包**,通常版本迭代后在几十 KB 级别(其他全量热更新服务所需流量通常在几十 MB 级别)。
|
||||
3. 始终跟进 RN 最新正式版本,第一时间提供支持。支持 hermes 字节码格式。(暂不支持新架构,会待其相对稳定后跟进)
|
||||
3. 始终跟进 RN 最新正式版本,第一时间提供支持。支持 hermes 字节码格式。支持新架构。
|
||||
4. 跨越多个版本进行更新时,只需要下载**一个更新包**,不需要逐版本依次更新。
|
||||
5. 命令行工具 & 网页双端管理,版本发布过程简单便捷,完全可以集成 CI。
|
||||
6. 支持崩溃回滚,安全可靠。
|
||||
|
1
android/proguard.pro
vendored
1
android/proguard.pro
vendored
@@ -1,2 +1,3 @@
|
||||
-keepnames class cn.reactnative.modules.update.DownloadTask { *; }
|
||||
-keepnames class cn.reactnative.modules.update.UpdateModuleImpl { *; }
|
||||
-keepnames class com.facebook.react.ReactInstanceManager { *; }
|
@@ -1,9 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="cn.reactnative.modules.update">
|
||||
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
|
||||
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
<application>
|
||||
<meta-data android:name="pushy_build_time" android:value="@string/pushy_build_time" />
|
||||
<provider
|
||||
|
@@ -53,7 +53,6 @@ typedef NS_ENUM(NSInteger, PushyType) {
|
||||
};
|
||||
|
||||
static BOOL ignoreRollback = false;
|
||||
static BOOL isUsingBundleUrl = false;
|
||||
|
||||
@implementation RCTPushy {
|
||||
RCTPushyManager *_fileManager;
|
||||
@@ -66,7 +65,6 @@ RCT_EXPORT_MODULE(RCTPushy);
|
||||
|
||||
+ (NSURL *)bundleURL
|
||||
{
|
||||
isUsingBundleUrl = true;
|
||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||
|
||||
NSDictionary *pushyInfo = [defaults dictionaryForKey:keyPushyInfo];
|
||||
@@ -163,7 +161,6 @@ RCT_EXPORT_MODULE(RCTPushy);
|
||||
ret[@"uuid"] = [defaults objectForKey:keyUuid];
|
||||
NSDictionary *pushyInfo = [defaults dictionaryForKey:keyPushyInfo];
|
||||
ret[@"currentVersion"] = [pushyInfo objectForKey:paramCurrentVersion];
|
||||
ret[@"isUsingBundleUrl"] = @(isUsingBundleUrl);
|
||||
|
||||
// clear isFirstTimemarked
|
||||
if (ret[@"isFirstTime"]) {
|
||||
|
@@ -1,75 +1,26 @@
|
||||
import { logger } from './utils';
|
||||
|
||||
let currentEndpoint = 'https://update.react-native.cn/api';
|
||||
let backupEndpoints: string[] = ['https://update.reactnative.cn/api'];
|
||||
let backupEndpointsQueryUrl: string | null = null;
|
||||
|
||||
function ping(url: string, rejectImmediate?: boolean) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.onreadystatechange = (e) => {
|
||||
if (xhr.readyState !== 4) {
|
||||
return;
|
||||
}
|
||||
if (xhr.status === 200) {
|
||||
resolve(url);
|
||||
} else {
|
||||
rejectImmediate ? reject() : setTimeout(reject, 5000);
|
||||
}
|
||||
};
|
||||
xhr.open('HEAD', url);
|
||||
xhr.send();
|
||||
xhr.timeout = 5000;
|
||||
xhr.ontimeout = reject;
|
||||
});
|
||||
}
|
||||
|
||||
function logger(...args: any[]) {
|
||||
console.log('Pushy: ', ...args);
|
||||
}
|
||||
|
||||
let backupEndpoints: string[] = [];
|
||||
let backupEndpointsQueryUrl: string | null =
|
||||
'https://cdn.jsdelivr.net/gh/reactnativecn/react-native-pushy@master/endpoints.json';
|
||||
|
||||
export async function tryBackupEndpoints() {
|
||||
if (!backupEndpoints.length && !backupEndpointsQueryUrl) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await ping(getStatusUrl(), true);
|
||||
logger('current endpoint ok', currentEndpoint);
|
||||
return;
|
||||
} catch (e) {
|
||||
logger('current endpoint failed', currentEndpoint);
|
||||
}
|
||||
if (!backupEndpoints.length && backupEndpointsQueryUrl) {
|
||||
export async function updateBackupEndpoints() {
|
||||
if (backupEndpointsQueryUrl) {
|
||||
try {
|
||||
const resp = await fetch(backupEndpointsQueryUrl);
|
||||
backupEndpoints = await resp.json();
|
||||
logger('get remote endpoints:', backupEndpoints);
|
||||
} catch (e) {
|
||||
logger('get remote endpoints failed');
|
||||
return;
|
||||
}
|
||||
}
|
||||
await pickFatestAvailableEndpoint();
|
||||
}
|
||||
|
||||
async function pickFatestAvailableEndpoint(endpoints = backupEndpoints) {
|
||||
const fastestEndpoint = await Promise.race(
|
||||
endpoints.map(pingAndReturnEndpoint),
|
||||
const remoteEndpoints = await resp.json();
|
||||
if (Array.isArray(remoteEndpoints)) {
|
||||
backupEndpoints = Array.from(
|
||||
new Set([...backupEndpoints, ...remoteEndpoints]),
|
||||
);
|
||||
if (typeof fastestEndpoint === 'string') {
|
||||
logger(`pick endpoint: ${fastestEndpoint}`);
|
||||
currentEndpoint = fastestEndpoint;
|
||||
} else {
|
||||
logger('all remote endpoints failed');
|
||||
logger('fetch remote endpoints:', remoteEndpoints);
|
||||
logger('merged backup endpoints:', backupEndpoints);
|
||||
}
|
||||
}
|
||||
|
||||
async function pingAndReturnEndpoint(endpoint = currentEndpoint) {
|
||||
return ping(getStatusUrl(endpoint)).then(() => endpoint);
|
||||
}
|
||||
|
||||
function getStatusUrl(endpoint = currentEndpoint) {
|
||||
return `${endpoint}/status`;
|
||||
} catch (e) {
|
||||
logger('fetch remote endpoints failed');
|
||||
}
|
||||
}
|
||||
return backupEndpoints;
|
||||
}
|
||||
|
||||
export function getCheckUrl(APPKEY, endpoint = currentEndpoint) {
|
||||
@@ -95,7 +46,6 @@ export function setCustomEndpoints({
|
||||
backupEndpointsQueryUrl = null;
|
||||
if (Array.isArray(backups) && backups.length > 0) {
|
||||
backupEndpoints = backups;
|
||||
pickFatestAvailableEndpoint();
|
||||
}
|
||||
if (typeof backupQueryUrl === 'string') {
|
||||
backupEndpointsQueryUrl = backupQueryUrl;
|
||||
|
@@ -14,5 +14,5 @@ export const markSuccess = noop;
|
||||
export const downloadAndInstallApk = noop;
|
||||
export const setCustomEndpoints = noop;
|
||||
export const getCurrentVersionInfo = noop;
|
||||
export const simpleUpdate = noop;
|
||||
export const simpleUpdate = (app) => app;
|
||||
export const onEvents = noop;
|
||||
|
80
lib/main.ts
80
lib/main.ts
@@ -1,5 +1,5 @@
|
||||
import {
|
||||
tryBackupEndpoints,
|
||||
updateBackupEndpoints,
|
||||
getCheckUrl,
|
||||
setCustomEndpoints,
|
||||
} from './endpoint';
|
||||
@@ -10,11 +10,13 @@ import {
|
||||
PermissionsAndroid,
|
||||
} from 'react-native';
|
||||
import {
|
||||
CheckResult,
|
||||
EventType,
|
||||
ProgressData,
|
||||
UpdateAvailableResult,
|
||||
UpdateEventsListener,
|
||||
} from './type';
|
||||
import { assertRelease, logger } from './utils';
|
||||
export { setCustomEndpoints };
|
||||
const {
|
||||
version: v,
|
||||
@@ -44,7 +46,7 @@ export const buildTime = PushyConstants.buildTime;
|
||||
let blockUpdate = PushyConstants.blockUpdate;
|
||||
let uuid = PushyConstants.uuid;
|
||||
|
||||
if (!PushyConstants.isUsingBundleUrl) {
|
||||
if (Platform.OS === 'android' && !PushyConstants.isUsingBundleUrl) {
|
||||
throw new Error(
|
||||
'react-native-update模块无法加载,请对照文档检查Bundle URL的配置',
|
||||
);
|
||||
@@ -73,10 +75,6 @@ if (!uuid) {
|
||||
PushyModule.setUuid(uuid);
|
||||
}
|
||||
|
||||
function logger(...args: string[]) {
|
||||
console.log('Pushy: ', ...args);
|
||||
}
|
||||
|
||||
const noop = () => {};
|
||||
let reporter: UpdateEventsListener = noop;
|
||||
|
||||
@@ -124,35 +122,28 @@ export const cInfo = {
|
||||
uuid,
|
||||
};
|
||||
|
||||
function assertRelease() {
|
||||
if (__DEV__) {
|
||||
throw new Error('react-native-update 只能在 RELEASE 版本中运行.');
|
||||
}
|
||||
}
|
||||
|
||||
let checkingThrottling = false;
|
||||
export async function checkUpdate(APPKEY: string, isRetry?: boolean) {
|
||||
let lastChecking;
|
||||
const empty = {};
|
||||
let lastResult: CheckResult;
|
||||
export async function checkUpdate(APPKEY: string) {
|
||||
assertRelease();
|
||||
if (checkingThrottling) {
|
||||
logger('repeated checking, ignored');
|
||||
return;
|
||||
const now = Date.now();
|
||||
if (lastResult && lastChecking && now - lastChecking < 1000 * 60) {
|
||||
// logger('repeated checking, ignored');
|
||||
return lastResult;
|
||||
}
|
||||
checkingThrottling = true;
|
||||
setTimeout(() => {
|
||||
checkingThrottling = false;
|
||||
}, 3000);
|
||||
lastChecking = now;
|
||||
if (blockUpdate && blockUpdate.until > Date.now() / 1000) {
|
||||
return report({
|
||||
report({
|
||||
type: 'errorChecking',
|
||||
message: `热更新已暂停,原因:${blockUpdate.reason}。请在"${new Date(
|
||||
blockUpdate.until * 1000,
|
||||
).toLocaleString()}"之后重试。`,
|
||||
});
|
||||
return lastResult || empty;
|
||||
}
|
||||
report({ type: 'checking' });
|
||||
let resp;
|
||||
try {
|
||||
resp = await fetch(getCheckUrl(APPKEY), {
|
||||
const fetchPayload = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
@@ -164,22 +155,45 @@ export async function checkUpdate(APPKEY: string, isRetry?: boolean) {
|
||||
buildTime,
|
||||
cInfo,
|
||||
}),
|
||||
});
|
||||
};
|
||||
let resp;
|
||||
try {
|
||||
resp = await fetch(getCheckUrl(APPKEY), fetchPayload);
|
||||
} catch (e) {
|
||||
if (isRetry) {
|
||||
return report({
|
||||
report({
|
||||
type: 'errorChecking',
|
||||
message: '无法连接主更新服务器,尝试备用节点',
|
||||
});
|
||||
const backupEndpoints = await updateBackupEndpoints();
|
||||
if (backupEndpoints) {
|
||||
try {
|
||||
resp = await Promise.race(
|
||||
backupEndpoints.map((endpoint) =>
|
||||
fetch(getCheckUrl(APPKEY, endpoint), fetchPayload),
|
||||
),
|
||||
);
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
if (!resp) {
|
||||
report({
|
||||
type: 'errorChecking',
|
||||
message: '无法连接更新服务器,请检查网络连接后重试',
|
||||
});
|
||||
return lastResult || empty;
|
||||
}
|
||||
await tryBackupEndpoints();
|
||||
return checkUpdate(APPKEY, true);
|
||||
}
|
||||
const result = await resp.json();
|
||||
const result: CheckResult = await resp.json();
|
||||
|
||||
lastResult = result;
|
||||
// @ts-ignore
|
||||
checkOperation(result.op);
|
||||
|
||||
if (resp.status !== 200) {
|
||||
return report({ type: 'errorChecking', message: result.message });
|
||||
report({
|
||||
type: 'errorChecking',
|
||||
//@ts-ignore
|
||||
message: result.message,
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@@ -26,7 +26,8 @@ export interface UpdateAvailableResult {
|
||||
export type CheckResult =
|
||||
| ExpiredResult
|
||||
| UpTodateResult
|
||||
| UpdateAvailableResult;
|
||||
| UpdateAvailableResult
|
||||
| {};
|
||||
|
||||
export interface ProgressData {
|
||||
hash: string;
|
||||
|
9
lib/utils.ts
Normal file
9
lib/utils.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export function logger(...args: any[]) {
|
||||
console.log('Pushy: ', ...args);
|
||||
}
|
||||
|
||||
export function assertRelease() {
|
||||
if (__DEV__) {
|
||||
throw new Error('react-native-update 只能在 RELEASE 版本中运行.');
|
||||
}
|
||||
}
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "react-native-update",
|
||||
"version": "9.0.0-beta.3",
|
||||
"version": "9.1.2",
|
||||
"description": "react-native hot update",
|
||||
"main": "lib/index.ts",
|
||||
"scripts": {
|
||||
@@ -39,7 +39,7 @@
|
||||
"url": "https://github.com/reactnativecn/react-native-pushy/issues"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react-native": ">=0.27.0"
|
||||
"react-native": ">=0.57.0"
|
||||
},
|
||||
"homepage": "https://github.com/reactnativecn/react-native-pushy#readme",
|
||||
"dependencies": {
|
||||
@@ -57,12 +57,15 @@
|
||||
"devDependencies": {
|
||||
"@types/fs-extra": "^9.0.13",
|
||||
"@types/jest": "^29.2.1",
|
||||
"@types/node": "^20.8.9",
|
||||
"@types/react": "^18.2.33",
|
||||
"detox": "^20.5.0",
|
||||
"firebase-tools": "^11.24.1",
|
||||
"fs-extra": "^9.1.0",
|
||||
"jest": "^29.2.1",
|
||||
"pod-install": "^0.1.37",
|
||||
"react-native": "^0.72.6",
|
||||
"ts-jest": "^29.0.3",
|
||||
"typescript": "^4.1.3"
|
||||
"typescript": "^5.2.2"
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user