From 4b1be878919de549f10654cefb3b7464409962d1 Mon Sep 17 00:00:00 2001 From: Blaine Cook Date: Tue, 25 Jul 2023 06:49:54 -0700 Subject: [PATCH] Update SemVer handling & add 0.9.0 compat (#104) * fix semver parsing and update compat to 0.9.1 (for compat with rs-ucan 0.2.0, which uses "0.9.0-canary") * fix: linting error --------- Co-authored-by: Steven Vandevelde --- packages/core/src/compatibility.ts | 2 +- packages/core/src/semver.ts | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/core/src/compatibility.ts b/packages/core/src/compatibility.ts index 2f447fe..688ac13 100644 --- a/packages/core/src/compatibility.ts +++ b/packages/core/src/compatibility.ts @@ -53,7 +53,7 @@ export function handleCompatibility(header: unknown, payload: unknown): UcanPart // parse either the "ucv" or "uav" as a version in the header // we translate 'uav: 1.0.0' into 'ucv: 0.3.0' - let version: "0.8.1" | "0.3.0" = "0.8.1" + let version: "0.9.1" | "0.3.0" = "0.9.1" if (!util.hasProp(header, "ucv") || typeof header.ucv !== "string") { if (!util.hasProp(header, "uav") || typeof header.uav !== "string") { throw fail("header", "Invalid format: Missing version indicator") diff --git a/packages/core/src/semver.ts b/packages/core/src/semver.ts index e9653fe..027384e 100644 --- a/packages/core/src/semver.ts +++ b/packages/core/src/semver.ts @@ -28,8 +28,17 @@ const matchesRegex = (regex: RegExp) => (str: string) => { } export function parse(version: string): SemVer | null { - const parts = version.split(".") - + const sv = version.match( + /^(?0|[1-9]\d*)\.(?0|[1-9]\d*)\.(?0|[1-9]\d*)(?:-(?(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/ + ) + + if (sv === null) { + return null + } + + const parts = + `${sv.groups?.major}.${sv.groups?.minor}.${sv.groups?.patch}`.split(".") + if (parts.length !== 3) { return null } @@ -37,9 +46,9 @@ export function parse(version: string): SemVer | null { if (!parts.every(matchesRegex(NUM_REGEX))) { return null } - - const [ major, minor, patch ] = parts.map(part => parseInt(part, 10)) - + + const [major, minor, patch] = parts.map(part => parseInt(part, 10)) + if (!Number.isSafeInteger(major) || !Number.isSafeInteger(minor) || !Number.isSafeInteger(patch)) { return null }