1 | var Transform = require('readable-stream/transform')
|
2 | , inherits = require('util').inherits
|
3 | , xtend = require('xtend')
|
4 |
|
5 |
|
6 |
|
7 | function noop (chunk, enc, callback) {
|
8 | callback(null, chunk)
|
9 | }
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | function through2 (construct) {
|
15 | return function (options, transform, flush) {
|
16 | if (typeof options == 'function') {
|
17 | flush = transform
|
18 | transform = options
|
19 | options = {}
|
20 | }
|
21 |
|
22 | if (typeof transform != 'function')
|
23 | transform = noop
|
24 |
|
25 | if (typeof flush != 'function')
|
26 | flush = null
|
27 |
|
28 | return construct(options, transform, flush)
|
29 | }
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 | module.exports = through2(function (options, transform, flush) {
|
35 | var t2 = new Transform(options)
|
36 |
|
37 | t2._transform = transform
|
38 |
|
39 | if (flush)
|
40 | t2._flush = flush
|
41 |
|
42 | return t2
|
43 | })
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | module.exports.ctor = through2(function (options, transform, flush) {
|
49 | function Through2 (override) {
|
50 | if (!(this instanceof Through2))
|
51 | return new Through2(override)
|
52 |
|
53 | this.options = xtend(options, override)
|
54 |
|
55 | Transform.call(this, this.options)
|
56 | }
|
57 |
|
58 | inherits(Through2, Transform)
|
59 |
|
60 | Through2.prototype._transform = transform
|
61 |
|
62 | if (flush)
|
63 | Through2.prototype._flush = flush
|
64 |
|
65 | return Through2
|
66 | })
|
67 |
|
68 |
|
69 | module.exports.obj = through2(function (options, transform, flush) {
|
70 | var t2 = new Transform(xtend({ objectMode: true, highWaterMark: 16 }, options))
|
71 |
|
72 | t2._transform = transform
|
73 |
|
74 | if (flush)
|
75 | t2._flush = flush
|
76 |
|
77 | return t2
|
78 | })
|