docker-hub-tags
Version:
NodeJS lib, with which you can easily find the latest version of Docker Hub images
98 lines (97 loc) • 3.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.semverLevelToString = exports.parsedVersionLevelToString = exports.parsedVersionToString = exports.isHigherVersion = exports.parseVersionWithLevel = exports.parseVersion = exports.fetchRetry = exports.sleep = void 0;
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
exports.sleep = sleep;
const fetchRetry = async (url, retry, init) => {
const RETRY_DELAY = [250, 750];
let tryCount = retry;
let lastError;
while (tryCount--)
try {
return await fetch(url, init);
}
catch (error) {
lastError = error;
await (0, exports.sleep)(Math.random() * RETRY_DELAY[1] + RETRY_DELAY[0]);
}
throw lastError;
};
exports.fetchRetry = fetchRetry;
const convertSemverLevel = (indicator) => {
switch (indicator) {
case '^':
return 'minor';
case '~':
return 'patch';
case '!':
return 'evenMajor';
default:
return 'major';
}
};
const RegEx = /^([!^~]?)v?(\d+)(\.(\d+))?(\.(\d+))?([\d.a-z-]+)*$/i;
const parseVersion = (version) => {
const match = version.match(RegEx);
if (!match)
return;
return {
major: Number(match[2]),
minor: Number(match[4] || '0'),
patch: Number(match[6] || '0'),
postfix: match[7]
};
};
exports.parseVersion = parseVersion;
const parseVersionWithLevel = (version) => {
const versionInfo = (0, exports.parseVersion)(version);
if (!versionInfo)
return;
const match = version.match(RegEx);
if (!match)
return;
return {
...versionInfo,
semverLevel: convertSemverLevel(match[1])
};
};
exports.parseVersionWithLevel = parseVersionWithLevel;
const isHigherVersion = (from, to) => {
if ((from.postfix || '').toLowerCase() !== (to.postfix || '').toLowerCase())
return false;
if ('semverLevel' in from)
switch (from.semverLevel) {
case 'patch':
return from.major === to.major && from.minor === to.minor && from.patch < to.patch;
case 'minor':
return (from.major === to.major &&
(from.minor < to.minor || (from.minor === to.minor && from.patch < to.patch)));
case 'major':
case 'evenMajor':
return ((from.major < to.major ||
(from.major === to.major &&
(from.minor < to.minor || (from.minor === to.minor && from.patch < to.patch)))) &&
(from.semverLevel === 'major' || from.major % 2 === to.major % 2));
}
return (from.major < to.major ||
(from.major === to.major &&
(from.minor < to.minor || (from.minor === to.minor && from.patch < to.patch))));
};
exports.isHigherVersion = isHigherVersion;
const parsedVersionToString = (version) => `${version.major}.${version.minor}.${version.patch}${version.postfix}`;
exports.parsedVersionToString = parsedVersionToString;
const parsedVersionLevelToString = (version) => `${(0, exports.semverLevelToString)(version.semverLevel)}${version.major}.${version.minor}.${version.patch}${version.postfix}`;
exports.parsedVersionLevelToString = parsedVersionLevelToString;
const semverLevelToString = (semverLevel) => {
switch (semverLevel) {
case 'minor':
return '^';
case 'patch':
return '~';
case 'evenMajor':
return '!';
default:
return '';
}
};
exports.semverLevelToString = semverLevelToString;