This repository has been archived by the owner on Jun 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
server-cli.js
134 lines (120 loc) · 4.64 KB
/
server-cli.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
var http = require("http");
var path = require("path");
var finalhandler = require("finalhandler");
var serveStatic = require("serve-static");
var testServer = require("./middleware");
var cli = require("buster-cli");
var DOCUMENT_ROOT = path.join(__dirname, "../public");
function ServerCli(cli, logger, options) {
this.cli = cli;
this.logger = logger;
this.documentRoot = options.documentRoot || DOCUMENT_ROOT;
this.templateRoot = options.templateRoot;
this.name = options.name;
this.binary = options.binary;
this.unexpectedErrorMessage = options.unexpectedErrorMessage;
this.phantom = options.phantom || require("phantom-proxy");
}
module.exports = ServerCli.prototype = {
create: function (stdout, stderr, options) {
var c = cli.create(options);
c.addHelpOption(options.missionStatement, options.description);
return new ServerCli(c, c.createLogger(stdout, stderr), options || {});
},
run: function (args, callback) {
callback = callback || function () {};
this.loadOptions();
this.cli.parseArgs(args, function (err, options) {
if (this.cli.loggedHelp) { return callback(); }
try {
var port = options["--port"].value;
var binding = options["--binding"].value;
var server = this.createServer(port, binding, function (err) {
if (err) {
this.handleError(err);
return callback(err);
}
var serverUrl = "http://localhost:" + port;
this.logger.log(this.binary + " running on " + serverUrl);
if (options['--capture-headless'].isSet) {
this.captureHeadlessBrowser(serverUrl, function () {
callback(null, server);
}.bind(this));
} else {
callback(null, server);
}
}.bind(this));
} catch (e) {
this.handleError(e);
callback(e);
}
}.bind(this));
},
createServer: function (port, binding, callback) {
var middleware, documentRoot = this.documentRoot;
var serve = serveStatic(documentRoot, {
maxAge: 300*1000
});
var httpServer = http.createServer(function (req, res) {
if (middleware.respond(req, res)) {
return;
}
serve(req, res, finalhandler(req, res));
});
middleware = testServer.create(httpServer, {
logger: this.logger,
templateRoot: this.templateRoot,
name: this.name
});
httpServer.on("error", this.handleError.bind(this));
httpServer.listen(port, binding, callback);
return httpServer;
},
captureHeadlessBrowser: function (serverUrl, sessionId, cb) {
this.logger.log('Starting headless browser...');
if (cb === undefined && typeof sessionId === 'function') {
cb = sessionId;
sessionId = undefined;
}
var url = serverUrl + '/capture';
if (sessionId !== undefined) {
url += '?id=' + sessionId;
}
this.phantom.create(function (proxy) {
proxy.page.open(url, function (success) {
if (success) {
this.logger.log('Headless browser was captured.');
} else {
this.logger.log(
'Headless browser was not captured. Something went wrong :-('
);
}
cb && cb();
}.bind(this));
}.bind(this));
},
loadOptions: function () {
this.cli.opt(["-p", "--port"], {
description: "The port to run the server on",
defaultValue: 1111,
validators: [this.cli.validators.integer()],
transform: function (value) { return parseInt(value, 10); }
});
this.cli.opt(["-b", "--binding"], {
description: "The address to bind the server to",
hasValue: true
});
this.cli.opt(['-c', '--capture-headless'], {
description: "Captures a headless webkit browser " +
"after the server was started."
});
},
handleError: function (err) {
if (/EADDRINUSE/.test(err.message)) {
this.cli.err("Address already in use. Pick another " +
"port with -p/--port to start " + this.binary);
} else {
this.cli.err(this.unexpectedErrorMessage + err.stack);
}
}
};