From 5d9c16ffbfeb9134dc968fdd7b64e065e2e5d5bf Mon Sep 17 00:00:00 2001 From: Mark Hindess Date: Mon, 8 Sep 2014 21:10:06 +0100 Subject: [PATCH] Remove redundant msg != null checks. --- nodes/core/core/75-exec.js | 96 +++++++++++++++---------------- nodes/core/core/80-function.js | 52 ++++++++--------- nodes/core/core/80-template.js | 14 ++--- nodes/core/io/10-mqtt.js | 30 +++++----- nodes/core/social/27-twitter.js | 81 ++++++++++++-------------- nodes/core/social/61-email.js | 38 ++++++------ nodes/core/storage/65-redisout.js | 34 ++++++----- 7 files changed, 163 insertions(+), 182 deletions(-) diff --git a/nodes/core/core/75-exec.js b/nodes/core/core/75-exec.js index c2e75a1cf..a07b14012 100644 --- a/nodes/core/core/75-exec.js +++ b/nodes/core/core/75-exec.js @@ -27,60 +27,56 @@ module.exports = function(RED) { var node = this; this.on("input", function(msg) { - if (msg != null) { - node.status({fill:"blue",shape:"dot"}); - if (this.useSpawn === true) { - // make the extra args into an array - // then prepend with the msg.payload - if (typeof(msg.payload !== "string")) { msg.payload = msg.payload.toString(); } - var arg = []; - if (node.append.length > 0) { arg = node.append.split(","); } - if (msg.payload.trim() !== "") { arg.unshift(msg.payload); } - node.log(node.cmd+" ["+arg+"]"); - if (node.cmd.indexOf(" ") == -1) { - var ex = spawn(node.cmd,arg); - ex.stdout.on('data', function (data) { - //console.log('[exec] stdout: ' + data); - msg.payload = data.toString(); - node.send([msg,null,null]); - }); - ex.stderr.on('data', function (data) { - //console.log('[exec] stderr: ' + data); - msg.payload = data.toString(); - node.send([null,msg,null]); - }); - ex.on('close', function (code) { - //console.log('[exec] result: ' + code); - msg.payload = code; - node.status({}); - node.send([null,null,msg]); - }); - ex.on('error', function (code) { - node.warn(code); - }); - } - else { node.error("Spawn command must be just the command - no spaces or extra parameters"); } - } - - else { - var cl = node.cmd+" "+msg.payload+" "+node.append; - node.log(cl); - var child = exec(cl, function (error, stdout, stderr) { - msg.payload = stdout; - var msg2 = {payload:stderr}; - var msg3 = null; - //console.log('[exec] stdout: ' + stdout); - //console.log('[exec] stderr: ' + stderr); - if (error !== null) { - msg3 = {payload:error}; - //console.log('[exec] error: ' + error); - } + node.status({fill:"blue",shape:"dot"}); + if (this.useSpawn === true) { + // make the extra args into an array + // then prepend with the msg.payload + if (typeof(msg.payload !== "string")) { msg.payload = msg.payload.toString(); } + var arg = []; + if (node.append.length > 0) { arg = node.append.split(","); } + if (msg.payload.trim() !== "") { arg.unshift(msg.payload); } + node.log(node.cmd+" ["+arg+"]"); + if (node.cmd.indexOf(" ") == -1) { + var ex = spawn(node.cmd,arg); + ex.stdout.on('data', function (data) { + //console.log('[exec] stdout: ' + data); + msg.payload = data.toString(); + node.send([msg,null,null]); + }); + ex.stderr.on('data', function (data) { + //console.log('[exec] stderr: ' + data); + msg.payload = data.toString(); + node.send([null,msg,null]); + }); + ex.on('close', function (code) { + //console.log('[exec] result: ' + code); + msg.payload = code; node.status({}); - node.send([msg,msg2,msg3]); + node.send([null,null,msg]); + }); + ex.on('error', function (code) { + node.warn(code); }); } + else { node.error("Spawn command must be just the command - no spaces or extra parameters"); } + } + else { + var cl = node.cmd+" "+msg.payload+" "+node.append; + node.log(cl); + var child = exec(cl, function (error, stdout, stderr) { + msg.payload = stdout; + var msg2 = {payload:stderr}; + var msg3 = null; + //console.log('[exec] stdout: ' + stdout); + //console.log('[exec] stderr: ' + stderr); + if (error !== null) { + msg3 = {payload:error}; + //console.log('[exec] error: ' + error); + } + node.status({}); + node.send([msg,msg2,msg3]); + }); } - }); } diff --git a/nodes/core/core/80-function.js b/nodes/core/core/80-function.js index 0a701a113..223e6a60b 100644 --- a/nodes/core/core/80-function.js +++ b/nodes/core/core/80-function.js @@ -39,38 +39,36 @@ module.exports = function(RED) { try { this.script = vm.createScript(functionText); this.on("input", function(msg) { - if (msg != null) { - try { - var start = process.hrtime(); - context.msg = msg; - this.script.runInContext(context); - var results = context.results; - if (results == null) { - results = []; - } else if (results.length == null) { - results = [results]; - } - if (msg._topic) { - for (var m in results) { - if (results[m]) { - if (util.isArray(results[m])) { - for (var n=0; n < results[m].length; n++) { - results[m][n]._topic = msg._topic; - } - } else { - results[m]._topic = msg._topic; + try { + var start = process.hrtime(); + context.msg = msg; + this.script.runInContext(context); + var results = context.results; + if (results == null) { + results = []; + } else if (results.length == null) { + results = [results]; + } + if (msg._topic) { + for (var m in results) { + if (results[m]) { + if (util.isArray(results[m])) { + for (var n=0; n < results[m].length; n++) { + results[m][n]._topic = msg._topic; } + } else { + results[m]._topic = msg._topic; } } } - this.send(results); - var duration = process.hrtime(start); - if (process.env.NODE_RED_FUNCTION_TIME) { - this.status({fill:"yellow",shape:"dot",text:""+Math.floor((duration[0]* 1e9 + duration[1])/10000)/100}); - } - } catch(err) { - this.error(err.toString()); } + this.send(results); + var duration = process.hrtime(start); + if (process.env.NODE_RED_FUNCTION_TIME) { + this.status({fill:"yellow",shape:"dot",text:""+Math.floor((duration[0]* 1e9 + duration[1])/10000)/100}); + } + } catch(err) { + this.error(err.toString()); } }); } catch(err) { diff --git a/nodes/core/core/80-template.js b/nodes/core/core/80-template.js index 0059f5d51..9c818dcec 100644 --- a/nodes/core/core/80-template.js +++ b/nodes/core/core/80-template.js @@ -47,14 +47,12 @@ module.exports = function(RED) { } node.on("input", function(msg) { - if (msg != null) { - try { - m = msg; - i = 0; - rec(msg); - } catch(err) { - node.error(err.message); - } + try { + m = msg; + i = 0; + rec(msg); + } catch(err) { + node.error(err.message); } }); } diff --git a/nodes/core/io/10-mqtt.js b/nodes/core/io/10-mqtt.js index 267a5368a..c8bc49015 100644 --- a/nodes/core/io/10-mqtt.js +++ b/nodes/core/io/10-mqtt.js @@ -82,24 +82,22 @@ module.exports = function(RED) { this.client = connectionPool.get(this.brokerConfig.broker,this.brokerConfig.port,this.brokerConfig.clientid,this.brokerConfig.username,this.brokerConfig.password); var node = this; this.on("input",function(msg) { - if (msg != null) { - if (msg.qos) { - msg.qos = parseInt(msg.qos); - if ((msg.qos !== 0) && (msg.qos !== 1) && (msg.qos !== 2)) { - msg.qos = null; - } + if (msg.qos) { + msg.qos = parseInt(msg.qos); + if ((msg.qos !== 0) && (msg.qos !== 1) && (msg.qos !== 2)) { + msg.qos = null; } - msg.qos = Number(node.qos || msg.qos || 0); - msg.retain = node.retain || msg.retain || false; - msg.retain = ((msg.retain === true) || (msg.retain === "true")) || false; - if (node.topic) { - msg.topic = node.topic; - } - if ((msg.hasOwnProperty("topic")) && (typeof msg.topic === "string") && (msg.topic !== "")) { // topic must exist - this.client.publish(msg); // send the message - } - else { node.warn("Invalid topic specified"); } } + msg.qos = Number(node.qos || msg.qos || 0); + msg.retain = node.retain || msg.retain || false; + msg.retain = ((msg.retain === true) || (msg.retain === "true")) || false; + if (node.topic) { + msg.topic = node.topic; + } + if ((msg.hasOwnProperty("topic")) && (typeof msg.topic === "string") && (msg.topic !== "")) { // topic must exist + this.client.publish(msg); // send the message + } + else { node.warn("Invalid topic specified"); } }); this.client.on("connectionlost",function() { node.status({fill:"red",shape:"ring",text:"disconnected"}); diff --git a/nodes/core/social/27-twitter.js b/nodes/core/social/27-twitter.js index 30118edc4..5cacd9eba 100644 --- a/nodes/core/social/27-twitter.js +++ b/nodes/core/social/27-twitter.js @@ -244,52 +244,47 @@ module.exports = function(RED) { access_token_secret: credentials.access_token_secret }); node.on("input", function(msg) { - if (msg != null) { - - node.status({fill:"blue",shape:"dot",text:"tweeting"}); - - if (msg.payload.length > 140) { - msg.payload = msg.payload.slice(0,139); - node.warn("Tweet greater than 140 : truncated"); - } - - if (msg.media && Buffer.isBuffer(msg.media)) { - var apiUrl = "https://api.twitter.com/1.1/statuses/update_with_media.json"; - var signedUrl = oa.signUrl(apiUrl, - credentials.access_token, - credentials.access_token_secret, - "POST"); - - var r = request.post(signedUrl,function(err,httpResponse,body) { - if (err) { - node.error(err.toString()); + node.status({fill:"blue",shape:"dot",text:"tweeting"}); + + if (msg.payload.length > 140) { + msg.payload = msg.payload.slice(0,139); + node.warn("Tweet greater than 140 : truncated"); + } + + if (msg.media && Buffer.isBuffer(msg.media)) { + var apiUrl = "https://api.twitter.com/1.1/statuses/update_with_media.json"; + var signedUrl = oa.signUrl(apiUrl, + credentials.access_token, + credentials.access_token_secret, + "POST"); + + var r = request.post(signedUrl,function(err,httpResponse,body) { + if (err) { + node.error(err.toString()); + node.status({fill:"red",shape:"ring",text:"failed"}); + } else { + var response = JSON.parse(body); + if (body.errors) { + var errorList = body.errors.map(function(er) { return er.code+": "+er.message }).join(", "); + node.error("tweet failed: "+errorList); node.status({fill:"red",shape:"ring",text:"failed"}); } else { - var response = JSON.parse(body); - if (body.errors) { - var errorList = body.errors.map(function(er) { return er.code+": "+er.message }).join(", "); - node.error("tweet failed: "+errorList); - node.status({fill:"red",shape:"ring",text:"failed"}); - } else { - node.status({}); - } + node.status({}); } - }); - var form = r.form(); - form.append("status",msg.payload); - form.append("media[]",msg.media,{filename:"image"}); - - - - } else { - twit.updateStatus(msg.payload, function (err, data) { - if (err) { - node.status({fill:"red",shape:"ring",text:"failed"}); - node.error(err); - } - node.status({}); - }); - } + } + }); + var form = r.form(); + form.append("status",msg.payload); + form.append("media[]",msg.media,{filename:"image"}); + + } else { + twit.updateStatus(msg.payload, function (err, data) { + if (err) { + node.status({fill:"red",shape:"ring",text:"failed"}); + node.error(err); + } + node.status({}); + }); } }); } diff --git a/nodes/core/social/61-email.js b/nodes/core/social/61-email.js index c910b17ff..7d0f8cb93 100644 --- a/nodes/core/social/61-email.js +++ b/nodes/core/social/61-email.js @@ -69,27 +69,25 @@ module.exports = function(RED) { }); this.on("input", function(msg) { - if (msg != null) { - if (smtpTransport) { - node.status({fill:"blue",shape:"dot",text:"sending"}); - var payload = RED.util.ensureString(msg.payload); - smtpTransport.sendMail({ - from: node.userid, // sender address - to: msg.to || node.name, // comma separated list of addressees - subject: msg.topic, // subject line - text: payload // plaintext body - }, function(error, info) { - if (error) { - node.error(error); - node.status({fill:"red",shape:"ring",text:"send failed"}); - } else { - node.log("Message sent: " + info.response); - node.status({}); - } - }); - } - else { node.warn("No Email credentials found. See info panel."); } + if (smtpTransport) { + node.status({fill:"blue",shape:"dot",text:"sending"}); + var payload = RED.util.ensureString(msg.payload); + smtpTransport.sendMail({ + from: node.userid, // sender address + to: msg.to || node.name, // comma separated list of addressees + subject: msg.topic, // subject line + text: payload // plaintext body + }, function(error, info) { + if (error) { + node.error(error); + node.status({fill:"red",shape:"ring",text:"send failed"}); + } else { + node.log("Message sent: " + info.response); + node.status({}); + } + }); } + else { node.warn("No Email credentials found. See info panel."); } }); } RED.nodes.registerType("e-mail",EmailNode,{ diff --git a/nodes/core/storage/65-redisout.js b/nodes/core/storage/65-redisout.js index 6f51bd311..907e2a558 100644 --- a/nodes/core/storage/65-redisout.js +++ b/nodes/core/storage/65-redisout.js @@ -79,27 +79,25 @@ module.exports = function(RED) { }); this.on("input", function(msg) { - if (msg != null) { - var k = this.key || msg.topic; - if (k) { - if (this.structtype == "string") { - this.client.set(k,RED.util.ensureString(msg.payload)); - } else if (this.structtype == "hash") { - var r = hashFieldRE.exec(msg.payload); - if (r) { - this.client.hset(k,r[1],r[2]); - } else { - this.warn("Invalid payload for redis hash"); - } - } else if (this.structtype == "set") { - this.client.sadd(k,msg.payload); - } else if (this.structtype == "list") { - this.client.rpush(k,msg.payload); - } + var k = this.key || msg.topic; + if (k) { + if (this.structtype == "string") { + this.client.set(k,RED.util.ensureString(msg.payload)); + } else if (this.structtype == "hash") { + var r = hashFieldRE.exec(msg.payload); + if (r) { + this.client.hset(k,r[1],r[2]); } else { - this.warn("No key or topic set"); + this.warn("Invalid payload for redis hash"); } + } else if (this.structtype == "set") { + this.client.sadd(k,msg.payload); + } else if (this.structtype == "list") { + this.client.rpush(k,msg.payload); } + } else { + this.warn("No key or topic set"); + } }); this.on("close", function() { redisConnectionPool.close(node.client);