mirror of
https://github.com/node-red/node-red-nodes.git
synced 2023-10-10 13:36:58 +02:00
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:
parent
5019f68c9c
commit
6a28ecfbc2
@ -4,7 +4,7 @@
|
||||
<label for="node-input-action"><i class="fa fa-dot-circle-o"></i> Action</label>
|
||||
<select style="width:70%" id="node-input-action">
|
||||
<option value="">Convert Buffer <-> Base64</option>
|
||||
<option value="str">Convert String to Base64</option>
|
||||
<option value="str">Encode as Base64</option>
|
||||
<option value="b64">Convert Base64 to String</option>
|
||||
</select>
|
||||
</div>
|
||||
@ -22,7 +22,8 @@
|
||||
<p>A function that converts the chosen property (default <code>msg.payload</code>) to and from base64 format.</p>
|
||||
<p>If the input is a buffer it converts it to a Base64 encoded string.</p>
|
||||
<p>If the input is a Base64 string it converts it back to a binary buffer.</p>
|
||||
<p>You can also fix conversion string to base64,and base64 to string if required.</p>
|
||||
<p>You can also fix coding into base64, and base64 to buffer if required.</p>
|
||||
<p>Note: Using "Encode to Base64" will encode an already encoded string.</p>
|
||||
</script>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
@ -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"); }
|
||||
});
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name" : "node-red-node-base64",
|
||||
"version" : "0.1.2",
|
||||
"version" : "0.1.3",
|
||||
"description" : "A Node-RED node to pack and unpack objects to base64 format",
|
||||
"dependencies" : {
|
||||
},
|
||||
|
@ -61,6 +61,7 @@ describe('base64 node', function() {
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
msg.should.have.a.property("payload");
|
||||
msg.payload.should.be.instanceof(Buffer);
|
||||
msg.payload.toString().should.equal("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
||||
done();
|
||||
});
|
||||
@ -76,6 +77,7 @@ describe('base64 node', function() {
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
msg.should.have.a.property("foo");
|
||||
msg.foo.should.be.instanceof(Buffer);
|
||||
msg.foo.toString().should.equal("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
||||
done();
|
||||
});
|
||||
@ -162,4 +164,125 @@ describe('base64 node', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('can force encode string to base64', function(done) {
|
||||
var flow = [{id:"n1", type:"base64", action:"str", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(testNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
msg.should.have.a.property("payload","YW5keQ==");
|
||||
done();
|
||||
});
|
||||
n1.emit("input", {payload:"andy"});
|
||||
});
|
||||
});
|
||||
|
||||
it('can force encode boolean to base64', function(done) {
|
||||
var flow = [{id:"n1", type:"base64", action:"str", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(testNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
msg.should.have.a.property("payload","dHJ1ZQ==");
|
||||
done();
|
||||
});
|
||||
n1.emit("input", {payload:true});
|
||||
});
|
||||
});
|
||||
|
||||
it('can force encode number to base64', function(done) {
|
||||
var flow = [{id:"n1", type:"base64", action:"str", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(testNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
msg.should.have.a.property("payload","MTIzNA==");
|
||||
done();
|
||||
});
|
||||
n1.emit("input", {payload:1234});
|
||||
});
|
||||
});
|
||||
|
||||
it('can force encode object to base64', function(done) {
|
||||
var flow = [{id:"n1", type:"base64", action:"str", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(testNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
msg.should.have.a.property("payload","eyJhIjoxfQ==");
|
||||
done();
|
||||
});
|
||||
n1.emit("input", {payload:{a:1}});
|
||||
});
|
||||
});
|
||||
|
||||
it('can force decode base64 to string', function(done) {
|
||||
var flow = [{id:"n1", type:"base64", action:"b64", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(testNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
msg.payload.should.be.instanceof(String);
|
||||
msg.should.have.a.property("payload","Hello World");
|
||||
done();
|
||||
});
|
||||
n1.emit("input", {payload:"SGVsbG8gV29ybGQ="});
|
||||
});
|
||||
});
|
||||
|
||||
it('wont decode base64 to string if not a valid string', function(done) {
|
||||
var flow = [{id:"n1", type:"base64", action:"b64", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(testNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
done("should not get here with no payload.");
|
||||
});
|
||||
setTimeout(function () {
|
||||
try {
|
||||
var logEvents = helper.log().args.filter(function (evt) {
|
||||
return evt[0].type == "base64";
|
||||
});
|
||||
logEvents[0][0].should.have.a.property('msg');
|
||||
logEvents[0][0].msg.toString().should.startWith("Invalid Base64 string");
|
||||
done();
|
||||
} catch (e) {
|
||||
done(e);
|
||||
}
|
||||
}, 45);
|
||||
n1.emit("input", {payload:"andy!@3"});
|
||||
});
|
||||
});
|
||||
|
||||
it('wont decode base64 to string if not a string', function(done) {
|
||||
var flow = [{id:"n1", type:"base64", action:"b64", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(testNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
done("should not get here with no payload.");
|
||||
});
|
||||
setTimeout(function () {
|
||||
try {
|
||||
var logEvents = helper.log().args.filter(function (evt) {
|
||||
return evt[0].type == "base64";
|
||||
});
|
||||
logEvents[0][0].should.have.a.property('msg');
|
||||
logEvents[0][0].msg.toString().should.startWith("Not a Base64 string");
|
||||
done();
|
||||
} catch (e) {
|
||||
done(e);
|
||||
}
|
||||
}, 45);
|
||||
n1.emit("input", {payload:1234});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user