diff --git a/packages/react-native/scripts/spm/__tests__/download-spm-artifacts-test.js b/packages/react-native/scripts/spm/__tests__/download-spm-artifacts-test.js index adb6b3d372f..6bbc1a0f76e 100644 --- a/packages/react-native/scripts/spm/__tests__/download-spm-artifacts-test.js +++ b/packages/react-native/scripts/spm/__tests__/download-spm-artifacts-test.js @@ -671,11 +671,14 @@ describe('download', () => { describe('resolveSnapshotUrl', () => { let origFetch; + let warnSpy; beforeEach(() => { origFetch = globalThis.fetch; + warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); }); afterEach(() => { globalThis.fetch = origFetch; + warnSpy.mockRestore(); }); const METADATA = ` @@ -716,7 +719,7 @@ describe('resolveSnapshotUrl', () => { it('throws when the metadata request fails', async () => { globalThis.fetch = routerFetch({ - 'maven-metadata.xml': {ok: false, status: 500}, + 'maven-metadata.xml': {ok: false, status: 404}, }); await expect( resolveSnapshotUrl( @@ -728,6 +731,25 @@ describe('resolveSnapshotUrl', () => { ).rejects.toThrow(/Failed to fetch snapshot metadata/); }); + it('retries the metadata request after a transient server error', async () => { + const router = routerFetch({'maven-metadata.xml': {text: METADATA}}); + globalThis.fetch = jest + .fn() + .mockResolvedValueOnce({ok: false, status: 503}) + .mockImplementation(router); + + const pending = resolveSnapshotUrl( + '0.85.0', + 'react', + 'react-native-artifacts', + 'reactnative-core-debug.tar.gz', + ); + await jest.advanceTimersByTimeAsync(1_000); + + expect(await pending).toContain('0.85.0-20260101.123456-7'); + expect(globalThis.fetch).toHaveBeenCalledTimes(2); + }); + it('throws when timestamp/buildNumber are absent', async () => { globalThis.fetch = routerFetch({ 'maven-metadata.xml': {text: ''}, @@ -749,11 +771,14 @@ describe('resolveSnapshotUrl', () => { describe('npm dist-tag resolvers', () => { let origFetch; + let warnSpy; beforeEach(() => { origFetch = globalThis.fetch; + warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); }); afterEach(() => { globalThis.fetch = origFetch; + warnSpy.mockRestore(); }); it('resolveNightlyVersion returns the version from the npm registry', async () => { @@ -765,6 +790,25 @@ describe('npm dist-tag resolvers', () => { ); }); + it('resolveNightlyVersion retries a dropped connection', async () => { + const router = routerFetch({ + 'react-native/nightly': {json: {version: '0.86.0-nightly-xyz'}}, + }); + globalThis.fetch = jest + .fn() + .mockRejectedValueOnce(new TypeError('fetch failed')) + .mockImplementation(router); + + const pending = resolveNightlyVersion('react-native'); + await jest.advanceTimersByTimeAsync(1_000); + + expect(await pending).toBe('0.86.0-nightly-xyz'); + expect(globalThis.fetch).toHaveBeenCalledTimes(2); + expect(warnSpy).toHaveBeenCalledWith( + expect.stringContaining('retrying in 1000ms'), + ); + }); + it('resolveNightlyVersion throws on a failed npm lookup', async () => { globalThis.fetch = routerFetch({ 'react-native/nightly': {ok: false, status: 404}, @@ -785,7 +829,13 @@ describe('npm dist-tag resolvers', () => { globalThis.fetch = routerFetch({ 'hermes-compiler/latest-v1': {ok: false, status: 500}, }); - await expect(resolveLatestV1Version()).rejects.toThrow(/npm lookup failed/); + + await Promise.all([ + expect(resolveLatestV1Version()).rejects.toThrow(/npm lookup failed/), + jest.advanceTimersByTimeAsync(3_000), + ]); + + expect(globalThis.fetch).toHaveBeenCalledTimes(3); }); }); @@ -1018,7 +1068,7 @@ describe('resolveCacheSlotVersion', () => { it('falls back to the raw label when the nightly lookup fails', async () => { globalThis.fetch = routerFetch({ - 'react-native/nightly': {ok: false, status: 503}, + 'react-native/nightly': {ok: false, status: 404}, }); expect(await resolveCacheSlotVersion('nightly')).toBe('nightly'); }); diff --git a/packages/react-native/scripts/spm/download-spm-artifacts.js b/packages/react-native/scripts/spm/download-spm-artifacts.js index fbd13b155e2..d7f7f2d1db0 100644 --- a/packages/react-native/scripts/spm/download-spm-artifacts.js +++ b/packages/react-native/scripts/spm/download-spm-artifacts.js @@ -243,7 +243,7 @@ async function resolveSnapshotUrl( `${version}-SNAPSHOT/maven-metadata.xml`; log(` Fetching snapshot metadata: ${metadataUrl}`); - const res = await fetch(metadataUrl); + const res = await fetchWithRetry(metadataUrl); if (!res.ok) { throw new Error( `Failed to fetch snapshot metadata (${res.status}): ${metadataUrl}`, @@ -305,7 +305,9 @@ async function resolveNightlyVersion( ) /*: Promise */ { log(` Resolving nightly version from npm: ${npmPackage}`); - const res = await fetch(`https://registry.npmjs.org/${npmPackage}/nightly`); + const res = await fetchWithRetry( + `https://registry.npmjs.org/${npmPackage}/nightly`, + ); if (!res.ok) { throw new Error(`npm lookup failed for ${npmPackage}: ${res.status}`); } @@ -358,8 +360,7 @@ async function resolveCacheSlotVersion( async function resolveLatestV1Version() /*: Promise */ { log(' Resolving latest-v1 Hermes from npm...'); - // $FlowFixMe[incompatible-call] global fetch not in Flow stubs - const res = await fetch( + const res = await fetchWithRetry( 'https://registry.npmjs.org/hermes-compiler/latest-v1', ); if (!res.ok) { @@ -651,7 +652,10 @@ function isRetryableDownloadError(error /*: Error */) /*: boolean */ { if (httpStatus == null) { return true; } - const status = Number(httpStatus); + return isRetryableHttpStatus(Number(httpStatus)); +} + +function isRetryableHttpStatus(status /*: number */) /*: boolean */ { return status === 408 || status === 429 || status >= 500; } @@ -819,6 +823,28 @@ async function download( } } +async function fetchWithRetry(url /*: string */) /*: Promise */ { + for (let attempt = 1; attempt < DOWNLOAD_MAX_ATTEMPTS; attempt++) { + let reason; + try { + const res = await fetch(url); + if (!isRetryableHttpStatus(res.status)) { + return res; + } + reason = `HTTP ${res.status}`; + } catch (error) { + reason = error.message; + } + const delayMs = DOWNLOAD_RETRY_BASE_DELAY_MS * attempt; + warn( + ` Request failed (${reason}); retrying in ${delayMs}ms ` + + `(attempt ${attempt + 1}/${DOWNLOAD_MAX_ATTEMPTS})`, + ); + await new Promise(resolve => setTimeout(resolve, delayMs)); + } + return fetch(url); +} + /** * Extracts a .tar.gz and returns the path to the first .xcframework found. */