refactor(server): extract createApp function for server initialization
- Replace Server.App() with Server.Default() for internal server access - Extract server app creation into Server.createApp(opts) for testability - Move CORS whitelist from module-level variable to function parameter - Update all tests to use Server.Default() instead of Server.App()
This commit is contained in:
@@ -667,7 +667,7 @@ export const RunCommand = cmd({
|
|||||||
await bootstrap(process.cwd(), async () => {
|
await bootstrap(process.cwd(), async () => {
|
||||||
const fetchFn = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
const fetchFn = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
||||||
const request = new Request(input, init)
|
const request = new Request(input, init)
|
||||||
return Server.App().fetch(request)
|
return Server.Default().fetch(request)
|
||||||
}) as typeof globalThis.fetch
|
}) as typeof globalThis.fetch
|
||||||
const sdk = createOpencodeClient({ baseUrl: "http://opencode.internal", fetch: fetchFn })
|
const sdk = createOpencodeClient({ baseUrl: "http://opencode.internal", fetch: fetchFn })
|
||||||
await execute(sdk)
|
await execute(sdk)
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ const startEventStream = (input: { directory: string; workspaceID?: string }) =>
|
|||||||
const request = new Request(input, init)
|
const request = new Request(input, init)
|
||||||
const auth = getAuthorizationHeader()
|
const auth = getAuthorizationHeader()
|
||||||
if (auth) request.headers.set("Authorization", auth)
|
if (auth) request.headers.set("Authorization", auth)
|
||||||
return Server.App().fetch(request)
|
return Server.Default().fetch(request)
|
||||||
}) as typeof globalThis.fetch
|
}) as typeof globalThis.fetch
|
||||||
|
|
||||||
const sdk = createOpencodeClient({
|
const sdk = createOpencodeClient({
|
||||||
@@ -110,7 +110,7 @@ export const rpc = {
|
|||||||
headers,
|
headers,
|
||||||
body: input.body,
|
body: input.body,
|
||||||
})
|
})
|
||||||
const response = await Server.App().fetch(request)
|
const response = await Server.Default().fetch(request)
|
||||||
const body = await response.text()
|
const body = await response.text()
|
||||||
return {
|
return {
|
||||||
status: response.status,
|
status: response.status,
|
||||||
|
|||||||
@@ -25,8 +25,7 @@ export namespace Plugin {
|
|||||||
const client = createOpencodeClient({
|
const client = createOpencodeClient({
|
||||||
baseUrl: "http://localhost:4096",
|
baseUrl: "http://localhost:4096",
|
||||||
directory: Instance.directory,
|
directory: Instance.directory,
|
||||||
// @ts-ignore - fetch type incompatibility
|
fetch: async (...args) => Server.Default().fetch(...args),
|
||||||
fetch: async (...args) => Server.App().fetch(...args),
|
|
||||||
})
|
})
|
||||||
const config = await Config.get()
|
const config = await Config.get()
|
||||||
const hooks: Hooks[] = []
|
const hooks: Hooks[] = []
|
||||||
@@ -35,7 +34,9 @@ export namespace Plugin {
|
|||||||
project: Instance.project,
|
project: Instance.project,
|
||||||
worktree: Instance.worktree,
|
worktree: Instance.worktree,
|
||||||
directory: Instance.directory,
|
directory: Instance.directory,
|
||||||
serverUrl: Server.url(),
|
get serverUrl(): URL {
|
||||||
|
throw new Error("Server URL is no longer supported in plugins")
|
||||||
|
},
|
||||||
$: Bun.$,
|
$: Bun.$,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -19,7 +19,7 @@ afterEach(async () => {
|
|||||||
describe("project.initGit endpoint", () => {
|
describe("project.initGit endpoint", () => {
|
||||||
test("initializes git and reloads immediately", async () => {
|
test("initializes git and reloads immediately", async () => {
|
||||||
await using tmp = await tmpdir()
|
await using tmp = await tmpdir()
|
||||||
const app = Server.App()
|
const app = Server.Default()
|
||||||
const seen: { directory?: string; payload: { type: string } }[] = []
|
const seen: { directory?: string; payload: { type: string } }[] = []
|
||||||
const fn = (evt: { directory?: string; payload: { type: string } }) => {
|
const fn = (evt: { directory?: string; payload: { type: string } }) => {
|
||||||
seen.push(evt)
|
seen.push(evt)
|
||||||
@@ -75,7 +75,7 @@ describe("project.initGit endpoint", () => {
|
|||||||
|
|
||||||
test("does not reload when the project is already git", async () => {
|
test("does not reload when the project is already git", async () => {
|
||||||
await using tmp = await tmpdir({ git: true })
|
await using tmp = await tmpdir({ git: true })
|
||||||
const app = Server.App()
|
const app = Server.Default()
|
||||||
const seen: { directory?: string; payload: { type: string } }[] = []
|
const seen: { directory?: string; payload: { type: string } }[] = []
|
||||||
const fn = (evt: { directory?: string; payload: { type: string } }) => {
|
const fn = (evt: { directory?: string; payload: { type: string } }) => {
|
||||||
seen.push(evt)
|
seen.push(evt)
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ describe("tui.selectSession endpoint", () => {
|
|||||||
const session = await Session.create({})
|
const session = await Session.create({})
|
||||||
|
|
||||||
// #when
|
// #when
|
||||||
const app = Server.App()
|
const app = Server.Default()
|
||||||
const response = await app.request("/tui/select-session", {
|
const response = await app.request("/tui/select-session", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
@@ -42,7 +42,7 @@ describe("tui.selectSession endpoint", () => {
|
|||||||
const nonExistentSessionID = "ses_nonexistent123"
|
const nonExistentSessionID = "ses_nonexistent123"
|
||||||
|
|
||||||
// #when
|
// #when
|
||||||
const app = Server.App()
|
const app = Server.Default()
|
||||||
const response = await app.request("/tui/select-session", {
|
const response = await app.request("/tui/select-session", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
@@ -63,7 +63,7 @@ describe("tui.selectSession endpoint", () => {
|
|||||||
const invalidSessionID = "invalid_session_id"
|
const invalidSessionID = "invalid_session_id"
|
||||||
|
|
||||||
// #when
|
// #when
|
||||||
const app = Server.App()
|
const app = Server.Default()
|
||||||
const response = await app.request("/tui/select-session", {
|
const response = await app.request("/tui/select-session", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
|
|||||||
Reference in New Issue
Block a user