Skip to content

Commit 8c40b08

Browse files
feat(NODE-6539): add base napi C++ template with standard Node team tooling (#28)
1 parent e5f4fe5 commit 8c40b08

14 files changed

+1549
-4072
lines changed

.clang-format

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
BasedOnStyle: Google
2+
AllowShortFunctionsOnASingleLine: Empty
3+
AllowShortIfStatementsOnASingleLine: false
4+
AllowShortLoopsOnASingleLine: false
5+
BinPackArguments: false
6+
BinPackParameters: false
7+
ColumnLimit: 100
8+
Cpp11BracedListStyle: true
9+
DerivePointerAlignment: false
10+
IndentWidth: 4
11+
MaxEmptyLinesToKeep: 1
12+
NamespaceIndentation: None
13+
SpaceBeforeAssignmentOperators: true
14+
Standard: Cpp11
15+
UseTab: Never
16+
InsertNewlineAtEOF: true

.eslintignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
!.eslintrc.json
2+
!.mocharc.json
3+
!.prettierrc.json

.eslintrc.json

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
{
2+
"root": true,
3+
"extends": [
4+
"eslint:recommended",
5+
"plugin:prettier/recommended",
6+
"plugin:@typescript-eslint/eslint-recommended",
7+
"plugin:@typescript-eslint/recommended"
8+
],
9+
"env": {
10+
"node": true,
11+
"mocha": true,
12+
"es6": true
13+
},
14+
"parserOptions": {
15+
"ecmaVersion": 2019
16+
},
17+
"plugins": ["@typescript-eslint", "prettier"],
18+
"rules": {
19+
"no-restricted-properties": [
20+
"error",
21+
{
22+
"object": "describe",
23+
"property": "only"
24+
},
25+
{
26+
"object": "it",
27+
"property": "only"
28+
},
29+
{
30+
"object": "context",
31+
"property": "only"
32+
}
33+
],
34+
"prettier/prettier": "error",
35+
"no-console": "error",
36+
"valid-typeof": "error",
37+
"eqeqeq": [
38+
"error",
39+
"always",
40+
{
41+
"null": "ignore"
42+
}
43+
],
44+
"strict": ["error", "global"],
45+
"no-restricted-syntax": [
46+
"error",
47+
{
48+
"selector": "TSEnumDeclaration",
49+
"message": "Do not declare enums"
50+
},
51+
{
52+
"selector": "BinaryExpression[operator=/[=!]==/] Identifier[name='undefined']",
53+
"message": "Do not strictly check undefined"
54+
},
55+
{
56+
"selector": "BinaryExpression[operator=/[=!]==/] Literal[raw='null']",
57+
"message": "Do not strictly check null"
58+
},
59+
{
60+
"selector": "BinaryExpression[operator=/[=!]==?/] Literal[value='undefined']",
61+
"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)"
62+
}
63+
],
64+
"@typescript-eslint/no-require-imports": "off"
65+
},
66+
"overrides": [
67+
{
68+
"files": ["test/**/*ts"],
69+
"rules": {
70+
// chat `expect(..)` style chaining is considered
71+
// an unused expression
72+
"@typescript-eslint/no-unused-expressions": "off"
73+
}
74+
},
75+
{
76+
// json configuration files
77+
"files": [".*.json"],
78+
"rules": {
79+
"@typescript-eslint/no-unused-expressions": "off"
80+
}
81+
}
82+
]
83+
}

.github/workflows/test.yml

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
on:
2+
push:
3+
branches: [main]
4+
pull_request:
5+
branches: [main]
6+
workflow_dispatch: {}
7+
8+
name: Test
9+
10+
jobs:
11+
host_tests:
12+
strategy:
13+
matrix:
14+
# os: [macos-latest, windows-2019]
15+
os: ['ubuntu-latest']
16+
node: [16.20.1, 18.x, 20.x, 22.x]
17+
runs-on: ${{ matrix.os }}
18+
steps:
19+
- uses: actions/checkout@v4
20+
21+
- uses: actions/setup-node@v4
22+
with:
23+
node-version: ${{ matrix.node }}
24+
cache: 'npm'
25+
registry-url: 'https://registry.npmjs.org'
26+
27+
- name: Build with Node.js ${{ matrix.node }} on ${{ matrix.os }}
28+
run: npm install && npm run compile
29+
shell: bash
30+
31+
- name: Test ${{ matrix.os }}
32+
shell: bash
33+
run: npm test
34+
35+
# container_tests:
36+
# runs-on: ubuntu-latest
37+
# strategy:
38+
# matrix:
39+
# linux_arch: [s390x, arm64, amd64]
40+
# node: [16.x, 18.x, 20.x, 22.x]
41+
# steps:
42+
# - uses: actions/checkout@v4
43+
44+
# - uses: actions/setup-node@v4
45+
# with:
46+
# node-version: ${{ matrix.node }}
47+
48+
# - name: Get Full Node.js Version
49+
# id: get_nodejs_version
50+
# shell: bash
51+
# run: |
52+
# echo "version=$(node --print 'process.version.slice(1)')" >> "$GITHUB_OUTPUT"
53+
# echo "ubuntu_version=$(node --print '(+process.version.slice(1).split(`.`).at(0)) > 16 ? `noble` : `bionic`')" >> "$GITHUB_OUTPUT"
54+
55+
# - name: Set up QEMU
56+
# uses: docker/setup-qemu-action@v3
57+
58+
# - name: Set up Docker Buildx
59+
# uses: docker/setup-buildx-action@v3
60+
61+
# - name: Run Buildx
62+
# run: |
63+
# docker buildx create --name builder --bootstrap --use
64+
# docker buildx build \
65+
# --platform linux/${{ matrix.linux_arch }} \
66+
# --build-arg="NODE_ARCH=${{ matrix.linux_arch == 'amd64' && 'x64' || matrix.linux_arch }}" \
67+
# --build-arg="NODE_VERSION=${{ steps.get_nodejs_version.outputs.version }}" \
68+
# --build-arg="UBUNTU_VERSION=${{ steps.get_nodejs_version.outputs.ubuntu_version }}" \
69+
# --build-arg="RUN_TEST=true" \
70+
# --output type=local,dest=./prebuilds,platform-split=false \
71+
# -f ./.github/docker/Dockerfile.glibc \
72+
# .

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ lib-cov
1010
.DS_Store
1111

1212
.vscode
13-
13+
xunit.xml
1414
pids
1515
logs
1616
results

.mocharc.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/mocharc.json",
3+
"extension": ["ts"],
4+
"recursive": true,
5+
"failZero": true,
6+
"color": true
7+
}

