Skip to content

Commit

Permalink
use builtin types
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Nov 14, 2024
1 parent 695581c commit 7b1e0e6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 60 deletions.
5 changes: 2 additions & 3 deletions addon/compress.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ CompressionResult compress(const std::vector<uint8_t> data, size_t compression_l
ZSTD_compress(output.data(), output.size(), data.data(), data.size(), compression_level);

if (ZSTD_isError(result_code)) {
std::string error(ZSTD_getErrorName(result_code));
return CompressionResult::Error(error);
return std::string(ZSTD_getErrorName(result_code));
}

output.resize(result_code);

return CompressionResult::Ok(output);
return output;
}
79 changes: 26 additions & 53 deletions addon/compression_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,16 @@

using namespace Napi;

/**
* @brief A class that represents the result of a compression operation. Once the
* MACOS_DEPLOYMENT_TARGET can be raised to 10.13 and use a c++17, we can remove this class and use
* a std::optional<std::variant<std::vector<uint8_t>, std::string>>> instead.
*/
struct CompressionResult {
CompressionResult(std::string error,
std::vector<uint8_t> result,
bool hasError,
bool hasResult,
bool initialized)
: error(error),
result(result),
hasError(hasError),
hasResult(hasResult),
initialized(true) {}

public:
static CompressionResult Error(std::string error) {
return CompressionResult(error, std::vector<uint8_t>(), true, false, true);
}

static CompressionResult Ok(std::vector<uint8_t> result) {
return CompressionResult(std::string(""), result, false, true, true);
}

static CompressionResult Empty() {
return CompressionResult(std::string(""), std::vector<uint8_t>(), false, false, false);
}
using CompressionResult = std::variant<std::vector<uint8_t>, std::string>;

std::string error;
std::vector<uint8_t> result;

bool hasError;
bool hasResult;
bool initialized;
// Implementation of the Overload pattern:
// https://www.cppstories.com/2019/02/2lines3featuresoverload.html/
template <class... Ts>
struct overload : Ts... {
using Ts::operator()...;
};
template <class... Ts>
overload(Ts...) -> overload<Ts...>;

/**
* @brief An asynchronous Napi::Worker that can be with any function that produces
Expand All @@ -51,10 +24,7 @@ struct CompressionResult {
class CompressionWorker : public Napi::AsyncWorker {
public:
CompressionWorker(const Napi::Env& env, std::function<CompressionResult()> worker)
: Napi::AsyncWorker{env, "Worker"},
m_deferred{env},
worker(worker),
result(CompressionResult::Empty()) {}
: Napi::AsyncWorker{env, "Worker"}, m_deferred{env}, worker(worker), result{} {}

Napi::Promise GetPromise() {
return m_deferred.Promise();
Expand All @@ -66,24 +36,27 @@ class CompressionWorker : public Napi::AsyncWorker {
}

void OnOK() {
if (!result.initialized) {
m_deferred.Reject(Napi::Error::New(Env(),
"zstd runtime error - async worker finished without "
"a compression or decompression result.")
.Value());
} else if (result.hasError) {
m_deferred.Reject(Napi::Error::New(Env(), result.error).Value());
} else if (result.hasResult) {
Buffer<uint8_t> output =
Buffer<uint8_t>::Copy(m_deferred.Env(), result.result.data(), result.result.size());

m_deferred.Resolve(output);
} else {
if (!result.has_value()) {
m_deferred.Reject(Napi::Error::New(Env(),
"zstd runtime error - async worker finished without "
"a compression or decompression result.")
.Value());
return;
}

auto result_visitor = overload{
[m_deferred = this->m_deferred](std::string error_message) {
auto error = Napi::Error::New(m_deferred.Env(), error_message);
m_deferred.Reject(error.Value());
},
[m_deferred = this->m_deferred](std::vector<uint8_t> result) {
Buffer<uint8_t> output =
Buffer<uint8_t>::Copy(m_deferred.Env(), result.data(), result.size());

m_deferred.Resolve(output);
},
};
std::visit(visitor, *result_visitor);
}

void OnError(const Napi::Error& err) {
Expand All @@ -93,7 +66,7 @@ class CompressionWorker : public Napi::AsyncWorker {
private:
Napi::Promise::Deferred m_deferred;
std::function<CompressionResult()> worker;
CompressionResult result;
std::optional<CompressionResult> result;
};

#endif
5 changes: 2 additions & 3 deletions addon/decompress.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ CompressionResult decompress(const std::vector<uint8_t>& compressed) {
ZSTD_outBuffer output = {chunk.data(), chunk.size(), 0};
size_t const ret = ZSTD_decompressStream(decompression_context.get(), &output, &input);
if (ZSTD_isError(ret)) {
std::string error(ZSTD_getErrorName(ret));
return CompressionResult::Error(error);
return std::string(ZSTD_getErrorName(ret));
}

for (size_t i = 0; i < output.pos; ++i) {
decompressed.push_back(chunk[i]);
}
}

return CompressionResult::Ok(decompressed);
return decompressed;
}
3 changes: 2 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
'xcode_settings': {
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
'CLANG_CXX_LIBRARY': 'libc++',
'MACOSX_DEPLOYMENT_TARGET': '10.12',
'MACOSX_DEPLOYMENT_TARGET': '11',
'GCC_SYMBOLS_PRIVATE_EXTERN': 'YES', # -fvisibility=hidden
},
'cflags!': [ '-fno-exceptions' ],
'cflags_cc!': [ '-fno-exceptions' ],
'cflags_cc': ['-std=c++17'],
'msvs_settings': {
'VCCLCompilerTool': { 'ExceptionHandling': 1 },
},
Expand Down

0 comments on commit 7b1e0e6

Please sign in to comment.