var util = require('util');
var Stream = require('stream');
-var Duplex = Stream.Duplex;
+var Duplex = require('_stream_duplex');
util.inherits(Writable, Stream);
this.ended = false;
this.ending = false;
+ // should we decode strings into buffers before passing to _write?
+ // this is here so that some node-core streams can optimize string
+ // handling at a lower level.
+ this.decodeStrings = options.hasOwnProperty('decodeStrings') ?
+ options.decodeStrings : true;
+
// not an actual buffer we keep track of, but a measurement
// of how much we're waiting to get pushed to some underlying
// socket or file.
return;
}
- if (typeof chunk === 'string')
- chunk = new Buffer(chunk, encoding);
-
var l = chunk.length;
+ if (false === state.decodeStrings)
+ chunk = [chunk, encoding];
+ else if (typeof chunk === 'string' || encoding) {
+ chunk = new Buffer(chunk + '', encoding);
+ l = chunk.length;
+ }
+
state.length += l;
var ret = state.length < state.highWaterMark;
}
state.writing = true;
- this._write(chunk, function writecb(er) {
+ this._write(chunk, writecb.bind(this));
+
+ return ret;
+
+ function writecb(er) {
state.writing = false;
if (er) {
this.emit('error', er);
this.emit('drain');
}.bind(this));
}
+ }
- }.bind(this));
-
- return ret;
};
Writable.prototype._write = function(chunk, cb) {