Skip to content

Commit

Permalink
feat: allow custom URL resolution with resolveAssetURL (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere authored May 6, 2020
1 parent 8857e40 commit 3481db1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ available:
* `nightly_mirror` String (optional) - The Electron nightly-specific mirror URL.
* `customDir` String (optional) - The name of the directory to download from, often scoped by version number.
* `customFilename` String (optional) - The name of the asset to download.
* `baseOnly` Boolean (optional) - Whether to download from the base URL only.
* `resolveAssetURL` Function (optional) - A function allowing customization of the url used to download the asset.

Anatomy of a download URL, in terms of `mirrorOptions`:

Expand Down
12 changes: 9 additions & 3 deletions src/artifact-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function getArtifactFileName(details: ElectronArtifactDetails) {
}

function mirrorVar(
name: keyof Omit<MirrorOptions, 'baseOnly'>,
name: keyof Omit<MirrorOptions, 'resolveAssetURL'>,
options: MirrorOptions,
defaultValue: string,
) {
Expand All @@ -42,7 +42,7 @@ function mirrorVar(
);
}

export function getArtifactRemoteURL(details: ElectronArtifactDetails): string {
export async function getArtifactRemoteURL(details: ElectronArtifactDetails): Promise<string> {
const opts: MirrorOptions = details.mirrorOptions || {};
let base = mirrorVar('mirror', opts, BASE_URL);
if (details.version.includes('nightly')) {
Expand All @@ -54,5 +54,11 @@ export function getArtifactRemoteURL(details: ElectronArtifactDetails): string {
);
const file = mirrorVar('customFilename', opts, getArtifactFileName(details));

return opts.baseOnly ? `${base}` : `${base}${path}/${file}`;
// Allow customized download URL resolution.
if (opts.resolveAssetURL) {
const url = await opts.resolveAssetURL(details);
return url;
}

return `${base}${path}/${file}`;
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export async function downloadArtifact(
process.env.ELECTRON_CUSTOM_VERSION || artifactDetails.version,
);
const fileName = getArtifactFileName(artifactDetails);
const url = getArtifactRemoteURL(artifactDetails);
const url = await getArtifactRemoteURL(artifactDetails);
const cache = new Cache(artifactDetails.cacheRoot);

// Do not check if the file exists in the cache when force === true
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ export interface MirrorOptions {
*/
customFilename?: string;
/**
* Whether to download from the base URL only,
* ignoring customDir and customFilename
* A function allowing customization of the url returned
* from getArtifactRemoteURL().
*/
baseOnly?: boolean;
resolveAssetURL?: (opts: DownloadOptions) => Promise<string>;
}

export interface ElectronDownloadRequest {
Expand Down
32 changes: 17 additions & 15 deletions test/artifact-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ describe('artifact-utils', () => {
});

describe('getArtifactRemoteURL', () => {
it('should generate a default URL correctly', () => {
it('should generate a default URL correctly', async () => {
expect(
getArtifactRemoteURL({
await getArtifactRemoteURL({
arch: 'x64',
artifactName: 'electron',
platform: 'linux',
Expand All @@ -52,9 +52,9 @@ describe('artifact-utils', () => {
);
});

it('should replace the base URL when mirrorOptions.mirror is set', () => {
it('should replace the base URL when mirrorOptions.mirror is set', async () => {
expect(
getArtifactRemoteURL({
await getArtifactRemoteURL({
arch: 'x64',
artifactName: 'electron',
mirrorOptions: {
Expand All @@ -66,26 +66,28 @@ describe('artifact-utils', () => {
).toMatchInlineSnapshot(`"https://mirror.example.com/v6.0.0/electron-v6.0.0-linux-x64.zip"`);
});

it('should allow for setting only a base url when mirrorOptions.baseOnly is set', () => {
it('should allow for custom URL resolution with mirrorOptions.resolveAssetURL', async () => {
expect(
getArtifactRemoteURL({
await getArtifactRemoteURL({
arch: 'x64',
artifactName: 'electron',
mirrorOptions: {
mirror: 'https://mirror.example.com',
customDir: 'v1.2.3',
customFilename: 'custom-built-electron.zip',
baseOnly: true,
resolveAssetURL: opts => {
return opts.mirrorOptions.mirror || '';
},
},
platform: 'linux',
version: 'v6.0.0',
}),
).toMatchInlineSnapshot(`"https://mirror.example.com"`);
});

it('should replace the nightly base URL when mirrorOptions.nightly_mirror is set', () => {
it('should replace the nightly base URL when mirrorOptions.nightly_mirror is set', async () => {
expect(
getArtifactRemoteURL({
await getArtifactRemoteURL({
arch: 'x64',
artifactName: 'electron',
mirrorOptions: {
Expand All @@ -100,9 +102,9 @@ describe('artifact-utils', () => {
);
});

it('should replace the version dir when mirrorOptions.customDir is set', () => {
it('should replace the version dir when mirrorOptions.customDir is set', async () => {
expect(
getArtifactRemoteURL({
await getArtifactRemoteURL({
arch: 'x64',
artifactName: 'electron',
mirrorOptions: {
Expand All @@ -116,9 +118,9 @@ describe('artifact-utils', () => {
);
});

it('should replace {{ version }} when mirrorOptions.customDir is set', () => {
it('should replace {{ version }} when mirrorOptions.customDir is set', async () => {
expect(
getArtifactRemoteURL({
await getArtifactRemoteURL({
arch: 'x64',
artifactName: 'electron',
mirrorOptions: {
Expand All @@ -132,9 +134,9 @@ describe('artifact-utils', () => {
);
});

it('should replace the filename when mirrorOptions.customFilename is set', () => {
it('should replace the filename when mirrorOptions.customFilename is set', async () => {
expect(
getArtifactRemoteURL({
await getArtifactRemoteURL({
arch: 'x64',
artifactName: 'electron',
mirrorOptions: {
Expand Down

0 comments on commit 3481db1

Please sign in to comment.