/*jslint node: true*/

var exports;
(function () {
  'use strict';
  var delayMs,
    modDelayResponse,
    lengthPerWrite = 2000;

  modDelayResponse = {
    send : function (content, writer, onEnd) {
      var writePos = 0,
        bufferLength = content.length,
        writeParts = function() {
          var endPos = bufferLength;
          if (writePos < endPos) {
            endPos = Math.min(endPos, writePos + lengthPerWrite);
            writer.write(content.substr(writePos, lengthPerWrite), 'binary');
            writePos = endPos;
            setTimeout(writeParts, delayMs);
          } else {
            writer.end();
            if (onEnd) {
              onEnd();
            }
          }
        };

      writeParts();
    },
    pipe : function (reader, writer, onEnd) {
      var bufferQueue = [],
        bufferLength = 0;

      reader.on('data', function (ck) {
        bufferQueue.push(ck);
        bufferLength += ck.length;
      });

      reader.on('end', function () {
        var bufferAll,
          pos = 0,
          writePos = 0,
          writeParts = function() {
            var endPos = bufferLength;
            if (writePos < endPos) {
              endPos = Math.min(endPos, writePos + lengthPerWrite);
              writer.write(bufferAll.slice(writePos, endPos));
              writePos = endPos;
              setTimeout(writeParts, delayMs);
            } else {
              writer.end();
            }
          };

        switch (bufferQueue.length) {
        case 1:
          bufferAll = bufferQueue[0];
          break;
        default:
          bufferAll = new Buffer(bufferLength);
          bufferQueue.forEach(function (buf) {
            buf.copy(bufferAll, pos);
            pos += buf.length;
          });
        }

        writeParts();

        if (onEnd) {
          onEnd();
        }
      });

    },

    defaultConf : {
      filter : null,
    },

    init : function () {
      var conf = this.conf,
        delay;

      delay = parseInt(conf.delay, 10);

      if (isNaN(delay)) {
        delay = 0;
      }

      delayMs = delay / 10;
      this.enabled = delay > 0;
      this.filter = this.enabled ? conf.filter || function () { return true; } :
          function () { return false; };
    }

  };

  if (exports === undefined) {
    exports = {};
  }
  exports.delayResponse = modDelayResponse;
}());


@@ -105,28 +105,25 @@
     
-lengthPerWrite = 200
+writeInterval = 5
 0;%0A%0A
@@ -255,16 +255,180 @@
 length,%0A
+        writeCounts = Math.ceil(delayMs / writeInterval),%0A        currentCount = 0,%0A        //lengthPerWrite = Math.max(Math.round(bufferLength / writeCounts), 1),%0A
         
@@ -482,32 +482,58 @@
 = bufferLength;%0A
+          currentCount++;%0A
           if (wr
@@ -555,32 +555,34 @@
 ) %7B%0A            
+//
 endPos = Math.mi
@@ -623,32 +623,228 @@
 e);%0A            
+endPos = Math.min(endPos, Math.floor(bufferLength / writeCounts * currentCount));%0A            if (endPos %3E writePos) %7B%0A              writer.write(content.slice(writePos, endPos));%0A              //
 writer.write(con
@@ -897,32 +897,34 @@
 ');%0A            
+  
 writePos = endPo
@@ -918,32 +918,46 @@
 tePos = endPos;%0A
+            %7D%0A
             setT
@@ -967,39 +967,45 @@
 out(writeParts, 
-delayMs
+writeInterval
 );%0A          %7D e
@@ -1461,32 +1461,120 @@
   writePos = 0,%0A
+          writeCounts = Math.ceil(delayMs / writeInterval),%0A          currentCount = 0,%0A
           writeP
@@ -1624,32 +1624,60 @@
 = bufferLength;%0A
+            currentCount++;%0A
             if (
@@ -1741,36 +1741,105 @@
 os, 
-writePos + lengthPerWrite);%0A
+Math.floor(bufferLength / writeCounts * currentCount));%0A              if (endPos %3E writePos) %7B%0A  
     
@@ -1903,32 +1903,34 @@
 ;%0A              
+  
 writePos = endPo
@@ -1924,32 +1924,48 @@
 tePos = endPos;%0A
+              %7D%0A
               se
@@ -1985,23 +1985,29 @@
 eParts, 
-delayMs
+writeInterval
 );%0A     
@@ -2694,24 +2694,24 @@
 0;%0A      %7D%0A%0A
+
       delayM
@@ -2723,13 +2723,8 @@
 elay
- / 10
 ;%0A  

fix bug in module delay response.