Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

require "test/unit"
require "socket"
require_relative "../rncore"
require_relative "../rndependencies"
require_relative "../../../sdks/hermes-engine/hermes-utils"

class PrebuiltArtifactTests < Test::Unit::TestCase
def with_repository(statuses)
server = TCPServer.new("127.0.0.1", 0)
previous = ENV["ENTERPRISE_REPOSITORY"]
ENV["ENTERPRISE_REPOSITORY"] = "http://127.0.0.1:#{server.addr[1]}"
thread = Thread.new do
loop do
socket = server.accept
socket.gets("\r\n\r\n")
status = statuses.length > 1 ? statuses.shift : statuses.first
socket.write("HTTP/1.1 #{status}\r\nContent-Length: 0\r\nConnection: close\r\n\r\n")
socket.close
end
end
yield ENV["ENTERPRISE_REPOSITORY"]
ensure
ENV["ENTERPRISE_REPOSITORY"] = previous
thread&.kill&.join
server&.close
end

def test_hermes_does_not_select_source_on_an_inconclusive_probe
with_repository(["403 Forbidden"]) do
assert_raise(RuntimeError) { hermes_source_type("0.88.1", ".") }
end
end

def test_core_and_dependencies_do_not_treat_a_repository_error_as_missing
with_repository(["403 Forbidden"]) do
[ReactNativeCoreUtils, ReactNativeDependenciesUtils].each do |component|
assert_raise(RuntimeError) { component.release_artifact_exists("0.88.1") }
end
end
end

def test_transient_failure_then_success_uses_prebuilt_hermes
with_repository(["503 Service Unavailable", "200 OK"]) do
assert_equal(HermesEngineSourceType::DOWNLOAD_PREBUILD_RELEASE_TARBALL, hermes_source_type("0.88.1", "."))
end
end

def test_a_healthy_repository_is_used_after_an_inconclusive_mirror
with_repository(["403 Forbidden", "200 OK"]) do |root|
assert_equal("#{root}/central", ReactNativeMavenArtifact.find_available(["#{root}/mirror", "#{root}/central"]))
assert_equal("#{root}/mirror", ReactNativeMavenArtifact.find_available(["#{root}/mirror"]))
end
end

def test_a_missing_artifact_on_one_repository_does_not_hide_a_failed_probe
with_repository(["403 Forbidden", "404 Not Found"]) do |root|
assert_raise(RuntimeError) { ReactNativeMavenArtifact.find_available(["#{root}/mirror", "#{root}/central"]) }
end
end

def test_missing_artifacts_still_allow_a_source_build
with_repository(["404 Not Found"]) do
assert_equal(HermesEngineSourceType::BUILD_FROM_GITHUB_STABLE_BRANCH, hermes_source_type("0.88.1", "."))
assert_false(ReactNativeCoreUtils.release_artifact_exists("0.88.1"))
assert_false(ReactNativeDependenciesUtils.release_artifact_exists("0.88.1"))
end
end
end
37 changes: 37 additions & 0 deletions packages/react-native/scripts/cocoapods/maven_artifact.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

require 'open3'

module ReactNativeMavenArtifact
# Cache only conclusive HEAD responses for the duration of pod install.
@available = {}

def self.find_available(urls)
errors = []
urls.each do |url|
next if url.empty? || url.match?(%r{/1000\.0\.0(?:-SNAPSHOT)?/})

unless @available.key?(url)
code, status = Open3.capture2(
'curl', '--silent', '--head', '--location', '--output', File::NULL,
'--write-out', '%{http_code}', '--retry', '2', '--retry-all-errors',
'--retry-max-time', '20', '--connect-timeout', '5', '--max-time', '15', '--', url
)
unless status.success? && ['200', '404', '410'].include?(code)
errors << (status.success? ? "HTTP #{code}" : "curl exit #{status.exitstatus}")
next
end
@available[url] = code == '200'
end
return url if @available[url]
end

unless errors.empty?
raise "Could not determine prebuilt artifact availability (#{errors.join(', ')}). Check Maven repository access and retry pod install."
end
nil
end
end
6 changes: 3 additions & 3 deletions packages/react-native/scripts/cocoapods/rncore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def self.setup_rncore(react_native_path, react_native_version)
end

