-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
476 lines (380 loc) · 14.6 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
var path = require('path');
var fs = require('fs');
var spawn = require('child_process').spawn;
var urlParse = require('url').parse;
exports.summary = 'Start a static web server';
exports.usage ='[options]';
exports.options = {
"target" : {
alias : 't'
,default : '.'
,describe : 'target directory'
},
"port" : {
alias : 'p'
,default : 3000
,describe : 'server port'
}
,'log':{
alias:'l'
,default: false
,describe: "log requests"
}
,'delay': {
alias: 'd'
,type : 'number'
,describe: 'bandwidth delay'
}
,"reload" : {
alias : 'r'
,default: false
,describe : 'enable live reload changed files'
}
,"watch" : {
alias : 'w'
,describe : 'files be watched and reloaded'
}
,"console": {
default: false
,describe : 'enable remote logging service'
}
,"proxies": {
type: "array",
describe : 'enable request proxy'
}
,'open':{
alias: 'o'
,default: true
,describe: 'open the default browser after server starting'
}
,'deploy': {
describe: 'start as a deploy server'
}
,'token': {
describe: 'remote deploy token'
}
};
exports.run = function (options, done) {
var connect = require('connect');
var target = path.resolve(options.target);
var port = options.port;
var watchOptions;
if(options.watch){
watchOptions = {
src: options.watch,
tasks: ""
}
}
var middleware = [];
if(options.proxies){
var proxy = require('./lib/proxy');
proxy.logger = exports.logger;
proxy.config(options.proxies);
middleware.push(proxy.proxyRequest);
}
if(options.console){
exports.log("remote logging service enable");
var consolePort = options.consolePort = Number(String(options.console)) || 9999;
var consoleId = options.consoleId = exports.utils.randomString();
// start a standalone logging server
connect.router = require('./lib/router');
var consoleServer = connect.createServer(
connect.bodyParser(),
connect.static(path.join(__dirname, './asset/jsconsole')),
connect.router( require('./lib/remotelogging') )
);
consoleServer.listen(consolePort);
exports.log('success start remote logging server on port ' + consolePort + '.');
var url = 'http://127.0.0.1:'+ consolePort + '/?:listen ' + consoleId;
exports.log('open browser:', url.green);
exports.utils.open(url);
}
// auto reload server
if(options.reload) {
middleware.push( connect.static(path.join(__dirname, './asset/livereload')) );
exports.log("reload service enable");
}
if(options.reload || options.console) {
middleware.push( injectScript(options, connect) );
}
// deploy server
if(options.deploy){
middleware.push( connect.multipart({defer: true}) );
middleware.push( connect.static(path.join(__dirname, './asset/deploy')) );
middleware.push( connect.query() );
middleware.push( upload(options, connect) );
middleware.push( execution(options, connect) );
exports.log("deploy service enable");
}
// log config
if(options.log){
// `default` ':remote-addr - - [:date] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"'
// `short` ':remote-addr - :method :url HTTP/:http-version :status :res[content-length] - :response-time ms'
//` tiny` ':method :url :status :res[content-length] - :response-time ms'
// `dev` concise output colored by response status for development use
middleware.push( connect.logger(options.log) );
}
// delay response config
if(options.delay){
middleware.push( delay(options, connect));
exports.log("delay service enable");
}
// common middleware
middleware = middleware.concat([
// http://www.senchalabs.org/connect/middleware-errorHandler.html
connect.errorHandler(),
connect.favicon(),
connect.static( target ),
connect.directory( target )
]);
// run server
connect.apply(null, middleware)
.on('error', function( err ) {
if ( err.code === 'EADDRINUSE' ) {
return this.listen(0); // 0 means random port
}
// not an EADDRINUSE error, buble up the error
done(err);
})
.listen(port, function(err) {
if(err){
done(err);
}
var port = this.address().port;
// if enable reload service
if(options.reload){
var Reactor = require('./lib/reactor');
// create the reactor object
// reload server
var reactor = new Reactor( {
server: this,
apiVersion: '1.7',
host: 'localhost',
port: port
} );
var defaultWatchOptions = {
src: "./**/*.*",
tasks: ""
};
exports.runTask('watch', watchOptions || exports.config().tasks.watch || defaultWatchOptions, function(err, watcher){
watcher.on('changed', function(changedFiles){
// console.log(changedFiles)
reactor.reload(changedFiles);
});
exports.log("reload watch task start");
});
}
exports.log('success start server on port ' + port + '.');
if(options.open) {
var url = 'http://127.0.0.1:'+port;
exports.log('open browser:', url.green);
exports.utils.open(url);
}
done(null);
});
};
// connect delay middleware
// Fiddler2 provides an option under Rules -> Performance Menu -> Simulate Modem speeds.
// By default the Internet Connection Speed available on selecting this option will be equivalent to 6.6 Kb/s.
function delay(options, connect){
//From http://publik.tuwien.ac.at/files/pub-et_12521.pdf
//
// Table 1. Measured ping times (32 bytes)
//Technology Bandwidth (down/up) Mean Std
// GPRS 80/40 kbit/s 488 ms 146 ms
// EDGE 240/120 kbit/s 504 ms 89 ms
// UMTS 384/128 kbit/s 142 ms 58 ms
// HSDPA 1800/384 kbit/s 91 ms 43 ms
// ADSL 1000/256 kbit/s 10.9 ms 0.8 ms
return function delay(req, res, next) {
if ('GET' != req.method && 'HEAD' != req.method){
return next();
}
var timeout = 0;
if (typeof options.delay === 'function'){
timeout = options.delay();
}else{
timeout = Number(options.delay);
}
var pause = connect.utils.pause(req);
setTimeout(function() {
next();
pause.resume();
}, timeout);
};
}
// connect inject middleware for liveload and jsconsole
function injectScript(options, connect) {
return function (req, res, next){
// build filepath from req.url and deal with index files for trailing `/`
var filepath = req.url.slice(-1) === '/' ? req.url + 'index.html' : req.url;
// strip querystring
filepath = urlParse(filepath).pathname;
// if ext is anything but .html, let it go through usual connect static middleware.
if ( path.extname(filepath) !== '.html' ) {
return next();
}
// setup some basic headers, at this point it's always text/html anyway
res.setHeader('Content-Type', connect.static.mime.lookup(filepath));
// can't use the ideal stream / pipe case, we need to alter the html response
// by injecting that little livereload snippet
filepath = path.join(options.target, filepath.replace(/^\//, ''));
fs.readFile(filepath, 'utf8', function(e, body) {
if(e) {
// go next and silently fail
return next();
}
if(options.console)
body = ["<!-- jsconsole snippet -->",
"<script>document.write('<script src=\"http://'",
" + (location.host || 'localhost').split(':')[0]",
" + ':" + options.consolePort + "/remote.js?"+ options.consoleId +"\"><\\/script>')",
"</script>"
].join('\n') + body;
if(options.reload){
var port = res.socket.server.address().port;
body += ["<!-- livereload snippet -->",
"<script>document.write('<script src=\"http://'",
" + (location.host || 'localhost').split(':')[0]",
" + ':" + port + "/livereload.js?snipver=1\"><\\/script>')",
"</script>"
].join('\n');
}
res.end(body);
// exports.log("inject", filepath);
});
}
}
function upload(options, connect){
var token = options.token || '';
return function upload(req, res, next) {
if(req.url.indexOf('/upload') === -1) return next();
var query = req.query;
var form = req.form;
// File save path
var destfile = query.dest;
var existed = exports.file.exists(destfile);
// Overwrite existed file default
var overwrite = query.overwrite !== 'false';
if(query.token == token) {
function part(part){
// part.name - the field name for this part
// part.filename - only if the part is an incoming file
if(overwrite || !existed){
// Assume parent dirs created
exports.file.mkdir(path.dirname(destfile));
var out = fs.createWriteStream(destfile);
part.pipe(out);
}
}
var bodyRawBufferList = [];
function data(data){
if(overwrite || !existed){
// Assume parent dirs created
bodyRawBufferList.push(data);
}
}
function close(){
if(bodyRawBufferList[0]){
var bodyRawBuffer = Buffer.concat(bodyRawBufferList);
exports.file.mkdir(path.dirname(destfile));
fs.writeFileSync(destfile, bodyRawBuffer);
bodyRawBufferList = [];
}
var result = '';
if(overwrite || !existed){
result = 'uploaded';
}else {
result = 'existed';
}
res.end(result);
exports.log('uploaded', destfile);
}
if(form) {
form.on('part', part);
form.on('close', close);
}else {
req.on('data', data);
req.on('end', close);
}
}else{
res.end('deploy token require');
}
};
}
// execution middleware
function execution (options, connect){
var token = options.token || '';
// TODO: stdin is not work by http, maybe do it when use socket connection
return function execution(req, res, next) {
if(req.url.indexOf('/exec') === -1) return next();
var query = req.query;
var command = (query.command || '').trim();
// console.log(query, req);
if(query.init){
return res.end(process.cwd());
}
if(!command) return res.end('command is null');
if(query.token == token) {
res.setHeader("Content-Type", "text/html; charset=UTF-8");
var args = command.split(/\s+/);
var bin = args.shift();
// command blacklist
if(['ssh'].indexOf(bin) > -1) return res.end(bin + ' not support');
if(bin === 'svn'){
args = args.concat(['--non-interactive', '--trust-server-cert', '--force']);
}
var cwd = path.resolve(path.relative(process.cwd(), query.cwd? query.cwd: ''));
try{
exports.log( cwd +' > ' +command.grey);
// current working directory of the child process
var cp = spawn(bin, args, {cwd: cwd, timeout: 60000, stdio: [ 'pipe', 'pipe', 'pipe', 'ipc']});
// By default, the parent will wait for the detached child to exit.
// To prevent the parent from waiting for a given child, use the child.unref() method,
// and the parent's event loop will not include the child in its reference count.
cp.unref();
}catch(e){
return res.end(e.toString());
}
function write(data){
// var str = data.toString(), lines = str.split(/\n/).join('<br/>');
// res.write(lines);
res.write(data.toString());
}
// cp.stdin.resume();
// cp.stdin.setEncoding('utf8');
// cp.stdin.write('\n');
// cp.stdin.on('data', function(data){
// console.log('process input:' + data)
// });
cp.stdout.on("data", write);
cp.stderr.on('data', write);
cp.on('error', function error(err) {
exports.log('process error: ' + err);
res.end(err.toString());
// Send a signal to the child process. If no argument is given, the process will be sent 'SIGTERM'
cp.kill();
});
cp.on('close', function exit(code){
exports.log('process close: ' + command.grey, 'exited with code ' + code);
res.end();
});
function end(signal){
exports.log('process signal: '+ signal );
}
// TODO: above signal events do not work now
// Hangup detected on controlling terminal or death of controlling process
cp.on('SIGHUP', end.bind(this, 'SIGHUP'));
// Interrupt from keyboard
cp.on('SIGINT', end.bind(this, 'SIGINT'));
// Termination signal
cp.on('SIGTERM', end.bind(this, 'SIGTERM'));
// tty input for background process
cp.on('SIGTTIN', end.bind(this, 'SIGTTIN'));
// Stop typed at tty
cp.on('SIGTSTP', end.bind(this, 'SIGTSTP'))
}else{
res.end('deploy token require');
}
};
}