Skip to content

Commit af2c647

Browse files
kitlangtonopencode
authored andcommitted
refactor: unwrap Server namespace + self-reexport (#22955)
1 parent 6a80289 commit af2c647

1 file changed

Lines changed: 84 additions & 84 deletions

File tree

packages/opencode/src/server/server.ts

Lines changed: 84 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -17,111 +17,111 @@ globalThis.AI_SDK_LOG_WARNINGS = false
1717

1818
initProjectors()
1919

20-
export namespace Server {
21-
const log = Log.create({ service: "server" })
20+
const log = Log.create({ service: "server" })
2221

23-
export type Listener = {
24-
hostname: string
25-
port: number
26-
url: URL
27-
stop: (close?: boolean) => Promise<void>
28-
}
29-
30-
export const Default = lazy(() => create({}))
22+
export type Listener = {
23+
hostname: string
24+
port: number
25+
url: URL
26+
stop: (close?: boolean) => Promise<void>
27+
}
3128

32-
function create(opts: { cors?: string[] }) {
33-
const app = new Hono()
34-
const runtime = adapter.create(app)
29+
export const Default = lazy(() => create({}))
3530

36-
if (Flag.OPENCODE_WORKSPACE_ID) {
37-
return {
38-
app: app
39-
.onError(ErrorMiddleware)
40-
.use(AuthMiddleware)
41-
.use(LoggerMiddleware)
42-
.use(CompressionMiddleware)
43-
.use(CorsMiddleware(opts))
44-
.use(FenceMiddleware)
45-
.route("/", ControlPlaneRoutes())
46-
.route("/", InstanceRoutes(runtime.upgradeWebSocket)),
47-
runtime,
48-
}
49-
}
31+
function create(opts: { cors?: string[] }) {
32+
const app = new Hono()
33+
const runtime = adapter.create(app)
5034

35+
if (Flag.OPENCODE_WORKSPACE_ID) {
5136
return {
5237
app: app
5338
.onError(ErrorMiddleware)
5439
.use(AuthMiddleware)
5540
.use(LoggerMiddleware)
5641
.use(CompressionMiddleware)
5742
.use(CorsMiddleware(opts))
43+
.use(FenceMiddleware)
5844
.route("/", ControlPlaneRoutes())
59-
.route("/", InstanceRoutes(runtime.upgradeWebSocket))
60-
.route("/", UIRoutes()),
45+
.route("/", InstanceRoutes(runtime.upgradeWebSocket)),
6146
runtime,
6247
}
6348
}
6449

65-
export async function openapi() {
66-
// Build a fresh app with all routes registered directly so
67-
// hono-openapi can see describeRoute metadata (`.route()` wraps
68-
// handlers when the sub-app has a custom errorHandler, which
69-
// strips the metadata symbol).
70-
const { app } = create({})
71-
const result = await generateSpecs(app, {
72-
documentation: {
73-
info: {
74-
title: "opencode",
75-
version: "1.0.0",
76-
description: "opencode api",
77-
},
78-
openapi: "3.1.1",
79-
},
80-
})
81-
return result
50+
return {
51+
app: app
52+
.onError(ErrorMiddleware)
53+
.use(AuthMiddleware)
54+
.use(LoggerMiddleware)
55+
.use(CompressionMiddleware)
56+
.use(CorsMiddleware(opts))
57+
.route("/", ControlPlaneRoutes())
58+
.route("/", InstanceRoutes(runtime.upgradeWebSocket))
59+
.route("/", UIRoutes()),
60+
runtime,
8261
}
62+
}
8363

84-
export let url: URL
64+
export async function openapi() {
65+
// Build a fresh app with all routes registered directly so
66+
// hono-openapi can see describeRoute metadata (`.route()` wraps
67+
// handlers when the sub-app has a custom errorHandler, which
68+
// strips the metadata symbol).
69+
const { app } = create({})
70+
const result = await generateSpecs(app, {
71+
documentation: {
72+
info: {
73+
title: "opencode",
74+
version: "1.0.0",
75+
description: "opencode api",
76+
},
77+
openapi: "3.1.1",
78+
},
79+
})
80+
return result
81+
}
8582

86-
export async function listen(opts: {
87-
port: number
88-
hostname: string
89-
mdns?: boolean
90-
mdnsDomain?: string
91-
cors?: string[]
92-
}): Promise<Listener> {
93-
const built = create(opts)
94-
const server = await built.runtime.listen(opts)
83+
export let url: URL
9584

96-
const next = new URL("http://localhost")
97-
next.hostname = opts.hostname
98-
next.port = String(server.port)
99-
url = next
85+
export async function listen(opts: {
86+
port: number
87+
hostname: string
88+
mdns?: boolean
89+
mdnsDomain?: string
90+
cors?: string[]
91+
}): Promise<Listener> {
92+
const built = create(opts)
93+
const server = await built.runtime.listen(opts)
10094

101-
const mdns =
102-
opts.mdns &&
103-
server.port &&
104-
opts.hostname !== "127.0.0.1" &&
105-
opts.hostname !== "localhost" &&
106-
opts.hostname !== "::1"
107-
if (mdns) {
108-
MDNS.publish(server.port, opts.mdnsDomain)
109-
} else if (opts.mdns) {
110-
log.warn("mDNS enabled but hostname is loopback; skipping mDNS publish")
111-
}
95+
const next = new URL("http://localhost")
96+
next.hostname = opts.hostname
97+
next.port = String(server.port)
98+
url = next
11299

113-
let closing: Promise<void> | undefined
114-
return {
115-
hostname: opts.hostname,
116-
port: server.port,
117-
url: next,
118-
stop(close?: boolean) {
119-
closing ??= (async () => {
120-
if (mdns) MDNS.unpublish()
121-
await server.stop(close)
122-
})()
123-
return closing
124-
},
125-
}
100+
const mdns =
101+
opts.mdns &&
102+
server.port &&
103+
opts.hostname !== "127.0.0.1" &&
104+
opts.hostname !== "localhost" &&
105+
opts.hostname !== "::1"
106+
if (mdns) {
107+
MDNS.publish(server.port, opts.mdnsDomain)
108+
} else if (opts.mdns) {
109+
log.warn("mDNS enabled but hostname is loopback; skipping mDNS publish")
110+
}
111+
112+
let closing: Promise<void> | undefined
113+
return {
114+
hostname: opts.hostname,
115+
port: server.port,
116+
url: next,
117+
stop(close?: boolean) {
118+
closing ??= (async () => {
119+
if (mdns) MDNS.unpublish()
120+
await server.stop(close)
121+
})()
122+
return closing
123+
},
126124
}
127125
}
126+
127+
export * as Server from "./server"

0 commit comments

Comments
 (0)