Skip to content

Commit 6ee14fc

Browse files
authored
feat: support ASTRAL_BIN_PATH (#169)
* feat: support ASTRAL_BIN_PATH * doc * lint
1 parent 02f6d9c commit 6ee14fc

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

docs/pages/advanced/env_vars.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
title: Env vars
3+
description:
4+
index: 5
5+
---
6+
7+
Astral lets you configure some of its behavior using environment variables.
8+
9+
## `ASTRAL_QUIET_INSTALL`
10+
11+
By default, if Astral needs to install a browser binary, it will print progress
12+
information to the console. You can disable this behavior by setting the
13+
`ASTRAL_QUIET_INSTALL` environment variable to `1`.
14+
15+
If `CI` is set to `true`, Astral will assume `ASTRAL_QUIET_INSTALL`.
16+
17+
## `ASTRAL_BIN_PATH`
18+
19+
If you already installed a supported browser binary, you can force Astral to use
20+
it by setting the `ASTRAL_BIN_PATH` environment variable.
21+
22+
The `getBinary()` function will then always return that path.
23+
24+
This is especially useful in containerized environments where you might reuse an
25+
existing base image or pre-install the browser binary yourself.
26+
27+
```dockerfile
28+
RUN apt-get install -y google-chrome-stable
29+
ENV ASTRAL_BIN_PATH=/usr/bin/google-chrome-stable
30+
```
31+
32+
## `ASTRAL_BIN_ARGS`
33+
34+
You may want to pass additional arguments to the browser binary when launching
35+
it without having to change your Astral `launch()` calls.
36+
37+
It can be achieved by setting the `ASTRAL_BIN_ARGS` environment variable.
38+
Arguments passed via this variable will be appended to any arguments passed to
39+
the `launch()` function.
40+
41+
This is especially useful in containerized environments where you might need to
42+
pass specific arguments to make the browser work properly.
43+
44+
```dockerfile
45+
ENV ASTRAL_BIN_ARGS="--no-sandbox"
46+
```

src/cache.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,15 @@ export async function getBinary(
169169
browser: "chrome" | "firefox",
170170
{ cache = getDefaultCachePath(), timeout = 60000 } = {},
171171
): Promise<string> {
172+
if (
173+
(await Deno.permissions.query({
174+
name: "env",
175+
variable: "ASTRAL_BIN_PATH",
176+
})).state === "granted" && (Deno.env.get("ASTRAL_BIN_PATH"))
177+
) {
178+
return Deno.env.get("ASTRAL_BIN_PATH")!;
179+
}
180+
172181
// TODO(lino-levan): fix firefox downloading
173182
const VERSION = SUPPORTED_VERSIONS[browser];
174183
const product = `${browser}-${SUPPORTED_VERSIONS[browser]}`;

tests/get_binary_test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { assertMatch, assertRejects, assertStringIncludes } from "@std/assert";
1+
import {
2+
assertEquals,
3+
assertMatch,
4+
assertRejects,
5+
assertStringIncludes,
6+
} from "@std/assert";
27
import { assert } from "@std/assert/assert";
38
import { resolve } from "@std/path/resolve";
49
import { cleanCache, getBinary, launch } from "../mod.ts";
@@ -75,3 +80,22 @@ Deno.test("Test download after failure", { permissions }, async () => {
7580
Deno.test("Clean cache after tests", async () => {
7681
await cleanCache({ cache });
7782
});
83+
84+
Deno.test(
85+
"Use path from environment variable if specified",
86+
{ permissions: { env: true } },
87+
async () => {
88+
const env = Deno.env.get("ASTRAL_BIN_PATH");
89+
try {
90+
const path = "/usr/bin/google-chrome-stable";
91+
Deno.env.set("ASTRAL_BIN_PATH", path);
92+
assertEquals(await getBinary("chrome"), path);
93+
} finally {
94+
if (typeof env === "string") {
95+
Deno.env.set("ASTRAL_BIN_PATH", env);
96+
} else {
97+
Deno.env.delete("ASTRAL_BIN_PATH");
98+
}
99+
}
100+
},
101+
);

0 commit comments

Comments
 (0)