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.0.3 to 0.0.4

LICENSE

13

lib/rw/read-sync.js

@@ -5,4 +5,7 @@ var fs = require("fs"),

module.exports = function(filename, options) {
if (filename === "/dev/stdin") {
var decoder = decode(options);
if (fs.statSync(filename).isFile()) {
return fs.readFileSync(filename, options);
} else {
var fd = fs.openSync(filename, options && options.flag || "r"),
decoder = decode(options);

@@ -12,5 +15,6 @@ while (true) {

var buffer = new Buffer(bufferSize),
bytesRead = fs.readSync(process.stdin.fd, buffer, 0, bufferSize);
bytesRead = fs.readSync(fd, buffer, 0, bufferSize);
} catch (e) {
if (e.code === "EOF") break;
fs.closeSync(fd);
throw e;

@@ -22,5 +26,4 @@ }

fs.closeSync(fd);
return decoder.value();
} else {
return fs.readFileSync(filename, options);
}

@@ -27,0 +30,0 @@ };

@@ -6,11 +6,14 @@ var fs = require("fs"),

if (arguments.length < 2) callback = options, options = null;
if (filename === "/dev/stdin") {
var decoder = decode(options);
process.stdin
.on("error", callback)
.on("data", function(d) { decoder.push(d); })
.on("end", function() { callback(null, decoder.value()); });
} else {
fs.readFile(filename, options, callback);
}
fs.stat(filename, function(error, stat) {
if (error) return callback(error);
if (stat.isFile()) {
fs.readFile(filename, options, callback);
} else {
var decoder = decode(options);
fs.createReadStream(filename, options && {flags: options.flag || "r"}) // N.B. flag / flags
.on("error", callback)
.on("data", function(d) { decoder.push(d); })
.on("end", function() { callback(null, decoder.value()); });
}
});
};

@@ -5,18 +5,29 @@ var fs = require("fs"),

module.exports = function(filename, data, options) {
if (filename === "/dev/stdout") {
data = encode(data, options);
var bytesWritten = 0,
bytesTotal = data.length;
var stat;
try {
stat = fs.statSync(filename);
} catch (error) {
if (error.code !== "ENOENT") throw error;
}
if (!stat || stat.isFile()) {
fs.writeFileSync(filename, data, options);
} else {
var fd = fs.openSync(filename, options && options.flag || "w"),
bytesWritten = 0,
bytesTotal = (data = encode(data, options)).length;
while (bytesWritten < bytesTotal) {
try {
bytesWritten += fs.writeSync(process.stdout.fd, data, bytesWritten, bytesTotal - bytesWritten, null);
bytesWritten += fs.writeSync(fd, data, bytesWritten, bytesTotal - bytesWritten, null);
} catch (error) {
if (error.code === "EPIPE") break; // ignore broken pipe, e.g., | head -n 1000
if (error.code === "EPIPE") break; // ignore broken pipe, e.g., | head
fs.closeSync(fd);
throw error;
}
}
} else {
fs.writeFileSync(filename, data, options);
fs.closeSync(fd);
}
};

@@ -6,18 +6,12 @@ var fs = require("fs"),

if (arguments.length < 3) callback = options, options = null;
if (filename === "/dev/stdout") {
data = encode(data, options);
try {
process.stdout.write(data, function(error) {
if (error && error.code === "EPIPE") { // ignore broken pipe, e.g., | head -n 1000
error = null;
process.stdout.once("error", function() {});
}
callback(error);
});
} catch (error) {
if (error.code !== "EPIPE") throw error;
fs.stat(filename, function(error, stat) {
if (error && error.code !== "ENOENT") return callback(error);
if (!stat || stat.isFile()) {
fs.writeFile(filename, data, options, callback);
} else {
fs.createWriteStream(filename, options && {flags: options.flag || "w"}) // N.B. flag / flags
.on("error", function(error) { callback(error.code === "EPIPE" ? null : error); }) // ignore broken pipe, e.g., | head
.end(encode(data, options), callback);
}
} else {
fs.writeFile(filename, data, options, callback);
}
});
};
{
"name": "rw",
"version": "0.0.3",
"version": "0.0.4",
"description": "Wrappers of fs.{read,write}File that work for /dev/std{in,out}.",

@@ -5,0 +5,0 @@ "keywords": [

@@ -9,5 +9,5 @@ # stdin & stdout, the right way

you’d be wrong, because this only reads up to what Node thinks is the size of the file. So, if you redirect a file to your program (`cat file | program`), you’ll only read the first 65,536 bytes of your file.
you’d be wrong, because Node only reads up to the size of the file reported by fs.stat rather than reading until it receives an EOF. So, if you redirect a file to your program (`cat file | program`), you’ll only read the first 65,536 bytes of your file. Oops.
OK, so how do you write a file to stdout? If you thought,
What about writing a file to stdout? If you thought,

@@ -18,3 +18,3 @@ ```js

you’d also be wrong, because this tries to close stdout, and you get this error:
you’d also be wrong, because this tries to close stdout, so you get this error:

@@ -29,3 +29,3 @@ ```

Well, you could use a different pattern for reading from stdin:
You could use a different pattern for reading from stdin:

@@ -41,3 +41,3 @@ ```js

But that’s a pain, since now your code has two different code paths for reading inputs, depending on whether you’re reading a real file or stdin. And the code gets even more complex if you want to read that file synchronously.
But that’s a pain, since now your code has two different code paths for reading inputs depending on whether you’re reading a real file or stdin. And the code gets even more complex if you want to [read that file synchronously](https://github.com/mbostock/rw/blob/master/lib/rw/read-sync.js).

@@ -68,3 +68,3 @@ You could also try a different pattern for writing to stdout:

The **rw** module fixes these problems. It provides an interface just like readFile, readFileSync, writeFile and writeFileSync, but these methods work the way you expect on stdin and stdout. If you use these methods on files other than /dev/stdin or /dev/stdout, they simply delegate to the fs methods, so you can trust that they behave identically to the methods you’re used to.
The **rw** module fixes these problems. It provides an interface just like readFile, readFileSync, writeFile and writeFileSync, but with implementations that work the way you expect on stdin and stdout. If you use these methods on files other than /dev/stdin or /dev/stdout, they simply delegate to the fs methods, so you can trust that they behave identically to the methods you’re used to.

@@ -83,2 +83,4 @@ For example, now you can read stdin synchronously like so:

And **rw** automatically squashes EPIPE errors, so you can pipe the output of your program to `head` and you won’t get a spurious stack trace.
And rw automatically squashes EPIPE errors, so you can pipe the output of your program to `head` and you won’t get a spurious stack trace.
To install, `npm install rw`.
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