Skip to content

Commit

Permalink
Update SemVer handling & add 0.9.0 compat (#104)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
blaine and icidasset authored Jul 25, 2023
1 parent d2b87d2 commit 4b1be87
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/compatibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
19 changes: 14 additions & 5 deletions packages/core/src/semver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,27 @@ const matchesRegex = (regex: RegExp) => (str: string) => {
}

export function parse(version: string): SemVer | null {
const parts = version.split(".")

const sv = version.match(
/^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?<buildmetadata>[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
}

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
}
Expand Down

0 comments on commit 4b1be87

Please sign in to comment.