dsh-token-dash 2.3.0 → 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.
- package/README.en.md +11 -8
- package/README.md +11 -8
- package/client.js +302 -76
- package/index.js +30 -8
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +4 -3
- package/src/adapters.js +568 -561
- package/src/core.js +919 -887
- package/src/diagnostics.js +160 -108
- package/src/quota-sources.js +228 -224
- package/src/version.js +44 -37
package/src/quota-sources.js
CHANGED
|
@@ -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
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
if (included
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
{ id: '
|
|
198
|
-
{ id: '
|
|
199
|
-
{
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
]
|
|
223
|
-
|
|
224
|
-
|
|
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
|
|
35
|
-
export function
|
|
36
|
-
|
|
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
|
+
}
|