mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-11-01 13:53:11 +08:00
add testurls
This commit is contained in:
24
lib/utils.ts
24
lib/utils.ts
@@ -1,3 +1,5 @@
|
|||||||
|
import { Platform } from "react-native";
|
||||||
|
|
||||||
export function logger(...args: any[]) {
|
export function logger(...args: any[]) {
|
||||||
console.log('Pushy: ', ...args);
|
console.log('Pushy: ', ...args);
|
||||||
}
|
}
|
||||||
@@ -7,3 +9,25 @@ export function assertRelease() {
|
|||||||
throw new Error('react-native-update 只能在 RELEASE 版本中运行.');
|
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);
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user