-
Notifications
You must be signed in to change notification settings - Fork 0
/
tapit.js
41 lines (34 loc) · 1.19 KB
/
tapit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Borrowed.
// https://gist.github.com/benbuckman/2758563
// Intercept writeStream to pass output thru callback.
//
//
// returns an unhook() function, call when done intercepting
var errors = {inputTypeError: new TypeError('tapit: input must be stream.Writable')};
module.exports = function(writeStream, tap) {
if (!writeStream.write || !writeStream.writable)
throw errors.inputTypeError;
var old_writeStream_write = writeStream.write;
writeStream.write = (function(write) {
return function(string, encoding, fd) {
var args = [].slice.call(arguments);
args[0] = interceptor(string, tap);
write.apply(writeStream, args);
};
}(writeStream.write));
function interceptor(string, callback) {
// only intercept the string
var result = callback(string);
if(typeof result == 'string') {
string = result.replace(/\n$/, '') + (result && (/\n$/).test(string) ? '\n' : '');
}
return string;
}
// puts back to original
return function unhook() {
writeStream.write = old_writeStream_write;
};
};
if(process.env.NODE_ENV === 'test') {
module.exports.errors = errors;
}