1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee
react-native-pushy/lib/index.js

192 lines
4.6 KiB
JavaScript
Raw Normal View History

2020-08-13 00:32:07 +08:00
import {
tryBackupEndpoints,
getCheckUrl,
setCustomEndpoints,
} from './endpoint';
2020-09-16 13:01:14 +08:00
import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
2020-07-28 23:15:42 +08:00
export { setCustomEndpoints };
2020-08-31 11:47:08 +08:00
const {
version: v,
} = require('react-native/Libraries/Core/ReactNativeVersion');
const RNVersion = `${v.major}.${v.minor}.${v.patch}`;
2020-08-31 18:39:03 +08:00
const uuidv4 = require('uuid/v4');
2016-04-04 23:02:28 +08:00
2020-04-30 17:59:28 +08:00
let Pushy = NativeModules.Pushy;
if (!Pushy) {
throw new Error('react-native-update模块无法加载请对照安装文档检查配置。');
}
2016-04-04 23:02:28 +08:00
2019-11-16 00:31:30 +08:00
export const downloadRootDir = Pushy.downloadRootDir;
export const packageVersion = Pushy.packageVersion;
export const currentVersion = Pushy.currentVersion;
export const isFirstTime = Pushy.isFirstTime;
export const isRolledBack = Pushy.isRolledBack;
export const buildTime = Pushy.buildTime;
2020-08-31 01:17:28 +08:00
let blockUpdate = Pushy.blockUpdate;
2020-08-31 11:47:08 +08:00
let uuid = Pushy.uuid;
2016-04-04 23:02:28 +08:00
2020-08-13 00:32:07 +08:00
if (Platform.OS === 'android' && !Pushy.isUsingBundleUrl) {
throw new Error(
'react-native-update模块无法加载请对照文档检查Bundle URL的配置',
);
}
2020-09-16 13:01:14 +08:00
const eventEmitter = new NativeEventEmitter(Pushy);
2020-08-31 11:47:08 +08:00
if (!uuid) {
uuid = uuidv4();
Pushy.setUuid(uuid);
}
2020-09-16 13:01:14 +08:00
function logger(text) {
console.log(`Pushy: ${text}`);
}
logger('uuid: ' + uuid);
2020-08-31 11:47:08 +08:00
2016-04-04 23:02:28 +08:00
/*
Return json:
2020-08-31 01:17:28 +08:00
Package expired:
2016-04-04 23:02:28 +08:00
{
expired: true,
downloadUrl: 'http://appstore/downloadUrl',
}
Package is up to date:
{
upToDate: true,
}
There is available update:
{
update: true,
name: '1.0.3-rc',
hash: 'hash',
description: '添加聊天功能\n修复商城页面BUG',
metaInfo: '{"silent":true}',
pdiffUrl: 'http://update-packages.reactnative.cn/hash',
diffUrl: 'http://update-packages.reactnative.cn/hash',
}
*/
2019-10-04 22:27:33 +08:00
function assertRelease() {
if (__DEV__) {
throw new Error('react-native-update can only run on RELEASE version.');
}
}
2020-01-18 23:55:10 +08:00
export async function checkUpdate(APPKEY, isRetry) {
2019-10-04 22:27:33 +08:00
assertRelease();
2020-08-31 18:39:03 +08:00
if (blockUpdate && blockUpdate.until > Date.now() / 1000) {
throw new Error(
`热更新已暂停,原因:${blockUpdate.reason}。请在"${new Date(
blockUpdate.until * 1000,
).toLocaleString()}"之后重试`,
2020-08-31 01:17:28 +08:00
);
}
2020-09-16 13:01:14 +08:00
if (typeof APPKEY !== 'string') {
throw new Error('未检查到合法的APPKEY请查看update.json文件是否正确生成');
}
logger('checking update');
2020-01-18 23:55:10 +08:00
let resp;
try {
2020-07-28 23:15:42 +08:00
resp = await fetch(getCheckUrl(APPKEY), {
2020-01-18 23:55:10 +08:00
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
packageVersion,
hash: currentVersion,
buildTime,
2020-08-31 11:47:08 +08:00
cInfo: {
pushy: require('../package.json').version,
rn: RNVersion,
os: Platform.OS + ' ' + Platform.Version,
uuid,
},
2020-01-18 23:55:10 +08:00
}),
});
} catch (e) {
if (isRetry) {
throw new Error('Could not connect to pushy server');
}
2020-07-28 23:15:42 +08:00
await tryBackupEndpoints(APPKEY);
2020-01-18 23:55:10 +08:00
return checkUpdate(APPKEY, true);
}
2020-08-31 18:39:03 +08:00
const result = await resp.json();
checkOperation(result.op);
2020-08-31 01:17:28 +08:00
2016-04-04 23:02:28 +08:00
if (resp.status !== 200) {
2020-08-31 18:39:03 +08:00
throw new Error(result.message);
2016-04-04 23:02:28 +08:00
}
2020-08-31 18:39:03 +08:00
return result;
2016-04-04 23:02:28 +08:00
}
2020-08-31 18:39:03 +08:00
function checkOperation(op) {
if (!Array.isArray(op)) {
2020-08-31 01:17:28 +08:00
return;
}
2020-08-31 18:39:03 +08:00
op.forEach((action) => {
2020-08-31 01:17:28 +08:00
if (action.type === 'block') {
blockUpdate = {
reason: action.reason,
2020-08-31 18:39:03 +08:00
until: (Date.now() + action.duration) / 1000,
2020-08-31 01:17:28 +08:00
};
Pushy.setBlockUpdate(blockUpdate);
}
});
}
2020-09-16 13:01:14 +08:00
export async function downloadUpdate(options, eventListeners) {
2019-10-04 22:27:33 +08:00
assertRelease();
2016-04-04 23:02:28 +08:00
if (!options.update) {
return;
}
2020-09-16 13:01:14 +08:00
if (eventListeners) {
if (eventListeners.onDownloadProgress) {
const downloadCallback = eventListeners.onDownloadProgress;
eventEmitter.addListener('RCTPushyDownloadProgress', (progressData) => {
if (progressData.hash === options.hash) {
downloadCallback(progressData);
}
});
}
}
2016-04-05 15:36:05 +08:00
if (options.diffUrl) {
2020-09-16 13:01:14 +08:00
logger('downloading diff');
2019-11-16 00:31:30 +08:00
await Pushy.downloadPatchFromPpk({
2016-04-04 23:02:28 +08:00
updateUrl: options.diffUrl,
hashName: options.hash,
2016-04-06 00:14:36 +08:00
originHashName: currentVersion,
2016-04-04 23:02:28 +08:00
});
2016-04-05 15:36:05 +08:00
} else if (options.pdiffUrl) {
2020-09-16 13:01:14 +08:00
logger('downloading pdiff');
2019-11-16 00:31:30 +08:00
await Pushy.downloadPatchFromPackage({
2016-04-04 23:02:28 +08:00
updateUrl: options.pdiffUrl,
hashName: options.hash,
});
}
2020-09-16 13:01:14 +08:00
eventEmitter.removeAllListeners('RCTPushyDownloadProgress');
2016-04-05 17:55:04 +08:00
return options.hash;
2016-04-04 23:02:28 +08:00
}
2019-10-04 22:27:33 +08:00
export function switchVersion(hash) {
assertRelease();
2020-09-16 13:01:14 +08:00
logger('switchVersion');
2019-11-16 00:31:30 +08:00
Pushy.reloadUpdate({ hashName: hash });
2016-04-04 23:02:28 +08:00
}
2019-10-04 22:27:33 +08:00
export function switchVersionLater(hash) {
assertRelease();
2020-09-16 13:01:14 +08:00
logger('switchVersionLater');
2019-11-16 00:31:30 +08:00
Pushy.setNeedUpdate({ hashName: hash });
2016-04-04 23:02:28 +08:00
}
2016-04-04 23:45:29 +08:00
2016-04-05 17:00:03 +08:00
export function markSuccess() {
2019-10-04 22:27:33 +08:00
assertRelease();
2020-09-16 13:01:14 +08:00
logger('markSuccess');
2019-11-16 00:31:30 +08:00
Pushy.markSuccess();
2016-04-04 23:45:29 +08:00
}