react-native-update 9.2.1 → 9.2.3
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/lib/endpoint.ts +3 -3
- package/lib/main.ts +5 -5
- package/lib/utils.ts +49 -8
- package/package.json +3 -2
package/lib/endpoint.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { logger } from './utils';
|
|
1
|
+
import { logger, promiseAny } from './utils';
|
|
2
2
|
|
|
3
3
|
let currentEndpoint = 'https://update.react-native.cn/api';
|
|
4
4
|
let backupEndpoints: string[] = [
|
|
@@ -13,8 +13,8 @@ let backupEndpointsQueryUrls = [
|
|
|
13
13
|
export async function updateBackupEndpoints() {
|
|
14
14
|
if (backupEndpointsQueryUrls) {
|
|
15
15
|
try {
|
|
16
|
-
const resp = await
|
|
17
|
-
backupEndpointsQueryUrls.map(
|
|
16
|
+
const resp = await promiseAny(
|
|
17
|
+
backupEndpointsQueryUrls.map(queryUrl => fetch(queryUrl)),
|
|
18
18
|
);
|
|
19
19
|
const remoteEndpoints = await resp.json();
|
|
20
20
|
if (Array.isArray(remoteEndpoints)) {
|
package/lib/main.ts
CHANGED
|
@@ -16,7 +16,7 @@ import {
|
|
|
16
16
|
UpdateAvailableResult,
|
|
17
17
|
UpdateEventsListener,
|
|
18
18
|
} from './type';
|
|
19
|
-
import { assertRelease, logger, testUrls } from './utils';
|
|
19
|
+
import { assertRelease, logger, promiseAny, testUrls } from './utils';
|
|
20
20
|
export { setCustomEndpoints };
|
|
21
21
|
const {
|
|
22
22
|
version: v,
|
|
@@ -167,8 +167,8 @@ export async function checkUpdate(APPKEY: string) {
|
|
|
167
167
|
const backupEndpoints = await updateBackupEndpoints();
|
|
168
168
|
if (backupEndpoints) {
|
|
169
169
|
try {
|
|
170
|
-
resp = await
|
|
171
|
-
backupEndpoints.map(
|
|
170
|
+
resp = await promiseAny(
|
|
171
|
+
backupEndpoints.map(endpoint =>
|
|
172
172
|
fetch(getCheckUrl(APPKEY, endpoint), fetchPayload),
|
|
173
173
|
),
|
|
174
174
|
);
|
|
@@ -205,7 +205,7 @@ function checkOperation(
|
|
|
205
205
|
if (!Array.isArray(op)) {
|
|
206
206
|
return;
|
|
207
207
|
}
|
|
208
|
-
op.forEach(
|
|
208
|
+
op.forEach(action => {
|
|
209
209
|
if (action.type === 'block') {
|
|
210
210
|
blockUpdate = {
|
|
211
211
|
reason: action.reason,
|
|
@@ -250,7 +250,7 @@ export async function downloadUpdate(
|
|
|
250
250
|
const downloadCallback = eventListeners.onDownloadProgress;
|
|
251
251
|
progressHandler = eventEmitter.addListener(
|
|
252
252
|
'RCTPushyDownloadProgress',
|
|
253
|
-
|
|
253
|
+
progressData => {
|
|
254
254
|
if (progressData.hash === options.hash) {
|
|
255
255
|
downloadCallback(progressData);
|
|
256
256
|
}
|
package/lib/utils.ts
CHANGED
|
@@ -1,9 +1,26 @@
|
|
|
1
|
-
import { Platform } from
|
|
1
|
+
import { Platform } from 'react-native';
|
|
2
2
|
|
|
3
3
|
export function logger(...args: any[]) {
|
|
4
4
|
console.log('Pushy: ', ...args);
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
+
export function promiseAny<T>(promises: Promise<T>[]) {
|
|
8
|
+
return new Promise<T>((resolve, reject) => {
|
|
9
|
+
let count = 0;
|
|
10
|
+
|
|
11
|
+
promises.forEach((promise) => {
|
|
12
|
+
Promise.resolve(promise)
|
|
13
|
+
.then(resolve)
|
|
14
|
+
.catch(() => {
|
|
15
|
+
count++;
|
|
16
|
+
if (count === promises.length) {
|
|
17
|
+
reject(new Error('All promises were rejected'));
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
7
24
|
export function assertRelease() {
|
|
8
25
|
if (__DEV__) {
|
|
9
26
|
throw new Error('react-native-update 只能在 RELEASE 版本中运行.');
|
|
@@ -13,20 +30,44 @@ export function assertRelease() {
|
|
|
13
30
|
const ping =
|
|
14
31
|
Platform.OS === 'web'
|
|
15
32
|
? Promise.resolve
|
|
16
|
-
: async (url: string) =>
|
|
17
|
-
|
|
33
|
+
: async (url: string) => {
|
|
34
|
+
let pingFinished = false;
|
|
35
|
+
return Promise.race([
|
|
18
36
|
fetch(url, {
|
|
19
37
|
method: 'HEAD',
|
|
20
38
|
})
|
|
21
|
-
.then(({ status }) =>
|
|
22
|
-
|
|
23
|
-
|
|
39
|
+
.then(({ status, statusText }) => {
|
|
40
|
+
pingFinished = true;
|
|
41
|
+
if (status === 200) {
|
|
42
|
+
return url;
|
|
43
|
+
}
|
|
44
|
+
logger('ping failed', url, status, statusText);
|
|
45
|
+
return null;
|
|
46
|
+
})
|
|
47
|
+
.catch((e) => {
|
|
48
|
+
pingFinished = true;
|
|
49
|
+
logger('ping error', url, e);
|
|
50
|
+
return null;
|
|
51
|
+
}),
|
|
52
|
+
new Promise((r) =>
|
|
53
|
+
setTimeout(() => {
|
|
54
|
+
r(null);
|
|
55
|
+
if (!pingFinished) {
|
|
56
|
+
logger('ping timeout', url);
|
|
57
|
+
}
|
|
58
|
+
}, 2000),
|
|
59
|
+
),
|
|
24
60
|
]);
|
|
25
|
-
|
|
61
|
+
};
|
|
26
62
|
|
|
27
63
|
export const testUrls = async (urls?: string[]) => {
|
|
28
64
|
if (!urls?.length) {
|
|
29
65
|
return null;
|
|
30
66
|
}
|
|
31
|
-
|
|
67
|
+
const ret = await promiseAny(urls.map(ping));
|
|
68
|
+
if (ret) {
|
|
69
|
+
return ret;
|
|
70
|
+
}
|
|
71
|
+
logger('all ping failed, use first url:', urls[0]);
|
|
72
|
+
return urls[0];
|
|
32
73
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-update",
|
|
3
|
-
"version": "9.2.
|
|
3
|
+
"version": "9.2.3",
|
|
4
4
|
"description": "react-native hot update",
|
|
5
5
|
"main": "lib/index.ts",
|
|
6
6
|
"scripts": {
|
|
@@ -67,5 +67,6 @@
|
|
|
67
67
|
"react-native": "^0.72.6",
|
|
68
68
|
"ts-jest": "^29.0.3",
|
|
69
69
|
"typescript": "^5.2.2"
|
|
70
|
-
}
|
|
70
|
+
},
|
|
71
|
+
"packageManager": "yarn@1.22.21+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72"
|
|
71
72
|
}
|