api: download from HOMEBREW_API_DOMAIN

This commit is contained in:
Xuehai Pan 2023-02-03 14:10:40 +00:00
parent 082e75e692
commit ba3bccf9ed
7 changed files with 52 additions and 15 deletions

View File

@ -41,7 +41,7 @@ Metrics/PerceivedComplexity:
Metrics/MethodLength: Metrics/MethodLength:
Max: 232 Max: 232
Metrics/ModuleLength: Metrics/ModuleLength:
Max: 466 Max: 473
Exclude: Exclude:
# TODO: extract more of the bottling logic # TODO: extract more of the bottling logic
- "dev-cmd/bottle.rb" - "dev-cmd/bottle.rb"

View File

@ -17,7 +17,6 @@ module Homebrew
module_function module_function
API_DOMAIN = "https://formulae.brew.sh/api"
HOMEBREW_CACHE_API = (HOMEBREW_CACHE/"api").freeze HOMEBREW_CACHE_API = (HOMEBREW_CACHE/"api").freeze
# Set a longer timeout just for large(r) files. # Set a longer timeout just for large(r) files.
@ -27,8 +26,13 @@ module Homebrew
def fetch(endpoint) def fetch(endpoint)
return cache[endpoint] if cache.present? && cache.key?(endpoint) return cache[endpoint] if cache.present? && cache.key?(endpoint)
api_url = "#{API_DOMAIN}/#{endpoint}" api_url = "#{Homebrew::EnvConfig.api_domain}/#{endpoint}"
output = Utils::Curl.curl_output("--fail", api_url) output = Utils::Curl.curl_output("--fail", api_url)
if !output.success? && Homebrew::EnvConfig.api_domain != HOMEBREW_API_DEFAULT_DOMAIN
# Fall back to the default API domain and try again
api_url = "#{HOMEBREW_API_DEFAULT_DOMAIN}/#{endpoint}"
output = Utils::Curl.curl_output("--fail", api_url)
end
raise ArgumentError, "No file found at #{Tty.underline}#{api_url}#{Tty.reset}" unless output.success? raise ArgumentError, "No file found at #{Tty.underline}#{api_url}#{Tty.reset}" unless output.success?
cache[endpoint] = JSON.parse(output.stdout) cache[endpoint] = JSON.parse(output.stdout)
@ -39,8 +43,9 @@ module Homebrew
sig { params(endpoint: String, target: Pathname).returns(Hash) } sig { params(endpoint: String, target: Pathname).returns(Hash) }
def fetch_json_api_file(endpoint, target:) def fetch_json_api_file(endpoint, target:)
retry_count = 0 retry_count = 0
url = "#{API_DOMAIN}/#{endpoint}" url = "#{Homebrew::EnvConfig.api_domain}/#{endpoint}"
curl_args = %W[--compressed --silent #{url}] default_url = "#{HOMEBREW_API_DEFAULT_DOMAIN}/#{endpoint}"
curl_args = %w[--compressed --silent]
curl_args.prepend("--time-cond", target) if target.exist? && !target.empty? curl_args.prepend("--time-cond", target) if target.exist? && !target.empty?
begin begin
@ -48,8 +53,17 @@ module Homebrew
# Disable retries here, we handle them ourselves below. # Disable retries here, we handle them ourselves below.
Utils::Curl.curl_download(*curl_args, to: target, max_time: JSON_API_MAX_TIME, retries: 0) Utils::Curl.curl_download(*curl_args, to: target, max_time: JSON_API_MAX_TIME, retries: 0)
rescue ErrorDuringExecution rescue ErrorDuringExecution
if url == default_url
raise unless target.exist? raise unless target.exist?
raise if target.empty? raise if target.empty?
elsif retry_count.zero? || !target.exist? || target.empty?
# Fall back to the default API domain and try again
# This block will be executed only once, because we set `url` to `default_url`
url = default_url
target.unlink if target.exist? && target.empty?
retry
end
opoo "#{target.basename}: update failed, falling back to cached version." opoo "#{target.basename}: update failed, falling back to cached version."
end end

View File

@ -601,6 +601,7 @@ then
unset HOMEBREW_BOTTLE_DOMAIN unset HOMEBREW_BOTTLE_DOMAIN
fi fi
HOMEBREW_API_DEFAULT_DOMAIN="https://formulae.brew.sh/api"
HOMEBREW_BOTTLE_DEFAULT_DOMAIN="https://ghcr.io/v2/homebrew/core" HOMEBREW_BOTTLE_DEFAULT_DOMAIN="https://ghcr.io/v2/homebrew/core"
HOMEBREW_USER_AGENT="${HOMEBREW_PRODUCT}/${HOMEBREW_USER_AGENT_VERSION} (${HOMEBREW_SYSTEM}; ${HOMEBREW_PROCESSOR} ${HOMEBREW_OS_USER_AGENT_VERSION})" HOMEBREW_USER_AGENT="${HOMEBREW_PRODUCT}/${HOMEBREW_USER_AGENT_VERSION} (${HOMEBREW_SYSTEM}; ${HOMEBREW_PROCESSOR} ${HOMEBREW_OS_USER_AGENT_VERSION})"
@ -633,6 +634,7 @@ export HOMEBREW_MACOS_VERSION
export HOMEBREW_MACOS_VERSION_NUMERIC export HOMEBREW_MACOS_VERSION_NUMERIC
export HOMEBREW_USER_AGENT export HOMEBREW_USER_AGENT
export HOMEBREW_USER_AGENT_CURL export HOMEBREW_USER_AGENT_CURL
export HOMEBREW_API_DEFAULT_DOMAIN
export HOMEBREW_BOTTLE_DEFAULT_DOMAIN export HOMEBREW_BOTTLE_DEFAULT_DOMAIN
export HOMEBREW_MACOS_SYSTEM_RUBY_NEW_ENOUGH export HOMEBREW_MACOS_SYSTEM_RUBY_NEW_ENOUGH

View File

@ -778,14 +778,24 @@ EOS
then then
INITIAL_JSON_BYTESIZE="$(wc -c "${HOMEBREW_CACHE}"/api/"${formula_or_cask}".json)" INITIAL_JSON_BYTESIZE="$(wc -c "${HOMEBREW_CACHE}"/api/"${formula_or_cask}".json)"
fi fi
JSON_URLS=()
if [[ -n "${HOMEBREW_API_DOMAIN}" && "${HOMEBREW_API_DOMAIN}" != "${HOMEBREW_API_DEFAULT_DOMAIN}" ]]
then
JSON_URLS=("${HOMEBREW_API_DOMAIN}/${formula_or_cask}.json")
fi
JSON_URLS+=("${HOMEBREW_API_DEFAULT_DOMAIN}/${formula_or_cask}.json")
for json_url in "${JSON_URLS[@]}"
do
curl \ curl \
"${CURL_DISABLE_CURLRC_ARGS[@]}" \ "${CURL_DISABLE_CURLRC_ARGS[@]}" \
--fail --compressed --silent --max-time 10 \ --fail --compressed --silent --max-time 10 \
--location --remote-time --output "${HOMEBREW_CACHE}/api/${formula_or_cask}.json" \ --location --remote-time --output "${HOMEBREW_CACHE}/api/${formula_or_cask}.json" \
--time-cond "${HOMEBREW_CACHE}/api/${formula_or_cask}.json" \ --time-cond "${HOMEBREW_CACHE}/api/${formula_or_cask}.json" \
--user-agent "${HOMEBREW_USER_AGENT_CURL}" \ --user-agent "${HOMEBREW_USER_AGENT_CURL}" \
"https://formulae.brew.sh/api/${formula_or_cask}.json" "${json_url}"
curl_exit_code=$? curl_exit_code=$?
[[ ${curl_exit_code} -eq 0 ]] && break
done
if [[ ${curl_exit_code} -eq 0 ]] if [[ ${curl_exit_code} -eq 0 ]]
then then
CURRENT_JSON_BYTESIZE="$(wc -c "${HOMEBREW_CACHE}"/api/"${formula_or_cask}".json)" CURRENT_JSON_BYTESIZE="$(wc -c "${HOMEBREW_CACHE}"/api/"${formula_or_cask}".json)"

View File

@ -15,6 +15,13 @@ module Homebrew
description: "Additional Google Analytics tracking ID to emit user behaviour analytics to. " \ description: "Additional Google Analytics tracking ID to emit user behaviour analytics to. " \
"For more information, see: <https://docs.brew.sh/Analytics>", "For more information, see: <https://docs.brew.sh/Analytics>",
}, },
HOMEBREW_API_DOMAIN: {
description: "Use this URL as the download mirror for Homebrew JSON API. " \
"If metadata files at that URL are temporarily unavailable, " \
"the default API domain will be used as a fallback mirror.",
default_text: "`https://formulae.brew.sh/api`.",
default: HOMEBREW_API_DEFAULT_DOMAIN,
},
HOMEBREW_ARCH: { HOMEBREW_ARCH: {
description: "Linux only: Pass this value to a type name representing the compiler's `-march` option.", description: "Linux only: Pass this value to a type name representing the compiler's `-march` option.",
default: "native", default: "native",

View File

@ -33,6 +33,7 @@ ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.irregular "it", "they" inflect.irregular "it", "they"
end end
HOMEBREW_API_DEFAULT_DOMAIN = ENV.fetch("HOMEBREW_API_DEFAULT_DOMAIN").freeze
HOMEBREW_BOTTLE_DEFAULT_DOMAIN = ENV.fetch("HOMEBREW_BOTTLE_DEFAULT_DOMAIN").freeze HOMEBREW_BOTTLE_DEFAULT_DOMAIN = ENV.fetch("HOMEBREW_BOTTLE_DEFAULT_DOMAIN").freeze
HOMEBREW_BREW_DEFAULT_GIT_REMOTE = ENV.fetch("HOMEBREW_BREW_DEFAULT_GIT_REMOTE").freeze HOMEBREW_BREW_DEFAULT_GIT_REMOTE = ENV.fetch("HOMEBREW_BREW_DEFAULT_GIT_REMOTE").freeze
HOMEBREW_CORE_DEFAULT_GIT_REMOTE = ENV.fetch("HOMEBREW_CORE_DEFAULT_GIT_REMOTE").freeze HOMEBREW_CORE_DEFAULT_GIT_REMOTE = ENV.fetch("HOMEBREW_CORE_DEFAULT_GIT_REMOTE").freeze

View File

@ -4404,6 +4404,8 @@ module Homebrew::EnvConfig
def self.all_proxy(); end def self.all_proxy(); end
def self.api_domain(); end
def self.arch(); end def self.arch(); end
def self.artifact_domain(); end def self.artifact_domain(); end
@ -5458,6 +5460,7 @@ class Object
FORMULA_COMPONENT_PRECEDENCE_LIST = ::T.let(nil, ::T.untyped) FORMULA_COMPONENT_PRECEDENCE_LIST = ::T.let(nil, ::T.untyped)
GZIP_BUFFER_SIZE = ::T.let(nil, ::T.untyped) GZIP_BUFFER_SIZE = ::T.let(nil, ::T.untyped)
HIDDEN_DESC_PLACEHOLDER = ::T.let(nil, ::T.untyped) HIDDEN_DESC_PLACEHOLDER = ::T.let(nil, ::T.untyped)
HOMEBREW_API_DEFAULT_DOMAIN = ::T.let(nil, ::T.untyped)
HOMEBREW_BOTTLES_EXTNAME_REGEX = ::T.let(nil, ::T.untyped) HOMEBREW_BOTTLES_EXTNAME_REGEX = ::T.let(nil, ::T.untyped)
HOMEBREW_BOTTLE_DEFAULT_DOMAIN = ::T.let(nil, ::T.untyped) HOMEBREW_BOTTLE_DEFAULT_DOMAIN = ::T.let(nil, ::T.untyped)
HOMEBREW_BREWED_CURL_PATH = ::T.let(nil, ::T.untyped) HOMEBREW_BREWED_CURL_PATH = ::T.let(nil, ::T.untyped)