Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scaffolding #32

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
BasedOnStyle: Google
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
BinPackArguments: false
BinPackParameters: false
ColumnLimit: 100
Cpp11BracedListStyle: true
DerivePointerAlignment: false
IndentWidth: 4
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
SpaceBeforeAssignmentOperators: true
Standard: Cpp11
UseTab: Never
InsertNewlineAtEOF: true
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
!.eslintrc.json
!.mocharc.json
!.prettierrc.json
83 changes: 83 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"root": true,
"extends": [
"eslint:recommended",
"plugin:prettier/recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"env": {
"node": true,
"mocha": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 2019
},
"plugins": ["@typescript-eslint", "prettier"],
"rules": {
"no-restricted-properties": [
"error",
{
"object": "describe",
"property": "only"
},
{
"object": "it",
"property": "only"
},
{
"object": "context",
"property": "only"
}
],
"prettier/prettier": "error",
"no-console": "error",
"valid-typeof": "error",
"eqeqeq": [
"error",
"always",
{
"null": "ignore"
}
],
"strict": ["error", "global"],
"no-restricted-syntax": [
"error",
{
"selector": "TSEnumDeclaration",
"message": "Do not declare enums"
},
{
"selector": "BinaryExpression[operator=/[=!]==/] Identifier[name='undefined']",
"message": "Do not strictly check undefined"
},
{
"selector": "BinaryExpression[operator=/[=!]==/] Literal[raw='null']",
"message": "Do not strictly check null"
},
{
"selector": "BinaryExpression[operator=/[=!]==?/] Literal[value='undefined']",
"message": "Do not strictly check typeof undefined (NOTE: currently this rule only detects the usage of 'undefined' string literal so this could be a misfire)"
}
],
"@typescript-eslint/no-require-imports": "off"
},
"overrides": [
{
"files": ["test/**/*ts"],
"rules": {
// chat `expect(..)` style chaining is considered
// an unused expression
"@typescript-eslint/no-unused-expressions": "off"
}
},
{
// json configuration files
"files": [".*.json"],
"rules": {
"@typescript-eslint/no-unused-expressions": "off"
}
}
]
}
80 changes: 80 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch: {}

name: Test

jobs:
host_tests:
strategy:
matrix:
# os: [macos-latest, windows-2019]
os: ['ubuntu-latest']
node: [16.20.1, 18.x, 20.x, 22.x]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: 'npm'
registry-url: 'https://registry.npmjs.org'

- name: Install zstd
run: npm run install-zstd
shell: bash

- name: install dependencies
run: npm install --loglevel verbose --ignore-scripts
shell: bash

- name: Compile addon
run: npm run compile
shell: bash

- name: Test ${{ matrix.os }}
shell: bash
run: npm test

# container_tests:
# runs-on: ubuntu-latest
# strategy:
# matrix:
# linux_arch: [s390x, arm64, amd64]
# node: [16.x, 18.x, 20.x, 22.x]
# steps:
# - uses: actions/checkout@v4

# - uses: actions/setup-node@v4
# with:
# node-version: ${{ matrix.node }}

# - name: Get Full Node.js Version
# id: get_nodejs_version
# shell: bash
# run: |
# echo "version=$(node --print 'process.version.slice(1)')" >> "$GITHUB_OUTPUT"
# echo "ubuntu_version=$(node --print '(+process.version.slice(1).split(`.`).at(0)) > 16 ? `noble` : `bionic`')" >> "$GITHUB_OUTPUT"

# - name: Set up QEMU
# uses: docker/setup-qemu-action@v3

# - name: Set up Docker Buildx
# uses: docker/setup-buildx-action@v3

# - name: Run Buildx
# run: |
# docker buildx create --name builder --bootstrap --use
# docker buildx build \
# --platform linux/${{ matrix.linux_arch }} \
# --build-arg="NODE_ARCH=${{ matrix.linux_arch == 'amd64' && 'x64' || matrix.linux_arch }}" \
# --build-arg="NODE_VERSION=${{ steps.get_nodejs_version.outputs.version }}" \
# --build-arg="UBUNTU_VERSION=${{ steps.get_nodejs_version.outputs.ubuntu_version }}" \
# --build-arg="RUN_TEST=true" \
# --output type=local,dest=./prebuilds,platform-split=false \
# -f ./.github/docker/Dockerfile.glibc \
# .
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ lib-cov
.DS_Store

.vscode

xunit.xml
pids
logs
results
node_modules
build

npm-debug.log
deps
7 changes: 7 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/mocharc.json",
"extension": ["ts"],
"recursive": true,
"failZero": true,
"color": true
}
7 changes: 7 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"singleQuote": true,
"tabWidth": 2,
"printWidth": 100,
"arrowParens": "avoid",
"trailingComma": "none"
}
98 changes: 98 additions & 0 deletions addon/compression_worker.h
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;
};
44 changes: 44 additions & 0 deletions addon/compressor.h
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);
}
};
Loading
Loading