Skip to content

Commit c34d321

Browse files
committed
Fix writable stream implementation
1 parent 1fbc17f commit c34d321

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

lib/streams.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,30 +92,23 @@ OutputStream.prototype._write = function (chunk, encoding, callback) {
9292
chunk = new Buffer(chunk, encoding);
9393
}
9494

95-
if (chunk.length === 0) {
95+
if (chunk.length <= 0) {
9696
callback.call(this);
9797
return;
9898
}
9999

100-
if (chunk.length <= 65535) {
101-
chunks.push(chunk);
102-
} else {
103-
var splits = Math.floor(chunk.length / 65535);
104-
var start = 0;
105-
for (var i = 0; i < splits; ++i) {
106-
chunks.push(chunk.slice(start, start += 65535));
107-
}
108-
chunks.push(chunk.slice(start, chunk.length));
109-
}
110-
111-
while (chunks.length > 1) {
100+
var start = 0, end = 65535;
101+
while (end < chunk.length) {
112102
this._conn.stream.writeRecord(
113-
this._id, new this.recordType(chunks.shift()));
103+
this._id, new this.recordType(chunk.slice(start, end)));
104+
105+
start = end;
106+
end += 65535;
114107
}
115108

116109
this._conn.stream.writeRecord(
117110
this._id,
118-
new this.recordType(chunks.shift()),
111+
new this.recordType(chunk.slice(start)),
119112
callback.bind(this));
120113
};
121114

0 commit comments

Comments
 (0)