process.exit(-1);
}
// create GPB object from payload
let gpbObj1 = TestRespGPBDef.create(payload);
utils.printObj(`Gpb obj1`,gpbObj1);
// encode GPB into a byte buffer
let buff1 = TestRespGPBDef.encode(gpbObj1).finish();
utils.printObj(`\ngmsg encodded as buff (len ${buff1.length})`, buff1);
// we should be able to decode buffer back into a GPB objerct
let gpbObj2 = TestRespGPBDef.decode(buff1);
utils.printObj(`sanity check, Gpb obj2`, gpbObj2);
// we need to convert to a string before sending to an AMQ stream
// supposed to encode an array to utf8 string, but eh not completely
let gmsgAsStr = buff1.toString('binary');
//let gmsgAsStr = buff1.toString('utf8');
console.log(`\n>>>>>>>gmsg as string (len ${gmsgAsStr.length}) [${gmsgAsStr}]`);
utils.printObj(`gmsg as string (len ${gmsgAsStr.length})`,gmsgAsStr);
let teArray = new TextEncoder().encode(gmsgAsStr);
console.log(`gmsg str encoded by node (len ${teArray.length})`,teArray);
//let barray = utils.stringToBinArray(gmsgAsStr);
//console.log(`gmsg as supposed binary array (len ${barray.length})`, barray);
// now lets take the string and try to reconstruct the GPB object
// from a string, first we need to create a buffer
let buff2 = Buffer.from(gmsgAsStr,'binary'); // ERROR RangeError: index out of range: 47 + 4 > 49
//let buff2 = Buffer.from(gmsgAsStr); // ERROR Error: invalid wire type 7 at offset 49
utils.printObj(`\nbuff2 from string (len ${buff2.length})`, buff2);
// and from the buffer we should be able to re-create gpb obj
let gpbObj3 = TestRespGPBDef.decode(buff2);
utils.printObj(`\nsanity check 3, resulting GPB`,gpbObj3);
console.log('w00t!');
}
/***/
async function asyncLoadGBP(protoDir, protoFile, protoMsgName) {
return new Promise( ((resolve,reject) => {
let retGpbTypeDef = null;
let gpbAbsFName = protoDir + '/' + protoFile;
protobuf.load(gpbAbsFName, function(err, root) {
if (err) {
let e = `ERROR LOADING GBP [${gpbAbsFName}] + ${utils.getObjAsString(err)}]`;
console.log(e);
reject(e);
}
console.log(`GPB [${gpbAbsFName}] loaded ok.`);
try {
retGpbTypeDef = root.lookupType(protoMsgName);
console.log(`GPB Message Type [${protoMsgName}] look up ok.`);
} catch( luExc) {
let e = `ERROR LOOKING UP GBP TYPE [${protoMsgName}] [${utils.getObjAsString(luExc)}]`;
console.log(e);
reject(e);
}
resolve(retGpbTypeDef);
});
})); // end promise
}