react-native-update 9.0.5 → 9.1.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.
@@ -1,9 +1,6 @@
1
1
  <?xml version="1.0" encoding="utf-8"?>
2
2
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3
3
  package="cn.reactnative.modules.update">
4
- <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
5
-
6
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
7
4
  <application>
8
5
  <meta-data android:name="pushy_build_time" android:value="@string/pushy_build_time" />
9
6
  <provider
package/lib/endpoint.ts CHANGED
@@ -1,75 +1,26 @@
1
- let currentEndpoint = 'https://update.react-native.cn/api';
2
-
3
- function ping(url: string, rejectImmediate?: boolean) {
4
- return new Promise((resolve, reject) => {
5
- const xhr = new XMLHttpRequest();
6
- xhr.onreadystatechange = (e) => {
7
- if (xhr.readyState !== 4) {
8
- return;
9
- }
10
- if (xhr.status === 200) {
11
- resolve(url);
12
- } else {
13
- rejectImmediate ? reject() : setTimeout(reject, 5000);
14
- }
15
- };
16
- xhr.open('HEAD', url);
17
- xhr.send();
18
- xhr.timeout = 5000;
19
- xhr.ontimeout = reject;
20
- });
21
- }
1
+ import { logger } from './utils';
22
2
 
23
- function logger(...args: any[]) {
24
- console.log('Pushy: ', ...args);
25
- }
26
-
27
- let backupEndpoints: string[] = [];
28
- let backupEndpointsQueryUrl: string | null =
29
- 'https://cdn.jsdelivr.net/gh/reactnativecn/react-native-pushy@master/endpoints.json';
3
+ let currentEndpoint = 'https://update.react-native.cn/api';
4
+ let backupEndpoints: string[] = ['https://update.reactnative.cn/api'];
5
+ let backupEndpointsQueryUrl: string | null = null;
30
6
 
