dsh-token-dash 2.3.1 → 2.4.0

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.
@@ -1,224 +1,228 @@
1
- /**
2
- * Declarative quota/balance sources. Endpoints and credential refs only;
3
- * request execution lives in adapters.js so this file stays pure data + parsing.
4
- */
5
-
6
- const TIMEOUT = 6000
7
-
8
- export const OPENCODE_USAGE = 'https://opencode.ai/zen/go/v1/usage'
9
- export const OPENCODE_USAGE_FALLBACK = 'https://opencode.ai/api/v1/usage'
10
- export const COMMAND_CODE_CREDITS = 'https://api.commandcode.ai/alpha/billing/credits'
11
- export const OLLAMA_BALANCE = 'https://ollama.com/api/balance'
12
- export const OLLAMA_USAGE = 'https://ollama.com/api/usage'
13
-
14
- function clampPercent(value) {
15
- const n = Number(value)
16
- if (!Number.isFinite(n)) return null
17
- return Math.max(0, Math.min(100, n))
18
- }
19
-
20
- function pickNumber(...values) {
21
- for (const value of values) {
22
- const n = Number(value)
23
- if (Number.isFinite(n)) return n
24
- }
25
- return null
26
- }
27
-
28
- /** 容错解析单个窗口:usedPercent / used_ratio / percentage / used+limit。 */
29
- export function parseWindow(raw, key) {
30
- if (raw === null || raw === undefined || typeof raw !== 'object') return null
31
- const direct = pickNumber(raw.usedPercent, raw.used_percent, raw.percentage, raw.percent)
32
- let percent = direct
33
- if (percent === null && raw.used_ratio !== undefined) percent = Number(raw.used_ratio) * 100
34
- if (percent === null) {
35
- const used = pickNumber(raw.used, raw.current, raw.consumed)
36
- const limit = pickNumber(raw.limit, raw.total, raw.max)
37
- if (used !== null && limit !== null && limit > 0) percent = (used / limit) * 100
38
- }
39
- if (percent === null) return null
40
- const resetAt = pickNumber(raw.resetAt, raw.reset_at, raw.resetTime, raw.reset_time, raw.nextResetTime, raw.next_reset_time)
41
- const usedPercent = clampPercent(percent)
42
- return { key, usedPercent: usedPercent === null ? null : Math.round(usedPercent * 100) / 100, resetAt: resetAt ?? undefined, source: 'official' }
43
- }
44
-
45
- function collectFromMap(source, map) {
46
- const windows = []
47
- for (const [key, names] of Object.entries(map)) {
48
- for (const name of names) {
49
- const parsed = parseWindow(source?.[name], key)
50
- if (parsed !== null) { windows.push(parsed); break }
51
- }
52
- }
53
- return windows
54
- }
55
-
56
- /**
57
- * OpenCode Go:兼容 `usage` / `data` 下的 fiveHour / weekly / monthly,
58
- * 以及数组形式的 windows / limits。无法识别的窗口不补占位。
59
- */
60
- export function parseOpenCodeUsage(body) {
61
- const data = body?.data ?? body
62
- const usage = data?.usage ?? data
63
- const windows = collectFromMap(usage, {
64
- '5h': ['fiveHour', 'five_hour', 'fiveHourWindow', '5h'],
65
- week: ['weekly', 'week', 'weekWindow'],
66
- month: ['monthly', 'month', 'monthWindow'],
67
- })
68
- const rows = Array.isArray(usage?.windows) ? usage.windows : Array.isArray(usage?.limits) ? usage.limits : []
69
- for (const row of rows) {
70
- const name = String(row?.key ?? row?.type ?? row?.name ?? '').toLowerCase()
71
- const key = name.includes('5') || name.includes('five') ? '5h'
72
- : name.includes('week') ? 'week'
73
- : name.includes('month') ? 'month' : null
74
- if (key === null || windows.some((item) => item.key === key)) continue
75
- const parsed = parseWindow(row, key)
76
- if (parsed !== null) windows.push(parsed)
77
- }
78
- return windows
79
- }
80
-
81
- /** Command Code:credits(数字或对象)+ windowLimits.fiveHour / weekly。 */
82
- export function parseCommandCodeCredits(body) {
83
- const data = body?.data ?? body
84
- const rawCredits = data?.credits ?? data?.credit ?? data?.balance
85
- const amount = pickNumber(
86
- typeof rawCredits === 'object' && rawCredits !== null ? rawCredits.amount : rawCredits,
87
- typeof rawCredits === 'object' && rawCredits !== null ? rawCredits.balance : undefined,
88
- typeof rawCredits === 'object' && rawCredits !== null ? rawCredits.value : undefined,
89
- )
90
- const limits = data?.windowLimits ?? data?.window_limits ?? data?.limits ?? data
91
- const windows = []
92
- const five = parseWindow(limits?.fiveHour ?? limits?.five_hour, '5h')
93
- const week = parseWindow(limits?.weekly ?? limits?.week, 'week')
94
- if (five !== null) windows.push(five)
95
- if (week !== null) windows.push(week)
96
- const balance = amount === null
97
- ? null
98
- : { amount, status: 'ok', currency: 'CNY', updatedAt: Date.now() }
99
- return { windows, balance }
100
- }
101
-
102
- function round2(value) {
103
- return Math.round(value * 100) / 100
104
- }
105
-
106
- function round6(value) {
107
- return Math.round(value * 1e6) / 1e6
108
- }
109
-
110
- /** 旧套餐窗口:只有剩余百分比与重置时间,没有金额。 */
111
- function legacyWindow(key, raw) {
112
- if (raw === null || raw === undefined || typeof raw !== 'object') return null
113
- const remaining = pickNumber(raw.remaining_percent)
114
- if (remaining === null) return null
115
- const resetAt = Date.parse(raw.resets_at)
116
- return {
117
- key,
118
- usedPercent: round2(clampPercent(100 - remaining)),
119
- resetAt: Number.isFinite(resetAt) ? resetAt : undefined,
120
- source: 'official',
121
- }
122
- }
123
-
124
- /**
125
- * Ollama Cloud:`included` 是 oneOf 二态——金额态(balance_usd / allowance_usd / period)
126
- * 与旧套餐态(session / weekly 百分比)。`purchased` 恒有,只含未过期额度。
127
- * 金额态 `amount` = 套餐剩余 + 购买余额,`used` 只用于套餐进度,不做分母混用。
128
- */
129
- export function parseOllamaBalance(body) {
130
- const data = body?.data ?? body
131
- const included = data?.included
132
- const purchased = pickNumber(data?.purchased?.balance_usd)
133
- const updatedAt = Date.now()
134
- if (included === null || included === undefined || typeof included !== 'object') {
135
- return { windows: [], balance: { amount: null, status: 'error', currency: 'USD', updatedAt, _error: { kind: 'parse' } } }
136
- }
137
- if (included.session !== undefined || included.weekly !== undefined) {
138
- const windows = []
139
- const session = legacyWindow('session', included.session)
140
- const weekly = legacyWindow('week', included.weekly)
141
- if (session !== null) windows.push(session)
142
- if (weekly !== null) windows.push(weekly)
143
- return {
144
- windows,
145
- balance: {
146
- amount: purchased ?? 0,
147
- ...(purchased === null ? {} : { purchased }),
148
- legacy: true,
149
- status: 'ok',
150
- currency: 'USD',
151
- updatedAt,
152
- },
153
- }
154
- }
155
- const left = pickNumber(included.balance_usd)
156
- const allowance = pickNumber(included.allowance_usd)
157
- if (left === null || allowance === null) {
158
- return { windows: [], balance: { amount: null, status: 'error', currency: 'USD', updatedAt, _error: { kind: 'parse' } } }
159
- }
160
- const periodEnd = Date.parse(included.period?.until)
161
- return {
162
- windows: [],
163
- balance: {
164
- amount: round6(left + (purchased ?? 0)),
165
- packageLeft: left,
166
- allowance,
167
- used: round6(Math.max(0, allowance - left)),
168
- ...(purchased === null ? {} : { purchased }),
169
- ...(Number.isFinite(periodEnd) ? { periodEnd } : {}),
170
- status: 'ok',
171
- currency: 'USD',
172
- updatedAt,
173
- },
174
- }
175
- }
176
-
177
- /** `/api/usage` 只读参考:仅取 totals,缺失字段留 null,不参与任何计价。 */
178
- export function parseOllamaUsage(body) {
179
- const totals = body?.totals
180
- if (totals === null || totals === undefined || typeof totals !== 'object') return {}
181
- return {
182
- channel: {
183
- range: typeof body?.range === 'string' ? body.range : null,
184
- usageUsd: pickNumber(totals.usage_usd),
185
- requests: pickNumber(totals.request_count),
186
- inputTokens: pickNumber(totals.input_tokens),
187
- cachedInputTokens: pickNumber(totals.cached_input_tokens),
188
- outputTokens: pickNumber(totals.output_tokens),
189
- },
190
- }
191
- }
192
-
193
- export const PROVIDER_SOURCES = [
194
- { id: 'deepseek-official', kind: 'api', currency: 'CNY', legacy: 'deepseek' },
195
- { id: 'zhipu', kind: 'subscription', currency: 'CNY', legacy: 'glm' },
196
- { id: 'moonshot', kind: 'subscription', currency: 'CNY', legacy: 'kimi' },
197
- { id: 'openrouter', kind: 'api', currency: 'USD', legacy: 'openrouter' },
198
- { id: 'qwen', kind: 'api', currency: 'CNY', legacy: 'qwen' },
199
- {
200
- id: 'opencode-go',
201
- kind: 'subscription',
202
- currency: 'USD',
203
- credential: { type: 'bearer', refs: ['OPENCODE_GO_API_KEY'] },
204
- quota: { url: OPENCODE_USAGE, fallbackUrl: OPENCODE_USAGE_FALLBACK, parse: parseOpenCodeUsage },
205
- },
206
- {
207
- id: 'command-code',
208
- kind: 'subscription',
209
- currency: 'CNY',
210
- credential: { type: 'bearer', refs: ['COMMAND_CODE_API_KEY'] },
211
- preflight: { refs: ['COMMAND_CODE_ORG_ID'], param: 'orgId' },
212
- quota: { url: COMMAND_CODE_CREDITS, parse: parseCommandCodeCredits },
213
- },
214
- {
215
- id: 'ollama',
216
- kind: 'api',
217
- currency: 'USD',
218
- credential: { type: 'bearer', refs: ['OLLAMA_API_KEY'] },
219
- quota: { url: OLLAMA_BALANCE, parse: parseOllamaBalance },
220
- usage: { url: OLLAMA_USAGE, range: '30d', parse: parseOllamaUsage },
221
- },
222
- ]
223
-
224
- export { TIMEOUT }
1
+ /**
2
+ * Declarative quota/balance sources. Endpoints and credential refs only;
3
+ * request execution lives in adapters.js so this file stays pure data + parsing.
4
+ */
5
+
6
+ const TIMEOUT = 6000
7
+
8
+ export const OPENCODE_USAGE = 'https://opencode.ai/zen/go/v1/usage'
9
+ export const OPENCODE_USAGE_FALLBACK = 'https://opencode.ai/api/v1/usage'
10
+ export const COMMAND_CODE_CREDITS = 'https://api.commandcode.ai/alpha/billing/credits'
11
+ export const OLLAMA_BALANCE = 'https://ollama.com/api/balance'
12
+ export const OLLAMA_USAGE = 'https://ollama.com/api/usage'
13
+
14
+ function clampPercent(value) {
15
+ const n = Number(value)
16
+ if (!Number.isFinite(n)) return null
17
+ return Math.max(0, Math.min(100, n))
18
+ }
19
+
20
+ function pickNumber(...values) {
21
+ for (const value of values) {
22
+ const n = Number(value)
23
+ if (Number.isFinite(n)) return n
24
+ }
25
+ return null
26
+ }
27
+
28
+ /** 容错解析单个窗口:usedPercent / used_ratio / percentage / used+limit。 */
29
+ export function parseWindow(raw, key) {
30
+ if (raw === null || raw === undefined || typeof raw !== 'object') return null
31
+ const direct = pickNumber(raw.usedPercent, raw.used_percent, raw.percentage, raw.percent)
32
+ let percent = direct
33
+ if (percent === null && raw.used_ratio !== undefined) percent = Number(raw.used_ratio) * 100
34
+ if (percent === null) {
35
+ const used = pickNumber(raw.used, raw.current, raw.consumed)
36
+ const limit = pickNumber(raw.limit, raw.total, raw.max)
37
+ if (used !== null && limit !== null && limit > 0) percent = (used / limit) * 100
38
+ }
39
+ if (percent === null) return null
40
+ const resetRaw = raw.resetAt ?? raw.reset_at ?? raw.resetTime ?? raw.reset_time
41
+ ?? raw.nextResetTime ?? raw.next_reset_time ?? raw.resetsAt ?? raw.resets_at
42
+ const resetParsed = typeof resetRaw === 'string' ? Date.parse(resetRaw) : Number(resetRaw)
43
+ const resetAt = Number.isFinite(resetParsed) ? resetParsed : null
44
+ const usedPercent = clampPercent(percent)
45
+ return { key, usedPercent: usedPercent === null ? null : Math.round(usedPercent * 100) / 100, resetAt: resetAt ?? undefined, source: 'official' }
46
+ }
47
+
48
+ function collectFromMap(source, map) {
49
+ const windows = []
50
+ for (const [key, names] of Object.entries(map)) {
51
+ for (const name of names) {
52
+ const parsed = parseWindow(source?.[name], key)
53
+ if (parsed !== null) { windows.push(parsed); break }
54
+ }
55
+ }
56
+ return windows
57
+ }
58
+
59
+ /**
60
+ * OpenCode Go:兼容 `usage` / `data` 下的 fiveHour / weekly / monthly,
61
+ * 以及数组形式的 windows / limits。无法识别的窗口不补占位。
62
+ */
63
+ export function parseOpenCodeUsage(body) {
64
+ const data = body?.data ?? body
65
+ const usage = data?.usage ?? data
66
+ const windows = collectFromMap(usage, {
67
+ '5h': ['fiveHour', 'five_hour', 'fiveHourWindow', '5h', 'rolling'],
68
+ week: ['weekly', 'week', 'weekWindow'],
69
+ month: ['monthly', 'month', 'monthWindow'],
70
+ })
71
+ const rows = Array.isArray(usage?.windows) ? usage.windows : Array.isArray(usage?.limits) ? usage.limits : []
72
+ for (const row of rows) {
73
+ const name = String(row?.key ?? row?.type ?? row?.name ?? '').toLowerCase()
74
+ const key = name.includes('5') || name.includes('five') ? '5h'
75
+ : name.includes('week') ? 'week'
76
+ : name.includes('month') ? 'month' : null
77
+ if (key === null || windows.some((item) => item.key === key)) continue
78
+ const parsed = parseWindow(row, key)
79
+ if (parsed !== null) windows.push(parsed)
80
+ }
81
+ return windows
82
+ }
83
+
84
+ /** Command Code:credits(数字或对象)+ windowLimits.fiveHour / weekly。 */
85
+ export function parseCommandCodeCredits(body) {
86
+ const data = body?.data ?? body
87
+ const rawCredits = data?.credits ?? data?.credit ?? data?.balance
88
+ const amount = pickNumber(
89
+ typeof rawCredits === 'object' && rawCredits !== null ? rawCredits.amount : rawCredits,
90
+ typeof rawCredits === 'object' && rawCredits !== null ? rawCredits.balance : undefined,
91
+ typeof rawCredits === 'object' && rawCredits !== null ? rawCredits.value : undefined,
92
+ )
93
+ const limits = data?.windowLimits ?? data?.window_limits ?? data?.limits ?? data
94
+ const windows = []
95
+ const five = parseWindow(limits?.fiveHour ?? limits?.five_hour, '5h')
96
+ const week = parseWindow(limits?.weekly ?? limits?.week, 'week')
97
+ if (five !== null) windows.push(five)
98
+ if (week !== null) windows.push(week)
99
+ const balance = amount === null
100
+ ? null
101
+ : { amount, status: 'ok', currency: 'CNY', updatedAt: Date.now() }
102
+ return { windows, balance }
103
+ }
104
+
105
+ function round2(value) {
106
+ return Math.round(value * 100) / 100
107
+ }
108
+
109
+ function round6(value) {
110
+ return Math.round(value * 1e6) / 1e6
111
+ }
112
+
113
+ /** 旧套餐窗口:只有剩余百分比与重置时间,没有金额。 */
114
+ function legacyWindow(key, raw) {
115
+ if (raw === null || raw === undefined || typeof raw !== 'object') return null
116
+ const remaining = pickNumber(raw.remaining_percent)
117
+ if (remaining === null) return null
118
+ const resetAt = Date.parse(raw.resets_at)
119
+ return {
120
+ key,
121
+ usedPercent: round2(clampPercent(100 - remaining)),
122
+ resetAt: Number.isFinite(resetAt) ? resetAt : undefined,
123
+ source: 'official',
124
+ }
125
+ }
126
+
127
+ /**
128
+ * Ollama Cloud:`included` 是 oneOf 二态——金额态(balance_usd / allowance_usd / period)
129
+ * 与旧套餐态(session / weekly 百分比)。`purchased` 恒有,只含未过期额度。
130
+ * 金额态 `amount` = 套餐剩余 + 购买余额,`used` 只用于套餐进度,不做分母混用。
131
+ */
132
+ export function parseOllamaBalance(body) {
133
+ const data = body?.data ?? body
134
+ const included = data?.included
135
+ const purchased = pickNumber(data?.purchased?.balance_usd)
136
+ const updatedAt = Date.now()
137
+ if (included === null || included === undefined || typeof included !== 'object') {
138
+ return { windows: [], balance: { amount: null, status: 'error', currency: 'USD', updatedAt, _error: { kind: 'parse' } } }
139
+ }
140
+ if (included.session !== undefined || included.weekly !== undefined) {
141
+ const windows = []
142
+ const session = legacyWindow('session', included.session)
143
+ const weekly = legacyWindow('week', included.weekly)
144
+ if (session !== null) windows.push(session)
145
+ if (weekly !== null) windows.push(weekly)
146
+ return {
147
+ windows,
148
+ balance: {
149
+ amount: purchased ?? 0,
150
+ ...(purchased === null ? {} : { purchased }),
151
+ legacy: true,
152
+ status: 'ok',
153
+ currency: 'USD',
154
+ updatedAt,
155
+ },
156
+ }
157
+ }
158
+ const left = pickNumber(included.balance_usd)
159
+ const allowance = pickNumber(included.allowance_usd)
160
+ if (left === null || allowance === null) {
161
+ return { windows: [], balance: { amount: null, status: 'error', currency: 'USD', updatedAt, _error: { kind: 'parse' } } }
162
+ }
163
+ const periodEnd = Date.parse(included.period?.until)
164
+ return {
165
+ windows: [],
166
+ balance: {
167
+ amount: round6(left + (purchased ?? 0)),
168
+ packageLeft: left,
169
+ allowance,
170
+ used: round6(Math.max(0, allowance - left)),
171
+ ...(purchased === null ? {} : { purchased }),
172
+ ...(Number.isFinite(periodEnd) ? { periodEnd } : {}),
173
+ status: 'ok',
174
+ currency: 'USD',
175
+ updatedAt,
176
+ },
177
+ }
178
+ }
179
+
180
+ /** `/api/usage` 只读参考:仅取 totals,缺失字段留 null,不参与任何计价。 */
181
+ export function parseOllamaUsage(body) {
182
+ const totals = body?.totals
183
+ if (totals === null || totals === undefined || typeof totals !== 'object') return {}
184
+ return {
185
+ channel: {
186
+ range: typeof body?.range === 'string' ? body.range : null,
187
+ usageUsd: pickNumber(totals.usage_usd),
188
+ requests: pickNumber(totals.request_count),
189
+ inputTokens: pickNumber(totals.input_tokens),
190
+ cachedInputTokens: pickNumber(totals.cached_input_tokens),
191
+ outputTokens: pickNumber(totals.output_tokens),
192
+ },
193
+ }
194
+ }
195
+
196
+ export const PROVIDER_SOURCES = [
197
+ { id: 'deepseek-official', kind: 'api', currency: 'CNY', legacy: 'deepseek' },
198
+ { id: 'zhipu', kind: 'subscription', currency: 'CNY', legacy: 'glm' },
199
+ { id: 'moonshot', kind: 'subscription', currency: 'CNY', legacy: 'kimi' },
200
+ { id: 'openrouter', kind: 'api', currency: 'USD', legacy: 'openrouter' },
201
+ { id: 'qwen', kind: 'api', currency: 'CNY', legacy: 'qwen' },
202
+ {
203
+ id: 'opencode-go',
204
+ kind: 'subscription',
205
+ currency: 'USD',
206
+ // OPENCODE_API_KEY 为 pi-ai 对 opencode / opencode-go 的共用名;旧名保留兼容既有配置
207
+ credential: { type: 'bearer', refs: ['OPENCODE_API_KEY', 'OPENCODE_GO_API_KEY'] },
208
+ quota: { url: OPENCODE_USAGE, fallbackUrl: OPENCODE_USAGE_FALLBACK, parse: parseOpenCodeUsage },
209
+ },
210
+ {
211
+ id: 'command-code',
212
+ kind: 'subscription',
213
+ currency: 'CNY',
214
+ credential: { type: 'bearer', refs: ['COMMAND_CODE_API_KEY'] },
215
+ preflight: { refs: ['COMMAND_CODE_ORG_ID'], param: 'orgId' },
216
+ quota: { url: COMMAND_CODE_CREDITS, parse: parseCommandCodeCredits },
217
+ },
218
+ {
219
+ id: 'ollama',
220
+ kind: 'api',
221
+ currency: 'USD',
222
+ credential: { type: 'bearer', refs: ['OLLAMA_API_KEY'] },
223
+ quota: { url: OLLAMA_BALANCE, parse: parseOllamaBalance },
224
+ usage: { url: OLLAMA_USAGE, range: '30d', parse: parseOllamaUsage },
225
+ },
226
+ ]
227
+
228
+ export { TIMEOUT }
package/src/version.js CHANGED
@@ -1,37 +1,44 @@
1
- /**
2
- * Version ordering for the update check.
3
- *
4
- * Local builds are published as `X.Y.Z-local.N`; that is a prerelease of the same
5
- * X.Y.Z, so `1.0.7-local.1 < 1.0.7` and local numbers grow numerically.
6
- */
7
-
8
- function parseVersion(value) {
9
- const text = String(value ?? '')
10
- const match = /^(\d+)\.(\d+)\.(\d+)(?:-local\.(\d+))?$/.exec(text)
11
- if (match === null) {
12
- const parts = text.split('.').map((part) => Number.parseInt(part, 10) || 0)
13
- return { base: [parts[0] ?? 0, parts[1] ?? 0, parts[2] ?? 0], local: null }
14
- }
15
- return {
16
- base: [Number(match[1]), Number(match[2]), Number(match[3])],
17
- local: match[4] === undefined ? null : Number(match[4]),
18
- }
19
- }
20
-
21
- /** -1 / 0 / 1 for left older than / equal to / newer than right. */
22
- export function compareVersions(left, right) {
23
- const a = parseVersion(left)
24
- const b = parseVersion(right)
25
- for (let index = 0; index < 3; index += 1) {
26
- if (a.base[index] !== b.base[index]) return a.base[index] > b.base[index] ? 1 : -1
27
- }
28
- if (a.local === null && b.local === null) return 0
29
- if (a.local === null) return 1
30
- if (b.local === null) return -1
31
- return a.local === b.local ? 0 : a.local > b.local ? 1 : -1
32
- }
33
-
34
- /** True when `latest` is strictly newer than `current`. */
35
- export function isNewer(latest, current) {
36
- return compareVersions(latest, current) > 0
37
- }
1
+ /**
2
+ * Version ordering for the update check.
3
+ *
4
+ * Local builds are published as `X.Y.Z-local.N`; that is a prerelease of the same
5
+ * X.Y.Z, so `1.0.7-local.1 < 1.0.7` and local numbers grow numerically.
6
+ */
7
+
8
+ function parseVersion(value) {
9
+ const text = String(value ?? '')
10
+ const match = /^(\d+)\.(\d+)\.(\d+)(?:-local\.(\d+))?$/.exec(text)
11
+ if (match === null) {
12
+ const parts = text.split('.').map((part) => Number.parseInt(part, 10) || 0)
13
+ return { base: [parts[0] ?? 0, parts[1] ?? 0, parts[2] ?? 0], local: null }
14
+ }
15
+ return {
16
+ base: [Number(match[1]), Number(match[2]), Number(match[3])],
17
+ local: match[4] === undefined ? null : Number(match[4]),
18
+ }
19
+ }
20
+
21
+ /** -1 / 0 / 1 for left older than / equal to / newer than right. */
22
+ export function compareVersions(left, right) {
23
+ const a = parseVersion(left)
24
+ const b = parseVersion(right)
25
+ for (let index = 0; index < 3; index += 1) {
26
+ if (a.base[index] !== b.base[index]) return a.base[index] > b.base[index] ? 1 : -1
27
+ }
28
+ if (a.local === null && b.local === null) return 0
29
+ if (a.local === null) return 1
30
+ if (b.local === null) return -1
31
+ return a.local === b.local ? 0 : a.local > b.local ? 1 : -1
32
+ }
33
+
34
+ /** True when both share the same X.Y.Z base, ignoring the local suffix. */
35
+ export function sameBase(left, right) {
36
+ const a = parseVersion(left)
37
+ const b = parseVersion(right)
38
+ return a.base[0] === b.base[0] && a.base[1] === b.base[1] && a.base[2] === b.base[2]
39
+ }
40
+
41
+ /** True when `latest` is strictly newer than `current`. */
42
+ export function isNewer(latest, current) {
43
+ return compareVersions(latest, current) > 0
44
+ }