-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathdev-server.mjs
More file actions
68 lines (61 loc) · 2.28 KB
/
Copy pathdev-server.mjs
File metadata and controls
68 lines (61 loc) · 2.28 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
// Minimal dev host backend for apps/web.
//
// The web app's HOST_TRPC_CLIENT points at this over HTTP (the electron
// equivalent is the IPC-served host router). A real cloud backend is a future
// workstream; this serves just the boot-path procedures so the @posthog/ui app
// renders its real first screen (the login/auth screen) in a browser instead of
// hanging on the auth-bootstrap spinner.
//
// Plain JS so it runs under bare `node` with no TS toolchain — it depends only
// on @trpc/server, never on the workspace TS sources.
import { initTRPC } from "@trpc/server";
import { createHTTPServer } from "@trpc/server/adapters/standalone";
const PORT = 8787;
const t = initTRPC.create();
const router = t.router;
const publicProcedure = t.procedure;
// A bootstrapped, logged-out state. `bootstrapComplete: true` is what releases
// the app's Loading gate; `status: "anonymous"` makes it render the login screen.
const ANONYMOUS_BOOTSTRAPPED = {
status: "anonymous",
bootstrapComplete: true,
cloudRegion: null,
orgProjectsMap: {},
currentOrgId: null,
currentProjectId: null,
hasCodeAccess: null,
needsScopeReauth: false,
};
const appRouter = router({
auth: router({
getState: publicProcedure.query(() => ANONYMOUS_BOOTSTRAPPED),
onStateChanged: publicProcedure.subscription(async function* (opts) {
yield ANONYMOUS_BOOTSTRAPPED;
// Hold the stream open until the client disconnects; tRPC aborts the
// generator via opts.signal so this doesn't leak.
await new Promise((resolve) => {
opts.signal?.addEventListener("abort", () => resolve(undefined));
});
}),
}),
analytics: router({
resetUser: publicProcedure.mutation(() => undefined),
setUserId: publicProcedure.mutation(() => undefined),
}),
});
createHTTPServer({
router: appRouter,
// Cross-origin: the Vite dev app is on :5273, this backend on :8787.
middleware: (req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
res.setHeader("Access-Control-Allow-Headers", "*");
if (req.method === "OPTIONS") {
res.writeHead(204);
res.end();
return;
}
next();
},
}).listen(PORT);
console.log(`[web dev-server] listening on http://localhost:${PORT}`);