From e073412da12c98689a0330a99cf777a52ce3f73a Mon Sep 17 00:00:00 2001 From: YeonGyu-Kim Date: Wed, 4 Feb 2026 14:52:31 +0900 Subject: [PATCH] fix(auth): add graceful fallback for server auth injection Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus --- src/shared/opencode-server-auth.test.ts | 15 +++---- src/shared/opencode-server-auth.ts | 60 +++++++++++++------------ 2 files changed, 39 insertions(+), 36 deletions(-) diff --git a/src/shared/opencode-server-auth.test.ts b/src/shared/opencode-server-auth.test.ts index 809dd502f..7d7623acc 100644 --- a/src/shared/opencode-server-auth.test.ts +++ b/src/shared/opencode-server-auth.test.ts @@ -1,3 +1,6 @@ +/// + +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", () => { diff --git a/src/shared/opencode-server-auth.ts b/src/shared/opencode-server-auth.ts index 75ac81b2b..d4bbb346d 100644 --- a/src/shared/opencode-server-auth.ts +++ b/src/shared/opencode-server-auth.ts @@ -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 }) => 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 }) => 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, - }, - }) }