.prettierrc.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"singleQuote": true,
3+
"tabWidth": 2,
4+
"printWidth": 100,
5+
"arrowParens": "avoid",
6+
"trailingComma": "none"
7+
}

addon/zstd.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <napi.h>
2+
3+
using namespace Napi;
4+
5+
Napi::String Compress(const Napi::CallbackInfo& info) {
6+
auto string = Napi::String::New(info.Env(), "compress()");
7+
return string;
8+
}
9+
Napi::String Decompress(const Napi::CallbackInfo& info) {
10+
auto string = Napi::String::New(info.Env(), "decompress()");
11+
return string;
12+
}
13+
14+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
15+
exports.Set(Napi::String::New(env, "compress"), Napi::Function::New(env, Compress));
16+
exports.Set(Napi::String::New(env, "decompress"), Napi::Function::New(env, Decompress));
17+
return exports;
18+
}
19+
20+
NODE_API_MODULE(zstd, Init)

binding.gyp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
'targets': [{
3+
'target_name': 'zstd',
4+
'type': 'loadable_module',
5+
'defines': ['ZSTD_STATIC_LINKING_ONLY'],
6+
'include_dirs': [
7+
"<!(node -p \"require('node-addon-api').include_dir\")"
8+
],
9+
'variables': {
10+
'ARCH': '<(host_arch)',
11+
'built_with_electron%': 0
12+
},
13+
'sources': [
14+
'addon/zstd.cpp'
15+
],
16+
'xcode_settings': {
17+
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
18+
'CLANG_CXX_LIBRARY': 'libc++',
19+
'MACOSX_DEPLOYMENT_TARGET': '10.12',
20+
'GCC_SYMBOLS_PRIVATE_EXTERN': 'YES', # -fvisibility=hidden
21+
},
22+
'cflags!': [ '-fno-exceptions' ],
23+
'cflags_cc!': [ '-fno-exceptions' ],
24+
'msvs_settings': {
25+
'VCCLCompilerTool': { 'ExceptionHandling': 1 },
26+
}
27+
}]
28+
}

index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export declare function compress(data: Buffer, level?: number | undefined | null): Promise<Buffer>
2-
export declare function decompress(data: Buffer): Promise<Buffer>
1+
export declare function compress(data: Buffer, level?: number | undefined | null): Promise<Buffer>;
2+
export declare function decompress(data: Buffer): Promise<Buffer>;

index.js lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { compress: _compress, decompress: _decompress } = require('./bindings');
1+
const { compress: _compress, decompress: _decompress } = require('bindings')('zstd');
22

33
// Error objects created via napi don't have JS stacks; wrap them so .stack is present
44
// https://github.com/nodejs/node/issues/25318#issuecomment-451068073

0 commit comments

Comments
 (0)