{"size": 951, "ext": "js", "max_stars_repo_path": "server.js", "max_stars_repo_name": "bug-is-not-a-wolf/share-jack-crypto-pipe", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server.js", "max_issues_repo_name": "bug-is-not-a-wolf/share-jack-crypto-pipe", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server.js", "max_forks_repo_name": "bug-is-not-a-wolf/share-jack-crypto-pipe", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.1951219512, "max_line_length": 97, "alphanum_fraction": 0.5920084122}
const ipc = require('node-ipc');
const fs = require('fs');

const algorithm = 'aes-256-cbc'; // cbc -> Cipher Block Chaining
const password = fs.readFileSync('keys/cryptoStream.key');  // TODO: nice API with these settings

const Crypt =  require('./crypto.js');    // TODO: naming
const crypt = new Crypt(algorithm, password);

ipc.config.id = 'cryptoStream';
ipc.config.retry = 1500;
ipc.config.silent = true;

ipc.serve(() => {
  ipc.server.on(
    'encrypt',
    (data, socket) => {
      data.message = crypt.encrypt(data.message);
      ipc.server.emit(socket, 'encrypted', data);
    }
  );

  ipc.server.on(
    'decrypt',
    (data, socket) => {
      try {
        data.message = crypt.decrypt(data.message);
      } catch(err) {
        data.message = '';
        data.err = true;
      } finally {
        ipc.server.emit(socket, 'decrypted', data);
      }
    }
  );
});

// module.exports = function start() {
ipc.server.start();
// };
