| Current Path : /home/emeraadmin/public_html/4d695/ |
| Current File : /home/emeraadmin/public_html/4d695/rw.tar |
package.json 0000644 00000002722 15167672762 0007060 0 ustar 00 {
"_args": [
[
"rw@1.3.3",
"C:\\Users\\Ovi-PC\\Downloads\\themekit-master\\themekit"
]
],
"_from": "rw@1.3.3",
"_id": "rw@1.3.3",
"_inBundle": false,
"_integrity": "sha1-P4Yt+pGrdmsUiF700BEkv9oHT7Q=",
"_location": "/rw",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "rw@1.3.3",
"name": "rw",
"escapedName": "rw",
"rawSpec": "1.3.3",
"saveSpec": null,
"fetchSpec": "1.3.3"
},
"_requiredBy": [
"/d3-dsv"
],
"_resolved": "https://registry.npmjs.org/rw/-/rw-1.3.3.tgz",
"_spec": "1.3.3",
"_where": "C:\\Users\\Ovi-PC\\Downloads\\themekit-master\\themekit",
"author": {
"name": "Mike Bostock",
"url": "http://bost.ocks.org/mike"
},
"bugs": {
"url": "https://github.com/mbostock/rw/issues"
},
"description": "Now stdin and stdout are files.",
"devDependencies": {
"d3-queue": "3",
"eslint": "3"
},
"homepage": "https://github.com/mbostock/rw",
"keywords": [
"fs",
"readFile",
"writeFile",
"stdin",
"stdout"
],
"license": "BSD-3-Clause",
"main": "index.js",
"name": "rw",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/mbostock/rw.git"
},
"scripts": {
"postpublish": "git push && git push --tags",
"prepublish": "npm test",
"test": "test/run-tests && eslint index.js lib"
},
"version": "1.3.3"
}
lib/rw/write-file-sync.js 0000644 00000001530 15167672762 0011343 0 ustar 00 var fs = require("fs"),
encode = require("./encode");
module.exports = function(filename, data, options) {
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(fd, data, bytesWritten, bytesTotal - bytesWritten, null);
} catch (error) {
if (error.code === "EPIPE") break; // ignore broken pipe, e.g., | head
fs.closeSync(fd);
throw error;
}
}
fs.closeSync(fd);
}
};
lib/rw/decode.js 0000644 00000001127 15167672762 0007547 0 ustar 00 module.exports = function(options) {
if (options) {
if (typeof options === "string") return encoding(options);
if (options.encoding !== null) return encoding(options.encoding);
}
return identity();
};
function identity() {
var chunks = [];
return {
push: function(chunk) { chunks.push(chunk); },
value: function() { return Buffer.concat(chunks); }
};
}
function encoding(encoding) {
var chunks = [];
return {
push: function(chunk) { chunks.push(chunk); },
value: function() { return Buffer.concat(chunks).toString(encoding); }
};
}
lib/rw/read-file-sync.js 0000644 00000001443 15167672762 0011127 0 ustar 00 var fs = require("fs"),
decode = require("./decode");
module.exports = function(filename, options) {
if (fs.statSync(filename).isFile()) {
return fs.readFileSync(filename, options);
} else {
var fd = fs.openSync(filename, options && options.flag || "r"),
decoder = decode(options);
while (true) { // eslint-disable-line no-constant-condition
try {
var buffer = new Buffer(bufferSize),
bytesRead = fs.readSync(fd, buffer, 0, bufferSize);
} catch (e) {
if (e.code === "EOF") break;
fs.closeSync(fd);
throw e;
}
if (bytesRead === 0) break;
decoder.push(buffer.slice(0, bytesRead));
}
fs.closeSync(fd);
return decoder.value();
}
};
var bufferSize = 1 << 16;
lib/rw/write-file.js 0000644 00000002054 15167672762 0010373 0 ustar 00 var fs = require("fs"),
encode = require("./encode");
module.exports = function(path, data, options, callback) {
if (arguments.length < 4) callback = options, options = null;
switch (path) {
case "/dev/stdout": return writeStream(process.stdout, "write", data, options, callback);
case "/dev/stderr": return writeStream(process.stderr, "write", data, options, callback);
}
fs.stat(path, function(error, stat) {
if (error && error.code !== "ENOENT") return callback(error);
if (stat && stat.isFile()) return fs.writeFile(path, data, options, callback);
writeStream(fs.createWriteStream(path, options ? {flags: options.flag || "w"} : {}), "end", data, options, callback); // N.B. flag / flags
});
};
function writeStream(stream, send, data, options, callback) {
stream.on("error", function(error) { callback(error.code === "EPIPE" ? null : error); }); // ignore broken pipe, e.g., | head
stream[send](encode(data, options), function(error) { callback(error && error.code === "EPIPE" ? null : error); });
}
lib/rw/encode.js 0000644 00000000374 15167672762 0007564 0 ustar 00 module.exports = function(data, options) {
return typeof data === "string"
? new Buffer(data, typeof options === "string" ? options
: options && options.encoding !== null ? options.encoding
: "utf8")
: data;
};
lib/rw/read-file.js 0000644 00000001470 15167672762 0010155 0 ustar 00 var fs = require("fs"),
decode = require("./decode");
module.exports = function(path, options, callback) {
if (arguments.length < 3) callback = options, options = null;
switch (path) {
case "/dev/stdin": return readStream(process.stdin, options, callback);
}
fs.stat(path, function(error, stat) {
if (error) return callback(error);
if (stat.isFile()) return fs.readFile(path, options, callback);
readStream(fs.createReadStream(path, options ? {flags: options.flag || "r"} : {}), options, callback); // N.B. flag / flags
});
};
function readStream(stream, options, callback) {
var decoder = decode(options);
stream.on("error", callback);
stream.on("data", function(d) { decoder.push(d); });
stream.on("end", function() { callback(null, decoder.value()); });
}
lib/rw/dash.js 0000644 00000001027 15167672762 0007242 0 ustar 00 var slice = Array.prototype.slice;
function dashify(method, file) {
return function(path) {
var argv = arguments;
if (path == "-") (argv = slice.call(argv)).splice(0, 1, file);
return method.apply(null, argv);
};
}
exports.readFile = dashify(require("./read-file"), "/dev/stdin");
exports.readFileSync = dashify(require("./read-file-sync"), "/dev/stdin");
exports.writeFile = dashify(require("./write-file"), "/dev/stdout");
exports.writeFileSync = dashify(require("./write-file-sync"), "/dev/stdout");
test/encode-object-async 0000644 00000000272 15167672762 0011306 0 ustar 00 #!/usr/bin/env node
var rw = require("../").dash;
rw.writeFile(process.argv[2] || "-", "gréén\n", {encoding: process.argv[3]}, function(error) {
if (error) throw error;
});
test/cat-sync 0000644 00000000211 15167672762 0007204 0 ustar 00 #!/usr/bin/env node
var rw = require("../").dash;
rw.writeFileSync("-", rw.readFileSync(process.argv[2] || "-", "utf8"), "utf8");
test/encoding-async 0000644 00000002514 15167672762 0010374 0 ustar 00 #!/usr/bin/env node
var fs = require("fs"),
queue = require("d3-queue").queue,
rw = require("../");
var code = 0;
queue(1)
.defer(testRead, "utf8", "gréén\n")
.defer(testRead, {encoding: "utf8"}, "gréén\n")
.defer(testRead, "ascii", "grC)C)n\n")
.defer(testRead, {encoding: "ascii"}, "grC)C)n\n")
.defer(testWrite, "utf8", "gréén\n")
.defer(testWrite, {encoding: "utf8"}, "gréén\n")
.defer(testWrite, "ascii", "gr��n\n")
.defer(testWrite, {encoding: "ascii"}, "gr��n\n")
.await(done);
function testRead(options, expected, callback) {
rw.readFile("test/utf8.txt", options, function(error, actual) {
if (error) return void callback(error);
if (actual !== expected) console.warn(actual + " !== " + expected), code = 1;
callback(null);
});
}
function testWrite(options, expected, callback) {
rw.writeFile("test/encoding-async.out", "gréén\n", options, function(error) {
if (error) return void callback(error);
fs.readFile("test/encoding-async.out", "utf8", function(error, actual) {
if (error) return void callback(error);
if (actual !== expected) console.warn(actual + " !== " + expected), code = 1;
callback(null);
});
});
}
function done(error) {
if (error) throw error;
process.exit(code);
}
test/write-async 0000644 00000000251 15167672762 0007734 0 ustar 00 #!/usr/bin/env node
var rw = require("../").dash;
rw.writeFile(process.argv[2] || "-", "Hello, world!", "utf8", function(error) {
if (error) throw error;
});
test/encoding-sync 0000644 00000003122 15167672762 0010227 0 ustar 00 #!/usr/bin/env node
var fs = require("fs"),
rw = require("../");
var code = 0,
actual,
expected;
if ((actual = rw.readFileSync("test/utf8.txt", "utf8")) !== (expected = "gréén\n")) code = 1, console.warn(actual + " !== " + expected);
if ((actual = rw.readFileSync("test/utf8.txt", {encoding: "utf8"})) !== (expected = "gréén\n")) code = 1, console.warn(actual + " !== " + expected);
if ((actual = rw.readFileSync("test/utf8.txt", "ascii")) !== (expected = "grC)C)n\n")) code = 1, console.warn(actual + " !== " + expected);
if ((actual = rw.readFileSync("test/utf8.txt", {encoding: "ascii"})) !== (expected = "grC)C)n\n")) code = 1, console.warn(actual + " !== " + expected);
rw.writeFileSync("test/encoding-sync.out", "gréén\n", "utf8"); if ((actual = fs.readFileSync("test/encoding-sync.out", "utf8")) !== (expected = "gréén\n")) code = 1, console.warn(actual + " !== " + expected);
rw.writeFileSync("test/encoding-sync.out", "gréén\n", {encoding: "utf8"}); if ((actual = fs.readFileSync("test/encoding-sync.out", "utf8")) !== (expected = "gréén\n")) code = 1, console.warn(actual + " !== " + expected);
rw.writeFileSync("test/encoding-sync.out", "gréén\n", "ascii"); if ((actual = fs.readFileSync("test/encoding-sync.out", "utf8")) !== (expected = "gr��n\n")) code = 1, console.warn(actual + " !== " + expected);
rw.writeFileSync("test/encoding-sync.out", "gréén\n", {encoding: "ascii"}); if ((actual = fs.readFileSync("test/encoding-sync.out", "utf8")) !== (expected = "gr��n\n")) code = 1, console.warn(actual + " !== " + expected);
process.exit(code);
test/utf8.txt 0000644 00000000011 15167672762 0007165 0 ustar 00 gréén
test/write-sync 0000644 00000000174 15167672762 0007577 0 ustar 00 #!/usr/bin/env node
var rw = require("../").dash;
rw.writeFileSync(process.argv[2] || "-", "Hello, world!", "utf8");
test/wc-async 0000644 00000000272 15167672762 0007216 0 ustar 00 #!/usr/bin/env node
var rw = require("../").dash;
rw.readFile(process.argv[2] || "-", function(error, contents) {
if (error) throw error;
console.log(contents.length);
});
test/encode-object-sync 0000644 00000000215 15167672762 0011142 0 ustar 00 #!/usr/bin/env node
var rw = require("../").dash;
rw.writeFileSync(process.argv[2] || "-", "gréén\n", {encoding: process.argv[3]});
test/wc-sync 0000644 00000000176 15167672762 0007060 0 ustar 00 #!/usr/bin/env node
var rw = require("../").dash;
console.log(rw.readFileSync(process.argv[2] || "-", "utf8").length);
test/run-tests 0000644 00000011530 15167672762 0007435 0 ustar 00 #!/bin/bash
FILE=test/input.txt
rm -f -- $FILE
for i in {1..10000}; do printf '%09X\n' $RANDOM >> $FILE; done
function test()
{
if [[ $1 -eq 0 ]]
then
echo -e "\x1B[1;32m✓ $2\x1B[0m"
else
echo -e "\x1B[1;31m✗ $2\x1B[0m"
fi
}
test/encoding-sync; test $? "encoding-sync applies the specified encodings"
test/encoding-async; test $? "encoding-async applies the specified encodings"
[ "$(test/wc-async $FILE)" = "100000" ]; test $? "wc-async reads an entire file"
[ "$(test/wc-sync $FILE)" = "100000" ]; test $? "wc-sync reads an entire file"
[ "$(test/wc-async < $FILE)" = "100000" ]; test $? "wc-async reads an entire file from stdin"
[ "$(test/wc-sync < $FILE)" = "100000" ]; test $? "wc-sync reads an entire file from stdin"
[ "$(cat $FILE | test/wc-async)" = "100000" ]; test $? "wc-async reads an entire file from a pipe"
[ "$(cat $FILE | test/wc-sync)" = "100000" ]; test $? "wc-sync reads an entire file from a pipe"
[ "$(test/cat-async $FILE | wc -c | tr -d ' ')" = "100000" ]; test $? "cat-async reads an entire file and writes it to a pipe"
[ "$(test/cat-sync $FILE | wc -c | tr -d ' ')" = "100000" ]; test $? "cat-sync reads an entire file and writes it to a pipe"
[ "$(test/cat-async $FILE | test/wc-async)" = "100000" ]; test $? "cat-async reads an entire file and writes it to a pipe to wc-async "
[ "$(test/cat-async $FILE | test/wc-sync)" = "100000" ]; test $? "cat-async reads an entire file and writes it to a pipe to wc-sync "
[ "$(test/cat-sync $FILE | test/wc-async)" = "100000" ]; test $? "cat-sync reads an entire file and writes it to a pipe to wc-async "
[ "$(test/cat-sync $FILE | test/wc-sync)" = "100000" ]; test $? "cat-sync reads an entire file and writes it to a pipe to wc-sync "
[ "$(test/cat-async < $FILE | wc -c | tr -d ' ')" = "100000" ]; test $? "cat-async reads an entire file from stdin and writes it to a pipe"
[ "$(test/cat-sync < $FILE | wc -c | tr -d ' ')" = "100000" ]; test $? "cat-sync reads an entire file from stdin and writes it to a pipe"
[ "$(test/cat-async < $FILE | test/wc-async)" = "100000" ]; test $? "cat-async reads an entire file from stdin and writes it to a pipe to wc-async"
[ "$(test/cat-async < $FILE | test/wc-sync)" = "100000" ]; test $? "cat-async reads an entire file from stdin and writes it to a pipe to wc-sync"
[ "$(test/cat-sync < $FILE | test/wc-async)" = "100000" ]; test $? "cat-sync reads an entire file from stdin and writes it to a pipe to wc-async"
[ "$(test/cat-sync < $FILE | test/wc-sync)" = "100000" ]; test $? "cat-sync reads an entire file from stdin and writes it to a pipe to wc-sync"
[ "$(cat $FILE | test/cat-async | test/wc-async)" = "100000" ]; test $? "cat-async reads an entire file from a pipe and writes it to a pipe to wc-async"
[ "$(cat $FILE | test/cat-async | test/wc-sync)" = "100000" ]; test $? "cat-async reads an entire file from a pipe and writes it to a pipe to wc-sync"
[ "$(cat $FILE | test/cat-sync | test/wc-async)" = "100000" ]; test $? "cat-sync reads an entire file from a pipe and writes it to a pipe to wc-async"
[ "$(cat $FILE | test/cat-sync | test/wc-sync)" = "100000" ]; test $? "cat-sync reads an entire file from a pipe and writes it to a pipe to wc-sync"
[ "$(cat $FILE | test/cat-async | head -n 100 | test/wc-async)" = "1000" ]; test $? "cat-async reads an entire file from a pipe and writes it to a pipe to head to wc-async"
[ "$(cat $FILE | test/cat-async | head -n 100 | test/wc-sync)" = "1000" ]; test $? "cat-async reads an entire file from a pipe and writes it to a pipe to head to wc-sync"
[ "$(cat $FILE | test/cat-sync | head -n 100 | test/wc-async)" = "1000" ]; test $? "cat-sync reads an entire file from a pipe and writes it to a pipe to head to wc-async"
[ "$(cat $FILE | test/cat-sync | head -n 100 | test/wc-sync)" = "1000" ]; test $? "cat-sync reads an entire file from a pipe and writes it to a pipe to head to wc-sync"
[ "$(cat $FILE 2> /dev/null | head -n 100 | test/cat-async | test/wc-async)" = "1000" ]; test $? "cat-async reads the head of a file from a pipe and writes it to wc-async"
[ "$(cat $FILE 2> /dev/null | head -n 100 | test/cat-async | test/wc-sync)" = "1000" ]; test $? "cat-async reads the head of a file from a pipe and writes it to wc-sync"
[ "$(cat $FILE 2> /dev/null | head -n 100 | test/cat-sync | test/wc-async)" = "1000" ]; test $? "cat-sync reads the head of a file from a pipe and writes it to wc-async"
[ "$(cat $FILE 2> /dev/null | head -n 100 | test/cat-sync | test/wc-sync)" = "1000" ]; test $? "cat-sync reads the head of a file from a pipe and writes it to wc-sync"
[ "$(test/write-async test/write.out && cat test/write.out)" = "Hello, world!" ]; test $? "write-async writes an entire file"
[ "$(test/write-sync test/write.out && cat test/write.out)" = "Hello, world!" ]; test $? "write-sync writes an entire file"
rm -f -- $FILE test/write.out test/encoding-sync.out test/encoding-async.out
test/encode-string-sync 0000644 00000000201 15167672762 0011175 0 ustar 00 #!/usr/bin/env node
var rw = require("../").dash;
rw.writeFileSync(process.argv[2] || "-", "gréén\n", process.argv[3]);
test/encode-string-async 0000644 00000000256 15167672762 0011350 0 ustar 00 #!/usr/bin/env node
var rw = require("../").dash;
rw.writeFile(process.argv[2] || "-", "gréén\n", process.argv[3], function(error) {
if (error) throw error;
});
test/cat-async 0000644 00000000376 15167672762 0007361 0 ustar 00 #!/usr/bin/env node
var rw = require("../").dash;
rw.readFile(process.argv[2] || "-", "utf8", function(error, contents) {
if (error) throw error;
rw.writeFile("-", contents, "utf8", function(error) {
if (error) throw error;
});
});
LICENSE 0000644 00000002657 15167672762 0005606 0 ustar 00 Copyright (c) 2014-2016, Michael Bostock
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* The name Michael Bostock may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL MICHAEL BOSTOCK BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.eslintrc 0000644 00000000074 15167672762 0006414 0 ustar 00 env:
node: true
extends:
"eslint:recommended"
index.js 0000644 00000000414 15167672762 0006233 0 ustar 00 exports.dash = require("./lib/rw/dash");
exports.readFile = require("./lib/rw/read-file");
exports.readFileSync = require("./lib/rw/read-file-sync");
exports.writeFile = require("./lib/rw/write-file");
exports.writeFileSync = require("./lib/rw/write-file-sync");
.npmignore 0000644 00000000051 15167672762 0006562 0 ustar 00 .DS_Store
node_modules
test/input.txt
README.md 0000644 00000016733 15167672762 0006060 0 ustar 00 # rw - Now stdin and stdout are files.
How do you read a file from stdin? If you thought,
```js
var contents = 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,
```js
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:
```js
var chunks = [];
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 to [read that file synchronously](https://github.com/mbostock/rw/blob/master/lib/rw/read-file-sync.js).
You could also try a different pattern for writing to stdout:
```js
process.stdout.write(contents);
```
Or even:
```js
console.log(contents);
```
But if you try to pipe your output to `head`, you’ll get this error:
```
Error: write EPIPE
at errnoException (net.js:904:11)
at Object.afterWrite (net.js:720:19)
```
Huh.
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.
For example, now you can read stdin synchronously like so:
```js
var contents = rw.readFileSync("/dev/stdin", "utf8");
```
Or to write to stdout:
```js
rw.writeFileSync("/dev/stdout", contents, "utf8");
```
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`.
### Note
If you want to read synchronously from stdin using [readFileSync](#readFileSync), you cannot also use process.stdin in the same program. Likewise, if you want to write synchronously to stdout or stderr using [writeFileSync](#writeFileSync), 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 methods [readFile](#readFile) and [writeFile](#writeFile) are supported on Windows. Node has no synchronous API for reading from process.[stdin](https://nodejs.org/api/process.html#process_process_stdin) or writing to process.[stdout](https://nodejs.org/api/process.html#process_process_stdout) or process.[stderr](https://nodejs.org/api/process.html#process_process_stderr), so you’re out of luck!
## API Reference
<a name="readFile" href="#readFile">#</a> rw.<b>readFile</b>(<i>path</i>[, <i>options</i>], <i>callback</i>)
Reads the file at the specified *path* completely into memory, invoking the specified *callback* once the data is available and the file is closed. The *callback* is invoked with two arguments: the *error* that occurred during read (hopefully null), and the read data. If *options* is a string, it specifies the encoding to use, in which case the read data will be a string; otherwise *options* is an object, and may specify encoding and flag properties. This method is a drop-in replacement for [fs.readFile](https://nodejs.org/api/fs.html#fs_fs_readfile_file_options_callback) and fixes the behavior of special files such as /dev/stdin.
<a name="readFileSync" href="#readFileSync">#</a> rw.<b>readFileSync</b>(<i>path</i>[, <i>options</i>])
Reads the file at the specified *path* completely into memory, synchronously, returning the data. If an error occurred during read, this function throws an error instead. If *options* is a string, it specifies the encoding to use, in which case the read data will be a string; otherwise *options* is an object, and may specify encoding and flag properties. This method is a drop-in replacement for [fs.readFileSync](https://nodejs.org/api/fs.html#fs_fs_readfilesync_file_options) and fixes the behavior of special files such as /dev/stdin.
<a name="writeFile" href="#writeFile">#</a> rw.<b>writeFile</b>(<i>path</i>, <i>data</i>[, <i>options</i>], <i>callback</i>)
Writes the specified *data* (completely in memory) to a file at the specified *path*, invoking the specified *callback* once the data is completely written and the file is closed. The *callback* is invoked with a single argument: the *error* that occurred during write (hopefully null). If *options* is a string, it specifies the encoding to use, in which case the *data* must be a string; otherwise *options* is an object, and may specify encoding, mode and flag properties. This method is a drop-in replacement for [fs.writeFile](https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback) and fixes the behavior of special files such as /dev/stdout.
<a name="writeFileSync" href="#writeFileSync">#</a> rw.<b>writeFileSync</b>(<i>path</i>, <i>data</i>[, <i>options</i>])
Writes the specified *data* (completely in memory) to a file at the specified *path*, synchronously, returning once the data is completely written and the file is closed. Throws an *error* if one occurs during write. If *options* is a string, it specifies the encoding to use, in which case the *data* must be a string; otherwise *options* is an object, and may specify encoding, mode and flag properties. This method is a drop-in replacement for [fs.writeFileSync](https://nodejs.org/api/fs.html#fs_fs_writefilesync_file_data_options) and fixes the behavior of special files such as /dev/stdout.
<a name="dash_readFile" href="#dash_readFile">#</a> rw.dash.<b>readFile</b>(<i>path</i>[, <i>options</i>], <i>callback</i>)
Equivalent to [rw.readFile](#readFile), except treats a *path* of `-` as `/dev/stdin`. Useful for command-line arguments.
<a name="dash_readFileSync" href="#dash_readFileSync">#</a> rw.dash.<b>readFileSync</b>(<i>path</i>[, <i>options</i>])
Equivalent to [rw.readFileSync](#readFileSync), except treats a *path* of `-` as `/dev/stdin`. Useful for command-line arguments.
<a name="dash_writeFile" href="#dash_writeFile">#</a> rw.dash.<b>writeFile</b>(<i>path</i>, <i>data</i>[, <i>options</i>], <i>callback</i>)
Equivalent to [rw.writeFile](#writeFile), except treats a *path* of `-` as `/dev/stdout`. Useful for command-line arguments.
<a name="dash_writeFileSync" href="#dash_writeFileSync">#</a> rw.dash.<b>writeFileSync</b>(<i>path</i>, <i>data</i>[, <i>options</i>])
Equivalent to [rw.writeFileSync](#writeFileSync), except treats a *path* of `-` as `/dev/stdout`. Useful for command-line arguments.
write-file-sync.js 0000644 00000001530 15170145312 0010121 0 ustar 00 var fs = require("fs"),
encode = require("./encode");
module.exports = function(filename, data, options) {
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(fd, data, bytesWritten, bytesTotal - bytesWritten, null);
} catch (error) {
if (error.code === "EPIPE") break; // ignore broken pipe, e.g., | head
fs.closeSync(fd);
throw error;
}
}
fs.closeSync(fd);
}
};
decode.js 0000644 00000001127 15170145312 0006325 0 ustar 00 module.exports = function(options) {
if (options) {
if (typeof options === "string") return encoding(options);
if (options.encoding !== null) return encoding(options.encoding);
}
return identity();
};
function identity() {
var chunks = [];
return {
push: function(chunk) { chunks.push(chunk); },
value: function() { return Buffer.concat(chunks); }
};
}
function encoding(encoding) {
var chunks = [];
return {
push: function(chunk) { chunks.push(chunk); },
value: function() { return Buffer.concat(chunks).toString(encoding); }
};
}
read-file-sync.js 0000644 00000001443 15170145312 0007705 0 ustar 00 var fs = require("fs"),
decode = require("./decode");
module.exports = function(filename, options) {
if (fs.statSync(filename).isFile()) {
return fs.readFileSync(filename, options);
} else {
var fd = fs.openSync(filename, options && options.flag || "r"),
decoder = decode(options);
while (true) { // eslint-disable-line no-constant-condition
try {
var buffer = new Buffer(bufferSize),
bytesRead = fs.readSync(fd, buffer, 0, bufferSize);
} catch (e) {
if (e.code === "EOF") break;
fs.closeSync(fd);
throw e;
}
if (bytesRead === 0) break;
decoder.push(buffer.slice(0, bytesRead));
}
fs.closeSync(fd);
return decoder.value();
}
};
var bufferSize = 1 << 16;
write-file.js 0000644 00000002054 15170145312 0007151 0 ustar 00 var fs = require("fs"),
encode = require("./encode");
module.exports = function(path, data, options, callback) {
if (arguments.length < 4) callback = options, options = null;
switch (path) {
case "/dev/stdout": return writeStream(process.stdout, "write", data, options, callback);
case "/dev/stderr": return writeStream(process.stderr, "write", data, options, callback);
}
fs.stat(path, function(error, stat) {
if (error && error.code !== "ENOENT") return callback(error);
if (stat && stat.isFile()) return fs.writeFile(path, data, options, callback);
writeStream(fs.createWriteStream(path, options ? {flags: options.flag || "w"} : {}), "end", data, options, callback); // N.B. flag / flags
});
};
function writeStream(stream, send, data, options, callback) {
stream.on("error", function(error) { callback(error.code === "EPIPE" ? null : error); }); // ignore broken pipe, e.g., | head
stream[send](encode(data, options), function(error) { callback(error && error.code === "EPIPE" ? null : error); });
}
encode.js 0000644 00000000374 15170145312 0006342 0 ustar 00 module.exports = function(data, options) {
return typeof data === "string"
? new Buffer(data, typeof options === "string" ? options
: options && options.encoding !== null ? options.encoding
: "utf8")
: data;
};
read-file.js 0000644 00000001470 15170145312 0006733 0 ustar 00 var fs = require("fs"),
decode = require("./decode");
module.exports = function(path, options, callback) {
if (arguments.length < 3) callback = options, options = null;
switch (path) {
case "/dev/stdin": return readStream(process.stdin, options, callback);
}
fs.stat(path, function(error, stat) {
if (error) return callback(error);
if (stat.isFile()) return fs.readFile(path, options, callback);
readStream(fs.createReadStream(path, options ? {flags: options.flag || "r"} : {}), options, callback); // N.B. flag / flags
});
};
function readStream(stream, options, callback) {
var decoder = decode(options);
stream.on("error", callback);
stream.on("data", function(d) { decoder.push(d); });
stream.on("end", function() { callback(null, decoder.value()); });
}
dash.js 0000644 00000001027 15170145312 0006020 0 ustar 00 var slice = Array.prototype.slice;
function dashify(method, file) {
return function(path) {
var argv = arguments;
if (path == "-") (argv = slice.call(argv)).splice(0, 1, file);
return method.apply(null, argv);
};
}
exports.readFile = dashify(require("./read-file"), "/dev/stdin");
exports.readFileSync = dashify(require("./read-file-sync"), "/dev/stdin");
exports.writeFile = dashify(require("./write-file"), "/dev/stdout");
exports.writeFileSync = dashify(require("./write-file-sync"), "/dev/stdout");