@magpie-community/opencode-qoder-auth 0.2.79 → 0.2.80

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.
Files changed (2) hide show
  1. package/index.mjs +22 -5
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -311,6 +311,12 @@ function expiresAt(jt) {
311
311
  return Date.now() + DAY
312
312
  }
313
313
 
314
+ // renewedTokens is whether next, what a renewal of a gave, holds another
315
+ // token than a: a new end alone is no new sign-in.
316
+ function renewedTokens(a, next) {
317
+ return ["access", "refresh", "deviceToken", "deviceRefresh"].some((k) => (a?.[k] ?? "") !== (next?.[k] ?? ""))
318
+ }
319
+
314
320
  // whenSaid is the time a token's end field names, in milliseconds: a date,
315
321
  // or an epoch in seconds or milliseconds; 0 for none (absent, 0, unreadable).
316
322
  function whenSaid(v) {
@@ -3673,6 +3679,16 @@ const makePlugin = (site) => async ({ client }) => {
3673
3679
  return renewals.has(cred) || was
3674
3680
  }
3675
3681
 
3682
+ // saveRenewed keeps what a renewal of a gave: saved only when it is
3683
+ // another token (a refresh can hand back the one it was asked with, and
3684
+ // a save that changes nothing has magpie read every sign-in again, from a
3685
+ // models listing too: plugins#1670)
3686
+ const saveRenewed = async (a, next) => {
3687
+ if (!renewedTokens(a, next)) return
3688
+ await client.auth.set({ path: { id: ID }, body: next })
3689
+ renewals.add(next)
3690
+ }
3691
+
3676
3692
  // fresh is the account with a live job token, refreshed and saved near
3677
3693
  // its end.
3678
3694
  const fresh = (getAuth) =>
@@ -3684,8 +3700,7 @@ const makePlugin = (site) => async ({ client }) => {
3684
3700
  const a = latest(stored)
3685
3701
  if (a.expires - Date.now() > REFRESH_LEAD) return a
3686
3702
  const next = { ...a, ...(await renew(a)) }
3687
- await client.auth.set({ path: { id: ID }, body: next })
3688
- renewals.add(next)
3703
+ await saveRenewed(a, next)
3689
3704
  return next
3690
3705
  })
3691
3706
 
@@ -3710,8 +3725,7 @@ const makePlugin = (site) => async ({ client }) => {
3710
3725
  const now = Date.now()
3711
3726
  for (const v of spent.values()) if (v.got.access === refused && now - v.at < RELOGIN_GAP) return undefined
3712
3727
  const next = { ...a, ...(await renew(a)) }
3713
- await client.auth.set({ path: { id: ID }, body: next })
3714
- renewals.add(next)
3728
+ await saveRenewed(a, next)
3715
3729
  return next
3716
3730
  })
3717
3731
 
@@ -4500,7 +4514,10 @@ const makePlugin = (site) => async ({ client }) => {
4500
4514
  } catch (e) {
4501
4515
  if (e?.expired) throw Object.assign(new Error(e.message), { signIn: "expired" })
4502
4516
  }
4503
- if (next && next.access !== cred.access) {
4517
+ // a renewal that gave the same token back renewed nothing: the
4518
+ // free view is that account's list, as for a renewed token
4519
+ if (next && next.access === cred.access) stillFree.add(cred.access)
4520
+ else if (next) {
4504
4521
  if (renewals.has(next)) unsaid.add(listingKey(next))
4505
4522
  cred = next
4506
4523
  ms = await models(cred, true)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-qoder-auth",
3
- "version": "0.2.79",
3
+ "version": "0.2.80",
4
4
  "description": "Qoder and QwenWork (千问办公) subscriptions in OpenCode and magpie: qoder.com, qoder.cn, gateway.qwenwork.cn and the enterprise flagship, VPC and dedicated domains",
5
5
  "type": "module",
6
6
  "main": "./opencode.mjs",