31
- export async function tryBackupEndpoints() {
32
- if (!backupEndpoints.length && !backupEndpointsQueryUrl) {
33
- return;
34
- }
35
- try {
36
- await ping(getStatusUrl(), true);
37
- logger('current endpoint ok', currentEndpoint);
38
- return;
39
- } catch (e) {
40
- logger('current endpoint failed', currentEndpoint);
41
- }
42
- if (!backupEndpoints.length && backupEndpointsQueryUrl) {
7
+ export async function updateBackupEndpoints() {
8
+ if (backupEndpointsQueryUrl) {
43
9
  try {
44
10
  const resp = await fetch(backupEndpointsQueryUrl);
45
- backupEndpoints = await resp.json();
46
- logger('get remote endpoints:', backupEndpoints);
11
+ const remoteEndpoints = await resp.json();
12
+ if (Array.isArray(remoteEndpoints)) {
13
+ backupEndpoints = Array.from(
14
+ new Set([...backupEndpoints, ...remoteEndpoints]),
15
+ );
16
+ logger('fetch remote endpoints:', remoteEndpoints);
17
+ logger('merged backup endpoints:', backupEndpoints);
18
+ }
47
19
  } catch (e) {
48
- logger('get remote endpoints failed');
49
- return;
20
+ logger('fetch remote endpoints failed');
50
21
  }
51
22
  }
52
- await pickFatestAvailableEndpoint();
53
- }
54
-
55
- async function pickFatestAvailableEndpoint(endpoints = backupEndpoints) {
56
- const fastestEndpoint = await Promise.race(
57
- endpoints.map(pingAndReturnEndpoint),
58
- );
59
- if (typeof fastestEndpoint === 'string') {
60
- logger(`pick endpoint: ${fastestEndpoint}`);
61
- currentEndpoint = fastestEndpoint;
62
- } else {
63
- logger('all remote endpoints failed');
64
- }
65
- }
66
-
67
- async function pingAndReturnEndpoint(endpoint = currentEndpoint) {
68
- return ping(getStatusUrl(endpoint)).then(() => endpoint);
69
- }
70
-
71
- function getStatusUrl(endpoint = currentEndpoint) {
72
- return `${endpoint}/status`;
23
+ return backupEndpoints;
73
24
  }
74
25
 
75
26
  export function getCheckUrl(APPKEY, endpoint = currentEndpoint) {
@@ -95,7 +46,6 @@ export function setCustomEndpoints({
95
46
  backupEndpointsQueryUrl = null;
96
47
  if (Array.isArray(backups) && backups.length > 0) {
97
48
  backupEndpoints = backups;
98
- pickFatestAvailableEndpoint();
99
49
  }
100
50
  if (typeof backupQueryUrl === 'string') {
101
51
  backupEndpointsQueryUrl = backupQueryUrl;
package/lib/main.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import {
2
- tryBackupEndpoints,
2
+ updateBackupEndpoints,
3
3
  getCheckUrl,
4
4
  setCustomEndpoints,
5
5
  } from './endpoint';
@@ -16,6 +16,7 @@ import {
16
16
  UpdateAvailableResult,
17
17
  UpdateEventsListener,
18
18
  } from './type';
19
+ import { assertRelease, logger } from './utils';
19
20
  export { setCustomEndpoints };
20
21
  const {
21
22
  version: v,
@@ -74,10 +75,6 @@ if (!uuid) {
74
75
  PushyModule.setUuid(uuid);
75
76
  }
76
77
 
77
- function logger(...args: string[]) {
78
- console.log('Pushy: ', ...args);
79
- }
80
-
81
78
  const noop = () => {};
82
79
  let reporter: UpdateEventsListener = noop;
83
80
 
@@ -125,16 +122,10 @@ export const cInfo = {
125
122
  uuid,
126
123
  };
127
124
 
128
- function assertRelease() {
129
- if (__DEV__) {
130
- throw new Error('react-native-update 只能在 RELEASE 版本中运行.');
131
- }
132
- }
133
-
134
125
  let lastChecking;
135
126
  const empty = {};
136
127
  let lastResult: CheckResult;
137
- export async function checkUpdate(APPKEY: string, isRetry?: boolean) {
128
+ export async function checkUpdate(APPKEY: string) {
138
129
  assertRelease();
139
130
  const now = Date.now();
140
131
  if (lastResult && lastChecking && now - lastChecking < 1000 * 60) {
@@ -152,31 +143,44 @@ export async function checkUpdate(APPKEY: string, isRetry?: boolean) {
152
143
  return lastResult || empty;
153
144
  }
154
145
  report({ type: 'checking' });
146
+ const fetchPayload = {
147
+ method: 'POST',
148
+ headers: {
149
+ Accept: 'application/json',
150
+ 'Content-Type': 'application/json',
151
+ },
152
+ body: JSON.stringify({
153
+ packageVersion,
154
+ hash: currentVersion,
155
+ buildTime,
156
+ cInfo,
157
+ }),
158
+ };
155
159
  let resp;
156
160
  try {
157
- resp = await fetch(getCheckUrl(APPKEY), {
158
- method: 'POST',
159
- headers: {
160
- Accept: 'application/json',
161
- 'Content-Type': 'application/json',
162
- },
163
- body: JSON.stringify({
164
- packageVersion,
165
- hash: currentVersion,
166
- buildTime,
167
- cInfo,
168
- }),
169
- });
161
+ resp = await fetch(getCheckUrl(APPKEY), fetchPayload);
170
162
  } catch (e) {
171
- if (isRetry) {
172
- report({
173
- type: 'errorChecking',
174
- message: '无法连接更新服务器,请检查网络连接后重试',
175
- });
176
- return lastResult || empty;
163
+ report({
164
+ type: 'errorChecking',
165
+ message: '无法连接主更新服务器,尝试备用节点',
166
+ });
167
+ const backupEndpoints = await updateBackupEndpoints();
168
+ if (backupEndpoints) {
169
+ try {
170
+ resp = await Promise.race(
171
+ backupEndpoints.map((endpoint) =>
172
+ fetch(getCheckUrl(APPKEY, endpoint), fetchPayload),
173
+ ),
174
+ );
175
+ } catch {}
177
176
  }
178
- await tryBackupEndpoints();
179
- return checkUpdate(APPKEY, true);
177
+ }
178
+ if (!resp) {
179
+ report({
180
+ type: 'errorChecking',
181
+ message: '无法连接更新服务器,请检查网络连接后重试',
182
+ });
183
+ return lastResult || empty;
180
184
  }
181
185
  const result: CheckResult = await resp.json();
182
186
 
package/lib/utils.ts ADDED
@@ -0,0 +1,9 @@
1
+ export function logger(...args: any[]) {
2
+ console.log('Pushy: ', ...args);
3
+ }
4
+
5
+ export function assertRelease() {
6
+ if (__DEV__) {
7
+ throw new Error('react-native-update 只能在 RELEASE 版本中运行.');
8
+ }
9
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-update",
3
- "version": "9.0.5",
3
+ "version": "9.1.0",
4
4
  "description": "react-native hot update",
5
5
  "main": "lib/index.ts",
6
6
  "scripts": {
@@ -39,7 +39,7 @@
39
39
  "url": "https://github.com/reactnativecn/react-native-pushy/issues"
40
40
  },
41
41
  "peerDependencies": {
42
- "react-native": ">=0.27.0"
42
+ "react-native": ">=0.57.0"
43
43
  },
44
44
  "homepage": "https://github.com/reactnativecn/react-native-pushy#readme",
45
45
  "dependencies": {
@@ -63,6 +63,6 @@
63
63
  "jest": "^29.2.1",
64
64
  "pod-install": "^0.1.37",
65
65
  "ts-jest": "^29.0.3",
66
- "typescript": "^4.1.3"
66
+ "typescript": "^5.2.2"
67
67
  }
68
68
  }