-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c40b08
commit 5bac6ab
Showing
12 changed files
with
380 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,4 @@ node_modules | |
build | ||
|
||
npm-debug.log | ||
deps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#include <napi.h> | ||
|
||
#include <optional> | ||
|
||
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); | ||
} | ||
|
||
std::string error; | ||
std::vector<uint8_t> result; | ||
|
||
bool hasError; | ||
bool hasResult; | ||
bool initialized; | ||
}; | ||
|
||
/** | ||
* @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 { | ||
public: | ||
Worker(const Napi::Env& env, TWorker worker) | ||
: Napi::AsyncWorker{env, "Worker"}, | ||
m_deferred{env}, | ||
worker(worker), | ||
result(CompressionResult::Empty()) {} | ||
|
||
Napi::Promise GetPromise() { | ||
return m_deferred.Promise(); | ||
} | ||
|
||
protected: | ||
void Execute() { | ||
result = worker(); | ||
} | ||
|
||
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 { | ||
m_deferred.Reject(Napi::Error::New(Env(), | ||
"zstd runtime error - async worker finished without " | ||
"a compression or decompression result.") | ||
.Value()); | ||
} | ||
} | ||
|
||
void OnError(const Napi::Error& err) { | ||
m_deferred.Reject(err.Value()); | ||
} | ||
|
||
private: | ||
Napi::Promise::Deferred m_deferred; | ||
TWorker worker; | ||
CompressionResult result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
#include <napi.h> | ||
|
||
#include <vector> | ||
|
||
#include "zstd.h" | ||
|
||
using namespace Napi; | ||
|
||
struct Compressor { | ||
std::vector<uint8_t> data; | ||
size_t compression_level; | ||
|
||
Compressor(std::vector<uint8_t> data, size_t compression_level) | ||
: data(data), compression_level(compression_level) {} | ||
|
||
CompressionResult operator()() { | ||
size_t output_buffer_size = ZSTD_compressBound(data.size()); | ||
std::vector<uint8_t> 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<uint8_t> data(to_compress.ElementLength()); | ||
|
||
std::copy(input_data, input_data + total, data.data()); | ||
|
||
return Compressor(std::move(data), compression_level); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <napi.h> | ||
|
||
#include <vector> | ||
|
||
#include "zstd.h" | ||
|
||
using namespace Napi; | ||
|
||
struct Decompressor { | ||
std::vector<uint8_t> data; | ||
size_t buffer_size; | ||
|
||
Decompressor(std::vector<uint8_t> data, size_t buffer_size) | ||
: data(data), buffer_size(buffer_size) {} | ||
|
||
CompressionResult operator()() { | ||
std::vector<uint8_t> decompressed(buffer_size); | ||
|
||
size_t _result = | ||
ZSTD_decompress(decompressed.data(), decompressed.size(), data.data(), data.size()); | ||
|
||
if (ZSTD_isError(_result)) { | ||
std::string error(ZSTD_getErrorName(_result)); | ||
return CompressionResult::Error(error); | ||
} | ||
|
||
decompressed.resize(_result); | ||
|
||
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<uint8_t> data(total); | ||
std::copy(input_data, input_data + total, data.data()); | ||
|
||
return Decompressor(data, total * 1000); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <napi.h> | ||
|
||
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]; | ||
} | ||
} | ||
|
||
/** | ||
* @brief Given an Napi;:Value, this function returns the value as a Uint8Array, if the | ||
* Value is a Uint8Array. Otherwise, this function throws. | ||
* | ||
* @param v - An Napi::Value | ||
* @param argument_name - the name of the value, to use when constructing an error message. | ||
* @return Napi::Uint8Array | ||
*/ | ||
Uint8Array Uint8ArrayFromValue(Value v, std::string argument_name) { | ||
if (!v.IsTypedArray() || v.As<TypedArray>().TypedArrayType() != napi_uint8_array) { | ||
std::string error_message = "Parameter `" + argument_name + "` must be a Uint8Array."; | ||
throw TypeError::New(v.Env(), error_message); | ||
} | ||
|
||
return v.As<Uint8Array>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
set -o xtrace | ||
|
||
clean_deps() { | ||
rm -rf deps | ||
} | ||
|
||
download_zstd() { | ||
rm -rf deps | ||
mkdir -p deps/zstd | ||
|
||
curl -L "https://github.com/facebook/zstd/releases/download/v1.5.6/zstd-1.5.6.tar.gz" \ | ||
| tar -zxf - -C deps/zstd --strip-components 1 | ||
} | ||
|
||
build_zstd() { | ||
export MACOSX_DEPLOYMENT_TARGET=10.12 | ||
cd deps/zstd/build/cmake | ||
|
||
cmake . | ||
make | ||
} | ||
|
||
clean_deps | ||
download_zstd | ||
build_zstd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.