forked from bolidozor/js9
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js9Helper.js
executable file
·859 lines (817 loc) · 24.9 KB
/
js9Helper.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
/*
*
* js9Helper: Node-based back-end server for JS9 (September 4, 2012)
*
* Principals: Eric Mandel
* Organization: Harvard Smithsonian Center for Astrophysics, Cambridge MA
* Contact: [email protected]
*
* Copyright (c) 2012 - 2014 Smithsonian Astrophysical Observatory
*
* Utilizes: socket.io, node-uuid
*
*/
/*jslint bitwise: true, plusplus: true, vars: true, white: true, continue: true, unparam: true, regexp: true, browser: true, devel: true, node: true, stupid: true */
/*jshint smarttabs:true */
"use strict";
// load required modules
var http = require('http'),
https = require('https'),
Sockio = require('socket.io'),
url = require('url'),
qs = require('querystring'),
cproc = require("child_process"),
fs = require("fs"),
uuid = require('node-uuid'),
rmdir = require('rimraf');
// internal variables
var app, io, secure;
var prefsfile = "js9Prefs.json";
var securefile = "js9Secure.json";
var fits2png = {};
var analysis = {str:[], pkgs:[]};
var envs = JSON.stringify(process.env);
var plugins = [];
var cdir = process.cwd();
// secure options ... change as necessary in securefile
var secureOpts = {
// key: "path_to_private.key", // openssl genrsa -out file 2014
// cert: "path_to_certificate_file", // from the certificate authority
// ca: "path_to_certificate_authority" // can be a chain file
};
// default options ... change as necessary in prefsfile
var globalOpts = {
helperPort: 2718,
helperHost: "0.0.0.0",
cmd: "js9helper",
analysisPlugins: "./analysis-plugins",
analysisWrappers: "./analysis-wrappers",
helperPlugins: "./helper-plugins",
maxBinaryBuffer: 150*1024000, // exec buffer: good for 4096^2 64-bit image
maxTextBuffer: 5*1024000, // exec buffer: good for text
textEncoding: "ascii", // encoding for returned stdout from exec
workDir: "", // top-level working directory for exec
workDirQuota: 50, // quota on working directory (Mb)
rmWorkDir: true, // remove workdir on disconnect?
remoteMsgs: 1 // 0 => none, 1 => samehost, 2 => all
};
//
// functions that might depend on specific implementations of socket.io
//
// get ip address and port of the current socket or http connection
function getHost(io, req){
// socket.io
if( req.handshake ){
return req.client.conn.remoteAddress;
}
// http server
// http://stackoverflow.com/questions/19266329/node-js-get-clients-ip
return (req.headers['x-forwarded-for'] || '').split(',')[0] ||
req.connection.remoteAddress;
}
// http://stackoverflow.com/questions/6563885/socket-io-how-do-i-get-a-list-of-connected-sockets-clients
function findClientsSocket(io, namespace, roomId){
var id, index;
var res = [];
var ns = io.of(namespace ||"/"); // the default namespace is "/"
if(ns){
for(id in ns.connected){
if( ns.connected.hasOwnProperty(id) ){
if(roomId){
index = ns.connected[id].rooms.indexOf(roomId) ;
if(index !== -1){
res.push(ns.connected[id]);
}
} else {
res.push(ns.connected[id]);
}
}
}
}
return res;
}
// get list of all clients currently connected
function getClients(io, socket){
return findClientsSocket(io);
}
// utility functions
// create a nice date/time string for logging
function datestr(){
return new Date().toISOString().replace(/T/, ' ').replace(/\..+/, ' UTC');
}
// output a log message on the console
function clog(){
var args = Array.prototype.slice.call(arguments, 0);
args.push(" [" + datestr() + "]");
console.log.apply(null, args);
}
// output an error message on the console
function cerr(){
var args = Array.prototype.slice.call(arguments, 0);
args.unshift("ERROR: ");
args.push(" [" + datestr() + "]");
console.log.apply(null, args);
}
// getTargets: identify target(s) for an external msg for a given ip
function getTargets(io, socket, msg){
var i, j, c, clip;
var displays;
var browserip = msg.browserip || "*";
var targets = [];
// ip associated with this socket
var myip = getHost(io, socket);
// list of all clients connected on this socket
var clients = getClients(io, socket);
// authentication function
var authenticate = function(myip, clip){
// if I'm localhost, I can send to anyone
if( (myip === "127.0.0.1") || (myip === "::ffff:127.0.0.1") ){
return true;
}
// I can send to myself, if we configured that way
if( (myip === clip) && (globalOpts.remoteMsgs > 0) ){
return true;
}
// security risk: anyone can send to anyone
if( globalOpts.remoteMsgs > 1 ){
return true;
}
// can't send to anyone
return false;
};
// look at all clients
for(i=0; i<clients.length; i++){
// current client
c = clients[i];
// sanity check
if( !c.js9 ){
continue;
}
// client ip
clip = getHost(io, c);
if( !authenticate(myip, clip) ){
continue;
}
// look for matching clients
// pageid is a unique id associated with a specific page
if( msg.pageid ){
if( msg.pageid === c.js9.pageid ){
targets.push(c);
}
} else if( ((browserip === "*") || (browserip === clip)) ){
// look for matches in display id
displays = c.js9.displays;
for(j=0; j<displays.length; j++){
// look for matching displays
if( (msg.id === "*") || (msg.id === displays[j]) ){
targets.push(c);
}
}
}
}
return targets;
}
// envClean: clean incoming environment variables
// this should match cleaning in js9Helper.cgi
function envClean(s) {
if( typeof s === "string" ){
return s.replace(/[`&]/g, "").replace(/\(\)[ ]*\{.*/g, "");
}
return s;
}
function loadSecurePreferences(securefile){
var s, obj, opt;
var secure = false;
if( fs.existsSync(securefile) ){
s = fs.readFileSync(securefile, "utf-8");
if( s ){
try{ obj = JSON.parse(s.toString()); }
catch(e){ cerr("can't parse: ", securefile, e); }
// merge opts into secureOpts
for( opt in obj ){
if( obj.hasOwnProperty(opt) ){
switch(opt){
case "key":
secureOpts.key = fs.readFileSync(obj[opt]);
break;
case "cert":
secureOpts.cert = fs.readFileSync(obj[opt]);
break;
case "ca":
secureOpts.ca = fs.readFileSync(obj[opt]);
break;
default:
cerr("unknown secure option: ", opt);
break;
}
}
}
// if we have a prefs file, we need to have defined required props
if( secureOpts.key && secureOpts.cert ){
secure = true;
} else {
cerr("missing one or more required secure properties");
}
}
}
return secure;
}
// load preference file, if possible
function loadPreferences(prefsfile){
var s, obj, opt, otype, jtype;
if( fs.existsSync(prefsfile) ){
s = fs.readFileSync(prefsfile, "utf-8");
if( s ){
try{ obj = JSON.parse(s.toString()); }
catch(e){ cerr("can't parse: ", prefsfile, e); }
// look for globalOpts and merge
if( obj && obj.globalOpts ){
for( opt in obj.globalOpts ){
if( obj.globalOpts.hasOwnProperty(opt) ){
otype = typeof obj.globalOpts[opt];
jtype = typeof globalOpts[opt];
if( (jtype === otype) || (jtype === "undefined") ){
switch(otype){
case "number":
globalOpts[opt] = obj.globalOpts[opt];
break;
case "boolean":
globalOpts[opt] = obj.globalOpts[opt];
break;
case "string":
globalOpts[opt] = obj.globalOpts[opt];
break;
default:
break;
}
}
}
}
}
}
}
}
// load analysis plugin files, if available
function loadAnalysisTasks(dir){
if( fs.existsSync(dir) ){
fs.readdir(dir, function(err, files){
var i, jstr, pathname;
for(i=0; i<files.length; i++){
pathname = dir + "/" + files[i];
if( fs.existsSync(pathname) ){
// only json files, please
if( !pathname.match(/.json$/) ){
continue;
}
analysis.temp = fs.readFileSync(pathname, "utf-8");
if( analysis.temp ){
jstr = analysis.temp.toString().trim();
switch(files[i]){
case "fits2png.json":
try{ fits2png = JSON.parse(jstr); }
catch(e1){cerr("can't parse: ", pathname, e1);}
break;
default:
try{
analysis.pkgs.push(JSON.parse(jstr));
analysis.str.push(jstr);
}
catch(e2){cerr("can't parse: ", pathname, e2);}
break;
}
}
}
}
});
}
}
// addAnalysisTask: add to the list of analysis tasks sent to browser
function addAnalysisTask(obj) {
analysis.pkgs.push(obj);
analysis.str.push("[" + JSON.stringify(obj) + "]");
}
// load user-defined plugins, if possible
function loadHelperPlugins(dir){
if( fs.existsSync(dir) ){
fs.readdir(dir, function(err, files){
var i, x, pathname, name;
for(i=0; i<files.length; i++){
pathname = dir + "/" + files[i];
if( fs.existsSync(pathname) ){
// only js files, please
if( !pathname.match(/.js$/) ){
continue;
}
// name of message
name = files[i].replace(/.js$/, "");
try{ x = require(pathname); }
catch(e){ cerr("warning: can't load: ", pathname); }
if( x ){
plugins.push({name: name,
sockio: x.sockio,
http: x.http,
httpList: x.httpList});
}
}
}
});
}
}
// parse an argument string into an array of arguments, where
// spaces and quotes are delimiters
function parseArgs(argstr){
var targs, i, j, ci, c1, c2, s;
var args = [];
// split arguments on spaces
targs = argstr.split(" ");
// now re-combine quoted args into one arg
for(i=0, j=0, ci=-1; i<targs.length; i++){
// remove or rename dangerous characters
s = targs[i].replace(/`/g, "").replace(/&/g, "__ampersand__");
// are we re-combining?
if( ci >= 0 ){
// yes, add to current arg
args[ci] = args[ci] + " " + s;
} else {
// no, add another arg
args[j] = s;
}
if( ci === -1 ){
// new quoted string?
c1 = s.charAt(0);
c2 = s.charAt(s.length-1);
if( c1 === "'" || c1 === '"' ){
// spread across more than one arg, so we need to recombine?
if( c2 === c1 ){
// no just remove the quotes from this one
args[j] = args[j].substr(1,args[j].length-2);
j++;
} else {
// yes
ci = j;
}
} else {
// no
j++;
}
} else {
// end of current quoted string?
c2 = s.charAt(s.length-1);
if( c2 === c1 ){
// yes, remove enclosing quotes
args[ci] = args[ci].substr(1,args[ci].length-2);
ci = -1;
j++;
}
}
}
return args;
}
//
// message callbacks
//
// execCmd: exec a analysis wrapper function to run a command
// this is the default callback for server-side analysis tasks
function execCmd(io, socket, obj, cbfunc) {
var cmd, argstr, args, maxbuf;
var myworkdir = null;
var myip = getHost(io, socket);
var myid = obj.id;
var myrtype = obj.rtype || "binary";
var myenv = JSON.parse(envs);
// sanity check
if( !obj.cmd ){
return;
}
// id of js9 display
myenv.JS9_ID = envClean(myid);
// host ip
myenv.JS9_HOST = envClean(myip);
// JS9 base dir
myenv.JS9_DIR = cdir;
// JS9 unique page id
myenv.JS9_PAGEID = socket.js9.pageid;
// js9 cookie in the sending browser
if( obj.cookie ){
myenv.HTTP_COOKIE = envClean(obj.cookie);
}
// datapath (for finding data files)
if( obj.dataPath ){
myenv.JS9_DATAPATH = envClean(obj.dataPath);
} else if( globalOpts.dataPath ){
myenv.JS9_DATAPATH = envClean(globalOpts.dataPath);
}
// set max buffer size
switch(myrtype){
case "text":
case "plot":
case "alert":
maxbuf = globalOpts.maxTextBuffer;
break;
default:
maxbuf = globalOpts.maxBinaryBuffer;
break;
}
// the command string
argstr = obj.cmd || "";
// expand directory macros
argstr = argstr
.replace(/\$\{?JS9_DIR\}?/, cdir)
.replace(/\$\{?JS9_WORKDIR\}?/, (socket.js9.workDir || ""));
// split arguments on spaces, respecting quotes
args = parseArgs(argstr);
// get commmand to execute
if( args[0] === globalOpts.cmd ){
// handle fitshelper specially
cmd = args[0];
} else {
// start in the appropriate work directory, if possible
if( (obj.useWorkDir !== false) && socket.js9 && socket.js9.workDir ){
myworkdir = socket.js9.workDir;
// working directory relative to JS9 dir
myenv.JS9_WORKDIR = myworkdir;
myenv.JS9_WORKDIR_QUOTA = globalOpts.workDirQuota;
}
// construct wrapper
cmd = globalOpts.analysisWrappers + "/" + args[0];
// make path absolute in case we change directories
if( cmd.charAt(0) !== "/" ){
cmd = cdir + "/" + cmd;
}
}
// log what we are about to do
clog("exec: %s [%s]", cmd, args.slice(1));
// execute the analysis script with cmd arguments
// NB: can't use exec because it's shell breaks, e.g. region command lines
cproc.execFile(cmd, args.slice(1),
{ encoding: "utf8",
timeout: 0,
maxBuffer: maxbuf,
killSignal: "SIGTERM",
cwd: myworkdir,
env: myenv
},
// return from exec
function(errcode, stdout, stderr) {
var res={stdout: null, stderr: null};
if( errcode ){
res.errcode = errcode.errno || errcode.code;
}
if( stdout ){
switch(myrtype){
case "text":
case "plot":
case "png":
case "alert":
res.encoding = globalOpts.textEncoding;
res.stdout = stdout.toString(res.encoding);
break;
default:
res.encoding = "binary";
res.stdout = stdout;
break;
}
}
if( stderr ){
res.stderr = stderr.toString();
}
// send results back to browser
if( cbfunc ){ cbfunc(res); }
});
}
// sendAnalysis: send list of analysis routines to browser
function sendAnalysisTasks(io, socket, obj, cbfunc) {
var s;
if( analysis && analysis.str.length ){
s = "[" + analysis.str.join(",") + "]";
if( cbfunc ){ cbfunc(s); }
}
}
// sendMsg: send a message to the browser
// this is the default callback for external communication with JS9
function sendMsg(io, socket, obj, cbfunc) {
var i, targets;
// callback function
var myfunc = function(s){
if( cbfunc ){ cbfunc(s); }
};
// get list of targets to send to
targets = getTargets(io, socket, obj);
// look for one target (or else that multi is allowed)
if( (targets.length === 1) || obj.multi ){
for(i=0; i<targets.length; i++){
targets[i].emit("msg", obj, myfunc);
}
} else {
if( cbfunc ){
cbfunc("ERROR: "+targets.length+" JS9 instance(s) found with" +
" id " + obj.id + " (" + obj.cmd+")");
}
}
}
//
// protocol handlers
//
// socketio handler: field socket.io requests
function socketioHandler(socket) {
var i, j, m, a;
// function outside loop needed to make jslint happy
var xfunc = function(obj, cbfunc) {
if( !obj ){return;}
// exec the analysis task (via a wrapper function)
execCmd(io, socket, obj, cbfunc);
};
// on disconnect: display a console message
// returns: N/A
// for other implementations, this is needed if you want to:
// show disconnects in the log
socket.on("disconnect", function() {
var myhost = getHost(io, socket);
// only show disconnect for displays (not js9 msgs)
if( socket.js9 && socket.js9.displays ){
clog("disconnect: %s (%s)", myhost, socket.js9.displays);
// clean up working directory
if( socket.js9.workDir && globalOpts.rmWorkDir ){
rmdir(socket.js9.workDir, function(error){
if( error ){
cerr("can't delete workDir: ", error);
}
});
}
}
});
// on displays: get the list of displays for this connection
// returns: unique page id (not currently used)
// for other implementations, this is needed if you want to:
// support sending external messages to JS9 (i.e., via js9 script)
socket.on("displays", function(obj, cbfunc) {
var myhost = getHost(io, socket);
if( !obj ){return;}
socket.js9 = {};
socket.js9.displays = obj.displays;
socket.js9.pageid = uuid.v4();
socket.js9.workDir = null;
if( fs.existsSync(globalOpts.workDir) ){
socket.js9.workDir = globalOpts.workDir + "/" + socket.js9.pageid;
try{ fs.mkdirSync(socket.js9.workDir, parseInt('0755',8)); }
catch(e){
cerr("can't create workDir: ", e.message);
socket.js9.workDir = null;
}
}
clog("connect: %s (%s)", myhost, socket.js9.displays);
if( cbfunc ){ cbfunc(socket.js9.pageid); }
});
// on alive: return "OK" to signal a valid connection
socket.on("alive", function(obj, cbfunc) {
if( cbfunc ){ cbfunc("OK"); }
});
// on image: signal from JS9 that a new or redisplayed image is active
// returns: [input file path, fits file path, fits + extension]
// for other implementations, this is needed if you want to:
// get FITS filename associated with PNG representation files
// (args 1 and 2 will be identical when imaging FITS files)
socket.on("image", function(obj, cbfunc) {
if( !obj ){return;}
if( globalOpts.cmd ){
// make up js9helper command
obj.cmd = globalOpts.cmd + " -i " + obj.image;
// exec the command
execCmd(io, socket, obj, cbfunc);
}
});
// on getAnalysis: get analysis task definitions
// returns: json string containing analysis task definitions
// for other implementations, this is needed if you want to:
// support default server-side analysis (i.e. exec a wrapper script)
socket.on("getAnalysis", function(obj, cbfunc) {
if( !obj ){return;}
sendAnalysisTasks(io, socket, obj, cbfunc);
});
// on runAnalysis: run an analysis task
// returns: object w/ errcode, stderr (error string), stdout (results)
// for other implementations, this is needed if you want to:
// support default server-side analysis (i.e. exec a wrapper script)
// NB: retained for backward compatibility with old (cached) versions of JS9
socket.on("runAnalysis", function(obj, cbfunc){
if( !obj ){return;}
// exec the analysis task (via a wrapper function)
execCmd(io, socket, obj, cbfunc);
});
// NB: instead of runAnalysis, now we use a handler for each separate task
// returns: object w/ errcode, stderr (error string), stdout (results)
// for other implementations, this is needed if you want to:
// support default server-side analysis (i.e. exec a wrapper script)
for(j=0; j<analysis.pkgs.length; j++){
for(i=0; i<analysis.pkgs[j].length; i++){
a = analysis.pkgs[j][i];
// check for require workDir
if( a.workDir && !globalOpts.workDir ){
continue;
}
// loadproxy must be enabled explicitly
if( a.name === "loadproxy" && !globalOpts.loadProxy ){
continue;
}
m = a.xclass ? (a.xclass + ":" + a.name) : a.name;
socket.on(m, xfunc);
}
}
// on fits2png: convert raw fits to png
// returns: object w/ errcode, stderr (error string), stdout (results)
// for other implementations, this is needed if you want to:
// support conversion of fits to png representation
socket.on("fits2png", function(obj, cbfunc) {
if( !obj ){return;}
if( fits2png[0] && fits2png[0].action ){
// make up fits2png command string from defined fits2png action
obj.cmd = fits2png[0].action + " " + obj.fits;
// don't use a workdir
obj.useWorkDir = false;
// exec the conversion task (via a wrapper function)
execCmd(io, socket, obj, cbfunc);
}
});
// on msg: send a command from an external source to a JS9 browser
// nb: this msg comes from an external source, not from js9 itself
// returns: results from JS9
// for other implementations, this is needed if you want to:
// support sending external messages to JS9 (i.e., via js9 script)
socket.on("msg", function(obj, cbfunc) {
if( !obj ){return;}
sendMsg(io, socket, obj, cbfunc);
});
// an example of site-specific in-line messsages
if( process.env.NODEJS_FOO ){
// After defining a foo message, you can do this in javascript:
// JS9.Send("FOO:foo",{keys:{"x":1,"y":2}},function(r){dofunc(r)});
socket.on("FOO:foo", function(obj, cbfunc) {
var s = "foo: " + JSON.stringify(obj.keys);
// analysis tasks should return an object containing one or more:
// error (error code), stdout (string result), stderr (error msg)
if( cbfunc ){ cbfunc({stdout: s}); }
});
}
// add plugins
for(i=0; i<plugins.length; i++){
if( plugins[i].sockio ){
try{ plugins[i].sockio(io, socket); }
catch(e){ cerr("can't add %s", plugins[i].name); }
}
}
}
// httpd handler: field pseudo-socket.io http requests
// public api:
// wget $MYHOST'/msg?{"id": "'$ID'", "cmd": "SetColormap", "args": ["red"]}'
// wget $MYHOST'/msg?{"id": "'$ID'", "cmd": "GetColormap"}'
// js9 command line:
// wget $MYHOST'/msg?{"id": "'$ID'", "cmd": "zoom", "args": [2]}'
// wget $MYHOST'/msg?{"id": "'$ID'", "cmd": "zoom"}'
// analysis commands:
// wget $MYHOST'/counts?{"id": "'$ID'", "cmd": "counts", "args": ["counts"]}'
function httpHandler(req, res){
var cmd, gobj, s, jstr;
var body = "";
// return error into to browser
var htmlerr = function(s){
res.writeHead(400, s, {"Content-Type": "text/plain"});
res.end();
};
// call-back function returning info to the client
var cbfunc = function(s){
switch(typeof s){
case "string":
break;
case "object":
s = JSON.stringify(s);
break;
default:
s = s.toString();
break;
}
res.writeHead(200, {"Content-Type": "text/plain"});
res.write(s);
res.end();
};
// generate object and run the cmd
var docmd = function(cmd, jstr){
var i, j, obj;
// the constructed string is stringified json, if it exists
// try to parse it into an object
if( jstr && jstr !== "null" ){
try{ obj = JSON.parse(jstr); }
catch(e){
htmlerr("can't parse JSON object in http request: " + jstr);
return;
}
} else {
obj = {};
}
// check for id and set default
obj.id = obj.id || "JS9";
// process the command
switch(cmd){
case "alive":
if( cbfunc ){ cbfunc("OK"); }
break;
case "msg":
// send a command from an external source to a JS9 browser
sendMsg(io, req, obj, cbfunc);
break;
default:
// plugin messages: NB needs authentication!
for(j=0; j<plugins.length; j++){
// simple plugin: name is the same as the plugin filename
if( plugins[j].http && (cmd === plugins[j].name) ){
plugins[j].http(io, req, obj, cbfunc);
return;
}
if( plugins[j].httpList ){
// list of plugins, each with their own name
for(i=0; i<plugins[j].httpList.length; i++){
if( cmd === plugins[j].httpList[i].name ){
plugins[j].httpList[i].func(io, req, obj, cbfunc);
return;
}
}
}
}
htmlerr("unknown command in " + req.method + " request: " + cmd);
break;
}
};
// parse url to get object for easier handling
gobj = url.parse(req.url);
// get command from pathname
if( gobj.pathname ){
cmd = gobj.pathname.replace(/^\//, "");
}
// method-specific processing
switch(req.method){
case "GET":
// fix some awkwardness in qs.parse
if( gobj.query ){
gobj.query = gobj.query.replace(/\+/g, "%2B");
}
// we pass the stringified json data directly command type,
// so prepend an obj so we can parse it
s = "obj=" + gobj.query;
try{ jstr = qs.parse(s).obj; }
catch(e){
htmlerr("can't parse JSON object in http GET request: " + s);
return;
}
docmd(cmd, jstr);
break;
case "POST":
req.on('data', function(chunk){
body += chunk;
});
req.on('end', function(){
jstr = body.toString();
docmd(cmd, jstr);
});
break;
default:
htmlerr("unsupported method: " + req.method);
return;
}
}
//
// initialization
//
// load secure preferences
secure = loadSecurePreferences(securefile);
// load preference file
loadPreferences(prefsfile);
// load analysis plugins
loadAnalysisTasks(globalOpts.analysisPlugins);
// load user-defined plugins
loadHelperPlugins(globalOpts.helperPlugins);
// start up http server
if( secure ){
app = https.createServer(secureOpts, httpHandler);
} else {
app = http.createServer(httpHandler);
}
// never timeout, analysis requests can be very long
app.setTimeout(0);
// and connect with the socket.io server
io = new Sockio(app);
// for each socket.io connection, receive and process custom events
io.on("connection", socketioHandler);
// start listening on the helper port
app.listen(globalOpts.helperPort, globalOpts.helperHost);
// an example of adding an in-line messsage to the analysis task list
if( process.env.NODEJS_FOO === "analysis" ){
// add foo to the list of analysis tasks sent to JS9
// it will go into the analysis menu and will be treated as analysis, i.e.
// automatic macro expansion, light-window to display of text/plot, etc.
// keys is an array macros to expand; they will be returned as an object
addAnalysisTask({xclass: "FOO", name: "foo", title: "Do Foo",
keys: ["filename", "regions", "id"]});
}
// last ditch attempt to keep the server up
process.on("uncaughtException", function(e){
cerr("uncaughtException: %s [%s]", e, e.stack || e.stacktrace || "");
});