Skip to content

Commit fd46baf

Browse files
committed
Update version to 2.1.3 in package.json; set baseUrl in .swcrc; refactor API query logic to use dynamic base URL and update defaultEndpoints in constants.ts
1 parent 917f99f commit fd46baf

File tree

5 files changed

+95
-10
lines changed

5 files changed

+95
-10
lines changed

.swcrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"jsc": {
3+
"baseUrl": "./src",
34
"loose": true,
45
"target": "es2018",
56
"parser": {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-update-cli",
3-
"version": "2.1.2",
3+
"version": "2.1.3",
44
"description": "command line tool for react-native-update (remote updates for react native)",
55
"main": "index.js",
66
"bin": {

src/api.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,16 @@ import packageJson from '../package.json';
1010
import type { Package, Session } from './types';
1111
import {
1212
credentialFile,
13-
defaultEndpoint,
1413
pricingPageUrl,
1514
} from './utils/constants';
1615
import { t } from './utils/i18n';
16+
import { getBaseUrl } from 'utils/http-helper';
1717

1818
const tcpPing = util.promisify(tcpp.ping);
1919

2020
let session: Session | undefined;
2121
let savedSession: Session | undefined;
2222

23-
const host =
24-
process.env.PUSHY_REGISTRY || process.env.RNU_API || defaultEndpoint;
2523

2624
const userAgent = `react-native-update-cli/${packageJson.version}`;
2725

@@ -64,7 +62,9 @@ export const closeSession = () => {
6462
};
6563

6664
async function query(url: string, options: fetch.RequestInit) {
67-
const resp = await fetch(url, options);
65+
const baseUrl = await getBaseUrl;
66+
const fullUrl = `${baseUrl}${url}`;
67+
const resp = await fetch(fullUrl, options);
6868
const text = await resp.text();
6969
let json: any;
7070
try {
@@ -83,7 +83,7 @@ async function query(url: string, options: fetch.RequestInit) {
8383

8484
function queryWithoutBody(method: string) {
8585
return (api: string) =>
86-
query(host + api, {
86+
query(api, {
8787
method,
8888
headers: {
8989
'User-Agent': userAgent,
@@ -94,7 +94,7 @@ function queryWithoutBody(method: string) {
9494

9595
function queryWithBody(method: string) {
9696
return (api: string, body?: Record<string, any>) =>
97-
query(host + api, {
97+
query(api, {
9898
method,
9999
headers: {
100100
'User-Agent': userAgent,
@@ -116,6 +116,7 @@ export async function uploadFile(fn: string, key?: string) {
116116
});
117117
let realUrl = url;
118118
if (backupUrl) {
119+
// @ts-ignore
119120
if (global.USE_ACC_OSS) {
120121
realUrl = backupUrl;
121122
} else {

src/utils/constants.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ export const pricingPageUrl = IS_CRESC
1010
? 'https://cresc.dev/pricing'
1111
: 'https://pushy.reactnative.cn/pricing.html';
1212

13-
export const defaultEndpoint = IS_CRESC
14-
? 'https://api.cresc.dev'
15-
: 'https://update.reactnative.cn/api';
13+
export const defaultEndpoints = IS_CRESC
14+
? ['https://api.cresc.dev', 'https://api.cresc.app']
15+
: ['https://update.reactnative.cn/api', 'https://update.react-native.cn/api'];

src/utils/http-helper.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { defaultEndpoints } from './constants';
2+
3+
// const baseUrl = `http://localhost:9000`;
4+
// let baseUrl = SERVER.main[0];
5+
// const baseUrl = `https://p.reactnative.cn/api`;
6+
7+
export function promiseAny<T>(promises: Promise<T>[]) {
8+
return new Promise<T>((resolve, reject) => {
9+
let count = 0;
10+
11+
for (const promise of promises) {
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+
24+
export const ping = async (url: string) => {
25+
let pingFinished = false;
26+
return Promise.race([
27+
fetch(url, {
28+
method: 'HEAD',
29+
})
30+
.then(({ status, statusText }) => {
31+
pingFinished = true;
32+
if (status === 200) {
33+
// console.log('ping success', url);
34+
return url;
35+
}
36+
// console.log('ping failed', url, status, statusText);
37+
throw new Error('ping failed');
38+
})
39+
.catch((e) => {
40+
pingFinished = true;
41+
// console.log('ping error', url, e);
42+
throw new Error('ping error');
43+
}),
44+
new Promise((_, reject) =>
45+
setTimeout(() => {
46+
reject(new Error('ping timeout'));
47+
if (!pingFinished) {
48+
// console.log('ping timeout', url);
49+
}
50+
}, 2000),
51+
),
52+
]) as Promise<string | null>;
53+
};
54+
55+
export const testUrls = async (urls?: string[]) => {
56+
if (!urls?.length) {
57+
return null;
58+
}
59+
const ret = await promiseAny(urls.map(ping));
60+
if (ret) {
61+
return ret;
62+
}
63+
// console.log('all ping failed, use first url:', urls[0]);
64+
return urls[0];
65+
};
66+
67+
export const getBaseUrl = (async () => {
68+
const testEndpoint = process.env.PUSHY_REGISTRY || process.env.RNU_API;
69+
if (testEndpoint) {
70+
return testEndpoint;
71+
}
72+
return testUrls(defaultEndpoints.map((url) => `${url}/status`)).then(
73+
(ret) => {
74+
let baseUrl = defaultEndpoints[0];
75+
if (ret) {
76+
// remove /status
77+
baseUrl = ret.replace('/status', '');
78+
}
79+
// console.log('baseUrl', baseUrl);
80+
return baseUrl;
81+
},
82+
);
83+
})();

0 commit comments

Comments
 (0)