@kumologica/kumologica-contrib-base64
Version:
Node that encodes and decodes base64 string
58 lines (56 loc) • 1.59 kB
JavaScript
module.exports = function (App) {
const { KLNode } = require('@kumologica/devkit');
class Base64Error extends Error {
constructor(error) {
super(error);
if (error) {
this.name = error.name;
this.message = error.message;
}
}
}
class Base64 extends KLNode {
constructor(props) {
super(App, props);
this.operation = props.operation;
this.property = props.property;
this.propertytype = props.propertyType;
}
handle(msg) {
if (this.operation === 'Encode') {
try {
let stringtoencode = App.util.evaluateDynamicField(
this.property,
msg,
this
);
let endcodedString = Buffer.from(stringtoencode).toString('base64');
msg.base64 = { encodedstring: endcodedString };
this.send(msg);
} catch (error) {
error.name = "Base64 Encoding Failed";
this.sendError(error,msg);
throw error;
}
} else {
try {
let stringtodecode = App.util.evaluateDynamicField(
this.property,
msg,
this
);
let decodedString = Buffer.from(stringtodecode, 'base64').toString(
'ascii'
);
msg.base64 = { decodedstring: decodedString };
this.send(msg);
} catch (error) {
error.name = "Base64 Decoding Failed";
this.sendError(new Base64Error(error),msg);
throw error;
}
}
}
}
App.nodes.registerType('Base64', Base64);
};