stream-replace.js
function replaceStream(needle, replacer) {
  var ts = new Transform();
  var chunks = [], len = 0, pos = 0;

  ts._transform = function _transform(chunk, enc, cb) {

    chunks.push(chunk);
    len += chunk.length;

    if (pos === 1) {
      var data = Buffer.concat(chunks, len)
        .toString()
        .replace(needle, replacer);

      // TODO: examine and profile garbage
      chunks = [];
      len = 0;

      this.push(data);
    }

    pos = 1 ^ pos;
    cb(null);
  };

  ts._flush = function _flush(cb) {
    if (chunks.length) {
      this.push(Buffer.concat(chunks, len)
        .toString()
        .replace(needle, replacer))
    }

    cb(null);
  }

  return ts;
}
function replaceStream(needle, replacer) {
  var ts = new Transform();
  var chunks = [], len = 0, pos = 0;

  ts._transform = function _transform(chunk, enc, cb) {

    chunks.push(chunk);
    len += chunk.length;

    if (pos === 1) {
      var data = Buffer.concat(chunks, len)
        .toString()
        .replace(needle, replacer);

      // TODO: examine and profile garbage
      chunks = [];
      len = 0;

      this.push(data);
    }

    pos = 1 ^ pos;
    cb(null);
  };

  ts._flush = function _flush(cb) {
    if (chunks.length) {
      this.push(Buffer.concat(chunks, len)
        .toString()
        .replace(needle, replacer))
    }

    cb(null);
  }

  return ts;
}
function replaceStream ( needle , replacer ) { var ts = new Transform ( ) ; var chunks = [ ] , len = 0 , pos = 0 ; ts . _transform = function _transform ( chunk , enc , cb ) { chunks . push ( chunk ) ; len += chunk . length ; if ( pos === 1 ) { var data = Buffer . concat ( chunks , len ) . toString ( ) . replace ( needle , replacer ) ; // TODO: examine and profile garbage chunks = [ ] ; len = 0 ; this . push ( data ) ; } pos = 1 ^ pos ; cb ( null ) ; } ; ts . _flush = function _flush ( cb ) { if ( chunks . length ) { this . push ( Buffer . concat ( chunks , len ) . toString ( ) . replace ( needle , replacer ) ) } cb ( null ) ; } return ts ; }
Returns a transform stream that replaces
'needle' with 'replacer' in the data piped into it.

All read data is converted ot utf-8 strings before
the replacement isperformed.

Honors needles appearing on chunk boundaries.

Abides by the same rules as String.replace();

@param  {String|RegExp}   needle   needle
@param  {String|Function} replacer replacer
@return {TransformStream}
Returns a transform stream that replaces needle with replacer in the data piped into it .