mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-09-18 02:26:10 +08:00
Compare commits
25 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
1daafb0142 | ||
![]() |
c1679a4cea | ||
![]() |
7ab7dffb0f | ||
![]() |
8622935bdf | ||
![]() |
b747b1f356 | ||
![]() |
7752581470 | ||
![]() |
33eb89d2a7 | ||
![]() |
d111bf5a9c | ||
![]() |
23346a5f1d | ||
![]() |
5aca2104c2 | ||
![]() |
fe0a05db3d | ||
![]() |
2b287786ff | ||
![]() |
7d128900cd | ||
![]() |
189e3ec78e | ||
![]() |
821722165a | ||
![]() |
6cb53ac655 | ||
![]() |
7b9a24168a | ||
![]() |
c6354bbedc | ||
![]() |
b53878c291 | ||
![]() |
ab01312f8d | ||
![]() |
44784b6d3e | ||
![]() |
30c21fed91 | ||
![]() |
15af7802ad | ||
![]() |
8bed6ef979 | ||
![]() |
7d03dc24b7 |
@@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
1. 基于阿里云高速 CDN 分发,对比其他服务器在国外的热更新服务,分发更稳定,更新成功率极高。
|
1. 基于阿里云高速 CDN 分发,对比其他服务器在国外的热更新服务,分发更稳定,更新成功率极高。
|
||||||
2. 基于 bsdiff/hdiff 算法创建的**超小更新包**,通常版本迭代后在几十 KB 级别(其他全量热更新服务所需流量通常在几十 MB 级别)。
|
2. 基于 bsdiff/hdiff 算法创建的**超小更新包**,通常版本迭代后在几十 KB 级别(其他全量热更新服务所需流量通常在几十 MB 级别)。
|
||||||
3. 始终跟进 RN 最新正式版本,第一时间提供支持。支持 hermes 字节码格式。(暂不支持新架构,会待其相对稳定后跟进)
|
3. 始终跟进 RN 最新正式版本,第一时间提供支持。支持 hermes 字节码格式。支持新架构。
|
||||||
4. 跨越多个版本进行更新时,只需要下载**一个更新包**,不需要逐版本依次更新。
|
4. 跨越多个版本进行更新时,只需要下载**一个更新包**,不需要逐版本依次更新。
|
||||||
5. 命令行工具 & 网页双端管理,版本发布过程简单便捷,完全可以集成 CI。
|
5. 命令行工具 & 网页双端管理,版本发布过程简单便捷,完全可以集成 CI。
|
||||||
6. 支持崩溃回滚,安全可靠。
|
6. 支持崩溃回滚,安全可靠。
|
||||||
|
@@ -9,6 +9,19 @@ def isNewArchitectureEnabled() {
|
|||||||
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
|
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def supportsNamespace() {
|
||||||
|
def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
|
||||||
|
def major = parsed[0].toInteger()
|
||||||
|
def minor = parsed[1].toInteger()
|
||||||
|
|
||||||
|
// Namespace support was added in 7.3.0
|
||||||
|
if (major == 7 && minor >= 3) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return major >= 8
|
||||||
|
}
|
||||||
|
|
||||||
apply plugin: 'com.android.library'
|
apply plugin: 'com.android.library'
|
||||||
if (isNewArchitectureEnabled()) {
|
if (isNewArchitectureEnabled()) {
|
||||||
apply plugin: 'com.facebook.react'
|
apply plugin: 'com.facebook.react'
|
||||||
@@ -16,6 +29,16 @@ if (isNewArchitectureEnabled()) {
|
|||||||
|
|
||||||
|
|
||||||
android {
|
android {
|
||||||
|
|
||||||
|
if (supportsNamespace()) {
|
||||||
|
namespace "cn.reactnative.modules.update"
|
||||||
|
|
||||||
|
sourceSets {
|
||||||
|
main {
|
||||||
|
manifest.srcFile "src/main/AndroidManifestNew.xml"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
compileSdkVersion safeExtGet('compileSdkVersion', 28)
|
compileSdkVersion safeExtGet('compileSdkVersion', 28)
|
||||||
buildToolsVersion safeExtGet('buildToolsVersion', '28.0.3')
|
buildToolsVersion safeExtGet('buildToolsVersion', '28.0.3')
|
||||||
|
|
||||||
|
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.DownloadTask { *; }
|
||||||
|
-keepnames class cn.reactnative.modules.update.UpdateModuleImpl { *; }
|
||||||
-keepnames class com.facebook.react.ReactInstanceManager { *; }
|
-keepnames class com.facebook.react.ReactInstanceManager { *; }
|
@@ -1,9 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="cn.reactnative.modules.update">
|
package="cn.reactnative.modules.update">
|
||||||
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
|
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
|
||||||
<application>
|
<application>
|
||||||
<meta-data android:name="pushy_build_time" android:value="@string/pushy_build_time" />
|
<meta-data android:name="pushy_build_time" android:value="@string/pushy_build_time" />
|
||||||
<provider
|
<provider
|
||||||
|
14
android/src/main/AndroidManifestNew.xml
Normal file
14
android/src/main/AndroidManifestNew.xml
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<application>
|
||||||
|
<meta-data android:name="pushy_build_time" android:value="@string/pushy_build_time" />
|
||||||
|
<provider
|
||||||
|
android:name=".PushyFileProvider"
|
||||||
|
android:authorities="${applicationId}.pushy.fileprovider"
|
||||||
|
android:exported="false"
|
||||||
|
android:grantUriPermissions="true">
|
||||||
|
<meta-data
|
||||||
|
android:name="android.support.FILE_PROVIDER_PATHS"
|
||||||
|
android:resource="@xml/pushy_file_paths" />
|
||||||
|
</provider>
|
||||||
|
</application>
|
||||||
|
</manifest>
|
@@ -237,19 +237,7 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
|
|||||||
while (entries.hasMoreElements()) {
|
while (entries.hasMoreElements()) {
|
||||||
ZipEntry ze = entries.nextElement();
|
ZipEntry ze = entries.nextElement();
|
||||||
|
|
||||||
String fn = ze.getName();
|
zipFile.unzipToPath(ze, param.unzipDirectory);
|
||||||
File fmd = new File(param.unzipDirectory, fn);
|
|
||||||
|
|
||||||
if (UpdateContext.DEBUG) {
|
|
||||||
Log.d("RNUpdate", "Unzipping " + fn);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ze.isDirectory()) {
|
|
||||||
fmd.mkdirs();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
zipFile.unzipToFile(ze, fmd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
zipFile.close();
|
zipFile.close();
|
||||||
@@ -324,8 +312,15 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
|
|||||||
} else {
|
} else {
|
||||||
target = copyList.get((from));
|
target = copyList.get((from));
|
||||||
}
|
}
|
||||||
target.add(new File(param.unzipDirectory, to));
|
File toFile = new File(param.unzipDirectory, to);
|
||||||
//copyFromResource(from, new File(param.unzipDirectory, to));
|
|
||||||
|
// Fixing a Zip Path Traversal Vulnerability
|
||||||
|
// https://support.google.com/faqs/answer/9294009
|
||||||
|
String canonicalPath = toFile.getCanonicalPath();
|
||||||
|
if (!canonicalPath.startsWith(param.unzipDirectory.getCanonicalPath() + File.separator)) {
|
||||||
|
throw new SecurityException("Illegal name: " + to);
|
||||||
|
}
|
||||||
|
target.add(toFile);
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -339,18 +334,9 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
|
|||||||
fout.close();
|
fout.close();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
File fmd = new File(param.unzipDirectory, fn);
|
|
||||||
|
|
||||||
if (UpdateContext.DEBUG) {
|
|
||||||
Log.d("RNUpdate", "Unzipping " + fn);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ze.isDirectory()) {
|
zipFile.unzipToPath(ze, param.unzipDirectory);
|
||||||
fmd.mkdirs();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
zipFile.unzipToFile(ze, fmd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
zipFile.close();
|
zipFile.close();
|
||||||
@@ -419,18 +405,8 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
|
|||||||
fout.close();
|
fout.close();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
File fmd = new File(param.unzipDirectory, fn);
|
|
||||||
|
|
||||||
if (UpdateContext.DEBUG) {
|
zipFile.unzipToPath(ze, param.unzipDirectory);
|
||||||
Log.d("RNUpdate", "Unzipping " + fn);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ze.isDirectory()) {
|
|
||||||
fmd.mkdirs();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
zipFile.unzipToFile(ze, fmd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
zipFile.close();
|
zipFile.close();
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
package cn.reactnative.modules.update;
|
package cn.reactnative.modules.update;
|
||||||
|
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
import java.io.BufferedOutputStream;
|
import java.io.BufferedOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -10,12 +12,15 @@ import java.util.Enumeration;
|
|||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipFile;
|
import java.util.zip.ZipFile;
|
||||||
|
|
||||||
|
|
||||||
public class SafeZipFile extends ZipFile {
|
public class SafeZipFile extends ZipFile {
|
||||||
|
|
||||||
public SafeZipFile(File file) throws IOException {
|
public SafeZipFile(File file) throws IOException {
|
||||||
super(file);
|
super(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final int BUFFER_SIZE = 8192;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Enumeration<? extends ZipEntry> entries() {
|
public Enumeration<? extends ZipEntry> entries() {
|
||||||
return new SafeZipEntryIterator(super.entries());
|
return new SafeZipEntryIterator(super.entries());
|
||||||
@@ -43,40 +48,46 @@ public class SafeZipFile extends ZipFile {
|
|||||||
* avoid ZipperDown
|
* avoid ZipperDown
|
||||||
*/
|
*/
|
||||||
if (null != name && (name.contains("../") || name.contains("..\\"))) {
|
if (null != name && (name.contains("../") || name.contains("..\\"))) {
|
||||||
throw new SecurityException("illegal entry: " + entry.getName());
|
throw new SecurityException("illegal entry: " + name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void unzipToFile(ZipEntry entry, File output) throws IOException {
|
public void unzipToPath(ZipEntry ze, File targetPath) throws IOException {
|
||||||
InputStream inputStream = null;
|
String name = ze.getName();
|
||||||
try {
|
File target = new File(targetPath, name);
|
||||||
inputStream = getInputStream(entry);
|
|
||||||
writeOutInputStream(output, inputStream);
|
// Fixing a Zip Path Traversal Vulnerability
|
||||||
} finally {
|
// https://support.google.com/faqs/answer/9294009
|
||||||
if (inputStream != null) {
|
String canonicalPath = target.getCanonicalPath();
|
||||||
inputStream.close();
|
if (!canonicalPath.startsWith(targetPath.getCanonicalPath() + File.separator)) {
|
||||||
|
throw new SecurityException("Illegal name: " + name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (UpdateContext.DEBUG) {
|
||||||
|
Log.d("RNUpdate", "Unzipping " + name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ze.isDirectory()) {
|
||||||
|
target.mkdirs();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
unzipToFile(ze, target);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void unzipToFile(ZipEntry ze, File target) throws IOException {
|
||||||
|
try (InputStream inputStream = getInputStream(ze)) {
|
||||||
|
try (BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(target));
|
||||||
|
BufferedInputStream input = new BufferedInputStream(inputStream)) {
|
||||||
|
byte[] buffer = new byte[BUFFER_SIZE];
|
||||||
|
int n;
|
||||||
|
while ((n = input.read(buffer, 0, BUFFER_SIZE)) >= 0) {
|
||||||
|
output.write(buffer, 0, n);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeOutInputStream(File file, InputStream inputStream) throws IOException {
|
|
||||||
BufferedOutputStream output = null;
|
|
||||||
try {
|
|
||||||
output = new BufferedOutputStream(
|
|
||||||
new FileOutputStream(file));
|
|
||||||
BufferedInputStream input = new BufferedInputStream(inputStream);
|
|
||||||
byte b[] = new byte[8192];
|
|
||||||
int n;
|
|
||||||
while ((n = input.read(b, 0, 8192)) >= 0) {
|
|
||||||
output.write(b, 0, n);
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (output != null) {
|
|
||||||
output.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
@@ -1,75 +1,26 @@
|
|||||||
|
import { logger } 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 backupEndpointsQueryUrl: string | null = null;
|
||||||
|
|
||||||
function ping(url: string, rejectImmediate?: boolean) {
|
export async function updateBackupEndpoints() {
|
||||||
return new Promise((resolve, reject) => {
|
if (backupEndpointsQueryUrl) {
|
||||||
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) {
|
|
||||||
try {
|
try {
|
||||||
const resp = await fetch(backupEndpointsQueryUrl);
|
const resp = await fetch(backupEndpointsQueryUrl);
|
||||||
backupEndpoints = await resp.json();
|
const remoteEndpoints = await resp.json();
|
||||||
logger('get remote endpoints:', backupEndpoints);
|
if (Array.isArray(remoteEndpoints)) {
|
||||||
|
backupEndpoints = Array.from(
|
||||||
|
new Set([...backupEndpoints, ...remoteEndpoints]),
|
||||||
|
);
|
||||||
|
logger('fetch remote endpoints:', remoteEndpoints);
|
||||||
|
logger('merged backup endpoints:', backupEndpoints);
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger('get remote endpoints failed');
|
logger('fetch remote endpoints failed');
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await pickFatestAvailableEndpoint();
|
return backupEndpoints;
|
||||||
}
|
|
||||||
|
|
||||||
async function pickFatestAvailableEndpoint(endpoints = backupEndpoints) {
|
|
||||||
const fastestEndpoint = await Promise.race(
|
|
||||||
endpoints.map(pingAndReturnEndpoint),
|
|
||||||
);
|
|
||||||
if (typeof fastestEndpoint === 'string') {
|
|
||||||
logger(`pick endpoint: ${fastestEndpoint}`);
|
|
||||||
currentEndpoint = fastestEndpoint;
|
|
||||||
} else {
|
|
||||||
logger('all remote endpoints failed');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function pingAndReturnEndpoint(endpoint = currentEndpoint) {
|
|
||||||
return ping(getStatusUrl(endpoint)).then(() => endpoint);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getStatusUrl(endpoint = currentEndpoint) {
|
|
||||||
return `${endpoint}/status`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getCheckUrl(APPKEY, endpoint = currentEndpoint) {
|
export function getCheckUrl(APPKEY, endpoint = currentEndpoint) {
|
||||||
@@ -95,7 +46,6 @@ export function setCustomEndpoints({
|
|||||||
backupEndpointsQueryUrl = null;
|
backupEndpointsQueryUrl = null;
|
||||||
if (Array.isArray(backups) && backups.length > 0) {
|
if (Array.isArray(backups) && backups.length > 0) {
|
||||||
backupEndpoints = backups;
|
backupEndpoints = backups;
|
||||||
pickFatestAvailableEndpoint();
|
|
||||||
}
|
}
|
||||||
if (typeof backupQueryUrl === 'string') {
|
if (typeof backupQueryUrl === 'string') {
|
||||||
backupEndpointsQueryUrl = backupQueryUrl;
|
backupEndpointsQueryUrl = backupQueryUrl;
|
||||||
|
@@ -14,5 +14,5 @@ export const markSuccess = noop;
|
|||||||
export const downloadAndInstallApk = noop;
|
export const downloadAndInstallApk = noop;
|
||||||
export const setCustomEndpoints = noop;
|
export const setCustomEndpoints = noop;
|
||||||
export const getCurrentVersionInfo = noop;
|
export const getCurrentVersionInfo = noop;
|
||||||
export const simpleUpdate = noop;
|
export const simpleUpdate = (app) => app;
|
||||||
export const onEvents = noop;
|
export const onPushyEvents = noop;
|
||||||
|
107
lib/main.ts
107
lib/main.ts
@@ -1,5 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
tryBackupEndpoints,
|
updateBackupEndpoints,
|
||||||
getCheckUrl,
|
getCheckUrl,
|
||||||
setCustomEndpoints,
|
setCustomEndpoints,
|
||||||
} from './endpoint';
|
} from './endpoint';
|
||||||
@@ -10,11 +10,13 @@ import {
|
|||||||
PermissionsAndroid,
|
PermissionsAndroid,
|
||||||
} from 'react-native';
|
} from 'react-native';
|
||||||
import {
|
import {
|
||||||
|
CheckResult,
|
||||||
EventType,
|
EventType,
|
||||||
ProgressData,
|
ProgressData,
|
||||||
UpdateAvailableResult,
|
UpdateAvailableResult,
|
||||||
UpdateEventsListener,
|
UpdateEventsListener,
|
||||||
} from './type';
|
} from './type';
|
||||||
|
import { assertRelease, logger } from './utils';
|
||||||
export { setCustomEndpoints };
|
export { setCustomEndpoints };
|
||||||
const {
|
const {
|
||||||
version: v,
|
version: v,
|
||||||
@@ -73,14 +75,10 @@ if (!uuid) {
|
|||||||
PushyModule.setUuid(uuid);
|
PushyModule.setUuid(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
function logger(...args: string[]) {
|
|
||||||
console.log('Pushy: ', ...args);
|
|
||||||
}
|
|
||||||
|
|
||||||
const noop = () => {};
|
const noop = () => {};
|
||||||
let reporter: UpdateEventsListener = noop;
|
let reporter: UpdateEventsListener = noop;
|
||||||
|
|
||||||
export function onEvents(customReporter: UpdateEventsListener) {
|
export function onPushyEvents(customReporter: UpdateEventsListener) {
|
||||||
reporter = customReporter;
|
reporter = customReporter;
|
||||||
if (isRolledBack) {
|
if (isRolledBack) {
|
||||||
report({
|
report({
|
||||||
@@ -124,63 +122,78 @@ export const cInfo = {
|
|||||||
uuid,
|
uuid,
|
||||||
};
|
};
|
||||||
|
|
||||||
function assertRelease() {
|
let lastChecking;
|
||||||
// @ts-expect-error
|
const empty = {};
|
||||||
if (__DEV__) {
|
let lastResult: CheckResult;
|
||||||
throw new Error('react-native-update 只能在 RELEASE 版本中运行.');
|
export async function checkUpdate(APPKEY: string) {
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let checkingThrottling = false;
|
|
||||||
export async function checkUpdate(APPKEY: string, isRetry?: boolean) {
|
|
||||||
assertRelease();
|
assertRelease();
|
||||||
if (checkingThrottling) {
|
const now = Date.now();
|
||||||
logger('repeated checking, ignored');
|
if (lastResult && lastChecking && now - lastChecking < 1000 * 60) {
|
||||||
return;
|
// logger('repeated checking, ignored');
|
||||||
|
return lastResult;
|
||||||
}
|
}
|
||||||
checkingThrottling = true;
|
lastChecking = now;
|
||||||
setTimeout(() => {
|
|
||||||
checkingThrottling = false;
|
|
||||||
}, 3000);
|
|
||||||
if (blockUpdate && blockUpdate.until > Date.now() / 1000) {
|
if (blockUpdate && blockUpdate.until > Date.now() / 1000) {
|
||||||
return report({
|
report({
|
||||||
type: 'errorChecking',
|
type: 'errorChecking',
|
||||||
message: `热更新已暂停,原因:${blockUpdate.reason}。请在"${new Date(
|
message: `热更新已暂停,原因:${blockUpdate.reason}。请在"${new Date(
|
||||||
blockUpdate.until * 1000,
|
blockUpdate.until * 1000,
|
||||||
).toLocaleString()}"之后重试。`,
|
).toLocaleString()}"之后重试。`,
|
||||||
});
|
});
|
||||||
|
return lastResult || empty;
|
||||||
}
|
}
|
||||||
report({ type: 'checking' });
|
report({ type: 'checking' });
|
||||||
|
const fetchPayload = {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
Accept: 'application/json',
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
packageVersion,
|
||||||
|
hash: currentVersion,
|
||||||
|
buildTime,
|
||||||
|
cInfo,
|
||||||
|
}),
|
||||||
|
};
|
||||||
let resp;
|
let resp;
|
||||||
try {
|
try {
|
||||||
resp = await fetch(getCheckUrl(APPKEY), {
|
resp = await fetch(getCheckUrl(APPKEY), fetchPayload);
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
Accept: 'application/json',
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
packageVersion,
|
|
||||||
hash: currentVersion,
|
|
||||||
buildTime,
|
|
||||||
cInfo,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (isRetry) {
|
report({
|
||||||
return report({
|
type: 'errorChecking',
|
||||||
type: 'errorChecking',
|
message: '无法连接主更新服务器,尝试备用节点',
|
||||||
message: '无法连接更新服务器,请检查网络连接后重试',
|
});
|
||||||
});
|
const backupEndpoints = await updateBackupEndpoints();
|
||||||
|
if (backupEndpoints) {
|
||||||
|
try {
|
||||||
|
resp = await Promise.race(
|
||||||
|
backupEndpoints.map((endpoint) =>
|
||||||
|
fetch(getCheckUrl(APPKEY, endpoint), fetchPayload),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} catch {}
|
||||||
}
|
}
|
||||||
await tryBackupEndpoints();
|
|
||||||
return checkUpdate(APPKEY, true);
|
|
||||||
}
|
}
|
||||||
const result = await resp.json();
|
if (!resp) {
|
||||||
|
report({
|
||||||
|
type: 'errorChecking',
|
||||||
|
message: '无法连接更新服务器,请检查网络连接后重试',
|
||||||
|
});
|
||||||
|
return lastResult || empty;
|
||||||
|
}
|
||||||
|
const result: CheckResult = await resp.json();
|
||||||
|
|
||||||
|
lastResult = result;
|
||||||
|
// @ts-ignore
|
||||||
checkOperation(result.op);
|
checkOperation(result.op);
|
||||||
|
|
||||||
if (resp.status !== 200) {
|
if (resp.status !== 200) {
|
||||||
return report({ type: 'errorChecking', message: result.message });
|
report({
|
||||||
|
type: 'errorChecking',
|
||||||
|
//@ts-ignore
|
||||||
|
message: result.message,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@@ -309,10 +322,12 @@ 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)) {
|
if (assertHash(hash) && !applyingUpdate) {
|
||||||
logger('switchVersion: ' + hash);
|
logger('switchVersion: ' + hash);
|
||||||
|
applyingUpdate = true;
|
||||||
PushyModule.reloadUpdate({ hash });
|
PushyModule.reloadUpdate({ hash });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -16,20 +16,20 @@ import {
|
|||||||
switchVersionLater,
|
switchVersionLater,
|
||||||
markSuccess,
|
markSuccess,
|
||||||
downloadAndInstallApk,
|
downloadAndInstallApk,
|
||||||
onEvents,
|
onPushyEvents,
|
||||||
} from './main';
|
} from './main';
|
||||||
import { UpdateEventsListener } from './type';
|
import { UpdateEventsListener } from './type';
|
||||||
|
|
||||||
export function simpleUpdate(
|
export function simpleUpdate(
|
||||||
WrappedComponent: ComponentType,
|
WrappedComponent: ComponentType,
|
||||||
options: { appKey?: string; onEvents?: UpdateEventsListener } = {},
|
options: { appKey?: string; onPushyEvents?: UpdateEventsListener } = {},
|
||||||
) {
|
) {
|
||||||
const { appKey, onEvents: eventListeners } = options;
|
const { appKey, onPushyEvents: eventListeners } = options;
|
||||||
if (!appKey) {
|
if (!appKey) {
|
||||||
throw new Error('appKey is required for simpleUpdate()');
|
throw new Error('appKey is required for simpleUpdate()');
|
||||||
}
|
}
|
||||||
if (typeof eventListeners === 'function') {
|
if (typeof eventListeners === 'function') {
|
||||||
onEvents(eventListeners);
|
onPushyEvents(eventListeners);
|
||||||
}
|
}
|
||||||
return __DEV__
|
return __DEV__
|
||||||
? WrappedComponent
|
? WrappedComponent
|
||||||
|
@@ -26,7 +26,8 @@ export interface UpdateAvailableResult {
|
|||||||
export type CheckResult =
|
export type CheckResult =
|
||||||
| ExpiredResult
|
| ExpiredResult
|
||||||
| UpTodateResult
|
| UpTodateResult
|
||||||
| UpdateAvailableResult;
|
| UpdateAvailableResult
|
||||||
|
| {};
|
||||||
|
|
||||||
export interface ProgressData {
|
export interface ProgressData {
|
||||||
hash: string;
|
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",
|
"name": "react-native-update",
|
||||||
"version": "9.0.0",
|
"version": "9.1.6",
|
||||||
"description": "react-native hot update",
|
"description": "react-native hot update",
|
||||||
"main": "lib/index.ts",
|
"main": "lib/index.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -39,7 +39,7 @@
|
|||||||
"url": "https://github.com/reactnativecn/react-native-pushy/issues"
|
"url": "https://github.com/reactnativecn/react-native-pushy/issues"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"react-native": ">=0.27.0"
|
"react-native": ">=0.57.0"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/reactnativecn/react-native-pushy#readme",
|
"homepage": "https://github.com/reactnativecn/react-native-pushy#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@@ -57,12 +57,15 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/fs-extra": "^9.0.13",
|
"@types/fs-extra": "^9.0.13",
|
||||||
"@types/jest": "^29.2.1",
|
"@types/jest": "^29.2.1",
|
||||||
|
"@types/node": "^20.8.9",
|
||||||
|
"@types/react": "^18.2.33",
|
||||||
"detox": "^20.5.0",
|
"detox": "^20.5.0",
|
||||||
"firebase-tools": "^11.24.1",
|
"firebase-tools": "^11.24.1",
|
||||||
"fs-extra": "^9.1.0",
|
"fs-extra": "^9.1.0",
|
||||||
"jest": "^29.2.1",
|
"jest": "^29.2.1",
|
||||||
"pod-install": "^0.1.37",
|
"pod-install": "^0.1.37",
|
||||||
|
"react-native": "^0.72.6",
|
||||||
"ts-jest": "^29.0.3",
|
"ts-jest": "^29.0.3",
|
||||||
"typescript": "^4.1.3"
|
"typescript": "^5.2.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user