Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Now stdin and stdout are files.

License

NotificationsYou must be signed in to change notification settings

mbostock/rw

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How do you read a file from stdin? If you thought,

varcontents=fs.readFileSync("/dev/stdin","utf8");

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.

What about writing a file to stdout? If you thought,

fs.writeFileSync("/dev/stdout",contents,"utf8");

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

Error: UNKNOWN, unknown error    at Object.fs.writeSync (fs.js:528:18)    at Object.fs.writeFileSync (fs.js:975:21)

(Also, this doesn’t work on Windows, because Windows doesn’t support /dev/stdout, /dev/stdin and /dev/stderr!)

Shucks. So what should you do?

You could use a different pattern for reading from stdin:

varchunks=[];process.stdin.on("data",function(chunk){chunks.push(chunk);}).on("end",function(){console.log(chunks.join("").length);}).setEncoding("utf8");

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 toread that file synchronously.

You could also try a different pattern for writing to stdout:

process.stdout.write(contents);

Or even:

console.log(contents);

But if you try to pipe your output tohead, you’ll get this error:

Error: write EPIPE    at errnoException (net.js:904:11)    at Object.afterWrite (net.js:720:19)

Huh.

Therw 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.

For example, now you can read stdin synchronously like so:

varcontents=rw.readFileSync("/dev/stdin","utf8");

Or to write to stdout:

rw.writeFileSync("/dev/stdout",contents,"utf8");

And rw automatically squashes EPIPE errors, so you can pipe the output of your program tohead and you won’t get a spurious stack trace.

To install,npm install rw.

Note

If you want to read synchronously from stdin usingreadFileSync, you cannot also use process.stdin in the same program. Likewise, if you want to write synchronously to stdout or stderr usingwriteFileSync, you cannot use process.stdout or process.stderr, respectively. (This includes using console.log and the like!) Failure to heed this warning may result in error: EAGAIN, resource temporarily unavailable. Unfortunately, it does not appear possible for this library to fix this issue automatically, so please use caution.

Only the asynchronous methodsreadFile andwriteFile are supported on Windows. Node has no synchronous API for reading from process.stdin or writing to process.stdout or process.stderr, so you’re out of luck!

API Reference

# rw.readFile(path[,options],callback)

Reads the file at the specifiedpath completely into memory, invoking the specifiedcallback once the data is available and the file is closed. Thecallback is invoked with two arguments: theerror that occurred during read (hopefully null), and the read data. Ifoptions is a string, it specifies the encoding to use, in which case the read data will be a string; otherwiseoptions is an object, and may specify encoding and flag properties. This method is a drop-in replacement forfs.readFile and fixes the behavior of special files such as /dev/stdin.

# rw.readFileSync(path[,options])

Reads the file at the specifiedpath completely into memory, synchronously, returning the data. If an error occurred during read, this function throws an error instead. Ifoptions is a string, it specifies the encoding to use, in which case the read data will be a string; otherwiseoptions is an object, and may specify encoding and flag properties. This method is a drop-in replacement forfs.readFileSync and fixes the behavior of special files such as /dev/stdin.

# rw.writeFile(path,data[,options],callback)

Writes the specifieddata (completely in memory) to a file at the specifiedpath, invoking the specifiedcallback once the data is completely written and the file is closed. Thecallback is invoked with a single argument: theerror that occurred during write (hopefully null). Ifoptions is a string, it specifies the encoding to use, in which case thedata must be a string; otherwiseoptions is an object, and may specify encoding, mode and flag properties. This method is a drop-in replacement forfs.writeFile and fixes the behavior of special files such as /dev/stdout.

# rw.writeFileSync(path,data[,options])

Writes the specifieddata (completely in memory) to a file at the specifiedpath, synchronously, returning once the data is completely written and the file is closed. Throws anerror if one occurs during write. Ifoptions is a string, it specifies the encoding to use, in which case thedata must be a string; otherwiseoptions is an object, and may specify encoding, mode and flag properties. This method is a drop-in replacement forfs.writeFileSync and fixes the behavior of special files such as /dev/stdout.

# rw.dash.readFile(path[,options],callback)

Equivalent torw.readFile, except treats apath of- as/dev/stdin. Useful for command-line arguments.

# rw.dash.readFileSync(path[,options])

Equivalent torw.readFileSync, except treats apath of- as/dev/stdin. Useful for command-line arguments.

# rw.dash.writeFile(path,data[,options],callback)

Equivalent torw.writeFile, except treats apath of- as/dev/stdout. Useful for command-line arguments.

# rw.dash.writeFileSync(path,data[,options])

Equivalent torw.writeFileSync, except treats apath of- as/dev/stdout. Useful for command-line arguments.

About

Now stdin and stdout are files.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp