mute-stream
Advanced tools
Comparing version 0.0.1 to 0.0.2
18
mute.js
@@ -7,7 +7,9 @@ var Stream = require('stream') | ||
// argument auto-pipes | ||
function MuteStream (dest) { | ||
function MuteStream (opts) { | ||
Stream.apply(this) | ||
opts = opts || {} | ||
this.writable = this.readable = true | ||
this.muted = false | ||
this.on('pipe', this._onpipe) | ||
this.replace = opts.replace | ||
} | ||
@@ -79,3 +81,6 @@ | ||
MuteStream.prototype.write = function (c) { | ||
if (this.muted) return true | ||
if (this.muted) { | ||
if (!this.replace) return true | ||
c = c.toString().replace(/./g, this.replace) | ||
} | ||
this.emit('data', c) | ||
@@ -85,3 +90,10 @@ } | ||
MuteStream.prototype.end = function (c) { | ||
if (!this.muted) this.emit('data', c) | ||
if (this.muted) { | ||
if (c && this.replace) { | ||
c = c.toString().replace(/./g, this.replace) | ||
} else { | ||
c = null | ||
} | ||
} | ||
if (c) this.emit('data', c) | ||
this.emit('end') | ||
@@ -88,0 +100,0 @@ } |
{ | ||
"name": "mute-stream", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"main": "mute.js", | ||
@@ -5,0 +5,0 @@ "directories": { |
@@ -13,3 +13,3 @@ # mute-stream | ||
var ms = new MuteStream | ||
var ms = new MuteStream(options) | ||
@@ -38,2 +38,10 @@ ms.pipe(process.stdout) | ||
## Options | ||
All options are optional. | ||
* `replace` Set to a string to replace each character with the | ||
specified string when muted. (So you can show `****` instead of the | ||
password, for example.) | ||
## ms.mute() | ||
@@ -40,0 +48,0 @@ |
@@ -136,1 +136,60 @@ var Stream = require('stream') | ||
}) | ||
tap.test('replace with *', function (t) { | ||
var str = new PassThrough | ||
var ms = new MS({replace: '*'}) | ||
str.pipe(ms) | ||
var expect = ['foo', '*****', 'bar', '***', 'baz', 'boo', '**', '****'] | ||
ms.on('data', function (c) { | ||
t.equal(c, expect.shift()) | ||
}) | ||
str.write('foo') | ||
ms.mute() | ||
str.write('12345') | ||
ms.unmute() | ||
str.write('bar') | ||
ms.mute() | ||
str.write('baz') | ||
ms.unmute() | ||
str.write('baz') | ||
str.write('boo') | ||
ms.mute() | ||
str.write('xy') | ||
str.write('xyzΩ') | ||
t.equal(expect.length, 0) | ||
t.end() | ||
}) | ||
tap.test('replace with ~YARG~', function (t) { | ||
var str = new PassThrough | ||
var ms = new MS({replace: '~YARG~'}) | ||
str.pipe(ms) | ||
var expect = ['foo', '~YARG~~YARG~~YARG~~YARG~~YARG~', 'bar', | ||
'~YARG~~YARG~~YARG~', 'baz', 'boo', '~YARG~~YARG~', | ||
'~YARG~~YARG~~YARG~~YARG~'] | ||
ms.on('data', function (c) { | ||
t.equal(c, expect.shift()) | ||
}) | ||
// also throw some unicode in there, just for good measure. | ||
str.write('foo') | ||
ms.mute() | ||
str.write('ΩΩ') | ||
ms.unmute() | ||
str.write('bar') | ||
ms.mute() | ||
str.write('Ω') | ||
ms.unmute() | ||
str.write('baz') | ||
str.write('boo') | ||
ms.mute() | ||
str.write('Ω') | ||
str.write('ΩΩ') | ||
t.equal(expect.length, 0) | ||
t.end() | ||
}) |
9190
263
63