Skip to content

Commit e7d7174

Browse files
committed
reduce artifacts exists calls on iOS builds
1 parent 854f0b7 commit e7d7174

2 files changed

Lines changed: 46 additions & 11 deletions

File tree

packages/react-native/scripts/ios-prebuild/hermes.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const pipeline = promisify(stream.pipeline);
1919
const hermesLog = createLogger('Hermes');
2020
const MAVEN_CENTRAL_REPOSITORY = 'https://repo1.maven.org/maven2';
2121
const REACT_NATIVE_MAVEN_CACHE_REPOSITORY = 'https://rnmaven.swmtest.xyz';
22+
const artifactExistenceCache = new Map();
2223

2324
/*::
2425
import type {BuildFlavor, Destination, Platform} from './types';
@@ -193,16 +194,25 @@ async function getTarballUrl(
193194
version /*: string */,
194195
buildType /*: BuildFlavor */,
195196
) /*: Promise<string> */ {
197+
const existingUrl = await findExistingTarballUrl(version, buildType);
198+
if (existingUrl != null) {
199+
return existingUrl;
200+
}
201+
202+
return getTarballUrls(version, buildType)[0];
203+
}
204+
205+
async function findExistingTarballUrl(
206+
version /*: string */,
207+
buildType /*: BuildFlavor */,
208+
) /*: Promise<?string> */ {
196209
const candidates = getTarballUrls(version, buildType);
197210
for (const url of candidates) {
198-
console.log(`Checking if Hermes artifact exists at URL: ${url}`);
199211
if (await hermesArtifactExists(url)) {
200-
console.log(`Found Hermes artifact at URL: ${url}`);
201212
return url;
202213
}
203-
console.log(`Hermes artifact not found at URL: ${url}`);
204214
}
205-
return candidates[0];
215+
return null;
206216
}
207217

208218
function getTarballUrls(
@@ -233,13 +243,20 @@ function getMavenRepositoryUrls() /*: Array<string> */ {
233243
async function hermesArtifactExists(
234244
tarballUrl /*: string */,
235245
) /*: Promise<boolean> */ {
246+
if (artifactExistenceCache.has(tarballUrl)) {
247+
return artifactExistenceCache.get(tarballUrl);
248+
}
249+
236250
try {
237251
const response /*: Response */ = await fetch(tarballUrl, {
238252
method: 'HEAD',
239253
});
240254

241-
return response.status === 200;
255+
const exists = response.status === 200;
256+
artifactExistenceCache.set(tarballUrl, exists);
257+
return exists;
242258
} catch (e) {
259+
artifactExistenceCache.set(tarballUrl, false);
243260
return false;
244261
}
245262
}
@@ -256,8 +273,7 @@ async function hermesSourceType(
256273
return HermesEngineSourceTypes.LOCAL_PREBUILT_TARBALL;
257274
}
258275

259-
const tarballUrl = await getTarballUrl(version, buildType);
260-
if (await hermesArtifactExists(tarballUrl)) {
276+
if ((await findExistingTarballUrl(version, buildType)) != null) {
261277
hermesLog(`Using download prebuild ${buildType} tarball`);
262278
return HermesEngineSourceTypes.DOWNLOAD_PREBUILD_TARBALL;
263279
}

packages/react-native/scripts/ios-prebuild/reactNativeDependencies.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const pipeline = promisify(stream.pipeline);
2222
const dependencyLog = createLogger('ReactNativeDependencies');
2323
const MAVEN_CENTRAL_REPOSITORY = 'https://repo1.maven.org/maven2';
2424
const REACT_NATIVE_MAVEN_CACHE_REPOSITORY = 'https://rnmaven.swmtest.xyz';
25+
const artifactExistenceCache = new Map();
2526

2627
/**
2728
* Downloads ReactNativeDependencies artifacts from the specified version and build type. If you want to specify a specific
@@ -184,13 +185,25 @@ async function getTarballUrl(
184185
version /*: string */,
185186
buildType /*: BuildFlavor */,
186187
) /*: Promise<string> */ {
188+
const existingUrl = await findExistingTarballUrl(version, buildType);
189+
if (existingUrl != null) {
190+
return existingUrl;
191+
}
192+
193+
return getTarballUrls(version, buildType)[0];
194+
}
195+
196+
async function findExistingTarballUrl(
197+
version /*: string */,
198+
buildType /*: BuildFlavor */,
199+
) /*: Promise<?string> */ {
187200
const candidates = getTarballUrls(version, buildType);
188201
for (const url of candidates) {
189202
if (await reactNativeDependenciesArtifactExists(url)) {
190203
return url;
191204
}
192205
}
193-
return candidates[0];
206+
return null;
194207
}
195208

196209
function getTarballUrls(
@@ -237,13 +250,20 @@ async function getNightlyTarballUrl(
237250
async function reactNativeDependenciesArtifactExists(
238251
tarballUrl /*: string */,
239252
) /*: Promise<boolean> */ {
253+
if (artifactExistenceCache.has(tarballUrl)) {
254+
return artifactExistenceCache.get(tarballUrl);
255+
}
256+
240257
try {
241258
const response /*: Response */ = await fetch(tarballUrl, {
242259
method: 'HEAD',
243260
});
244261

245-
return response.status === 200;
262+
const exists = response.status === 200;
263+
artifactExistenceCache.set(tarballUrl, exists);
264+
return exists;
246265
} catch (e) {
266+
artifactExistenceCache.set(tarballUrl, false);
247267
return false;
248268
}
249269
}
@@ -255,8 +275,7 @@ async function reactNativeDependenciesSourceType(
255275
version /*: string */,
256276
buildType /*: BuildFlavor */,
257277
) /*: Promise<ReactNativeDependenciesEngineSourceType> */ {
258-
const tarballUrl = await getTarballUrl(version, buildType);
259-
if (await reactNativeDependenciesArtifactExists(tarballUrl)) {
278+
if ((await findExistingTarballUrl(version, buildType)) != null) {
260279
dependencyLog(`Using download prebuild ${buildType} tarball`);
261280
return ReactNativeDependenciesEngineSourceTypes.DOWNLOAD_PREBUILD_TARBALL;
262281
}

0 commit comments

Comments
 (0)