From 06e5c8470288fe81807e07c1fbf45e6f88f49cc5 Mon Sep 17 00:00:00 2001 From: Yann Flory Date: Fri, 11 Mar 2016 17:32:45 +0100 Subject: [PATCH] Clean some code --- npm-debug.log | 49 ----------- www/common/realtime-input.js | 159 ++++++++++++++++++----------------- 2 files changed, 81 insertions(+), 127 deletions(-) delete mode 100644 npm-debug.log diff --git a/npm-debug.log b/npm-debug.log deleted file mode 100644 index 036f8d58e..000000000 --- a/npm-debug.log +++ /dev/null @@ -1,49 +0,0 @@ -0 info it worked if it ends with ok -1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe', -1 verbose cli 'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js', -1 verbose cli 'run-script', -1 verbose cli 'lint' ] -2 info using npm@3.6.0 -3 info using node@v5.6.0 -4 verbose run-script [ 'prelint', 'lint', 'postlint' ] -5 info lifecycle cryptpad@0.1.0~prelint: cryptpad@0.1.0 -6 silly lifecycle cryptpad@0.1.0~prelint: no script for prelint, continuing -7 info lifecycle cryptpad@0.1.0~lint: cryptpad@0.1.0 -8 verbose lifecycle cryptpad@0.1.0~lint: unsafe-perm in lifecycle true -9 verbose lifecycle cryptpad@0.1.0~lint: PATH: C:\Program Files\nodejs\node_modules\npm\bin\node-gyp-bin;C:\Users\Yann\Documents\Git projects\cryptpad\node_modules\.bin;C:\Users\Yann\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\local\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Users\Yann\bin;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;E:\apache-maven-3.3.9\bin;C:\Program Files (x86)\Skype\Phone;C:\Program Files (x86)\GNU\GnuPG\pub;C:\Users\Yann\.gnupg;C:\Program Files\nodejs;C:\Users\Yann\AppData\Roaming\npm;C:\Program Files\Git\usr\bin\vendor_perl;C:\Program Files\Git\usr\bin\core_perl -10 verbose lifecycle cryptpad@0.1.0~lint: CWD: C:\Users\Yann\Documents\Git projects\cryptpad -11 silly lifecycle cryptpad@0.1.0~lint: Args: [ '/d /s /c', -11 silly lifecycle 'jshint --config .jshintrc --exclude-path .jshintignore .' ] -12 silly lifecycle cryptpad@0.1.0~lint: Returned: code: 2 signal: null -13 info lifecycle cryptpad@0.1.0~lint: Failed to exec lint script -14 verbose stack Error: cryptpad@0.1.0 lint: `jshint --config .jshintrc --exclude-path .jshintignore .` -14 verbose stack Exit status 2 -14 verbose stack at EventEmitter. (C:\Program Files\nodejs\node_modules\npm\lib\utils\lifecycle.js:232:16) -14 verbose stack at emitTwo (events.js:100:13) -14 verbose stack at EventEmitter.emit (events.js:185:7) -14 verbose stack at ChildProcess. (C:\Program Files\nodejs\node_modules\npm\lib\utils\spawn.js:24:14) -14 verbose stack at emitTwo (events.js:100:13) -14 verbose stack at ChildProcess.emit (events.js:185:7) -14 verbose stack at maybeClose (internal/child_process.js:827:16) -14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5) -15 verbose pkgid cryptpad@0.1.0 -16 verbose cwd C:\Users\Yann\Documents\Git projects\cryptpad -17 error Windows_NT 10.0.10586 -18 error argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run-script" "lint" -19 error node v5.6.0 -20 error npm v3.6.0 -21 error code ELIFECYCLE -22 error cryptpad@0.1.0 lint: `jshint --config .jshintrc --exclude-path .jshintignore .` -22 error Exit status 2 -23 error Failed at the cryptpad@0.1.0 lint script 'jshint --config .jshintrc --exclude-path .jshintignore .'. -23 error Make sure you have the latest version of node.js and npm installed. -23 error If you do, this is most likely a problem with the cryptpad package, -23 error not with npm itself. -23 error Tell the author that this fails on your system: -23 error jshint --config .jshintrc --exclude-path .jshintignore . -23 error You can get information on how to open an issue for this project with: -23 error npm bugs cryptpad -23 error Or if that isn't available, you can get their info via: -23 error npm owner ls cryptpad -23 error There is likely additional logging output above. -24 verbose exit [ 1, true ] diff --git a/www/common/realtime-input.js b/www/common/realtime-input.js index 6e5c23791..fcc8e8612 100644 --- a/www/common/realtime-input.js +++ b/www/common/realtime-input.js @@ -28,6 +28,12 @@ define([ var PARANOIA = true; var module = { exports: {} }; + /** + * If an error is encountered but it is recoverable, do not immediately fail + * but if it keeps firing errors over and over, do fail. + */ + var MAX_RECOVERABLE_ERRORS = 15; + var debug = function (x) { console.log(x); }, warn = function (x) { console.error(x); }, verbose = function (x) { console.log(x); }; @@ -92,7 +98,6 @@ define([ var userName = config.userName; var channel = config.channel; var cryptKey = config.cryptKey; - var passwd = 'y'; // make sure configuration is defined @@ -101,19 +106,35 @@ define([ var doc = config.doc || null; // trying to deprecate onRemote, prefer loading it via the conf - onRemote = config.onRemote || null; - - transformFunction = config.transformFunction || null; + var onRemote = config.onRemote || null; // define this in case it gets called before the rest of our stuff is ready. var onEvent = function () { }; var allMessages = []; var initializing = true; - + var recoverableErrorCount = 0; var bump = function () {}; - var messagesHistory = []; + var chainpadAdapter = {}; + var realtime; + + var parseMessage = function (msg) { + var res ={}; + // two or more? use a for + ['pass','user','channelId','content'].forEach(function(attr){ + var len=msg.slice(0,msg.indexOf(':')), + // taking an offset lets us slice out the prop + // and saves us one string copy + o=len.length+1, + prop=res[attr]=msg.slice(o,Number(len)+o); + // slice off the property and its descriptor + msg = msg.slice(prop.length+o); + }); + // content is the only attribute that's not a string + res.content=JSON.parse(res.content); + return res; + }; var mkMessage = function (user, channel, content) { content = JSON.stringify(content); @@ -122,7 +143,55 @@ define([ content.length + ':' + content; }; - var chainpadAdapter = { + var onPeerMessage = function(toId, type, wc) { + if(type === 6) { + messagesHistory.forEach(function(msg) { + var fromId = chainpadAdapter.historyOut('1:y'+msg) || wc.myID; + wc.sendTo(fromId, toId, '1:y'+msg); + }); + } + }; + + var whoami = new RegExp(userName.replace(/[\/\+]/g, function (c) { + return '\\' +c; + })); + + var onMessage = function(peer, msg) { + + var message = chainpadAdapter.msgIn(peer, msg); + + verbose(message); + allMessages.push(message); + if (!initializing) { + if (PARANOIA) { + onEvent(); + } + } + realtime.message(message); + if (/\[5,/.test(message)) { verbose("pong"); } + + if (!initializing) { + if (/\[2,/.test(message)) { + //verbose("Got a patch"); + if (whoami.test(message)) { + //verbose("Received own message"); + } else { + //verbose("Received remote message"); + // obviously this is only going to get called if + if (onRemote) { onRemote(realtime.getUserDoc()); } + } + } + } + }; + + var onJoining = function(peer) { + }; + + var onLeaving = function(peer) { + chainpadAdapter.leaving(peer); + }; + + chainpadAdapter = { usernamesMapping : {}, msgIn : function(peerId, msg) { var parsed = parseMessage(msg); @@ -158,7 +227,7 @@ define([ }, leaving : function(peerId) { if(this.usernamesMapping[peerId]) { - var chainpadUser = this.usernamesMapping[peerId] + var chainpadUser = this.usernamesMapping[peerId]; onMessage('', '1:y'+mkMessage(chainpadUser, channel, [3,0])); } }, @@ -168,7 +237,7 @@ define([ var value = parsed.user; var obj = this.usernamesMapping; // Find the key (peerId) associated to the chainpad user in the map - var peerId = Object.keys(obj).filter(function(key) {return obj[key] === value})[0]; + var peerId = Object.keys(obj).filter(function(key) {return obj[key] === value;})[0]; return peerId; } return; @@ -180,7 +249,6 @@ define([ }; var rtc = true; - var realtime; if(!getParameterByName("webrtc") || !webrtcUrl) { rtc = false; @@ -211,11 +279,11 @@ define([ wc.onLeaving = onLeaving; // On user leaving the session wc.onPeerMessage = function(peerId, type) { onPeerMessage(peerId, type, wc); - } + }; window.onunload = function() { wc.leave(); - } + }; // Open a Chainpad session realtime = createRealtime(); @@ -249,7 +317,7 @@ define([ // Get the channel history var hc; if(rtc) { - for (let c of wc.channels) { hc = c; break; } + wc.channels.forEach(function (c) { if(!hc) { hc = c; } }); if(hc) { wc.getHistory(hc.peerID); } @@ -298,54 +366,6 @@ define([ joinChannel(); - var whoami = new RegExp(userName.replace(/[\/\+]/g, function (c) { - return '\\' +c; - })); - - var onPeerMessage = function(toId, type, wc) { - if(type === 6) { - messagesHistory.forEach(function(msg) { - var fromId = chainpadAdapter.historyOut('1:y'+msg) || wc.myID; - wc.sendTo(fromId, toId, '1:y'+msg); - }); - } - }; - - var onMessage = function(peer, msg) { - - message = chainpadAdapter.msgIn(peer, msg); - - verbose(message); - allMessages.push(message); - if (!initializing) { - if (PARANOIA) { - onEvent(); - } - } - realtime.message(message); - if (/\[5,/.test(message)) { verbose("pong"); } - - if (!initializing) { - if (/\[2,/.test(message)) { - //verbose("Got a patch"); - if (whoami.test(message)) { - //verbose("Received own message"); - } else { - //verbose("Received remote message"); - // obviously this is only going to get called if - if (onRemote) { onRemote(realtime.getUserDoc()); } - } - } - } - } - - var onJoining = function(peer) { - } - - var onLeaving = function(peer) { - chainpadAdapter.leaving(peer); - } - var checkConnection = function(wc) { if(wc.channels && wc.channels.size > 0) { var channels = Array.from(wc.channels); @@ -373,23 +393,6 @@ define([ } }, 200); } - } - - var parseMessage = function (msg) { - var res ={}; - // two or more? use a for - ['pass','user','channelId','content'].forEach(function(attr){ - var len=msg.slice(0,msg.indexOf(':')), - // taking an offset lets us slice out the prop - // and saves us one string copy - o=len.length+1, - prop=res[attr]=msg.slice(o,Number(len)+o); - // slice off the property and its descriptor - msg = msg.slice(prop.length+o); - }); - // content is the only attribute that's not a string - res.content=JSON.parse(res.content); - return res; }; return {