I am trying to read a file in the NAPI application and call the callback function to write it to the writestream in the nodejs application.
exmaple_Class.cpp
void readFromTransientFile(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Function cb = info[0].As<Napi::Function>();
while ((this->actualClass_->pos() < this->actualClass_->size()) &&
((readLength = this->actualClass_->read((void *)tempBuffer, sizeof(tempBuffer))) > 0))
{
//cb.Call(env.Global(), {Napi::Buffer<char>::New(env, tempBuffer, sizeof(tempBuffer))});
cb.Call(env.Global(), {Napi::String::New(env, tempBuffer)});
writeTotal += readLength;
}
std::cout << "Done!" << std::endl;
}
exmaple_Class.js
const testAddon = require("./build/Release/testaddon.node");
const fs = require("fs");
const prevInstance = new testAddon.OtClass();
const writeStream = fs.createWriteStream("./downloads/lake.jpg");
prevInstance.readFromTransientFile(
function (msg) {
console.log(">> received buffer from getFile: ", msg);
console.log(">> typeof msg", typeof msg);
writeStream.write(msg);
}
);
writeStream.end();
Limitation of C++ side function is that it cannot return value, so data has to return in the callback. The funny part is that if it is a text file, it works correctly but for other types of files like zip or jpeg, I get garbled data. If I pass a file descriptor to a C++ function and use UNIX write function, then I get the file. But I would like to send that data over HTTP using express as well. So what is going wrong? How can I correctly wrap and return binary data in NAPI objects.