-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
658 lines (658 loc) · 18.3 KB
/
Copy pathserver.js
File metadata and controls
658 lines (658 loc) · 18.3 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/**
* server.js (drop-in replacement)
* - Keeps existing behavior: static dist hosting (prod), demo API, events/progress logging, optional WS
* - Adds /api/proxy (GET + POST/PUT/PATCH/DELETE) to allow browser UI to reach sidecars safely
*
* Usage from frontend:
* POST /api/proxy
* {
* "url": "http://localhost:3001/users",
* "method": "GET",
* "headers": {"Authorization":"Bearer ..."},
* "body": "{\"x\":1}" // or object, or null
* }
*
* Or simple GET proxy:
* GET /api/proxy?url=http%3A%2F%2Flocalhost%3A3001%2Fusers
*/
const http = require('http');
const fs = require('fs');
const fsp = fs.promises;
const path = require('path');
const url = require('url');
// Try to load WebSocket module, fallback if not available
let WebSocket = null;
let isWebSocketAvailable = false;
try {
WebSocket = require('ws');
isWebSocketAvailable = true;
console.log('WebSocket support enabled');
} catch (error) {
console.log('WebSocket support disabled (ws package not installed)');
console.log('Install with: npm install ws');
}
const DIST_DIR = path.join(__dirname, 'dist');
const DATA_DIR = path.join(__dirname, '.api-sim-data');
const EVENTS_FILE = path.join(DATA_DIR, 'events.jsonl');
const PROGRESS_FILE = path.join(DATA_DIR, 'progress.json');
const isProduction = process.env.IS_PRODUCTION === 'true';
if (isProduction && !fs.existsSync(DIST_DIR)) {
throw new Error(`Production mode enabled but dist directory does not exist: ${DIST_DIR}`);
}
// Force port 3000 in production, otherwise use PORT env or default to 3000
const PORT = isProduction ? 3000 : (process.env.PORT || 3000);
if (!fs.existsSync(DATA_DIR)) {
fs.mkdirSync(DATA_DIR, { recursive: true });
}
// Track connected WebSocket clients
const wsClients = new Set();
// Demo API state
const demoUsers = [];
let nextDemoUserId = 1;
// MIME types
const mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
'.ttf': 'font/ttf',
'.eot': 'application/vnd.ms-fontobject'
};
function getMimeType(filePath) {
const ext = path.extname(filePath).toLowerCase();
return mimeTypes[ext] || 'text/plain';
}
function serveFile(filePath, res) {
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('File not found');
return;
}
const mimeType = getMimeType(filePath);
res.writeHead(200, { 'Content-Type': mimeType });
res.end(data);
});
}
function sendJson(res, statusCode, payload) {
res.writeHead(statusCode, { 'Content-Type': 'application/json' });
if (statusCode === 204) {
res.end();
return;
}
res.end(JSON.stringify(payload));
}
function readRequestBody(req) {
return new Promise((resolve, reject) => {
let body = '';
req.on('data', (chunk) => {
body += chunk.toString();
if (body.length > 5 * 1024 * 1024) {
req.destroy();
reject(new Error('Payload too large'));
}
});
req.on('end', () => resolve(body));
req.on('error', reject);
});
}
/**
* =========================
* WebSocket message endpoint
* =========================
*/
async function handleMessagePost(req, res) {
try {
const body = await readRequestBody(req);
const data = JSON.parse(body);
const message = data.message;
if (!message) {
sendJson(res, 400, { error: 'Message is required' });
return;
}
if (!isWebSocketAvailable) {
sendJson(res, 503, {
error: 'WebSocket functionality not available',
details: 'Install the ws package with: npm install ws'
});
return;
}
wsClients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({ type: 'message', message }));
}
});
sendJson(res, 200, { success: true, clientCount: wsClients.size });
} catch (error) {
sendJson(res, 400, { error: 'Invalid JSON' });
}
}
/**
* ==========
* Event logs
* ==========
*/
function isValidEvent(event) {
const eventType = typeof event.eventType === 'string' ? event.eventType : event.type;
return (
event &&
typeof event === 'object' &&
typeof eventType === 'string' &&
typeof event.timestamp === 'string' &&
typeof event.sessionId === 'string' &&
typeof event.taskId === 'string'
);
}
async function appendEvent(event) {
await fsp.appendFile(EVENTS_FILE, `${JSON.stringify(event)}\n`, 'utf-8');
}
async function handleEventsPost(req, res) {
try {
const body = await readRequestBody(req);
const event = JSON.parse(body);
if (!isValidEvent(event)) {
sendJson(res, 400, { error: 'Invalid event payload' });
return;
}
if (typeof event.eventType !== 'string' && typeof event.type === 'string') {
event.eventType = event.type;
delete event.type;
}
await appendEvent(event);
sendJson(res, 204, {});
} catch (error) {
sendJson(res, 400, { error: 'Invalid JSON' });
}
}
async function readEvents(limit) {
try {
const data = await fsp.readFile(EVENTS_FILE, 'utf-8');
const lines = data.split('\n').filter(Boolean);
const slice = lines.slice(-limit);
return slice
.map((line) => {
try {
return JSON.parse(line);
} catch (_) {
return null;
}
})
.filter(Boolean);
} catch (error) {
if (error.code === 'ENOENT') return [];
throw error;
}
}
async function handleEventsGet(req, res, parsedUrl) {
const limitParam = parsedUrl.query.limit;
const limit = Math.min(Math.max(parseInt(limitParam, 10) || 50, 1), 500);
try {
const events = await readEvents(limit);
sendJson(res, 200, events);
} catch (error) {
console.error('Failed to read events', error);
sendJson(res, 500, { error: 'Failed to read events' });
}
}
/**
* =========
* Progress
* =========
*/
function isValidProgress(progress) {
return (
progress &&
typeof progress === 'object' &&
typeof progress.taskId === 'string' &&
typeof progress.sessionId === 'string' &&
Array.isArray(progress.completedStepIds) &&
(progress.lastSelectedStepId === null || typeof progress.lastSelectedStepId === 'string') &&
typeof progress.updatedAt === 'string'
);
}
async function writeProgress(progress) {
const tempFile = `${PROGRESS_FILE}.tmp`;
await fsp.writeFile(tempFile, JSON.stringify(progress, null, 2), 'utf-8');
await fsp.rename(tempFile, PROGRESS_FILE);
}
async function handleProgressPost(req, res) {
try {
const body = await readRequestBody(req);
const progress = JSON.parse(body);
if (!isValidProgress(progress)) {
sendJson(res, 400, { error: 'Invalid progress payload' });
return;
}
await writeProgress(progress);
sendJson(res, 204, {});
} catch (error) {
sendJson(res, 400, { error: 'Invalid JSON' });
}
}
async function handleProgressGet(res) {
try {
const data = await fsp.readFile(PROGRESS_FILE, 'utf-8');
sendJson(res, 200, JSON.parse(data));
} catch (error) {
if (error.code === 'ENOENT') {
sendJson(res, 404, { error: 'Progress not found' });
return;
}
console.error('Failed to read progress file', error);
sendJson(res, 500, { error: 'Failed to read progress' });
}
}
async function handleReset(res) {
try {
await Promise.all([
fsp.rm(EVENTS_FILE, { force: true }),
fsp.rm(PROGRESS_FILE, { force: true })
]);
sendJson(res, 204, {});
} catch (error) {
sendJson(res, 500, { error: 'Failed to reset data' });
}
}
/**
* ===========================
* Sidecar proxy (/api/proxy)
* ===========================
*
* Why this exists:
* - Browser in CodeSignal preview cannot reach container localhost/sidecars directly.
* - This endpoint runs inside the container, so it CAN reach sidecars (localhost:3001 or service-name:3001).
*
* Security:
* - Only configured local sidecar hosts and ports are allowed.
* - Override the defaults with PROXY_ALLOWED_HOSTS / PROXY_ALLOWED_PORTS.
*/
const DEFAULT_ALLOWED_HOSTS = [
'localhost',
'127.0.0.1',
'::1',
'[::1]',
'reading-tracker'
];
const DEFAULT_ALLOWED_PORTS = ['3001', '3003', '4000', '5432', '5000', '8000', '8080'];
function readCsvSetFromEnv(name, defaults) {
const raw = process.env[name];
if (!raw) return new Set(defaults);
return new Set(
raw
.split(',')
.map((item) => item.trim())
.filter(Boolean)
);
}
const ALLOWED_PROXY_HOSTS = readCsvSetFromEnv('PROXY_ALLOWED_HOSTS', DEFAULT_ALLOWED_HOSTS);
const ALLOWED_PROXY_PORTS = readCsvSetFromEnv('PROXY_ALLOWED_PORTS', DEFAULT_ALLOWED_PORTS);
function normalizeHeaders(input) {
const out = {};
if (!input || typeof input !== 'object') return out;
for (const [k, v] of Object.entries(input)) {
if (typeof k !== 'string') continue;
// ignore undefined/null
if (v === undefined || v === null) continue;
const lowerKey = k.toLowerCase();
if (lowerKey === 'host' || lowerKey === 'content-length') continue;
out[k] = String(v);
}
return out;
}
function shouldAllowTarget(targetUrl) {
try {
const u = new URL(targetUrl);
const hostOk = ALLOWED_PROXY_HOSTS.has(u.hostname);
const port = u.port || (u.protocol === 'https:' ? '443' : '80');
const portOk = ALLOWED_PROXY_PORTS.has(port);
// only http(s)
const protoOk = u.protocol === 'http:' || u.protocol === 'https:';
return protoOk && hostOk && portOk;
} catch (_) {
return false;
}
}
function getProxyAllowlistDetails() {
return {
allowedHosts: Array.from(ALLOWED_PROXY_HOSTS).sort(),
allowedPorts: Array.from(ALLOWED_PROXY_PORTS).sort(),
allowedProtocols: ['http:', 'https:']
};
}
async function handleProxy(req, res, parsedUrl) {
try {
let targetUrl = '';
let method = req.method || 'GET';
let headers = {};
let body = null;
if (req.method === 'GET') {
// GET /api/proxy?url=...
const q = parsedUrl.query.url;
if (typeof q !== 'string' || !q) {
sendJson(res, 400, { error: 'Missing url query param' });
return;
}
targetUrl = q;
} else {
// POST/PUT/PATCH/DELETE /api/proxy
const bodyText = await readRequestBody(req);
const payload = JSON.parse(bodyText || '{}');
targetUrl = payload.url;
method = (payload.method || req.method || 'GET').toUpperCase();
headers = normalizeHeaders(payload.headers);
body = payload.body ?? null;
}
if (typeof targetUrl !== 'string' || !targetUrl) {
sendJson(res, 400, { error: 'url is required' });
return;
}
targetUrl = targetUrl.trim();
if (!targetUrl) {
sendJson(res, 400, { error: 'url is required' });
return;
}
if (!shouldAllowTarget(targetUrl)) {
sendJson(res, 403, {
error: 'Target not allowed',
details: 'Only configured local sidecar hosts and ports are allowed.',
target: targetUrl,
allowlist: getProxyAllowlistDetails()
});
return;
}
// Ensure method is one of supported ones
const allowedMethods = new Set(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS']);
if (!allowedMethods.has(method)) {
sendJson(res, 400, { error: `Unsupported method: ${method}` });
return;
}
// Prepare body
let outgoingBody = undefined;
if (method !== 'GET' && method !== 'HEAD' && body != null) {
if (typeof body === 'string') {
outgoingBody = body;
} else {
// If body is object/array, send JSON
outgoingBody = JSON.stringify(body);
if (!headers['Content-Type'] && !headers['content-type']) {
headers['Content-Type'] = 'application/json';
}
}
}
// Timeout to avoid hanging
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 12000);
const resp = await fetch(targetUrl, {
method,
headers,
body: outgoingBody,
signal: controller.signal
}).finally(() => clearTimeout(timeout));
const respContentType = resp.headers.get('content-type') || 'text/plain';
const respText = await resp.text();
// Forward response as-is
res.writeHead(resp.status, {
'Content-Type': respContentType,
// helpful for debugging
'X-Proxy-Target': targetUrl
});
res.end(respText);
} catch (e) {
sendJson(res, 500, { error: 'Proxy failed', details: String(e && e.message ? e.message : e) });
}
}
/**
* ============
* API routing
* ============
*/
function routeApiRequest(req, res, parsedUrl) {
// proxy supports GET and POST/PUT/PATCH/DELETE (all via /api/proxy)
if (parsedUrl.pathname === '/api/proxy') {
void handleProxy(req, res, parsedUrl);
return true;
}
if (req.method === 'GET') {
if (parsedUrl.pathname === '/api/events') {
void handleEventsGet(req, res, parsedUrl);
return true;
}
if (parsedUrl.pathname === '/api/progress') {
void handleProgressGet(res);
return true;
}
} else if (req.method === 'POST') {
if (parsedUrl.pathname === '/api/events') {
void handleEventsPost(req, res);
return true;
}
if (parsedUrl.pathname === '/api/progress') {
void handleProgressPost(req, res);
return true;
}
if (!isProduction && parsedUrl.pathname === '/api/reset') {
void handleReset(res);
return true;
}
}
return false;
}
/**
* ==========
* Demo API
* ==========
*/
async function routeDemoRequest(req, res, parsedUrl) {
const pathname = parsedUrl.pathname;
const isDemoApi = pathname.startsWith('/demo-api');
const isDemo = pathname.startsWith('/demo');
if (!isDemoApi && !isDemo) {
return false;
}
const basePath = isDemoApi ? '/demo-api' : '/demo';
const relativePath = pathname.startsWith(basePath) ? pathname.slice(basePath.length) || '/' : '/';
if (req.method === 'GET' && relativePath === '/health') {
sendJson(res, 200, { ok: true, time: new Date().toISOString() });
return true;
}
if (req.method === 'GET' && relativePath === '/users') {
sendJson(res, 200, demoUsers);
return true;
}
const userMatch = relativePath.match(/^\/users\/(\d+)$/);
if (req.method === 'GET' && userMatch) {
const userId = Number(userMatch[1]);
const user = demoUsers.find((item) => item.id === userId);
if (!user) {
sendJson(res, 404, { error: 'User not found' });
return true;
}
sendJson(res, 200, user);
return true;
}
if (req.method === 'POST' && relativePath === '/users') {
try {
const body = await readRequestBody(req);
const data = JSON.parse(body);
if (typeof data.name !== 'string' || data.name.trim().length === 0) {
sendJson(res, 400, { error: 'Name is required' });
return true;
}
const user = { id: nextDemoUserId++, name: data.name.trim() };
demoUsers.push(user);
sendJson(res, 201, user);
return true;
} catch (error) {
sendJson(res, 400, { error: 'Invalid JSON' });
return true;
}
}
if ((req.method === 'PUT' || req.method === 'PATCH') && userMatch) {
try {
const userId = Number(userMatch[1]);
const body = await readRequestBody(req);
const data = JSON.parse(body);
if (typeof data.name !== 'string' || data.name.trim().length === 0) {
sendJson(res, 400, { error: 'Name is required' });
return true;
}
const index = demoUsers.findIndex((item) => item.id === userId);
if (index < 0) {
sendJson(res, 404, { error: 'User not found' });
return true;
}
demoUsers[index] = { ...demoUsers[index], name: data.name.trim() };
sendJson(res, 200, demoUsers[index]);
return true;
} catch (error) {
sendJson(res, 400, { error: 'Invalid JSON' });
return true;
}
}
if (req.method === 'DELETE' && userMatch) {
const userId = Number(userMatch[1]);
const index = demoUsers.findIndex((item) => item.id === userId);
if (index < 0) {
sendJson(res, 404, { error: 'User not found' });
return true;
}
demoUsers.splice(index, 1);
sendJson(res, 200, { deleted: true });
return true;
}
sendJson(res, 404, { error: 'Demo endpoint not found' });
return true;
}
function handlePostRequest(req, res, parsedUrl) {
if (parsedUrl.pathname === '/message') {
void handleMessagePost(req, res);
} else if (parsedUrl.pathname.startsWith('/api/')) {
if (!routeApiRequest(req, res, parsedUrl)) {
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Not found' }));
}
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not found');
}
}
/**
* ============
* HTTP server
* ============
*/
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
const pathName = parsedUrl.pathname === '/' ? '/index.html' : parsedUrl.pathname;
// Basic CORS (for your own /api/* endpoints)
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PUT,PATCH,DELETE,OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
if (req.method === 'OPTIONS') {
res.writeHead(204);
res.end();
return;
}
// /api/*
if (parsedUrl.pathname.startsWith('/api/')) {
if (!routeApiRequest(req, res, parsedUrl)) {
sendJson(res, 404, { error: 'Not found' });
}
return;
}
// /demo-api/* or /demo/*
if (parsedUrl.pathname.startsWith('/demo-api') || parsedUrl.pathname.startsWith('/demo')) {
void routeDemoRequest(req, res, parsedUrl);
return;
}
// POST /message and legacy handler
if (req.method === 'POST') {
handlePostRequest(req, res, parsedUrl);
return;
}
// Production: serve static dist
if (isProduction) {
// Strip leading slashes so path.join/resolve can't ignore DIST_DIR
const filePath = path.join(DIST_DIR, pathName.replace(/^\/+/, ''));
// Security check - prevent directory traversal
const resolvedDistDir = path.resolve(DIST_DIR);
const resolvedFilePath = path.resolve(filePath);
const relativePath = path.relative(resolvedDistDir, resolvedFilePath);
if (relativePath.startsWith('..')) {
res.writeHead(403, { 'Content-Type': 'text/plain' });
res.end('Forbidden');
return;
}
serveFile(filePath, res);
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not found (development mode - use Vite dev server `npm run start:dev`)');
}
});
/**
* =============
* WebSocket /ws
* =============
*/
if (isWebSocketAvailable) {
const wss = new WebSocket.Server({
server,
path: '/ws'
});
wss.on('connection', (ws) => {
console.log('New WebSocket client connected');
wsClients.add(ws);
ws.on('close', () => {
console.log('WebSocket client disconnected');
wsClients.delete(ws);
});
ws.on('error', (error) => {
console.error('WebSocket error:', error);
wsClients.delete(ws);
});
});
}
/**
* ==========
* Startup
* ==========
*/
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
if (isProduction) {
console.log(`Serving static files from: ${DIST_DIR}`);
} else {
console.log(`Development mode - static files served by Vite`);
}
if (isWebSocketAvailable) {
console.log(`WebSocket server running on /ws`);
} else {
console.log(`WebSocket functionality disabled - install 'ws' package to enable`);
}
console.log('Press Ctrl+C to stop the server');
});
// Handle server errors
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.error(`Port ${PORT} is already in use. Please try a different port.`);
} else {
console.error('Server error:', err);
}
process.exit(1);
});
// Graceful shutdown
process.on('SIGINT', () => {
console.log('\nShutting down server...');
server.close(() => {
console.log('Server closed');
process.exit(0);
});
});