fix: standardize version parsing and simplify string escaping
This commit is contained in:
1 parent
4e2b6b1200
commit
38a7c3da63
23 files changed
+245
-137
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { APP_VERSION } from '@snowluma/common/version';
|
||||
import { compareVersions, parseVersion } from '@snowluma/common/semver';
|
||||
import { createLogger } from '@snowluma/common/logger';
|
||||
import { createSingleFlightCache } from '@snowluma/common/single-flight-cache';
|
||||
|
||||
@@ -40,49 +41,6 @@ function isEnabled(): boolean {
|
||||
return v !== '0' && v !== 'false' && v !== 'off' && v !== 'no';
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare two dotted versions. Returns >0 if `a` is newer than `b`, <0 if
|
||||
* older, 0 if equal. The numeric core (major.minor.patch) compares
|
||||
* numerically; a prerelease (`-rc.1`) ranks below its release. Good enough
|
||||
* for "is the latest stable strictly newer than what we run" — and avoids
|
||||
* pulling in a `semver` runtime dependency (the dist bundle ships none).
|
||||
*/
|
||||
export function compareVersions(a: string, b: string): number {
|
||||
const parse = (v: string) => {
|
||||
const [core = '', pre = ''] = v.replace(/^v/, '').split('-', 2);
|
||||
const nums = core.split('.').map((n) => parseInt(n, 10) || 0);
|
||||
|
||||
while (nums.length < 3) {
|
||||
nums.push(0);
|
||||
}
|
||||
|
||||
return { nums, pre };
|
||||
};
|
||||
|
||||
const pa = parse(a);
|
||||
const pb = parse(b);
|
||||
|
||||
for (let i = 0; i < 3; i++) {
|
||||
if (pa.nums[i] !== pb.nums[i]) {
|
||||
return pa.nums[i] - pb.nums[i];
|
||||
}
|
||||
}
|
||||
|
||||
if (pa.pre === pb.pre) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!pa.pre) {
|
||||
return 1;
|
||||
} // release > prerelease
|
||||
|
||||
if (!pb.pre) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return pa.pre < pb.pre ? -1 : 1;
|
||||
}
|
||||
|
||||
// TTL + single-flight: cache good results for CACHE_TTL_MS (well under GitHub's
|
||||
// 60 req/hr limit) and collapse concurrent checks onto one fetch. Error results
|
||||
// are intentionally not cached (shouldCache) so the next check retries.
|
||||
@@ -130,13 +88,18 @@ async function fetchLatest(current: string): Promise<UpdateCheckResult> {
|
||||
}
|
||||
|
||||
const rel = (await res.json()) as GithubRelease;
|
||||
const tag = (rel.tag_name ?? '').trim();
|
||||
const tag = typeof rel.tag_name === 'string' ? rel.tag_name.trim() : '';
|
||||
|
||||
if (!tag) {
|
||||
return { ...base, error: 'no tag' };
|
||||
}
|
||||
|
||||
const latest = tag.replace(/^v/, '');
|
||||
const latest = tag.startsWith('v') ? tag.slice(1) : tag;
|
||||
|
||||
if (!parseVersion(latest)) {
|
||||
return { ...base, error: 'invalid release version' };
|
||||
}
|
||||
|
||||
return {
|
||||
current,
|
||||
latest,
|
||||
|
||||
@@ -3,6 +3,7 @@ import path from 'node:path';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { constants, existsSync } from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import { parseVersion } from '@snowluma/common/semver';
|
||||
|
||||
export const MANIFEST_FILE = 'release.json';
|
||||
|
||||
@@ -86,7 +87,7 @@ export async function readManifest(root: string): Promise<ReleaseManifest> {
|
||||
typeof value !== 'object' ||
|
||||
!('version' in value) ||
|
||||
typeof value.version !== 'string' ||
|
||||
!/^\d+\.\d+\.\d+(?:-[\w.-]+)?(?:\+[\w.-]+)?$/.test(value.version) ||
|
||||
!parseVersion(value.version) ||
|
||||
!('target' in value) ||
|
||||
typeof value.target !== 'string' ||
|
||||
!/^(win32-x64|linux-(x64|arm64))$/.test(value.target) ||
|
||||
|
||||
@@ -2,7 +2,7 @@ import { existsSync } from 'node:fs';
|
||||
import { mkdir, rename, rm } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { createLogger } from '@snowluma/common/logger';
|
||||
import { compareVersions } from './check';
|
||||
import { parseVersion } from '@snowluma/common/semver';
|
||||
import { downloadUpdate } from './download';
|
||||
import { exists, MANIFEST_FILE, readManifest, updateDirectory } from './files';
|
||||
|
||||
@@ -76,17 +76,15 @@ export async function createUpdateService(
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
typeof version !== 'string' ||
|
||||
!/^\d+\.\d+\.\d+$/.test(version) ||
|
||||
compareVersions(version, current) <= 0
|
||||
) {
|
||||
const parsed = parseVersion(version);
|
||||
|
||||
if (!parsed || parsed.prerelease.length || parsed.compare(current) <= 0) {
|
||||
throw new Error('请选择更新的正式版本');
|
||||
}
|
||||
|
||||
state = { phase: 'downloading', version };
|
||||
log.info('下载 v%s', version);
|
||||
void prepare(version);
|
||||
state = { phase: 'downloading', version: parsed.raw };
|
||||
log.info('下载 v%s', parsed.raw);
|
||||
void prepare(parsed.raw);
|
||||
},
|
||||
restart() {
|
||||
if (state.phase !== 'ready') {
|
||||
|
||||
Reference in new issue
Block a user