• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

camatcode / basenji / f99a53d32dd7a2c0ab9fe7d09a5367c1fce48a87

16 Jul 2025 11:59PM UTC coverage: 58.437%. First build
f99a53d32dd7a2c0ab9fe7d09a5367c1fce48a87

Pull #49

github

camatcode
fix: EPIPE error on github envs
Pull Request #49: perf: performance improvements

27 of 57 new or added lines in 10 files covered. (47.37%)

942 of 1612 relevant lines covered (58.44%)

347.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.37
/lib/basenji/reader/process/jpeg_optimizer.ex
1
defmodule Basenji.Reader.Process.JPEGOptimizer do
2
  @moduledoc false
3

4
  import Basenji.Reader
5

6
  def optimize(jpeg_bytes, opts \\ [])
2✔
7

8
  def optimize(list, opts) when is_list(list) do
9
    :binary.list_to_bin(list) |> optimize(opts)
257✔
10
  end
11

12
  def optimize(<<0xFF, 0xD8, 0xFF, _::binary>> = jpeg_bytes, opts), do: optimize_impl(jpeg_bytes, opts)
287✔
13

14
  def optimize(not_jpeg_bytes, _) when is_binary(not_jpeg_bytes), do: {:ok, not_jpeg_bytes}
1✔
15

16
  def optimize!(jpeg_bytes, opts \\ []) do
17
    with {:ok, optimized_bytes} <- optimize(jpeg_bytes, opts) do
286✔
18
      optimized_bytes
286✔
19
    end
20
  end
21

22
  defp optimize_impl(bytes, _opts) when is_binary(bytes) do
23
    # Use file-based approach in CI environments to avoid epipe issues
24
    # GitHub Actions and other CI systems can have issues with stdin/stdout pipes
25
    use_file_mode =
287✔
26
      System.get_env("CI") == "true" or
287✔
NEW
27
        System.get_env("GITHUB_ACTIONS") == "true" or
×
NEW
28
        System.get_env("BASENJI_JPEG_FILE_MODE") == "true"
×
29

30
    if use_file_mode do
287✔
31
      optimize_with_file(bytes)
287✔
32
    else
33
      # Try stdin first for local development (more efficient), fallback to file on error
NEW
34
      case optimize_with_stdin(bytes) do
×
NEW
35
        {:ok, result} -> {:ok, result}
×
NEW
36
        {:error, _} -> optimize_with_file(bytes)
×
37
      end
38
    end
39
  end
40

41
  defp optimize_with_stdin(bytes) do
NEW
42
    cmd = "jpegoptim"
×
NEW
43
    cmd_opts = ["-f", "--stdout", "-q", "--stdin"]
×
NEW
44
    exec(cmd, cmd_opts, in: bytes)
×
45
  end
46

47
  defp optimize_with_file(bytes) do
48
    cmd = "jpegoptim"
287✔
49

50
    tmp_dir = System.tmp_dir!() |> Path.join("basenji") |> Path.join("jpeg_optimize")
287✔
51
    :ok = File.mkdir_p!(tmp_dir)
287✔
52
    path = Path.join(tmp_dir, "#{System.monotonic_time(:nanosecond)}.jpg")
287✔
53

54
    try do
287✔
55
      :ok = File.write!(path, bytes)
287✔
56
      cmd_opts = ["-f", "--stdout", "-q", path]
287✔
57
      exec(cmd, cmd_opts)
287✔
58
    after
59
      # Ensure cleanup even if exec fails
60
      File.rm(path)
287✔
61
    end
62
  end
63
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc