/
index.js
69 lines (62 loc) · 2.2 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
var fs = require('fs');
var path = require('path');
var verify = require('adventure-verify');
var spawn = require('child_process').spawn;
var http = require('http')
var wsock = require('websocket-stream')
var browserify = require('browserify')
var split = require('split');
var concat = require('concat-stream')
var through = require('through2');
exports.problem = fs.createReadStream(path.join(__dirname, 'problem.txt'));
exports.solution = function () {
var r = fs.createReadStream(path.join(__dirname, 'solution.js'));
r.once('end', function () {
setTimeout(function () { process.exit(0) }, 500);
});
return r;
};
exports.verify = verify({ modeReset: true }, function (args, t) {
t.plan(1);
var server = createServer(path.resolve(args[0]));
t.once('end', function () {
server.close();
wss.close();
});
var wss = wsock.createServer({ server: server }, handle);
function handle (stream) {
stream.pipe(split()).pipe(through(function (buf, enc, next) {
t.equal(buf.toString(), 'hello');
stream.end();
}));
}
});
exports.run = function (args) {
var server = createServer(path.resolve(args[0]));
var wss = wsock.createServer({ server: server }, handle);
function handle (stream) {
stream.pipe(process.stdout);
}
};
function createServer (main) {
var server = http.createServer(function (req, res) {
if (req.url === '/bundle.js') {
res.setHeader('content-type', 'text/javascript');
var b = browserify(main, { debug: true });
b.bundle().pipe(res);
}
else {
res.setHeader('content-type', 'text/html')
return res.end('<script src="/bundle.js"></script>')
}
});
server.listen(8099, function () {
console.log('################################################');
console.log('# #');
console.log('# Open http://localhost:8099 to run your code! #');
console.log('# #');
console.log('################################################');
console.log();
});
return server;
}