解压缩和解密的传入消息 - NodeJS

时间:2017-09-15 16:56:34

标签: javascript node.js xml encryption unzip

我目前正在开发一个项目,该项目接收来自Amazons SQS(简单队列服务)的传入消息。

我已建立与SQS服务的连接并接收消息。

接收消息采用以下格式:

    { MessageId: '124a42b-657d-481d-348f-ddd9b8d8b143b',
  ReceiptHandle: 'AQEBSSVzlCbqsSUQ3E.....',
  MD5OfBody: '7ba46c7c8874fc6d0c4a141a2d3d4d5a721',
  Body: '10987235@PUBLISH;aSeIgjS78Iy4KRZHSeAy43...' }

邮件正文已压缩并加密。

我的问题是解密和解压缩消息Body的最佳方法是什么?目前我正在编写消息。对文件进行操作并尝试解密并提取表单文件。这是我的代码的副本:

const Consumer = require('sqs-consumer');
const AWS = require('aws-sdk');
const fs = require('fs');
const path = require("path");
const unzipper = require('unzipper');

const app = Consumer.create({
  queueUrl: 'https://sqs.us-west-1.amazonaws.com/QueueName',
  handleMessage: (message, done) => {

    saveToFile(message.Body)
      .then(() => {
        return unzipFile();
      })
      .then((success) => {
        // read content of XML file and save to DB
        console.log('success:', success);
      })
      .catch(e => {
        console.log('Error:', e);
      });

  },
  sqs: new AWS.SQS()
});

app.on('error', (err) => {
  console.log(err.message);
});

app.start();


const saveToFile = (message) => {
  debugger;
  console.log('saveToFile fired!');
  return new Promise((resolve, reject) => {
    fs.appendFile(path.resolve(__dirname) + '/zips/decodeMsg.zip', message, (err) => {
      if (err) reject(error);
      resolve();
    });
  });
}

const unzipFile = () => {
  return unzipper.Open.file(path.resolve(__dirname) + '/zips/decodeMsg.zip') // Fails To Read The File Here.
    .then(function (directory) {
      console.log('directory', directory);
      return new Promise(function (resolve, reject) {
        directory.files[0].stream(password)  // PASSING IN PASSWORD HERE
          .pipe(fs.createWriteStream('readablefile')) // should be XML
          .on('error', reject)
          .on('finish', resolve)
      });
    });
}

此时调用unzipFile()并返回错误:

Error: Uncaught, unspecified "error" event. (FILE_ENDED)
at PullStream.emit (events.js:163:17)
at PullStream.onerror (_stream_readable.js:579:12)
at emitOne (events.js:96:13)
at PullStream.emit (events.js:188:7)

我的问题是,我是否应该将消息Body写入文件并尝试提取和解密,或者有更好的方法来执行此操作吗?

非常感谢任何输入。

0 个答案:

没有答案