-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsession.ts
More file actions
106 lines (92 loc) · 3.29 KB
/
Copy pathsession.ts
File metadata and controls
106 lines (92 loc) · 3.29 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
// ─────────────────────────────────────────────────────────────────────────
// session manager
//
// each connected user gets a Session. the session id is derived from the
// chosen username so reconnecting from the same browser tab resumes the same
// session (no state loss between flaky network blips).
//
// the project workspace is cloned from apps/server/templates/greeter-api on
// first hello. ports are handed out by workspace.ts on demand.
// ─────────────────────────────────────────────────────────────────────────
import type { ServerWebSocket } from "bun"
import { destroyWorkspace, ensureWorkspace } from "./workspace"
export type Session = {
/** opaque id used to label all docker artifacts */
id: string
username: string
/** absolute path of the user's *home directory* on the server */
homeDir: string
/**
* absolute path of the demo project (greeter-api) inside the home dir.
* cached so docker build/compose don't have to recompute it.
*/
projectDir: string
createdAt: number
/** the live websocket for this session, if any */
socket?: ServerWebSocket<SocketData>
}
/** Per-socket attachment used by Bun.serve's websocket handler. */
export type SocketData = {
sessionId: string | null
}
const sessions = new Map<string, Session>()
/** Sanitize a username into something safe to use in docker labels and paths. */
function sanitize(username: string): string {
return username
.toLowerCase()
.replace(/[^a-z0-9-]/g, "-")
.replace(/-+/g, "-")
.replace(/^-|-$/g, "")
.slice(0, 24) || "anon"
}
export function getOrCreateSession(username: string, workspaceRoot: string): Session {
const id = sanitize(username)
const existing = sessions.get(id)
if (existing) return existing
// Build the user's home dir on first hello. Returns both the home root
// (the user's "filesystem") and the project subdir (docker build cwd).
const { homeDir, projectDir } = ensureWorkspace(workspaceRoot, id)
const session: Session = {
id,
username,
homeDir,
projectDir,
createdAt: Date.now(),
}
sessions.set(id, session)
return session
}
export function getSession(id: string): Session | undefined {
return sessions.get(id)
}
export function attachSocket(
sessionId: string,
socket: ServerWebSocket<SocketData>
): void {
const s = sessions.get(sessionId)
if (!s) return
s.socket = socket
}
export function detachSocket(sessionId: string): void {
const s = sessions.get(sessionId)
if (!s) return
s.socket = undefined
}
/**
* Drop a session from the in-memory map and delete its workspace on disk.
* Called by the cleanup sweep after the grace period expires.
*/
export function removeSession(sessionId: string, workspaceRoot: string): void {
const s = sessions.get(sessionId)
if (!s) return
sessions.delete(sessionId)
try {
destroyWorkspace(workspaceRoot, sessionId)
} catch {
/* best effort */
}
}
/** Used by the dev "/_debug" endpoint and tests. */
export function listSessions(): Session[] {
return [...sessions.values()]
}