From b624f1860f30e98d494d96b67fcf8ffe3dd2d42a Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Thu, 14 Nov 2024 11:35:30 -0700 Subject: [PATCH] works --- addon/compression_worker.h | 14 ++++------- addon/compressor.h | 44 --------------------------------- addon/decompressor.h | 50 -------------------------------------- addon/napi_utils.h | 17 ++++++------- addon/zstd.cpp | 16 +++++++----- binding.gyp | 4 +-- 6 files changed, 25 insertions(+), 120 deletions(-) delete mode 100644 addon/compressor.h delete mode 100644 addon/decompressor.h diff --git a/addon/compression_worker.h b/addon/compression_worker.h index 07bee02..38ef355 100644 --- a/addon/compression_worker.h +++ b/addon/compression_worker.h @@ -43,15 +43,11 @@ struct CompressionResult { }; /** - * @brief An asynchronous Napi::Worker that can be with any functor that produces - * CompressionResults. - * - * @tparam TWorker - The functor to call asynchronously. - */ -template -class Worker : public Napi::AsyncWorker { + * @brief An asynchronous Napi::Worker that can be with any function that produces CompressionResults. + * */ +class CompressionWorker : public Napi::AsyncWorker { public: - Worker(const Napi::Env& env, TWorker worker) + CompressionWorker(const Napi::Env& env, std::function worker) : Napi::AsyncWorker{env, "Worker"}, m_deferred{env}, worker(worker), @@ -93,6 +89,6 @@ class Worker : public Napi::AsyncWorker { private: Napi::Promise::Deferred m_deferred; - TWorker worker; + std::function worker; CompressionResult result; }; diff --git a/addon/compressor.h b/addon/compressor.h deleted file mode 100644 index 52467e2..0000000 --- a/addon/compressor.h +++ /dev/null @@ -1,44 +0,0 @@ - -#include - -#include - -#include "zstd.h" - -using namespace Napi; - -struct Compressor { - std::vector data; - size_t compression_level; - - Compressor(std::vector data, size_t compression_level) - : data(data), compression_level(compression_level) {} - - CompressionResult operator()() { - size_t output_buffer_size = ZSTD_compressBound(data.size()); - std::vector output(output_buffer_size); - - size_t result_code = 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); - } - - output.resize(result_code); - - return CompressionResult::Ok(output); - } - - static Compressor fromUint8Array(const Uint8Array& to_compress, size_t compression_level) { - const uint8_t* input_data = to_compress.Data(); - size_t total = to_compress.ElementLength(); - - std::vector data(to_compress.ElementLength()); - - std::copy(input_data, input_data + total, data.data()); - - return Compressor(std::move(data), compression_level); - } -}; diff --git a/addon/decompressor.h b/addon/decompressor.h deleted file mode 100644 index 1e365fa..0000000 --- a/addon/decompressor.h +++ /dev/null @@ -1,50 +0,0 @@ -#include - -#include - -#include "zstd.h" - -using namespace Napi; - -using DCTX_Deleter = void (*)(ZSTD_DCtx*); - -struct Decompressor { - std::vector data; - - Decompressor(std::vector data) : data(data) {} - - CompressionResult operator()() { - std::vector decompressed; - - std::unique_ptr decompression_context(ZSTD_createDCtx(), - (DCTX_Deleter)ZSTD_freeDCtx); - - ZSTD_inBuffer input = {data.data(), data.size(), 0}; - - while (input.pos < input.size) { - std::vector chunk(ZSTD_DStreamOutSize()); - 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); - } - - for (size_t i = 0; i < output.pos; ++i) { - decompressed.push_back(chunk[i]); - } - } - - return CompressionResult::Ok(decompressed); - } - - static Decompressor fromUint8Array(const Uint8Array& compressed_data) { - const uint8_t* input_data = compressed_data.Data(); - size_t total = compressed_data.ElementLength(); - - std::vector data(total); - std::copy(input_data, input_data + total, data.data()); - - return Decompressor(data); - } -}; diff --git a/addon/napi_utils.h b/addon/napi_utils.h index ff60435..b949ca4 100644 --- a/addon/napi_utils.h +++ b/addon/napi_utils.h @@ -2,15 +2,14 @@ using namespace Napi; -/** - * @brief Given a T* source and a T* destination, copies count - * elements from source into destination. - */ -template -void copy_buffer_data(T* source, T* dest, size_t count) { - for (size_t i = 0; i < count; ++i) { - dest[i] = source[i]; - } +std::vector getBytesFromUint8Array(const Uint8Array& source) { + const uint8_t* input_data = source.Data(); + size_t total = source.ElementLength(); + std::vector data(total); + + std::copy(input_data, input_data + total, data.data()); + + return data; } /** diff --git a/addon/zstd.cpp b/addon/zstd.cpp index 43251c1..b079008 100644 --- a/addon/zstd.cpp +++ b/addon/zstd.cpp @@ -6,8 +6,8 @@ #include #include "compression_worker.h" -#include "compressor.h" -#include "decompressor.h" +#include "compress.h" +#include "decompress.h" #include "napi_utils.h" using namespace Napi; @@ -20,10 +20,13 @@ Napi::Promise Compress(const Napi::CallbackInfo& info) { } Uint8Array to_compress = Uint8ArrayFromValue(info[0], "buffer"); + std::vector data = getBytesFromUint8Array(to_compress); + size_t compression_level = (size_t)info[1].ToNumber().Int32Value(); - Compressor compressor = Compressor::fromUint8Array(to_compress, compression_level); - Worker* worker = new Worker(info.Env(), std::move(compressor)); + CompressionWorker* worker = new CompressionWorker( + info.Env(), + [data = std::move(data), compression_level] { return compress(data, compression_level); }); worker->Queue(); @@ -38,8 +41,9 @@ Napi::Promise Decompress(const CallbackInfo& info) { } Napi::Uint8Array compressed_data = Uint8ArrayFromValue(info[0], "buffer"); - Decompressor decompressor = Decompressor::fromUint8Array(compressed_data); - Worker* worker = new Worker(info.Env(), decompressor); + std::vector data = getBytesFromUint8Array(compressed_data); + CompressionWorker* worker = + new CompressionWorker(info.Env(), [data = std::move(data)] { return decompress(data); }); worker->Queue(); diff --git a/binding.gyp b/binding.gyp index e381a02..abe9483 100644 --- a/binding.gyp +++ b/binding.gyp @@ -14,8 +14,8 @@ 'sources': [ 'addon/zstd.cpp', 'addon/compression_worker.h', - 'addon/compressor.h', - 'addon/decompressor.h', + 'addon/compress.h', + 'addon/decompress.h', 'addon/napi_utils.h', ], 'xcode_settings': {