From eb17013fbedff04c3b9f4af8f8a7f569e3296dc2 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Wed, 22 May 2019 17:46:22 -0700 Subject: [PATCH] fix: add user-friendly validation when an invalid input object is provided (#89) --- src/artifact-utils.ts | 6 ++++++ src/index.ts | 5 +++-- src/utils.ts | 6 ++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/artifact-utils.ts b/src/artifact-utils.ts index dffa4677a..7e968c349 100644 --- a/src/artifact-utils.ts +++ b/src/artifact-utils.ts @@ -1,4 +1,5 @@ import { ElectronArtifactDetails, MirrorOptions } from './types'; +import { ensureIsTruthyString } from './utils'; const BASE_URL = 'https://github.com/electron/electron/releases/download/'; const NIGHTLY_BASE_URL = 'https://github.com/electron/nightlies/releases/download/'; @@ -12,6 +13,9 @@ export function getArtifactFileName( details: ElectronArtifactDetails, usage: FileNameUse = FileNameUse.LOCAL, ) { + ensureIsTruthyString(details, 'artifactName'); + ensureIsTruthyString(details, 'version'); + if (details.isGeneric) { // When downloading we have to use the artifact name directly as that it was is stored in the release on GitHub if (usage === FileNameUse.REMOTE) { @@ -21,6 +25,8 @@ export function getArtifactFileName( return `${details.version}-${details.artifactName}`; } + ensureIsTruthyString(details, 'platform'); + ensureIsTruthyString(details, 'arch'); return `${[details.artifactName, details.version, details.platform, details.arch].join('-')}.zip`; } diff --git a/src/index.ts b/src/index.ts index 5700a50cd..5dd16fae6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,7 @@ import { getArtifactFileName, getArtifactRemoteURL, FileNameUse } from './artifa import { ElectronArtifactDetails, ElectronDownloadRequestOptions } from './types'; import { Cache } from './Cache'; import { getDownloaderForSystem } from './downloader-resolver'; -import { withTempDirectory, normalizeVersion, getHostArch } from './utils'; +import { withTempDirectory, normalizeVersion, getHostArch, ensureIsTruthyString } from './utils'; export { getHostArch } from './utils'; @@ -36,9 +36,10 @@ export function download( * @param artifactDetails The information required to download the artifact */ export async function downloadArtifact(_artifactDetails: ElectronArtifactDetails): Promise { - const artifactDetails = { + const artifactDetails: ElectronArtifactDetails = { ..._artifactDetails, }; + ensureIsTruthyString(artifactDetails, 'version'); artifactDetails.version = normalizeVersion(artifactDetails.version); const fileName = getArtifactFileName(artifactDetails); diff --git a/src/utils.ts b/src/utils.ts index c79a70266..21112c5a4 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -65,3 +65,9 @@ export function getNodeArch(arch: string) { return arch; } + +export function ensureIsTruthyString(obj: T, key: K) { + if (!obj[key] || typeof obj[key] !== 'string') { + throw new Error(`Expected property "${key}" to be provided as a string but it was not`); + } +}