Skip to content

Commit

Permalink
Add snap utils methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gantunesr committed Nov 20, 2022
1 parent e08f36f commit 71913df
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions app/util/snaps/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { satisfies as satisfiesSemver } from 'semver';
import { SNAP_BLOCKLIST } from './snap-blocklist';

/**
* Checks if provided snaps are on the block list.
*
* @param snapsToCheck - An object containing snap ids and other information.
* @param blocklist - An object containing snap ids, version or shasum of the blocked snaps.
* @returns An object structure containing snaps block information.
*/
const checkSnapsBlockList = async (snapsToCheck, blocklist) =>
Object.entries(snapsToCheck).reduce((acc, [snapId, snapInfo]) => {
const blockInfo = blocklist.find(
(blocked) =>
(blocked.id === snapId &&
satisfiesSemver(snapInfo.version, blocked.versionRange, {
includePrerelease: true,
})) ||
// Check for null/undefined for a case in which SnapController did not return
// a valid message. This will avoid blocking all snaps in the given case.
// Avoid having (undefined === undefined).
(blocked.shasum ? blocked.shasum === snapInfo.shasum : false),
);

acc[snapId] = blockInfo
? {
blocked: true,
reason: blockInfo.reason,
infoUrl: blockInfo.infoUrl,
}
: { blocked: false };
return acc;
}, {});

export { checkSnapsBlockList, SNAP_BLOCKLIST };

0 comments on commit 71913df

Please sign in to comment.