UNPKG

7.91 kBJavaScriptView Raw
1const test = require('tape')
2 , through2 = require('../')
3 , crypto = require('crypto')
4 , bl = require('bl')
5 , spigot = require('stream-spigot')
6
7test('plain through', function (t) {
8 var th2 = through2(function (chunk, enc, callback) {
9 if (!this._i)
10 this._i = 97 // 'a'
11 else
12 this._i++
13 var b = new Buffer(chunk.length)
14 for (var i = 0; i < chunk.length; i++)
15 b[i] = this._i
16 this.push(b)
17 callback()
18 })
19
20 th2.pipe(bl(function (err, b) {
21 var s = b.toString('ascii')
22 t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string')
23 t.end()
24 }))
25
26 th2.write(crypto.randomBytes(10))
27 th2.write(crypto.randomBytes(5))
28 th2.write(crypto.randomBytes(10))
29 th2.end()
30})
31
32test('pipeable through', function (t) {
33 var th2 = through2(function (chunk, enc, callback) {
34 if (!this._i)
35 this._i = 97 // 'a'
36 else
37 this._i++
38 var b = new Buffer(chunk.length)
39 for (var i = 0; i < chunk.length; i++)
40 b[i] = this._i
41 this.push(b)
42 callback()
43 })
44
45 th2.pipe(bl(function (err, b) {
46 var s = b.toString('ascii')
47 // bl() acts like a proper streams2 stream and passes as much as it's
48 // asked for, so we really only get one write with such a small amount
49 // of data
50 t.equal(s, 'aaaaaaaaaaaaaaaaaaaaaaaaa', 'got transformed string')
51 t.end()
52 }))
53
54 var bufs = bl()
55 bufs.append(crypto.randomBytes(10))
56 bufs.append(crypto.randomBytes(5))
57 bufs.append(crypto.randomBytes(10))
58 bufs.pipe(th2)
59})
60
61test('object through', function (t) {
62 t.plan(3)
63
64 var th2 = through2({ objectMode: true}, function (chunk, enc, callback) {
65 this.push({ out: chunk.in + 1 })
66 callback()
67 })
68
69 var e = 0
70 th2.on('data', function (o) {
71 t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
72 e++
73 })
74
75 th2.write({ in: 101 })
76 th2.write({ in: 202 })
77 th2.write({ in: -100 })
78 th2.end()
79})
80
81test('object through with through2.obj', function (t) {
82 t.plan(3)
83
84 var th2 = through2.obj(function (chunk, enc, callback) {
85 this.push({ out: chunk.in + 1 })
86 callback()
87 })
88
89 var e = 0
90 th2.on('data', function (o) {
91 t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
92 e++
93 })
94
95 th2.write({ in: 101 })
96 th2.write({ in: 202 })
97 th2.write({ in: -100 })
98 th2.end()
99})
100
101test('flushing through', function (t) {
102 var th2 = through2(function (chunk, enc, callback) {
103 if (!this._i)
104 this._i = 97 // 'a'
105 else
106 this._i++
107 var b = new Buffer(chunk.length)
108 for (var i = 0; i < chunk.length; i++)
109 b[i] = this._i
110 this.push(b)
111 callback()
112 }, function (callback) {
113 this.push(new Buffer([ 101, 110, 100 ]))
114 callback()
115 })
116
117 th2.pipe(bl(function (err, b) {
118 var s = b.toString('ascii')
119 t.equal(s, 'aaaaaaaaaabbbbbccccccccccend', 'got transformed string')
120 t.end()
121 }))
122
123 th2.write(crypto.randomBytes(10))
124 th2.write(crypto.randomBytes(5))
125 th2.write(crypto.randomBytes(10))
126 th2.end()
127})
128
129test('plain through ctor', function (t) {
130 var Th2 = through2.ctor(function (chunk, enc, callback) {
131 if (!this._i)
132 this._i = 97 // 'a'
133 else
134 this._i++
135 var b = new Buffer(chunk.length)
136 for (var i = 0; i < chunk.length; i++)
137 b[i] = this._i
138 this.push(b)
139 callback()
140 })
141
142 var th2 = new Th2()
143
144 th2.pipe(bl(function (err, b) {
145 var s = b.toString('ascii')
146 t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string')
147 t.end()
148 }))
149
150 th2.write(crypto.randomBytes(10))
151 th2.write(crypto.randomBytes(5))
152 th2.write(crypto.randomBytes(10))
153 th2.end()
154})
155
156test('reuse through ctor', function (t) {
157 t.plan(4)
158
159 var Th2 = through2.ctor(function (chunk, enc, callback) {
160 if (!this._i) {
161 t.ok(1, 'did not contain previous instance data (this._i)')
162 this._i = 97 // 'a'
163 } else
164 this._i++
165 var b = new Buffer(chunk.length)
166 for (var i = 0; i < chunk.length; i++)
167 b[i] = this._i
168 this.push(b)
169 callback()
170 })
171
172 var th2 = Th2()
173
174 th2.pipe(bl(function (err, b) {
175 var s = b.toString('ascii')
176 t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string')
177
178 var newInstance = Th2()
179 newInstance.pipe(bl(function (err, b) {
180 var s = b.toString('ascii')
181 t.equal('aaaaaaabbbbccccccc', s, 'got transformed string')
182 }))
183
184 newInstance.write(crypto.randomBytes(7))
185 newInstance.write(crypto.randomBytes(4))
186 newInstance.write(crypto.randomBytes(7))
187 newInstance.end()
188 }))
189
190 th2.write(crypto.randomBytes(10))
191 th2.write(crypto.randomBytes(5))
192 th2.write(crypto.randomBytes(10))
193 th2.end()
194})
195
196test('object through ctor', function (t) {
197 t.plan(3)
198
199 var Th2 = through2.ctor({ objectMode: true}, function (chunk, enc, callback) {
200 this.push({ out: chunk.in + 1 })
201 callback()
202 })
203
204 var th2 = new Th2()
205
206 var e = 0
207 th2.on('data', function (o) {
208 t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
209 e++
210 })
211
212 th2.write({ in: 101 })
213 th2.write({ in: 202 })
214 th2.write({ in: -100 })
215 th2.end()
216})
217
218test('pipeable object through ctor', function (t) {
219 t.plan(4)
220
221 var Th2 = through2.ctor({ objectMode: true}, function (record, enc, callback) {
222 if (record.temp != null && record.unit == 'F') {
223 record.temp = ( ( record.temp - 32 ) * 5 ) / 9
224 record.unit = 'C'
225 }
226 this.push(record)
227 callback()
228 })
229
230 var th2 = Th2()
231
232 var expect = [-19, -40, 100, 22]
233 th2.on('data', function (o) {
234 t.deepEqual(o, { temp: expect.shift(), unit: 'C' }, 'got transformed object')
235 })
236
237 spigot({objectMode: true}, [
238 {temp: -2.2, unit: 'F'},
239 {temp: -40, unit: 'F'},
240 {temp: 212, unit: 'F'},
241 {temp: 22, unit: 'C'}
242 ]).pipe(th2)
243})
244
245test('object through ctor override', function (t) {
246 t.plan(3)
247
248 var Th2 = through2.ctor(function (chunk, enc, callback) {
249 this.push({ out: chunk.in + 1 })
250 callback()
251 })
252
253 var th2 = Th2({objectMode: true})
254
255 var e = 0
256 th2.on('data', function (o) {
257 t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
258 e++
259 })
260
261 th2.write({ in: 101 })
262 th2.write({ in: 202 })
263 th2.write({ in: -100 })
264 th2.end()
265})
266
267test('object settings available in transform', function (t) {
268 t.plan(6)
269
270 var Th2 = through2.ctor({objectMode: true, peek: true}, function (chunk, enc, callback) {
271 t.ok(this.options.peek, "reading options from inside _transform")
272 this.push({ out: chunk.in + 1 })
273 callback()
274 })
275
276 var th2 = Th2()
277
278 var e = 0
279 th2.on('data', function (o) {
280 t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
281 e++
282 })
283
284 th2.write({ in: 101 })
285 th2.write({ in: 202 })
286 th2.write({ in: -100 })
287 th2.end()
288})
289
290test('object settings available in transform override', function (t) {
291 t.plan(6)
292
293 var Th2 = through2.ctor(function (chunk, enc, callback) {
294 t.ok(this.options.peek, "reading options from inside _transform")
295 this.push({ out: chunk.in + 1 })
296 callback()
297 })
298
299 var th2 = Th2({objectMode: true, peek: true})
300
301 var e = 0
302 th2.on('data', function (o) {
303 t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
304 e++
305 })
306
307 th2.write({ in: 101 })
308 th2.write({ in: 202 })
309 th2.write({ in: -100 })
310 th2.end()
311})
312
313test('object override extends options', function (t) {
314 t.plan(6)
315
316 var Th2 = through2.ctor({objectMode: true}, function (chunk, enc, callback) {
317 t.ok(this.options.peek, "reading options from inside _transform")
318 this.push({ out: chunk.in + 1 })
319 callback()
320 })
321
322 var th2 = Th2({peek: true})
323
324 var e = 0
325 th2.on('data', function (o) {
326 t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
327 e++
328 })
329
330 th2.write({ in: 101 })
331 th2.write({ in: 202 })
332 th2.write({ in: -100 })
333 th2.end()
334})
\No newline at end of file