Comparing version 0.1.0 to 0.1.1
@@ -16,3 +16,4 @@ module.exports = function(open, write, close) { | ||
bufferLength = 1 << 16, | ||
buffers = []; | ||
bufferOverflowLength = bufferLength << 1, | ||
buffer = new Buffer(bufferOverflowLength); | ||
@@ -43,2 +44,4 @@ process.nextTick(open.bind(null, function(newError, newChannel) { | ||
bufferLength = newBufferLength; | ||
bufferOverflowLength = bufferLength << 1; | ||
buffer = new Buffer(bufferOverflowLength); | ||
return writer; | ||
@@ -53,3 +56,3 @@ } | ||
if (writer.ended) throw new Error("cannot drain after ended"); | ||
if (!buffers.length) return void process.nextTick(callback.bind(writer, null)); | ||
if (!bufferUsed) return void process.nextTick(callback.bind(writer, null)); | ||
@@ -62,9 +65,8 @@ // A drain is now in-progress. | ||
// Write each buffer, in their original order. | ||
buffers.reverse(); | ||
(function writeNext(newError) { | ||
// Write out the buffer. | ||
write(channel, buffer, 0, bufferUsed, function(newError) { | ||
drainCallback = null; | ||
// If an error occurs, stop writing, and shut it all down. | ||
if (error = newError) { | ||
drainCallback = null; | ||
@@ -79,10 +81,7 @@ // Close the channel, ignoring any secondary errors. | ||
// If there’s still data to write, write it. Otherwise callback. | ||
var data = buffers.pop(); | ||
if (!data) return drainCallback = null, bufferUsed = 0, void callback(null); | ||
write(channel, data, 0, data.length, writeNext); | ||
})(null); | ||
bufferUsed = 0; | ||
callback(null); | ||
}); | ||
} | ||
// Caution: the specified data must NOT be modified after draining starts. | ||
function writer_write(data) { | ||
@@ -92,4 +91,17 @@ if (error) throw error; | ||
if (!(data instanceof Buffer)) data = new Buffer(data + "", encoding); | ||
buffers.push(data); | ||
var oldBufferUsed = bufferUsed; | ||
bufferUsed += data.length; | ||
// If the buffer would overflow with this new data, double its size. | ||
if (bufferUsed > bufferOverflowLength) { | ||
var oldBuffer = buffer; | ||
buffer = new Buffer(bufferOverflowLength <<= 1); | ||
oldBuffer.copy(buffer, 0, 0, oldBufferUsed); | ||
} | ||
// Combining multiple buffers into a single buffer is much faster than | ||
// myriad tiny writes. In addition, this copies the data to be written, | ||
// isolating it from any external changes (such as the reader refilling). | ||
data.copy(buffer, oldBufferUsed); | ||
return bufferUsed < bufferLength; | ||
@@ -96,0 +108,0 @@ } |
{ | ||
"name": "rw", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Wrappers of fs.{read,write}File that work for /dev/std{in,out}.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
31103
568
0