mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-09-17 15:36:13 +08:00
Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
427b05f3a4 | ||
![]() |
73c3dc538c |
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.UpdateModule { *; }
|
||||||
-keepnames class com.facebook.react.ReactInstanceManager { *; }
|
-keepnames class com.facebook.react.ReactInstanceManager { *; }
|
107
lib/main.js
107
lib/main.js
@@ -58,34 +58,42 @@ if (!uuid) {
|
|||||||
Pushy.setUuid(uuid);
|
Pushy.setUuid(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
function logger(text) {
|
function logger(...args) {
|
||||||
console.log(`Pushy: ${text}`);
|
console.log('Pushy: ', ...args);
|
||||||
}
|
}
|
||||||
|
|
||||||
function report(hash, type) {
|
const noop = () => {};
|
||||||
logger(type);
|
let reporter = noop;
|
||||||
fetch(getReportUrl(), {
|
|
||||||
method: 'POST',
|
export function onEvents(customReporter) {
|
||||||
headers: {
|
reporter = customReporter;
|
||||||
Accept: 'application/json',
|
if (isRolledBack) {
|
||||||
'Content-Type': 'application/json',
|
report({
|
||||||
},
|
type: 'rollback',
|
||||||
body: JSON.stringify({
|
data: {
|
||||||
hash,
|
rolledBackVersion,
|
||||||
type,
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function report({ type, message = '', data = {} }) {
|
||||||
|
logger(type + ' ' + message);
|
||||||
|
reporter({
|
||||||
|
type,
|
||||||
|
data: {
|
||||||
|
currentVersion,
|
||||||
cInfo,
|
cInfo,
|
||||||
packageVersion,
|
packageVersion,
|
||||||
buildTime,
|
buildTime,
|
||||||
}),
|
message,
|
||||||
}).catch((_e) => {});
|
...data,
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
logger('uuid: ' + uuid);
|
logger('uuid: ' + uuid);
|
||||||
|
|
||||||
if (isRolledBack) {
|
|
||||||
report(rolledBackVersion, 'rollback');
|
|
||||||
}
|
|
||||||
|
|
||||||
export const cInfo = {
|
export const cInfo = {
|
||||||
pushy: require('../package.json').version,
|
pushy: require('../package.json').version,
|
||||||
rn: RNVersion,
|
rn: RNVersion,
|
||||||
@@ -99,28 +107,30 @@ function assertRelease() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let checkingThrottling = false;
|
let lastChecking;
|
||||||
|
const empty = {};
|
||||||
|
let lastResult;
|
||||||
export async function checkUpdate(APPKEY, isRetry) {
|
export async function checkUpdate(APPKEY, isRetry) {
|
||||||
assertRelease();
|
|
||||||
if (checkingThrottling) {
|
|
||||||
logger('repeated checking, ignored');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
checkingThrottling = true;
|
|
||||||
setTimeout(() => {
|
|
||||||
checkingThrottling = false;
|
|
||||||
}, 3000);
|
|
||||||
if (blockUpdate && blockUpdate.until > Date.now() / 1000) {
|
|
||||||
throw new Error(
|
|
||||||
`热更新已暂停,原因:${blockUpdate.reason}。请在"${new Date(
|
|
||||||
blockUpdate.until * 1000,
|
|
||||||
).toLocaleString()}"之后重试。`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (typeof APPKEY !== 'string') {
|
if (typeof APPKEY !== 'string') {
|
||||||
throw new Error('未检查到合法的APPKEY,请查看update.json文件是否正确生成');
|
throw new Error('未检查到合法的APPKEY,请查看update.json文件是否正确生成');
|
||||||
}
|
}
|
||||||
logger('checking update');
|
assertRelease();
|
||||||
|
const now = Date.now();
|
||||||
|
if (lastResult && lastChecking && now - lastChecking < 1000 * 60) {
|
||||||
|
// logger('repeated checking, ignored');
|
||||||
|
return lastResult;
|
||||||
|
}
|
||||||
|
lastChecking = now;
|
||||||
|
if (blockUpdate && blockUpdate.until > Date.now() / 1000) {
|
||||||
|
report({
|
||||||
|
type: 'errorChecking',
|
||||||
|
message: `热更新已暂停,原因:${blockUpdate.reason}。请在"${new Date(
|
||||||
|
blockUpdate.until * 1000,
|
||||||
|
).toLocaleString()}"之后重试。`,
|
||||||
|
});
|
||||||
|
return lastResult || empty;
|
||||||
|
}
|
||||||
|
report({ type: 'checking' });
|
||||||
let resp;
|
let resp;
|
||||||
try {
|
try {
|
||||||
resp = await fetch(getCheckUrl(APPKEY), {
|
resp = await fetch(getCheckUrl(APPKEY), {
|
||||||
@@ -138,16 +148,25 @@ export async function checkUpdate(APPKEY, isRetry) {
|
|||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (isRetry) {
|
if (isRetry) {
|
||||||
throw new Error('无法连接更新服务器,请检查网络连接后重试');
|
report({
|
||||||
|
type: 'errorChecking',
|
||||||
|
message: '无法连接更新服务器,请检查网络连接后重试',
|
||||||
|
});
|
||||||
|
return lastResult || empty;
|
||||||
}
|
}
|
||||||
await tryBackupEndpoints();
|
await tryBackupEndpoints();
|
||||||
return checkUpdate(APPKEY, true);
|
return checkUpdate(APPKEY, true);
|
||||||
}
|
}
|
||||||
const result = await resp.json();
|
const result = await resp.json();
|
||||||
|
lastResult = result;
|
||||||
|
|
||||||
checkOperation(result.op);
|
checkOperation(result.op);
|
||||||
|
|
||||||
if (resp.status !== 200) {
|
if (resp.status !== 200) {
|
||||||
throw new Error(result.message);
|
report({
|
||||||
|
type: 'errorChecking',
|
||||||
|
message: result.message,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@@ -206,6 +225,7 @@ export async function downloadUpdate(options, eventListeners) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
let succeeded = false;
|
let succeeded = false;
|
||||||
|
report({ type: 'downloading' });
|
||||||
if (options.diffUrl) {
|
if (options.diffUrl) {
|
||||||
logger('downloading diff');
|
logger('downloading diff');
|
||||||
try {
|
try {
|
||||||
@@ -245,8 +265,7 @@ export async function downloadUpdate(options, eventListeners) {
|
|||||||
}
|
}
|
||||||
progressHandler && progressHandler.remove();
|
progressHandler && progressHandler.remove();
|
||||||
if (!succeeded) {
|
if (!succeeded) {
|
||||||
report(options.hash, 'error');
|
return report({ type: 'errorUpdate', data: { newVersion: options.hash } });
|
||||||
throw new Error('all update attempts failed');
|
|
||||||
}
|
}
|
||||||
setLocalHashInfo(options.hash, {
|
setLocalHashInfo(options.hash, {
|
||||||
name: options.name,
|
name: options.name,
|
||||||
@@ -305,10 +324,10 @@ export async function downloadAndInstallApk({ url, onDownloadProgress }) {
|
|||||||
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
|
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
|
||||||
);
|
);
|
||||||
if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
|
if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
|
||||||
return;
|
return report({ type: 'rejectStoragePermission' });
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn(err);
|
return report({ type: 'errorStoragePermission' });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let hash = Date.now().toString();
|
let hash = Date.now().toString();
|
||||||
@@ -327,6 +346,8 @@ export async function downloadAndInstallApk({ url, onDownloadProgress }) {
|
|||||||
url,
|
url,
|
||||||
target: 'update.apk',
|
target: 'update.apk',
|
||||||
hash,
|
hash,
|
||||||
|
}).catch(() => {
|
||||||
|
report({ type: 'errowDownloadAndInstallApk' });
|
||||||
});
|
});
|
||||||
progressHandler && progressHandler.remove();
|
progressHandler && progressHandler.remove();
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "react-native-update",
|
"name": "react-native-update",
|
||||||
"version": "8.1.0",
|
"version": "8.2.0",
|
||||||
"description": "react-native hot update",
|
"description": "react-native hot update",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
Reference in New Issue
Block a user