use_local_xcframework = ENV["RCT_TESTONLY_RNCORE_TARBALL_PATH"] && File.exist?(ENV["RCT_TESTONLY_RNCORE_TARBALL_PATH"])
artifacts_exists = ENV["RCT_USE_PREBUILT_RNCORE"] == "1" && (@@use_nightly ? nightly_artifact_exists(@@react_native_version) : release_artifact_exists(@@react_native_version))
artifacts_exists = !use_local_xcframework && ENV["RCT_USE_PREBUILT_RNCORE"] == "1" && (@@use_nightly ? nightly_artifact_exists(@@react_native_version) : release_artifact_exists(@@react_native_version))
@@build_from_source = !use_local_xcframework && !artifacts_exists

if @@build_from_source && ENV["RCT_TESTONLY_RNCORE_TARBALL_PATH"] && !use_local_xcframework
Expand Down Expand Up @@ -351,7 +351,7 @@ def self.generate_plist_content(mappings)

def self.stable_tarball_url(version, build_type, dsyms = false)
candidates = stable_tarball_urls(version, build_type, dsyms)
return candidates.find { |url| artifact_exists(url) } || candidates.first
return ReactNativeMavenArtifact.find_available(candidates) || candidates.first
end

def self.stable_tarball_urls(version, build_type, dsyms = false)
Expand Down Expand Up @@ -467,7 +467,7 @@ def self.download_rncore_tarball(react_native_path, tarball_url, version, config
end

def self.release_artifact_exists(version)
return stable_tarball_urls(version, :debug).any? { |url| artifact_exists(url) }
return !ReactNativeMavenArtifact.find_available(stable_tarball_urls(version, :debug)).nil?
end

def self.nightly_artifact_exists(version)
Expand Down
6 changes: 3 additions & 3 deletions packages/react-native/scripts/cocoapods/rndependencies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ def self.setup_react_native_dependencies(react_native_path, react_native_version
abort_if_use_local_rndeps_with_no_file()
end

artifacts_exists = ENV["RCT_USE_RN_DEP"] == "1" && (@@use_nightly ? nightly_artifact_exists(@@react_native_version) : release_artifact_exists(@@react_native_version))
use_local_xcframework = ENV["RCT_USE_LOCAL_RN_DEP"] && File.exist?(ENV["RCT_USE_LOCAL_RN_DEP"])
artifacts_exists = !use_local_xcframework && ENV["RCT_USE_RN_DEP"] == "1" && (@@use_nightly ? nightly_artifact_exists(@@react_native_version) : release_artifact_exists(@@react_native_version))

@@build_from_source = !use_local_xcframework && !artifacts_exists

Expand Down Expand Up @@ -233,7 +233,7 @@ def self.podspec_source_download_prebuild_release_tarball()

def self.release_tarball_url(version, build_type)
candidates = release_tarball_urls(version, build_type)
return candidates.find { |url| artifact_exists(url) } || candidates.first
return ReactNativeMavenArtifact.find_available(candidates) || candidates.first
end

def self.release_tarball_urls(version, build_type)
Expand Down Expand Up @@ -375,7 +375,7 @@ def self.download_rndeps_tarball(react_native_path, tarball_url, version, config
end

def self.release_artifact_exists(version)
return release_tarball_urls(version, :debug).any? { |url| artifact_exists(url) }
return !ReactNativeMavenArtifact.find_available(release_tarball_urls(version, :debug)).nil?
end

def self.nightly_artifact_exists(version)
Expand Down
18 changes: 2 additions & 16 deletions packages/react-native/scripts/cocoapods/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'uri'
require 'net/http'

require_relative "./maven_artifact.rb"
require_relative "./helpers.rb"
require_relative "./jsengine.rb"

Expand Down Expand Up @@ -992,25 +993,10 @@ def self.resolve_use_frameworks(spec, header_mappings_dir: nil, module_name: nil
# metadata lookups. The answers should not change within one install, so
# each request is issued at most once per process and then served from
# these in-memory caches.
@@artifact_exists_cache = {}
@@get_response_cache = {}

# Memoized existence probe (HTTP HEAD) for a prebuilt artifact URL.
# Only conclusive answers are cached. If curl never got an HTTP status
# (DNS failure, no route, ...) the probe is left uncached so that a
# transient hiccup doesn't permanently mark the artifact as missing.
def self.artifact_exists?(tarball_url)
unpublished_version = Regexp.escape(UNPUBLISHED_MAVEN_VERSION)
return false if tarball_url.match?(%r{/#{unpublished_version}(?:-SNAPSHOT)?/})

unless @@artifact_exists_cache.key?(tarball_url)
# -L is used to follow redirects, useful for the nightlies
# The url is wrapped in quotes to avoid escaping & and ?.
http_code = `curl -o /dev/null --silent -Iw '%{http_code}' -L "#{tarball_url}"`
return false if !$?.success? || http_code == "000"
@@artifact_exists_cache[tarball_url] = (http_code == "200")
end
return @@artifact_exists_cache[tarball_url]
!ReactNativeMavenArtifact.find_available([tarball_url]).nil?
end

# Memoized HTTP GET for small metadata lookups (Maven snapshot metadata).
Expand Down
37 changes: 4 additions & 33 deletions packages/react-native/sdks/hermes-engine/hermes-utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,12 @@
# LICENSE file in the root directory of this source tree.

require 'digest'
require_relative '../../scripts/cocoapods/maven_artifact'

HERMES_GITHUB_URL = "https://github.com/facebook/hermes.git"
ENV_BUILD_FROM_SOURCE = "RCT_BUILD_HERMES_FROM_SOURCE"
MAVEN_CENTRAL_REPOSITORY = "https://repo1.maven.org/maven2"
REACT_NATIVE_MAVEN_MIRROR_REPOSITORY = "https://repo.reactnative.dev/maven2"
UNPUBLISHED_HERMES_VERSION = "1000.0.0"

# Memoized results of requests to the Maven repositories (mirror or central).
# hermes-engine.podspec is evaluated several times during a single
# `pod install`, and every evaluation re-resolves the artifact source from
# scratch; without memoization that re-issues identical artifact existence
# probes. The answers should not change within one install, so each request
# is issued at most once per process.
HERMES_ARTIFACT_EXISTS_CACHE = {}

module HermesEngineSourceType
LOCAL_PREBUILT_TARBALL = :local_prebuilt_tarball
DOWNLOAD_PREBUILD_RELEASE_TARBALL = :download_prebuild_release_tarball
Expand Down Expand Up @@ -100,7 +91,7 @@ def force_build_from_stable_branch(react_native_path)
end

def release_artifact_exists(version)
return release_tarball_urls(version, :debug).any? { |url| hermes_artifact_exists(url) }
return !ReactNativeMavenArtifact.find_available(release_tarball_urls(version, :debug)).nil?
end

def podspec_source(source_type, version, react_native_path)
Expand Down Expand Up @@ -202,7 +193,7 @@ def hermestag_file(react_native_path)

def release_tarball_url(version, build_type)
candidates = release_tarball_urls(version, build_type)
return candidates.find { |url| hermes_artifact_exists(url) } || candidates.first
return ReactNativeMavenArtifact.find_available(candidates) || candidates.first
end

def release_tarball_urls(version, build_type)
Expand Down Expand Up @@ -346,28 +337,8 @@ def resolve_url_redirects(url)
return (`curl -Ls -o /dev/null -w %{url_effective} \"#{url}\"`)
end

# This function checks that Hermes artifact exists.
# As of now it should check it on the Maven repo.
# The probe is memoized, so repeated podspec evaluations in one `pod install`
# don't re-request the same URL. Only conclusive answers are cached: if curl
# never got an HTTP status (DNS failure, no route, ...) the probe is left
# uncached so a transient hiccup doesn't permanently mark the artifact as
# missing.
#
# Parameters
# - tarball_url: the URL of the Hermes artifact to probe
def hermes_artifact_exists(tarball_url)
unpublished_version = Regexp.escape(UNPUBLISHED_HERMES_VERSION)
return false if tarball_url.match?(%r{/#{unpublished_version}(?:-SNAPSHOT)?/})

unless HERMES_ARTIFACT_EXISTS_CACHE.key?(tarball_url)
# -L is used to follow redirects, useful for the nightlies
# I also needed to wrap the url in quotes to avoid escaping & and ?.
http_code = `curl -o /dev/null --silent -Iw '%{http_code}' -L "#{tarball_url}"`
return false if !$?.success? || http_code == "000"
HERMES_ARTIFACT_EXISTS_CACHE[tarball_url] = (http_code == "200")
end
return HERMES_ARTIFACT_EXISTS_CACHE[tarball_url]
!ReactNativeMavenArtifact.find_available([tarball_url]).nil?
end

def hermes_log(message, level = :warning)
Expand Down