This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
336 lines (248 loc) · 6.86 KB
/
index.js
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
const { exec, execFile, execSync } = require('child_process');
const fs = require('fs');
const rimraf = require('rimraf');
const mkdirp = require('mkdirp');
const copy = require('recursive-copy');
const express = require('express');
const path = require('path');
const PORT = process.env.PORT || 5000;
let nbid = 1;
let queue = [], builders = {}, busy = false;
let projects = {};
let deprecatedFlags = {
"PROJ_HIRES":1,
"POK_ENABLE_FPSCOUNTER":1
};
let settings = `
#ifndef MY_SETTINGS_H
#define MY_SETTINGS_H
#define PROJ_BUTTONS_POLLING_ONLY 1
#define PROJ_STARTUPLOGO 1
#define PROJ_GAMEBUINO 0
#define PROJ_HIGH_RAM HIGH_RAM_MUSIC
#define PROJ_STREAMING_MUSIC 1
#define PROJ_ENABLE_SYNTH 0
#define PROJ_ENABLE_SOUND 1
#define PROJ_AUD_FREQ 8000
#define PROJ_STREAM_TO_DAC 1
#define PROJ_USE_PWM 1
#define PROJ_GBSOUND 0
#define PROJ_ENABLE_SYNTH 0
#define PROJ_FPS 40
// Python specific
#define PROJ_PYTHON_REPL 0
#define MICROPY_ENABLE_GC 1
#define USE_USB_SERIAL_PRINT (0)
$flags
#if (PROJ_SCREENMODE == TASMODE) || (PROJ_SCREENMODE == TASMODELOW)
#define TASUI
#define TASUI_CLUT 0
#define TASUI_TILE_DELTA 0
#endif
#endif
`;
function sendHeaders( res ){
res.writeHead(200, {
'Content-Type': 'text/plain'
});
}
function Builder( PROJ_ID ){
this.id = nbid++;
this.result = "";
this.state = "INIT";
builders[ this.id ] = this;
if( PROJ_ID )
projects[ PROJ_ID ] = this.id;
console.log("New builder: " + PROJ_ID);
fs.mkdirSync(__dirname + '/builds/' + this.id);
fs.mkdirSync(__dirname + '/public/builds/' + this.id);
let data = '', stdout = '', files;
let flags = {}, dirtyLib = true;
let main = null;
let retry = {};
this.addFlags = obj => {
for( let k in obj ){
let v = obj[k];
if( /^[A-Z0-9_]+$/.test(k) && /^[A-Z0-9_]+$/i.test(v) && flags[k] != v ){
console.log(`Dirty: ${flags[k]} != ${v}`);
flags[k] = v;
dirtyLib = true;
}
}
};
this.destroy = _ => {
this.destroyHND = 0;
delete projects[ PROJ_ID ];
if( builders[ this.id ] == this )
delete builders[ this.id ];
try{
rimraf( __dirname + '/builds/' + this.id, _ => {});
}catch( err ){
console.error(err);
}
try{
rimraf( __dirname + '/public/builds/' + this.id, _ => {});
}catch( err ){
console.error(err);
}
};
this.resetDestroy = _ => {
if( this.destroyHND )
clearTimeout( this.destroyHND );
this.destroyHND = setTimeout( this.destroy, 5 * 60 * 1000 );
};
this.resetDestroy();
this.addData = _data => {
data += _data;
if( data.length > 3 * 1024 * 1024 ){
data = '';
return false;
}
this.resetDestroy();
return true;
};
this.start = _ => {
try{
files = JSON.parse(data);
}catch( ex ){
data = '';
this.result = ex.toString();
this.state = "DONE";
return;
}
data = '';
this.resetDestroy();
queue.push( this );
this.state = "QUEUED";
};
this.run = _ => {
if( this.state != "QUEUED" ) return;
this.resetDestroy();
this.state = "BUILDING";
busy = true;
let strflags = '';
for( let k in flags ){
if(k in deprecatedFlags)
continue;
strflags += `
#if defined(${k})
#undef ${k}
#endif
`;
if( flags[k] != "undefined" )
strflags += `
#define ${k} ${flags[k]}
`;
}
files["My_settings.h"] = settings.replace("$flags", strflags);
let fileList = Object.keys( files );
this.pop( fileList );
};
this.pop = fileList => {
if( !fileList.length )
return this.compile();
let file = fileList.shift().replace(/\\/g, '/'); // convert \ to /
let fullPath = __dirname + '/builds/' + this.id + '/' + file.replace(/\/\.+\//g, '/'); // remove shenanigans
let parts = fullPath.split('/');
parts.pop();
if( parts.length ){
mkdirp( parts.join('/'), e => writeFile.call( this ) );
}else
writeFile.call(this);
function writeFile(){
let str = files[file];
let buf = Buffer.from(str, file == "My_settings.h" ? 'utf-8' : 'base64');
fs.writeFile( fullPath, buf, e => {
if( e ){
this.state = 'DONE';
busy = false;
this.result = "ERROR: " + file + " - " + e.toString();
return;
}
this.pop(fileList);
});
}
};
this.compile = _ => {
try{
const child = exec(
[
__dirname + '/build.sh',
this.id,
dirtyLib|0
].join(" "),
(error, _stdout, stderr) => {
stdout += _stdout;
if( error ){
busy = false;
this.state = "DONE";
this.result = "ERROR: " + error + " " + stderr + stdout;
}else{
dirtyLib = false;
busy = false;
this.state = "DONE";
this.result = 'DONE';
}
});
}catch( ex ){
console.error(ex);
this.result = "ERROR: " + ex;
this.state = "DONE";
busy = false;
}
};
}
setInterval( _ => {
if( !queue.length || busy )
return;
let next = queue.shift();
next.run();
}, 1000 );
execSync("chmod +x -R " + __dirname + "/gcc-arm-none-eabi/");
execSync("chmod +x " + __dirname + "/build.sh");
express()
.get('/*',function(req,res,next){
res.header('Access-Control-Allow-Origin', '*');
next();
})
.post('/*',function(req,res,next){
res.header('Access-Control-Allow-Origin', '*');
next();
})
.use(express.static(path.join(__dirname, 'public')))
.set('views', path.join(__dirname, 'views'))
.set('view engine', 'ejs')
.get('/', (req, res) => res.render('pages/index'))
.get('/blockly', (req, res) => res.render('pages/blockly'))
.get('/emulator', (req, res) => res.render('pages/emulator'))
.get('/poll', (req, res) => {
let builder = builders[ req.query.id ];
sendHeaders( res );
if( builder ){
if( builder.state !== "DONE" ){
builder.resetDestroy();
res.end( builder.state );
}else
res.end( builder.result );
}else
res.end( "DESTROYED" );
})
.post('/build', (req, res) => {
let builder;
if( req.query.PROJ_ID && projects[req.query.PROJ_ID] )
builder = builders[ projects[req.query.PROJ_ID] ];
console.log("Build request: " + req.query.PROJ_ID + ", " + projects[req.query.PROJ_ID] );
if( !builder )
builder = new Builder( req.query.PROJ_ID );
builder.addFlags( req.query );
req.on('data', function (data) {
if( !builder.addData( data ) )
req.connection.destroy();
});
req.on('end', function () {
builder.start();
sendHeaders( res );
res.end( builder.id.toString() );
});
})
.listen(PORT, () => console.log(`Listening on ${ PORT }`))