prebake 0.3.6 → 0.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 161b7e1eed05964edde295c9cc1aaa60a7ed90f82c44c14fdd37c9e5cc494a0b
4
- data.tar.gz: be30dacaea00707f20fe812ef14239df3040a9770a333c70bf469cb01f66a11d
3
+ metadata.gz: 19a1c62dd2df6b16ab27629bda27ea6d6c18ae200c6e5874e1de7a17a642b643
4
+ data.tar.gz: ddcba7e497fa98ce7955fb0c78804a64945f5a8d64b6fa017d56b23a32f624fe
5
5
  SHA512:
6
- metadata.gz: 7572d756db73f2518fa95e885bd89456f55b71c27dab1fa52c51ed0459617b2dd19950e64a286147759fd613ce30f9560a28d1ade8500d2ef9badf105a75c6e6
7
- data.tar.gz: 10f5df9f52a3f23af1c875eaabd2de0dcb3ea24e14fee4cc60f6e223b244596e4c728444f3d6318d8279041d132cf51ecb2af3eca95587aaaa240eb1fb538e14
6
+ metadata.gz: d5e9a824142b4bb5734a917ed9b52fc2ec75286fee31bd41129f0b7aeadffd4e918ff368a64a64e0262b365823e448d505f7a9e5af1d69380ba78c2cc816c74a
7
+ data.tar.gz: 33687eb0a1d9ddf94b2b1773b2a9548783c93280c8731a1cc6c56bc598543ec339fdaebf336986b1f4727ef759795ea2dae048907b73fc5d715b4921c800fb2c
@@ -13,6 +13,11 @@ module Prebake
13
13
  nil
14
14
  end
15
15
 
16
+ def fetch_with_checksum(cache_key)
17
+ path = fetch(cache_key)
18
+ [path, path && fetch_checksum(cache_key)]
19
+ end
20
+
16
21
  def push(gem_path, cache_key, checksum)
17
22
  raise NotImplementedError, "#{self.class}#push not implemented"
18
23
  end
@@ -11,6 +11,9 @@ module Prebake
11
11
  class Http < Base
12
12
  include HttpClient
13
13
 
14
+ CHECKSUM_HEADER = "X-Prebake-Sha256"
15
+ SHA256_HEX = /\A[0-9a-f]{64}\z/
16
+
14
17
  def initialize(url:, token: nil)
15
18
  @url = url.chomp("/")
16
19
  @token = token
@@ -19,15 +22,16 @@ module Prebake
19
22
  end
20
23
 
21
24
  def fetch(cache_key)
22
- response = http_request(:get, gem_uri(cache_key))
23
- return nil unless response.is_a?(Net::HTTPSuccess)
25
+ download(cache_key)&.first
26
+ end
24
27
 
25
- path = File.join(Dir.tmpdir, "prebake-#{SecureRandom.hex(16)}.gem")
26
- File.binwrite(path, response.body)
27
- path
28
- rescue StandardError => e
29
- Logger.debug "Fetch failed for #{cache_key}: #{e.message}"
30
- nil
28
+ # The hosted cache sends the checksum with the gem, so a hit costs one
29
+ # request. For servers that don't, the .sha256 file is fetched instead.
30
+ def fetch_with_checksum(cache_key)
31
+ path, checksum = download(cache_key)
32
+ return [nil, nil] unless path
33
+
34
+ [path, checksum || fetch_checksum(cache_key)]
31
35
  end
32
36
 
33
37
  def fetch_checksum(cache_key)
@@ -82,6 +86,19 @@ module Prebake
82
86
 
83
87
  private
84
88
 
89
+ def download(cache_key)
90
+ response = http_request(:get, gem_uri(cache_key))
91
+ return unless response.is_a?(Net::HTTPSuccess)
92
+
93
+ path = File.join(Dir.tmpdir, "prebake-#{SecureRandom.hex(16)}.gem")
94
+ File.binwrite(path, response.body)
95
+ checksum = response[CHECKSUM_HEADER]
96
+ [path, (checksum if checksum&.match?(SHA256_HEX))]
97
+ rescue StandardError => e
98
+ Logger.debug "Fetch failed for #{cache_key}: #{e.message}"
99
+ nil
100
+ end
101
+
85
102
  def gem_uri(cache_key)
