Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cacheMode option #267

Merged
merged 11 commits into from
Jul 15, 2024
Prev Previous commit
Next Next commit
update to use enum
MarshallOfSound committed Jul 12, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit c6f673e01ecf19fff90b1935588a75b9a691e5d1
30 changes: 26 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -82,6 +82,28 @@ export interface ElectronDownloadRequest {
artifactName: string;
}

export enum ElectronDownloadCacheMode {
/**
* Reads from the cache if present
* Writes to the cache after fetch if not present
*/
ReadWrite,
/**
* Reads from the cache if present
* Will **not** write back to the cache after fetching missing artifact
*/
ReadOnly,
/**
* Skips reading from the cache
* Will write back into the cache, overwriting anything currently in the cache after fetch
*/
WriteOnly,
/**
* Bypasses the cache completely, neither reads from nor writes to the cache
*/
Bypass,
}

/**
* @category Download Electron
*/
@@ -90,6 +112,7 @@ export interface ElectronDownloadRequestOptions {
* Whether to download an artifact regardless of whether it's in the cache directory.
*
* @defaultValue `false`
* @deprecated This option is depracated and directly maps to `cacheMode: ElectronDownloadCacheMode.WriteOnly`
*/
force?: boolean;
/**
@@ -149,12 +172,11 @@ export interface ElectronDownloadRequestOptions {
*/
tempDirectory?: string;
/**
* When set to `true`, do not put the downloaded artifact into the cache, leaving it
* in the temporary directory. You need to clean up the temporary directory yourself.
* Controls the cache read and write behavior.
*
* Defaults to `false`.
* Defaults to `ElectronDownloadCacheMode.ReadWrite`.
*/
dontCache?: boolean;
cacheMode?: ElectronDownloadCacheMode;
}

/**
41 changes: 41 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -2,6 +2,11 @@ import * as childProcess from 'child_process';
import * as fs from 'fs-extra';
import * as os from 'os';
import * as path from 'path';
import {
ElectronDownloadCacheMode,
ElectronGenericArtifactDetails,
ElectronPlatformArtifactDetailsWithDefaults,
} from './types';

async function useAndRemoveDirectory<T>(
directory: string,
@@ -134,3 +139,39 @@ export function setEnv(key: string, value: string | undefined): void {
process.env[key] = value;
}
}

export function effectiveCacheMode(
artifactDetails: ElectronPlatformArtifactDetailsWithDefaults | ElectronGenericArtifactDetails,
): ElectronDownloadCacheMode {
if (artifactDetails.force) {
if (artifactDetails.cacheMode) {
throw new Error(
'Setting both "force" and "cacheMode" is not supported, please exclusively use "cacheMode"',
);
}
return ElectronDownloadCacheMode.ReadWrite;
}

return artifactDetails.cacheMode || ElectronDownloadCacheMode.ReadWrite;
}

export function shouldTryReadCache(cacheMode: ElectronDownloadCacheMode): boolean {
return (
cacheMode === ElectronDownloadCacheMode.ReadOnly ||
cacheMode === ElectronDownloadCacheMode.ReadWrite
);
}

export function shouldWriteCache(cacheMode: ElectronDownloadCacheMode): boolean {
return (
cacheMode === ElectronDownloadCacheMode.WriteOnly ||
cacheMode === ElectronDownloadCacheMode.ReadWrite
);
}

export function doesCallerOwnTemporaryOutput(cacheMode: ElectronDownloadCacheMode): boolean {
return (
cacheMode === ElectronDownloadCacheMode.Bypass ||
cacheMode === ElectronDownloadCacheMode.ReadOnly
);
}
18 changes: 9 additions & 9 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ import * as path from 'path';

import { FixtureDownloader } from './FixtureDownloader';
import { download, downloadArtifact } from '../src';
import { DownloadOptions } from '../src/types';
import { DownloadOptions, ElectronDownloadCacheMode } from '../src/types';
import * as sumchecker from 'sumchecker';

jest.mock('sumchecker');
@@ -132,11 +132,11 @@ describe('Public API', () => {
process.env.ELECTRON_CUSTOM_VERSION = '';
});

it('should not put artifact in cache when dontCache=true', async () => {
it('should not put artifact in cache when cacheMode=ReadOnly', async () => {
const zipPath = await download('2.0.10', {
cacheRoot,
downloader,
dontCache: true,
cacheMode: ElectronDownloadCacheMode.ReadOnly,
});
expect(typeof zipPath).toEqual('string');
expect(await fs.pathExists(zipPath)).toEqual(true);
@@ -145,7 +145,7 @@ describe('Public API', () => {
expect(fs.readdirSync(cacheRoot).length).toEqual(0);
});

it('should use cache hits when dontCache=true', async () => {
it('should use cache hits when cacheMode=ReadOnly', async () => {
const zipPath = await download('2.0.9', {
cacheRoot,
downloader,
@@ -154,7 +154,7 @@ describe('Public API', () => {
const zipPath2 = await download('2.0.9', {
cacheRoot,
downloader,
dontCache: true,
cacheMode: ElectronDownloadCacheMode.ReadOnly,
});
expect(zipPath2).not.toEqual(zipPath);
expect(path.dirname(zipPath2).startsWith(cacheRoot)).toEqual(false);
@@ -264,15 +264,15 @@ describe('Public API', () => {
expect(path.basename(zipPath)).toMatchInlineSnapshot(`"electron-v2.0.10-darwin-x64.zip"`);
});

it('should not put artifact in cache when dontCache=true', async () => {
it('should not put artifact in cache when cacheMode=ReadOnly', async () => {
const driverPath = await downloadArtifact({
cacheRoot,
downloader,
version: '2.0.9',
artifactName: 'chromedriver',
platform: 'darwin',
arch: 'x64',
dontCache: true,
cacheMode: ElectronDownloadCacheMode.ReadOnly,
});
expect(await fs.pathExists(driverPath)).toEqual(true);
expect(path.basename(driverPath)).toMatchInlineSnapshot(
@@ -283,7 +283,7 @@ describe('Public API', () => {
expect(fs.readdirSync(cacheRoot).length).toEqual(0);
});

it('should use cache hits when dontCache=true', async () => {
it('should use cache hits when cacheMode=ReadOnly', async () => {
const driverPath = await downloadArtifact({
cacheRoot,
downloader,
@@ -300,7 +300,7 @@ describe('Public API', () => {
artifactName: 'chromedriver',
platform: 'darwin',
arch: 'x64',
dontCache: true,
cacheMode: ElectronDownloadCacheMode.ReadOnly,
});
expect(driverPath2).not.toEqual(driverPath);
expect(path.dirname(driverPath2).startsWith(cacheRoot)).toEqual(false);