@magpie-community/opencode-qoder-auth 0.2.17 → 0.2.18

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 +14 -4
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -799,16 +799,26 @@ function failure(status, text) {
799
799
  let code = ""
800
800
  let queue
801
801
  try {
802
- const j = JSON.parse(text)
802
+ let j = JSON.parse(msg)
803
+ // #167: an HTTP/message wrapper can hold the actual 10605 body.
804
+ // Unwrap only that known queue code; a real auth/credit/request code
805
+ // on the outer body remains authoritative.
806
+ const outerCode = j?.code == null ? "" : String(j.code)
807
+ if (["", "401", "403"].includes(outerCode) && typeof j?.message === "string") {
808
+ try {
809
+ const inner = JSON.parse(j.message.trim())
810
+ if (inner && !Array.isArray(inner) && String(inner.code) === QUEUED) j = inner
811
+ } catch {}
812
+ }
803
813
  code = j?.code == null ? "" : String(j.code)
804
814
  if (j?.message) msg = String(j.message)
805
815
  // details is a JSON string, or the object itself (gjson reads either)
806
- const d = typeof j?.details === "string" ? JSON.parse(j.details) : j?.details
816
+ const d = typeof j?.details === "string" ? JSON.parse(j.details.trim()) : j?.details
807
817
  if (d?.error?.message) msg += ": " + d.error.message
808
818
  // the queue's state: the message itself, as a JSON string, or a field
809
819
  queue = j?.queue && typeof j.queue === "object" ? j.queue : undefined
810
820
  if (!queue && typeof j?.message === "string" && j.message.trim().startsWith("{")) {
811
- const q = JSON.parse(j.message)
821
+ const q = JSON.parse(j.message.trim())
812
822
  if (q && typeof q === "object" && ("isQueued" in q || "serviceAvailable" in q || "retryAfterSeconds" in q)) queue = q
813
823
  }
814
824
  } catch {}
@@ -1029,7 +1039,7 @@ async function* events(body) {
1029
1039
  if (inner.trim() === "[DONE]") break
1030
1040
  let chunk
1031
1041
  try {
1032
- chunk = JSON.parse(inner)
1042
+ chunk = JSON.parse(inner.trim())
1033
1043
  } catch {
1034
1044
  continue
1035
1045
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-qoder-auth",
3
- "version": "0.2.17",
3
+ "version": "0.2.18",
4
4
  "description": "Qoder subscriptions in OpenCode and magpie: Qoder (qoder.com) and Qoder CN (qoder.cn)",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",