refactor: modularize backend security and observability

This commit is contained in:
237899745
2026-07-27 13:06:54 +08:00
parent f90311e68c
commit dce1e3622a
25 changed files with 1633 additions and 849 deletions

45
backend/utils/version.js Normal file
View File

@@ -0,0 +1,45 @@
const SHA256_PATTERN = /^[a-f0-9]{64}$/;
function normalizeVersion(rawVersion, fallback = '0.0.0') {
const value = String(rawVersion || '').trim();
return value || fallback;
}
function compareLooseVersion(left, right) {
const normalize = (value) => normalizeVersion(value, '0.0.0')
.replace(/^v/i, '')
.split('.')
.map((part) => parseInt(part, 10))
.map((num) => (Number.isFinite(num) ? num : 0));
const a = normalize(left);
const b = normalize(right);
const size = Math.max(a.length, b.length);
for (let i = 0; i < size; i += 1) {
const av = a[i] || 0;
const bv = b[i] || 0;
if (av > bv) return 1;
if (av < bv) return -1;
}
return 0;
}
function normalizeReleaseNotes(rawValue) {
return String(rawValue || '')
.replace(/\\r\\n/g, '\n')
.replace(/\\n/g, '\n')
.replace(/\\r/g, '\n')
.trim();
}
function normalizeSha256(rawValue) {
const digest = String(rawValue || '').trim().toLowerCase();
return SHA256_PATTERN.test(digest) ? digest : '';
}
module.exports = {
compareLooseVersion,
normalizeReleaseNotes,
normalizeSha256,
normalizeVersion
};