build.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * Module dependencies
  3. */
  4. var flaverr = require('flaverr');
  5. var helpBuildMachine = require('./private/help-build-machine');
  6. /**
  7. * .build()
  8. *
  9. * > aka Machine()
  10. *
  11. * Build a callable ("wet") machine.
  12. *
  13. * - - - - - - - - - - - - - - - - - - - - - - - - - - -
  14. * @param {Dictionary} nmDef
  15. * A Node-Machine definition.
  16. *
  17. * @param {Error?} omen
  18. * A custom build-time omen.
  19. * (For a more complete explanation of what an "omen" is,
  20. * see the relevant comments in `build.js`.)
  21. * - - - - - - - - - - - - - - - - - - - - - - - - - - -
  22. * @returns {Function}
  23. * A callable, "wet" machine.
  24. * - - - - - - - - - - - - - - - - - - - - - - - - - - -
  25. */
  26. module.exports = function build(nmDef, omen){
  27. // Potentially build an "omen": an Error instance defined ahead of time in order to grab
  28. // a nice, clean, appropriate stack trace originating from the line of code that actually
  29. // invoked this code in userland (i.e. wherever .build() got called from)
  30. //
  31. // More generally, omens are used for providing a better experience when viewing the
  32. // stack trace of errors that come from one or more asynchronous ticks down the line;
  33. // e.g. uniqueness errors. But really, they're useful for prettying up any stack trace--
  34. // whether it's asynchronous or not.
  35. //
  36. // Remember that an omen should only be used in an Error ONCE!
  37. //
  38. // > Inspired by the implementation originally devised for Waterline:
  39. // > https://github.com/balderdashy/waterline/blob/6b1f65e77697c36561a0edd06dff537307986cb7/lib/waterline/utils/query/build-omen.js
  40. //
  41. // See npmjs.com/package/flaverr for more info.
  42. omen = omen || flaverr.omen(build);
  43. // Then call our private utility to finish building the machine.
  44. return helpBuildMachine({ def: nmDef }, omen);
  45. };