Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rw

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rw - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

38

lib/rw/writer.js

@@ -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": [

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc