1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee

feat: test urls

This commit is contained in:
sunnylqm 2024-03-02 12:24:41 +08:00
parent c63e1501fe
commit 84ef668102
No known key found for this signature in database
3 changed files with 34 additions and 3 deletions

View File

@ -1,5 +1,5 @@
import { CheckResult, PushyOptions, ProgressData, EventType } from './type'; import { CheckResult, PushyOptions, ProgressData, EventType } from './type';
import { assertRelease, log } from './utils'; import { assertRelease, log, testUrls } from './utils';
import { import {
EmitterSubscription, EmitterSubscription,
PermissionsAndroid, PermissionsAndroid,
@ -225,8 +225,18 @@ export class Pushy {
onDownloadProgress?: (data: ProgressData) => void, onDownloadProgress?: (data: ProgressData) => void,
) => { ) => {
assertRelease(); assertRelease();
const { hash, diffUrl, pdiffUrl, updateUrl, name, description, metaInfo } = const {
info; hash,
diffUrl: _diffUrl,
diffUrls,
pdiffUrl: _pdiffUrl,
pdiffUrls,
updateUrl: _updateUrl,
updateUrls,
name,
description,
metaInfo,
} = info;
if (!info.update || !hash) { if (!info.update || !hash) {
return; return;
} }
@ -253,6 +263,7 @@ export class Pushy {
} }
let succeeded = false; let succeeded = false;
this.report({ type: 'downloading' }); this.report({ type: 'downloading' });
const diffUrl = (await testUrls(diffUrls)) || _diffUrl;
if (diffUrl) { if (diffUrl) {
log('downloading diff'); log('downloading diff');
try { try {
@ -266,6 +277,7 @@ export class Pushy {
log(`diff error: ${e.message}, try pdiff`); log(`diff error: ${e.message}, try pdiff`);
} }
} }
const pdiffUrl = (await testUrls(pdiffUrls)) || _pdiffUrl;
if (!succeeded && pdiffUrl) { if (!succeeded && pdiffUrl) {
log('downloading pdiff'); log('downloading pdiff');
try { try {
@ -278,6 +290,7 @@ export class Pushy {
log(`pdiff error: ${e.message}, try full patch`); log(`pdiff error: ${e.message}, try full patch`);
} }
} }
const updateUrl = (await testUrls(updateUrls)) || _updateUrl;
if (!succeeded && updateUrl) { if (!succeeded && updateUrl) {
log('downloading full patch'); log('downloading full patch');
try { try {

View File

@ -8,8 +8,11 @@ export interface CheckResult {
description?: string; description?: string;
metaInfo?: string; metaInfo?: string;
pdiffUrl?: string; pdiffUrl?: string;
pdiffUrls?: string[];
diffUrl?: string; diffUrl?: string;
diffUrls?: string[];
updateUrl?: string; updateUrl?: string;
updateUrls?: string[];
paused?: 'app' | 'package'; paused?: 'app' | 'package';
message?: string; message?: string;
} }

View File

@ -7,3 +7,18 @@ export function assertRelease() {
throw new Error('react-native-update 只能在 RELEASE 版本中运行.'); 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)));
};