Comparing version 0.0.8 to 0.0.10
@@ -7,4 +7,6 @@ exports.read = require("./lib/rw/read"); | ||
exports.writer = require("./lib/rw/writer"); | ||
exports.lineWriter = require("./lib/rw/line-writer"); | ||
exports.reader = require("./lib/rw/reader"); | ||
exports.lineReader = require("./lib/rw/line-reader"); | ||
exports.fixedReader = require("./lib/rw/fixed-reader"); |
var reader = require("./reader"); | ||
module.exports = function(filePath) { | ||
var read = reader(filePath), // TODO propagate options | ||
callbacks, | ||
var read = reader(filePath), // TODO options | ||
defers, | ||
buffer = new Buffer(0), | ||
@@ -12,12 +12,23 @@ bufferIndex = 0, | ||
return function readFixed(length, callback) { | ||
if (!((length = +length) >= 0)) return void callback(new Error("invalid length")); | ||
if (!((length = +length) >= 0)) throw new Error("invalid length"); | ||
if (!callback) throw new Error("callback is required"); | ||
// If we’ve reached the end of the file, return null. | ||
if (!buffer) return void callback(null, null); | ||
if (!buffer) { | ||
// Combine this with previously-read fragments, if any. | ||
if (fragments) { | ||
fragment = Buffer.concat(fragments); | ||
fragments = null; | ||
return void callback(null, fragment); | ||
} | ||
return void callback(null, null); | ||
} | ||
// If we’re at the end of our buffer, read some bytes, then try again. | ||
if (bufferIndex >= bufferLength) { | ||
var args = [length, callback]; | ||
if (callbacks) return void callbacks.push(args); | ||
callbacks = [args]; | ||
var defer = [length, callback]; | ||
if (defers) return void defers.push(defer); | ||
defers = [defer]; | ||
return void read(function(error_, buffer_) { | ||
@@ -28,5 +39,5 @@ error = error_; | ||
bufferLength = buffer && buffer.length; | ||
var callbacks_ = callbacks; | ||
callbacks = null; | ||
callbacks_.forEach(function(args) { readFixed(args[0], args[1]); }); | ||
var defers_ = defers; | ||
defers = null; | ||
defers_.forEach(function(defer) { readFixed(defer[0], defer[1]); }); | ||
}); | ||
@@ -42,3 +53,3 @@ } | ||
// Combine this with previously-read line fragments, if any. | ||
// Combine this with previously-read fragments, if any. | ||
if (fragments) { | ||
@@ -45,0 +56,0 @@ fragments.push(fragment); |
var reader = require("./reader"); | ||
module.exports = function(filePath) { | ||
var read = reader(filePath), // TODO propagate options | ||
callbacks, | ||
var read = reader(filePath), // TODO options | ||
defers, | ||
buffer = new Buffer(0), | ||
bufferIndex = 0, | ||
bufferLength = 0, | ||
encoding = "utf8", // TODO options | ||
encoding = "utf8", | ||
fragments = null; | ||
return function readLine(callback) { | ||
if (!callback) throw new Error("callback is required"); | ||
@@ -19,4 +20,4 @@ // If we’ve reached the end of the file, return null. | ||
if (bufferIndex >= bufferLength) { | ||
if (callbacks) return void callbacks.push(callback); | ||
callbacks = [callback]; | ||
if (defers) return void defers.push(callback); | ||
defers = [callback]; | ||
return void read(function(error_, buffer_) { | ||
@@ -27,5 +28,5 @@ error = error_; | ||
bufferLength = buffer && buffer.length; | ||
var callbacks_ = callbacks; | ||
callbacks = null; | ||
callbacks_.forEach(readLine); | ||
var defers_ = defers; | ||
defers = null; | ||
defers_.forEach(readLine); | ||
}); | ||
@@ -32,0 +33,0 @@ } |
@@ -6,7 +6,8 @@ var fs = require("fs"); | ||
eof = false, | ||
callbacks = null, | ||
defers = [], | ||
descriptor = null, | ||
bufferLength = 1 << 14; // TODO options | ||
return function read(callback) { | ||
function read(callback) { | ||
if (!callback) throw new Error("callback is required"); | ||
@@ -20,27 +21,26 @@ // If there’s an error, return the error. | ||
// If there’s an operation already in-progress, wait for that to finish. | ||
if (callbacks) return void callbacks.push(callback); | ||
if (defers) return void defers.push(callback); | ||
// If the file hasn’t been opened yet, open it, and then read. | ||
if (!descriptor) { | ||
callbacks = [callback]; | ||
return void fs.open(filePath, "r", 438 /*=0666*/, function(error_, descriptor_) { | ||
error = error_; | ||
descriptor = descriptor_; | ||
var callbacks_ = callbacks; | ||
callbacks = null; | ||
callbacks_.forEach(read); | ||
}); | ||
} | ||
// Otherwise, read some bytes from the file. | ||
callbacks = []; | ||
defers = []; | ||
fs.read(descriptor, new Buffer(bufferLength), 0, bufferLength, null, function(error_, bufferLength_, buffer) { | ||
error = error_; | ||
if (bufferLength_) callback(null, buffer.slice(0, bufferLength_)); | ||
if (bufferLength_) callback(null, bufferLength_ < bufferLength ? buffer.slice(0, bufferLength_) : buffer); | ||
else eof = true, fs.close(descriptor, callback); | ||
var callbacks_ = callbacks; | ||
callbacks = null; | ||
callbacks_.forEach(read); | ||
var defers_ = defers; | ||
defers = null; | ||
defers_.forEach(read); | ||
}); | ||
}; | ||
} | ||
// Open the file for reading. | ||
fs.open(filePath, "r", 438 /*=0666*/, function(error_, descriptor_) { | ||
error = error_; | ||
descriptor = descriptor_; | ||
var defers_ = defers; | ||
defers = null; | ||
defers_.forEach(read); | ||
}); | ||
return read; | ||
}; |
@@ -5,3 +5,3 @@ var fs = require("fs"); | ||
var error = null, | ||
callbacks = null, | ||
defers = [], | ||
descriptor = null, | ||
@@ -12,3 +12,4 @@ bufferLength = 1 << 14, // TODO options | ||
return function write(data, callback) { | ||
function write(data, callback) { | ||
if (!callback) throw new Error("callback is required"); | ||
@@ -19,21 +20,8 @@ // If there’s an error, return the error. | ||
// If there’s an operation already in-progress, wait for that to finish. | ||
if (callbacks) return void callbacks.push(callback); | ||
if (defers) return void defers.push([data, callback]); | ||
// If the file hasn’t been opened yet, open it, and then read. | ||
if (!descriptor) { | ||
callbacks = [[data, callback]]; | ||
return void fs.open(filePath, "w", 438 /*=0666*/, function(error_, descriptor_) { | ||
error = error_; | ||
descriptor = descriptor_; | ||
var callbacks_ = callbacks; | ||
callbacks = null; | ||
callbacks_.forEach(function(args) { write(args[0], args[1]); }); | ||
}); | ||
} | ||
// If the file is done, flush the buffer and close the file descriptor. | ||
if (data == null) { | ||
error = new Error("already closed"); | ||
return void fs.write(descriptor, buffer, 0, bufferIndex, null, function(error_, bufferLength_) { | ||
if (!error_ && bufferLength_ < bufferIndex) error_ = new Error("unable to write"); | ||
return void writeAll(descriptor, buffer, 0, bufferIndex, function(error_) { | ||
if (error_) return void callback(error_); | ||
@@ -44,3 +32,4 @@ fs.close(descriptor, callback); | ||
// If we can write this to the buffer, do so. | ||
// Always copy the input data into our private buffer, | ||
// so that we ignore changes to the input after this function returns. | ||
var bufferAdded = Math.min(bufferLength - bufferIndex, data.length); | ||
@@ -54,12 +43,30 @@ data.copy(buffer, bufferIndex, 0, bufferAdded); | ||
// Otherwise, write accumulated bytes to the file. | ||
callbacks = [[data.slice(bufferAdded), callback]]; | ||
fs.write(descriptor, buffer, 0, bufferIndex, null, function(error_, bufferLength_) { | ||
if (!error_ && bufferLength_ < bufferIndex) error_ = new Error("unable to write"); | ||
defers = [[data.slice(bufferAdded), callback]]; | ||
writeAll(descriptor, buffer, 0, bufferIndex, function(error_) { | ||
error = error_; | ||
bufferIndex = 0; | ||
var callbacks_ = callbacks; | ||
callbacks = null; | ||
callbacks_.forEach(function(args) { write(args[0], args[1]); }); | ||
var defers_ = defers; | ||
defers = null; | ||
defers_.forEach(function(defer) { write(defer[0], defer[1]); }); | ||
}); | ||
}; | ||
} | ||
// Open the file for writing. | ||
fs.open(filePath, "w", 438 /*=0666*/, function(error_, descriptor_) { | ||
error = error_; | ||
descriptor = descriptor_; | ||
var defers_ = defers; | ||
defers = null; | ||
defers_.forEach(function(defer) { write(defer[0], defer[1]); }); | ||
}); | ||
return write; | ||
}; | ||
function writeAll(descriptor, buffer, bufferIndex, bufferLength, callback) { | ||
fs.write(descriptor, buffer, bufferIndex, bufferLength, null, function(error_, bufferLength_) { | ||
if (error_) return void callback(error_.code === "EPIPE" ? null : error_); // ignore broken pipe, e.g., | head | ||
if (bufferLength_ < bufferLength) return void writeAll(descriptor, buffer, bufferIndex + bufferLength_, bufferLength - bufferLength_, callback); | ||
callback(null); | ||
}); | ||
} |
{ | ||
"name": "rw", | ||
"version": "0.0.8", | ||
"version": "0.0.10", | ||
"description": "Wrappers of fs.{read,write}File that work for /dev/std{in,out}.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
21693
26
339