Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions sdk/typescript/src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,36 @@ function isNewerVersion(latest: string, current: string): boolean {
if (candidate[4] === installed[4]) return false;
if (candidate[4] === undefined) return true;
if (installed[4] === undefined) return false;
return (
new Intl.Collator("en", { numeric: true }).compare(
candidate[4],
installed[4],
) > 0
return isNewerPrerelease(candidate[4], installed[4]);
}

function isNewerPrerelease(candidate: string, installed: string): boolean {
const candidateIdentifiers = candidate.split(".");
const installedIdentifiers = installed.split(".");
const length = Math.max(
candidateIdentifiers.length,
installedIdentifiers.length,
);

for (let index = 0; index < length; index += 1) {
const candidateIdentifier = candidateIdentifiers[index];
const installedIdentifier = installedIdentifiers[index];
if (candidateIdentifier === installedIdentifier) continue;
if (candidateIdentifier === undefined) return false;
if (installedIdentifier === undefined) return true;

const candidateIsNumeric = /^\d+$/u.test(candidateIdentifier);
const installedIsNumeric = /^\d+$/u.test(installedIdentifier);
if (candidateIsNumeric && installedIsNumeric) {
return BigInt(candidateIdentifier) > BigInt(installedIdentifier);
}
if (candidateIsNumeric !== installedIsNumeric) {
return !candidateIsNumeric;
}
return candidateIdentifier > installedIdentifier;
}

return false;
}

function packageVersions(url: URL): {
Expand Down
40 changes: 40 additions & 0 deletions sdk/typescript/tests-ts/update-notice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,46 @@ describe("CLI update notice", () => {
).toBeUndefined();
});

test("uses SemVer precedence for prerelease identifiers", async () => {
const precedence = [
"1.0.0-alpha",
"1.0.0-alpha.1",
"1.0.0-alpha.beta",
"1.0.0-beta",
"1.0.0-beta.2",
"1.0.0-beta.11",
"1.0.0-rc.1",
"1.0.0",
];

for (let index = 1; index < precedence.length; index += 1) {
const lower = precedence[index - 1]!;
const higher = precedence[index]!;
await expect(
checkForUpdate({
environment: {},
currentVersion: lower,
fetch: registryResponse(higher),
}),
).resolves.toBeDefined();
await expect(
checkForUpdate({
environment: {},
currentVersion: higher,
fetch: registryResponse(lower),
}),
).resolves.toBeUndefined();
}

await expect(
checkForUpdate({
environment: {},
currentVersion: "1.0.0-alpha.1",
fetch: registryResponse("1.0.0-alpha-1"),
}),
).resolves.toBeDefined();
});

test("suppresses registry checks in CI or when disabled", async () => {
let requests = 0;
const fetchLatest = async () => {
Expand Down