feat: test urls
This commit is contained in:
parent
c63e1501fe
commit
84ef668102
@ -1,5 +1,5 @@
|
||||
import { CheckResult, PushyOptions, ProgressData, EventType } from './type';
|
||||
import { assertRelease, log } from './utils';
|
||||
import { assertRelease, log, testUrls } from './utils';
|
||||
import {
|
||||
EmitterSubscription,
|
||||
PermissionsAndroid,
|
||||
@ -225,8 +225,18 @@ export class Pushy {
|
||||
onDownloadProgress?: (data: ProgressData) => void,
|
||||
) => {
|
||||
assertRelease();
|
||||
const { hash, diffUrl, pdiffUrl, updateUrl, name, description, metaInfo } =
|
||||
info;
|
||||
const {
|
||||
hash,
|
||||
diffUrl: _diffUrl,
|
||||
diffUrls,
|
||||
pdiffUrl: _pdiffUrl,
|
||||
pdiffUrls,
|
||||
updateUrl: _updateUrl,
|
||||
updateUrls,
|
||||
name,
|
||||
description,
|
||||
metaInfo,
|
||||
} = info;
|
||||
if (!info.update || !hash) {
|
||||
return;
|
||||
}
|
||||
@ -253,6 +263,7 @@ export class Pushy {
|
||||
}
|
||||
let succeeded = false;
|
||||
this.report({ type: 'downloading' });
|
||||
const diffUrl = (await testUrls(diffUrls)) || _diffUrl;
|
||||
if (diffUrl) {
|
||||
log('downloading diff');
|
||||
try {
|
||||
@ -266,6 +277,7 @@ export class Pushy {
|
||||
log(`diff error: ${e.message}, try pdiff`);
|
||||
}
|
||||
}
|
||||
const pdiffUrl = (await testUrls(pdiffUrls)) || _pdiffUrl;
|
||||
if (!succeeded && pdiffUrl) {
|
||||
log('downloading pdiff');
|
||||
try {
|
||||
@ -278,6 +290,7 @@ export class Pushy {
|
||||
log(`pdiff error: ${e.message}, try full patch`);
|
||||
}
|
||||
}
|
||||
const updateUrl = (await testUrls(updateUrls)) || _updateUrl;
|
||||
if (!succeeded && updateUrl) {
|
||||
log('downloading full patch');
|
||||
try {
|
||||
|
@ -8,8 +8,11 @@ export interface CheckResult {
|
||||
description?: string;
|
||||
metaInfo?: string;
|
||||
pdiffUrl?: string;
|
||||
pdiffUrls?: string[];
|
||||
diffUrl?: string;
|
||||
diffUrls?: string[];
|
||||
updateUrl?: string;
|
||||
updateUrls?: string[];
|
||||
paused?: 'app' | 'package';
|
||||
message?: string;
|
||||
}
|
||||
|
15
src/utils.ts
15
src/utils.ts
@ -7,3 +7,18 @@ export function assertRelease() {
|
||||
throw new Error('react-native-update 只能在 RELEASE 版本中运行.');
|
||||
}
|
||||
}
|
||||
|
||||
const ping = async (url: string) =>
|
||||
fetch(url, {
|
||||
method: 'HEAD',
|
||||
redirect: 'follow',
|
||||
}).then(({ status }) => status === 200);
|
||||
|
||||
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((url) => ping(url).then(() => url)));
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user