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:
parent
0dd42e2901
commit
e073412da1
@ -1,3 +1,6 @@
|
|||||||
|
/// <reference types="bun-types" />
|
||||||
|
|
||||||
|
import { describe, test, expect, beforeEach, afterEach } from "bun:test"
|
||||||
import { getServerBasicAuthHeader, injectServerAuthIntoClient } from "./opencode-server-auth"
|
import { getServerBasicAuthHeader, injectServerAuthIntoClient } from "./opencode-server-auth"
|
||||||
|
|
||||||
describe("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"
|
process.env.OPENCODE_SERVER_PASSWORD = "secret"
|
||||||
const client = {}
|
const client = {}
|
||||||
|
|
||||||
expect(() => injectServerAuthIntoClient(client)).toThrow(
|
expect(() => injectServerAuthIntoClient(client)).not.toThrow()
|
||||||
"[opencode-server-auth] OPENCODE_SERVER_PASSWORD is set but SDK client structure is incompatible"
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
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"
|
process.env.OPENCODE_SERVER_PASSWORD = "secret"
|
||||||
const client = { _client: {} }
|
const client = { _client: {} }
|
||||||
|
|
||||||
expect(() => injectServerAuthIntoClient(client)).toThrow(
|
expect(() => injectServerAuthIntoClient(client)).not.toThrow()
|
||||||
"[opencode-server-auth] OPENCODE_SERVER_PASSWORD is set but SDK client._client.setConfig is not a function"
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
test("#given no server password #when client is invalid #then does not throw", () => {
|
test("#given no server password #when client is invalid #then does not throw", () => {
|
||||||
|
|||||||
@ -33,33 +33,37 @@ export function injectServerAuthIntoClient(client: unknown): void {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Runtime type guard for SDK client structure
|
try {
|
||||||
if (
|
if (
|
||||||
typeof client !== "object" ||
|
typeof client !== "object" ||
|
||||||
client === null ||
|
client === null ||
|
||||||
!("_client" in client) ||
|
!("_client" in client) ||
|
||||||
typeof (client as { _client: unknown })._client !== "object" ||
|
typeof (client as { _client: unknown })._client !== "object" ||
|
||||||
(client as { _client: unknown })._client === null
|
(client as { _client: unknown })._client === null
|
||||||
) {
|
) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"[opencode-server-auth] OPENCODE_SERVER_PASSWORD is set but SDK client structure is incompatible. " +
|
"[opencode-server-auth] OPENCODE_SERVER_PASSWORD is set but SDK client structure is incompatible. " +
|
||||||
"This may indicate an OpenCode SDK version mismatch."
|
"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,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user