react-native-update-cli 2.20.3 → 2.20.4

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,4 +1,4 @@
1
1
  export declare function promiseAny<T>(promises: Promise<T>[]): Promise<T>;
2
- export declare const ping: (url: string) => Promise<string | null>;
3
- export declare const testUrls: (urls?: string[]) => Promise<string | null>;
2
+ export declare const ping: (url: string, signal?: AbortSignal) => Promise<string | null>;
3
+ export declare const testUrls: (urls?: string[], hedgeDelayMs?: number) => Promise<string | null>;
4
4
  export declare const getBaseUrl: Promise<string>;
@@ -22,12 +22,13 @@ function promiseAny(promises) {
22
22
  }
23
23
  });
24
24
  }
25
- const ping = async (url) => {
25
+ const ping = async (url, signal) => {
26
26
  let timer;
27
27
  try {
28
28
  return await Promise.race([
29
29
  (0, runtime_1.runtimeFetch)(url, {
30
30
  method: 'HEAD',
31
+ signal,
31
32
  }).then(({ status }) => {
32
33
  if (status === 200) {
33
34
  return url;
@@ -47,22 +48,67 @@ const ping = async (url) => {
47
48
  }
48
49
  };
49
50
  exports.ping = ping;
50
- const testUrls = async (urls) => {
51
+ const HEDGE_DELAY_MS = 250;
52
+ // Hedged race instead of pinging every endpoint at once: the preferred (first)
53
+ // url is tried immediately, each following one only after HEDGE_DELAY_MS of
54
+ // silence (or immediately when a previous ping failed). The first success wins
55
+ // and the losing pings are aborted. Falls back to urls[0] when all fail.
56
+ const testUrls = async (urls, hedgeDelayMs = HEDGE_DELAY_MS) => {
51
57
  if (!urls?.length) {
52
58
  return null;
53
59
  }
54
- let ret = null;
55
- try {
56
- ret = await promiseAny(urls.map(exports.ping));
57
- }
58
- catch (_e) {
59
- // fallback to urls[0]
60
- }
61
- if (ret) {
62
- return ret;
63
- }
64
- // console.log('all ping failed, use first url:', urls[0]);
65
- return urls[0];
60
+ return new Promise((resolve) => {
61
+ const controllers = [];
62
+ let nextIndex = 0;
63
+ let pending = 0;
64
+ let settled = false;
65
+ let hedgeTimer;
66
+ const finish = (winner, winnerController) => {
67
+ if (settled) {
68
+ return;
69
+ }
70
+ settled = true;
71
+ if (hedgeTimer) {
72
+ clearTimeout(hedgeTimer);
73
+ }
74
+ for (const controller of controllers) {
75
+ if (controller !== winnerController) {
76
+ controller.abort();
77
+ }
78
+ }
79
+ resolve(winner ?? urls[0]);
80
+ };
81
+ const launchNext = () => {
82
+ if (hedgeTimer) {
83
+ clearTimeout(hedgeTimer);
84
+ hedgeTimer = undefined;
85
+ }
86
+ if (settled || nextIndex >= urls.length) {
87
+ return;
88
+ }
89
+ const url = urls[nextIndex++];
90
+ const controller = new AbortController();
91
+ controllers.push(controller);
92
+ pending++;
93
+ (0, exports.ping)(url, controller.signal).then(() => finish(url, controller), () => {
94
+ pending--;
95
+ if (settled) {
96
+ return;
97
+ }
98
+ if (nextIndex < urls.length) {
99
+ // A failure frees its slot: hedge the next url right away.
100
+ launchNext();
101
+ }
102
+ else if (pending === 0) {
103
+ finish(null);
104
+ }
105
+ });
106
+ if (!settled && nextIndex < urls.length) {
107
+ hedgeTimer = setTimeout(launchNext, hedgeDelayMs);
108
+ }
109
+ };
110
+ launchNext();
111
+ });
66
112
  };
67
113
  exports.testUrls = testUrls;
68
114
  exports.getBaseUrl = (async () => {
@@ -20,6 +20,7 @@ export declare function getIpaInfo(fn: string): Promise<{
20
20
  export declare function getAabInfo(fn: string): Promise<{
21
21
  versionName: string;
22
22
  buildTime: number;
23
+ bundleHash?: string | undefined;
23
24
  }>;
24
25
  export declare function printVersionCommand(): Promise<void>;
25
26
  export { checkPlugins };
@@ -273,7 +273,24 @@ async function getAabInfo(fn) {
273
273
  if (!Number.isFinite(buildTime) || buildTime === 0) {
274
274
  throw new Error((0, i18n_1.t)('buildTimeNotFound'));
275
275
  }
276
- return { versionName, buildTime, ...appCredential };
276
+ // The JS bundle lives in the base module; these are the same bytes the
277
+ // extracted APK carries (extractApk copies entries verbatim), so the hash
278
+ // matches what uploadAab ends up registering via getApkInfo. Lenient on
279
+ // absence — getAabInfo is display-only (parseAab); the upload path goes
280
+ // through extractApk → getApkInfo which enforces the bundle's presence.
281
+ let bundleHash;
282
+ try {
283
+ bundleHash = await sha256(await readZipEntry(fn, 'base/assets/index.android.bundle'));
284
+ }
285
+ catch {
286
+ // no embedded bundle (e.g. debug AAB); omit the field
287
+ }
288
+ return {
289
+ versionName,
290
+ buildTime,
291
+ ...(bundleHash ? { bundleHash } : {}),
292
+ ...appCredential,
293
+ };
277
294
  }
278
295
  async function readZipEntry(fn, entryName) {
279
296
  return new Promise((resolve, reject) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-update-cli",
3
- "version": "2.20.3",
3
+ "version": "2.20.4",
4
4
  "description": "command line tool for react-native-update (remote updates for react native)",
5
5
  "main": "lib/exports.js",
6
6
  "types": "lib/exports.d.ts",
@@ -22,12 +22,13 @@ export function promiseAny<T>(promises: Promise<T>[]) {
22
22
  });
23
23
  }
24
24
 
25
- export const ping = async (url: string) => {
25
+ export const ping = async (url: string, signal?: AbortSignal) => {
26
26
  let timer: ReturnType<typeof setTimeout> | undefined;
27
27
  try {
28
28
  return await (Promise.race([
29
29
  runtimeFetch(url, {
30
30
  method: 'HEAD',
31
+ signal,
31
32
  }).then(({ status }) => {
32
33
  if (status === 200) {
33
34
  return url;
@@ -46,21 +47,79 @@ export const ping = async (url: string) => {
46
47
  }
47
48
  };
48
49
 
49
- export const testUrls = async (urls?: string[]) => {
50
+ const HEDGE_DELAY_MS = 250;
51
+
52
+ // Hedged race instead of pinging every endpoint at once: the preferred (first)
53
+ // url is tried immediately, each following one only after HEDGE_DELAY_MS of
54
+ // silence (or immediately when a previous ping failed). The first success wins
55
+ // and the losing pings are aborted. Falls back to urls[0] when all fail.
56
+ export const testUrls = async (
57
+ urls?: string[],
58
+ hedgeDelayMs: number = HEDGE_DELAY_MS,
59
+ ) => {
50
60
  if (!urls?.length) {
51
61
  return null;
52
62
  }
53
- let ret: string | null = null;
54
- try {
55
- ret = await promiseAny(urls.map(ping));
56
- } catch (_e) {
57
- // fallback to urls[0]
58
- }
59
- if (ret) {
60
- return ret;
61
- }
62
- // console.log('all ping failed, use first url:', urls[0]);
63
- return urls[0];
63
+ return new Promise<string>((resolve) => {
64
+ const controllers: AbortController[] = [];
65
+ let nextIndex = 0;
66
+ let pending = 0;
67
+ let settled = false;
68
+ let hedgeTimer: ReturnType<typeof setTimeout> | undefined;
69
+
70
+ const finish = (
71
+ winner: string | null,
72
+ winnerController?: AbortController,
73
+ ) => {
74
+ if (settled) {
75
+ return;
76
+ }
77
+ settled = true;
78
+ if (hedgeTimer) {
79
+ clearTimeout(hedgeTimer);
80
+ }
81
+ for (const controller of controllers) {
82
+ if (controller !== winnerController) {
83
+ controller.abort();
84
+ }
85
+ }
86
+ resolve(winner ?? urls[0]);
87
+ };
88
+
89
+ const launchNext = () => {
90
+ if (hedgeTimer) {
91
+ clearTimeout(hedgeTimer);
92
+ hedgeTimer = undefined;
93
+ }
94
+ if (settled || nextIndex >= urls.length) {
95
+ return;
96
+ }
97
+ const url = urls[nextIndex++];
98
+ const controller = new AbortController();
99
+ controllers.push(controller);
100
+ pending++;
101
+ ping(url, controller.signal).then(
102
+ () => finish(url, controller),
103
+ () => {
104
+ pending--;
105
+ if (settled) {
106
+ return;
107
+ }
108
+ if (nextIndex < urls.length) {
109
+ // A failure frees its slot: hedge the next url right away.
110
+ launchNext();
111
+ } else if (pending === 0) {
112
+ finish(null);
113
+ }
114
+ },
115
+ );
116
+ if (!settled && nextIndex < urls.length) {
117
+ hedgeTimer = setTimeout(launchNext, hedgeDelayMs);
118
+ }
119
+ };
120
+
121
+ launchNext();
122
+ });
64
123
  };
65
124
 
66
125
  export const getBaseUrl = (async () => {
@@ -372,7 +372,26 @@ export async function getAabInfo(fn: string) {
372
372
  throw new Error(t('buildTimeNotFound'));
373
373
  }
374
374
 
375
- return { versionName, buildTime, ...appCredential };
375
+ // The JS bundle lives in the base module; these are the same bytes the
376
+ // extracted APK carries (extractApk copies entries verbatim), so the hash
377
+ // matches what uploadAab ends up registering via getApkInfo. Lenient on
378
+ // absence — getAabInfo is display-only (parseAab); the upload path goes
379
+ // through extractApk → getApkInfo which enforces the bundle's presence.
380
+ let bundleHash: string | undefined;
381
+ try {
382
+ bundleHash = await sha256(
383
+ await readZipEntry(fn, 'base/assets/index.android.bundle'),
384
+ );
385
+ } catch {
386
+ // no embedded bundle (e.g. debug AAB); omit the field
387
+ }
388
+
389
+ return {
390
+ versionName,
391
+ buildTime,
392
+ ...(bundleHash ? { bundleHash } : {}),
393
+ ...appCredential,
394
+ };
376
395
  }
377
396
 
378
397
  async function readZipEntry(fn: string, entryName: string): Promise<Buffer> {