@metamask/snaps-utils
Version:
A collection of utilities for MetaMask Snaps
39 lines • 1.64 kB
JavaScript
import { validate } from "@metamask/superstruct";
import { VersionRangeStruct } from "@metamask/utils";
import { maxSatisfying as maxSatisfyingSemver } from "semver";
export const DEFAULT_REQUESTED_SNAP_VERSION = '*';
/**
* Return the highest version in the list that satisfies the range, or `null` if
* none of them do. For the satisfaction check, pre-release versions will only
* be checked if no satisfactory non-prerelease version is found first.
*
* @param versions - The list of version to check.
* @param versionRange - The SemVer version range to check against.
* @returns The highest version in the list that satisfies the range,
* or `null` if none of them do.
*/
export function getTargetVersion(versions, versionRange) {
const maxSatisfyingNonPreRelease = maxSatisfyingSemver(versions, versionRange);
// By default don't use pre-release versions
if (maxSatisfyingNonPreRelease) {
return maxSatisfyingNonPreRelease;
}
// If no satisfying release version is found by default, try pre-release versions
return maxSatisfyingSemver(versions, versionRange, {
includePrerelease: true,
});
}
/**
* Parse a version received by some subject attempting to access a snap.
*
* @param version - The received version value.
* @returns `*` if the version is `undefined` or `latest", otherwise returns
* the specified version.
*/
export function resolveVersionRange(version) {
if (version === undefined || version === 'latest') {
return [undefined, DEFAULT_REQUESTED_SNAP_VERSION];
}
return validate(version, VersionRangeStruct);
}
//# sourceMappingURL=versions.mjs.map