如何将base64流转换为字符串并写入另一个文件?

How to convert base64 stream into string and write into another file?

我正在从电子邮件正文中获取消息流并且该流是 base64 格式我想解码 stream.I 我正在使用 'base64-stream' 这是 npm 模块来解码流但是我收到错误.

TypeError: base64.decode 不是函数

   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);
    });
  };
}

我猜你需要用这种方式

const {Base64Decode} = require("base64-stream");
stream.pipe(new Base64Decode()).pipe(writeStream);