mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-09-18 23:50:39 +08:00
Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
af932665fe | ||
![]() |
d1920cdb4f | ||
![]() |
3fe3daf599 | ||
![]() |
7fea63b2eb | ||
![]() |
2630096760 | ||
![]() |
baebeff7cb | ||
![]() |
c7b78f0d46 | ||
![]() |
21f2c3918e |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -44,3 +44,4 @@ npm-debug.log
|
|||||||
Example/**/update.json
|
Example/**/update.json
|
||||||
yarn-error.log
|
yarn-error.log
|
||||||
Example/testHotUpdate/.pushy
|
Example/testHotUpdate/.pushy
|
||||||
|
dist/
|
||||||
|
@@ -1,13 +1,21 @@
|
|||||||
import { logger } from './utils';
|
import { logger, promiseAny } 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 backupEndpoints: string[] = [
|
||||||
let backupEndpointsQueryUrl: string | null = null;
|
'https://pushy-koa-qgbgqmcpis.cn-beijing.fcapp.run',
|
||||||
|
'https://update.reactnative.cn/api',
|
||||||
|
];
|
||||||
|
let backupEndpointsQueryUrls = [
|
||||||
|
'https://gitee.com/sunnylqm/react-native-pushy/raw/master/endpoints.json',
|
||||||
|
'https://cdn.jsdelivr.net/gh/reactnativecn/react-native-pushy@master/endpoints.json',
|
||||||
|
];
|
||||||
|
|
||||||
export async function updateBackupEndpoints() {
|
export async function updateBackupEndpoints() {
|
||||||
if (backupEndpointsQueryUrl) {
|
if (backupEndpointsQueryUrls) {
|
||||||
try {
|
try {
|
||||||
const resp = await fetch(backupEndpointsQueryUrl);
|
const resp = await promiseAny(
|
||||||
|
backupEndpointsQueryUrls.map((queryUrl) => fetch(queryUrl)),
|
||||||
|
);
|
||||||
const remoteEndpoints = await resp.json();
|
const remoteEndpoints = await resp.json();
|
||||||
if (Array.isArray(remoteEndpoints)) {
|
if (Array.isArray(remoteEndpoints)) {
|
||||||
backupEndpoints = Array.from(
|
backupEndpoints = Array.from(
|
||||||
@@ -16,7 +24,7 @@ export async function updateBackupEndpoints() {
|
|||||||
logger('fetch remote endpoints:', remoteEndpoints);
|
logger('fetch remote endpoints:', remoteEndpoints);
|
||||||
logger('merged backup endpoints:', backupEndpoints);
|
logger('merged backup endpoints:', backupEndpoints);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e: any) {
|
||||||
logger('fetch remote endpoints failed');
|
logger('fetch remote endpoints failed');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -36,18 +44,17 @@ export function getCheckUrl(APPKEY, endpoint = currentEndpoint) {
|
|||||||
export function setCustomEndpoints({
|
export function setCustomEndpoints({
|
||||||
main,
|
main,
|
||||||
backups,
|
backups,
|
||||||
backupQueryUrl,
|
backupQueryUrls,
|
||||||
}: {
|
}: {
|
||||||
main: string;
|
main: string;
|
||||||
backups?: string[];
|
backups?: string[];
|
||||||
backupQueryUrl?: string;
|
backupQueryUrls?: string[];
|
||||||
}) {
|
}) {
|
||||||
currentEndpoint = main;
|
currentEndpoint = main;
|
||||||
backupEndpointsQueryUrl = null;
|
|
||||||
if (Array.isArray(backups) && backups.length > 0) {
|
if (Array.isArray(backups) && backups.length > 0) {
|
||||||
backupEndpoints = backups;
|
backupEndpoints = backups;
|
||||||
}
|
}
|
||||||
if (typeof backupQueryUrl === 'string') {
|
if (Array.isArray(backupQueryUrls) && backupQueryUrls.length > 0) {
|
||||||
backupEndpointsQueryUrl = backupQueryUrl;
|
backupEndpointsQueryUrls = backupQueryUrls;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
14
lib/main.ts
14
lib/main.ts
@@ -16,7 +16,7 @@ import {
|
|||||||
UpdateAvailableResult,
|
UpdateAvailableResult,
|
||||||
UpdateEventsListener,
|
UpdateEventsListener,
|
||||||
} from './type';
|
} from './type';
|
||||||
import { assertRelease, logger, testUrls } from './utils';
|
import { assertRelease, logger, promiseAny, testUrls } from './utils';
|
||||||
export { setCustomEndpoints };
|
export { setCustomEndpoints };
|
||||||
const {
|
const {
|
||||||
version: v,
|
version: v,
|
||||||
@@ -154,7 +154,7 @@ export async function checkUpdate(APPKEY: string) {
|
|||||||
let resp;
|
let resp;
|
||||||
try {
|
try {
|
||||||
resp = await fetch(getCheckUrl(APPKEY), fetchPayload);
|
resp = await fetch(getCheckUrl(APPKEY), fetchPayload);
|
||||||
} catch (e) {
|
} catch (e: any) {
|
||||||
report({
|
report({
|
||||||
type: 'errorChecking',
|
type: 'errorChecking',
|
||||||
message: '无法连接主更新服务器,尝试备用节点',
|
message: '无法连接主更新服务器,尝试备用节点',
|
||||||
@@ -162,7 +162,7 @@ export async function checkUpdate(APPKEY: string) {
|
|||||||
const backupEndpoints = await updateBackupEndpoints();
|
const backupEndpoints = await updateBackupEndpoints();
|
||||||
if (backupEndpoints) {
|
if (backupEndpoints) {
|
||||||
try {
|
try {
|
||||||
resp = await Promise.race(
|
resp = await promiseAny(
|
||||||
backupEndpoints.map((endpoint) =>
|
backupEndpoints.map((endpoint) =>
|
||||||
fetch(getCheckUrl(APPKEY, endpoint), fetchPayload),
|
fetch(getCheckUrl(APPKEY, endpoint), fetchPayload),
|
||||||
),
|
),
|
||||||
@@ -265,7 +265,7 @@ export async function downloadUpdate(
|
|||||||
originHash: currentVersion,
|
originHash: currentVersion,
|
||||||
});
|
});
|
||||||
succeeded = true;
|
succeeded = true;
|
||||||
} catch (e) {
|
} catch (e: any) {
|
||||||
logger(`diff error: ${e.message}, try pdiff`);
|
logger(`diff error: ${e.message}, try pdiff`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -279,7 +279,7 @@ export async function downloadUpdate(
|
|||||||
hash: options.hash,
|
hash: options.hash,
|
||||||
});
|
});
|
||||||
succeeded = true;
|
succeeded = true;
|
||||||
} catch (e) {
|
} catch (e: any) {
|
||||||
logger(`pdiff error: ${e.message}, try full patch`);
|
logger(`pdiff error: ${e.message}, try full patch`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -294,7 +294,7 @@ export async function downloadUpdate(
|
|||||||
hash: options.hash,
|
hash: options.hash,
|
||||||
});
|
});
|
||||||
succeeded = true;
|
succeeded = true;
|
||||||
} catch (e) {
|
} catch (e: any) {
|
||||||
logger(`full patch error: ${e.message}`);
|
logger(`full patch error: ${e.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -371,7 +371,7 @@ export async function downloadAndInstallApk({
|
|||||||
if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
|
if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
|
||||||
return report({ type: 'rejectStoragePermission' });
|
return report({ type: 'rejectStoragePermission' });
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err: any) {
|
||||||
return report({ type: 'errorStoragePermission' });
|
return report({ type: 'errorStoragePermission' });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -34,7 +34,7 @@ export function simpleUpdate(
|
|||||||
return __DEV__
|
return __DEV__
|
||||||
? WrappedComponent
|
? WrappedComponent
|
||||||
: class AppUpdate extends PureComponent {
|
: class AppUpdate extends PureComponent {
|
||||||
stateListener: NativeEventSubscription;
|
stateListener: NativeEventSubscription | null = null;
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
if (isRolledBack) {
|
if (isRolledBack) {
|
||||||
Alert.alert('抱歉', '刚刚更新遭遇错误,已为您恢复到更新前版本');
|
Alert.alert('抱歉', '刚刚更新遭遇错误,已为您恢复到更新前版本');
|
||||||
@@ -77,7 +77,7 @@ export function simpleUpdate(
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
} catch (err) {
|
} catch (err: any) {
|
||||||
Alert.alert('更新失败', err.message);
|
Alert.alert('更新失败', err.message);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -86,7 +86,7 @@ export function simpleUpdate(
|
|||||||
let info;
|
let info;
|
||||||
try {
|
try {
|
||||||
info = await checkUpdate(appKey!);
|
info = await checkUpdate(appKey!);
|
||||||
} catch (err) {
|
} catch (err: any) {
|
||||||
Alert.alert('更新检查失败', err.message);
|
Alert.alert('更新检查失败', err.message);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
59
lib/utils.ts
59
lib/utils.ts
@@ -1,4 +1,21 @@
|
|||||||
import { Platform } from "react-native";
|
import { Platform } from 'react-native';
|
||||||
|
|
||||||
|
export function promiseAny<T>(promises: Promise<T>[]) {
|
||||||
|
return new Promise<T>((resolve, reject) => {
|
||||||
|
let count = 0;
|
||||||
|
|
||||||
|
promises.forEach((promise) => {
|
||||||
|
Promise.resolve(promise)
|
||||||
|
.then(resolve)
|
||||||
|
.catch(() => {
|
||||||
|
count++;
|
||||||
|
if (count === promises.length) {
|
||||||
|
reject(new Error('All promises were rejected'));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export function logger(...args: any[]) {
|
export function logger(...args: any[]) {
|
||||||
console.log('Pushy: ', ...args);
|
console.log('Pushy: ', ...args);
|
||||||
@@ -13,20 +30,46 @@ export function assertRelease() {
|
|||||||
const ping =
|
const ping =
|
||||||
Platform.OS === 'web'
|
Platform.OS === 'web'
|
||||||
? Promise.resolve
|
? Promise.resolve
|
||||||
: async (url: string) =>
|
: async (url: string) => {
|
||||||
Promise.race([
|
let pingFinished = false;
|
||||||
|
return Promise.race([
|
||||||
fetch(url, {
|
fetch(url, {
|
||||||
method: 'HEAD',
|
method: 'HEAD',
|
||||||
})
|
})
|
||||||
.then(({ status }) => (status === 200 ? url : null))
|
.then(({ status, statusText }) => {
|
||||||
.catch(() => null),
|
pingFinished = true;
|
||||||
new Promise(r => setTimeout(() => r(null), 2000)),
|
if (status === 200) {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
logger('ping failed', url, status, statusText);
|
||||||
|
throw new Error('Ping failed');
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
pingFinished = true;
|
||||||
|
logger('ping error', url, e);
|
||||||
|
throw e;
|
||||||
|
}),
|
||||||
|
new Promise((_, reject) =>
|
||||||
|
setTimeout(() => {
|
||||||
|
reject(new Error('Ping timeout'));
|
||||||
|
if (!pingFinished) {
|
||||||
|
logger('ping timeout', url);
|
||||||
|
}
|
||||||
|
}, 2000),
|
||||||
|
),
|
||||||
]);
|
]);
|
||||||
|
};
|
||||||
|
|
||||||
export const testUrls = async (urls?: string[]) => {
|
export const testUrls = async (urls?: string[]) => {
|
||||||
if (!urls?.length) {
|
if (!urls?.length) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return Promise.race(urls.map(ping)).catch(() => null);
|
try {
|
||||||
|
const ret = await promiseAny(urls.map(ping));
|
||||||
|
if (ret) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
} catch {}
|
||||||
|
logger('all ping failed, use first url:', urls[0]);
|
||||||
|
return urls[0];
|
||||||
};
|
};
|
||||||
|
14
package.json
14
package.json
@@ -1,10 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "react-native-update",
|
"name": "react-native-update",
|
||||||
"version": "8.5.1",
|
"version": "8.5.6",
|
||||||
"description": "react-native hot update",
|
"description": "react-native hot update",
|
||||||
"main": "lib/index.ts",
|
"main": "dist/index.js",
|
||||||
|
"types": "dist/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"prepublish": "yarn submodule",
|
"prepublishOnly": "yarn submodule && yarn build",
|
||||||
|
"build": "yarn clean && tsc",
|
||||||
|
"clean": "rm -rf dist",
|
||||||
"submodule": "git submodule update --init --recursive",
|
"submodule": "git submodule update --init --recursive",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
"build-lib": "yarn submodule && $ANDROID_HOME/ndk/20.1.5948944/ndk-build NDK_PROJECT_PATH=android APP_BUILD_SCRIPT=android/jni/Android.mk NDK_APPLICATION_MK=android/jni/Application.mk NDK_LIBS_OUT=android/lib"
|
"build-lib": "yarn submodule && $ANDROID_HOME/ndk/20.1.5948944/ndk-build NDK_PROJECT_PATH=android APP_BUILD_SCRIPT=android/jni/Android.mk NDK_APPLICATION_MK=android/jni/Application.mk NDK_LIBS_OUT=android/lib"
|
||||||
@@ -25,7 +28,7 @@
|
|||||||
"url": "https://github.com/reactnativecn/react-native-pushy/issues"
|
"url": "https://github.com/reactnativecn/react-native-pushy/issues"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"react-native": ">=0.57.0"
|
"react-native": "*"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/reactnativecn/react-native-pushy#readme",
|
"homepage": "https://github.com/reactnativecn/react-native-pushy#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@@ -36,5 +39,6 @@
|
|||||||
"@types/react": "^18.2.33",
|
"@types/react": "^18.2.33",
|
||||||
"react-native": "^0.72.6",
|
"react-native": "^0.72.6",
|
||||||
"typescript": "^5.2.2"
|
"typescript": "^5.2.2"
|
||||||
}
|
},
|
||||||
|
"packageManager": "yarn@1.22.21+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72"
|
||||||
}
|
}
|
||||||
|
18
tsconfig.json
Normal file
18
tsconfig.json
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "es2016",
|
||||||
|
"module": "commonjs",
|
||||||
|
"declaration": true,
|
||||||
|
"outDir": "./dist",
|
||||||
|
"strict": true,
|
||||||
|
"noImplicitAny": false,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"forceConsistentCasingInFileNames": true,
|
||||||
|
"jsx": "react-native",
|
||||||
|
"lib": ["es2016", "dom"],
|
||||||
|
"moduleResolution": "node"
|
||||||
|
},
|
||||||
|
"include": ["lib/**/*"],
|
||||||
|
"exclude": ["node_modules", "dist", "Example"]
|
||||||
|
}
|
Reference in New Issue
Block a user