Skip to content

Commit

Permalink
works
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Nov 14, 2024
1 parent 9a6bdf6 commit b624f18
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 120 deletions.
14 changes: 5 additions & 9 deletions addon/compression_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename TWorker>
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<CompressionResult()> worker)
: Napi::AsyncWorker{env, "Worker"},
m_deferred{env},
worker(worker),
Expand Down Expand Up @@ -93,6 +89,6 @@ class Worker : public Napi::AsyncWorker {

private:
Napi::Promise::Deferred m_deferred;
TWorker worker;
std::function<CompressionResult()> worker;
CompressionResult result;
};
44 changes: 0 additions & 44 deletions addon/compressor.h

This file was deleted.

50 changes: 0 additions & 50 deletions addon/decompressor.h

This file was deleted.

17 changes: 8 additions & 9 deletions addon/napi_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

using namespace Napi;

/**
* @brief Given a T* source and a T* destination, copies count
* elements from source into destination.
*/
template <typename T>
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<uint8_t> getBytesFromUint8Array(const Uint8Array& source) {
const uint8_t* input_data = source.Data();
size_t total = source.ElementLength();
std::vector<uint8_t> data(total);

std::copy(input_data, input_data + total, data.data());

return data;
}

/**
Expand Down
16 changes: 10 additions & 6 deletions addon/zstd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include <vector>

#include "compression_worker.h"
#include "compressor.h"
#include "decompressor.h"
#include "compress.h"
#include "decompress.h"
#include "napi_utils.h"

using namespace Napi;
Expand All @@ -20,10 +20,13 @@ Napi::Promise Compress(const Napi::CallbackInfo& info) {
}

Uint8Array to_compress = Uint8ArrayFromValue(info[0], "buffer");
std::vector<uint8_t> data = getBytesFromUint8Array(to_compress);

size_t compression_level = (size_t)info[1].ToNumber().Int32Value();

Compressor compressor = Compressor::fromUint8Array(to_compress, compression_level);
Worker<Compressor>* worker = new Worker<Compressor>(info.Env(), std::move(compressor));
CompressionWorker* worker = new CompressionWorker(
info.Env(),
[data = std::move(data), compression_level] { return compress(data, compression_level); });

worker->Queue();

Expand All @@ -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<Decompressor>* worker = new Worker<Decompressor>(info.Env(), decompressor);
std::vector<uint8_t> data = getBytesFromUint8Array(compressed_data);
CompressionWorker* worker =
new CompressionWorker(info.Env(), [data = std::move(data)] { return decompress(data); });

worker->Queue();

Expand Down
4 changes: 2 additions & 2 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand Down

0 comments on commit b624f18

Please sign in to comment.