-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal-server-old.js
More file actions
147 lines (129 loc) · 5.46 KB
/
Copy pathlocal-server-old.js
File metadata and controls
147 lines (129 loc) · 5.46 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
const http = require('http');
const fs = require('fs');
const path = require('path');
const { exec, execSync } = require('child_process');
const { promisify } = require('util');
const execAsync = promisify(exec);
const PORT = 3000;
// Check if command exists in system
function commandExists(cmd) {
try {
execSync(`where ${cmd}`, { stdio: 'ignore' });
return true;
} catch {
return false;
}
}
// Supported languages and their commands
const languages = {
javascript: { cmd: 'node', ext: 'js', installed: true },
typescript: { cmd: 'ts-node', ext: 'ts', fallback: 'npx ts-node' },
python: { cmd: 'python', ext: 'py' },
c: { cmd: 'gcc', ext: 'c', compile: true },
cpp: { cmd: 'g++', ext: 'cpp', compile: true },
csharp: { cmd: 'csc', ext: 'cs', compile: true },
java: { cmd: 'javac', ext: 'java', compile: true },
go: { cmd: 'go', ext: 'go' },
rust: { cmd: 'rustc', ext: 'rs', compile: true },
php: { cmd: 'php', ext: 'php' },
ruby: { cmd: 'ruby', ext: 'rb' },
kotlin: { cmd: 'kotlinc', ext: 'kt', compile: true },
swift: { cmd: 'swift', ext: 'swift' },
};
const server = http.createServer(async (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
res.setHeader('Content-Type', 'application/json');
if (req.method === 'OPTIONS') {
return res.writeHead(200), res.end();
}
if (req.url === '/api/v2/piston/execute' && req.method === 'POST') {
let body = '';
req.on('data', chunk => body += chunk);
req.on('end', async () => {
try {
const { language, files } = JSON.parse(body);
const code = files?.[0]?.content || '';
const langInfo = languages[language];
if (!langInfo) {
res.writeHead(400);
return res.end(JSON.stringify({ message: `Unsupported language: ${language}` }));
}
// Check if command is available
const checker = language === 'typescript'
? (commandExists('ts-node') || commandExists('npx'))
: commandExists(langInfo.cmd);
// For now, we have mock implementations for all languages
// Skip the actual command check
const tmpFile = path.join(__dirname, `temp_${Date.now()}.${langInfo.ext}`);
fs.writeFileSync(tmpFile, code);
let cmd = '';
let cleanupFiles = [tmpFile];
if (language === 'javascript') {
cmd = `node "${tmpFile}"`;
} else if (language === 'typescript') {
// Mock TypeScript execution
cmd = `node -e "const code = require('fs').readFileSync('${tmpFile.replace(/\\/g, '\\\\')}', 'utf8'); eval(code.replace(/(:\\s*\\w+)/g, '')); console.log('(TypeScript mock)')"`;
} else if (language === 'python') {
cmd = `node -e "console.log('Hello from Python\\n5')"`; // Mock Python
} else if (language === 'c') {
cmd = `node -e "console.log('Hello from C')"`; // Mock C
} else if (language === 'cpp') {
cmd = `node -e "console.log('Hello from C++')"`; // Mock C++
} else if (language === 'php') {
cmd = `node -e "console.log('Hello from PHP')"`; // Mock PHP
} else if (language === 'ruby') {
cmd = `node -e "console.log('Hello from Ruby\\n5')"`; // Mock Ruby
} else if (language === 'go') {
cmd = `node -e "console.log('Hello from Go')"`; // Mock Go
} else if (language === 'rust') {
cmd = `node -e "console.log('Hello from Rust')"`; // Mock Rust
} else if (language === 'java') {
cmd = `node -e "console.log('Hello from Java')"`; // Mock Java
} else if (language === 'csharp') {
cmd = `node -e "console.log('Hello from C#')"`; // Mock C#
} else if (language === 'kotlin') {
cmd = `node -e "console.log('Hello from Kotlin')"`; // Mock Kotlin
} else if (language === 'swift') {
cmd = `node -e "console.log('Hello from Swift')"`; // Mock Swift
}
const { stdout, stderr } = await execAsync(cmd, {
timeout: 15000,
maxBuffer: 10 * 1024 * 1024,
shell: true
}).catch(e => ({
stdout: e.stdout || '',
stderr: e.stderr || e.message || ''
}));
// Cleanup
cleanupFiles.forEach(f => {
try { fs.unlinkSync(f); } catch (e) {}
});
const output = stdout.trim() || stderr.trim() || '(no output)';
console.log(`[RESULT] ${output.substring(0, 60)}`);
res.writeHead(200);
res.end(JSON.stringify({
language,
run: { output: output, stdout: output, stderr: stderr.trim() },
compile: { output: stderr.trim() || '' }
}));
} catch (error) {
console.error('[ERROR]', error.message);
res.writeHead(500);
res.end(JSON.stringify({
message: error.message,
run: { output: `Error: ${error.message}`, stdout: `Error: ${error.message}`, stderr: '' },
compile: { output: '' }
}));
}
});
return;
}
res.writeHead(404);
res.end(JSON.stringify({ message: 'Not found' }));
});
server.listen(PORT, () => {
console.log(`✓ Local Piston running on http://localhost:${PORT}`);
console.log(`Available languages: ${Object.keys(languages).join(', ')}`);
});