MCPcopy Create free account
hub / github.com/CloudBoost/cloudboost / decodeString

Function decodeString

sdk/src/CloudSocketClientLib.js:6685–6751  ·  view source on GitHub ↗

* Decode a packet String (JSON data) * * @param {String} str * @return {Object} packet * @api private

(str)

Source from the content-addressed store, hash-verified

6683 */
6684
6685 function decodeString(str) {
6686 var p = {};
6687 var i = 0;
6688
6689 // look up type
6690 p.type = Number(str.charAt(0));
6691 if (null == exports.types[p.type])
6692 return error();
6693
6694 // look up attachments if type binary
6695 if (exports.BINARY_EVENT == p.type || exports.BINARY_ACK == p.type) {
6696 var buf = '';
6697 while (str.charAt(++i) != '-') {
6698 buf += str.charAt(i);
6699 if (i == str.length)
6700 break;
6701 }
6702 if (buf != Number(buf) || str.charAt(i) != '-') {
6703 throw new Error('Illegal attachments');
6704 }
6705 p.attachments = Number(buf);
6706 }
6707
6708 // look up namespace (if any)
6709 if ('/' == str.charAt(i + 1)) {
6710 p.nsp = '';
6711 while (++i) {
6712 var c = str.charAt(i);
6713 if (',' == c)
6714 break;
6715 p.nsp += c;
6716 if (i == str.length)
6717 break;
6718 }
6719 } else {
6720 p.nsp = '/';
6721 }
6722
6723 // look up id
6724 var next = str.charAt(i + 1);
6725 if ('' !== next && Number(next) == next) {
6726 p.id = '';
6727 while (++i) {
6728 var c = str.charAt(i);
6729 if (null == c || Number(c) != c) {
6730 --i;
6731 break;
6732 }
6733 p.id += str.charAt(i);
6734 if (i == str.length)
6735 break;
6736 }
6737 p.id = Number(p.id);
6738 }
6739
6740 // look up json data
6741 if (str.charAt(++i)) {
6742 try {

Callers 1

Calls 2

errorFunction · 0.70
debugFunction · 0.70

Tested by

no test coverage detected