Skip to content

Commit

Permalink
feat(get): add options.shaSum to enable/disable shaSum checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushmanchhabra committed Nov 24, 2024
1 parent c381ae8 commit 8254d87
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ Options
| cache | `boolean` | `true`| If true the existing cache is used. Otherwise it removes and redownloads it. |
| ffmpeg | `boolean` | `false`| If true the chromium ffmpeg is replaced by community version with proprietary codecs. |
| logLevel | `"error" \| "warn" \| "info" \| "debug"` | `"info"`| Specify level of logging. |
| shaSum | `boolean` | `true` | Flag to enable/disable shasum checks. |
| srcDir | `string` | `"./"` | File paths to application code |
| argv | `string[]` | `[]` | Command line arguments to pass to NW executable in run mode. You can also define these in `chromium-args` in NW.js manifest. |
| glob | `boolean` | `true`| If true file globbing is enabled when parsing `srcDir`. |
Expand Down
1 change: 1 addition & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ program
.option('--ffmpeg <boolean>', 'Flag to enable/disable downloading community ffmpeg', false)
.option('--glob <boolean>', 'Flag to enable/disable globbing', true)
.option('--logLevel <string>', 'Specify log level')
.option('--shaSum <string>', 'Flag to enable/disable shasum', true)
.option('--zip <string>', 'Flag to enable/disable compression', false)
.option('--managedManifest <string>', 'Managed manifest mode', false)
.option('--nodeAddon <boolean>', 'Download NW.js Node headers', false);
Expand Down
4 changes: 4 additions & 0 deletions src/get/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import util from '../util.js';
* @property {boolean} [cache = true] If false, remove cache and redownload.
* @property {boolean} [ffmpeg = false] If true, ffmpeg is not downloaded.
* @property {false | "gyp"} [nativeAddon = false] Rebuild native modules
* @property {string} [logLevel = 'info'] User defined log level.
* @property {boolean} [shaSum = true] If true shasum is enabled, otherwise disabled.
*/

/**
Expand Down Expand Up @@ -98,6 +100,8 @@ async function get(options) {
`${options.cacheDir}/shasum/${options.version}.txt`,
options.cacheDir,
options.ffmpeg,
options.logLevel,
options.shaSum,
);

if (options.ffmpeg === true) {
Expand Down
13 changes: 10 additions & 3 deletions src/get/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import util from '../util.js';
* @param {string} shaUrl - URL to get the shasum text file from.
* @param {string} shaOut - File path to shasum text file.
* @param {string} cacheDir - File path to cache directory.
* @param {ffmpeg} ffmpeg - Toggle between community (true) and official (false) ffmpeg binary
* @param {boolean} ffmpeg - Toggle between community (true) and official (false) ffmpeg binary
* @param {string} logLevel - User defined log level.
* @param {boolean} shaSum - Throws error if true, otherwise logs a warning.
* @throws {Error}
* @returns {Promise<boolean>} - Returns true if the checksums match.
*/
export default async function verify(shaUrl, shaOut, cacheDir, ffmpeg) {
export default async function verify(shaUrl, shaOut, cacheDir, ffmpeg, logLevel, shaSum) {
const shaOutExists = await util.fileExists(shaOut);

if (shaOutExists === false) {
Expand All @@ -42,7 +44,12 @@ export default async function verify(shaUrl, shaOut, cacheDir, ffmpeg) {
if (filePath.includes('ffmpeg') && ffmpeg) {
console.warn(`The generated shasum for the community ffmpeg at ${filePath} is ${generatedSha}. The integrity of this file should be manually verified.`);
} else {
throw new Error(`SHA256 checksums do not match. The file ${filePath} expected shasum is ${storedSha} but the actual shasum is ${generatedSha}.`);
const message = `SHA256 checksums do not match. The file ${filePath} expected shasum is ${storedSha} but the actual shasum is ${generatedSha}.`;
if (shaSum) {
throw new Error(message);
} else {
util.log('warn', logLevel, message);
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import util from './util.js';
* @property {boolean} [ffmpeg=false] If true the chromium ffmpeg is replaced by community version
* @property {boolean} [glob=true] If true file globbing is enabled when parsing srcDir.
* @property {"error" | "warn" | "info" | "debug"} [logLevel="info"] Specify level of logging.
* @property {boolean} [shaSum = true] If true, shasum is enabled. Otherwise, disabled.
* @property {boolean | "zip" | "tar" | "tgz"} [zip=false] If true, "zip", "tar" or "tgz" the outDir directory is compressed.
* @property {boolean | string | object} [managedManifest = false] Managed manifest mode
* @property {false | "gyp"} [nodeAddon = false] Rebuild Node native addons
Expand Down Expand Up @@ -101,6 +102,8 @@ async function nwbuild(options) {
cache: options.cache,
ffmpeg: options.ffmpeg,
nativeAddon: options.nativeAddon,
shaSum: options.shaSum,
logLevel: options.logLevel,
});

if (options.mode === 'get') {
Expand Down
7 changes: 7 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ export const parse = async (options, pkg) => {
options.cache = str2Bool(options.cache ?? true);
options.ffmpeg = str2Bool(options.ffmpeg ?? false);
options.logLevel = options.logLevel ?? 'info';
options.shaSum = shaSum ?? true;

Check failure on line 214 in src/util.js

View workflow job for this annotation

GitHub Actions / tests (macos-15)

'shaSum' is not defined

Check failure on line 214 in src/util.js

View workflow job for this annotation

GitHub Actions / tests (ubuntu-24.04)

'shaSum' is not defined

Check failure on line 214 in src/util.js

View workflow job for this annotation

GitHub Actions / tests (windows-2022)

'shaSum' is not defined

if (options.mode === 'get') {
return { ...options };
Expand Down Expand Up @@ -381,6 +382,12 @@ export const validate = async (options, releaseInfo) => {
);
}

if (typeof options.shaSum !== 'boolean') {
throw new Error(
'Expected options.shaSum to be a boolean. Got ' + typeof options.shaSum,
);
}

if (options.mode === 'get') {
return undefined;
}
Expand Down

0 comments on commit 8254d87

Please sign in to comment.