Skip to content

Commit

Permalink
try catching
Browse files Browse the repository at this point in the history
  • Loading branch information
mmomtchev committed Jan 2, 2024
1 parent 77c89a6 commit 3c1f39d
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions src/binding/avcpp-readable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,34 +70,39 @@ int ReadableCustomIO::seekable() const { return 0; }
// with the lock already held
void ReadableCustomIO::PushPendingData(int64_t to_read) {
verbose("ReadableCustomIO: push pending data: request %lu bytes\n", to_read);
Napi::Env env(Env());
Napi::Function push = this->Value().Get("push").As<Napi::Function>();
assert(!queue.empty());
do {
auto buf = queue.front();
queue.pop();
if (buf->data == nullptr) {
// This is EOF
verbose("ReadableCustomIO: pushing null to signal EOF\n");
push.MakeCallback(this->Value(), {env.Null()});
delete buf;
eof = true;
return;
}
to_read -= buf->length;
// Some alternative Node-API implementations (Electron for example) disallow external buffers
try {
Napi::Env env(Env());
Napi::Function push = this->Value().Get("push").As<Napi::Function>();
assert(!queue.empty());
do {
auto buf = queue.front();
queue.pop();
if (buf->data == nullptr) {
// This is EOF
verbose("ReadableCustomIO: pushing null to signal EOF\n");
push.MakeCallback(this->Value(), {env.Null()});
delete buf;
eof = true;
return;
}
to_read -= buf->length;
// Some alternative Node-API implementations (Electron for example) disallow external buffers
#ifdef NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
napi_value js_buffer = Napi::Buffer<uint8_t>::Copy(env, buf->data, buf->length);
delete[] buf->data;
napi_value js_buffer = Napi::Buffer<uint8_t>::Copy(env, buf->data, buf->length);
delete[] buf->data;
#else
napi_value js_buffer =
Napi::Buffer<uint8_t>::New(env, buf->data, buf->length, [](Napi::Env, uint8_t *buffer) { delete[] buffer; });
napi_value js_buffer =
Napi::Buffer<uint8_t>::New(env, buf->data, buf->length, [](Napi::Env, uint8_t *buffer) { delete[] buffer; });
#endif
verbose("ReadableCustomIO: pushed Buffer length %lu, request remaining %ld\n", buf->length, to_read);
push.MakeCallback(this->Value(), 1, &js_buffer);
queue_size -= buf->length;
delete buf;
} while (!queue.empty() && to_read > 0);
verbose("ReadableCustomIO: pushed Buffer length %lu, request remaining %ld\n", buf->length, to_read);
push.MakeCallback(this->Value(), 1, &js_buffer);
queue_size -= buf->length;
delete buf;
} while (!queue.empty() && to_read > 0);
} catch (const std::exception &e) {
verbose("ReadableCustomIO: C++ exception %s\n", e.what());
std::rethrow_exception(std::current_exception());
}
}

void ReadableCustomIO::_Read(const Napi::CallbackInfo &info) {
Expand Down

0 comments on commit 3c1f39d

Please sign in to comment.