Class: Shadwire::RegistryClient

Inherits:
Object
  • Object
show all
Defined in:
lib/shadwire/registry_client.rb

Overview

Fetches the published registry over file:// or http(s)://. Both #index and #item are memoized within the instance.

Constant Summary collapse

MAX_REDIRECTS =

Registries move: GitHub Pages answers 301 for every path once a custom domain is configured. Follow a few hops, then assume a misconfiguration rather than chasing a chain forever.

5

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url) ⇒ RegistryClient

Returns a new instance of RegistryClient.



40
41
42
43
44
# File 'lib/shadwire/registry_client.rb', line 40

def initialize(base_url)
  # Normalise: drop any trailing slash so URL building is consistent.
  @base_url = base_url.chomp("/")
  @cache = {}
end

Class Method Details

.redirect_target(from_uri, location) ⇒ Object

Resolves a redirect's Location against the URL it came from, rejecting redirects we must not follow. A registry serves source code that gets written into the user's app, so a redirect may upgrade to HTTPS but never drop it (GitHub Pages answers Location: http://… for a custom domain until "Enforce HTTPS" is on).



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/shadwire/registry_client.rb', line 20

def self.redirect_target(from_uri, location)
  if location.to_s.strip.empty?
    raise RegistryError, "Registry redirect from #{from_uri} has no Location header"
  end

  begin
    target = from_uri + location.strip
  rescue URI::Error => e
    raise RegistryError,
          "Registry redirect from #{from_uri} has an unusable Location (#{location}): #{e.message}"
  end

  if from_uri.scheme == "https" && target.scheme != "https"
    raise RegistryError,
          "Registry redirect from #{from_uri} drops HTTPS (to #{target}); refusing to follow it"
  end

  target
end

Instance Method Details

#indexObject

Returns the parsed index.json hash (memoized).



47
48
49
# File 'lib/shadwire/registry_client.rb', line 47

def index
  @cache[:__index__] ||= fetch("index.json")
end

#item(name) ⇒ Object

Returns the parsed name.json hash (memoized per name). Raises Shadwire::RegistryError if the item is not found.



53
54
55
# File 'lib/shadwire/registry_client.rb', line 53

def item(name)
  @cache[name] ||= fetch("#{name}.json", name:)
end