86
103
  URI("#{@url}/gems/#{cache_key}")
87
104
  end
@@ -1,11 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "net/http"
4
+ require "timeout"
4
5
 
5
6
  module Prebake
6
7
  module Backends
7
8
  module HttpClient
8
9
  TIMEOUT = 30
10
+ # Net::HTTP's timeouts restart on every read, so a trickling response needs a total cap.
11
+ DEADLINE = 10
9
12
  HTTP_METHODS = { get: Net::HTTP::Get, head: Net::HTTP::Head,
10
13
  post: Net::HTTP::Post, put: Net::HTTP::Put,
11
14
  delete: Net::HTTP::Delete }.freeze
@@ -13,18 +16,27 @@ module Prebake
13
16
  private
14
17
 
15
18
  def http_request(method, uri, body: nil)
19
+ # After one failed download the cache is likely struggling, so the rest of the install builds from source.
20
+ raise Error, "Skipped: an earlier request to the cache failed" if @unavailable && !body
21
+
16
22
  request = build_http_request(method, uri)
17
23
  if body
18
24
  request["Content-Type"] = "application/octet-stream"
19
25
  request.body = body
20
26
  end
21
27
 
22
- Net::HTTP.start(uri.host, uri.port,
23
- use_ssl: uri.scheme == "https",
24
- open_timeout: TIMEOUT,
25
- read_timeout: TIMEOUT) do |http|
26
- http.request(request)
28
+ # Uploads carry whole gems, so they keep only the per-phase timeouts.
29
+ Timeout.timeout(body ? nil : DEADLINE) do
30
+ Net::HTTP.start(uri.host, uri.port,
31
+ use_ssl: uri.scheme == "https",
32
+ open_timeout: TIMEOUT,
33
+ read_timeout: TIMEOUT) do |http|
34
+ http.request(request)
35
+ end
27
36
  end
37
+ rescue StandardError
38
+ @unavailable = true unless body
39
+ raise
28
40
  end
29
41
 
30
42
  def build_http_request(method, uri)
@@ -34,15 +34,8 @@ module Prebake
34
34
  platform = Platform.generalized
35
35
  cache_key = CacheKey.for(@spec.name, @spec.version.to_s, platform)
36
36
 
37
- expected_checksum = Prebake.backend.fetch_checksum(cache_key)
38
-
39
- if expected_checksum.nil? && Prebake.backend.checksums_supported?
40
- trigger_rebuild(cache_key)
41
- return super
42
- end
43
-
44
37
  begin
45
- cached_gem = Prebake.backend.fetch(cache_key)
38
+ cached_gem, expected_checksum = Prebake.backend.fetch_with_checksum(cache_key)
46
39
  rescue StandardError => e
47
40
  Logger.debug "Cache fetch error for #{@spec.name}: #{e.message}"
48
41
  return super
@@ -53,6 +46,11 @@ module Prebake
53
46
  return super
54
47
  end
55
48
 
49
+ if expected_checksum.nil? && Prebake.backend.checksums_supported?
50
+ trigger_rebuild(cache_key)
51
+ return super
52
+ end
53
+
56
54
  if checksum_matches?(cache_key, expected_checksum, cached_gem)
57
55
  unless PortabilityGuard.portable_for_host?(cached_gem, spec_name: @spec.name)
58
56
  # Binary is valid for other hosts; don't delete from backend.
@@ -83,7 +81,9 @@ module Prebake
83
81
 
84
82
  def trigger_rebuild(cache_key)
85
83
  Logger.warn "No checksum available for #{cache_key}, removing cached gem"
86
- Prebake.backend.delete(cache_key)
84
+ # Without the delete the gem is still there, so the GET below would only download it again.
85
+ return unless Prebake.backend.delete(cache_key)
86
+
87
87
  # GET triggers the worker to dispatch a rebuild via GH Actions on cache miss
88
88
  leftover = Prebake.backend.fetch(cache_key)
89
89
  FileUtils.rm_f(leftover) if leftover
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prebake
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thejus Paul
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-10-25 00:00:00.000000000 Z
11
+ date: 2026-10-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Prebake speeds up bundle install by skipping native gem compilation.
14
14
  It fetches precompiled binaries for gems like puma, nokogiri, pg, grpc, and bootsnap