decodeString static method
Decode a packet String (JSON data)
@param {String} str @return {Object} packet @api private
Implementation
static decodeString(String str) {
var i = 0;
var endLen = str.length - 1;
// look up type
var p = <String, dynamic>{'type': num.parse(str[0])};
if (null == PacketTypes[p['type']]) {
throw new UnsupportedError("unknown packet type " + p['type']);
}
// look up attachments if type binary
if (BINARY_EVENT == p['type'] || BINARY_ACK == p['type']) {
final start = i + 1;
while (str[++i] != '-' && i != str.length) {}
var buf = str.substring(start, i);
if (buf != '${num.tryParse(buf) ?? -1}' || str[i] != '-') {
throw new ArgumentError('Illegal attachments');
}
p['attachments'] = num.parse(buf);
}
// look up namespace (if any)
if (i < endLen - 1 && '/' == str[i + 1]) {
var start = i + 1;
while (++i > 0) {
if (i == str.length) break;
var c = str[i];
if ("," == c) break;
}
p['nsp'] = str.substring(start, i);
} else {
p['nsp'] = '/';
}
// look up id
var next = i < endLen - 1 ? str[i + 1] : null;
if (next?.isNotEmpty == true && '${num.tryParse(next!)}' == next) {
var start = i + 1;
while (++i > 0) {
var c = str.length > i ? str[i] : null;
if ('${num.tryParse(c!)}' != c) {
--i;
break;
}
if (i == str.length) break;
}
p['id'] = int.tryParse(str.substring(start, i + 1));
}
// look up json data
if (i < endLen - 1 && str[++i].isNotEmpty == true) {
var payload = tryParse(str.substring(i));
if (isPayloadValid(p['type'], payload)) {
p['data'] = payload;
} else {
throw new UnsupportedError("invalid payload");
}
}
// debug('decoded %s as %j', str, p);
return p;
}