-
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 3290ded
Showing
14 changed files
with
2,156 additions
and
88 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
name: Lint | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
name: ${{ matrix.lint-target }} | ||
strategy: | ||
matrix: | ||
lint-target: ["c++", "typescript"] | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Use Node.js LTS | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: 'lts/*' | ||
cache: 'npm' | ||
|
||
- name: Install dependencies | ||
shell: bash | ||
run: npm i --ignore-scripts | ||
|
||
- if: matrix.lint-target == 'c++' | ||
shell: bash | ||
run: | | ||
npm run check:clang-format | ||
- if: matrix.lint-target == 'typescript' | ||
shell: bash | ||
run: | | ||
npm run check:eslint |
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,25 @@ | ||
|
||
#include <napi.h> | ||
|
||
#include <vector> | ||
|
||
#include "zstd.h" | ||
|
||
using namespace Napi; | ||
|
||
CompressionResult compress(const std::vector<uint8_t> data, size_t compression_level) { | ||
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); | ||
} |
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,94 @@ | ||
#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 function that produces CompressionResults. | ||
* */ | ||
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::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; | ||
std::function<CompressionResult()> 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,34 @@ | ||
#include <napi.h> | ||
|
||
#include <vector> | ||
|
||
#include "zstd.h" | ||
|
||
using namespace Napi; | ||
|
||
CompressionResult decompress(const std::vector<uint8_t>& compressed) { | ||
std::vector<uint8_t> decompressed; | ||
|
||
using DCTX_Deleter = void (*)(ZSTD_DCtx*); | ||
|
||
std::unique_ptr<ZSTD_DCtx, DCTX_Deleter> decompression_context(ZSTD_createDCtx(), | ||
(DCTX_Deleter)ZSTD_freeDCtx); | ||
|
||
ZSTD_inBuffer input = {compressed.data(), compressed.size(), 0}; | ||
|
||
while (input.pos < input.size) { | ||
std::vector<uint8_t> 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); | ||
} |
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,30 @@ | ||
#include <napi.h> | ||
|
||
using namespace Napi; | ||
|
||
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; | ||
} | ||
|
||
/** | ||
* @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 |
Oops, something went wrong.