Skip to content

Instantly share code, notes, and snippets.

@kriskowal
Created May 5, 2014 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kriskowal/4488ec2443bd51ed9624 to your computer and use it in GitHub Desktop.
Save kriskowal/4488ec2443bd51ed9624 to your computer and use it in GitHub Desktop.
var nodeFs = require("fs");
var fs = {};
fs.readFile = function (path, callback) {
nodeFs.readFile(path, callback);
};
fs.readChunk = function (path, start, end, callback) {
var stream = nodeFs.createReadStream(path, {
start: start,
end: end - 1
});
var chunks = [];
stream.on("readable", function () {
var chunk = stream.read();
if (chunk === null) {
callback(null, join(chunks));
} else {
chunks.push(chunk);
}
});
stream.on("error", function (error) {
callback(error);
});
};
fs.writeFile = function (path, binary, callback) {
nodeFs.writeFile(path, binary, callback);
};
fs.readDir = function (path, callback) {
nodeFs.readdir(path, callback);
};
function join(buffers) {
var length = 0;
var at;
var index;
var count = buffers.length;
var buffer;
var result;
for (index = 0; index < count; index++) {
buffer = buffers[index];
length += buffer.length;
}
result = new Buffer(length);
at = 0;
for (index = 0; index < count; index++) {
buffer = buffers[index];
buffer.copy(result, at, 0);
at += buffer.length;
}
buffers.splice(0, count, result);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment