Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Nov 18, 2024
1 parent 1abda0f commit 5375216
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
},
"overrides": [
{
"files": ["lib/*js"],
"files": ["lib/*.js"],
"parserOptions": {
"ecmaVersion": 2019,
"sourceType": "commonjs"
Expand Down
2 changes: 0 additions & 2 deletions addon/compression.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


#include "compression.h"

std::vector<uint8_t> mongodb_zstd::compress(const std::vector<uint8_t>& data,
Expand Down
18 changes: 9 additions & 9 deletions addon/compression_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ namespace mongodb_zstd {
* @brief An asynchronous Napi::Worker that can be with any function that produces
* CompressionResults.
* */
class CompressionWorker final : public Napi::AsyncWorker {
class CompressionWorker final : public AsyncWorker {
public:
CompressionWorker(const Napi::Function& callback, std::function<std::vector<uint8_t>()> worker)
: Napi::AsyncWorker{callback, "compression worker"}, m_worker(worker), m_result{} {}
CompressionWorker(const Function& callback, std::function<std::vector<uint8_t>()> worker)
: AsyncWorker{callback, "compression worker"}, m_worker(worker), m_result{} {}

protected:
void Execute() {
void Execute() final {
m_result = m_worker();
}

void OnOK() {
void OnOK() final {
if (!m_result.has_value()) {
Callback().Call({Napi::Error::New(Env(),
"zstd runtime error - async worker finished without "
"a compression or decompression result.")
Callback().Call({Error::New(Env(),
"zstd runtime - async worker finished without "
"a compression or decompression result.")
.Value()});
return;
}

std::vector<uint8_t> data = *m_result;
const std::vector<uint8_t>& data = m_result.value();
Buffer result = Buffer<uint8_t>::Copy(Env(), data.data(), data.size());

Callback().Call({Env().Undefined(), result});
Expand Down
14 changes: 7 additions & 7 deletions addon/zstd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using namespace Napi;

namespace mongodb_zstd {
void Compress(const Napi::CallbackInfo& info) {
void Compress(const CallbackInfo& info) {
// Argument handling happens in JS
if (info.Length() != 3) {
const char* error_message = "Expected three arguments.";
Expand All @@ -21,7 +21,7 @@ void Compress(const Napi::CallbackInfo& info) {
std::vector<uint8_t> data(to_compress.Data(), to_compress.Data() + to_compress.ElementLength());

size_t compression_level = static_cast<size_t>(info[1].ToNumber().Int32Value());
const Napi::Function& callback = info[2].As<Function>();
Function callback = info[2].As<Function>();

CompressionWorker* worker =
new CompressionWorker(callback, [data = std::move(data), compression_level] {
Expand All @@ -38,20 +38,20 @@ void Decompress(const CallbackInfo& info) {
throw TypeError::New(info.Env(), error_message);
}

Napi::Uint8Array compressed_data = info[0].As<Uint8Array>();
Uint8Array compressed_data = info[0].As<Uint8Array>();
std::vector<uint8_t> data(compressed_data.Data(),
compressed_data.Data() + compressed_data.ElementLength());
const Napi::Function& callback = info[1].As<Function>();
Function callback = info[1].As<Function>();

CompressionWorker* worker = new CompressionWorker(
callback, [data = std::move(data)] { return mongodb_zstd::decompress(data); });

worker->Queue();
}

Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "compress"), Napi::Function::New(env, Compress));
exports.Set(Napi::String::New(env, "decompress"), Napi::Function::New(env, Decompress));
Object Init(Env env, Object exports) {
exports.Set(String::New(env, "compress"), Function::New(env, Compress));
exports.Set(String::New(env, "decompress"), Function::New(env, Decompress));
return exports;
}

Expand Down
2 changes: 1 addition & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
'msvs_settings': {
'VCCLCompilerTool': { 'ExceptionHandling': 1 },
},
'link_settings': {
'link_settings': {
'libraries': [
'<(module_root_dir)/deps/zstd/build/cmake/lib/libzstd.a',
]
Expand Down

0 comments on commit 5375216

Please sign in to comment.