Skip to content

Commit

Permalink
Merge pull request #32 from lowlighter/fix-dont-require-env-perms-wit…
Browse files Browse the repository at this point in the history
…h-cache

fix: don't require --allow-env perms when cache param is specified
  • Loading branch information
lino-levan authored Oct 19, 2023
2 parents 484c45a + de2021a commit 2e82aeb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export interface LaunchOptions {
product?: "chrome" | "firefox";
args?: string[];
wsEndpoint?: string;
cache?: string;
}

/**
Expand All @@ -190,6 +191,7 @@ export async function launch(opts?: LaunchOptions) {
const product = opts?.product ?? "chrome";
const args = opts?.args ?? [];
const wsEndpoint = opts?.wsEndpoint;
const cache = opts?.cache;
let path = opts?.path;

const options: BrowserOptions = {
Expand All @@ -205,7 +207,7 @@ export async function launch(opts?: LaunchOptions) {
}

if (!path) {
path = await getBinary(product);
path = await getBinary(product, { cache });
}

// Launch child process
Expand Down
21 changes: 13 additions & 8 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ export const SUPPORTED_VERSIONS = {
firefox: "116.0",
} as const;

const HOME_PATH = Deno.build.os === "windows"
? Deno.env.get("USERPROFILE")!
: Deno.env.get("HOME")!;
const BASE_PATH = resolve(HOME_PATH, ".astral");
const CONFIG_FILE = "cache.json";

const LOCK_FILES = {} as { [cache: string]: { [product: string]: Lock } };
Expand All @@ -42,7 +38,16 @@ async function knownGoodVersions(): Promise<KnownGoodVersions> {
return await req.json();
}

function getCachedConfig({ cache = BASE_PATH }): Record<string, string> {
function getDefaultCachePath() {
const HOME_PATH = Deno.build.os === "windows"
? Deno.env.get("USERPROFILE")!
: Deno.env.get("HOME")!;
return resolve(HOME_PATH, ".astral");
}

function getCachedConfig(
{ cache = getDefaultCachePath() } = {},
): Record<string, string> {
try {
return JSON.parse(Deno.readTextFileSync(resolve(cache, CONFIG_FILE)));
} catch {
Expand All @@ -53,7 +58,7 @@ function getCachedConfig({ cache = BASE_PATH }): Record<string, string> {
/**
* Clean cache
*/
export async function cleanCache({ cache = BASE_PATH } = {}) {
export async function cleanCache({ cache = getDefaultCachePath() } = {}) {
try {
if (await exists(cache)) {
delete LOCK_FILES[cache];
Expand Down Expand Up @@ -126,7 +131,7 @@ async function decompressArchive(source: string, destination: string) {
*/
export async function getBinary(
browser: "chrome" | "firefox",
{ cache = BASE_PATH, timeout = 60000 } = {},
{ cache = getDefaultCachePath(), timeout = 60000 } = {},
): Promise<string> {
// TODO(lino-levan): fix firefox downloading
const VERSION = SUPPORTED_VERSIONS[browser];
Expand Down Expand Up @@ -257,7 +262,7 @@ export async function getBinary(
class Lock {
readonly path;

constructor({ cache = BASE_PATH } = {}) {
constructor({ cache = getDefaultCachePath() } = {}) {
this.path = resolve(cache, ".lock");
}

Expand Down
1 change: 0 additions & 1 deletion tests/get_binary_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const permissions = {
],
read: [cache],
net: true,
env: true,
run: true,
};

Expand Down

0 comments on commit 2e82aeb

Please sign in to comment.