Comparing version 3.0.2 to 4.0.0
{ | ||
"name": "through2", | ||
"version": "3.0.2", | ||
"version": "4.0.0", | ||
"description": "A tiny wrapper around Node.js streams.Transform (Streams2/3) to avoid explicit subclassing noise", | ||
"main": "through2.js", | ||
"scripts": { | ||
"test": "nyc node test/test.js | faucet && nyc report" | ||
"test:node": "hundreds mocha test/test.js", | ||
"test:browser": "node -e 'process.exit(process.version.startsWith(\"v8.\") ? 0 : 1)' || polendina --cleanup --runner=mocha test/test.js", | ||
"test": "npm run lint && npm run test:node && npm run test:browser", | ||
"lint": "standard", | ||
"coverage": "c8 --reporter=text --reporter=html mocha test/test.js && npx st -d coverage -p 8888" | ||
}, | ||
@@ -23,11 +27,14 @@ "repository": { | ||
"inherits": "^2.0.4", | ||
"readable-stream": "2 || 3" | ||
"readable-stream": "3" | ||
}, | ||
"devDependencies": { | ||
"bl": "~2.0.1", | ||
"faucet": "0.0.1", | ||
"nyc": "~13.1.0", | ||
"stream-spigot": "~3.0.6", | ||
"tape": "~4.9.1" | ||
"bl": "^4.0.2", | ||
"buffer": "^5.6.0", | ||
"chai": "^4.2.0", | ||
"hundreds": "~0.0.7", | ||
"mocha": "^7.2.0", | ||
"polendina": "^1.0.0", | ||
"standard": "^14.3.4", | ||
"stream-spigot": "^3.0.6" | ||
} | ||
} |
# through2 | ||
![Build & Test](https://github.com/rvagg/through2/workflows/Build%20&%20Test/badge.svg) | ||
[![NPM](https://nodei.co/npm/through2.png?downloads&downloadRank)](https://nodei.co/npm/through2/) | ||
@@ -9,8 +11,6 @@ | ||
***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._ | ||
```js | ||
fs.createReadStream('ex.txt') | ||
.pipe(through2(function (chunk, enc, callback) { | ||
for (var i = 0; i < chunk.length; i++) | ||
for (let i = 0; i < chunk.length; i++) | ||
if (chunk[i] == 97) | ||
@@ -30,3 +30,3 @@ chunk[i] = 122 // swap 'a' for 'z' | ||
```js | ||
var all = [] | ||
const all = [] | ||
@@ -36,3 +36,3 @@ fs.createReadStream('data.csv') | ||
.pipe(through2.obj(function (chunk, enc, callback) { | ||
var data = { | ||
conat data = { | ||
name : chunk[0] | ||
@@ -56,2 +56,16 @@ , address : chunk[3] | ||
## Do you need this? | ||
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'`): | ||
```js | ||
const { Transform } = require('readable-stream') | ||
const transformer = new Transform({ | ||
transform(chunk, enc, callback) { | ||
// ... | ||
} | ||
}) | ||
``` | ||
## API | ||
@@ -111,3 +125,3 @@ | ||
```js | ||
var FToC = through2.ctor({objectMode: true}, function (record, encoding, callback) { | ||
const FToC = through2.ctor({objectMode: true}, function (record, encoding, callback) { | ||
if (record.temp != null && record.unit == "F") { | ||
@@ -122,19 +136,11 @@ record.temp = ( ( record.temp - 32 ) * 5 ) / 9 | ||
// Create instances of FToC like so: | ||
var converter = new FToC() | ||
const converter = new FToC() | ||
// Or: | ||
var converter = FToC() | ||
const converter = FToC() | ||
// Or specify/override options when you instantiate, if you prefer: | ||
var converter = FToC({objectMode: true}) | ||
const converter = FToC({objectMode: true}) | ||
``` | ||
## See Also | ||
- [through2-map](https://github.com/brycebaril/through2-map) - Array.prototype.map analog for streams. | ||
- [through2-filter](https://github.com/brycebaril/through2-filter) - Array.prototype.filter analog for streams. | ||
- [through2-reduce](https://github.com/brycebaril/through2-reduce) - Array.prototype.reduce analog for streams. | ||
- [through2-spy](https://github.com/brycebaril/through2-spy) - Wrapper for simple stream.PassThrough spies. | ||
- the [mississippi stream utility collection](https://github.com/maxogden/mississippi) includes `through2` as well as many more useful stream modules similar to this one | ||
## License | ||
**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. | ||
**through2** is Copyright © 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. |
@@ -1,44 +0,28 @@ | ||
var Transform = require('readable-stream').Transform | ||
, inherits = require('inherits') | ||
const { Transform } = require('readable-stream') | ||
function DestroyableTransform(opts) { | ||
Transform.call(this, opts) | ||
this._destroyed = false | ||
} | ||
inherits(DestroyableTransform, Transform) | ||
DestroyableTransform.prototype.destroy = function(err) { | ||
if (this._destroyed) return | ||
this._destroyed = true | ||
var self = this | ||
process.nextTick(function() { | ||
if (err) | ||
self.emit('error', err) | ||
self.emit('close') | ||
function inherits (fn, sup) { | ||
fn.super_ = sup | ||
fn.prototype = Object.create(sup.prototype, { | ||
constructor: { value: fn, enumerable: false, writable: true, configurable: true } | ||
}) | ||
} | ||
// a noop _transform function | ||
function noop (chunk, enc, callback) { | ||
callback(null, chunk) | ||
} | ||
// create a new export function, used by both the main export and | ||
// the .ctor export, contains common logic for dealing with arguments | ||
function through2 (construct) { | ||
return function (options, transform, flush) { | ||
if (typeof options == 'function') { | ||
flush = transform | ||
return (options, transform, flush) => { | ||
if (typeof options === 'function') { | ||
flush = transform | ||
transform = options | ||
options = {} | ||
options = {} | ||
} | ||
if (typeof transform != 'function') | ||
transform = noop | ||
if (typeof transform !== 'function') { | ||
// noop | ||
transform = (chunk, enc, cb) => cb(null, chunk) | ||
} | ||
if (typeof flush != 'function') | ||
if (typeof flush !== 'function') { | ||
flush = null | ||
} | ||
@@ -49,11 +33,11 @@ return construct(options, transform, flush) | ||
// main export, just make me a transform stream! | ||
module.exports = through2(function (options, transform, flush) { | ||
var t2 = new DestroyableTransform(options) | ||
const make = through2((options, transform, flush) => { | ||
const t2 = new Transform(options) | ||
t2._transform = transform | ||
if (flush) | ||
if (flush) { | ||
t2._flush = flush | ||
} | ||
@@ -63,35 +47,39 @@ return t2 | ||
// make me a reusable prototype that I can `new`, or implicitly `new` | ||
// with a constructor call | ||
module.exports.ctor = through2(function (options, transform, flush) { | ||
const ctor = through2((options, transform, flush) => { | ||
function Through2 (override) { | ||
if (!(this instanceof Through2)) | ||
if (!(this instanceof Through2)) { | ||
return new Through2(override) | ||
} | ||
this.options = Object.assign({}, options, override) | ||
DestroyableTransform.call(this, this.options) | ||
Transform.call(this, this.options) | ||
this._transform = transform | ||
if (flush) { | ||
this._flush = flush | ||
} | ||
} | ||
inherits(Through2, DestroyableTransform) | ||
inherits(Through2, Transform) | ||
Through2.prototype._transform = transform | ||
if (flush) | ||
Through2.prototype._flush = flush | ||
return Through2 | ||
}) | ||
const obj = through2(function (options, transform, flush) { | ||
const t2 = new Transform(Object.assign({ objectMode: true, highWaterMark: 16 }, options)) | ||
module.exports.obj = through2(function (options, transform, flush) { | ||
var t2 = new DestroyableTransform(Object.assign({ objectMode: true, highWaterMark: 16 }, options)) | ||
t2._transform = transform | ||
if (flush) | ||
if (flush) { | ||
t2._flush = flush | ||
} | ||
return t2 | ||
}) | ||
module.exports = make | ||
module.exports.ctor = ctor | ||
module.exports.obj = obj |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
829739
6
141
8
63
Updatedreadable-stream@3