Commit 5c988ac066ee3f26164a94cb0e8be3cf7524ef0f

Michael Crumm 2022-01-19T11:34:43

Support http/https proxies, closes #10

diff --git a/CHANGELOG.md b/CHANGELOG.md
index b95f3ad..d116b3f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@
 
 ## v0.3.1
 
+  * Support `HTTP_PROXY/HTTPS_PROXY` to fetch esbuild (h/t @iaddict)
   * Update Sass version to `1.43.4`
   * Allow `config :dart_sass, :sass_path, path` to configure the path to the Sass snapshot (or executable)
   * Allow `config :dart_sass, :dart_path, path` to configure the path to the Dart executable
diff --git a/lib/dart_sass.ex b/lib/dart_sass.ex
index 92a7f0c..c4342ce 100644
--- a/lib/dart_sass.ex
+++ b/lib/dart_sass.ex
@@ -314,6 +314,18 @@ defmodule DartSass do
     {:ok, _} = Application.ensure_all_started(:inets)
     {:ok, _} = Application.ensure_all_started(:ssl)
 
+    if proxy = System.get_env("HTTP_PROXY") || System.get_env("http_proxy") do
+      Logger.debug("Using HTTP_PROXY: #{proxy}")
+      %{host: host, port: port} = URI.parse(proxy)
+      :httpc.set_options([{:proxy, {{String.to_charlist(host), port}, []}}])
+    end
+
+    if proxy = System.get_env("HTTPS_PROXY") || System.get_env("https_proxy") do
+      Logger.debug("Using HTTPS_PROXY: #{proxy}")
+      %{host: host, port: port} = URI.parse(proxy)
+      :httpc.set_options([{:https_proxy, {{String.to_charlist(host), port}, []}}])
+    end
+
     # https://erlef.github.io/security-wg/secure_coding_and_deployment_hardening/inets
     cacertfile = CAStore.file_path() |> String.to_charlist()