index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict'
  2. let $module
  3. /*
  4. let contextProto = this.context;
  5. while (contextProto = Object.getPrototypeOf(contextProto)) {
  6. completionGroups.push(Object.getOwnPropertyNames(contextProto));
  7. }
  8. */
  9. function handle (data) {
  10. let idx = data.idx
  11. , child = data.child
  12. , method = data.method
  13. , args = data.args
  14. , callback = function () {
  15. let _args = Array.prototype.slice.call(arguments)
  16. if (_args[0] instanceof Error) {
  17. let e = _args[0]
  18. _args[0] = {
  19. '$error' : '$error'
  20. , 'type' : e.constructor.name
  21. , 'message' : e.message
  22. , 'stack' : e.stack
  23. }
  24. Object.keys(e).forEach(function(key) {
  25. _args[0][key] = e[key]
  26. })
  27. }
  28. process.send({ owner: 'farm', idx: idx, child: child, args: _args })
  29. }
  30. , exec
  31. if (method == null && typeof $module == 'function')
  32. exec = $module
  33. else if (typeof $module[method] == 'function')
  34. exec = $module[method]
  35. if (!exec)
  36. return console.error('NO SUCH METHOD:', method)
  37. exec.apply(null, args.concat([ callback ]))
  38. }
  39. process.on('message', function (data) {
  40. if (data.owner !== 'farm') {
  41. return;
  42. }
  43. if (!$module) return $module = require(data.module)
  44. if (data.event == 'die') return process.exit(0)
  45. handle(data)
  46. })