@magpie-community/opencode-mimo-auth 0.1.2 → 0.1.4
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/index.mjs +28 -11
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -324,7 +324,9 @@ function serverTime(v) {
|
|
|
324
324
|
// ---- the plugin -------------------------------------------------------------
|
|
325
325
|
|
|
326
326
|
export const MimoAuthPlugin = async ({ client }) => {
|
|
327
|
-
|
|
327
|
+
// renewals under way, one to an account: two accounts signing on at
|
|
328
|
+
// once each keep their own session
|
|
329
|
+
const renewing = new Map()
|
|
328
330
|
|
|
329
331
|
const save = async (auth) => {
|
|
330
332
|
try {
|
|
@@ -339,8 +341,10 @@ export const MimoAuthPlugin = async ({ client }) => {
|
|
|
339
341
|
const a = fromAuth(await getAuth())
|
|
340
342
|
if (!a) throw new Error("Xiaomi MiMo: not signed in")
|
|
341
343
|
if (!force && Object.keys(a.cookies).length && Date.now() < a.expires) return a
|
|
342
|
-
|
|
343
|
-
|
|
344
|
+
const who = String(a.creds.userId ?? "")
|
|
345
|
+
let r = renewing.get(who)
|
|
346
|
+
if (!r) {
|
|
347
|
+
r = (async () => {
|
|
344
348
|
try {
|
|
345
349
|
const s = await session(a.creds, a.creds.base)
|
|
346
350
|
const issued = Date.now()
|
|
@@ -352,10 +356,11 @@ export const MimoAuthPlugin = async ({ client }) => {
|
|
|
352
356
|
throw e
|
|
353
357
|
}
|
|
354
358
|
})().finally(() => {
|
|
355
|
-
renewing
|
|
359
|
+
renewing.delete(who)
|
|
356
360
|
})
|
|
361
|
+
renewing.set(who, r)
|
|
357
362
|
}
|
|
358
|
-
return
|
|
363
|
+
return r
|
|
359
364
|
}
|
|
360
365
|
|
|
361
366
|
// page asks the MiMo server for one of the account's pages, signing on
|
|
@@ -452,6 +457,9 @@ export const MimoAuthPlugin = async ({ client }) => {
|
|
|
452
457
|
const req = input instanceof Request ? input : null
|
|
453
458
|
const url = req ? req.url : String(input)
|
|
454
459
|
let body = init.body ?? (req ? await req.text() : undefined)
|
|
460
|
+
// a body handed over as bytes is read as the string it is
|
|
461
|
+
if (body instanceof ArrayBuffer) body = new TextDecoder().decode(body)
|
|
462
|
+
else if (ArrayBuffer.isView(body)) body = new TextDecoder().decode(new Uint8Array(body.buffer, body.byteOffset, body.byteLength))
|
|
455
463
|
// mimo-auto, the app's default, is asked as the model it stands for
|
|
456
464
|
if (typeof body === "string" && body.includes('"mimo-auto"')) {
|
|
457
465
|
try {
|
|
@@ -469,12 +477,21 @@ export const MimoAuthPlugin = async ({ client }) => {
|
|
|
469
477
|
headers.set("X-Client-Version", APP_VERSION)
|
|
470
478
|
return fetch(url, { ...init, method: init.method ?? req?.method ?? "POST", headers, body })
|
|
471
479
|
}
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
480
|
+
try {
|
|
481
|
+
const res = await send(false)
|
|
482
|
+
// a session gone stale, or a redirect to Xiaomi's sign-in page
|
|
483
|
+
const stale = res.status === 401 || (res.status === 200 && /text\/html/i.test(res.headers.get("content-type") ?? ""))
|
|
484
|
+
if (!stale || (body != null && typeof body !== "string")) return res
|
|
485
|
+
await res.arrayBuffer().catch(() => {})
|
|
486
|
+
return await send(true)
|
|
487
|
+
} catch (e) {
|
|
488
|
+
// the passToken no longer signs the account on: the account's
|
|
489
|
+
// own 401, in the built-in's words (mimoLapse)
|
|
490
|
+
if (!(e instanceof Lapsed)) throw e
|
|
491
|
+
const who = fromAuth(await getAuth())?.creds.userId ?? a.creds.userId
|
|
492
|
+
const message = `${who}: the Xiaomi MiMo sign-in has expired — sign in again`
|
|
493
|
+
return new Response(JSON.stringify({ error: { message, type: "authentication_error", code: null } }), { status: 401, headers: { "content-type": "application/json" } })
|
|
494
|
+
}
|
|
478
495
|
},
|
|
479
496
|
}
|
|
480
497
|
},
|
package/package.json
CHANGED