Class: Pod::CDNSource
- Inherits:
-
Source
show all
- Includes:
- Concurrent
- Defined in:
- lib/cocoapods-core/cdn_source.rb
Overview
Subclass of Pod::Source to provide support for CDN-based Specs repositories
Constant Summary
collapse
- MAX_CONCURRENCY =
(ENV['COCOAPODS_CDN_MAX_CONCURRENCY'] || 200).to_i
- MAX_NUMBER_OF_RETRIES =
(ENV['COCOAPODS_CDN_MAX_NUMBER_OF_RETRIES'] || 5).to_i
- HYDRA_EXECUTOR =
Single thread executor for all network activity.
Concurrent::SingleThreadExecutor.new
Constants inherited
from Source
Source::DEFAULT_SPECS_BRANCH
Instance Attribute Summary
Attributes inherited from Source
#metadata, #repo
Querying the source
collapse
Searching the source
collapse
Instance Method Summary
collapse
Methods inherited from Source
#<=>, #diff_until_commit_hash, #fuzzy_search, #git_commit_hash, #git_tracking_branch, #inspect, #load_spec_gracefully, #metadata_path, #name, #pod_path, #pods_for_specification_paths, #repo_git, #set, #specification, #to_hash, #to_yaml, #unchanged_github_repo?, #update_git_repo, #verify_compatibility!
Constructor Details
#initialize(repo) ⇒ CDNSource
Returns a new instance of CDNSource.
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/cocoapods-core/cdn_source.rb', line 22
def initialize(repo)
@check_existing_files_for_update = false
@startup_time = Time.new
@version_arrays_by_fragment_by_name = {}
super(repo)
end
|
Instance Method Details
#algolia_search_index ⇒ Object
302
303
304
305
306
307
308
309
310
311
312
|
# File 'lib/cocoapods-core/cdn_source.rb', line 302
def algolia_search_index
@index ||= begin
require 'algoliasearch'
raise Informative, "Cannot perform full-text search in repo #{name} because it's missing Algolia config" if download_file('AlgoliaSearch.yml').nil?
algolia_config = YAMLHelper.load_string(local_file('AlgoliaSearch.yml', &:read))
client = Algolia::Client.new(:application_id => algolia_config['application_id'], :api_key => algolia_config['api_key'])
Algolia::Index.new(algolia_config['index'], client)
end
end
|
Returns all the specifications contained by the
source.
178
179
180
|
# File 'lib/cocoapods-core/cdn_source.rb', line 178
def all_specs
raise Informative, "Can't retrieve all the specs for a CDN-backed source, it will take forever"
end
|
#backoff_time(retries) ⇒ Object
435
436
437
438
|
# File 'lib/cocoapods-core/cdn_source.rb', line 435
def backoff_time(retries)
current_retry = MAX_NUMBER_OF_RETRIES - retries
4 * 2**current_retry
end
|
#concurrent_requests_catching_errors ⇒ Object
484
485
486
487
488
489
490
|
# File 'lib/cocoapods-core/cdn_source.rb', line 484
def concurrent_requests_catching_errors
yield
rescue MultipleErrors => e
errors = e.errors
raise Informative, "CDN: #{name} Repo update failed - #{e.errors.size} error(s):\n#{errors.join("\n")}"
end
|
#debug(message) ⇒ Object
476
477
478
479
480
481
482
|
# File 'lib/cocoapods-core/cdn_source.rb', line 476
def debug(message)
if defined?(Pod::UI)
Pod::UI.message(message)
else
CoreUI.puts(message)
end
end
|
#deprecated_local_podspecs ⇒ Object
78
79
80
81
82
83
|
# File 'lib/cocoapods-core/cdn_source.rb', line 78
def deprecated_local_podspecs
download_file('deprecated_podspecs.txt')
local_file('deprecated_podspecs.txt', &:to_a).
map { |f| Pathname.new(f.chomp) }.
select { |f| repo.join(f).exist? }
end
|
#download_and_save_with_retries_async(partial_url, file_remote_url, etag, retries = MAX_NUMBER_OF_RETRIES) ⇒ Object
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
# File 'lib/cocoapods-core/cdn_source.rb', line 374
def download_and_save_with_retries_async(partial_url, file_remote_url, etag, retries = MAX_NUMBER_OF_RETRIES)
path = repo + partial_url
etag_path = path.sub_ext(path.extname + '.etag')
download_task = download_typhoeus_impl_async(file_remote_url, etag).then do |response|
case response.response_code
when 301, 302
redirect_location = response.['location']
debug "CDN: #{name} Redirecting from #{file_remote_url} to #{redirect_location}"
download_and_save_with_retries_async(partial_url, redirect_location, etag)
when 304
debug "CDN: #{name} Relative path not modified: #{partial_url}"
FileUtils.touch path
partial_url
when 200
File.open(path, 'w') { |f| f.write(response.response_body.force_encoding('UTF-8')) }
etag_new = response.['etag'] unless response..nil?
debug "CDN: #{name} Relative path downloaded: #{partial_url}, save ETag: #{etag_new}"
File.open(etag_path, 'w') { |f| f.write(etag_new) } unless etag_new.nil?
partial_url
when 404
debug "CDN: #{name} Relative path couldn't be downloaded: #{partial_url} Response: #{response.response_code}"
nil
when 502, 503, 504
if retries <= 1
raise Informative, "CDN: #{name} URL couldn't be downloaded: #{file_remote_url} Response: #{response.response_code} #{response.response_body}"
else
debug "CDN: #{name} URL couldn't be downloaded: #{file_remote_url} Response: #{response.response_code} #{response.response_body}, retries: #{retries - 1}"
exponential_backoff_async(retries).then do
download_and_save_with_retries_async(partial_url, file_remote_url, etag, retries - 1)
end
end
when 0
if retries <= 1
raise Informative, "CDN: #{name} URL couldn't be downloaded: #{file_remote_url} Response: #{response.return_message}"
else
debug "CDN: #{name} URL couldn't be downloaded: #{file_remote_url} Response: #{response.return_message}, retries: #{retries - 1}"
exponential_backoff_async(retries).then do
download_and_save_with_retries_async(partial_url, file_remote_url, etag, retries - 1)
end
end
else
raise Informative, "CDN: #{name} URL couldn't be downloaded: #{file_remote_url} Response: #{response.response_code} #{response.response_body}"
end
end
download_task.run
end
|
#download_file(partial_url) ⇒ Object
340
341
342
343
344
345
|
# File 'lib/cocoapods-core/cdn_source.rb', line 340
def download_file(partial_url)
download_file_async(partial_url).wait!
end
|
#download_file_async(partial_url) ⇒ Object
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
# File 'lib/cocoapods-core/cdn_source.rb', line 347
def download_file_async(partial_url)
file_remote_url = Addressable::URI.encode(url + partial_url.to_s)
path = repo + partial_url
file_okay = local_file_okay?(partial_url)
if file_okay
if @startup_time < File.mtime(path)
debug "CDN: #{name} Relative path: #{partial_url} modified during this run! Returning local"
return Promises.fulfilled_future(partial_url, HYDRA_EXECUTOR)
end
unless @check_existing_files_for_update
debug "CDN: #{name} Relative path: #{partial_url} exists! Returning local because checking is only performed in repo update"
return Promises.fulfilled_future(partial_url, HYDRA_EXECUTOR)
end
end
path.dirname.mkpath
etag_path = path.sub_ext(path.extname + '.etag')
etag = File.read(etag_path) if file_okay && File.exist?(etag_path)
debug "CDN: #{name} Relative path: #{partial_url}, has ETag? #{etag}" unless etag.nil?
download_and_save_with_retries_async(partial_url, file_remote_url, etag)
end
|
#download_typhoeus_impl_async(file_remote_url, etag) ⇒ Object
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
# File 'lib/cocoapods-core/cdn_source.rb', line 445
def download_typhoeus_impl_async(file_remote_url, etag)
require 'typhoeus'
request = Typhoeus::Request.new(
file_remote_url,
:method => :get,
:http_version => :httpv2_0,
:timeout => 10,
:connecttimeout => 10,
:accept_encoding => 'gzip',
:netrc => :optional,
:netrc_file => Netrc.default_path,
:headers => etag.nil? ? {} : { 'If-None-Match' => etag },
)
future = Promises.resolvable_future_on(HYDRA_EXECUTOR)
queue_request(request)
request.on_complete do |response|
future.fulfill(response)
end
future
end
|
#ensure_versions_file_loaded(fragment) ⇒ Object
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
# File 'lib/cocoapods-core/cdn_source.rb', line 279
def ensure_versions_file_loaded(fragment)
return if !@version_arrays_by_fragment_by_name[fragment].nil? && !@check_existing_files_for_update
index_file_name = index_file_name_for_fragment(fragment)
download_file(index_file_name)
file_okay = local_file_okay?(index_file_name)
if file_okay
versions_raw = local_file(index_file_name, &:to_a).map(&:chomp)
@version_arrays_by_fragment_by_name[fragment] = versions_raw.reduce({}) do |hash, row|
row = row.split('/')
pod = row.shift
versions = row
hash[pod] = versions
hash
end
else
debug "CDN: #{name} Relative path: #{index_file_name} not available in this source set"
end
end
|
#exponential_backoff_async(retries) ⇒ Object
431
432
433
|
# File 'lib/cocoapods-core/cdn_source.rb', line 431
def exponential_backoff_async(retries)
sleep_async(backoff_time(retries))
end
|
#files_definitely_to_update ⇒ Object
74
75
76
|
# File 'lib/cocoapods-core/cdn_source.rb', line 74
def files_definitely_to_update
Pathname.glob(repo.join('**/*.{txt,yml}')).map { |f| f.relative_path_from(repo).to_s }
end
|
#git? ⇒ Boolean
269
270
271
|
# File 'lib/cocoapods-core/cdn_source.rb', line 269
def git?
false
end
|
#index_file_name_for_fragment(fragment) ⇒ Object
314
315
316
317
318
|
# File 'lib/cocoapods-core/cdn_source.rb', line 314
def index_file_name_for_fragment(fragment)
fragment_joined = fragment.join('_')
fragment_joined = '_' + fragment_joined unless fragment.empty?
"all_pods_versions#{fragment_joined}.txt"
end
|
#indexable? ⇒ Boolean
273
274
275
|
# File 'lib/cocoapods-core/cdn_source.rb', line 273
def indexable?
false
end
|
#local_file(partial_url) ⇒ Object
329
330
331
332
333
334
|
# File 'lib/cocoapods-core/cdn_source.rb', line 329
def local_file(partial_url)
file_path = repo.join(partial_url)
File.open(file_path) do |file|
yield file if block_given?
end
end
|
#local_file_okay?(partial_url) ⇒ Boolean
324
325
326
327
|
# File 'lib/cocoapods-core/cdn_source.rb', line 324
def local_file_okay?(partial_url)
file_path = repo.join(partial_url)
File.exist?(file_path) && File.size(file_path) > 0
end
|
#pod_sets ⇒ Array<Sets>
Returns the sets of all the Pods.
184
185
186
|
# File 'lib/cocoapods-core/cdn_source.rb', line 184
def pod_sets
raise Informative, "Can't retrieve all the pod sets for a CDN-backed source, it will take forever"
end
|
#pod_shard_fragment(pod_name) ⇒ Object
320
321
322
|
# File 'lib/cocoapods-core/cdn_source.rb', line 320
def pod_shard_fragment(pod_name)
metadata.path_fragment(pod_name)[0..-2]
end
|
#pods ⇒ Array<String>
Returns the list of the name of all the Pods.
96
97
98
99
|
# File 'lib/cocoapods-core/cdn_source.rb', line 96
def pods
download_file('all_pods.txt')
local_file('all_pods.txt', &:to_a).map(&:chomp)
end
|
#preheat_existing_files ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/cocoapods-core/cdn_source.rb', line 60
def preheat_existing_files
files_to_update = files_definitely_to_update + deprecated_local_podspecs - ['deprecated_podspecs.txt']
debug "CDN: #{name} Going to update #{files_to_update.count} files"
concurrent_requests_catching_errors do
loaders = files_to_update.map do |file|
download_file_async(file)
end
Promises.zip_futures_on(HYDRA_EXECUTOR, *loaders).wait!
end
end
|
#queue_request(request) ⇒ Object
492
493
494
495
496
497
498
499
500
501
502
503
504
505
|
# File 'lib/cocoapods-core/cdn_source.rb', line 492
def queue_request(request)
@hydra ||= Typhoeus::Hydra.new(:max_concurrency => MAX_CONCURRENCY)
@hydra.queue(request)
HYDRA_EXECUTOR.post(@hydra, &:run)
end
|
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/cocoapods-core/cdn_source.rb', line 46
def refresh_metadata
if metadata.nil?
unless repo.exist?
debug "CDN: Repo #{name} does not exist!"
return
end
specs_dir.mkpath
download_file('CocoaPods-version.yml')
end
super
end
|
#relative_pod_path(pod_name) ⇒ Object
336
337
338
|
# File 'lib/cocoapods-core/cdn_source.rb', line 336
def relative_pod_path(pod_name)
pod_path(pod_name).relative_path_from(repo)
end
|
#search(query) ⇒ Set
Note:
This method is optimized for fast lookups by name, i.e. it does
not require iterating through #pod_sets
Returns a set for a given dependency. The set is identified by the
name of the dependency and takes into account subspecs.
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
# File 'lib/cocoapods-core/cdn_source.rb', line 199
def search(query)
unless specs_dir
raise Informative, "Unable to find a source named: `#{name}`"
end
if query.is_a?(Dependency)
query = query.root_name
end
fragment = pod_shard_fragment(query)
ensure_versions_file_loaded(fragment)
version_arrays_by_name = @version_arrays_by_fragment_by_name[fragment] || {}
found = version_arrays_by_name[query].nil? ? nil : query
if found
set = set(query)
set if set.specification_name == query
end
end
|
#search_by_name(query, full_text_search = false) ⇒ Array<Set>
Note:
full text search requires to load the specification for each pod,
and therefore not supported.
Returns The list of the sets that contain the search term.
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
# File 'lib/cocoapods-core/cdn_source.rb', line 232
def search_by_name(query, full_text_search = false)
if full_text_search
require 'algoliasearch'
begin
algolia_result = algolia_search_index.search(query, :attributesToRetrieve => 'name')
names = algolia_result['hits'].map { |r| r['name'] }
names.map { |n| set(n) }.reject { |s| s.versions.compact.empty? }
rescue Algolia::AlgoliaError => e
raise Informative, "CDN: #{name} - Cannot perform full-text search because Algolia returned an error: #{e}"
end
else
super(query)
end
end
|
#sleep_async(seconds) ⇒ Object
440
441
442
443
|
# File 'lib/cocoapods-core/cdn_source.rb', line 440
def sleep_async(seconds)
Promises.schedule_on(HYDRA_EXECUTOR, seconds)
end
|
#specification_path(name, version) ⇒ Pathname
Returns the path of the specification with the given name and version.
161
162
163
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/cocoapods-core/cdn_source.rb', line 161
def specification_path(name, version)
raise ArgumentError, 'No name' unless name
raise ArgumentError, 'No version' unless version
unless versions(name).include?(Version.new(version))
raise StandardError, "Unable to find the specification #{name} " \
"(#{version}) in the #{self.name} source."
end
podspec_version_path_relative = Pathname.new(version.to_s).join("#{name}.podspec.json")
relative_podspec = relative_pod_path(name).join(podspec_version_path_relative).to_s
download_file(relative_podspec)
pod_path(name).join(podspec_version_path_relative)
end
|
#specs_dir ⇒ Pathname
Returns The directory where the specs are stored.
87
88
89
|
# File 'lib/cocoapods-core/cdn_source.rb', line 87
def specs_dir
@specs_dir ||= repo + 'Specs'
end
|
#type ⇒ String
Returns The type of the source.
42
43
44
|
# File 'lib/cocoapods-core/cdn_source.rb', line 42
def type
'CDN'
end
|
#update(_show_output) ⇒ Array<String>
Check update dates for all existing files.
Does not download non-existing specs, since CDN-backed repo is updated live.
255
256
257
258
259
260
261
262
263
|
# File 'lib/cocoapods-core/cdn_source.rb', line 255
def update(_show_output)
@check_existing_files_for_update = true
begin
preheat_existing_files
ensure
@check_existing_files_for_update = false
end
[]
end
|
#updateable? ⇒ Boolean
265
266
267
|
# File 'lib/cocoapods-core/cdn_source.rb', line 265
def updateable?
true
end
|
#url ⇒ String
Returns The URL of the source.
36
37
38
|
# File 'lib/cocoapods-core/cdn_source.rb', line 36
def url
@url ||= File.read(repo.join('.url')).chomp.chomp('/') + '/'
end
|
#versions(name) ⇒ Array<Version>
Returns all the available versions for the Pod, sorted
from highest to lowest.
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/cocoapods-core/cdn_source.rb', line 107
def versions(name)
return nil unless specs_dir
raise ArgumentError, 'No name' unless name
fragment = pod_shard_fragment(name)
ensure_versions_file_loaded(fragment)
return @versions_by_name[name] unless @versions_by_name[name].nil?
pod_path_actual = pod_path(name)
pod_path_relative = relative_pod_path(name)
return nil if @version_arrays_by_fragment_by_name.dig(fragment, name).nil?
concurrent_requests_catching_errors do
loaders = []
@versions_by_name[name] ||= @version_arrays_by_fragment_by_name[fragment][name].map do |version|
podspec_version_path_relative = Pathname.new(version).join("#{name}.podspec.json")
unless pod_path_actual.join(podspec_version_path_relative).exist?
loaders << download_file_async(pod_path_relative.join(podspec_version_path_relative).to_s)
end
begin
Version.new(version) if version[0, 1] != '.'
rescue ArgumentError
raise Informative, 'An unexpected version directory ' \
"`#{version}` was encountered for the " \
"`#{pod_path_actual}` Pod in the `#{name}` repository."
end
end.compact.sort.reverse
Promises.zip_futures_on(HYDRA_EXECUTOR, *loaders).wait!
end
@versions_by_name[name]
end
|