-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-server.js
More file actions
100 lines (85 loc) · 2.95 KB
/
test-server.js
File metadata and controls
100 lines (85 loc) · 2.95 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
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3001;
const DESIGN_SYSTEM_DIR = __dirname;
const PUBLIC_DIR = path.join(__dirname, '..');
const MIME_TYPES = {
'.html': 'text/html; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.js': 'application/javascript; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.md': 'text/markdown; charset=utf-8',
'.svg': 'image/svg+xml',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.ico': 'image/x-icon',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
'.ttf': 'font/ttf',
'.txt': 'text/plain; charset=utf-8'
};
function isPathInside(child, parent) {
const relative = path.relative(parent, child);
return !!relative && !relative.startsWith('..') && !path.isAbsolute(relative);
}
function sendFile(res, filePath, status = 200) {
const ext = path.extname(filePath).toLowerCase();
const contentType = MIME_TYPES[ext] || 'application/octet-stream';
res.writeHead(status, {
'Content-Type': contentType,
'Cache-Control': 'no-store'
});
const read = fs.createReadStream(filePath);
read.on('error', () => {
res.writeHead(500, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('Internal Server Error');
});
read.pipe(res);
}
const server = http.createServer((req, res) => {
const urlObj = new URL(req.url, 'http://localhost');
let pathname = decodeURIComponent(urlObj.pathname || '/');
// Default to test.html
if (pathname === '/') {
pathname = '/test.html';
}
// Remove leading slash and resolve path
const relativePath = pathname.startsWith('/') ? pathname.slice(1) : pathname;
// Resolve file path - check in design-system directory first, then public directory
let fsPath = path.join(DESIGN_SYSTEM_DIR, relativePath);
// If file doesn't exist in design-system, try public directory
if (!fs.existsSync(fsPath)) {
fsPath = path.join(PUBLIC_DIR, relativePath);
}
// Prevent directory traversal
if (!isPathInside(fsPath, DESIGN_SYSTEM_DIR) && !isPathInside(fsPath, PUBLIC_DIR)) {
res.writeHead(403, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('Forbidden');
return;
}
fs.stat(fsPath, (err, stats) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('Not Found');
return;
}
if (stats.isDirectory()) {
const indexPath = path.join(fsPath, 'index.html');
if (fs.existsSync(indexPath)) {
sendFile(res, indexPath, 200);
} else {
res.writeHead(403, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('Forbidden');
}
return;
}
sendFile(res, fsPath, 200);
});
});
server.listen(PORT, () => {
console.log(`Design System Test Server running at http://localhost:${PORT}`);
console.log(`Open http://localhost:${PORT} in your browser`);
});