react-native-update 10.1.3 → 10.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-update",
3
- "version": "10.1.3",
3
+ "version": "10.2.1",
4
4
  "description": "react-native hot update",
5
5
  "main": "src/index.ts",
6
6
  "scripts": {
package/src/client.tsx CHANGED
@@ -1,5 +1,5 @@
1
1
  import { CheckResult, PushyOptions, ProgressData, EventType } from './type';
2
- import { assertRelease, log } from './utils';
2
+ import { assertRelease, log, testUrls } from './utils';
3
3
  import {
4
4
  EmitterSubscription,
5
5
  PermissionsAndroid,
@@ -225,8 +225,18 @@ export class Pushy {
225
225
  onDownloadProgress?: (data: ProgressData) => void,
226
226
  ) => {
227
227
  assertRelease();
228
- const { hash, diffUrl, pdiffUrl, updateUrl, name, description, metaInfo } =
229
- info;
228
+ const {
229
+ hash,
230
+ diffUrl: _diffUrl,
231
+ diffUrls,
232
+ pdiffUrl: _pdiffUrl,
233
+ pdiffUrls,
234
+ updateUrl: _updateUrl,
235
+ updateUrls,
236
+ name,
237
+ description,
238
+ metaInfo,
239
+ } = info;
230
240
  if (!info.update || !hash) {
231
241
  return;
232
242
  }
@@ -253,6 +263,7 @@ export class Pushy {
253
263
  }
254
264
  let succeeded = false;
255
265
  this.report({ type: 'downloading' });
266
+ const diffUrl = (await testUrls(diffUrls)) || _diffUrl;
256
267
  if (diffUrl) {
257
268
  log('downloading diff');
258
269
  try {
@@ -266,6 +277,7 @@ export class Pushy {
266
277
  log(`diff error: ${e.message}, try pdiff`);
267
278
  }
268
279
  }
280
+ const pdiffUrl = (await testUrls(pdiffUrls)) || _pdiffUrl;
269
281
  if (!succeeded && pdiffUrl) {
270
282
  log('downloading pdiff');
271
283
  try {
@@ -278,6 +290,7 @@ export class Pushy {
278
290
  log(`pdiff error: ${e.message}, try full patch`);
279
291
  }
280
292
  }
293
+ const updateUrl = (await testUrls(updateUrls)) || _updateUrl;
281
294
  if (!succeeded && updateUrl) {
282
295
  log('downloading full patch');
283
296
  try {
package/src/type.ts CHANGED
@@ -8,8 +8,11 @@ export interface CheckResult {
8
8
  description?: string;
9
9
  metaInfo?: string;
10
10
  pdiffUrl?: string;
11
+ pdiffUrls?: string[];
11
12
  diffUrl?: string;
13
+ diffUrls?: string[];
12
14
  updateUrl?: string;
15
+ updateUrls?: string[];
13
16
  paused?: 'app' | 'package';
14
17
  message?: string;
15
18
  }
package/src/utils.ts CHANGED
@@ -7,3 +7,20 @@ export function assertRelease() {
7
7
  throw new Error('react-native-update 只能在 RELEASE 版本中运行.');
8
8
  }
9
9
  }
10
+
11
+ const ping = async (url: string) =>
12
+ fetch(url, {
13
+ method: 'HEAD',
14
+ redirect: 'follow',
15
+ }).then(({ status }) => status === 200);
16
+
17
+ const canUseGoogle = ping('https://www.google.com');
18
+
19
+ export const testUrls = async (urls?: string[]) => {
20
+ if (!urls?.length || (await canUseGoogle)) {
21
+ return null;
22
+ }
23
+ return Promise.race(urls.map((url) => ping(url).then(() => url))).catch(
24
+ () => null,
25
+ );
26
+ };