-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
100 lines (79 loc) · 2.49 KB
/
server.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
var connect = require('connect')
, http = require('http')
, fs = require('fs')
var server;
// URL jobID regex
var regex = /^\/(.*?)\//; //Smallest possible first url segment;
module.exports = {
start: function(opts, results, cb){
var app = connect()
, testfileregex = new RegExp(opts.testfile.replace(opts.path, ""));
app.use(connect.bodyParser());
if (opts.useID){
app.use(function(req, res, next){
req.origUrl = req.url;
var m = regex.exec(req.url)
req.jobID = m && m[1];
if (req.jobID){
req.url = req.url.replace(regex, '/')
}
next();
})
}
if (opts.middleware){
if (opts.middleware.length){
opts.middleware.forEach(function(m){
app.use(m)
})
} else {
app.use(opts.middleware);
}
}
app.use(function(req, res, next){
if (req.url == '/strider-progress'){
var data = JSON.parse(req.body.data)
, m = regex.exec(data.url)
data.id = m ? m[1] : undefined;
if (opts.progressCB){
opts.progressCB(null, data);
}
return res.end("Progress..");
} else if (req.url == '/strider-report'){
var data = JSON.parse(req.body.data)
, m = regex.exec(data.url)
data.id = m ? m[1] : undefined;
if (data.tracebacks) {
for (var i = 0; i<data.tracebacks.length; i++){
data.tracebacks[i] = JSON.parse(data.tracebacks[i])
}
}
results(data);
return res.end("Results received")
} else if (testfileregex.test(req.url)){
var f = fs.readFileSync(opts.testfile, 'utf8')
// Replace body close tag with script, body
f = f.replace(/(.*)<\/body>/,
"$1<script type='text/javascript'>" +
fs.readFileSync(__dirname + "/json2.js", "utf8") +
fs.readFileSync(__dirname + "/qunit-plugin.js", "utf8") +
"</script></body>");
return res.end(f);
}
if (/\.php$/.test(req.url)){
console.log("PHP: " + req.url);
}
return next();
});
app.use(connect.static(opts.testdir))
server = http.createServer(app)
server.listen(opts.port);
cb();
}
, open: function(){return !!server}
, close: function(){
if(server){
server.close();
server = null;
}
}
}