var Transform = require('pipestream').Transform;
var util = require('util');
var iconv = require('iconv-lite');

function WhistleTransform(options) {
	if (!(this instanceof WhistleTransform)) {
		  return new WhistleTransform(options);
	  }
	
	Transform.call(this);
	options = options || {};
	var value = parseInt(options.speed * 1000 / 8);
	if (value > 0) {
		this._speed = value;
	}
	if ((value = parseInt(options.delay)) > 0) {
		this._delay = value;
	}
	
	var charset = options.charset && String(options.charset);
	if (!iconv.encodingExists(charset)) {
		charset = 'utf8';
	}
	
	this._body = getBuffer(options, 'body', charset);
	this._top = getBuffer(options, 'top', charset);
	this._bottom = getBuffer(options, 'bottom', charset);
}

function getBuffer(options, name, charset) {
	var buf = options[name];
	if (buf instanceof Buffer) {
		return buf;
	}
	buf = buf && String(buf);
	return buf ? iconv.encode(buf, charset) : null;
}

util.inherits(WhistleTransform, Transform);

WhistleTransform.prototype._transform = function(chunk, encoding, callback) {
	var self = this;
	var cb = function() {
		if (self._ended && self._bottom) {
			chunk = chunk ? Buffer.concat([chunk, self._bottom]) : self._bottom;
			self._bottom = null;
		}
		if (chunk && self._speed) {
			setTimeout(function() {
				callback(null, chunk);
			}, Math.round(chunk.length * 1000 / self._speed));
		} else {
			callback(null, chunk);
		}
	};
	
	if (!self._ended) {
		self._ended = !chunk;
	}
	
	if (!self._inited) {
		self._inited = true;
		if (self._body) {
			self._ended = true;
			chunk = self._body;
		}
		if (self._top) {
			chunk = chunk ? Buffer.concat([self._top, chunk]) : self._top;
			self._top = null;
		}
		return self._delay ? setTimeout(cb, self._delay) :  cb();
	}
	
	if (self._ended) {
		chunk = null;
	}
	
	cb();
};

module.exports = WhistleTransform;
@@ -783,120 +783,63 @@
  %7B%0A%09
-var buf = options%5Bname%5D;%0A%09if (buf instanceof Buffer) %7B%0A%09%09return buf;%0A%09%7D%0A%09buf = buf && String(buf);%0A%09return buf ?
+%0A%09return (buf == null %7C%7C buf instanceof Buffer) ? buf :
  ico
@@ -851,16 +851,21 @@
 code(buf
+ + ''
 , charse
@@ -870,15 +870,8 @@
 set)
- : null
 ;%0A%7D%0A

refine code