Fixup Base64 node and add better tests

Revert correct <-> behaviour, make encoding info clearer, added type
checking - to close #401 and close #437
This commit is contained in:
Dave Conway-Jones
2018-04-29 17:35:30 +01:00
parent 5019f68c9c
commit 6a28ecfbc2
4 changed files with 148 additions and 17 deletions

View File

@@ -6,10 +6,29 @@ module.exports = function(RED) {
this.action = n.action || "";
this.property = n.property || "payload";
var node = this;
var regexp = new RegExp('^[A-Za-z0-9+\/=]*$'); // check it only contains valid characters
this.on("input", function(msg) {
var value = RED.util.getMessageProperty(msg,node.property);
if (value !== undefined) {
if (node.action === "") {
if (node.action === "str") {
value = RED.util.ensureBuffer(value).toString('base64');
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
}
else if (node.action === "b64") {
if ( typeof value === "string") {
var load = value.replace(/\s+/g,'');
if (regexp.test(load) && (load.length % 4 === 0) ) {
value = Buffer.from(load,'base64').toString('binary');
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
}
else { node.error("Invalid Base64 string",msg); }
}
else { node.error("Not a Base64 string",msg); }
}
else {
if (Buffer.isBuffer(value)) {
// Take binary buffer and make into a base64 string
value = value.toString('base64');
@@ -21,9 +40,8 @@ module.exports = function(RED) {
var load = value.replace(/\s+/g,''); // remove any whitespace
//var load = value.replace(/[\t\r\n\f]+/g,'');
//var load = value;
var regexp = new RegExp('^[A-Za-z0-9+\/=]*$'); // check it only contains valid characters
if ( regexp.test(load) && (load.length % 4 === 0) ) {
value = Buffer.from(load,'base64').toString('binary');
value = Buffer.from(load,'base64');
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
}
@@ -38,17 +56,6 @@ module.exports = function(RED) {
node.warn("This node only handles strings or buffers.");
}
}
if (node.action === "str") {
value = Buffer.from(value).toString('base64');
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
}
if (node.action === "b64") {
var load = value.replace(/\s+/g,'');
value = Buffer.from(load,'base64').toString('binary');
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
}
}
else { node.warn("No property found to process"); }
});