math = require("math") module.exports = { decode_string: decode_string, decode_varint: decode_varint, encode_big_endian: encode_big_endian, encode: encode, reverse: reverse, } function reverse(buf){ for (var i = 0; i < buf.length/2; i++){ a = buf[i]; b = buf[buf.length-1 - i]; buf[i] = b; buf[buf.length-1 - i] = a; } return buf } function uvarint_size(i){ if (i == 0){ return 0 } for(var j = 1; j < 9; j++) { if ( i < 1< 0xF0){ negate = true } if (negate) { size = size - 0xF0 } i = decode_big_endian(reader, size); if (negate) { i = i * -1} return i } function encode_list(l){ var l2 = l.map(encode); var buf = new Buffer(encode_varint(l2.length)); return Buffer.concat([buf, Buffer.concat(l2)]); } function encode(b){ if (b == null){ return Buffer(0) } else if (typeof b == "number"){ return encode_varint(b) } else if (typeof b == "string"){ return encode_string(b) } else if (Array.isArray(b)){ return encode_list(b) } else{ console.log("UNSUPPORTED TYPE!", typeof b, b) } }