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