Making a "transparent" server object


#1

This is mostly syntactic sugar, but wondering if anyone has run into it. I have a package that needs to run on the server.

let net = Npm.require('net');
let EventEmitter = Npm.require('events').EventEmitter;

let Cylon = new EventEmitter();
Cylon._connected = false;
Cylon.config = Meteor.settings.galil;
Cylon._commands = net.connect(Galil.config.connection, () => this.emit('connect'));

Cylon.sendCommand = function (command) {
  check(command, String);
  return new Promise((resolve, reject) => {
    this._commands.write(`${command}\r`, 'utf8', resolve);
  });
}

Cylon.on('connect', () => {
  this._connected = true;
});

What I really want is to create a very transparent layer on the client that essentially is the same object, but will call it on the server and return its results in a callback or promise. For example, on the client, I basically just want to do:

// on client.
Cylon.sendCommand('Message "Hello"', () => {
  console.log('Command completed');
});
// or
Cylon.sendCommand(`Message "hi"`).then(console.log);

Anyone have any ideas on this one? Currently I just make it very ugly by putting every command in a Meteor.methods and then making an object that calls all those meteor.methods.