abstract.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // the parent class for all fstreams.
  2. module.exports = Abstract
  3. var Stream = require('stream').Stream
  4. var inherits = require('inherits')
  5. function Abstract () {
  6. Stream.call(this)
  7. }
  8. inherits(Abstract, Stream)
  9. Abstract.prototype.on = function (ev, fn) {
  10. if (ev === 'ready' && this.ready) {
  11. process.nextTick(fn.bind(this))
  12. } else {
  13. Stream.prototype.on.call(this, ev, fn)
  14. }
  15. return this
  16. }
  17. Abstract.prototype.abort = function () {
  18. this._aborted = true
  19. this.emit('abort')
  20. }
  21. Abstract.prototype.destroy = function () {}
  22. Abstract.prototype.warn = function (msg, code) {
  23. var self = this
  24. var er = decorate(msg, code, self)
  25. if (!self.listeners('warn')) {
  26. console.error('%s %s\n' +
  27. 'path = %s\n' +
  28. 'syscall = %s\n' +
  29. 'fstream_type = %s\n' +
  30. 'fstream_path = %s\n' +
  31. 'fstream_unc_path = %s\n' +
  32. 'fstream_class = %s\n' +
  33. 'fstream_stack =\n%s\n',
  34. code || 'UNKNOWN',
  35. er.stack,
  36. er.path,
  37. er.syscall,
  38. er.fstream_type,
  39. er.fstream_path,
  40. er.fstream_unc_path,
  41. er.fstream_class,
  42. er.fstream_stack.join('\n'))
  43. } else {
  44. self.emit('warn', er)
  45. }
  46. }
  47. Abstract.prototype.info = function (msg, code) {
  48. this.emit('info', msg, code)
  49. }
  50. Abstract.prototype.error = function (msg, code, th) {
  51. var er = decorate(msg, code, this)
  52. if (th) throw er
  53. else this.emit('error', er)
  54. }
  55. function decorate (er, code, self) {
  56. if (!(er instanceof Error)) er = new Error(er)
  57. er.code = er.code || code
  58. er.path = er.path || self.path
  59. er.fstream_type = er.fstream_type || self.type
  60. er.fstream_path = er.fstream_path || self.path
  61. if (self._path !== self.path) {
  62. er.fstream_unc_path = er.fstream_unc_path || self._path
  63. }
  64. if (self.linkpath) {
  65. er.fstream_linkpath = er.fstream_linkpath || self.linkpath
  66. }
  67. er.fstream_class = er.fstream_class || self.constructor.name
  68. er.fstream_stack = er.fstream_stack ||
  69. new Error().stack.split(/\n/).slice(3).map(function (s) {
  70. return s.replace(/^ {4}at /, '')
  71. })
  72. return er
  73. }