Class: HttpMimic::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/http_mimic/downloader.rb

Defined Under Namespace

Classes: DownloadError, UnsupportedPlatformError

Constant Summary collapse

DEFAULT_GITHUB_REPO =
'lexiforest/curl-impersonate'
DEFAULT_VERSION =
'v2.1.1'
DEFAULT_QJS_REPO =
'quickjs-ng/quickjs'
DEFAULT_QJS_VERSION =
'v0.16.2'

Class Method Summary collapse

Class Method Details

.available_binaries(install_dir: nil) ⇒ Object



176
177
178
179
180
181
182
183
# File 'lib/http_mimic/downloader.rb', line 176

def available_binaries(install_dir: nil)
  target_dir = File.expand_path(install_dir || HttpMimic.configuration.install_dir)
  return [] unless Dir.exist?(target_dir)

  Dir.glob(File.join(target_dir, '*')).select do |file|
    File.file?(file) && File.executable?(file)
  end.map { |f| File.basename(f) }
end

.binary_path(name, install_dir: nil) ⇒ Object



168
169
170
171
172
173
174
# File 'lib/http_mimic/downloader.rb', line 168

def binary_path(name, install_dir: nil)
  target_dir = File.expand_path(install_dir || HttpMimic.configuration.install_dir)
  candidate = File.join(target_dir, binary_name_for_platform(name))
  return candidate if File.file?(candidate) && File.executable?(candidate)

  nil
end

