From a7f3412b5dda067b98cb08516a54343b3e040fd2 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 5 Oct 2013 14:04:39 -0400 Subject: [PATCH 01/12] protocol.js -> weechat-protocol.js This makes it more clear that this script's code parses the WeeChat protocol specifically. --- index.html | 2 +- js/{protocol.js => weechat-protocol.js} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename js/{protocol.js => weechat-protocol.js} (100%) diff --git a/index.html b/index.html index 8c5b1bb..e0880c0 100644 --- a/index.html +++ b/index.html @@ -7,7 +7,7 @@ - + diff --git a/js/protocol.js b/js/weechat-protocol.js similarity index 100% rename from js/protocol.js rename to js/weechat-protocol.js From 9bdcb814ab96fd81fda11c34ee7654ac544bdebf Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 5 Oct 2013 14:24:36 -0400 Subject: [PATCH 02/12] weechat-protocol.js: clean code (indentation/trailing spaces) --- js/websockets.js | 2 +- js/weechat-protocol.js | 310 +++++++++++++++++++++-------------------- 2 files changed, 163 insertions(+), 149 deletions(-) diff --git a/js/websockets.js b/js/websockets.js index df62a1a..6344fa9 100644 --- a/js/websockets.js +++ b/js/websockets.js @@ -324,7 +324,7 @@ weechat.factory('handlers', ['$rootScope', 'colors', 'pluginManager', function($ }]); weechat.factory('connection', ['$rootScope', '$log', 'handlers', 'colors', function($rootScope, $log, handlers, colors) { - protocol = new Protocol(); + protocol = new WeeChatProtocol(); var websocket = null; diff --git a/js/weechat-protocol.js b/js/weechat-protocol.js index ea6dda8..19c79e5 100644 --- a/js/weechat-protocol.js +++ b/js/weechat-protocol.js @@ -1,180 +1,194 @@ -var Protocol = function() { - var self = this; +var WeeChatProtocol = function() { + var self = this; - var getInfo = function() { - var info = {}; - info.key = getString(); - info.value = getString(); - return info; + var getInfo = function() { + var info = {}; + info.key = getString(); + info.value = getString(); + + return info; + }; + + var getHdata = function() { + var paths; + var count; + var objs = []; + var hpath = getString(); + + keys = getString().split(','); + paths = hpath.split('/'); + count = getInt(); + + keys = keys.map(function(key) { + return key.split(':'); + }); + + for (var i = 0; i < count; i++) { + var tmp = {}; + + tmp.pointers = paths.map(function(path) { + return getPointer(); + }); + keys.forEach(function(key) { + tmp[key[0]] = runType(key[1]); + }); + objs.push(tmp); }; - var getHdata = function() { - var paths; - var count; - var objs = []; - var hpath = getString(); + return objs; + }; - + function getPointer() { + var l = getChar(); + var pointer = getSlice(l) + var parsed_data = new Uint8Array(pointer); - keys = getString().split(','); - paths = hpath.split('/'); - count = getInt(); + return _uiatos(parsed_data); + }; - keys = keys.map(function(key) { - return key.split(':'); - }); - var i; - for (i = 0; i < count; i++) { - var tmp = {}; + var _uiatos = function(uia) { + var _str = []; + for (var c = 0; c < uia.length; c++) { + _str[c] = String.fromCharCode(uia[c]); + } - tmp.pointers = paths.map(function(path) { - return getPointer(); - }); + return decodeURIComponent(escape(_str.join(""))); + }; - keys.forEach(function(key) { - tmp[key[0]] = runType(key[1]); - }); - objs.push(tmp); - }; - return objs; - }; + var getInt = function() { + var parsed_data = new Uint8Array(getSlice(4)); + var i = ((parsed_data[0] & 0xff) << 24) | ((parsed_data[1] & 0xff) << 16) | ((parsed_data[2] & 0xff) << 8) | (parsed_data[3] & 0xff); - function getPointer() { - var l = getChar(); + return i; + }; + + var getChar = function() { + var parsed_data = new Uint8Array(getSlice(1)); + + return parsed_data[0]; + }; + + var getString = function() { + var l = getInt(); + + if (l > 0) { + var s = getSlice(l); + var parsed_data = new Uint8Array(s); - var pointer = getSlice(l) - var parsed_data = new Uint8Array(pointer); return _uiatos(parsed_data); - - }; - - var _uiatos =function(uia) { - var _str = []; - for (var c = 0; c < uia.length; c++) { - _str[c] = String.fromCharCode(uia[c]); - } - return decodeURIComponent(escape(_str.join(""))); - }; - - var getInt = function() { - var parsed_data = new Uint8Array(getSlice(4)); - var i = ((parsed_data[0] & 0xff) << 24) | ((parsed_data[1] & 0xff) << 16) | ((parsed_data[2] & 0xff) << 8) | (parsed_data[3] & 0xff); - return i; - - }; - - var getChar = function() { - var parsed_data = new Uint8Array(getSlice(1)); - return parsed_data[0]; - }; - - var getString = function() { - var l = getInt(); - if (l > 0) { - var s = getSlice(l); - var parsed_data = new Uint8Array(s); - return _uiatos(parsed_data); - } - return ""; - }; - - var getSlice = function(length) { - var slice = self.data.slice(0,length); - self.data = self.data.slice(length); - return slice; - }; - - var getType = function() { - var t = getSlice(3); - return _uiatos(new Uint8Array(t)); - }; - - var runType = function(type) { - if (type in types) { - return types[type](); - } - 0; - }; - - var getHeader = function() { - return { - length: getInt(), - compression: getChar(), - } - }; - - var getId = function() { - return getString(); } - var getObject = function() { - var type = getType(); - if (type) { - return object = { - type: type, - content: runType(type), - } + return ""; + }; + + var getSlice = function(length) { + var slice = self.data.slice(0,length); + + self.data = self.data.slice(length); + + return slice; + }; + + var getType = function() { + var t = getSlice(3); + + return _uiatos(new Uint8Array(t)); + }; + + var runType = function(type) { + if (type in types) { + return types[type](); + } + }; + + var getHeader = function() { + var len = getInt(); + var comp = getChar(); + + return { + length: len, + compression: comp, + }; + }; + + var getId = function() { + return getString(); + } + + var getObject = function() { + var type = getType(); + + if (type) { + return object = { + type: type, + content: runType(type), } } + } - self.parse = function(data) { - self.setData(data); - var header = getHeader(); - var id = getId(); - var objects = []; - var object = getObject(); - while(object) { - objects.push(object); - object = getObject(); - } - return { - header: header, - id: id, - objects: objects, - } + self.parse = function(data) { + self.setData(data); + + var header = getHeader(); + var id = getId(); + var objects = []; + var object = getObject(); + + while(object) { + objects.push(object); + object = getObject(); } - self.setData = function (data) { - self.data = data; + return { + header: header, + id: id, + objects: objects, + }; + } + + self.setData = function (data) { + self.data = data; + }; + + function array() { + var type; + var count; + var values; + + type = getType(); + count = getInt(); + values = []; + + for (var i = 0; i < count; i++) { + values.push(runType(type)); }; - function array() { - var type; - var count; - var values; + return values; + } - type = getType(); - count = getInt(); - values = []; - var i; - for (i = 0; i < count; i++) { - values.push(runType(type)); - }; - return values; - } + var types = { + chr: getChar, + "int": getInt, + str: getString, + inf: getInfo, + hda: getHdata, + ptr: getPointer, + lon: getPointer, + tim: getPointer, + buf: getString, + arr: array + }; - var types = { - chr: getChar, - "int": getInt, - str: getString, - inf: getInfo, - hda: getHdata, - ptr: getPointer, - lon: getPointer, - tim: getPointer, - buf: getString, - arr: array - }; //TODO: IMPLEMENT THIS STUFF // chr: this.getChar, // 'int': getInt, - // hacks +// hacks - // hacks +// hacks // htb: getHashtable, // inf: Protocol.getInfo, // inl: getInfolist, // }, -} +}; From 3a9ccc4098a72fdf418d1930c533beca97486a02 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 5 Oct 2013 15:01:51 -0400 Subject: [PATCH 03/12] weechat-protocol.js: refactor with prototype --- js/weechat-protocol.js | 212 ++++++++++++++++++----------------------- 1 file changed, 95 insertions(+), 117 deletions(-) diff --git a/js/weechat-protocol.js b/js/weechat-protocol.js index 19c79e5..91bb0c2 100644 --- a/js/weechat-protocol.js +++ b/js/weechat-protocol.js @@ -1,23 +1,43 @@ var WeeChatProtocol = function() { - var self = this; + this.types = { + 'chr': this.getChar, + 'int': this.getInt, + 'str': this.getString, + 'inf': this.getInfo, + 'hda': this.getHdata, + 'ptr': this.getPointer, + 'lon': this.getPointer, + 'tim': this.getPointer, + 'buf': this.getString, + 'arr': this.getArray + }; +}; +WeeChatProtocol._uiatos = function(uia) { + var _str = []; + for (var c = 0; c < uia.length; c++) { + _str[c] = String.fromCharCode(uia[c]); + } - var getInfo = function() { + return decodeURIComponent(escape(_str.join(""))); +}; +WeeChatProtocol.prototype = { + getInfo: function() { var info = {}; - info.key = getString(); - info.value = getString(); + info.key = this.getString(); + info.value = this.getString(); return info; - }; - - var getHdata = function() { + }, + getHdata: function() { + var self = this; var paths; var count; var objs = []; - var hpath = getString(); + var hpath = this.getString(); - keys = getString().split(','); + keys = this.getString().split(','); paths = hpath.split('/'); - count = getInt(); + count = this.getInt(); keys = keys.map(function(key) { return key.split(':'); @@ -27,116 +47,101 @@ var WeeChatProtocol = function() { var tmp = {}; tmp.pointers = paths.map(function(path) { - return getPointer(); + return self.getPointer(); }); keys.forEach(function(key) { - tmp[key[0]] = runType(key[1]); + tmp[key[0]] = self.runType(key[1]); }); objs.push(tmp); }; return objs; - }; - - function getPointer() { - var l = getChar(); - var pointer = getSlice(l) + }, + getPointer: function() { + var l = this.getChar(); + var pointer = this.getSlice(l) var parsed_data = new Uint8Array(pointer); - return _uiatos(parsed_data); - }; + return WeeChatProtocol._uiatos(parsed_data); + }, + getInt: function() { + var parsed_data = new Uint8Array(this.getSlice(4)); - var _uiatos = function(uia) { - var _str = []; - for (var c = 0; c < uia.length; c++) { - _str[c] = String.fromCharCode(uia[c]); - } - - return decodeURIComponent(escape(_str.join(""))); - }; - - var getInt = function() { - var parsed_data = new Uint8Array(getSlice(4)); - var i = ((parsed_data[0] & 0xff) << 24) | ((parsed_data[1] & 0xff) << 16) | ((parsed_data[2] & 0xff) << 8) | (parsed_data[3] & 0xff); - - return i; - }; - - var getChar = function() { - var parsed_data = new Uint8Array(getSlice(1)); + return ((parsed_data[0] & 0xff) << 24) | + ((parsed_data[1] & 0xff) << 16) | + ((parsed_data[2] & 0xff) << 8) | + (parsed_data[3] & 0xff); + }, + getChar: function() { + var parsed_data = new Uint8Array(this.getSlice(1)); return parsed_data[0]; - }; - - var getString = function() { - var l = getInt(); + }, + getString: function() { + var l = this.getInt(); if (l > 0) { - var s = getSlice(l); + var s = this.getSlice(l); var parsed_data = new Uint8Array(s); - return _uiatos(parsed_data); + return WeeChatProtocol._uiatos(parsed_data); } return ""; - }; + }, + getSlice: function(length) { + var slice = this.data.slice(0,length); - var getSlice = function(length) { - var slice = self.data.slice(0,length); - - self.data = self.data.slice(length); + this.data = this.data.slice(length); return slice; - }; + }, + getType: function() { + var t = this.getSlice(3); - var getType = function() { - var t = getSlice(3); + return WeeChatProtocol._uiatos(new Uint8Array(t)); + }, + runType: function(type) { + var cb = this.types[type]; + var boundCb = cb.bind(this); - return _uiatos(new Uint8Array(t)); - }; - - var runType = function(type) { - if (type in types) { - return types[type](); - } - }; - - var getHeader = function() { - var len = getInt(); - var comp = getChar(); + return boundCb(); + }, + getHeader: function() { + var len = this.getInt(); + var comp = this.getChar(); return { length: len, compression: comp, }; - }; - - var getId = function() { - return getString(); - } - - var getObject = function() { - var type = getType(); + }, + getId: function() { + return this.getString(); + }, + getObject: function() { + var self = this; + var type = this.getType(); if (type) { return object = { type: type, - content: runType(type), + content: self.runType(type), } } - } + }, + parse: function(data) { + var self = this; + this.setData(data); - self.parse = function(data) { - self.setData(data); - - var header = getHeader(); - var id = getId(); + var header = this.getHeader(); + var id = this.getId(); var objects = []; - var object = getObject(); + var object = this.getObject(); - while(object) { + while (object) { objects.push(object); - object = getObject(); + object = self.getObject(); } return { @@ -144,51 +149,24 @@ var WeeChatProtocol = function() { id: id, objects: objects, }; - } - - self.setData = function (data) { - self.data = data; - }; - - function array() { + }, + setData: function (data) { + this.data = data; + }, + getArray: function() { + var self = this; var type; var count; var values; - type = getType(); - count = getInt(); + type = this.getType(); + count = this.getInt(); values = []; for (var i = 0; i < count; i++) { - values.push(runType(type)); + values.push(self.runType(type)); }; return values; } - - var types = { - chr: getChar, - "int": getInt, - str: getString, - inf: getInfo, - hda: getHdata, - ptr: getPointer, - lon: getPointer, - tim: getPointer, - buf: getString, - arr: array - }; - -//TODO: IMPLEMENT THIS STUFF -// chr: this.getChar, -// 'int': getInt, -// hacks - -// hacks -// htb: getHashtable, -// inf: Protocol.getInfo, -// inl: getInfolist, - -// }, - }; From 5a14c1a412053b782fb7b9e4768179560cac76e7 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 5 Oct 2013 15:03:36 -0400 Subject: [PATCH 04/12] weechat-protocol.js: uiatos -> uia2s --- js/weechat-protocol.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/js/weechat-protocol.js b/js/weechat-protocol.js index 91bb0c2..ba65d37 100644 --- a/js/weechat-protocol.js +++ b/js/weechat-protocol.js @@ -12,8 +12,9 @@ var WeeChatProtocol = function() { 'arr': this.getArray }; }; -WeeChatProtocol._uiatos = function(uia) { +WeeChatProtocol._uia2s = function(uia) { var _str = []; + for (var c = 0; c < uia.length; c++) { _str[c] = String.fromCharCode(uia[c]); } @@ -62,7 +63,7 @@ WeeChatProtocol.prototype = { var pointer = this.getSlice(l) var parsed_data = new Uint8Array(pointer); - return WeeChatProtocol._uiatos(parsed_data); + return WeeChatProtocol._uia2s(parsed_data); }, getInt: function() { var parsed_data = new Uint8Array(this.getSlice(4)); @@ -84,7 +85,7 @@ WeeChatProtocol.prototype = { var s = this.getSlice(l); var parsed_data = new Uint8Array(s); - return WeeChatProtocol._uiatos(parsed_data); + return WeeChatProtocol._uia2s(parsed_data); } return ""; @@ -99,7 +100,7 @@ WeeChatProtocol.prototype = { getType: function() { var t = this.getSlice(3); - return WeeChatProtocol._uiatos(new Uint8Array(t)); + return WeeChatProtocol._uia2s(new Uint8Array(t)); }, runType: function(type) { var cb = this.types[type]; From f2b44a4e6b22a5876ea7f5408be2b75747e17543 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 5 Oct 2013 15:06:30 -0400 Subject: [PATCH 05/12] weechat-protocol.js: prepend _ to private methods names --- js/weechat-protocol.js | 106 ++++++++++++++++++++--------------------- 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/js/weechat-protocol.js b/js/weechat-protocol.js index ba65d37..85b081a 100644 --- a/js/weechat-protocol.js +++ b/js/weechat-protocol.js @@ -1,15 +1,15 @@ var WeeChatProtocol = function() { - this.types = { - 'chr': this.getChar, - 'int': this.getInt, - 'str': this.getString, - 'inf': this.getInfo, - 'hda': this.getHdata, - 'ptr': this.getPointer, - 'lon': this.getPointer, - 'tim': this.getPointer, - 'buf': this.getString, - 'arr': this.getArray + this._types = { + 'chr': this._getChar, + 'int': this._getInt, + 'str': this._getString, + 'inf': this._getInfo, + 'hda': this._getHdata, + 'ptr': this._getPointer, + 'lon': this._getPointer, + 'tim': this._getPointer, + 'buf': this._getString, + 'arr': this._getArray }; }; WeeChatProtocol._uia2s = function(uia) { @@ -22,23 +22,23 @@ WeeChatProtocol._uia2s = function(uia) { return decodeURIComponent(escape(_str.join(""))); }; WeeChatProtocol.prototype = { - getInfo: function() { + _getInfo: function() { var info = {}; - info.key = this.getString(); - info.value = this.getString(); + info.key = this._getString(); + info.value = this._getString(); return info; }, - getHdata: function() { + _getHdata: function() { var self = this; var paths; var count; var objs = []; - var hpath = this.getString(); + var hpath = this._getString(); - keys = this.getString().split(','); + keys = this._getString().split(','); paths = hpath.split('/'); - count = this.getInt(); + count = this._getInt(); keys = keys.map(function(key) { return key.split(':'); @@ -48,41 +48,41 @@ WeeChatProtocol.prototype = { var tmp = {}; tmp.pointers = paths.map(function(path) { - return self.getPointer(); + return self._getPointer(); }); keys.forEach(function(key) { - tmp[key[0]] = self.runType(key[1]); + tmp[key[0]] = self._runType(key[1]); }); objs.push(tmp); }; return objs; }, - getPointer: function() { - var l = this.getChar(); - var pointer = this.getSlice(l) + _getPointer: function() { + var l = this._getChar(); + var pointer = this._getSlice(l) var parsed_data = new Uint8Array(pointer); return WeeChatProtocol._uia2s(parsed_data); }, - getInt: function() { - var parsed_data = new Uint8Array(this.getSlice(4)); + _getInt: function() { + var parsed_data = new Uint8Array(this._getSlice(4)); return ((parsed_data[0] & 0xff) << 24) | ((parsed_data[1] & 0xff) << 16) | ((parsed_data[2] & 0xff) << 8) | (parsed_data[3] & 0xff); }, - getChar: function() { - var parsed_data = new Uint8Array(this.getSlice(1)); + _getChar: function() { + var parsed_data = new Uint8Array(this._getSlice(1)); return parsed_data[0]; }, - getString: function() { - var l = this.getInt(); + _getString: function() { + var l = this._getInt(); if (l > 0) { - var s = this.getSlice(l); + var s = this._getSlice(l); var parsed_data = new Uint8Array(s); return WeeChatProtocol._uia2s(parsed_data); @@ -90,59 +90,59 @@ WeeChatProtocol.prototype = { return ""; }, - getSlice: function(length) { + _getSlice: function(length) { var slice = this.data.slice(0,length); this.data = this.data.slice(length); return slice; }, - getType: function() { - var t = this.getSlice(3); + _getType: function() { + var t = this._getSlice(3); return WeeChatProtocol._uia2s(new Uint8Array(t)); }, - runType: function(type) { - var cb = this.types[type]; + _runType: function(type) { + var cb = this._types[type]; var boundCb = cb.bind(this); return boundCb(); }, - getHeader: function() { - var len = this.getInt(); - var comp = this.getChar(); + _getHeader: function() { + var len = this._getInt(); + var comp = this._getChar(); return { length: len, compression: comp, }; }, - getId: function() { - return this.getString(); + _getId: function() { + return this._getString(); }, - getObject: function() { + _getObject: function() { var self = this; - var type = this.getType(); + var type = this._getType(); if (type) { return object = { type: type, - content: self.runType(type), + content: self._runType(type), } } }, parse: function(data) { var self = this; - this.setData(data); + this._setData(data); - var header = this.getHeader(); - var id = this.getId(); + var header = this._getHeader(); + var id = this._getId(); var objects = []; - var object = this.getObject(); + var object = this._getObject(); while (object) { objects.push(object); - object = self.getObject(); + object = self._getObject(); } return { @@ -151,21 +151,21 @@ WeeChatProtocol.prototype = { objects: objects, }; }, - setData: function (data) { + _setData: function (data) { this.data = data; }, - getArray: function() { + _getArray: function() { var self = this; var type; var count; var values; - type = this.getType(); - count = this.getInt(); + type = this._getType(); + count = this._getInt(); values = []; for (var i = 0; i < count; i++) { - values.push(self.runType(type)); + values.push(self._runType(type)); }; return values; From 7011f8f6b98900ed5fde2a3d159579be165cf86a Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 5 Oct 2013 15:07:49 -0400 Subject: [PATCH 06/12] weechat-protocol.js: put similar methods nearby --- js/weechat-protocol.js | 60 +++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/js/weechat-protocol.js b/js/weechat-protocol.js index 85b081a..af40b4c 100644 --- a/js/weechat-protocol.js +++ b/js/weechat-protocol.js @@ -22,6 +22,17 @@ WeeChatProtocol._uia2s = function(uia) { return decodeURIComponent(escape(_str.join(""))); }; WeeChatProtocol.prototype = { + _getType: function() { + var t = this._getSlice(3); + + return WeeChatProtocol._uia2s(new Uint8Array(t)); + }, + _runType: function(type) { + var cb = this._types[type]; + var boundCb = cb.bind(this); + + return boundCb(); + }, _getInfo: function() { var info = {}; info.key = this._getString(); @@ -97,17 +108,6 @@ WeeChatProtocol.prototype = { return slice; }, - _getType: function() { - var t = this._getSlice(3); - - return WeeChatProtocol._uia2s(new Uint8Array(t)); - }, - _runType: function(type) { - var cb = this._types[type]; - var boundCb = cb.bind(this); - - return boundCb(); - }, _getHeader: function() { var len = this._getInt(); var comp = this._getChar(); @@ -131,6 +131,25 @@ WeeChatProtocol.prototype = { } } }, + _getArray: function() { + var self = this; + var type; + var count; + var values; + + type = this._getType(); + count = this._getInt(); + values = []; + + for (var i = 0; i < count; i++) { + values.push(self._runType(type)); + }; + + return values; + }, + _setData: function (data) { + this.data = data; + }, parse: function(data) { var self = this; this._setData(data); @@ -150,24 +169,5 @@ WeeChatProtocol.prototype = { id: id, objects: objects, }; - }, - _setData: function (data) { - this.data = data; - }, - _getArray: function() { - var self = this; - var type; - var count; - var values; - - type = this._getType(); - count = this._getInt(); - values = []; - - for (var i = 0; i < count; i++) { - values.push(self._runType(type)); - }; - - return values; } }; From c12f56c614ea5d2aa2b96af2e28dd4c60831c45c Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 5 Oct 2013 15:10:57 -0400 Subject: [PATCH 07/12] weechat-protocol.js: uniformize code --- js/weechat-protocol.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/js/weechat-protocol.js b/js/weechat-protocol.js index af40b4c..4cb3d3c 100644 --- a/js/weechat-protocol.js +++ b/js/weechat-protocol.js @@ -72,37 +72,37 @@ WeeChatProtocol.prototype = { _getPointer: function() { var l = this._getChar(); var pointer = this._getSlice(l) - var parsed_data = new Uint8Array(pointer); + var parsedData = new Uint8Array(pointer); - return WeeChatProtocol._uia2s(parsed_data); + return WeeChatProtocol._uia2s(parsedData); }, _getInt: function() { - var parsed_data = new Uint8Array(this._getSlice(4)); + var parsedData = new Uint8Array(this._getSlice(4)); - return ((parsed_data[0] & 0xff) << 24) | - ((parsed_data[1] & 0xff) << 16) | - ((parsed_data[2] & 0xff) << 8) | - (parsed_data[3] & 0xff); + return ((parsedData[0] & 0xff) << 24) | + ((parsedData[1] & 0xff) << 16) | + ((parsedData[2] & 0xff) << 8) | + (parsedData[3] & 0xff); }, _getChar: function() { - var parsed_data = new Uint8Array(this._getSlice(1)); + var parsedData = new Uint8Array(this._getSlice(1)); - return parsed_data[0]; + return parsedData[0]; }, _getString: function() { var l = this._getInt(); if (l > 0) { var s = this._getSlice(l); - var parsed_data = new Uint8Array(s); + var parsedData = new Uint8Array(s); - return WeeChatProtocol._uia2s(parsed_data); + return WeeChatProtocol._uia2s(parsedData); } return ""; }, _getSlice: function(length) { - var slice = this.data.slice(0,length); + var slice = this.data.slice(0, length); this.data = this.data.slice(length); @@ -125,10 +125,10 @@ WeeChatProtocol.prototype = { var type = this._getType(); if (type) { - return object = { + return { type: type, content: self._runType(type), - } + }; } }, _getArray: function() { From df7d21f63f21d821757676f0bc7dae84b52e9eac Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 5 Oct 2013 15:20:42 -0400 Subject: [PATCH 08/12] weechat-protocol.js: use pointer instead of slicing I don't know if this changes anything to performance, but my instinct tells me that it's better to update a pointer within a big array to get slices than creating a new slice each time. Perhaps the JS engine is clever enough to not create a copy each time and only update its internal references of the array bounds, but just in case... --- js/weechat-protocol.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/js/weechat-protocol.js b/js/weechat-protocol.js index 4cb3d3c..672bec7 100644 --- a/js/weechat-protocol.js +++ b/js/weechat-protocol.js @@ -102,9 +102,9 @@ WeeChatProtocol.prototype = { return ""; }, _getSlice: function(length) { - var slice = this.data.slice(0, length); + var slice = this._data.slice(this._dataAt, this._dataAt + length); - this.data = this.data.slice(length); + this._dataAt += length; return slice; }, @@ -148,11 +148,13 @@ WeeChatProtocol.prototype = { return values; }, _setData: function (data) { - this.data = data; + this._data = data; }, parse: function(data) { var self = this; + this._setData(data); + this._dataAt = 0; var header = this._getHeader(); var id = this._getId(); From 0c02cae6a9e2ab9166f4912d377cf6eca4df3ae2 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 5 Oct 2013 15:29:18 -0400 Subject: [PATCH 09/12] weechat-protocol.js: clean _uia2s() --- js/weechat-protocol.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/js/weechat-protocol.js b/js/weechat-protocol.js index 672bec7..b80bcac 100644 --- a/js/weechat-protocol.js +++ b/js/weechat-protocol.js @@ -13,13 +13,13 @@ var WeeChatProtocol = function() { }; }; WeeChatProtocol._uia2s = function(uia) { - var _str = []; + var str = []; for (var c = 0; c < uia.length; c++) { - _str[c] = String.fromCharCode(uia[c]); + str.push(String.fromCharCode(uia[c])); } - return decodeURIComponent(escape(_str.join(""))); + return decodeURIComponent(escape(str.join(''))); }; WeeChatProtocol.prototype = { _getType: function() { From fef3e7542a7f453a162d7742a9747cd98ce63476 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 5 Oct 2013 15:32:32 -0400 Subject: [PATCH 10/12] weechat-protocol.js: move getSlice() --- js/weechat-protocol.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/js/weechat-protocol.js b/js/weechat-protocol.js index b80bcac..90c2e23 100644 --- a/js/weechat-protocol.js +++ b/js/weechat-protocol.js @@ -101,13 +101,6 @@ WeeChatProtocol.prototype = { return ""; }, - _getSlice: function(length) { - var slice = this._data.slice(this._dataAt, this._dataAt + length); - - this._dataAt += length; - - return slice; - }, _getHeader: function() { var len = this._getInt(); var comp = this._getChar(); @@ -147,6 +140,13 @@ WeeChatProtocol.prototype = { return values; }, + _getSlice: function(length) { + var slice = this._data.slice(this._dataAt, this._dataAt + length); + + this._dataAt += length; + + return slice; + }, _setData: function (data) { this._data = data; }, From de163d6bc9eda523aedf85454389fda1cc027715 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 5 Oct 2013 15:53:40 -0400 Subject: [PATCH 11/12] weechat-protocol.js: add time parsing and factor "number as string" code --- js/weechat-protocol.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/js/weechat-protocol.js b/js/weechat-protocol.js index 90c2e23..8bbf159 100644 --- a/js/weechat-protocol.js +++ b/js/weechat-protocol.js @@ -6,8 +6,8 @@ var WeeChatProtocol = function() { 'inf': this._getInfo, 'hda': this._getHdata, 'ptr': this._getPointer, - 'lon': this._getPointer, - 'tim': this._getPointer, + 'lon': this._getStrNumber, + 'tim': this._getTime, 'buf': this._getString, 'arr': this._getArray }; @@ -33,6 +33,12 @@ WeeChatProtocol.prototype = { return boundCb(); }, + _getStrNumber: function() { + var len = new Uint8Array(this._getSlice(1))[0]; + var str = this._getSlice(len); + + return WeeChatProtocol._uia2s(new Uint8Array(str)); + }, _getInfo: function() { var info = {}; info.key = this._getString(); @@ -70,11 +76,12 @@ WeeChatProtocol.prototype = { return objs; }, _getPointer: function() { - var l = this._getChar(); - var pointer = this._getSlice(l) - var parsedData = new Uint8Array(pointer); - - return WeeChatProtocol._uia2s(parsedData); + return this._getStrNumber(); + }, + _getTime: function() { + var str = this._getStrNumber(); + + return new Date(parseInt(str)); }, _getInt: function() { var parsedData = new Uint8Array(this._getSlice(4)); From 74192c3df46ba27216f396a8ce51d4f9f7e55358 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 5 Oct 2013 16:42:44 -0400 Subject: [PATCH 12/12] weechat-protocol.js: implement hash table parsing --- js/weechat-protocol.js | 51 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/js/weechat-protocol.js b/js/weechat-protocol.js index 8bbf159..b2829ae 100644 --- a/js/weechat-protocol.js +++ b/js/weechat-protocol.js @@ -9,7 +9,15 @@ var WeeChatProtocol = function() { 'lon': this._getStrNumber, 'tim': this._getTime, 'buf': this._getString, - 'arr': this._getArray + 'arr': this._getArray, + 'htb': this._getHashTable + }; + this._typesStr = { + 'chr': this._strDirect, + 'str': this._strDirect, + 'int': this._strToString, + 'tim': this._strToString, + 'ptr': this._strDirect }; }; WeeChatProtocol._uia2s = function(uia) { @@ -34,11 +42,23 @@ WeeChatProtocol.prototype = { return boundCb(); }, _getStrNumber: function() { - var len = new Uint8Array(this._getSlice(1))[0]; + var len = this._getByte(); var str = this._getSlice(len); return WeeChatProtocol._uia2s(new Uint8Array(str)); }, + _strDirect: function(obj) { + return obj; + }, + _strToString: function(obj) { + return obj.toString(); + }, + _objToString: function(obj, type) { + var cb = this._typesStr[type]; + var boundCb = cb.bind(this); + + return boundCb(obj); + }, _getInfo: function() { var info = {}; info.key = this._getString(); @@ -80,7 +100,7 @@ WeeChatProtocol.prototype = { }, _getTime: function() { var str = this._getStrNumber(); - + return new Date(parseInt(str)); }, _getInt: function() { @@ -91,11 +111,14 @@ WeeChatProtocol.prototype = { ((parsedData[2] & 0xff) << 8) | (parsedData[3] & 0xff); }, - _getChar: function() { + _getByte: function() { var parsedData = new Uint8Array(this._getSlice(1)); return parsedData[0]; }, + _getChar: function() { + return String.fromCharCode(this._getByte()); + }, _getString: function() { var l = this._getInt(); @@ -110,7 +133,7 @@ WeeChatProtocol.prototype = { }, _getHeader: function() { var len = this._getInt(); - var comp = this._getChar(); + var comp = this._getByte(); return { length: len, @@ -131,6 +154,24 @@ WeeChatProtocol.prototype = { }; } }, + _getHashTable: function() { + var self = this; + var typeKeys, typeValues, count; + var dict = {}; + + typeKeys = this._getType(); + typeValues = this._getType(); + count = this._getInt(); + + for (var i = 0; i < count; ++i) { + var key = self._runType(typeKeys); + var keyStr = self._objToString(key, typeKeys); + var value = self.runType(typeValues); + dict[keyStr] = value; + } + + return dict; + }, _getArray: function() { var self = this; var type;