Repository navigation
Expand file tree
/
Copy pathdev-server.ts
More file actions
151 lines (130 loc) · 5.83 KB
/
Copy pathdev-server.ts
File metadata and controls
151 lines (130 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* Dev server — the single server for `bun web`.
*
* One process, one port. This server:
* 1. Exposes ACN ensurance
* 2. Serves the web app via Vite's middleware
* 3. Exposes one cancellable ensure stream
* 4. Proxies RPC, health, and logs to the fixed daemon endpoint; RPC retains the SDK instance fence
*
* The browser talks only to this same-origin server.
*/
import http, { createServer, type ServerResponse } from "node:http"
import { createServer as createViteServer } from "vite"
import { Effect, Exit, Fiber, Layer, Runtime, Option, Schema, Scope, Stream } from "effect"
import { FetchHttpClient } from "@effect/platform"
import { BunContext } from "@effect/platform-bun"
import { makeLocalAcnInstanceManager, BunDetachedChildProcessSpawner, ChildProcessSpawner, DAEMON_TARGET, makeServiceStarter } from "@magnitudedev/daemon-management"
import { MAGNITUDE_SERVICE_ORIGIN, ServiceStartFailed } from "@magnitudedev/sdk"
import { RemoteServiceStartMessageSchema, type RemoteServiceStartMessage } from "@magnitudedev/client-common"
import { BunSqliteDriverLayer } from "@magnitudedev/daemon-management/bun"
import { resolve } from "node:path"
// ─── Daemon host boundaries ─────────────────────────────────────────────────
const rt = Runtime.defaultRuntime
const ensurerScope = await Runtime.runPromise(rt)(Scope.make())
const acnSourcePath = resolve(import.meta.dir, "..", "..", "packages", "acn", "src", "binary.ts")
const developmentDataDir = process.env.MAGNITUDE_DEV_DATA_DIR
async function createEnsurer() {
return Runtime.runPromise(rt)(makeLocalAcnInstanceManager({
dataDir: developmentDataDir,
launchOverride: {
target: DAEMON_TARGET,
command: ["bun", acnSourcePath, "serve"],
},
}).pipe(
Effect.provideService(ChildProcessSpawner, BunDetachedChildProcessSpawner),
Effect.provideService(Scope.Scope, ensurerScope),
Effect.provide(Layer.mergeAll(FetchHttpClient.layer, BunContext.layer, BunSqliteDriverLayer)),
))
}
const managerPromise = createEnsurer()
// ─── Dev-mode launch command ────────────────────────────────────────────────
const encodeStartMessage = Schema.encode(Schema.parseJson(RemoteServiceStartMessageSchema))
const respondError = async (
res: ServerResponse,
status: number,
error: unknown,
): Promise<void> => {
res.writeHead(status, { "Content-Type": "application/json" })
res.end(JSON.stringify({ error: String(error) }))
}
// ─── HTTP server with Vite middleware ─────────────────────────────────
const PORT = Number(process.env.PORT) || 5173
const vite = await createViteServer({
configFile: resolve(import.meta.dir, "..", "vite.config.ts"),
root: resolve(import.meta.dir, ".."),
server: { middlewareMode: true },
})
const server = createServer(async (req, res) => {
const url = new URL(req.url!, `http://localhost:${PORT}`)
// ── ACN process operations ──────────────────────────────────────
if (url.pathname === "/service/start" && req.method === "POST") {
try {
const manager = await managerPromise
res.writeHead(200, {
"Content-Type": "application/x-ndjson",
"Cache-Control": "no-store",
})
const write = (message: RemoteServiceStartMessage) => {
return encodeStartMessage(message).pipe(
Effect.flatMap((encoded) =>
Effect.sync(() => {
if (!res.destroyed) res.write(`${encoded}\n`)
}),
),
// Every message originates from the schema-typed instance manager. An
// encode failure is therefore an internal invariant violation, not an
// ensurance-domain failure that can be represented on this same stream.
Effect.orDie,
)
}
const observer = Runtime.runFork(rt)(makeServiceStarter(manager).start.pipe(
Stream.runForEach(write),
Effect.catchAll((error) => write({ _tag: "Failed", error })),
))
const interrupt = () => { Runtime.runFork(rt)(Fiber.interrupt(observer)) }
res.once("close", interrupt)
await Runtime.runPromise(rt)(Fiber.await(observer))
res.off("close", interrupt)
if (!res.destroyed) res.end()
} catch (err) {
if (res.headersSent) res.destroy(err instanceof Error ? err : new Error(String(err)))
else await respondError(res, 500, err)
}
return
}
// ── Same-origin ACN proxy (streaming) ───────────────────────────
const proxyMatch = url.pathname.match(/^\/acn(\/rpc|\/health|\/logs)$/)
if (proxyMatch) {
const targetPath = proxyMatch[1]!
const target = new URL(MAGNITUDE_SERVICE_ORIGIN)
const proxyReq = http.request({
hostname: target.hostname,
port: target.port,
path: targetPath + url.search,
method: req.method,
headers: req.headers,
}, (proxyRes) => {
res.writeHead(proxyRes.statusCode ?? 502, proxyRes.headers)
proxyRes.pipe(res)
})
proxyReq.on("error", (error) => {
if (res.headersSent) {
res.destroy(error)
return
}
if (targetPath === "/health") res.destroy(error)
else void respondError(res, 502, error)
})
req.pipe(proxyReq)
return
}
// ── Everything else → Vite ───────────────────────────────────────
vite.middlewares(req, res)
})
server.listen(PORT, () => {
console.log(`[dev] Server running at http://localhost:${PORT}`)
})
server.on("close", () => {
void Runtime.runPromise(rt)(Scope.close(ensurerScope, Exit.void))
})