.download!(version: nil, install_dir: nil, repo: nil, force: false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/http_mimic/downloader.rb', line 23

def download!(version: nil, install_dir: nil, repo: nil, force: false)
  target_version = normalize_version(version || HttpMimic.configuration.driver_version || DEFAULT_VERSION)
  target_dir     = File.expand_path(install_dir || HttpMimic.configuration.install_dir)
  target_repo    = repo || HttpMimic.configuration.github_repo || DEFAULT_GITHUB_REPO

  FileUtils.mkdir_p(target_dir)

  # Check if the target version is already installed and intact
  if !force && installed?(version: target_version, install_dir: target_dir)
    log_info("curl-impersonate #{target_version} is already installed in #{target_dir}")
    return target_dir
  end

  slug = platform_slug
  asset_name = "curl-impersonate-#{target_version}.#{slug}.tar.gz"
  download_url = "https://github.com/#{target_repo}/releases/download/#{target_version}/#{asset_name}"

  log_info("Downloading curl-impersonate (#{target_version}) [#{asset_name}]...")

  archive_data = fetch_binary(download_url)

  log_info("Extracting archive to #{target_dir}...")
  extract_tar_gz(archive_data, target_dir)

  # Write version marker file
  File.write(version_file_path(target_dir), target_version)

  log_info("curl-impersonate #{target_version} installation complete!")
  target_dir
end

.download_qjs!(version: nil, install_dir: nil, repo: nil, force: false) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/http_mimic/downloader.rb', line 69

def download_qjs!(version: nil, install_dir: nil, repo: nil, force: false)
  target_version = normalize_version(version || HttpMimic.configuration.qjs_version || DEFAULT_QJS_VERSION)
  target_dir     = File.expand_path(install_dir || HttpMimic.configuration.install_dir)
  target_repo    = repo || HttpMimic.configuration.qjs_github_repo || DEFAULT_QJS_REPO

  FileUtils.mkdir_p(target_dir)

  dest_binary = File.join(target_dir, binary_name_for_platform('qjs'))

  if !force && qjs_installed?(version: target_version, install_dir: target_dir)
    log_info("qjs #{target_version} is already installed in #{target_dir}")
    return dest_binary
  end

  asset_name = qjs_platform_asset
  download_url = "https://github.com/#{target_repo}/releases/download/#{target_version}/#{asset_name}"

  log_info("Downloading QuickJS (#{target_version}) [#{asset_name}]...")
  binary_data = fetch_binary(download_url)

  File.binwrite(dest_binary, binary_data)
  File.chmod(0755, dest_binary)

  File.write(qjs_version_file_path(target_dir), target_version)

  log_info("QuickJS #{target_version} installation complete! (#{dest_binary})")
  dest_binary
end

.installed?(version: nil, install_dir: nil) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/http_mimic/downloader.rb', line 54

def installed?(version: nil, install_dir: nil)
  target_dir = File.expand_path(install_dir || HttpMimic.configuration.install_dir)
  core_binary = File.join(target_dir, binary_name_for_platform('curl-impersonate'))
  return false unless File.file?(core_binary) && File.executable?(core_binary)

  if version
    target_version = normalize_version(version)
    v_file = version_file_path(target_dir)
    return false unless File.file?(v_file)
    return File.read(v_file).strip == target_version
  end

  true
end

.platform_slugObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/http_mimic/downloader.rb', line 185

def platform_slug
  os  = host_os
  cpu = host_cpu

  case os
  when :macos
    case cpu
    when :arm64 then 'arm64-macos'
    when :x86_64 then 'x86_64-macos'
    else
      raise UnsupportedPlatformError, "Unsupported macOS CPU architecture: #{cpu}"
    end
  when :linux
    libc = linux_libc
    case cpu
    when :x86_64
      libc == :musl ? 'x86_64-linux-musl' : 'x86_64-linux-gnu'
    when :aarch64, :arm64
      libc == :musl ? 'aarch64-linux-musl' : 'aarch64-linux-gnu'
    when :i386, :i686
      'i386-linux-gnu'
    when :arm
      'arm-linux-gnueabihf'
    when :riscv64
      'riscv64-linux-gnu'
    when :loongarch64
      'loongarch64-linux-gnu'
    else
      raise UnsupportedPlatformError, "Unsupported Linux CPU architecture: #{cpu}"
    end
  when :windows
    case cpu
    when :x86_64 then 'x86_64-win32'
    when :arm64 then 'arm64-win32'
    when :i386, :i686 then 'i686-win32'
    else
      raise UnsupportedPlatformError, "Unsupported Windows CPU architecture: #{cpu}"
    end
  when :freebsd
    case cpu
    when :x86_64 then 'x86_64-freebsd'
    when :arm64, :aarch64 then 'aarch64-freebsd'
    else
      raise UnsupportedPlatformError, "Unsupported FreeBSD CPU architecture: #{cpu}"
    end
  else
    raise UnsupportedPlatformError, "Unsupported operating system: #{RbConfig::CONFIG['host_os']}"
  end
end

.qjs_installed?(version: nil, install_dir: nil) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/http_mimic/downloader.rb', line 98

def qjs_installed?(version: nil, install_dir: nil)
  target_dir = File.expand_path(install_dir || HttpMimic.configuration.install_dir)
  qjs_bin = File.join(target_dir, binary_name_for_platform('qjs'))
  return false unless File.file?(qjs_bin) && File.executable?(qjs_bin)

  if version
    target_version = normalize_version(version)
    v_file = qjs_version_file_path(target_dir)
    return false unless File.file?(v_file)
    return File.read(v_file).strip == target_version
  end

  true
end

.qjs_path(install_dir: nil) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/http_mimic/downloader.rb', line 113

def qjs_path(install_dir: nil)
  target_dir = File.expand_path(install_dir || HttpMimic.configuration.install_dir)
  candidate = File.join(target_dir, binary_name_for_platform('qjs'))
  return candidate if File.file?(candidate) && File.executable?(candidate)

  # Fallback to system PATH
  sys_qjs = `which qjs 2>/dev/null`.strip
  return sys_qjs if !sys_qjs.empty? && File.executable?(sys_qjs)

  nil
end

.qjs_platform_assetObject



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
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/http_mimic/downloader.rb', line 125

def qjs_platform_asset
  os  = host_os
  cpu = host_cpu

  case os
  when :macos
    case cpu
    when :arm64 then 'qjs-darwin-arm64'
    when :x86_64 then 'qjs-darwin-x86_64'
    else
      raise UnsupportedPlatformError, "Unsupported macOS CPU architecture for QuickJS: #{cpu}"
    end
  when :linux
    case cpu
    when :x86_64
      'qjs-linux-x86_64'
    when :aarch64, :arm64
      'qjs-linux-aarch64'
    when :arm
      'qjs-linux-armv7'
    when :i386, :i686
      'qjs-linux-x86'
    when :riscv64
      'qjs-linux-riscv64'
    else
      raise UnsupportedPlatformError, "Unsupported Linux CPU architecture for QuickJS: #{cpu}"
    end
  when :windows
    case cpu
    when :x86_64 then 'qjs-windows-x86_64.exe'
    when :i386, :i686 then 'qjs-windows-x86.exe'
    else
      raise UnsupportedPlatformError, "Unsupported Windows CPU architecture for QuickJS: #{cpu}"
    end
  else
    raise UnsupportedPlatformError, "Unsupported operating system for QuickJS: #{RbConfig::CONFIG['host_os']}"
  end
end

.qjs_version_file_path(dir) ⇒ Object



164
165
166
# File 'lib/http_mimic/downloader.rb', line 164

def qjs_version_file_path(dir)
  File.join(dir, '.qjs_version')
end