* Spawns a new PTY, registers it in the session store, and wires up all * event plumbing between the PTY and the WebSocket. * * Returns the session token, or null if at capacity or PTY spawn fails. * When `user` is provided the session is associated with that user and * per-user limit
(ws: WebSocket, cols = 80, rows = 24, user?: AuthUser)
| 135 | * per-user limits are enforced. |
| 136 | */ |
| 137 | create(ws: WebSocket, cols = 80, rows = 24, user?: AuthUser): string | null { |
| 138 | if (this.isFull) return null; |
| 139 | |
| 140 | const userId = user?.id ?? "default"; |
| 141 | |
| 142 | if (this.isUserAtConcurrentLimit(userId)) { |
| 143 | ws.send( |
| 144 | JSON.stringify({ |
| 145 | type: "error", |
| 146 | message: `Session limit reached for your account (max ${this.maxSessionsPerUser}).`, |
| 147 | }), |
| 148 | ); |
| 149 | ws.close(1013, "Per-user session limit reached"); |
| 150 | return null; |
| 151 | } |
| 152 | |
| 153 | if (this.isUserRateLimited(userId)) { |
| 154 | ws.send( |
| 155 | JSON.stringify({ |
| 156 | type: "error", |
| 157 | message: "Too many sessions created recently. Please wait before starting a new session.", |
| 158 | }), |
| 159 | ); |
| 160 | ws.close(1013, "Rate limited"); |
| 161 | return null; |
| 162 | } |
| 163 | |
| 164 | let pty: IPty; |
| 165 | try { |
| 166 | pty = this.spawnPty(cols, rows, user); |
| 167 | } catch (err) { |
| 168 | const message = |
| 169 | err instanceof Error ? err.message : "Unknown PTY spawn error"; |
| 170 | ws.send( |
| 171 | JSON.stringify({ type: "error", message: `PTY spawn failed: ${message}` }), |
| 172 | ); |
| 173 | ws.close(1011, "PTY spawn failure"); |
| 174 | return null; |
| 175 | } |
| 176 | |
| 177 | // Record the creation only after a successful spawn. |
| 178 | this.rateLimiter.record(userId); |
| 179 | |
| 180 | const session = this.store.register(pty, userId); |
| 181 | session.ws = ws; |
| 182 | const { token } = session; |
| 183 | |
| 184 | this.wirePtyEvents(token, pty); |
| 185 | this.wireWsEvents(token, ws, pty); |
| 186 | |
| 187 | console.log( |
| 188 | `[session ${token.slice(0, 8)}] Created for user ${userId} (active: ${this.store.size}/${this.maxSessions})`, |
| 189 | ); |
| 190 | return token; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Attaches a new WebSocket to an existing session identified by `token`. |
nothing calls this directly
no test coverage detected