From 15c02ca42f465678d08c8d9d66335f0f5abf7cd0 Mon Sep 17 00:00:00 2001 From: Ayushman Chhabra <14110965+ayushmanchhabra@users.noreply.github.com> Date: Sat, 9 Nov 2024 10:23:20 +0530 Subject: [PATCH 1/5] fix(util): throw error instead of returning it in validate function --- src/util.js | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/util.js b/src/util.js index ae2a1207..0d636f1e 100644 --- a/src/util.js +++ b/src/util.js @@ -307,7 +307,11 @@ export const parse = async (options, pkg) => { * @throws {Error} Throw error if options are invalid */ export const validate = async (options, releaseInfo) => { - if (!['get', 'run', 'build'].includes(options.mode)) { + if ( + options.mode !== 'get' && + options.mode !== 'run' && + options.mode !== 'build' + ) { throw new Error( `Unknown mode ${options.mode}. Expected "get", "run" or "build".`, ); @@ -331,16 +335,16 @@ export const validate = async (options, releaseInfo) => { `Platform ${options.platform} and architecture ${options.arch} is not supported by this download server.`, ); } - // if (typeof options.cacheDir !== "string") { - // throw new Error("Expected options.cacheDir to be a string. Got " + typeof options.cacheDir); - // } + if (typeof options.cacheDir !== "string") { + throw new Error("Expected options.cacheDir to be a string. Got " + typeof options.cacheDir); + } if (typeof options.cache !== 'boolean') { - return new Error( + throw new Error( 'Expected options.cache to be a boolean. Got ' + typeof options.cache, ); } if (typeof options.ffmpeg !== 'boolean') { - return new Error( + throw new Error( 'Expected options.ffmpeg to be a boolean. Got ' + typeof options.ffmpeg, ); } @@ -361,12 +365,12 @@ export const validate = async (options, releaseInfo) => { return undefined; } if (Array.isArray(options.argv)) { - return new Error( + throw new Error( 'Expected options.argv to be an array. Got ' + typeof options.argv, ); } if (typeof options.glob !== 'boolean') { - return new Error( + throw new Error( 'Expected options.glob to be a boolean. Got ' + typeof options.glob, ); } @@ -388,7 +392,7 @@ export const validate = async (options, releaseInfo) => { typeof options.managedManifest !== 'object' && typeof options.managedManifest !== 'string' ) { - return new Error( + throw new Error( 'Expected options.managedManifest to be a boolean, object or string. Got ' + typeof options.managedManifest, ); @@ -396,20 +400,20 @@ export const validate = async (options, releaseInfo) => { if (typeof options.managedManifest === 'object') { if (options.managedManifest.name === undefined) { - return new Error('Expected NW.js Manifest to have a `name` property.'); + throw new Error('Expected NW.js Manifest to have a `name` property.'); } if (options.managedManifest.main === undefined) { - return new Error('Expected NW.js Manifest to have a `main` property.'); + throw new Error('Expected NW.js Manifest to have a `main` property.'); } } if (typeof options.nativeAddon !== 'boolean') { if (typeof options.nativeAddon !== 'boolean' && typeof options.nativeAddon !== 'string') { - return new Error('Expected options.nativeAddon to be a boolean or string type. Got ' + typeof options.nativeAddon); + throw new Error('Expected options.nativeAddon to be a boolean or string type. Got ' + typeof options.nativeAddon); } if (semver.parse(options.version).minor >= '83' && options.nativeAddon !== false) { - return new Error('Native addons are not supported for NW.js v0.82.0 and below'); + throw new Error('Native addons are not supported for NW.js v0.82.0 and below'); } } From 164c92d41540a042fc0e5fd6f893f23bd55d92a2 Mon Sep 17 00:00:00 2001 From: Ayushman Chhabra <14110965+ayushmanchhabra@users.noreply.github.com> Date: Sat, 9 Nov 2024 13:36:50 +0530 Subject: [PATCH 2/5] chore(util): validate all get mode optionsc --- src/util.js | 20 +++++++++++------ tests/specs/util.test.js | 46 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/util.js b/src/util.js index 0d636f1e..a7ee6e16 100644 --- a/src/util.js +++ b/src/util.js @@ -335,9 +335,16 @@ export const validate = async (options, releaseInfo) => { `Platform ${options.platform} and architecture ${options.arch} is not supported by this download server.`, ); } - if (typeof options.cacheDir !== "string") { + if (typeof options.downloadUrl === 'string' && !options.downloadUrl.startsWith('http') && !options.downloadUrl.startsWith('file')) { + throw new Error("Expected options.downloadUrl to be a string and starts with `http` or `file`."); + } + if (typeof options.manifestUrl === 'string' && !options.manifestUrl.startsWith('http') && !options.manifestUrl.startsWith('file')) { + throw new Error("Expected options.manifestUrl to be a string and starts with `http` or `file`."); + } + if (typeof options.cacheDir !== 'string') { throw new Error("Expected options.cacheDir to be a string. Got " + typeof options.cacheDir); } + if (typeof options.cache !== 'boolean') { throw new Error( 'Expected options.cache to be a boolean. Got ' + typeof options.cache, @@ -364,6 +371,9 @@ export const validate = async (options, releaseInfo) => { if (options.mode === 'get') { return undefined; } + if (typeof options.srcDir !== 'string') { + throw new Error("Expected options.srcDir to be a string. Got " + typeof options.srcDir); + } if (Array.isArray(options.argv)) { throw new Error( 'Expected options.argv to be an array. Got ' + typeof options.argv, @@ -375,16 +385,12 @@ export const validate = async (options, releaseInfo) => { ); } - if (options.srcDir) { - await fs.promises.readdir(options.srcDir); - } - if (options.mode === 'run') { return undefined; } - if (options.outDir) { - await fs.promises.readdir(options.outDir); + if (typeof options.outDir !== 'string') { + throw new Error("Expected options.outDir to be a string. Got " + typeof options.outDir); } if ( diff --git a/tests/specs/util.test.js b/tests/specs/util.test.js index 7c1185c7..c3225a04 100644 --- a/tests/specs/util.test.js +++ b/tests/specs/util.test.js @@ -19,5 +19,49 @@ describe('util/log', function () { it('throws error if user defined log level is invalid', async function () { expect(() => util.log('debug', 'errory', 'Lorem ipsum')).toThrow(); }); - + }); + +describe('util/validate', function () { + + it('throws error on invalid mode', async function () { + await expect(util.validate({ mode: 'gety' }, {})).rejects.toThrow(Error); + }); + + it('throws error if releases info is undefined', async function () { + await expect(util.validate({ mode: 'get' }, undefined)).rejects.toThrow(Error); + }); + + it('throws error on invalid flavor', async function () { + await expect(util.validate({ mode: 'get', flavor: 'notsdk' }, { flavours: ['normal'] })).rejects.toThrow(Error); + }); + + it('throws error on invalid platform', async function () { + await expect(util.validate({ mode: 'get', flavor: 'normal', platform: 'linox' }, { flavours: ['normal'], files: ['linux-x64'] })).rejects.toThrow(Error); + }); + + it('throws error on invalid architecture', async function () { + await expect(util.validate({ mode: 'get', flavor: 'normal', platform: 'linux', arch: 'x64000' }, { flavors: ['normal'], files: ['linux-x64'] })).rejects.toThrow(Error); + }); + + it('throws error on invalid download url', async function () { + await expect(util.validate({ mode: 'get', flavor: 'normal', platform: 'linux', arch: 'x64', downloadUrl: null }, { flavors: ['normal'], files: ['linux-x64'] })).rejects.toThrow(Error); + }); + + it('throws error on invalid manifest url', async function () { + await expect(util.validate({ mode: 'get', flavor: 'normal', platform: 'linux', arch: 'x64', downloadUrl: 'file://path/to/fs', manifestUrl: null }, { flavors: ['normal'], files: ['linux-x64'] })).rejects.toThrow(Error); + }); + + it('throws error on invalid cache directory', async function () { + await expect(util.validate({ mode: 'get', flavor: 'normal', platform: 'linux', arch: 'x64', downloadUrl: 'file://path/to/fs', manifestUrl: 'http://path/to/manifest', cacheDir: null }, { flavors: ['normal'], files: ['linux-x64'] })).rejects.toThrow(Error); + }); + + it('throws error on invalid cache flag', async function () { + await expect(util.validate({ mode: 'get', flavor: 'normal', platform: 'linux', arch: 'x64', downloadUrl: 'file://path/to/fs', manifestUrl: 'http://path/to/manifest', cacheDir: './path/to/cache', cache: 'true' }, { flavors: ['normal'], files: ['linux-x64'] })).rejects.toThrow(Error); + }); + + it('throws error on invalid ffmpeg flag', async function () { + await expect(util.validate({ mode: 'get', flavor: 'normal', platform: 'linux', arch: 'x64', downloadUrl: 'file://path/to/fs', manifestUrl: 'http://path/to/manifest', cacheDir: './path/to/cache', cache: true, ffmpeg: 'true' }, { flavors: ['normal'], files: ['linux-x64'] })).rejects.toThrow(Error); + }); + +}); \ No newline at end of file From b7d65c5f0a5177703c1374f4a13479fb4caaedeb Mon Sep 17 00:00:00 2001 From: Ayushman Chhabra <14110965+ayushmanchhabra@users.noreply.github.com> Date: Sun, 10 Nov 2024 11:29:32 +0530 Subject: [PATCH 3/5] fix(util): resolve linting errors --- src/util.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/util.js b/src/util.js index a7ee6e16..cf53a235 100644 --- a/src/util.js +++ b/src/util.js @@ -336,13 +336,13 @@ export const validate = async (options, releaseInfo) => { ); } if (typeof options.downloadUrl === 'string' && !options.downloadUrl.startsWith('http') && !options.downloadUrl.startsWith('file')) { - throw new Error("Expected options.downloadUrl to be a string and starts with `http` or `file`."); + throw new Error('Expected options.downloadUrl to be a string and starts with `http` or `file`.'); } if (typeof options.manifestUrl === 'string' && !options.manifestUrl.startsWith('http') && !options.manifestUrl.startsWith('file')) { - throw new Error("Expected options.manifestUrl to be a string and starts with `http` or `file`."); + throw new Error('Expected options.manifestUrl to be a string and starts with `http` or `file`.'); } if (typeof options.cacheDir !== 'string') { - throw new Error("Expected options.cacheDir to be a string. Got " + typeof options.cacheDir); + throw new Error('Expected options.cacheDir to be a string. Got ' + typeof options.cacheDir); } if (typeof options.cache !== 'boolean') { @@ -372,7 +372,7 @@ export const validate = async (options, releaseInfo) => { return undefined; } if (typeof options.srcDir !== 'string') { - throw new Error("Expected options.srcDir to be a string. Got " + typeof options.srcDir); + throw new Error('Expected options.srcDir to be a string. Got ' + typeof options.srcDir); } if (Array.isArray(options.argv)) { throw new Error( @@ -390,7 +390,7 @@ export const validate = async (options, releaseInfo) => { } if (typeof options.outDir !== 'string') { - throw new Error("Expected options.outDir to be a string. Got " + typeof options.outDir); + throw new Error('Expected options.outDir to be a string. Got ' + typeof options.outDir); } if ( From 9378d6f62463e6dace7f3f0e007fe33dfb0ad064 Mon Sep 17 00:00:00 2001 From: Ayushman Chhabra <14110965+ayushmanchhabra@users.noreply.github.com> Date: Sat, 16 Nov 2024 01:15:48 +0530 Subject: [PATCH 4/5] fix(util): validate options.zip --- src/util.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/util.js b/src/util.js index cf53a235..65117494 100644 --- a/src/util.js +++ b/src/util.js @@ -421,6 +421,10 @@ export const validate = async (options, releaseInfo) => { if (semver.parse(options.version).minor >= '83' && options.nativeAddon !== false) { throw new Error('Native addons are not supported for NW.js v0.82.0 and below'); } + + if (typeof options.zip !== "boolean" && options.zip !== "zip" && options.zip !== "tar" && options.zip !== "tgz") { + throw new Error('Expected options.zip to be a boolean, `zip`, `tar` or `tgz`. Got ' + typeof options.zip); + } } // TODO: Validate app options From 0c757d9ab70b8073d9973f8850a4bfe15e24a61a Mon Sep 17 00:00:00 2001 From: Ayushman Chhabra <14110965+ayushmanchhabra@users.noreply.github.com> Date: Sat, 16 Nov 2024 01:23:43 +0530 Subject: [PATCH 5/5] chore(util): resolve linting errors --- src/util.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/util.js b/src/util.js index 65117494..7d02358b 100644 --- a/src/util.js +++ b/src/util.js @@ -422,7 +422,10 @@ export const validate = async (options, releaseInfo) => { throw new Error('Native addons are not supported for NW.js v0.82.0 and below'); } - if (typeof options.zip !== "boolean" && options.zip !== "zip" && options.zip !== "tar" && options.zip !== "tgz") { + if (typeof options.zip !== 'boolean' & + options.zip !== 'zip' && + options.zip !== 'tar' && + options.zip !== 'tgz') { throw new Error('Expected options.zip to be a boolean, `zip`, `tar` or `tgz`. Got ' + typeof options.zip); } }