This repository has been archived by the owner on Feb 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
165 lines (129 loc) · 3.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
var EventEmitter = require('events').EventEmitter
var inherits = require('inherits')
var noop = function () {}
module.exports = Abstract
inherits(Abstract, EventEmitter)
function Abstract (opts) {
if (!(this instanceof Abstract)) return new Abstract(opts)
EventEmitter.call(this)
this.opened = false
this._opening = null
this._closing = null
if (opts) {
if (opts.read) this._read = opts.read
if (opts.write) this._write = opts.write
if (opts.open) this._open = opts.open
if (opts.close) this._close = opts.close
if (opts.end) this._end = opts.end
if (opts.unlink) this._unlink = opts.unlink
}
}
Abstract.prototype.open = function (callback) {
if (!callback) callback = noop
if (this.opened) return process.nextTick(callback)
var self = this
if (this._opening) {
this._opening.push(callback)
} else {
this._opening = [callback]
this._open(opened)
}
function opened (err) {
if (!err) self.opened = true
var cbs = self._opening
self._opening = null
self.emit('open')
for (var i = 0; i < cbs.length; i++) cbs[i](err)
}
}
Abstract.prototype._open = function (callback) {
process.nextTick(callback)
}
Abstract.prototype.write = function (offset, buffer, callback) {
if (!callback) callback = noop
if (typeof offset !== 'number') throw new Error('Scalar offset')
if (!Buffer.isBuffer(buffer)) throw new Error('Buffer')
if (!this.opened) return openAndWrite(this, offset, buffer, callback)
this._write(offset, buffer, callback)
}
Abstract.prototype._write = function (offset, buffer, callback) {
process.nextTick(function () {
callback(new Error('Write not implemented'))
})
}
Abstract.prototype.read = function (offset, length, callback) {
if (typeof offset !== 'number') throw new Error('Scalar offset')
if (typeof length !== 'number') throw new Error('Scalar length')
if (typeof callback !== 'function') throw new Error('Callback')
if (!this.opened) return openAndRead(this, offset, length, callback)
this._read(offset, length, callback)
}
Abstract.prototype._read = function (offset, length, callback) {
process.nextTick(function () {
callback(new Error('Read not implemented'))
})
}
Abstract.prototype.close = function (callback) {
if (!callback) callback = noop
var self = this
if (!this.opened) this.open(next)
else next()
function next (err) {
if (err) return callback(err)
if (self._closing) {
self._closing.push(callback)
} else {
self._closing = [callback]
self._close(closed)
}
}
function closed (err) {
if (!err) self.opened = false
var cbs = self._closing
self._closing = null
self.emit('close')
for (var i = 0; i < cbs.length; i++) cbs[i](err)
}
}
Abstract.prototype._close = function (callback) {
process.nextTick(callback)
}
Abstract.prototype.end = function (options, callback) {
if (typeof options === 'function') return this.end(null, options)
if (!callback) callback = noop
var self = this
if (!this.opened) this.open(next)
else next()
function next (err) {
if (err) return callback(err)
self._end(options, callback)
}
}
Abstract.prototype._end = function (options, callback) {
process.nextTick(callback)
}
Abstract.prototype.unlink = function (callback) {
if (!callback) callback = noop
var self = this
if (!this.opened) this.open(next)
else next()
function next (err) {
if (err) return callback(err)
self._unlink(callback)
}
}
Abstract.prototype._unlink = function (callback) {
process.nextTick(callback)
}
function openAndWrite (self, offset, buffer, callback) {
self.open(function (err) {
if (err) return callback(err)
self.write(offset, buffer, callback)
})
}
function openAndRead (self, offset, length, callback) {
self.open(function (err) {
if (err) return callback(err)
self.read(offset, length, callback)
})
}