-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwritable.js
181 lines (147 loc) · 4.25 KB
/
writable.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
var events = require('events')
var inherits = require('inherits')
module.exports = Writable
function WritableState () {
this.vintageStream = true
this.destroyed = false
this.destroyReason = null
this.toBuffer = false
this.halfOpen = true
this.syncWrite = false
this.corked = false
this.drained = true
this.ended = false
this.finishEmitted = false
this.writing = false
this.buffer = []
this.source = null
this.afterPipe = null
this.afterDestroy = null
this.afterWrite = null
this.afterEnd = null
}
function Writable (opts) {
if (!(this instanceof Writable)) return new Writable(opts)
if (!opts) opts = {}
events.EventEmitter.call(this)
if (opts.write) this._write = opts.write
if (opts.end) this._end = opts.end
if (opts.destroy) this._destroy = opts.destroy
var self = this
var state = this._writableState = new WritableState()
state.toBuffer = !opts.toBuffer && !opts.writableToBuffer
state.afterDestroy = afterDestroy
state.afterWrite = afterWrite
state.afterEnd = afterEnd
function afterWrite (err) {
if (err) return self.destroy(err)
state.writing = false
if (!state.drained) {
state.drained = true
if (state.source) state.source.resume()
self.emit('drain')
}
if (!state.writing && !state.syncWrite && state.buffer.length) {
write(self, state, state.buffer.shift())
}
emitFinishMaybe(self, state)
}
function afterDestroy (err) {
if (!err) err = state.destroyReason
if (state.afterPipe) {
var cb = state.afterPipe
state.afterPipe = null
cb(err || (state.finishEmitted ? null : (state.destroyReason || new Error('Stream closed prematurely'))))
}
if (err) self.emit('error', err)
self.emit('close')
}
function afterEnd (err) {
if (err) return self.destroy(err)
self.emit('finish')
if (!state.halfOpen) self.push(null)
self.destroyMaybe()
}
}
inherits(Writable, events.EventEmitter)
Writable.prototype.cork = function () {
var state = this._writableState
if (state.corked) return
state.corked = true
this.emit('cork')
}
Writable.prototype.uncork = function () {
var state = this._writableState
if (!state.corked) return
state.corked = false
this.emit('uncork')
if (!state.writing) state.afterWrite(null)
}
Writable.prototype.write = function (data) {
var state = this._writableState
return writeMaybe(this, state, (!state.objectMode && typeof data === 'string') ? Buffer(data) : data)
}
Writable.prototype.end = function (data) {
var state = this._writableState
if (state.ended) return
if (data) this.write(data)
state.ended = true
emitFinishMaybe(this, state)
}
Writable.prototype._write = function (data, cb) {
// Override me
cb(null)
}
Writable.prototype._end = function (cb) {
// Override me
cb(null)
}
Writable.prototype.destroyMaybe = function () {
if (this._writableState.finishEmitted) this.destroy()
}
Writable.prototype.destroy =
Writable.prototype.destroyWritable = function (err) {
var state = this._writableState
if (state.destroyed) return
state.destroyed = true
if (err) state.destroyReason = err
state.writing = true // set these to stop reading
if (state.source) {
var src = state.source
state.source = null
if (src.destroy) src.destroy()
}
this._destroy(state.afterDestroy)
}
Writable.prototype._destroy = function (cb) {
// Override me
cb(null)
}
function writeMaybe (self, state, data) {
if (state.destroyed || state.ended) return false
if (!state.writing && !state.buffer.length && !state.corked) {
write(self, state, data)
return true
}
var length = state.buffer.push(data)
if (state.drained) state.drained = length < 16
return state.drained
}
function write (self, state, data) {
while (!state.destroyed && !state.writing) {
state.writing = true
state.syncWrite = true
self._write(data, state.afterWrite)
state.syncWrite = false
if (state.writing) return
if (!state.buffer.length) break
data = state.buffer
}
emitFinishMaybe(self, state)
}
function emitFinishMaybe (self, state) {
if (!state.ended || state.buffer.length || state.finishEmitted || state.destroyed || state.corked || state.writing) return
state.finishEmitted = true
state.writing = true
self._end(state.afterEnd)
}