weinre.js
6.12 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
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
// Generated by CoffeeScript 1.8.0
var HttpChannelHandler, Version, channelManager, checkForDeath, checkHost, deathTimeout, dns, dumpingHandler, express, fs, getStaticWebDir, getVersion, jsonBodyParser, net, path, processOptions, run2, serviceManager, startDeathWatcher, startServer, utils, _;
fs = require('fs');
net = require('net');
dns = require('dns');
path = require('path');
_ = require('underscore');
express = require('express');
utils = require('./utils');
jsonBodyParser = require('./jsonBodyParser');
HttpChannelHandler = require('./HttpChannelHandler');
dumpingHandler = require('./dumpingHandler');
channelManager = require('./channelManager');
serviceManager = require('./serviceManager');
exports.run = function(options) {
return processOptions(options, run2);
};
run2 = function() {
var options;
options = utils.options;
serviceManager.registerProxyClass('WeinreClientEvents');
serviceManager.registerProxyClass('WeinreTargetEvents');
serviceManager.registerLocalClass('WeinreClientCommands');
serviceManager.registerLocalClass('WeinreTargetCommands');
startDeathWatcher(options.deathTimeout);
return startServer();
};
processOptions = function(options, cb) {
var name, nameLen, names, reducer, _i, _len;
options.httpPort = utils.ensureInteger(options.httpPort, 'the value of the option httpPort is not a number');
options.boundHost = utils.ensureString(options.boundHost, 'the value of the option boundHost is not a string');
options.verbose = utils.ensureBoolean(options.verbose, 'the value of the option verbose is not a boolean');
options.debug = utils.ensureBoolean(options.debug, 'the value of the option debug is not a boolean');
options.readTimeout = utils.ensureInteger(options.readTimeout, 'the value of the option readTimeout is not a number');
options.deathTimeout = utils.ensureInteger(options.deathTimeout, 'the value of the option deathTimeout is not a number');
if (options.debug) {
options.verbose = true;
}
options.staticWebDir = getStaticWebDir();
utils.logVerbose("pid: " + process.pid);
utils.logVerbose("version: " + (getVersion()));
utils.logVerbose("node versions:");
names = _.keys(process.versions);
reducer = function(memo, name) {
return Math.max(memo, name.length);
};
nameLen = _.reduce(names, reducer, 0);
for (_i = 0, _len = names.length; _i < _len; _i++) {
name = names[_i];
utils.logVerbose(" " + (utils.alignLeft(name, nameLen)) + ": " + process.versions[name]);
}
utils.logVerbose("options:");
utils.logVerbose(" httpPort: " + options.httpPort);
utils.logVerbose(" boundHost: " + options.boundHost);
utils.logVerbose(" verbose: " + options.verbose);
utils.logVerbose(" debug: " + options.debug);
utils.logVerbose(" readTimeout: " + options.readTimeout);
utils.logVerbose(" deathTimeout: " + options.deathTimeout);
utils.setOptions(options);
return checkHost(options.boundHost, function(err) {
if (err) {
utils.exit("unable to resolve boundHost address: " + options.boundHost);
}
return cb();
});
};
checkHost = function(hostName, cb) {
if (hostName === '-all-') {
return cb();
}
if (hostName === 'localhost') {
return cb();
}
if (net.isIP(hostName)) {
return cb();
}
return dns.lookup(hostName, cb);
};
deathTimeout = null;
startDeathWatcher = function(timeout) {
deathTimeout = utils.options.deathTimeout * 1000;
return setInterval(checkForDeath, 1000);
};
checkForDeath = function() {
var channel, now, _i, _len, _ref, _results;
now = (new Date).valueOf();
_ref = channelManager.getChannels();
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
channel = _ref[_i];
if (now - channel.lastRead > deathTimeout) {
_results.push(channel.close());
} else {
_results.push(void 0);
}
}
return _results;
};
startServer = function() {
var app, clientHandler, favIcon, options, staticCacheOptions, targetHandler;
options = utils.options;
clientHandler = new HttpChannelHandler('/ws/client');
targetHandler = new HttpChannelHandler('/ws/target');
channelManager.initialize();
favIcon = "" + options.staticWebDir + "/images/weinre-icon-32x32.png";
staticCacheOptions = {
maxObjects: 500,
maxLength: 32 * 1024 * 1024
};
app = express.createServer();
app.on('error', function(error) {
return utils.exit("error running server: " + error);
});
app.use(express.favicon(favIcon));
app.use(jsonBodyParser());
app.all(/^\/ws\/client(.*)/, function(request, response, next) {
var uri;
uri = request.params[0];
if (uri === '') {
uri = '/';
}
if (options.debug) {
dumpingHandler(request, response, uri);
}
return clientHandler.handle(request, response, uri);
});
app.all(/^\/ws\/target(.*)/, function(request, response, next) {
var uri;
uri = request.params[0];
if (uri === '') {
uri = '/';
}
if (options.debug) {
dumpingHandler(request, response, uri);
}
return targetHandler.handle(request, response, uri);
});
app.use(express.errorHandler({
dumpExceptions: true
}));
app.use(express.staticCache(staticCacheOptions));
app.use(express["static"](options.staticWebDir));
if (options.boundHost === '-all-') {
utils.log("starting server at http://localhost:" + options.httpPort);
return app.listen(options.httpPort);
} else {
utils.log("starting server at http://" + options.boundHost + ":" + options.httpPort);
return app.listen(options.httpPort, options.boundHost);
}
};
getStaticWebDir = function() {
var webDir;
webDir = path.normalize(path.join(__dirname, '../web'));
if (utils.fileExistsSync(webDir)) {
return webDir;
}
return utils.exit('unable to find static files to serve in #{webDir}; did you do a build?');
};
Version = null;
getVersion = exports.getVersion = function() {
var json, packageJsonName, values;
if (Version) {
return Version;
}
packageJsonName = path.join(path.dirname(fs.realpathSync(__filename)), '../package.json');
json = fs.readFileSync(packageJsonName, 'utf8');
values = JSON.parse(json);
Version = values.version;
return Version;
};