mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-10-28 20:13:10 +08:00
feat: implement enhancedFetch for improved error handling and fallback support
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -46,3 +46,4 @@ Example/**/.pushy
|
|||||||
Example/testHotUpdate/artifacts
|
Example/testHotUpdate/artifacts
|
||||||
|
|
||||||
yarn-error.log
|
yarn-error.log
|
||||||
|
.cursor/mcp.json
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import {
|
|||||||
UpdateAvailableResult,
|
UpdateAvailableResult,
|
||||||
UpdateEventsListener,
|
UpdateEventsListener,
|
||||||
} from './type';
|
} from './type';
|
||||||
import { assertRelease, logger, promiseAny, testUrls } from './utils';
|
import { assertRelease, enhancedFetch, logger, promiseAny, testUrls } from './utils';
|
||||||
export { setCustomEndpoints };
|
export { setCustomEndpoints };
|
||||||
const {
|
const {
|
||||||
version: v,
|
version: v,
|
||||||
@@ -158,7 +158,7 @@ export async function checkUpdate(APPKEY: string) {
|
|||||||
};
|
};
|
||||||
let resp;
|
let resp;
|
||||||
try {
|
try {
|
||||||
resp = await fetch(getCheckUrl(APPKEY), fetchPayload);
|
resp = await enhancedFetch(getCheckUrl(APPKEY), fetchPayload);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
report({
|
report({
|
||||||
type: 'errorChecking',
|
type: 'errorChecking',
|
||||||
@@ -169,7 +169,7 @@ export async function checkUpdate(APPKEY: string) {
|
|||||||
try {
|
try {
|
||||||
resp = await promiseAny(
|
resp = await promiseAny(
|
||||||
backupEndpoints.map(endpoint =>
|
backupEndpoints.map(endpoint =>
|
||||||
fetch(getCheckUrl(APPKEY, endpoint), fetchPayload),
|
enhancedFetch(getCheckUrl(APPKEY, endpoint), fetchPayload),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
} catch {}
|
} catch {}
|
||||||
|
|||||||
33
lib/utils.ts
33
lib/utils.ts
@@ -33,7 +33,7 @@ const ping =
|
|||||||
: async (url: string) => {
|
: async (url: string) => {
|
||||||
let pingFinished = false;
|
let pingFinished = false;
|
||||||
return Promise.race([
|
return Promise.race([
|
||||||
fetch(url, {
|
enhancedFetch(url, {
|
||||||
method: 'HEAD',
|
method: 'HEAD',
|
||||||
})
|
})
|
||||||
.then(({ status, statusText }) => {
|
.then(({ status, statusText }) => {
|
||||||
@@ -73,3 +73,34 @@ export const testUrls = async (urls?: string[]) => {
|
|||||||
logger('all ping failed, use first url:', urls[0]);
|
logger('all ping failed, use first url:', urls[0]);
|
||||||
return urls[0];
|
return urls[0];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// export const isAndroid70AndBelow = () => {
|
||||||
|
// // android 7.0 and below devices do not support letsencrypt cert
|
||||||
|
// // https://letsencrypt.org/2023/07/10/cross-sign-expiration/
|
||||||
|
// return Platform.OS === 'android' && Platform.Version <= 24;
|
||||||
|
// };
|
||||||
|
|
||||||
|
export const enhancedFetch = async (
|
||||||
|
url: string,
|
||||||
|
params: Parameters<typeof fetch>[1],
|
||||||
|
isRetry = false,
|
||||||
|
) => {
|
||||||
|
return fetch(url, params)
|
||||||
|
.then((r) => {
|
||||||
|
if (r.ok) {
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
throw new Error(`${r.status} ${r.statusText}`);
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
logger('fetch error', url, e);
|
||||||
|
if (isRetry) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
logger('trying fallback to http');
|
||||||
|
return enhancedFetch(url.replace('https', 'http'), params, true);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "react-native-update",
|
"name": "react-native-update",
|
||||||
"version": "9.2.5",
|
"version": "9.2.6",
|
||||||
"description": "react-native hot update",
|
"description": "react-native hot update",
|
||||||
"main": "lib/index.ts",
|
"main": "lib/index.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
Reference in New Issue
Block a user