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

127 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-08-13 00:32:07 +08:00
import {
tryBackupEndpoints,
getCheckUrl,
setCustomEndpoints,
} from './endpoint';
import { NativeAppEventEmitter, NativeModules, Platform } from 'react-native';
2020-07-28 23:15:42 +08:00
export { setCustomEndpoints };
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;
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的配置',
);
}
2016-04-04 23:02:28 +08:00
/*
Return json:
Package was expired:
{
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-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,
}),
});
} 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);
}
2016-04-04 23:02:28 +08:00
if (resp.status !== 200) {
throw new Error((await resp.json()).message);
2016-04-04 23:02:28 +08:00
}
2019-10-04 22:27:33 +08:00
return resp.json();
2016-04-04 23:02:28 +08:00
}
export async function downloadUpdate(options) {
2019-10-04 22:27:33 +08:00
assertRelease();
2016-04-04 23:02:28 +08:00
if (!options.update) {
return;
}
2016-04-05 15:36:05 +08:00
if (options.diffUrl) {
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) {
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,
});
}
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();
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();
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();
2019-11-16 00:31:30 +08:00
Pushy.markSuccess();
2016-04-04 23:45:29 +08:00
}
2016-04-06 09:47:26 +08:00
2020-08-13 00:32:07 +08:00
NativeAppEventEmitter.addListener('RCTPushyDownloadProgress', (params) => {});
2016-04-06 09:47:26 +08:00
2020-08-13 00:32:07 +08:00
NativeAppEventEmitter.addListener('RCTPushyUnzipProgress', (params) => {});