Skip to content

Commit

Permalink
feat: test urls
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnylqm committed Mar 2, 2024
1 parent c63e150 commit 84ef668
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/client.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
15 changes: 15 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
};

0 comments on commit 84ef668

Please sign in to comment.