forked from michelhelsdingen/ensemble
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
219 lines (188 loc) · 7.24 KB
/
Copy pathserver.ts
File metadata and controls
219 lines (188 loc) · 7.24 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* Ensemble Server — Standalone HTTP server
* Lightweight replacement for Next.js API routes.
*/
import http from 'http'
import {
createEnsembleTeam, getEnsembleTeam, listEnsembleTeams,
getTeamFeed, sendTeamMessage, disbandTeam,
} from './services/ensemble-service'
const PORT = parseInt(process.env.ENSEMBLE_PORT || process.env.ORCHESTRA_PORT || '23000', 10)
const HOST = process.env.ENSEMBLE_HOST || '127.0.0.1'
const RATE_LIMIT_WINDOW_MS = 60_000
const RATE_LIMIT_MAX_REQUESTS = 100
const DEFAULT_CORS_ORIGIN_PATTERNS = [
/^http:\/\/localhost(?::\d+)?$/i,
/^http:\/\/127\.0\.0\.1(?::\d+)?$/i,
/^http:\/\/\[::1\](?::\d+)?$/i,
]
type RateLimitEntry = {
count: number
windowStart: number
}
const rateLimitByIp = new Map<string, RateLimitEntry>()
// Periodic cleanup of stale rate limit entries to prevent unbounded Map growth
setInterval(() => {
const now = Date.now()
for (const [ip, entry] of rateLimitByIp) {
if (now - entry.windowStart >= RATE_LIMIT_WINDOW_MS) {
rateLimitByIp.delete(ip)
}
}
}, 60_000)
function getAllowedCorsOrigins(): string[] {
const configured = process.env.ENSEMBLE_CORS_ORIGIN?.trim()
if (!configured) return []
return configured
.split(',')
.map(origin => origin.trim())
.filter(Boolean)
}
function isAllowedOrigin(origin: string): boolean {
const configuredOrigins = getAllowedCorsOrigins()
if (configuredOrigins.length > 0) return configuredOrigins.includes(origin)
return DEFAULT_CORS_ORIGIN_PATTERNS.some(pattern => pattern.test(origin))
}
function buildCorsHeaders(origin?: string): Record<string, string> {
const headers: Record<string, string> = {
'Content-Type': 'application/json',
'Access-Control-Allow-Methods': 'GET,POST,DELETE,OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
'Vary': 'Origin',
}
if (origin && isAllowedOrigin(origin)) {
headers['Access-Control-Allow-Origin'] = origin
}
return headers
}
function json(res: http.ServerResponse, data: unknown, status = 200, origin?: string) {
res.writeHead(status, buildCorsHeaders(origin))
res.end(JSON.stringify(data))
}
async function readBody(req: http.IncomingMessage): Promise<string> {
return new Promise((resolve, reject) => {
let body = ''
req.on('data', chunk => body += chunk)
req.on('end', () => resolve(body))
req.on('error', reject)
})
}
function getClientIp(req: http.IncomingMessage): string {
const forwardedFor = req.headers['x-forwarded-for']
if (typeof forwardedFor === 'string') {
const firstIp = forwardedFor.split(',')[0]?.trim()
if (firstIp) return firstIp
}
return req.socket.remoteAddress || 'unknown'
}
function isRateLimited(ip: string): boolean {
const now = Date.now()
const current = rateLimitByIp.get(ip)
if (!current || now - current.windowStart >= RATE_LIMIT_WINDOW_MS) {
rateLimitByIp.set(ip, { count: 1, windowStart: now })
return false
}
current.count += 1
return current.count > RATE_LIMIT_MAX_REQUESTS
}
const server = http.createServer(async (req, res) => {
const url = new URL(req.url || '/', `http://localhost:${PORT}`)
const path = url.pathname
const method = req.method || 'GET'
const origin = req.headers.origin
if (origin && !isAllowedOrigin(origin)) {
return json(res, { error: 'CORS origin forbidden' }, 403, origin)
}
// CORS preflight
if (method === 'OPTIONS') {
res.writeHead(204, buildCorsHeaders(origin))
res.end()
return
}
try {
// Health check — always exempt from rate limiting
if (path === '/api/v1/health') {
return json(res, { status: 'healthy', version: '1.0.0' }, 200, origin)
}
// Internal ensemble API routes are exempt from rate limiting
const isInternalEnsembleApi = path.startsWith('/api/ensemble/')
if (!isInternalEnsembleApi && isRateLimited(getClientIp(req))) {
return json(res, { error: 'Rate limit exceeded' }, 429, origin)
}
// List teams / Create team
if (path === '/api/ensemble/teams') {
if (method === 'GET') {
const result = listEnsembleTeams()
return json(res, result.data, result.status, origin)
}
if (method === 'POST') {
let body: unknown
try {
body = JSON.parse(await readBody(req))
} catch {
return json(res, { error: 'Bad Request: malformed JSON' }, 400, origin)
}
const result = await createEnsembleTeam(body as Parameters<typeof createEnsembleTeam>[0])
if (result.error) return json(res, { error: result.error }, result.status, origin)
return json(res, result.data, result.status, origin)
}
}
// Team operations: /api/ensemble/teams/:id
const teamMatch = path.match(/^\/api\/ensemble\/teams\/([^/]+)$/)
if (teamMatch) {
const teamId = teamMatch[1]
if (method === 'GET') {
const result = getEnsembleTeam(teamId)
if (result.error) return json(res, { error: result.error }, result.status, origin)
return json(res, result.data, result.status, origin)
}
if (method === 'POST') {
let body: Record<string, unknown>
try {
body = JSON.parse(await readBody(req))
} catch {
return json(res, { error: 'Bad Request: malformed JSON' }, 400, origin)
}
const result = await sendTeamMessage(teamId, (body.to as string) || 'team', body.content as string, body.from as string, body.id as string, body.timestamp as string)
if (result.error) return json(res, { error: result.error }, result.status, origin)
return json(res, result.data, result.status, origin)
}
if (method === 'DELETE') {
const result = await disbandTeam(teamId)
if (result.error) return json(res, { error: result.error }, result.status, origin)
return json(res, result.data, result.status, origin)
}
}
// Disband: /api/ensemble/teams/:id/disband
const disbandMatch = path.match(/^\/api\/ensemble\/teams\/([^/]+)\/disband$/)
if (disbandMatch && method === 'POST') {
const result = await disbandTeam(disbandMatch[1])
if (result.error) return json(res, { error: result.error }, result.status, origin)
return json(res, result.data, result.status, origin)
}
// Feed: /api/ensemble/teams/:id/feed
const feedMatch = path.match(/^\/api\/ensemble\/teams\/([^/]+)\/feed$/)
if (feedMatch && method === 'GET') {
const since = url.searchParams.get('since') || undefined
const result = getTeamFeed(feedMatch[1], since)
if (result.error) return json(res, { error: result.error }, result.status, origin)
return json(res, result.data, result.status, origin)
}
json(res, { error: 'Not found' }, 404, origin)
} catch (err) {
console.error('[Server] Error:', err)
json(res, { error: 'Internal server error' }, 500, origin)
}
})
server.on('error', (err: NodeJS.ErrnoException) => {
if (err.code === 'EADDRINUSE') {
console.error(`[Ensemble] Port ${PORT} is already in use on ${HOST}. Stop the other process or set ENSEMBLE_PORT to a different port.`)
process.exit(1)
}
console.error('[Ensemble] Server failed to start:', err)
process.exit(1)
})
server.listen(PORT, HOST, () => {
console.log(`[Ensemble] Server running on http://${HOST}:${PORT}`)
console.log(`[Ensemble] Health: http://localhost:${PORT}/api/v1/health`)
})