fix(auth): add graceful fallback for server auth injection

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
YeonGyu-Kim
2026-02-04 14:52:31 +09:00
parent 0dd42e2901
commit e073412da1
2 changed files with 39 additions and 36 deletions

View File

@@ -1,3 +1,6 @@
/// <reference types="bun-types" />
import { describe, test, expect, beforeEach, afterEach } from "bun:test"
import { getServerBasicAuthHeader, injectServerAuthIntoClient } from "./opencode-server-auth"
describe("opencode-server-auth", () => {
@@ -68,22 +71,18 @@ describe("opencode-server-auth", () => {
})
})
test("#given server password #when client has no _client #then throws error", () => {
test("#given server password #when client has no _client #then does not throw", () => {
process.env.OPENCODE_SERVER_PASSWORD = "secret"
const client = {}
expect(() => injectServerAuthIntoClient(client)).toThrow(
"[opencode-server-auth] OPENCODE_SERVER_PASSWORD is set but SDK client structure is incompatible"
)
expect(() => injectServerAuthIntoClient(client)).not.toThrow()
})
test("#given server password #when client._client has no setConfig #then throws error", () => {
test("#given server password #when client._client has no setConfig #then does not throw", () => {
process.env.OPENCODE_SERVER_PASSWORD = "secret"
const client = { _client: {} }
expect(() => injectServerAuthIntoClient(client)).toThrow(
"[opencode-server-auth] OPENCODE_SERVER_PASSWORD is set but SDK client._client.setConfig is not a function"
)
expect(() => injectServerAuthIntoClient(client)).not.toThrow()
})
test("#given no server password #when client is invalid #then does not throw", () => {

View File

@@ -33,33 +33,37 @@ export function injectServerAuthIntoClient(client: unknown): void {
return
}
// Runtime type guard for SDK client structure
if (
typeof client !== "object" ||
client === null ||
!("_client" in client) ||
typeof (client as { _client: unknown })._client !== "object" ||
(client as { _client: unknown })._client === null
) {
throw new Error(
"[opencode-server-auth] OPENCODE_SERVER_PASSWORD is set but SDK client structure is incompatible. " +
"This may indicate an OpenCode SDK version mismatch."
)
try {
if (
typeof client !== "object" ||
client === null ||
!("_client" in client) ||
typeof (client as { _client: unknown })._client !== "object" ||
(client as { _client: unknown })._client === null
) {
throw new Error(
"[opencode-server-auth] OPENCODE_SERVER_PASSWORD is set but SDK client structure is incompatible. " +
"This may indicate an OpenCode SDK version mismatch."
)
}
const internal = (client as { _client: { setConfig?: (config: { headers: Record<string, string> }) => void } })
._client
if (typeof internal.setConfig !== "function") {
throw new Error(
"[opencode-server-auth] OPENCODE_SERVER_PASSWORD is set but SDK client._client.setConfig is not a function. " +
"This may indicate an OpenCode SDK version mismatch."
)
}
internal.setConfig({
headers: {
Authorization: auth,
},
})
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
console.warn(`[opencode-server-auth] Failed to inject server auth: ${message}`)
}
const internal = (client as { _client: { setConfig?: (config: { headers: Record<string, string> }) => void } })
._client
if (typeof internal.setConfig !== "function") {
throw new Error(
"[opencode-server-auth] OPENCODE_SERVER_PASSWORD is set but SDK client._client.setConfig is not a function. " +
"This may indicate an OpenCode SDK version mismatch."
)
}
internal.setConfig({
headers: {
Authorization: auth,
},
})
}