@fabrika/auth-core 0.9.0-dev.21 → 0.9.0-dev.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabrika/auth-core",
3
- "version": "0.9.0-dev.21",
3
+ "version": "0.9.0-dev.23",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,2 +1,72 @@
1
1
  /** A framework release as npm publishes it; the only form one may take before it reaches a generated build command. */
2
2
  export const FRAMEWORK_RELEASE_PATTERN = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/
3
+
4
+ /**
5
+ * Whether framework release `release` is at least `since`. A prerelease counts as its `major.minor`; a release that
6
+ * does not start with `<major>.<minor>.` is never at least anything (ADR-0062).
7
+ */
8
+ export const releaseAtLeast = (release: string, since: { readonly major: number; readonly minor: number }): boolean => {
9
+ const match = /^(\d+)\.(\d+)\./.exec(release)
10
+ if (match === null) return false
11
+ const major = Number(match[1])
12
+ return major > since.major || (major === since.major && Number(match[2]) >= since.minor)
13
+ }
14
+
15
+ /** A release as semver orders it: the numeric core, then the prerelease identifiers (none sorts above any). */
16
+ interface ReleaseVersion {
17
+ readonly core: readonly number[]
18
+ readonly prerelease: readonly string[]
19
+ }
20
+
21
+ const parseReleaseVersion = (release: string): ReleaseVersion | null => {
22
+ const match = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z.-]+)?$/.exec(release)
23
+ if (match === null) return null
24
+ return { core: [Number(match[1]), Number(match[2]), Number(match[3])], prerelease: match[4] === undefined ? [] : match[4].split('.') }
25
+ }
26
+
27
+ /** Semver's order of two prerelease identifiers: numerically when both are numeric, a numeric one first, else by ASCII. */
28
+ const compareIdentifiers = (a: string, b: string): number => {
29
+ const numericA = /^\d+$/.test(a)
30
+ const numericB = /^\d+$/.test(b)
31
+ if (numericA && numericB) return Number(a) - Number(b)
32
+ if (numericA !== numericB) return numericA ? -1 : 1
33
+ return a < b ? -1 : a > b ? 1 : 0
34
+ }
35
+
36
+ const compareReleases = (a: ReleaseVersion, b: ReleaseVersion): number => {
37
+ for (const [index, part] of a.core.entries()) {
38
+ const other = b.core[index] ?? 0
39
+ if (part !== other) return part - other
40
+ }
41
+ if (a.prerelease.length === 0 || b.prerelease.length === 0) return b.prerelease.length - a.prerelease.length
42
+ for (const [index, identifier] of a.prerelease.entries()) {
43
+ const other = b.prerelease[index]
44
+ if (other === undefined) return 1
45
+ const order = compareIdentifiers(identifier, other)
46
+ if (order !== 0) return order
47
+ }
48
+ return a.prerelease.length - b.prerelease.length
49
+ }
50
+
51
+ /**
52
+ * Whether framework release `release` is at or above the exact version `since` in semver's precedence, prerelease
53
+ * included; a release that is not a semver version never is (ADR-0062).
54
+ */
55
+ export const releaseFrom = (release: string, since: string): boolean => {
56
+ const floor = parseReleaseVersion(since)
57
+ if (floor === null) throw new Error(`releaseFrom: \`${since}\` is not a semver version`)
58
+ const version = parseReleaseVersion(release)
59
+ return version !== null && compareReleases(version, floor) >= 0
60
+ }
61
+
62
+ /**
63
+ * The release a gate reads for an approved access: the application's pin, never the reader's `checker`, which is this
64
+ * platform's patch of the pin's minor and so passes a `releaseFrom` gate a prerelease pin does not. An access approved before Control recorded the pin is read as the oldest release of its
65
+ * reader's minor (`<major>.<minor>.0-0`): every `releaseAtLeast` gate answers as for that minor, and every `releaseFrom`
66
+ * gate of a prerelease or a patch fails closed, as for an older application, until a deploy reads its pin.
67
+ */
68
+ export const applicationRelease = (access: { readonly checker: string; readonly framework?: string }): string => {
69
+ if (access.framework !== undefined) return access.framework
70
+ const minor = /^(\d+)\.(\d+)\./.exec(access.checker)
71
+ return minor === null ? access.checker : `${minor[1]}.${minor[2]}.0-0`
72
+ }
@@ -1,2 +1,25 @@
1
1
  /** A framework release as npm publishes it; the only form one may take before it reaches a generated build command. */
2
2
  export declare const FRAMEWORK_RELEASE_PATTERN: RegExp;
3
+ /**
4
+ * Whether framework release `release` is at least `since`. A prerelease counts as its `major.minor`; a release that
5
+ * does not start with `<major>.<minor>.` is never at least anything (ADR-0062).
6
+ */
7
+ export declare const releaseAtLeast: (release: string, since: {
8
+ readonly major: number;
9
+ readonly minor: number;
10
+ }) => boolean;
11
+ /**
12
+ * Whether framework release `release` is at or above the exact version `since` in semver's precedence, prerelease
13
+ * included; a release that is not a semver version never is (ADR-0062).
14
+ */
15
+ export declare const releaseFrom: (release: string, since: string) => boolean;
16
+ /**
17
+ * The release a gate reads for an approved access: the application's pin, never the reader's `checker`, which is this
18
+ * platform's patch of the pin's minor and so passes a `releaseFrom` gate a prerelease pin does not. An access approved before Control recorded the pin is read as the oldest release of its
19
+ * reader's minor (`<major>.<minor>.0-0`): every `releaseAtLeast` gate answers as for that minor, and every `releaseFrom`
20
+ * gate of a prerelease or a patch fails closed, as for an older application, until a deploy reads its pin.
21
+ */
22
+ export declare const applicationRelease: (access: {
23
+ readonly checker: string;
24
+ readonly framework?: string;
25
+ }) => string;