-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·129 lines (104 loc) · 3.35 KB
/
Copy pathserver.js
File metadata and controls
executable file
·129 lines (104 loc) · 3.35 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
#!/usr/bin/env node
var express = require('express'),
app = express(),
bodyParser = require('body-parser'),
fs = require('fs'),
sprintf = require('sprintf'),
_ = require('lodash'),
Nightmare = require('nightmare'),
nightmare = new Nightmare({show: false}),
Driver = require('./driver').Driver;
app.use(bodyParser.text({type: "*/*"}));
var queue = [];
// This must be limited to 1 (or boolean) as long as this process is limited to 1 nightmare/electron.
var dispatching = false;
var requestId = 0;
app.get('/*', handleExpressReq);
app.post('/*', handleExpressReq);
app.listen(3000, function () {
console.log('server.js listening on port 3000!');
});
function handleExpressReq(req, res) {
queue.push({req: req, res: res});
dispatchNext();
}
function dispatchNext() {
// TODO promises will probably help coalesce callbacks and exception handling
try {
if (dispatching) {
console.log('I am too busy. Enqueued request. Queue:', queue.length, 'Dispatching:', dispatching);
return;
}
if (!queue.length) {
console.log('No more requests queued up. Queue:', queue.length, 'Dispatching:', dispatching);
return;
}
var requestTuple = queue.shift(),
req = requestTuple.req,
res = requestTuple.res,
reqId = ++requestId;
dispatching = true;
console.log(reqId, 'Dispatching request', req.path, '. Queue:', queue.length, 'Dispatching:', dispatching);
if (req.query.template) {
fs.readFile('templates/' + req.query.template + '.js', 'utf8', function (err, template) {
if (err) {
// Presumably template not found.
errorResponse(400, err);
}
try {
var fnStr = sprintf(template, req.query);
} catch (e) {
console.log(e);
if (_.indexOf(e, 'does not exist')) {
// Presumably missing formatter args.
errorResponse(400, e);
} else {
errorResponse(500, e);
}
}
return driveRequest(fnStr);
});
} else {
return driveRequest(req.body);
}
} catch (e) {
errorResponse(500, e);
}
function driveRequest(fnStr) {
try {
var userScript = eval(fnStr);
if (!_.isFunction(userScript)) {
return errorResponse(400, 'Request body is not a function. Should follow the form (function(driver|nightmare){ /* ... */ })')
}
var script;
if (req.path.indexOf('/driver') == 0) {
script = userScript;
} else {
// Wrap it up as a raw nightmare script
script = (function (driver) {
userScript(driver.nightmare());
});
}
new Driver(reqId, nightmare)
.reset()
.runDriverScript(script)
.finish(function (status, result) {
res.status(status).send(result);
res.end();
dispatching = false;
console.log(reqId, 'Finished positive response. Queue:', queue.length, 'Dispatching:', dispatching);
dispatchNext();
});
} catch (e) {
errorResponse(400, e);
}
}
function errorResponse(status, error) {
console.error(error);
res.status(status).send(error.toString());
res.end();
dispatching = false;
console.log(reqId, 'Finished error response. Queue:', queue.length, 'Dispatching:', dispatching);
dispatchNext();
}
}