Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if(bodytype === 'xml') {
var xml2js = require('xml2js');
var parser = new xml2js.Parser({explicitArray: false});
parser.parseString(message.utf8Data.toString(), function (err, jsonObj) {
if (err) {
console.log('[nonblocking-async-ws] xml2js parser error');
}
else {
console.log('----> [nonblocking-async-ws] response for notification through mqtt ' + res.headers['x-m2m-rsc']);
connection.close();
}
});
}
else if(bodytype === 'cbor') {
var encoded = message.utf8Data.toString();
cbor.decodeFirst(encoded, function(err, jsonObj) {
if (err) {
console.log('[nonblocking-async-ws] cbor parser error');
}
else {
if (jsonObj.rsc == 2001 || jsonObj.rsc == 2000) {
console.log('----> [nonblocking-async-ws] response for notification through ws ' + jsonObj.rsc);
connection.close();
}
}
});
}
else { // 'json'
var jsonObj = JSON.parse(message.utf8Data.toString());
try {
if (jsonObj.rsc == 2001 || jsonObj.rsc == 2000) {
mserv.on('message', function(topic, buf) {
cbor.decodeFirst(buf, function(error, msg) {
switch (topic) {
case '/admin/request/all':
// Requests are published here by nodes under this broker
try {
adminService(msg, function(rmsg) {
var encode = cbor.encode(rmsg);
mserv.publish('/admin/announce/all', encode);
});
} catch (e) {
console.log("ERROR!: ", e);
}
break;
case '/level/func/request':
// These are requests by the C nodes under this broker
rep.on('data', function(buf) {
console.log("Buffer size " + Buffer.byteLength(buf));
cbor.decodeFirst(buf, function(error, msg) {
// if error != null .. it seems nanomsg is
// receiving more bytes than what the C sent..
// TODO: investigate this problem.
//
processMsg(rep, msg)
console.log("Helllo");
});
});
sock.on('data', function(buf) {
console.log("In new service.. Buffer size " + Buffer.byteLength(buf));
cbor.decodeFirst(buf, function(error, msg) {
console.log(msg);
if (msg["cmd"] == "PING") {
msg["cmd"] = "PONG";
var encode = cbor.encode(msg);
sock.send(encode);
}
});
});
it('should produce output optionally as a cbor buffer', (done) => {
let signedTxn =
ecdsaEncryption.signUpdate(
'/my_txn_family',
{"x": 1, "y": 2},
TEST_WIF,
{output: "cbor"});
assert.isTrue(Buffer.isBuffer(signedTxn));
cbor.decodeFirst(signedTxn, (err, unmarshelled) => {
assert.isNull(err);
assert.equal(unmarshelled.__TYPE__, '/my_txn_family');
assert.isNumber(unmarshelled.__NONCE__);
assert.isNotNull(unmarshelled.__SIGNATURE__);
assert.equal(unmarshelled.Transaction.x, 1);
assert.equal(unmarshelled.Transaction.y, 2);
assert.isNotNull(unmarshelled.Transaction.Signature);
done();
});
});
});
sock.on('data', function(buf) {
console.log("In new service.. Buffer size " + Buffer.byteLength(buf));
cbor.decodeFirst(buf, function(error, msg) {
console.log(msg);
});
});
new Promise((resolve, reject) =>
cbor.decodeFirst(buffer, (err, obj) => err ? reject(err) : resolve(obj)))
sock.on('data', function(buf) {
cbor.decodeFirst(buf, function(error, msg) {
if ((msg["cmd"] === "REXEC") &&
(msg["opt"] === "SYN")) {
runSyncCallback(sock, msg);
}
else
if ((msg["cmd"] === "REXEC") &&
(msg["opt"] === "ASY")) {
runAsyncCallback(sock, msg);
}
else
if ((msg["cmd"] === "REXEC-RES") &&
(msg["opt"] === "GET")) {
getResults(sock, msg);
}
else
if ((msg["cmd"] === "REXEC-RDY") &&
request.headers.rootnm = Object.keys(body_Obj)[0];
request.bodyObj = body_Obj;
callback('1', body_Obj);
}
});
}
catch(e) {
responder.error_result(request, response, 400, 4000, '[parse_to_json] do not parse xml body');
callback('0', body_Obj);
return '0';
}
}
else if (request.usebodytype === 'cbor') {
try {
var encoded = request.body;
cbor.decodeFirst(encoded, function(err, result) {
if (err) {
responder.error_result(request, response, 400, 4000, '[parse_to_json] do not parse cbor body');
callback('0', body_Obj);
return '0';
}
else {
body_Obj = result;
make_short_nametype(body_Obj);
//make_json_arraytype(body_Obj);
request.headers.rootnm = Object.keys(body_Obj)[0];
request.bodyObj = body_Obj;
callback('1', body_Obj);
}
});
}