forked from chaoscollective/Space_Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
1178 lines (1164 loc) · 42.7 KB
/
app.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
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// SERVER-SIDE
// Node.JS! :)
//
console.log(".---------------------------.");
console.log("| * Starting Node service * |");
console.log("'---------------------------'");
var express = require("express");
var util = require("util");
var fs = require('fs');
var crypto = require('crypto');
var walk = require('walk');
var spawn = require('child_process').spawn;
var exec = require('child_process').exec;
var _ = require('underscore');
// ------------------------------------------------
// BASIC USER AUTH w/ EXPRESS
// ------------------------------------------------
function authorize(user, pw) {
var userIsOk = false;
userIsOk |= (user === 'user' && pw === 'password');
return userIsOk;
}
var app = express.createServer();
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({
secret: "a very secret secret",
store: new express.session.MemoryStore,
cookie: {
path : '/',
httpOnly : true,
maxAge : 1000*60*60*24*30*2 //60 days
}
}));
//app.use(express.basicAuth(authorize));
var uCount = (new Date()).getTime()%99999;
app.use(function(req, res, next){
req.user = req.user || {};
if(req.cookies && req.cookies["_username"]){
req.user.displayName = req.cookies["_username"];
}else{
req.user.displayName = "user_"+(uCount++);
res.cookie("_username", req.user.displayName);
console.log("say hello to new user: " + req.user.displayName);
}
next();
});
var staticProvider = express.static(__dirname + '/public');
app.use(staticProvider); // this is where static files will be served (html, css, js, media, etc.)
// ------------------------------------------------
// ------------------------------------------------
app.get('/',function(req,res,next){
req.url = "index.html";
staticProvider(req, res, next);
});
var port = process.env.PORT || 3149;
app.listen(port);
var EDITABLE_APPS_DIR = "/APPS/";
var ENABLE_LAUNCH = false;
// -----------------------------------------------------
// for demo clean-up (remove if this gives you problems)
var REPLACE_SANDBOX_APP_DEMO_FILES = true;
if(REPLACE_SANDBOX_APP_DEMO_FILES){
fs.copyF = function (src, dst, cb) {
function copy(err) {
var is
, os
;
if (!err) {
return cb(new Error("File " + dst + " exists."));
}
fs.stat(src, function (err) {
if (err) {
return cb(err);
}
is = fs.createReadStream(src);
os = fs.createWriteStream(dst);
util.pump(is, os, cb);
});
}
fs.stat(dst, copy);
};
fs.unlinkSync(EDITABLE_APPS_DIR+"SandboxApp/app.js");
fs.copyF(__dirname+"/app.js", EDITABLE_APPS_DIR+"SandboxApp/app.js", function(err){
if(err){
console.log(err);
}else{
console.log("copied app.js to SandboxApp");
}
});
fs.unlinkSync(EDITABLE_APPS_DIR+"SandboxApp/public/index.less");
fs.copyF(__dirname+"/public/index.css", EDITABLE_APPS_DIR+"SandboxApp/public/index.less", function(err){
if(err){
console.log(err);
}else{
console.log("copied public/index.less to SandboxApp");
}
});
fs.unlinkSync(EDITABLE_APPS_DIR+"SandboxApp/public/index.js");
fs.copyF(__dirname+"/public/editFile.js", EDITABLE_APPS_DIR+"SandboxApp/public/index.js", function(err){
if(err){
console.log(err);
}else{
console.log("copied public/index.js to SandboxApp");
}
});
}
// end of demo clean-up section.
// ------------------------------------------------------
var thisAppDirName = __dirname.substring(__dirname.lastIndexOf("/")+1);
var teamID = "SandboxApp";
// ------------------------------------------------------------
// ------------------------------------------------------------
// TODO: check credentials before doing any of these GET/POST...
app.get("/allProjectFiles", function(req, res){
if(req.query.project && req.query.project.length > 2){
var project = req.query.project.replace(/\.\./g, "");
var projectRoot = EDITABLE_APPS_DIR+project;
console.log("Listing all project files [" + projectRoot+"] for user: "+req.user.displayName + " --> (~"+usersInGroup[project]+" sockets)");
try{
var walker = walk.walk(projectRoot, {followLinks: false});
var filesAndInfo = [];
walker.on("names", function (root, nodeNamesArray) {
// use this to remove/sort files before doing the more expensive "stat" operation.
for(var i=nodeNamesArray.length-1; i>=0; i--){
if(nodeNamesArray[i] == ".git" || nodeNamesArray[i] == "node_modules" || nodeNamesArray[i] == "_db"){
nodeNamesArray.splice(i, 1);
}
}
});
walker.on("file", function (root, fileStats, next) {
var rt = root.substring(projectRoot.length+1);
if(rt.length > 0){
rt += "/";
}
var fname = rt + fileStats.name;
var sz = fileSizeCache[project+"/"+fname];
if(sz === undefined){
// first time checking files size.. get it!
sz = fileStats.size;
fileSizeCache[project+"/"+fname] = sz;
}
var td = fileTodoCache[project+"/"+fname];
var fd = null;
if(td === undefined && sz < 1000000){
fd = fs.readFileSync(projectRoot+"/"+fname, "utf8");
td = occurrences(fd, "TODO");
fileTodoCache[project+"/"+fname] = td;
}
var fm = fileFixMeCache[project+"/"+fname];
if(fm === undefined && sz < 1000000){
if(fd === null){
fd = fs.readFileSync(projectRoot+"/"+fname, "utf8");
}
fm = occurrences(fd, "FIXME");
fileFixMeCache[project+"/"+fname] = fm;
}
var n = usersInGroup[project+"/"+fname];
if(n){
filesAndInfo.push([fname, n, sz, td, fm]);
}else{
filesAndInfo.push([fname, 0, sz, td, fm]);
}
fd = null;
next();
});
walker.on("end", function() {
//console.log("Recursively listed project files for: " + project);
// indicate total team members online.
var n = usersInGroup[project];
if(n){
filesAndInfo.push(["", n]);
}else{
filesAndInfo.push(["", 0]);
}
res.send(JSON.stringify(filesAndInfo));
//callback(null, filesAndInfo);
});
}catch(ex){
console.log("<span style='color: #F00;'>*** exception walking files!</span>");
console.log(err);
}
}else{
res.send("FAIL: no project name.");
}
});
app.post("/launchProject", function(req, res){
if(!ENABLE_LAUNCH){
res.send("FAIL: Sorry, but launching projects is not currently enabled.");
return;
}
if(req.query.project && req.query.project.length > 2){
var projectName = req.query.project.replace(/\.\./g, "");
console.log("LAUNCHING Project ["+req.user.displayName+"] >> " + projectName);
var projPath = EDITABLE_APPS_DIR+projectName;
exec('stop node_'+projectName, {
encoding: 'utf8',
timeout: 30000,
maxBuffer: 200*1024,
killSignal: 'SIGTERM',
env: null
},
function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
// return res.send("FAIL:");
}
console.log("STOP: " + stdout);
exec('start node_'+projectName, {
encoding: 'utf8',
timeout: 30000,
maxBuffer: 200*1024,
killSignal: 'SIGTERM',
env: null
},
function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
return res.send("FAIL:");
}
var launchURL = "http://"+projectName.toLowerCase()+".chaoscollective.org/";
console.log("START: " + stdout);
console.log("DEPLOY SUCCESSFUL: " + launchURL);
res.send("ok");
}
); // exec 2
}
); // exec 1
}else{
res.send("FAIL: no project name.");
}
});
app.post("/createFile", function(req, res){
console.log("CREATE FILE ["+req.user.displayName+"]");
if(req.query.project && req.query.project.length > 2 && req.body.fname){
var projectName = req.query.project.replace(/\.\./g, "");
var fname = req.body.fname;
if(!fname || fname.length < 2){
return;
}
var safeFName = fname.split("..").join("").replace(/[^a-zA-Z_\.\-0-9\/\(\)]+/g, '');
var path = EDITABLE_APPS_DIR+projectName+"/"+safeFName;
try{
fs.realpathSync(path);
console.log("file already exists.. no need to create it: " + path);
return res.send("FAIL: File already exists. No need to create it.");
}catch(ex){
console.log("file doesn't exist yet. creating it: " + path);
fs.writeFile(path, "", function(err) {
if(err) {
console.log(err);
return res.send("FAIL: Error creating new file.");
} else {
localFileIsMostRecent[projectName+"/"+safeFName] = true; // mark file as saved with no pending changes.
console.log("FILE SAVED: " + safeFName);
res.send(safeFName);
}
});
}
}else{
res.send("FAIL: no project and/or filename.");
}
});
app.post("/deleteFile", function(req, res){
console.log("DELETE FILE ["+req.user.displayName+"]");
if(req.query.project && req.query.project.length > 2 && req.body.fname){
var projectName = req.query.project.replace(/\.\./g, "");
var fname = req.body.fname;
if(!fname || fname.length < 2){
return;
}
var safeFName = fname.split("..").join("").replace(/[^a-zA-Z_\.\-0-9\/\(\)]+/g, '');
var path = EDITABLE_APPS_DIR+projectName+"/"+safeFName;
if(usersInGroup[projectName+"/"+safeFName]){
console.log("Delete stopped, users still in file: "+path);
return res.send("FAIL: users still in file.");
}
try{
fs.realpathSync(path);
console.log("file exists.. delete it: " + path);
fs.unlink(path, function (err) {
if (err){
console.log(err);
return res.send("FAIL: could not delete file.");
}
console.log("successfully deleted: " + path);
return res.send(safeFName);
});
}catch(ex){
return res.send("FAIL: File doesn't exist. No need to delete it.");
}
}else{
res.send("FAIL: no project and/or filename.");
}
});
app.post("/renameFile", function(req, res){
console.log("RENAME FILE ["+req.user.displayName+"]");
if(req.query.project && req.query.project.length > 2 && req.body.fname && req.body.newfname){
var projectName = req.query.project.replace(/\.\./g, "");
var fname = req.body.fname;
if(!fname || fname.length < 2){
return;
}
var newfname = req.body.newfname;
if(!newfname || newfname.length < 2){
return;
}
var safeFName = fname.split("..").join("").replace(/[^a-zA-Z_\.\-0-9\/\(\)]+/g, '');
var safeNewFName = newfname.split("..").join("").replace(/[^a-zA-Z_\.\-0-9\/\(\)]+/g, '');
var pathA = EDITABLE_APPS_DIR+projectName+"/"+safeFName;
var pathB = EDITABLE_APPS_DIR+projectName+"/"+safeNewFName;
try{
fs.realpathSync(pathA);
try{
fs.realpathSync(pathB);
// if pathB exists, don't do the rename -- it will copy over an existing file!
console.log("trying to rename file to something that already exists: " + pathA + " >> " + pathB);
return res.send("FAIL: Cannot rename a file to something that already exists.");
}catch(ex2){
// ok, all set!
//console.log("all set to rename file: " + pathA + " >> " + pathB);
fs.rename(pathA, pathB, function (err) {
if (err){
console.log(err);
return res.send("FAIL: Error renaming file.");
}
console.log("successfully renamed file ["+req.user.displayName+"]: " + pathA + " >> " + pathB);
return res.send(safeNewFName);
});
}
}catch(ex){
console.log("trying to rename a file that doesn't exist: " + pathA);
return res.send("FAIL: File doesn't exist. Cannot rename it.");
}
}else{
res.send("FAIL: no project and/or filename.");
}
});
app.post("/duplicateFile", function(req, res){
console.log("DUPLICATE FILE ["+req.user.displayName+"]");
if(req.query.project && req.query.project.length > 2 && req.body.fname && req.body.newfname){
var projectName = req.query.project.replace(/\.\./g, "");
var fname = req.body.fname;
if(!fname || fname.length < 2){
return;
}
var newfname = req.body.newfname;
if(!newfname || newfname.length < 2){
return;
}
var safeFName = fname.split("..").join("").replace(/[^a-zA-Z_\.\-0-9\/\(\)]+/g, '');
var safeNewFName = newfname.split("..").join("").replace(/[^a-zA-Z_\.\-0-9\/\(\)]+/g, '');
var pathA = EDITABLE_APPS_DIR+projectName+"/"+safeFName;
var pathB = EDITABLE_APPS_DIR+projectName+"/"+safeNewFName;
try{
fs.realpathSync(pathA);
try{
fs.realpathSync(pathB);
// if pathB exists, don't do the rename -- it will copy over an existing file!
console.log("trying to duplicate file to something that already exists: " + pathA + " >> " + pathB);
return res.send("FAIL: Cannot duplicate a file to something that already exists.");
}catch(ex2){
// ok, all set!
var is = fs.createReadStream(pathA);
var os = fs.createWriteStream(pathB);
util.pump(is, os, function(err){
if (err){
console.log(err);
return res.send("FAIL: Error duplicating file.");
}
console.log("successfully duplicated file ["+req.user.displayName+"]: " + pathA + " >> " + pathB);
return res.send(safeNewFName);
});
}
}catch(ex){
console.log("trying to duplicate a file that doesn't exist: " + pathA);
return res.send("FAIL: File doesn't exist. Cannot duplicate it.");
}
}else{
res.send("FAIL: no project and/or filename.");
}
});
app.get("/allUsersEditingProjectsIFrame", function(req, res){
var html = "<html></head><script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script><head><body><script>";
//html += "var u = "+JSON.stringify(nowUsersList)+";";
html += "function receiveMessage(event){var o = event.origin; var p = parent; $.get('/allUsersEditingProjects', function(data){p.postMessage(JSON.parse(data), o);});};";
html += "window.addEventListener('message', receiveMessage, false);";
html += "</script></body></html>";
res.send(html);
});
app.get("/allUsersEditingProjects", function(req, res){
var nowUsers = everyone.users || {}; //nowjs.server.connected || {};
var nowUsersList = [];
_.each(nowUsers, function(val, name){
var u = (val||{}).user || {};
var a = u.about || {};
a.grouplist = u.grouplist;
nowUsersList.push(a);
});
/*
var html = "<html></head><head><body><script>";
html += "var u = "+JSON.stringify(nowUsersList)+";";
html += "function receiveMessage(event){parent.postMessage(u, event.origin);};";
html += "window.addEventListener('message', receiveMessage, false);";
html += "</script></body></html>";
res.send(html);
*/
res.send(JSON.stringify(nowUsersList));
});
// ------------------------------------------------------------
// ------------------------------------------------------------
var localFileIsMostRecent = []; // an array of flags indicating if the file has been modified since last save.
var nowjs = require("now");
var everyone = nowjs.initialize(app);
// ------ REALTIME NOWJS COLLABORATION ------
//var nowcollab = require("../CHAOS/nowcollab");
//nowcollab.initialize(nowjs, everyone, true);
//-------------------------------------------
nowjs.on('connect', function () {
//console.log("CONNECT > " + this.user.clientId);
this.user.teamID = teamID;
if(this.now.teamID != ''){
this.user.teamID = this.now.teamID;
}
//console.log(this.user);
//console.log(everyone.users);
//console.log(" >> PROJECT="+this.user.teamID);
// hack to get out best guess at the user (since now.js doesn't give us the request object or session!);
var u = {}; //(Auth || {}).getUserFromCache(decodeURIComponent(this.user.cookie['_chaos.auth'])) || {};
// now populate it..
this.user.about = {};
this.user.about._id = u._id || 0;
this.user.about.name = u.nameGiven || u.displayName || this.user.cookie["_username"] || "???";
this.user.about.email = u.emailPrimary || "[email protected]";
// -----
this.now.name = this.user.about.name;
// -----
this.user.grouplist = []; // file groups starts out empty.
addUserToFileGroup(this.user, ""); // the blank file group is the the team group.
this.now.c_confirmProject(this.user.teamID);
});
nowjs.on('disconnect', function () {
//console.log("DISCONNECT > "+this.user.clientId+" >> "+this.user.about.name+" <"+this.user.about.email+">");
//console.log("DISCONNECT > "+this.user.clientId+" >> "+this.now.name);
var teamgroup = nowjs.getGroup(this.user.teamID);
// remove user from all file groups.
if(this.user.grouplist !== undefined){
for(var i=this.user.grouplist.length-1; i>=0; i--){
var g = this.user.grouplist[i];
var fname = g.substring(g.indexOf("/")+1);
usersInGroupMinusMinus(g);
teamgroup.now.c_processUserFileEvent(fname, "leaveFile", this.user.clientId, usersInGroup[g]);
}
}
// finally, remove the user from the team group. (don't need this now since team is also in user.grouplist)
teamgroup.now.c_processUserEvent("leave", this.user.clientId, this.now.name);
});
//---------
// NOW: Remote collab messages.
everyone.now.s_sendCursorUpdate = function(fname, range, changedByUser){
var userObj = this.user;
var filegroup = nowjs.getGroup(userObj.teamID+"/"+fname);
//console.log(filegroup);
filegroup.now.c_updateCollabCursor(this.user.clientId, this.now.name, range, changedByUser);
};
everyone.now.s_sendDiffPatchesToCollaborators = function(fname, patches, crc32){
var userObj = this.user;
localFileIsMostRecent[userObj.teamID+"/"+fname] = false; // mark file as changed.
var filegroup = nowjs.getGroup(userObj.teamID+"/"+fname);
filegroup.now.c_updateWithDiffPatches(this.user.clientId, patches, crc32);
};
// NOW: Remote file tools.
everyone.now.s_getLatestFileContentsAndJoinFileGroup = function(fname, fileRequesterCallback){
var callerID = this.user.clientId;
var userObj = this.user;
addUserToFileGroup(userObj, fname);
//removeUserFromAllFileGroupsAndAddToThis(origUser, fname);
if(localFileIsMostRecent[userObj.teamID+"/"+fname] === true || localFileIsMostRecent[userObj.teamID+"/"+fname] === undefined){
localFileFetch(userObj, fname, fileRequesterCallback);
//console.log("FILE FETCH: " + userObj.teamID + " >> " + fname + ", by user: " + (userObj.about.name || callerID));
}else{
console.log("FILE FETCH (passed to user): " + userObj.teamID + " >> " + fname + ", by user: " + callerID);
var filegroup = nowjs.getGroup(userObj.teamID+"/"+fname);
var users = filegroup.getUsers(function (users) {
var foundUser = false;
for (var i = 0; i < users.length; i++){
if(users[i] != callerID){
// this looks like a valid user to get the file from. :)
console.log("Trying to get file from: " + users[i]);
nowjs.getClient(users[i], function(){
if(this.now === undefined){
console.log("Undefined clientId for requestFullFileFromUserID (using local) >> " + users[i]);
localFileFetch(userObj, fname, fileRequesterCallback);
}else{
this.now.c_userRequestedFullFile(fname, callerID, fileRequesterCallback);
}
});
foundUser = true;
break;
}
}
if(!foundUser){
console.log("Flagged as changed, but no user with file: "+userObj.teamID+" >> "+fname+" >> FETCHING last saved.");
localFileFetch(userObj, fname, fileRequesterCallback);
}
});
}
};
everyone.now.s_saveUserFileContentsToServer = function(fname, fcontents, fileSaverCallback){
localFileSave(this.user, fname, fcontents, fileSaverCallback);
};
//-------
// get rid of this is possible...
everyone.now.s_requestFullFileFromUserID = function(fname, id, fileRequesterCallback){
var callerID = this.user.clientId;
var userObj = this.user;
var filegroup = nowjs.getGroup(userObj.teamID+"/"+fname);
filegroup.hasClient(id, function (bool) {
if (bool) {
//console.log("requesting full file. valid filegroup. :)");
nowjs.getClient(id, function(){
if(this.now === undefined){
console.log("Undefined clientId for requestFullFileFromUserID >> " + id);
}else{
this.now.c_userRequestedFullFile(fname, callerID, fileRequesterCallback);
}
});
}
});
};
//-------
everyone.now.s_teamMessageBroadcast = function(type, message){
var teamgroup = nowjs.getGroup(this.user.teamID);
var scope = "team";
var fromUserId = this.user.clientId;
var fromUserName = this.now.name;
teamgroup.now.c_processMessage(scope, type, message, fromUserId, fromUserName);
};
everyone.now.s_leaveFile = function(fname){
var teamgroup = nowjs.getGroup(this.user.teamID);
var fromUserId = this.user.clientId;
removeUserFromFileGroup(this.user, fname);
};
everyone.now.s_sendUserEvent = function(event){
var teamgroup = nowjs.getGroup(this.user.teamID);
var fromUserId = this.user.clientId;
var fromUserName = this.now.name;
teamgroup.now.c_processUserEvent(event, fromUserId, fromUserName);
};
//-------
everyone.now.s_getAllProjectsFiles = function(callback){
var team = this.user.teamID;
var projectRoot = EDITABLE_APPS_DIR+team;
var walker = walk.walk(projectRoot, {followLinks: false});
var filesAndInfo = [];
walker.on("names", function (root, nodeNamesArray) {
// use this to remove/sort files before doing the more expensive "stat" operation.
//console.log(root + " / " + nodeNamesArray);
for(var i=nodeNamesArray.length-1; i>=0; i--){
if(nodeNamesArray[i] == ".git" || nodeNamesArray[i] == "node_modules" || nodeNamesArray[i] == "_db"){
nodeNamesArray.splice(i, 1);
}
}
});
walker.on("file", function (root, fileStats, next) {
var rt = root.substring(projectRoot.length+1);
if(rt.length > 0){
rt += "/";
}
var fname = rt + fileStats.name;
var sz = fileSizeCache[team+"/"+fname];
if(sz === undefined){
// first time checking files size.. get it!
sz = fileStats.size;
fileSizeCache[team+"/"+fname] = sz;
}
var n = usersInGroup[team+"/"+fname];
if(n){
filesAndInfo.push([fname, n, sz]);
}else{
filesAndInfo.push([fname, 0, sz]);
}
next();
});
walker.on("end", function() {
console.log("Recursively listed project files for: " + team);
// indicate total team members online.
var n = usersInGroup[team];
if(n){
filesAndInfo.push(["", n]);
}else{
filesAndInfo.push(["", 0]);
}
callback(null, filesAndInfo);
});
};
everyone.now.s_createNewFile = function(newFilename, fileCreatorCallback){
localFileCreate(this.user, newFilename, fileCreatorCallback);
};
everyone.now.s_deleteFile = function(fname, fileDeleterCallback){
var usersInFile = usersInGroup[this.user.teamID+fname];
if(usersInFile === undefined || usersInFile === 0){
localFileDelete(this.user, fname, fileDeleterCallback);
}else{
console.log("Cannot delete file. There are users in it! " + this.user.teamID+" >> "+fname);
fileCallback(fname, ["Cannot delete file. There are users in it!"]);
}
};
everyone.now.s_renameFile = function(fname, newFName, fileRenamerCallback){
var usersInFile = usersInGroup[this.user.teamID+fname];
if(usersInFile === undefined || usersInFile === 0){
localFileRename(this.user, fname, newFName, fileRenamerCallback);
}else{
console.log("Cannot rename file. There are users in it! " + this.user.teamID+" >> "+fname);
fileCallback(fname, ["Cannot rename file. There are users in it!"]);
}
};
everyone.now.s_duplicateFile = function(fname, newFName, fileDuplicatorCallback){
localFileDuplicate(this.user, fname, newFName, fileDuplicatorCallback);
};
everyone.now.s_commitProject = function(txt, committerCallback){
var team = this.user.teamID;
console.log("committing project... >> " + team);
var teamProjGitPath = EDITABLE_APPS_DIR+team;
// this only needs done when a new repo is created...
//localRepoInitBare(teamProjGitPath, function(err){});
localRepoCommit(this.user, teamProjGitPath, txt, function(err){
if(err) {
console.log(err);
}
committerCallback(err);
});
};
everyone.now.s_fetchProjectCommits = function(fetcherCallback){
var team = this.user.teamID;
console.log("fetching project commits... >> " + team);
var teamProjGitPath = EDITABLE_APPS_DIR+team;
localRepoFetchGitLog(this.user, teamProjGitPath, "", function(err, gitlog){
if(err) {
console.log(err);
if(err && err[0] && err[0].indexOf("Not a git repository") > 0){
localRepoInitBare(teamProjGitPath, function(err){
if(err){
console.log("ERROR INITITIALIZING GIT REPO.");
}else{
console.log("Returned from git repo init.");
}
});
}
}
fetcherCallback(gitlog);
});
};
everyone.now.s_deployProject = function(txt, deployerCallback){
var team = this.user.teamID;
console.log("DEPLOYING Project >> " + team);
localProjectDeploy(this.user, deployerCallback);
};
//--------
//
// Git Repository management stuff.
//
function localRepoInitBare(gitRepoPath, callback){
var child = exec('git init', {
encoding: 'utf8',
timeout: 30000,
maxBuffer: 200*1024,
killSignal: 'SIGTERM',
cwd: gitRepoPath,
env: null
}, function (error, stdout, stderr) {
if (error !== null) {
console.log('git init exec error: ' + error);
}
console.log("GIT: Init >> " + gitRepoPath);
callback(error);
});
}
function localRepoCommit(userObj, gitRepoPath, message, callback){
var safeMsg = Utf8.encode(message).replace(/\"/g, "\\\"");
var authString = userObj.about.name+" <"+userObj.about.email+">";
var safeAuthString = Utf8.encode(authString).replace(/\"/g, "\\\"");
console.log("GIT: Commit to >> "+gitRepoPath+" by: "+safeAuthString);
var child = exec('git add .; git commit -a --allow-empty --allow-empty-message --author=\"'+safeAuthString+'\" -m \"'+safeMsg+'\";', {
encoding: 'utf8',
timeout: 30000,
maxBuffer: 200*1024,
killSignal: 'SIGTERM',
cwd: gitRepoPath,
env: null
}, function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
}else{
// success! notify team members.
var teamgroup = nowjs.getGroup(userObj.teamID);
var fromUserId = userObj.clientId;
teamgroup.now.c_processUserFileEvent("", "commitProject", fromUserId, 0, "", safeMsg);
}
callback(error);
});
}
function localRepoFetchGitLog(userObj, gitRepoPath, fname, fetcherCallback) {
// TODO: Make the filtering part of the git command, not an after thought with a ton of results.
// Seeing all checkpoints since the beginning of a project could lead to looking at many thousand results...
var authString = userObj.about.name+" <"+userObj.about.email+">";
var safeAuthString = Utf8.encode(authString).replace(/\"/g, "\\\"");
var maxInitialFetch = 10; // hardcoded max value so things don't get crazy until it's explicitly part of the git command...
var maxResults = 5;
var saveThisEntry = false;
var filter = null;
console.log("GIT: Fetch commit logs from >> "+gitRepoPath+" by: "+safeAuthString);
var cmd = "git log -n"+maxInitialFetch+" --numstat --pretty=format:\"commit %H%naname %an%namail %ae%nrdate %ar%nutime %at%ncnote %s\" -- "+fname;
//console.log(cmd);
var child = exec(cmd, {
encoding: 'utf8',
timeout: 30000,
maxBuffer: 200*1024,
killSignal: 'SIGTERM',
cwd: gitRepoPath,
env: null
},
function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
if(fetcherCallback){
fetcherCallback(error, null);
}
}else{
// success! notify team members.
var teamgroup = nowjs.getGroup(userObj.teamID);
var fromUserId = userObj.clientId;
//teamgroup.now.c_processUserFileEvent("", "commitProject", fromUserId, 0, "", safeMsg);
console.log("***** STD OUT *****");
var logLines = stdout.split("\n");
//console.log(logLines);
var out = [];
for(var i=0; i<logLines.length; i++){
var line = logLines[i];
if(line.indexOf("commit") == 0){
// new entry.. first check if we've hit max entries
if(out.length >= maxResults){
break;
}
out.push({}); // start a new array
out[out.length-1]['commit'] = line.substring(8);
saveThisEntry = true;
}
if(saveThisEntry){
if(line.indexOf("aname") == 0){
out[out.length-1]['auth_name'] = line.substring(8);
}
if(line.indexOf("amail") == 0){
out[out.length-1]['auth_email'] = line.substring(8);
}
if(line.indexOf("rdate") == 0){
out[out.length-1]['time_relative'] = line.substring(8);
}
if(line.indexOf("utime") == 0){
out[out.length-1]['time_epoch'] = line.substring(8);
}
if(line.indexOf("cnote") == 0){
var comment = line.substring(8);
out[out.length-1]['comment'] = comment;
if(filter != null){
if(comment.indexOf(filter) < 0){
out.pop();
saveThisEntry = false;
}
}
}
if(line.length > 0 && !isNaN(line.charAt(0)) ){
//echo "numeric line.. counting changes!\n";
var numArray = line.split("\t");
if(numArray.length >= 2 && !isNaN(numArray[0]) && !isNaN(numArray[1]) && numArray[0] != "-" && numArray[1] != "-"){
out[out.length-1]['linesAdded'] = numArray[0];
out[out.length-1]['linesDeleted'] = numArray[1];
}
}
}
}
//console.log(out);
fetcherCallback(null, out);
}
});
}
//
// group management stuff.
//
var usersInGroup = {};
function addUserToFileGroup(userObj, fname){
var groupname = userObj.teamID;
if(fname && fname !== ""){
groupname += "/" + fname;
}
//console.log("ADD TO GROUP: " + groupname);
//console.log(" team: " + userObj.teamID);
//console.log(" fname: " + fname);
var g = nowjs.getGroup(groupname);
if(!g.users[userObj.clientId]){
// user not in group yet.
// add to NOW group.
g.addUser(userObj.clientId);
// add to local group.
userObj.grouplist.push(groupname);
// keep track locally of users in group.
usersInGroupPlusPlus(groupname);
if(fname.length > 0){
var teamgroup = nowjs.getGroup(userObj.teamID);
teamgroup.now.c_processUserFileEvent(fname, "joinFile", userObj.clientId, usersInGroup[groupname]);
}
//console.log("Added user " + user + " to group: " + group);
}else{
console.log("no need to add user " + userObj.clientId + " to group: " + groupname + " ???");
//console.log(g.users[userObj.clientId]);
}
}
function removeUserFromFileGroup(userObj, fname){
var groupname = userObj.teamID;
if(fname && fname !== ""){
groupname += "/" + fname;
}
var g = nowjs.getGroup(groupname);
if(g.users[userObj.clientId]){
// user was in group.
// remove user from NOW group.
g.removeUser(userObj.clientId);
// remove user from local group.
for(var i=userObj.grouplist.length; i>=0; i--){
if(userObj.grouplist[i] == groupname){
userObj.grouplist.splice(i, 1);
}
}
// keep track locally of users in group.
usersInGroupMinusMinus(groupname);
if(fname.length > 0){
var teamgroup = nowjs.getGroup(userObj.teamID);
teamgroup.now.c_processUserFileEvent(fname, "leaveFile", userObj.clientId, usersInGroup[groupname]);
}
//console.log("Removed user " + userObj.clientId + " from: " + groupname);
}else{
//console.log(g);
//console.log("no need to remove user " + userObj.clientId + " from group: " + groupname + " ???");
}
}
function usersInGroupPlusPlus(group){
if(usersInGroup[group]){
usersInGroup[group]++;
}else{
usersInGroup[group] = 1;
}
//console.log("UsersInGroup(+): " + group + " >> " + usersInGroup[group]);
}
function usersInGroupMinusMinus(group){
if(usersInGroup[group]){
usersInGroup[group]--;
}else{
usersInGroup[group] = 0;
}
//console.log("UsersInGroup(-): " + group + " >> " + usersInGroup[group]);
}
//
// local file stuff
//
var fileSizeCache = {};
var fileTodoCache = {};
var fileFixMeCache = {};
function localFileFetch(userObj, fname, fileRequesterCallback){
var team = userObj.teamID;
fs.readFile(EDITABLE_APPS_DIR+team+"/"+fname, "utf-8", function (err, data) {
if (err){
console.warn("couldn't open: "+team+"/"+fname);
}
fileRequesterCallback(fname, data, err, true);
});
}
function localFileSave(userObj, fname, fcontents, fileSaverCallback){
var team = userObj.teamID;
fs.writeFile(EDITABLE_APPS_DIR+team+"/"+fname, fcontents, function(err) {
if(err) {
console.log(err);
} else {
localFileIsMostRecent[team+"/"+fname] = true; // mark file as saved with no pending changes.
console.log("FILE SAVED: " + team+"/"+fname);
var filegroup = nowjs.getGroup(team+"/"+fname);
filegroup.now.c_fileStatusChanged(fname, "saved");
var sz = fcontents.length;
fileSizeCache[team+"/"+fname] = sz;
if(sz < 1000000){
fileTodoCache[team+"/"+fname] = occurrences(fcontents, "TODO");
fileFixMeCache[team+"/"+fname] = occurrences(fcontents, "FIXME");
}
}
fileSaverCallback(err);
});
}
// ---------
function localFileCreate(userObj, fname, fileCreatorCallback){
var team = userObj.teamID;
if(!fname){
return;
}
var safeFName = fname.split("..").join("").replace(/[^a-zA-Z_\.\-0-9\/\(\)]+/g, '');
var path = EDITABLE_APPS_DIR+team+"/"+safeFName;
try{
fs.realpathSync(path);
console.log("file already exists.. no need to create it: " + path);
fileCreatorCallback(safeFName, ["File already exists. No need to create it."]);
}catch(ex){
console.log("file doesn't exist yet. creating it: " + path);
fs.writeFile(path, "", function(err) {
if(err) {
console.log(err);
} else {
localFileIsMostRecent[teamID+safeFName] = true; // mark file as saved with no pending changes.
console.log("FILE SAVED: " + safeFName);
var filegroup = nowjs.getGroup(teamID+safeFName);
filegroup.now.c_fileStatusChanged(safeFName, "saved");
}
var teamgroup = nowjs.getGroup(userObj.teamID);
var fromUserId = userObj.clientId;
teamgroup.now.c_processUserFileEvent(safeFName, "createFile", fromUserId, 0);
fileCreatorCallback(safeFName, err);
});
}
}
function localFileDelete(userObj, fname, fileDeleterCallback){
var team = userObj.teamID;
if(!fname){
return;
}
var safeFName = fname.split("..").join("").replace(/[^a-zA-Z_\.\-0-9\/\(\)]+/g, '');
var path = EDITABLE_APPS_DIR+team+"/"+safeFName;
try{
fs.realpathSync(path);
console.log("all set to delete file: " + path);
fs.unlink(path, function (err) {
if (err) throw err;
console.log("successfully deleted: " + path);
var teamgroup = nowjs.getGroup(userObj.teamID);
var fromUserId = userObj.clientId;
teamgroup.now.c_processUserFileEvent(safeFName, "deleteFile", fromUserId, 0);
fileDeleterCallback(safeFName, []);
});
}catch(ex){
console.log("trying to delete file, but it doesn't exist: " + path);
fileDeleterCallback(safeFName, ["File doesn't exist. No need to delete it."]);
}
}
function localFileRename(userObj, fname, newFName, fileRenamerCallback){
var team = userObj.teamID;
if(!fname || !newFName){
return;
}
var safeFName = fname.split("..").join("").replace(/[^a-zA-Z_\.\-0-9\/\(\)]+/g, '');
var safeNewFName = newFName.split("..").join("").replace(/[^a-zA-Z_\.\-0-9\/\(\)]+/g, '');
var pathA = EDITABLE_APPS_DIR+team+"/"+safeFName;
var pathB = EDITABLE_APPS_DIR+team+"/"+safeNewFName;
try{
fs.realpathSync(pathA);
try{
fs.realpathSync(pathB);
// if pathB exists, don't do the rename -- it will copy over an existing file!
console.log("trying to rename file to something that already exists: " + pathA + " >> " + pathB);
fileRenamerCallback(safeFName, ["Cannot rename a file to something that already exists."]);
}catch(ex2){
// ok, all set!
console.log("all set to rename file: " + pathA + " >> " + pathB);
fs.rename(pathA, pathB, function (err) {
if (err) throw err;
console.log("successfully renamed file: " + pathA + " >> " + pathB);
var teamgroup = nowjs.getGroup(userObj.teamID);
var fromUserId = userObj.clientId;
teamgroup.now.c_processUserFileEvent(safeFName, "renameFile", fromUserId, 0, safeNewFName);
fileRenamerCallback(safeFName, []);
});
}
}catch(ex){
console.log("trying to rename a file that doesn't exist: " + pathA);
fileRenamerCallback(safeFName, ["File doesn't exist. Cannot rename it."]);
}
}
function localFileDuplicate(userObj, fname, newFName, fileDuplicatorCallback){
var team = userObj.teamID;
if(!fname || !newFName){
return;
}
var safeFName = fname.split("..").join("").replace(/[^a-zA-Z_\.\-0-9\/\(\)]+/g, '');
var safeNewFName = newFName.split("..").join("").replace(/[^a-zA-Z_\.\-0-9\/\(\)]+/g, '');
var pathA = EDITABLE_APPS_DIR+team+"/"+safeFName;
var pathB = EDITABLE_APPS_DIR+team+"/"+safeNewFName;
try{
fs.realpathSync(pathA);