mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-09-18 19:10:38 +08:00
Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
23346a5f1d | ||
![]() |
5aca2104c2 | ||
![]() |
fe0a05db3d | ||
![]() |
2b287786ff | ||
![]() |
7d128900cd | ||
![]() |
189e3ec78e |
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,6 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?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
|
||||||
|
@@ -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 onEvents = noop;
|
||||||
|
73
lib/main.ts
73
lib/main.ts
@@ -1,5 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
tryBackupEndpoints,
|
updateBackupEndpoints,
|
||||||
getCheckUrl,
|
getCheckUrl,
|
||||||
setCustomEndpoints,
|
setCustomEndpoints,
|
||||||
} from './endpoint';
|
} from './endpoint';
|
||||||
@@ -16,6 +16,7 @@ import {
|
|||||||
UpdateAvailableResult,
|
UpdateAvailableResult,
|
||||||
UpdateEventsListener,
|
UpdateEventsListener,
|
||||||
} from './type';
|
} from './type';
|
||||||
|
import { assertRelease, logger } from './utils';
|
||||||
export { setCustomEndpoints };
|
export { setCustomEndpoints };
|
||||||
const {
|
const {
|
||||||
version: v,
|
version: v,
|
||||||
@@ -74,10 +75,6 @@ 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;
|
||||||
|
|
||||||
@@ -125,21 +122,15 @@ export const cInfo = {
|
|||||||
uuid,
|
uuid,
|
||||||
};
|
};
|
||||||
|
|
||||||
function assertRelease() {
|
|
||||||
if (__DEV__) {
|
|
||||||
throw new Error('react-native-update 只能在 RELEASE 版本中运行.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let lastChecking;
|
let lastChecking;
|
||||||
const empty = {};
|
const empty = {};
|
||||||
let lastResult: CheckResult;
|
let lastResult: CheckResult;
|
||||||
export async function checkUpdate(APPKEY: string, isRetry?: boolean) {
|
export async function checkUpdate(APPKEY: string) {
|
||||||
assertRelease();
|
assertRelease();
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
if (lastResult && lastChecking && now - lastChecking < 1000 * 60) {
|
if (lastResult && lastChecking && now - lastChecking < 1000 * 60) {
|
||||||
// logger('repeated checking, ignored');
|
// logger('repeated checking, ignored');
|
||||||
return lastResult || empty;
|
return lastResult;
|
||||||
}
|
}
|
||||||
lastChecking = now;
|
lastChecking = now;
|
||||||
if (blockUpdate && blockUpdate.until > Date.now() / 1000) {
|
if (blockUpdate && blockUpdate.until > Date.now() / 1000) {
|
||||||
@@ -152,31 +143,44 @@ export async function checkUpdate(APPKEY: string, isRetry?: boolean) {
|
|||||||
return lastResult || empty;
|
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({
|
||||||
report({
|
type: 'errorChecking',
|
||||||
type: 'errorChecking',
|
message: '无法连接主更新服务器,尝试备用节点',
|
||||||
message: '无法连接更新服务器,请检查网络连接后重试',
|
});
|
||||||
});
|
const backupEndpoints = await updateBackupEndpoints();
|
||||||
return lastResult || empty;
|
if (backupEndpoints) {
|
||||||
|
try {
|
||||||
|
resp = await Promise.race(
|
||||||
|
backupEndpoints.map((endpoint) =>
|
||||||
|
fetch(getCheckUrl(APPKEY, endpoint), fetchPayload),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} catch {}
|
||||||
}
|
}
|
||||||
await tryBackupEndpoints();
|
}
|
||||||
return checkUpdate(APPKEY, true);
|
if (!resp) {
|
||||||
|
report({
|
||||||
|
type: 'errorChecking',
|
||||||
|
message: '无法连接更新服务器,请检查网络连接后重试',
|
||||||
|
});
|
||||||
|
return lastResult || empty;
|
||||||
}
|
}
|
||||||
const result: CheckResult = await resp.json();
|
const result: CheckResult = await resp.json();
|
||||||
|
|
||||||
@@ -190,7 +194,6 @@ export async function checkUpdate(APPKEY: string, isRetry?: boolean) {
|
|||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
message: result.message,
|
message: result.message,
|
||||||
});
|
});
|
||||||
return lastResult;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
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.5",
|
"version": "9.1.2",
|
||||||
"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