I am getting message stream from email body and this stream is in base64 formate I want to decode the stream.I am using 'base64-stream' which is npm module to decode the stream but I am getting error.
TypeError: base64.decode is not a function
function buildAttMessageFunction(attachment) {
var filename = attachment.params.name;
var encoding = attachment.encoding;
return function (msg, seqno) {
var prefix = '(#' + seqno + ') ';
msg.on('body', function(stream, info) {
//Create a write stream so that we can stream the attachment to file;
console.log(prefix + 'Streaming this attachment to file', filename, info);
var writeStream = fs.createWriteStream(filename);
writeStream.on('finish', function() {
console.log(prefix + 'Done writing to file %s', filename);
});
//stream.pipe(writeStream); this would write base64 data to the file.
//so we decode during streaming using
if (toUpper(encoding) === 'BASE64') {
//the stream is base64 encoded, so here the stream is decode on the fly and piped to the write stream (file)
stream.pipe(base64.decode()).pipe(writeStream);
} else {
//here we have none or some other decoding streamed directly to the file which renders it useless probably
stream.pipe(writeStream);
}
});
msg.once('end', function() {
console.log(prefix + 'Finished attachment %s', filename);
});
};
}
arnt
9,8035 gold badges27 silver badges37 bronze badges
asked Mar 11, 2020 at 10:57
1 Answer 1
I guess you need to use this way
const {Base64Decode} = require("base64-stream");
stream.pipe(new Base64Decode()).pipe(writeStream);
answered Mar 11, 2020 at 11:13
Comments
lang-js