diff --git a/.travis.yml b/.travis.yml index fdaa286..6be4615 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,8 @@ language: node_js node_js: -- '0.10' -- '0.11' +- '6' +- '8' +- '10' branches: only: - master @@ -9,8 +10,3 @@ notifications: email: - rod@vagg.org script: npm test -env: - global: - - SAUCE_USER=through2-sauce - - secure: A8KyygIlklZOcTwTGhZHpkOu7Cn4tI1QQasg75CTSjudczGzkuh1pG2RDIkWqGcrWv9o5CjI6fFYYx0JvQu8o5PO+ZAtV1Z0AlnM965vE8kAx0XEC7giAV323bibfnHwyQu74vIhbOfdes7E4/cYerV2kAjK0T375D4DYEpDuvw= - diff --git a/LICENSE.md b/LICENSE.md index 7f0b93d..a2429b6 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,9 +1,9 @@ # The MIT License (MIT) -**Copyright (c) 2016 Rod Vagg (the "Original Author") and additional contributors** +**Copyright (c) Rod Vagg (the "Original Author") and additional contributors** Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index a916f15..59d5b47 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![NPM](https://nodei.co/npm/through2.png?downloads&downloadRank)](https://nodei.co/npm/through2/) -**A tiny wrapper around Node streams.Transform (Streams2) to avoid explicit subclassing noise** +**A tiny wrapper around Node streams.Transform (Streams2/3) to avoid explicit subclassing noise** 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) { ... })`. @@ -133,4 +133,4 @@ var converter = FToC({objectMode: true}) ## License -**through2** is Copyright (c) 2013 Rod Vagg [@rvagg](https://twitter.com/rvagg) 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 (c) Rod Vagg [@rvagg](https://twitter.com/rvagg) 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. diff --git a/package.json b/package.json index 12d8fd6..f9b7d1c 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,7 @@ "description": "A tiny wrapper around Node streams2 Transform to avoid explicit subclassing noise", "main": "through2.js", "scripts": { - "test": "node test/test.js | faucet", - "test-local": "brtapsauce-local test/basic-test.js" + "test": "node test/test.js | faucet" }, "repository": { "type": "git", @@ -20,13 +19,13 @@ "author": "Rod Vagg (https://github.com/rvagg)", "license": "MIT", "dependencies": { - "readable-stream": "^2.1.5", + "readable-stream": "2 || 3", "xtend": "~4.0.1" }, "devDependencies": { - "bl": "~1.1.2", + "bl": "~2.0.1", "faucet": "0.0.1", - "stream-spigot": "~3.0.5", - "tape": "~4.6.2" + "stream-spigot": "~3.0.6", + "tape": "~4.9.1" } } diff --git a/test/basic-test.js b/test/basic-test.js deleted file mode 100644 index 63d2542..0000000 --- a/test/basic-test.js +++ /dev/null @@ -1,361 +0,0 @@ -const test = require('tape') - , through2 = require('../') - , crypto = require('crypto') - , bl = require('bl') - , spigot = require('stream-spigot') - -test('plain through', function (t) { - var th2 = through2(function (chunk, enc, callback) { - if (!this._i) - this._i = 97 // 'a' - else - this._i++ - var b = new Buffer(chunk.length) - for (var i = 0; i < chunk.length; i++) - b[i] = this._i - this.push(b) - callback() - }) - - th2.pipe(bl(function (err, b) { - var s = b.toString('ascii') - t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string') - t.end() - })) - - th2.write(crypto.randomBytes(10)) - th2.write(crypto.randomBytes(5)) - th2.write(crypto.randomBytes(10)) - th2.end() -}) - -test('pipeable through', function (t) { - var th2 = through2(function (chunk, enc, callback) { - if (!this._i) - this._i = 97 // 'a' - else - this._i++ - var b = new Buffer(chunk.length) - for (var i = 0; i < chunk.length; i++) - b[i] = this._i - this.push(b) - callback() - }) - - th2.pipe(bl(function (err, b) { - var s = b.toString('ascii') - // bl() acts like a proper streams2 stream and passes as much as it's - // asked for, so we really only get one write with such a small amount - // of data - t.equal(s, 'aaaaaaaaaaaaaaaaaaaaaaaaa', 'got transformed string') - t.end() - })) - - var bufs = bl() - bufs.append(crypto.randomBytes(10)) - bufs.append(crypto.randomBytes(5)) - bufs.append(crypto.randomBytes(10)) - bufs.pipe(th2) -}) - -test('object through', function (t) { - t.plan(3) - - var th2 = through2({ objectMode: true}, function (chunk, enc, callback) { - this.push({ out: chunk.in + 1 }) - callback() - }) - - var e = 0 - th2.on('data', function (o) { - t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object') - e++ - }) - - th2.write({ in: 101 }) - th2.write({ in: 202 }) - th2.write({ in: -100 }) - th2.end() -}) - -test('object through with through2.obj', function (t) { - t.plan(3) - - var th2 = through2.obj(function (chunk, enc, callback) { - this.push({ out: chunk.in + 1 }) - callback() - }) - - var e = 0 - th2.on('data', function (o) { - t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object') - e++ - }) - - th2.write({ in: 101 }) - th2.write({ in: 202 }) - th2.write({ in: -100 }) - th2.end() -}) - -test('flushing through', function (t) { - var th2 = through2(function (chunk, enc, callback) { - if (!this._i) - this._i = 97 // 'a' - else - this._i++ - var b = new Buffer(chunk.length) - for (var i = 0; i < chunk.length; i++) - b[i] = this._i - this.push(b) - callback() - }, function (callback) { - this.push(new Buffer([ 101, 110, 100 ])) - callback() - }) - - th2.pipe(bl(function (err, b) { - var s = b.toString('ascii') - t.equal(s, 'aaaaaaaaaabbbbbccccccccccend', 'got transformed string') - t.end() - })) - - th2.write(crypto.randomBytes(10)) - th2.write(crypto.randomBytes(5)) - th2.write(crypto.randomBytes(10)) - th2.end() -}) - -test('plain through ctor', function (t) { - var Th2 = through2.ctor(function (chunk, enc, callback) { - if (!this._i) - this._i = 97 // 'a' - else - this._i++ - var b = new Buffer(chunk.length) - for (var i = 0; i < chunk.length; i++) - b[i] = this._i - this.push(b) - callback() - }) - - var th2 = new Th2() - - th2.pipe(bl(function (err, b) { - var s = b.toString('ascii') - t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string') - t.end() - })) - - th2.write(crypto.randomBytes(10)) - th2.write(crypto.randomBytes(5)) - th2.write(crypto.randomBytes(10)) - th2.end() -}) - -test('reuse through ctor', function (t) { - t.plan(4) - - var Th2 = through2.ctor(function (chunk, enc, callback) { - if (!this._i) { - t.ok(1, 'did not contain previous instance data (this._i)') - this._i = 97 // 'a' - } else - this._i++ - var b = new Buffer(chunk.length) - for (var i = 0; i < chunk.length; i++) - b[i] = this._i - this.push(b) - callback() - }) - - var th2 = Th2() - - th2.pipe(bl(function (err, b) { - var s = b.toString('ascii') - t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string') - - var newInstance = Th2() - newInstance.pipe(bl(function (err, b) { - var s = b.toString('ascii') - t.equal('aaaaaaabbbbccccccc', s, 'got transformed string') - })) - - newInstance.write(crypto.randomBytes(7)) - newInstance.write(crypto.randomBytes(4)) - newInstance.write(crypto.randomBytes(7)) - newInstance.end() - })) - - th2.write(crypto.randomBytes(10)) - th2.write(crypto.randomBytes(5)) - th2.write(crypto.randomBytes(10)) - th2.end() -}) - -test('object through ctor', function (t) { - t.plan(3) - - var Th2 = through2.ctor({ objectMode: true}, function (chunk, enc, callback) { - this.push({ out: chunk.in + 1 }) - callback() - }) - - var th2 = new Th2() - - var e = 0 - th2.on('data', function (o) { - t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object') - e++ - }) - - th2.write({ in: 101 }) - th2.write({ in: 202 }) - th2.write({ in: -100 }) - th2.end() -}) - -test('pipeable object through ctor', function (t) { - t.plan(4) - - var Th2 = through2.ctor({ objectMode: true}, function (record, enc, callback) { - if (record.temp != null && record.unit == 'F') { - record.temp = ( ( record.temp - 32 ) * 5 ) / 9 - record.unit = 'C' - } - this.push(record) - callback() - }) - - var th2 = Th2() - - var expect = [-19, -40, 100, 22] - th2.on('data', function (o) { - t.deepEqual(o, { temp: expect.shift(), unit: 'C' }, 'got transformed object') - }) - - spigot({objectMode: true}, [ - {temp: -2.2, unit: 'F'}, - {temp: -40, unit: 'F'}, - {temp: 212, unit: 'F'}, - {temp: 22, unit: 'C'} - ]).pipe(th2) -}) - -test('object through ctor override', function (t) { - t.plan(3) - - var Th2 = through2.ctor(function (chunk, enc, callback) { - this.push({ out: chunk.in + 1 }) - callback() - }) - - var th2 = Th2({objectMode: true}) - - var e = 0 - th2.on('data', function (o) { - t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object') - e++ - }) - - th2.write({ in: 101 }) - th2.write({ in: 202 }) - th2.write({ in: -100 }) - th2.end() -}) - -test('object settings available in transform', function (t) { - t.plan(6) - - var Th2 = through2.ctor({objectMode: true, peek: true}, function (chunk, enc, callback) { - t.ok(this.options.peek, "reading options from inside _transform") - this.push({ out: chunk.in + 1 }) - callback() - }) - - var th2 = Th2() - - var e = 0 - th2.on('data', function (o) { - t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object') - e++ - }) - - th2.write({ in: 101 }) - th2.write({ in: 202 }) - th2.write({ in: -100 }) - th2.end() -}) - -test('object settings available in transform override', function (t) { - t.plan(6) - - var Th2 = through2.ctor(function (chunk, enc, callback) { - t.ok(this.options.peek, "reading options from inside _transform") - this.push({ out: chunk.in + 1 }) - callback() - }) - - var th2 = Th2({objectMode: true, peek: true}) - - var e = 0 - th2.on('data', function (o) { - t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object') - e++ - }) - - th2.write({ in: 101 }) - th2.write({ in: 202 }) - th2.write({ in: -100 }) - th2.end() -}) - -test('object override extends options', function (t) { - t.plan(6) - - var Th2 = through2.ctor({objectMode: true}, function (chunk, enc, callback) { - t.ok(this.options.peek, "reading options from inside _transform") - this.push({ out: chunk.in + 1 }) - callback() - }) - - var th2 = Th2({peek: true}) - - var e = 0 - th2.on('data', function (o) { - t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object') - e++ - }) - - th2.write({ in: 101 }) - th2.write({ in: 202 }) - th2.write({ in: -100 }) - th2.end() -}) - -test('can be destroyed', function(t) { - t.plan(1) - - var th = through2() - - th.on('close', function() { - t.ok(true, 'shoud emit close') - t.end() - }) - - th.destroy() -}) - -test('can be destroyed twice', function(t) { - t.plan(1) - - var th = through2() - - th.on('close', function() { - t.ok(true, 'shoud emit close') - t.end() - }) - - th.destroy() - th.destroy() -}) \ No newline at end of file diff --git a/test/sauce.js b/test/sauce.js deleted file mode 100644 index 7266a6f..0000000 --- a/test/sauce.js +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env node - -var user = process.env.SAUCE_USER - , key = process.env.SAUCE_KEY - , path = require('path') - , brtapsauce - , testFile = path.join(__dirname, 'basic-test.js') - -try { - brtapsauce = require('brtapsauce') -} catch (e) { - return -} - -const capabilities = [ - { browserName: 'chrome' , platform: 'Windows XP', version: '' } - , { browserName: 'firefox' , platform: 'Windows 8' , version: '' } - , { browserName: 'firefox' , platform: 'Windows XP', version: '4' } - , { browserName: 'internet explorer' , platform: 'Windows 8' , version: '10' } - , { browserName: 'internet explorer' , platform: 'Windows 7' , version: '9' } - , { browserName: 'internet explorer' , platform: 'Windows 7' , version: '8' } - , { browserName: 'internet explorer' , platform: 'Windows XP', version: '7' } - , { browserName: 'internet explorer' , platform: 'Windows XP', version: '6' } - , { browserName: 'safari' , platform: 'Windows 7' , version: '5' } - , { browserName: 'safari' , platform: 'OS X 10.8' , version: '6' } - , { browserName: 'opera' , platform: 'Windows 7' , version: '' } - , { browserName: 'opera' , platform: 'Windows 7' , version: '11' } - , { browserName: 'ipad' , platform: 'OS X 10.8' , version: '6' } - , { browserName: 'android' , platform: 'Linux' , version: '4.0', 'device-type': 'tablet' } - ] - -if (!user) - throw new Error('Must set a SAUCE_USER env var') -if (!key) - throw new Error('Must set a SAUCE_KEY env var') - -brtapsauce({ - name : 'Traversty' - , user : user - , key : key - , brsrc : testFile - , capabilities : capabilities - , options : { timeout: 60 * 6 } -}) \ No newline at end of file diff --git a/test/test.js b/test/test.js index aa9b487..2418be8 100644 --- a/test/test.js +++ b/test/test.js @@ -1,9 +1,361 @@ -require('./basic-test') +const test = require('tape') + , through2 = require('../') + , crypto = require('crypto') + , bl = require('bl') + , spigot = require('stream-spigot') -if (!process.env.SAUCE_KEY || !process.env.SAUCE_USER) - return console.log('SAUCE_KEY and/or SAUCE_USER not set, not running sauce tests') +test('plain through', function (t) { + var th2 = through2(function (chunk, enc, callback) { + if (!this._i) + this._i = 97 // 'a' + else + this._i++ + var b = Buffer.alloc(chunk.length) + for (var i = 0; i < chunk.length; i++) + b[i] = this._i + this.push(b) + callback() + }) -if (!/v0\.10/.test(process.version)) - return console.log('Not Node v0.10.x, not running sauce tests') + th2.pipe(bl(function (err, b) { + var s = b.toString('ascii') + t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string') + t.end() + })) -require('./sauce.js') \ No newline at end of file + th2.write(crypto.randomBytes(10)) + th2.write(crypto.randomBytes(5)) + th2.write(crypto.randomBytes(10)) + th2.end() +}) + +test('pipeable through', function (t) { + var th2 = through2(function (chunk, enc, callback) { + if (!this._i) + this._i = 97 // 'a' + else + this._i++ + var b = Buffer.alloc(chunk.length) + for (var i = 0; i < chunk.length; i++) + b[i] = this._i + this.push(b) + callback() + }) + + th2.pipe(bl(function (err, b) { + var s = b.toString('ascii') + // bl() acts like a proper streams2 stream and passes as much as it's + // asked for, so we really only get one write with such a small amount + // of data + t.equal(s, 'aaaaaaaaaaaaaaaaaaaaaaaaa', 'got transformed string') + t.end() + })) + + var bufs = bl() + bufs.append(crypto.randomBytes(10)) + bufs.append(crypto.randomBytes(5)) + bufs.append(crypto.randomBytes(10)) + bufs.pipe(th2) +}) + +test('object through', function (t) { + t.plan(3) + + var th2 = through2({ objectMode: true}, function (chunk, enc, callback) { + this.push({ out: chunk.in + 1 }) + callback() + }) + + var e = 0 + th2.on('data', function (o) { + t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object') + e++ + }) + + th2.write({ in: 101 }) + th2.write({ in: 202 }) + th2.write({ in: -100 }) + th2.end() +}) + +test('object through with through2.obj', function (t) { + t.plan(3) + + var th2 = through2.obj(function (chunk, enc, callback) { + this.push({ out: chunk.in + 1 }) + callback() + }) + + var e = 0 + th2.on('data', function (o) { + t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object') + e++ + }) + + th2.write({ in: 101 }) + th2.write({ in: 202 }) + th2.write({ in: -100 }) + th2.end() +}) + +test('flushing through', function (t) { + var th2 = through2(function (chunk, enc, callback) { + if (!this._i) + this._i = 97 // 'a' + else + this._i++ + var b = Buffer.alloc(chunk.length) + for (var i = 0; i < chunk.length; i++) + b[i] = this._i + this.push(b) + callback() + }, function (callback) { + this.push(Buffer.from([ 101, 110, 100 ])) + callback() + }) + + th2.pipe(bl(function (err, b) { + var s = b.toString('ascii') + t.equal(s, 'aaaaaaaaaabbbbbccccccccccend', 'got transformed string') + t.end() + })) + + th2.write(crypto.randomBytes(10)) + th2.write(crypto.randomBytes(5)) + th2.write(crypto.randomBytes(10)) + th2.end() +}) + +test('plain through ctor', function (t) { + var Th2 = through2.ctor(function (chunk, enc, callback) { + if (!this._i) + this._i = 97 // 'a' + else + this._i++ + var b = Buffer.alloc(chunk.length) + for (var i = 0; i < chunk.length; i++) + b[i] = this._i + this.push(b) + callback() + }) + + var th2 = new Th2() + + th2.pipe(bl(function (err, b) { + var s = b.toString('ascii') + t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string') + t.end() + })) + + th2.write(crypto.randomBytes(10)) + th2.write(crypto.randomBytes(5)) + th2.write(crypto.randomBytes(10)) + th2.end() +}) + +test('reuse through ctor', function (t) { + t.plan(4) + + var Th2 = through2.ctor(function (chunk, enc, callback) { + if (!this._i) { + t.ok(1, 'did not contain previous instance data (this._i)') + this._i = 97 // 'a' + } else + this._i++ + var b = Buffer.alloc(chunk.length) + for (var i = 0; i < chunk.length; i++) + b[i] = this._i + this.push(b) + callback() + }) + + var th2 = Th2() + + th2.pipe(bl(function (err, b) { + var s = b.toString('ascii') + t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string') + + var newInstance = Th2() + newInstance.pipe(bl(function (err, b) { + var s = b.toString('ascii') + t.equal('aaaaaaabbbbccccccc', s, 'got transformed string') + })) + + newInstance.write(crypto.randomBytes(7)) + newInstance.write(crypto.randomBytes(4)) + newInstance.write(crypto.randomBytes(7)) + newInstance.end() + })) + + th2.write(crypto.randomBytes(10)) + th2.write(crypto.randomBytes(5)) + th2.write(crypto.randomBytes(10)) + th2.end() +}) + +test('object through ctor', function (t) { + t.plan(3) + + var Th2 = through2.ctor({ objectMode: true}, function (chunk, enc, callback) { + this.push({ out: chunk.in + 1 }) + callback() + }) + + var th2 = new Th2() + + var e = 0 + th2.on('data', function (o) { + t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object') + e++ + }) + + th2.write({ in: 101 }) + th2.write({ in: 202 }) + th2.write({ in: -100 }) + th2.end() +}) + +test('pipeable object through ctor', function (t) { + t.plan(4) + + var Th2 = through2.ctor({ objectMode: true}, function (record, enc, callback) { + if (record.temp != null && record.unit == 'F') { + record.temp = ( ( record.temp - 32 ) * 5 ) / 9 + record.unit = 'C' + } + this.push(record) + callback() + }) + + var th2 = Th2() + + var expect = [-19, -40, 100, 22] + th2.on('data', function (o) { + t.deepEqual(o, { temp: expect.shift(), unit: 'C' }, 'got transformed object') + }) + + spigot({objectMode: true}, [ + {temp: -2.2, unit: 'F'}, + {temp: -40, unit: 'F'}, + {temp: 212, unit: 'F'}, + {temp: 22, unit: 'C'} + ]).pipe(th2) +}) + +test('object through ctor override', function (t) { + t.plan(3) + + var Th2 = through2.ctor(function (chunk, enc, callback) { + this.push({ out: chunk.in + 1 }) + callback() + }) + + var th2 = Th2({objectMode: true}) + + var e = 0 + th2.on('data', function (o) { + t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object') + e++ + }) + + th2.write({ in: 101 }) + th2.write({ in: 202 }) + th2.write({ in: -100 }) + th2.end() +}) + +test('object settings available in transform', function (t) { + t.plan(6) + + var Th2 = through2.ctor({objectMode: true, peek: true}, function (chunk, enc, callback) { + t.ok(this.options.peek, "reading options from inside _transform") + this.push({ out: chunk.in + 1 }) + callback() + }) + + var th2 = Th2() + + var e = 0 + th2.on('data', function (o) { + t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object') + e++ + }) + + th2.write({ in: 101 }) + th2.write({ in: 202 }) + th2.write({ in: -100 }) + th2.end() +}) + +test('object settings available in transform override', function (t) { + t.plan(6) + + var Th2 = through2.ctor(function (chunk, enc, callback) { + t.ok(this.options.peek, "reading options from inside _transform") + this.push({ out: chunk.in + 1 }) + callback() + }) + + var th2 = Th2({objectMode: true, peek: true}) + + var e = 0 + th2.on('data', function (o) { + t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object') + e++ + }) + + th2.write({ in: 101 }) + th2.write({ in: 202 }) + th2.write({ in: -100 }) + th2.end() +}) + +test('object override extends options', function (t) { + t.plan(6) + + var Th2 = through2.ctor({objectMode: true}, function (chunk, enc, callback) { + t.ok(this.options.peek, "reading options from inside _transform") + this.push({ out: chunk.in + 1 }) + callback() + }) + + var th2 = Th2({peek: true}) + + var e = 0 + th2.on('data', function (o) { + t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object') + e++ + }) + + th2.write({ in: 101 }) + th2.write({ in: 202 }) + th2.write({ in: -100 }) + th2.end() +}) + +test('can be destroyed', function(t) { + t.plan(1) + + var th = through2() + + th.on('close', function() { + t.ok(true, 'shoud emit close') + t.end() + }) + + th.destroy() +}) + +test('can be destroyed twice', function(t) { + t.plan(1) + + var th = through2() + + th.on('close', function() { + t.ok(true, 'shoud emit close') + t.end() + }) + + th.destroy() + th.destroy() +}) \ No newline at end of file diff --git a/through2.js b/through2.js index 5b7a880..6baa6a1 100644 --- a/through2.js +++ b/through2.js @@ -1,4 +1,4 @@ -var Transform = require('readable-stream/transform') +var Transform = require('readable-stream').Transform , inherits = require('util').inherits , xtend = require('xtend')