diff --git a/nodes/core/io/22-websocket.js b/nodes/core/io/22-websocket.js index 058b1082f..d21225dda 100644 --- a/nodes/core/io/22-websocket.js +++ b/nodes/core/io/22-websocket.js @@ -160,14 +160,7 @@ module.exports = function(RED) { delete msg._session; payload = JSON.stringify(msg); } else { - payload = msg.payload; - if (Buffer.isBuffer(payload)) { - payload = payload.toString(); - } else if (typeof payload === "object") { - payload = JSON.stringify(payload); - } else if (typeof payload !== "string") { - payload = ""+payload; - } + payload = RED.utils.ensureString(msg.payload); } if (msg._session && msg._session.type == "websocket") { node.serverConfig.send(msg._session.id,payload); diff --git a/nodes/core/social/61-email.js b/nodes/core/social/61-email.js index 2f45a0a6e..2f0e98a7e 100644 --- a/nodes/core/social/61-email.js +++ b/nodes/core/social/61-email.js @@ -73,14 +73,7 @@ module.exports = function(RED) { if (msg != null) { if (smtpTransport) { node.status({fill:"blue",shape:"dot",text:"sending"}); - var payload = msg.payload; - if (Buffer.isBuffer(payload)) { - payload = payload.toString(); - } else if (typeof payload === "object") { - payload = JSON.stringify(payload); - } else if (typeof payload !== "string") { - payload = ""+payload; - } + var payload = RED.utils.ensureString(msg.payload); smtpTransport.sendMail({ from: node.userid, // sender address diff --git a/nodes/core/storage/65-redisout.js b/nodes/core/storage/65-redisout.js index 0cfef2a68..fabb2bbcd 100644 --- a/nodes/core/storage/65-redisout.js +++ b/nodes/core/storage/65-redisout.js @@ -83,14 +83,7 @@ module.exports = function(RED) { var k = this.key || msg.topic; if (k) { if (this.structtype == "string") { - if (Buffer.isBuffer(msg.payload)) { - msg.payload = msg.payload.toString(); - } else if (typeof msg.payload === "object") { - msg.payload = JSON.stringify(msg.payload); - } else if (typeof msg.payload !== "string") { - msg.payload = ""+msg.payload; - } - this.client.set(k,msg.payload); + this.client.set(k,RED.utils.ensureString(msg.payload)); } else if (this.structtype == "hash") { var r = hashFieldRE.exec(msg.payload); if (r) { diff --git a/red/red.js b/red/red.js index a97c153d3..559eb96bf 100644 --- a/red/red.js +++ b/red/red.js @@ -19,6 +19,7 @@ var nodes = require("./nodes"); var library = require("./library"); var comms = require("./comms"); var log = require("./log"); +var utils = require("./utils"); var fs = require("fs"); var settings = null; var credentials = require("./nodes/credentials"); @@ -48,6 +49,7 @@ var RED = { events: events, log: log, comms: comms, + utils: utils, version: function () { var p = require(path.join(process.env.NODE_RED_HOME,"package.json")); if (fs.existsSync(path.join(process.env.NODE_RED_HOME,".git"))) { diff --git a/red/utils.js b/red/utils.js new file mode 100644 index 000000000..012504e3e --- /dev/null +++ b/red/utils.js @@ -0,0 +1,31 @@ +/** + * Copyright 2014 IBM Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ + +function ensureString(o) { + if (Buffer.isBuffer(o)) { + return o.toString(); + } else if (typeof o === "object") { + return JSON.stringify(o); + } else if (typeof o === "string") { + return o; + } + return ""+o; +} + +module.exports = { + ensureString: ensureString, +}; + diff --git a/test/red/utils_spec.js b/test/red/utils_spec.js new file mode 100644 index 000000000..b39db5c26 --- /dev/null +++ b/test/red/utils_spec.js @@ -0,0 +1,41 @@ +/** + * Copyright 2014 IBM Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ +var should = require("should"); +var utils = require("../../red/utils"); + +describe("red/utils", function() { + describe('ensureString', function() { + it('strings are preserved', function() { + utils.ensureString('string').should.equal('string'); + }); + it('Buffer is converted', function() { + var s = utils.ensureString(new Buffer('foo')); + s.should.equal('foo'); + (typeof s).should.equal('string'); + }); + it('Object is converted to JSON', function() { + var s = utils.ensureString({foo: "bar"}); + (typeof s).should.equal('string'); + should.deepEqual(JSON.parse(s), {foo:"bar"}); + }); + it('stringifies other things', function() { + var s = utils.ensureString(123); + (typeof s).should.equal('string'); + s.should.equal('123'); + }); + }); +}); +