through2 3.0.1 → 4.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,17 +1,17 @@
1
1
  # through2
2
2
 
3
+ ![Build & Test](https://github.com/rvagg/through2/workflows/Build%20&%20Test/badge.svg)
4
+
3
5
  [![NPM](https://nodei.co/npm/through2.png?downloads&downloadRank)](https://nodei.co/npm/through2/)
4
6
 
5
7
  **A tiny wrapper around Node.js streams.Transform (Streams2/3) to avoid explicit subclassing noise**
6
8
 
7
9
  Inspired by [Dominic Tarr](https://github.com/dominictarr)'s [through](https://github.com/dominictarr/through) in that it's so much easier to make a stream out of a function than it is to set up the prototype chain properly: `through(function (chunk) { ... })`.
8
10
 
9
- ***Note: Users of Node.js 0.10 and 0.12 should install `through2@2.x`. As of through2@3.x, readable-stream@3 is being used and is not compatible with older versions of Node.js.*** _v2.x support is being maintained on the [v2.x](https://github.com/rvagg/through2/tree/v2.x) branch._
10
-
11
11
  ```js
12
12
  fs.createReadStream('ex.txt')
13
13
  .pipe(through2(function (chunk, enc, callback) {
14
- for (var i = 0; i < chunk.length; i++)
14
+ for (let i = 0; i < chunk.length; i++)
15
15
  if (chunk[i] == 97)
16
16
  chunk[i] = 122 // swap 'a' for 'z'
17
17
 
@@ -26,12 +26,12 @@ fs.createReadStream('ex.txt')
26
26
  Or object streams:
27
27
 
28
28
  ```js
29
- var all = []
29
+ const all = []
30
30
 
31
31
  fs.createReadStream('data.csv')
32
32
  .pipe(csv2())
33
33
  .pipe(through2.obj(function (chunk, enc, callback) {
34
- var data = {
34
+ const data = {
35
35
  name : chunk[0]
36
36
  , address : chunk[3]
37
37
  , phone : chunk[10]
@@ -50,11 +50,25 @@ fs.createReadStream('data.csv')
50
50
 
51
51
  Note that `through2.obj(fn)` is a convenience wrapper around `through2({ objectMode: true }, fn)`.
52
52
 
53
+ ## Do you need this?
54
+
55
+ Since Node.js introduced [Simplified Stream Construction](https://nodejs.org/api/stream.html#stream_simplified_construction), many uses of **through2** have become redundant. Consider whether you really need to use **through2** or just want to use the `'readable-stream'` package, or the core `'stream'` package (which is derived from `'readable-stream'`):
56
+
57
+ ```js
58
+ const { Transform } = require('readable-stream')
59
+
60
+ const transformer = new Transform({
61
+ transform(chunk, enc, callback) {
62
+ // ...
63
+ }
64
+ })
65
+ ```
66
+
53
67
  ## API
54
68
 
55
69
  <b><code>through2([ options, ] [ transformFunction ] [, flushFunction ])</code></b>
56
70
 
57
- Consult the **[stream.Transform](http://nodejs.org/docs/latest/api/stream.html#stream_class_stream_transform)** documentation for the exact rules of the `transformFunction` (i.e. `this._transform`) and the optional `flushFunction` (i.e. `this._flush`).
71
+ Consult the **[stream.Transform](https://nodejs.org/docs/latest/api/stream.html#stream_class_stream_transform)** documentation for the exact rules of the `transformFunction` (i.e. `this._transform`) and the optional `flushFunction` (i.e. `this._flush`).
58
72
 
59
73
  ### options
60
74
 
@@ -69,7 +83,7 @@ fs.createReadStream('/tmp/important.dat')
69
83
  cb(null, 'wut?') // note we can use the second argument on the callback
70
84
  // to provide data as an alternative to this.push('wut?')
71
85
  }
72
- )
86
+ ))
73
87
  .pipe(fs.createWriteStream('/tmp/wut.txt'))
74
88
  ```
75
89
 
@@ -104,7 +118,7 @@ fs.createReadStream('/tmp/important.dat')
104
118
  Instead of returning a `stream.Transform` instance, `through2.ctor()` returns a **constructor** for a custom Transform. This is useful when you want to use the same transform logic in multiple instances.
105
119
 
106
120
  ```js
107
- var FToC = through2.ctor({objectMode: true}, function (record, encoding, callback) {
121
+ const FToC = through2.ctor({objectMode: true}, function (record, encoding, callback) {
108
122
  if (record.temp != null && record.unit == "F") {
109
123
  record.temp = ( ( record.temp - 32 ) * 5 ) / 9
110
124
  record.unit = "C"
@@ -114,21 +128,13 @@ var FToC = through2.ctor({objectMode: true}, function (record, encoding, callbac
114
128
  })
115
129
 
116
130
  // Create instances of FToC like so:
117
- var converter = new FToC()
131
+ const converter = new FToC()
118
132
  // Or:
119
- var converter = FToC()
133
+ const converter = FToC()
120
134
  // Or specify/override options when you instantiate, if you prefer:
121
- var converter = FToC({objectMode: true})
135
+ const converter = FToC({objectMode: true})
122
136
  ```
123
137
 
124
- ## See Also
125
-
126
- - [through2-map](https://github.com/brycebaril/through2-map) - Array.prototype.map analog for streams.
127
- - [through2-filter](https://github.com/brycebaril/through2-filter) - Array.prototype.filter analog for streams.
128
- - [through2-reduce](https://github.com/brycebaril/through2-reduce) - Array.prototype.reduce analog for streams.
129
- - [through2-spy](https://github.com/brycebaril/through2-spy) - Wrapper for simple stream.PassThrough spies.
130
- - the [mississippi stream utility collection](https://github.com/maxogden/mississippi) includes `through2` as well as many more useful stream modules similar to this one
131
-
132
138
  ## License
133
139
 
134
- **through2** is Copyright (c) Rod Vagg and additional contributors and licensed under the MIT license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.
140
+ **through2** is Copyright &copy; Rod Vagg and additional contributors and licensed under the MIT license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.
package/package.json CHANGED
@@ -1,10 +1,14 @@
1
1
  {
2
2
  "name": "through2",
3
- "version": "3.0.1",
3
+ "version": "4.0.2",
4
4
  "description": "A tiny wrapper around Node.js streams.Transform (Streams2/3) to avoid explicit subclassing noise",
5
5
  "main": "through2.js",
6
6
  "scripts": {
7
- "test": "nyc node test/test.js | faucet && nyc report"
7
+ "test:node": "hundreds mocha test/test.js",
8
+ "test:browser": "node -e 'process.exit(process.version.startsWith(\"v8.\") ? 0 : 1)' || polendina --cleanup --runner=mocha test/test.js",
9
+ "test": "npm run lint && npm run test:node && npm run test:browser",
10
+ "lint": "standard",
11
+ "coverage": "c8 --reporter=text --reporter=html mocha test/test.js && npx st -d coverage -p 8888"
8
12
  },
9
13
  "repository": {
10
14
  "type": "git",
@@ -19,13 +23,16 @@
19
23
  "author": "Rod Vagg <r@va.gg> (https://github.com/rvagg)",
20
24
  "license": "MIT",
21
25
  "dependencies": {
22
- "readable-stream": "2 || 3"
26
+ "readable-stream": "3"
23
27
  },
24
28
  "devDependencies": {
25
- "bl": "~2.0.1",
26
- "faucet": "0.0.1",
27
- "nyc": "~13.1.0",
28
- "stream-spigot": "~3.0.6",
29
- "tape": "~4.9.1"
29
+ "bl": "^4.0.2",
30
+ "buffer": "^5.6.0",
31
+ "chai": "^4.2.0",
32
+ "hundreds": "~0.0.7",
33
+ "mocha": "^7.2.0",
34
+ "polendina": "^1.0.0",
35
+ "standard": "^14.3.4",
36
+ "stream-spigot": "^3.0.6"
30
37
  }
31
38
  }
package/through2.js CHANGED
@@ -1,95 +1,83 @@
1
- var Transform = require('readable-stream').Transform
2
- , inherits = require('util').inherits
1
+ const { Transform } = require('readable-stream')
3
2
 
4
- function DestroyableTransform(opts) {
5
- Transform.call(this, opts)
6
- this._destroyed = false
7
- }
8
-
9
- inherits(DestroyableTransform, Transform)
10
-
11
- DestroyableTransform.prototype.destroy = function(err) {
12
- if (this._destroyed) return
13
- this._destroyed = true
14
-
15
- var self = this
16
- process.nextTick(function() {
17
- if (err)
18
- self.emit('error', err)
19
- self.emit('close')
3
+ function inherits (fn, sup) {
4
+ fn.super_ = sup
5
+ fn.prototype = Object.create(sup.prototype, {
6
+ constructor: { value: fn, enumerable: false, writable: true, configurable: true }
20
7
  })
21
8
  }
22
9
 
23
- // a noop _transform function
24
- function noop (chunk, enc, callback) {
25
- callback(null, chunk)
26
- }
27
-
28
-
29
10
  // create a new export function, used by both the main export and
30
11
  // the .ctor export, contains common logic for dealing with arguments
31
12
  function through2 (construct) {
32
- return function (options, transform, flush) {
33
- if (typeof options == 'function') {
34
- flush = transform
13
+ return (options, transform, flush) => {
14
+ if (typeof options === 'function') {
15
+ flush = transform
35
16
  transform = options
36
- options = {}
17
+ options = {}
37
18
  }
38
19
 
39
- if (typeof transform != 'function')
40
- transform = noop
20
+ if (typeof transform !== 'function') {
21
+ // noop
22
+ transform = (chunk, enc, cb) => cb(null, chunk)
23
+ }
41
24
 
42
- if (typeof flush != 'function')
25
+ if (typeof flush !== 'function') {
43
26
  flush = null
27
+ }
44
28
 
45
29
  return construct(options, transform, flush)
46
30
  }
47
31
  }
48
32
 
49
-
50
33
  // main export, just make me a transform stream!
51
- module.exports = through2(function (options, transform, flush) {
52
- var t2 = new DestroyableTransform(options)
34
+ const make = through2((options, transform, flush) => {
35
+ const t2 = new Transform(options)
53
36
 
54
37
  t2._transform = transform
55
38
 
56
- if (flush)
39
+ if (flush) {
57
40
  t2._flush = flush
41
+ }
58
42
 
59
43
  return t2
60
44
  })
61
45
 
62
-
63
46
  // make me a reusable prototype that I can `new`, or implicitly `new`
64
47
  // with a constructor call
65
- module.exports.ctor = through2(function (options, transform, flush) {
48
+ const ctor = through2((options, transform, flush) => {
66
49
  function Through2 (override) {
67
- if (!(this instanceof Through2))
50
+ if (!(this instanceof Through2)) {
68
51
  return new Through2(override)
52
+ }
69
53
 
70
54
  this.options = Object.assign({}, options, override)
71
55
 
72
- DestroyableTransform.call(this, this.options)
73
- }
56
+ Transform.call(this, this.options)
74
57
 
75
- inherits(Through2, DestroyableTransform)
76
-
77
- Through2.prototype._transform = transform
58
+ this._transform = transform
59
+ if (flush) {
60
+ this._flush = flush
61
+ }
62
+ }
78
63
 
79
- if (flush)
80
- Through2.prototype._flush = flush
64
+ inherits(Through2, Transform)
81
65
 
82
66
  return Through2
83
67
  })
84
68
 
85
-
86
- module.exports.obj = through2(function (options, transform, flush) {
87
- var t2 = new DestroyableTransform(Object.assign({ objectMode: true, highWaterMark: 16 }, options))
69
+ const obj = through2(function (options, transform, flush) {
70
+ const t2 = new Transform(Object.assign({ objectMode: true, highWaterMark: 16 }, options))
88
71
 
89
72
  t2._transform = transform
90
73
 
91
- if (flush)
74
+ if (flush) {
92
75
  t2._flush = flush
76
+ }
93
77
 
94
78
  return t2
95
79
  })
80
+
81
+ module.exports = make
82
+ module.exports.ctor = ctor
83
+ module.exports.obj = obj
@@ -1 +0,0 @@
1
- {"/Users/rvagg/git/through2/through2.js":{"path":"/Users/rvagg/git/through2/through2.js","statementMap":{"0":{"start":{"line":1,"column":16},"end":{"line":1,"column":52}},"1":{"start":{"line":2,"column":16},"end":{"line":2,"column":40}},"2":{"start":{"line":5,"column":2},"end":{"line":5,"column":28}},"3":{"start":{"line":6,"column":2},"end":{"line":6,"column":25}},"4":{"start":{"line":9,"column":0},"end":{"line":9,"column":41}},"5":{"start":{"line":11,"column":0},"end":{"line":21,"column":1}},"6":{"start":{"line":12,"column":2},"end":{"line":12,"column":29}},"7":{"start":{"line":12,"column":23},"end":{"line":12,"column":29}},"8":{"start":{"line":13,"column":2},"end":{"line":13,"column":24}},"9":{"start":{"line":15,"column":13},"end":{"line":15,"column":17}},"10":{"start":{"line":16,"column":2},"end":{"line":20,"column":4}},"11":{"start":{"line":17,"column":4},"end":{"line":18,"column":29}},"12":{"start":{"line":18,"column":6},"end":{"line":18,"column":29}},"13":{"start":{"line":19,"column":4},"end":{"line":19,"column":22}},"14":{"start":{"line":25,"column":2},"end":{"line":25,"column":23}},"15":{"start":{"line":32,"column":2},"end":{"line":46,"column":3}},"16":{"start":{"line":33,"column":4},"end":{"line":37,"column":5}},"17":{"start":{"line":34,"column":6},"end":{"line":34,"column":27}},"18":{"start":{"line":35,"column":6},"end":{"line":35,"column":25}},"19":{"start":{"line":36,"column":6},"end":{"line":36,"column":20}},"20":{"start":{"line":39,"column":4},"end":{"line":40,"column":22}},"21":{"start":{"line":40,"column":6},"end":{"line":40,"column":22}},"22":{"start":{"line":42,"column":4},"end":{"line":43,"column":18}},"23":{"start":{"line":43,"column":6},"end":{"line":43,"column":18}},"24":{"start":{"line":45,"column":4},"end":{"line":45,"column":47}},"25":{"start":{"line":51,"column":0},"end":{"line":60,"column":2}},"26":{"start":{"line":52,"column":11},"end":{"line":52,"column":44}},"27":{"start":{"line":54,"column":2},"end":{"line":54,"column":27}},"28":{"start":{"line":56,"column":2},"end":{"line":57,"column":21}},"29":{"start":{"line":57,"column":4},"end":{"line":57,"column":21}},"30":{"start":{"line":59,"column":2},"end":{"line":59,"column":11}},"31":{"start":{"line":65,"column":0},"end":{"line":83,"column":2}},"32":{"start":{"line":67,"column":4},"end":{"line":68,"column":35}},"33":{"start":{"line":68,"column":6},"end":{"line":68,"column":35}},"34":{"start":{"line":70,"column":4},"end":{"line":70,"column":55}},"35":{"start":{"line":72,"column":4},"end":{"line":72,"column":49}},"36":{"start":{"line":75,"column":2},"end":{"line":75,"column":42}},"37":{"start":{"line":77,"column":2},"end":{"line":77,"column":43}},"38":{"start":{"line":79,"column":2},"end":{"line":80,"column":37}},"39":{"start":{"line":80,"column":4},"end":{"line":80,"column":37}},"40":{"start":{"line":82,"column":2},"end":{"line":82,"column":17}},"41":{"start":{"line":86,"column":0},"end":{"line":95,"column":2}},"42":{"start":{"line":87,"column":11},"end":{"line":87,"column":100}},"43":{"start":{"line":89,"column":2},"end":{"line":89,"column":27}},"44":{"start":{"line":91,"column":2},"end":{"line":92,"column":21}},"45":{"start":{"line":92,"column":4},"end":{"line":92,"column":21}},"46":{"start":{"line":94,"column":2},"end":{"line":94,"column":11}}},"fnMap":{"0":{"name":"DestroyableTransform","decl":{"start":{"line":4,"column":9},"end":{"line":4,"column":29}},"loc":{"start":{"line":4,"column":36},"end":{"line":7,"column":1}},"line":4},"1":{"name":"(anonymous_1)","decl":{"start":{"line":11,"column":41},"end":{"line":11,"column":42}},"loc":{"start":{"line":11,"column":55},"end":{"line":21,"column":1}},"line":11},"2":{"name":"(anonymous_2)","decl":{"start":{"line":16,"column":19},"end":{"line":16,"column":20}},"loc":{"start":{"line":16,"column":30},"end":{"line":20,"column":3}},"line":16},"3":{"name":"noop","decl":{"start":{"line":24,"column":9},"end":{"line":24,"column":13}},"loc":{"start":{"line":24,"column":37},"end":{"line":26,"column":1}},"line":24},"4":{"name":"through2","decl":{"start":{"line":31,"column":9},"end":{"line":31,"column":17}},"loc":{"start":{"line":31,"column":30},"end":{"line":47,"column":1}},"line":31},"5":{"name":"(anonymous_5)","decl":{"start":{"line":32,"column":9},"end":{"line":32,"column":10}},"loc":{"start":{"line":32,"column":46},"end":{"line":46,"column":3}},"line":32},"6":{"name":"(anonymous_6)","decl":{"start":{"line":51,"column":26},"end":{"line":51,"column":27}},"loc":{"start":{"line":51,"column":63},"end":{"line":60,"column":1}},"line":51},"7":{"name":"(anonymous_7)","decl":{"start":{"line":65,"column":31},"end":{"line":65,"column":32}},"loc":{"start":{"line":65,"column":68},"end":{"line":83,"column":1}},"line":65},"8":{"name":"Through2","decl":{"start":{"line":66,"column":11},"end":{"line":66,"column":19}},"loc":{"start":{"line":66,"column":31},"end":{"line":73,"column":3}},"line":66},"9":{"name":"(anonymous_9)","decl":{"start":{"line":86,"column":30},"end":{"line":86,"column":31}},"loc":{"start":{"line":86,"column":67},"end":{"line":95,"column":1}},"line":86}},"branchMap":{"0":{"loc":{"start":{"line":12,"column":2},"end":{"line":12,"column":29}},"type":"if","locations":[{"start":{"line":12,"column":2},"end":{"line":12,"column":29}},{"start":{"line":12,"column":2},"end":{"line":12,"column":29}}],"line":12},"1":{"loc":{"start":{"line":17,"column":4},"end":{"line":18,"column":29}},"type":"if","locations":[{"start":{"line":17,"column":4},"end":{"line":18,"column":29}},{"start":{"line":17,"column":4},"end":{"line":18,"column":29}}],"line":17},"2":{"loc":{"start":{"line":33,"column":4},"end":{"line":37,"column":5}},"type":"if","locations":[{"start":{"line":33,"column":4},"end":{"line":37,"column":5}},{"start":{"line":33,"column":4},"end":{"line":37,"column":5}}],"line":33},"3":{"loc":{"start":{"line":39,"column":4},"end":{"line":40,"column":22}},"type":"if","locations":[{"start":{"line":39,"column":4},"end":{"line":40,"column":22}},{"start":{"line":39,"column":4},"end":{"line":40,"column":22}}],"line":39},"4":{"loc":{"start":{"line":42,"column":4},"end":{"line":43,"column":18}},"type":"if","locations":[{"start":{"line":42,"column":4},"end":{"line":43,"column":18}},{"start":{"line":42,"column":4},"end":{"line":43,"column":18}}],"line":42},"5":{"loc":{"start":{"line":56,"column":2},"end":{"line":57,"column":21}},"type":"if","locations":[{"start":{"line":56,"column":2},"end":{"line":57,"column":21}},{"start":{"line":56,"column":2},"end":{"line":57,"column":21}}],"line":56},"6":{"loc":{"start":{"line":67,"column":4},"end":{"line":68,"column":35}},"type":"if","locations":[{"start":{"line":67,"column":4},"end":{"line":68,"column":35}},{"start":{"line":67,"column":4},"end":{"line":68,"column":35}}],"line":67},"7":{"loc":{"start":{"line":79,"column":2},"end":{"line":80,"column":37}},"type":"if","locations":[{"start":{"line":79,"column":2},"end":{"line":80,"column":37}},{"start":{"line":79,"column":2},"end":{"line":80,"column":37}}],"line":79},"8":{"loc":{"start":{"line":91,"column":2},"end":{"line":92,"column":21}},"type":"if","locations":[{"start":{"line":91,"column":2},"end":{"line":92,"column":21}},{"start":{"line":91,"column":2},"end":{"line":92,"column":21}}],"line":91}},"s":{"0":1,"1":1,"2":16,"3":16,"4":1,"5":1,"6":3,"7":1,"8":2,"9":2,"10":2,"11":2,"12":0,"13":2,"14":0,"15":3,"16":15,"17":8,"18":8,"19":8,"20":15,"21":2,"22":15,"23":14,"24":15,"25":1,"26":6,"27":6,"28":6,"29":1,"30":6,"31":1,"32":16,"33":7,"34":9,"35":9,"36":8,"37":8,"38":8,"39":0,"40":8,"41":1,"42":1,"43":1,"44":1,"45":0,"46":1},"f":{"0":16,"1":3,"2":2,"3":0,"4":3,"5":15,"6":6,"7":8,"8":16,"9":1},"b":{"0":[1,2],"1":[0,2],"2":[8,7],"3":[2,13],"4":[14,1],"5":[1,5],"6":[7,9],"7":[0,8],"8":[0,1]},"_coverageSchema":"43e27e138ebf9cfc5966b082cf9a028302ed4184","hash":"0e6d9c139041e468222a5271346b1d4b44840ea4","contentHash":"e8d9fa1f16cd3fbb5bfdcbe9002323f0"}}