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 #31

Closed
wants to merge 13 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"
}
}
]
}
19 changes: 19 additions & 0 deletions .github/docker/Dockerfile.glibc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ARG UBUNTU_VERSION=bionic
FROM ubuntu:${UBUNTU_VERSION} AS build

ARG NODE_VERSION=16.20.1
# Possible values: s390x, arm64, x64
ARG NODE_ARCH
ADD https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-${NODE_ARCH}.tar.gz /
RUN mkdir -p /nodejs && tar -xzf /node-v${NODE_VERSION}-linux-${NODE_ARCH}.tar.gz --strip-components=1 -C /nodejs
ENV PATH=$PATH:/nodejs/bin

WORKDIR /tmp
COPY . .

RUN apt-get -qq update && apt-get -qq install -y python3 build-essential && ldd --version

RUN bash etc/install-zstd.sh && npm install && npm i @mongodb-js/zstd

ARG RUN_TEST
RUN [ -n "$RUN_TEST" ] && npm run test || echo 'skipping testing!'
39 changes: 39 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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
71 changes: 71 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch: {}

name: Test

jobs:
host_tests:
strategy:
matrix:
os: [macos-latest, windows-2019]
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: Build with Node.js ${{ matrix.node }} on ${{ matrix.os }}
run: bash etc/install-zstd.sh && npm install && npm i @mongodb-js/zstd
shell: bash

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

container_tests_glibc:
runs-on: ubuntu-latest
strategy:
matrix:
linux_arch: [s390x, arm64, amd64]
node: [16.20.1, 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;
};
Loading
Loading