1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-09-16 13:01:38 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee

add testurls

This commit is contained in:
sunnylqm
2024-07-26 15:28:13 +08:00
parent 41af560a39
commit fcfc8c3dbb

View File

@@ -1,3 +1,5 @@
import { Platform } from "react-native";
export function logger(...args: any[]) {
console.log('Pushy: ', ...args);
}
@@ -7,3 +9,25 @@ export function assertRelease() {
throw new Error('react-native-update 只能在 RELEASE 版本中运行.');
}
}
const ping =
Platform.OS === 'web'
? Promise.resolve
: async (url: string) =>
Promise.race([
fetch(url, {
method: 'HEAD',
})
.then(({ status }) => (status === 200 ? url : null))
.catch(() => null),
new Promise(r => setTimeout(() => r(null), 2000)),
]);
const canUseGoogle = ping('https://www.google.com');
export const testUrls = async (urls?: string[]) => {
if (!urls?.length || (await canUseGoogle)) {
return null;
}
return Promise.race(urls.map(ping)).catch(() => null);
};