I try to use sdk fs like that:
var fs = require('sdk/io/fs');
fs.readFile(filepath, 'utf-8', function (error, data) {
if (components.isSuccessCode(error)) {
var lines = data.toString().split(/[\r\n]+/);
for (var i = 0; i < lines.length; i++) {
listener.process(lines[i]);
}
}
else {
callback({error: error});
}
}
but i only get 2 first symbols of file(2 mb). Why i get such a weird behaviour?
Declaration of readFile from sdk:
function readFile(path, encoding, callback) {
if (isFunction(encoding)) {
callback = encoding
encoding = null
}
let buffer = null;
try {
let readStream = new ReadStream(path);
readStream.on("data", function(data) {
if (!buffer){
buffer = data;
}
else{
buffer = Buffer.concat([buffer, data], 2);
}
});
readStream.on("error", function onError(error) {
callback(error);
});
readStream.on("end", function onEnd() {
// Note: Need to destroy before invoking a callback
// so that file descriptor is released.
readStream.destroy();
callback(null, buffer);
});
}
catch (error) {
setTimeout(callback, 0, error);
}
};
Don't use fs.readFile use OS.file: https://developer.mozilla.org/en-US/docs/JavaScript_OS.File/OS.File_for_the_main_thread