-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
263 lines (213 loc) · 6.74 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
var a = require('assert')
var macgyver = require('macgyver')
var Stream = require('stream')
function merge (to, from) {
to = to || {}
for (var k in from)
if('undefined' === typeof to[k])
to[k] = from[k]
return to
}
module.exports = function (stream, opts) {
a.ok(stream instanceof Stream)
a.ok('function', typeof stream.pipe)
a.ok('function', typeof stream.destroy)
var mac = macgyver()
var opts = merge(('string' == typeof opts ? {name: opts} : opts) || {}, {name: 'stream'})
var spec = {}
function add(name, method) {
spec[name] = function (_opts) {
method(mac, stream, merge(_opts, opts))
return this
}
}
add('through' , throughSpec)
add('basic' , throughSpec) //legacy, remove this.
add('duplex' , duplexSpec)
add('readable' , readableSpec)
add('writable' , writableSpec)
add('pausable' , pauseSpec)
add('drainable' , drainSpec)
add('strictPause' , strictSpec)
spec.all = function (opts) {
if(stream.writable && stream.readable)
return this.through(opts).pausable(opts)
else if(stream.writable)
return this.writable().pausable()
else
return this.readable()
}
spec.validate = function () {
mac.validate()
return this
}
spec.validateOnExit = function () {
//your test framework probably has assigned a listener for on exit also,
//make sure we are first. so the framework has a chance to detect a
//validation error.
if(process.listeners)
process.listeners('exit').unshift(function () {
try {
mac.validate()
} catch (err) {
console.error(err && err.stack)
throw err
}
})
else
setTimeout(mac.validate, 10e3)
return this
}
return spec
}
function writableSpec (mac, stream, opts) {
a.ok('function', typeof stream.destroy, opts.name + '.end *must* be a function')
a.equal(stream.writable, true, opts.name + '.writable *must* == true')
function e (n) { return opts.name + '.emit(\''+n+'\')' }
function n (n) { return opts.name + '.'+n+'()' }
stream.end = mac(stream.end, n('end')).returns(function () {
a.equal(stream.writable, false, opts.name + ' must not be writable after end()')
}).once()
stream.write =
mac(stream.write, n('write'))
.throws(function (err, threw) {
// a.equal(threw, !stream.writable, 'write should throw if !writable')
})
var onClose = mac(function (){
if(opts.debug) console.error(e('close'))
}, e('close')).once()
var onError = mac(function (err){
if(opts.debug) console.error(e('error'), err)
}, e('error')).before(onClose)
stream.on('close', onClose)
stream.on('error', onError)
if(opts.error === false)
onError.never()
if(opts.error === true)
onError.once()
}
function readableSpec (mac, stream, opts) {
merge(opts, {end: true})
function e (n) { return opts.name + '.emit(\''+n+'\')' }
function n (n) { return opts.name + '.'+n+'()' }
var onError = mac(function (err){
//'error' means the same thing as 'close'.
onClose.maybeOnce()
if(opts.debug) console.error(e('error'), err)
}, e('error'))
//.before(onClose) error does not emit close, officially, yet.
var onEnd = mac(function end (){
if(opts.debug) console.error(e('end'), err)
}, e('end'))
.isPassed(function () {
a.equal(stream.readable, false, 'stream must not be readable on "end"')
})
var onClose = mac(function (){
if(opts.debug) console.error(e('close'))
}, e('close'))
.once()
//on end must occur before onClose or onError
//that is to say, end MUST NOT occur after 'close' or 'error'
onEnd.before(onClose).before(onError)
var onData = mac(function data (){}, e('data')).before(onEnd)
stream.on('close', onClose)
stream.on('end', onEnd)
stream.on('data', onData)
if(opts.end !== false) onEnd.once()
else onEnd.never()
if(opts.error === false)
onError.never()
if(opts.error === true)
onError.once()
}
function throughSpec (mac, stream, opts) {
writableSpec(mac, stream, opts)
readableSpec(mac, stream, opts)
throughPauseSpec(mac, stream, opts)
}
function duplexSpec (mac, stream, opts) {
writableSpec(mac, stream, opts)
readableSpec(mac, stream, opts)
pauseSpec(mac, stream, opts)
drainSpec(mac, stream, opts)
}
function drainSpec (mac, stream, opts) {
var paused = false
function e (n) { return opts.name + '.emit(\''+n+'\')' }
function n (n) { return opts.name + '.'+n+'()' }
function drain() {
paused = false
}
var onDrain = mac(drain).never()
stream.on('drain', onDrain)
stream.write =
mac(stream.write, n('write'))
.returns(function (written) {
if(!paused && !written) {
//after write returns false, it must emit drain eventually.
onDrain.again()
}
paused = (written === false)
})
}
//for through-streams
function throughPauseSpec (mac, stream, opts) {
var paused = false
function e (n) { return opts.name + '.emit(\''+n+'\')' }
function n (n) { return opts.name + '.'+n+'()' }
function drain() {
paused = false
}
var onDrain = mac(drain, e('drain')).never()
a.ok(stream.pause, 'stream *must* have pause')
if(!stream.readable)
throw new Error('pause does not make sense for a non-readable stream')
stream.pause = mac(stream.pause, n('pause'))
.isPassed(function () {
if(paused) return
//console.log('entered pause state by pause()')
paused = true
onDrain.again()
})
stream.on('drain', onDrain)
stream.write =
mac(stream.write, n('write'))
.returns(function (written) {
if(!paused && !written) {
//after write returns false, it must emit drain eventually.
//console.log('entered pause state by write() === false')
onDrain.again()
}
paused = (written === false)
})
if(opts.strict)
stream.on('data', function onData(data) {
//stream must not emit data when paused!
a.equal(paused, false, 'a strict stream *must not* emit \'data\' when paused')
})
}
/*
demand that the stream does not emit any data when paused
*/
function pauseSpec (mac, stream, opts) {
var paused = false
function e (n) { return opts.name + '.emit(\''+n+'\')' }
function n (n) { return opts.name + '.'+n+'()' }
if(!stream.readable)
throw new Error('strict pause does not make sense for a non-readable stream')
stream.pause = mac(stream.pause)
.isPassed(function () {
paused = true
})
stream.resume = mac(stream.resume)
.isPassed(function () {
paused = false
})
if(opts.strict)
stream.on('data', function () {
a.equal(paused, false, 'a strict pausing stream must not emit data when paused')
})
}
function strictSpec (mac, stream, opts) {
return pauseSpec(mac, stream, merge(opts, {strict: true}))
}