react-native-update 9.1.6 → 9.2.0

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.
@@ -57,7 +57,7 @@ public class UpdateModule extends NativeUpdateSpec {
57
57
 
58
58
  @Override
59
59
  public void downloadFullUpdate(ReadableMap options, final Promise promise) {
60
- UpdateModuleImpl.downloadFullUpdate(this.updateContext,options,promise);
60
+ UpdateModuleImpl.downloadFullUpdate(this.updateContext,options,promise);
61
61
  }
62
62
 
63
63
  @Override
package/lib/endpoint.ts CHANGED
@@ -1,13 +1,21 @@
1
1
  import { logger } from './utils';
2
2
 
3
3
  let currentEndpoint = 'https://update.react-native.cn/api';
4
- let backupEndpoints: string[] = ['https://update.reactnative.cn/api'];
5
- let backupEndpointsQueryUrl: string | null = null;
4
+ let backupEndpoints: string[] = [
5
+ 'https://pushy-koa-qgbgqmcpis.cn-beijing.fcapp.run',
6
+ 'https://update.reactnative.cn/api',
7
+ ];
8
+ let backupEndpointsQueryUrls = [
9
+ 'https://gitee.com/sunnylqm/react-native-pushy/raw/master/endpoints.json',
10
+ 'https://cdn.jsdelivr.net/gh/reactnativecn/react-native-pushy@master/endpoints.json',
11
+ ];
6
12
 
7
13
  export async function updateBackupEndpoints() {
8
- if (backupEndpointsQueryUrl) {
14
+ if (backupEndpointsQueryUrls) {
9
15
  try {
10
- const resp = await fetch(backupEndpointsQueryUrl);
16
+ const resp = await Promise.race(
17
+ backupEndpointsQueryUrls.map((queryUrl) => fetch(queryUrl)),
18
+ );
11
19
  const remoteEndpoints = await resp.json();
12
20
  if (Array.isArray(remoteEndpoints)) {
13
21
  backupEndpoints = Array.from(
@@ -36,18 +44,17 @@ export function getCheckUrl(APPKEY, endpoint = currentEndpoint) {
36
44
  export function setCustomEndpoints({
37
45
  main,
38
46
  backups,
39
- backupQueryUrl,
47
+ backupQueryUrls,
40
48
  }: {
41
49
  main: string;
42
50
  backups?: string[];
43
- backupQueryUrl?: string;
51
+ backupQueryUrls?: string[];
44
52
  }) {
45
53
  currentEndpoint = main;
46
- backupEndpointsQueryUrl = null;
47
54
  if (Array.isArray(backups) && backups.length > 0) {
48
55
  backupEndpoints = backups;
49
56
  }
50
- if (typeof backupQueryUrl === 'string') {
51
- backupEndpointsQueryUrl = backupQueryUrl;
57
+ if (Array.isArray(backupQueryUrls) && backupQueryUrls.length > 0) {
58
+ backupEndpointsQueryUrls = backupQueryUrls;
52
59
  }
53
60
  }
package/lib/main.ts CHANGED
@@ -16,7 +16,7 @@ import {
16
16
  UpdateAvailableResult,
17
17
  UpdateEventsListener,
18
18
  } from './type';
19
- import { assertRelease, logger } from './utils';
19
+ import { assertRelease, logger, testUrls } from './utils';
20
20
  export { setCustomEndpoints };
21
21
  const {
22
22
  version: v,
@@ -260,11 +260,12 @@ export async function downloadUpdate(
260
260
  }
261
261
  let succeeded = false;
262
262
  report({ type: 'downloading' });
263
- if (options.diffUrl) {
263
+ const diffUrl = (await testUrls(options.diffUrls)) || options.diffUrl;
264
+ if (diffUrl) {
264
265
  logger('downloading diff');
265
266
  try {
266
267
  await PushyModule.downloadPatchFromPpk({
267
- updateUrl: options.diffUrl,
268
+ updateUrl: diffUrl,
268
269
  hash: options.hash,
269
270
  originHash: currentVersion,
270
271
  });
@@ -273,28 +274,34 @@ export async function downloadUpdate(
273
274
  logger(`diff error: ${e.message}, try pdiff`);
274
275
  }
275
276
  }
276
- if (!succeeded && options.pdiffUrl) {
277
- logger('downloading pdiff');
278
- try {
279
- await PushyModule.downloadPatchFromPackage({
280
- updateUrl: options.pdiffUrl,
281
- hash: options.hash,
282
- });
283
- succeeded = true;
284
- } catch (e) {
285
- logger(`pdiff error: ${e.message}, try full patch`);
277
+ if (!succeeded) {
278
+ const pdiffUrl = (await testUrls(options.pdiffUrls)) || options.pdiffUrl;
279
+ if (pdiffUrl) {
280
+ logger('downloading pdiff');
281
+ try {
282
+ await PushyModule.downloadPatchFromPackage({
283
+ updateUrl: pdiffUrl,
284
+ hash: options.hash,
285
+ });
286
+ succeeded = true;
287
+ } catch (e) {
288
+ logger(`pdiff error: ${e.message}, try full patch`);
289
+ }
286
290
  }
287
291
  }
288
- if (!succeeded && options.updateUrl) {
289
- logger('downloading full patch');
290
- try {
291
- await PushyModule.downloadFullUpdate({
292
- updateUrl: options.updateUrl,
293
- hash: options.hash,
294
- });
295
- succeeded = true;
296
- } catch (e) {
297
- logger(`full patch error: ${e.message}`);
292
+ if (!succeeded) {
293
+ const updateUrl = (await testUrls(options.updateUrls)) || options.updateUrl;
294
+ if (updateUrl) {
295
+ logger('downloading full patch');
296
+ try {
297
+ await PushyModule.downloadFullUpdate({
298
+ updateUrl: updateUrl,
299
+ hash: options.hash,
300
+ });
301
+ succeeded = true;
302
+ } catch (e) {
303
+ logger(`full patch error: ${e.message}`);
304
+ }
298
305
  }
299
306
  }
300
307
  progressHandler && progressHandler.remove();
@@ -322,12 +329,10 @@ function assertHash(hash: string) {
322
329
  return true;
323
330
  }
324
331
 
325
- let applyingUpdate = false;
326
332
  export function switchVersion(hash: string) {
327
333
  assertRelease();
328
- if (assertHash(hash) && !applyingUpdate) {
334
+ if (assertHash(hash)) {
329
335
  logger('switchVersion: ' + hash);
330
- applyingUpdate = true;
331
336
  PushyModule.reloadUpdate({ hash });
332
337
  }
333
338
  }
package/lib/type.ts CHANGED
@@ -19,8 +19,11 @@ export interface UpdateAvailableResult {
19
19
  description: string;
20
20
  metaInfo: string;
21
21
  pdiffUrl: string;
22
+ pdiffUrls?: string[];
22
23
  diffUrl?: string;
24
+ diffUrls?: string[];
23
25
  updateUrl?: string;
26
+ updateUrls?: string[];
24
27
  }
25
28
 
26
29
  export type CheckResult =
package/lib/utils.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { Platform } from "react-native";
2
+
1
3
  export function logger(...args: any[]) {
2
4
  console.log('Pushy: ', ...args);
3
5
  }
@@ -7,3 +9,24 @@ export function assertRelease() {
7
9
  throw new Error('react-native-update 只能在 RELEASE 版本中运行.');
8
10
  }
9
11
  }
12
+
13
+ const ping =
14
+ Platform.OS === 'web'
15
+ ? Promise.resolve
16
+ : async (url: string) =>
17
+ Promise.race([
18
+ fetch(url, {
19
+ method: 'HEAD',
20
+ })
21
+ .then(({ status }) => (status === 200 ? url : null))
22
+ .catch(() => null),
23
+ new Promise(r => setTimeout(() => r(null), 2000)),
24
+ ]);
25
+
26
+
27
+ export const testUrls = async (urls?: string[]) => {
28
+ if (!urls?.length) {
29
+ return null;
30
+ }
31
+ return Promise.race(urls.map(ping)).catch(() => null);
32
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-update",
3
- "version": "9.1.6",
3
+ "version": "9.2.0",
4
4
  "description": "react-native hot update",
5
5
  "main": "lib/index.ts",
6
6
  "scripts": {