1 | var tape = require('tape')
|
2 | var through = require('through2')
|
3 | var pumpify = require('./')
|
4 | var stream = require('stream')
|
5 | var duplexify = require('duplexify')
|
6 |
|
7 | tape('basic', function(t) {
|
8 | t.plan(3)
|
9 |
|
10 | var pipeline = pumpify(
|
11 | through(function(data, enc, cb) {
|
12 | t.same(data.toString(), 'hello')
|
13 | cb(null, data.toString().toUpperCase())
|
14 | }),
|
15 | through(function(data, enc, cb) {
|
16 | t.same(data.toString(), 'HELLO')
|
17 | cb(null, data.toString().toLowerCase())
|
18 | })
|
19 | )
|
20 |
|
21 | pipeline.write('hello')
|
22 | pipeline.on('data', function(data) {
|
23 | t.same(data.toString(), 'hello')
|
24 | t.end()
|
25 | })
|
26 | })
|
27 |
|
28 | tape('3 times', function(t) {
|
29 | t.plan(4)
|
30 |
|
31 | var pipeline = pumpify(
|
32 | through(function(data, enc, cb) {
|
33 | t.same(data.toString(), 'hello')
|
34 | cb(null, data.toString().toUpperCase())
|
35 | }),
|
36 | through(function(data, enc, cb) {
|
37 | t.same(data.toString(), 'HELLO')
|
38 | cb(null, data.toString().toLowerCase())
|
39 | }),
|
40 | through(function(data, enc, cb) {
|
41 | t.same(data.toString(), 'hello')
|
42 | cb(null, data.toString().toUpperCase())
|
43 | })
|
44 | )
|
45 |
|
46 | pipeline.write('hello')
|
47 | pipeline.on('data', function(data) {
|
48 | t.same(data.toString(), 'HELLO')
|
49 | t.end()
|
50 | })
|
51 | })
|
52 |
|
53 | tape('destroy', function(t) {
|
54 | var test = through()
|
55 | test.destroy = function() {
|
56 | t.ok(true)
|
57 | t.end()
|
58 | }
|
59 |
|
60 | var pipeline = pumpify(through(), test)
|
61 |
|
62 | pipeline.destroy()
|
63 | })
|
64 |
|
65 | tape('close', function(t) {
|
66 | var test = through()
|
67 | var pipeline = pumpify(through(), test)
|
68 |
|
69 | pipeline.on('error', function(err) {
|
70 | t.same(err.message, 'lol')
|
71 | t.end()
|
72 | })
|
73 |
|
74 | test.emit('error', new Error('lol'))
|
75 | })
|
76 |
|
77 | tape('end waits for last one', function(t) {
|
78 | var ran = false
|
79 |
|
80 | var a = through()
|
81 | var b = through()
|
82 | var c = through(function(data, enc, cb) {
|
83 | setTimeout(function() {
|
84 | ran = true
|
85 | cb()
|
86 | }, 100)
|
87 | })
|
88 |
|
89 | var pipeline = pumpify(a, b, c)
|
90 |
|
91 | pipeline.write('foo')
|
92 | pipeline.end(function() {
|
93 | t.ok(ran)
|
94 | t.end()
|
95 | })
|
96 |
|
97 | t.ok(!ran)
|
98 | })
|
99 |
|
100 | tape('always wait for finish', function(t) {
|
101 | var a = new stream.Readable()
|
102 | a._read = function() {}
|
103 | a.push('hello')
|
104 |
|
105 | var pipeline = pumpify(a, through(), through())
|
106 | var ran = false
|
107 |
|
108 | pipeline.on('finish', function() {
|
109 | t.ok(ran)
|
110 | t.end()
|
111 | })
|
112 |
|
113 | setTimeout(function() {
|
114 | ran = true
|
115 | a.push(null)
|
116 | }, 100)
|
117 | })
|
118 |
|
119 | tape('async', function(t) {
|
120 | var pipeline = pumpify()
|
121 |
|
122 | t.plan(4)
|
123 |
|
124 | pipeline.write('hello')
|
125 | pipeline.on('data', function(data) {
|
126 | t.same(data.toString(), 'HELLO')
|
127 | t.end()
|
128 | })
|
129 |
|
130 | setTimeout(function() {
|
131 | pipeline.setPipeline(
|
132 | through(function(data, enc, cb) {
|
133 | t.same(data.toString(), 'hello')
|
134 | cb(null, data.toString().toUpperCase())
|
135 | }),
|
136 | through(function(data, enc, cb) {
|
137 | t.same(data.toString(), 'HELLO')
|
138 | cb(null, data.toString().toLowerCase())
|
139 | }),
|
140 | through(function(data, enc, cb) {
|
141 | t.same(data.toString(), 'hello')
|
142 | cb(null, data.toString().toUpperCase())
|
143 | })
|
144 | )
|
145 | }, 100)
|
146 | })
|
147 |
|
148 | tape('early destroy', function(t) {
|
149 | var a = through()
|
150 | var b = through()
|
151 | var c = through()
|
152 |
|
153 | b.destroy = function() {
|
154 | t.ok(true)
|
155 | t.end()
|
156 | }
|
157 |
|
158 | var pipeline = pumpify()
|
159 |
|
160 | pipeline.destroy()
|
161 | setTimeout(function() {
|
162 | pipeline.setPipeline(a, b, c)
|
163 | }, 100)
|
164 | })
|
165 |
|
166 | tape('preserves error', function (t) {
|
167 | var a = through()
|
168 | var b = through(function (data, enc, cb) {
|
169 | cb(new Error('stop'))
|
170 | })
|
171 | var c = through()
|
172 | var s = pumpify()
|
173 |
|
174 | s.on('error', function (err) {
|
175 | t.same(err.message, 'stop')
|
176 | t.end()
|
177 | })
|
178 |
|
179 | s.setPipeline(a, b, c)
|
180 | s.resume()
|
181 | s.write('hi')
|
182 | })
|
183 |
|
184 | tape('preserves error again', function (t) {
|
185 | var ws = new stream.Writable()
|
186 | var rs = new stream.Readable({highWaterMark: 16})
|
187 |
|
188 | ws._write = function (data, enc, cb) {
|
189 | cb(null)
|
190 | }
|
191 |
|
192 | var once = true
|
193 | rs._read = function () {
|
194 | process.nextTick(function () {
|
195 | if (!once) return
|
196 | once = false
|
197 | rs.push('hello world')
|
198 | })
|
199 | }
|
200 |
|
201 | var pumpifyErr = pumpify(
|
202 | through(),
|
203 | through(function(chunk, _, cb) {
|
204 | cb(new Error('test'))
|
205 | }),
|
206 | ws
|
207 | )
|
208 |
|
209 | rs.pipe(pumpifyErr)
|
210 | .on('error', function (err) {
|
211 | t.ok(err)
|
212 | t.ok(err.message !== 'premature close', 'does not close with premature close')
|
213 | t.end()
|
214 | })
|
215 | })
|
216 |
|
217 | tape('returns error from duplexify', function (t) {
|
218 | var a = through()
|
219 | var b = duplexify()
|
220 | var s = pumpify()
|
221 |
|
222 | s.setPipeline(a, b)
|
223 |
|
224 | s.on('error', function (err) {
|
225 | t.same(err.message, 'stop')
|
226 | t.end()
|
227 | })
|
228 |
|
229 | s.write('data')
|
230 |
|
231 | s.end()
|
232 |
|
233 | b.setWritable(through())
|
234 |
|
235 | setImmediate(function () {
|
236 | b.destroy(new Error('stop'))
|
237 | })
|
238 | })
|