Skip to content
Merged
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
80 changes: 78 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,51 @@ jobs:
name: bindings-${{ env.PLATFORM_NAME }}
path: packages/*/*.${{ env.PLATFORM_NAME }}.node

build_musl:
if: "!contains(github.event.head_commit.message, 'skip ci')"
name: stable - linux-musl - node@12
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Login to registry
run: |
docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD $DOCKER_REGISTRY_URL
env:
DOCKER_REGISTRY_URL: docker.pkg.github.com
DOCKER_USERNAME: ${{ github.actor }}
DOCKER_PASSWORD: ${{ secrets.GITHUB_TOKEN }}

- name: Pull docker image
run: |
docker pull docker.pkg.github.com/napi-rs/napi-rs/rust-nodejs-alpine:lts
docker tag docker.pkg.github.com/napi-rs/napi-rs/rust-nodejs-alpine:lts builder

- name: 'Install dependencies'
run: yarn install --frozen-lockfile --registry https://registry.npmjs.org

- name: 'Build'
run: |
docker run --rm -v $(pwd)/.cargo:/root/.cargo -v $(pwd):/node-rs -w /node-rs builder sh -c "cargo build --release"
docker run --rm -v $(pwd)/.cargo:/root/.cargo -v $(pwd):/node-rs -w /node-rs builder sh -c "./node_modules/.bin/lerna run build --stream -- --musl"

- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: bindings-musl
path: packages/*/*.musl.node

test_binding:
name: Test bindings on ${{ matrix.os }} - node@${{ matrix.node }}
needs: lint_and_build
needs:
- lint_and_build
- build_musl
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
node: ['10', '12', '13', '14']
node: ['10', '12', '14']
runs-on: ${{ matrix.os }}

steps:
Expand Down Expand Up @@ -158,6 +195,45 @@ jobs:
- name: Run benchmark
run: yarn bench

test_musl_binding:
name: Test bindings on alpine - node@${{ matrix.node }}
needs:
- lint_and_build
- build_musl
strategy:
fail-fast: false
matrix:
node: ['10', '12', '14']
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Download artifacts
uses: actions/download-artifact@v2
with:
name: bindings-musl
path: artifacts

- name: Move artifacts
run: node scripts/mv-artifacts.js
shell: bash
env:
MOVE_TARGET: musl

- name: List packages
run: ls -R packages
shell: bash

- name: 'Install dependencies'
run: yarn install --frozen-lockfile --ignore-scripts --registry https://registry.npmjs.org

- name: Build TypeScript
run: yarn build:ts

- name: Run simple tests
run: docker run --rm -v $(pwd)/.cargo:/root/.cargo -v $(pwd):/node-rs -w /node-rs node:${{ matrix.node }}-alpine sh -c "./node_modules/.bin/lerna run test:simple --stream"

publish:
name: Publish
if: "startsWith(github.event.head_commit.message, 'chore(release): publish')"
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ members = [
"./packages/crc32",
"./packages/jieba"
]

[profile.release]
opt-level = 3
lto = true
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ Make rust crates binding to NodeJS use [napi-rs](https://github.com/Brooooooklyn

# Support matrix

| | node 10 | node12 | node13 | node14 |
| ----------------- | ------- | ------ | ------ | ------ |
| Windows 64 latest | ✅ | ✅ | ✅ | ✅ |
| macOS latest | ✅ | ✅ | ✅ | ✅ |
| Linux | ✅ | ✅ | ✅ | ✅ |
| | node 10 | node12 | node14 |
| ----------------- | ------- | ------ | ------ |
| Windows 64 latest | ✅ | ✅ | ✅ |
| macOS latest | ✅ | ✅ | ✅ |
| Linux | ✅ | ✅ | ✅ |

# Packages

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"husky": "^4.2.5",
"lerna": "^3.20.2",
"lint-staged": "^10.1.7",
"napi-rs": "^0.2.3",
"napi-rs": "^0.2.4",
"npm-run-all": "^4.1.5",
"nyc": "^15.0.1",
"prettier": "^2.0.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/bcrypt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ napi-rs-derive = { version = "0.2" }
rand = "0.7"
phf = { version = "0.8", features = ["macros"] }

[target.'cfg(unix)'.dependencies]
[target.'cfg(all(unix, not(target_env = "musl")))'.dependencies]
jemallocator = { version = "0.3", features = ["disable_initial_exec_tls"] }

[dev-dependencies]
Expand Down
10 changes: 5 additions & 5 deletions packages/bcrypt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

## Support matrix

| | node 10 | node12 | node13 | node14 |
| ----------------- | ------- | ------ | ------ | ------ |
| Windows 64 latest | ✓ | ✓ | ✓ | ✓ |
| macOS latest | ✓ | ✓ | ✓ | ✓ |
| Linux | ✓ | ✓ | ✓ | ✓ |
| | node 10 | node12 | node14 |
| ----------------- | ------- | ------ | ------ |
| Windows 64 latest | ✓ | ✓ | ✓ |
| macOS latest | ✓ | ✓ | ✓ |
| Linux | ✓ | ✓ | ✓ |

## Usage

Expand Down
4 changes: 2 additions & 2 deletions packages/bcrypt/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { locateBinding } = require('@node-rs/helper')
const { loadBinding } = require('@node-rs/helper')

const binding = require(locateBinding(__dirname, 'bcrypt'))
const binding = loadBinding(__dirname, 'bcrypt')

const DEFAULT_COST = 12

Expand Down
3 changes: 2 additions & 1 deletion packages/bcrypt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"scripts": {
"bench": "cross-env NODE_ENV=production node benchmark/bcrypt.js",
"build": "napi --release ./bcrypt",
"build:debug": "napi ./bcrypt.debug"
"build:debug": "napi ./bcrypt.debug",
"test:simple": "node ./simple-test.js"
},
"bugs": {
"url": "https://github.com/Brooooooklyn/node-rs/issues"
Expand Down
3 changes: 3 additions & 0 deletions packages/bcrypt/simple-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { verifySync } = require('./index')

console.assert(verifySync('hello', '$2b$12$k4V9p7YpSYHY05bORujyyeesfPaWNQfPkjVzETFGfozAN/fH0LtdG'))
2 changes: 1 addition & 1 deletion packages/bcrypt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use verify_task::VerifyTask;
mod hash_task;
mod verify_task;

#[cfg(unix)]
#[cfg(all(unix, not(target_env = "musl")))]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

Expand Down
2 changes: 1 addition & 1 deletion packages/crc32/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ napi-rs = { version = "0.3" }
napi-rs-derive = { version = "0.2" }
crc32fast = "1.2"

[target.'cfg(unix)'.dependencies]
[target.'cfg(all(unix, not(target_env = "musl")))'.dependencies]
jemallocator = { version = "0.3", features = ["disable_initial_exec_tls"] }

[build-dependencies]
Expand Down
10 changes: 5 additions & 5 deletions packages/crc32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ js_crc32 for inputs 16931844B, avg 2066B x 22.12 ops/sec ±5.20% (40 runs sample

## Support matrix

| | node 10 | node12 | node13 | node14 |
| ----------------- | ------- | ------ | ------ | ------ |
| Windows 64 latest | ✓ | ✓ | ✓ | ✓ |
| macOS latest | ✓ | ✓ | ✓ | ✓ |
| Linux | ✓ | ✓ | ✓ | ✓ |
| | node 10 | node12 | node14 |
| ----------------- | ------- | ------ | ------ |
| Windows 64 latest | ✓ | ✓ | ✓ |
| macOS latest | ✓ | ✓ | ✓ |
| Linux | ✓ | ✓ | ✓ |

## API

Expand Down
4 changes: 2 additions & 2 deletions packages/crc32/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { locateBinding } = require('@node-rs/helper')
const { loadBinding } = require('@node-rs/helper')

const binding = require(locateBinding(__dirname, 'crc32'))
const binding = loadBinding(__dirname, 'crc32')

module.exports = {
crc32: function crc32(input, crc) {
Expand Down
3 changes: 2 additions & 1 deletion packages/crc32/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"scripts": {
"bench": "cross-env NODE_ENV=production node benchmark/crc32.js",
"build": "napi --release ./crc32",
"build:debug": "napi ./index"
"build:debug": "napi ./index",
"test:simple": "node ./simple-test.js"
},
"bugs": {
"url": "https://github.com/Brooooooklyn/node-rs/issues"
Expand Down
3 changes: 3 additions & 0 deletions packages/crc32/simple-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { crc32 } = require('./index')

console.assert(crc32('hello') === '907060870')
2 changes: 1 addition & 1 deletion packages/crc32/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crc32fast::Hasher;
use napi::{Buffer, CallContext, Env, Number, Object, Result, Value};
use std::convert::TryInto;

#[cfg(unix)]
#[cfg(all(unix, not(target_env = "musl")))]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

Expand Down
2 changes: 1 addition & 1 deletion packages/helper/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { locateBinding } from './loader'
export { loadBinding } from './loader'
12 changes: 10 additions & 2 deletions packages/helper/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { join } from 'path'

const SupportedPlatforms = new Set<NodeJS.Platform>(['darwin', 'win32', 'linux'])

export function locateBinding(dirname: string, filename = 'index') {
export function loadBinding(dirname: string, filename = 'index') {
const platformName = platform()
if (!SupportedPlatforms.has(platformName)) {
throw new TypeError(
Expand All @@ -14,9 +14,17 @@ export function locateBinding(dirname: string, filename = 'index') {

const bindingFilePath = join(dirname, `${filename}.${platformName}.node`)

if (platformName === 'linux') {
try {
return require(bindingFilePath)
} catch {
return require(join(dirname, `${filename}.musl.node`))
}
}

if (!existsSync(bindingFilePath)) {
throw new TypeError(`Could not find binding file on path ${bindingFilePath}`)
}

return bindingFilePath
return require(bindingFilePath)
}
2 changes: 1 addition & 1 deletion packages/jieba/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ napi-rs = { version = "0.3" }
napi-rs-derive = { version = "0.2" }
once_cell = "1.3"

[target.'cfg(unix)'.dependencies]
[target.'cfg(all(unix, not(target_env = "musl")))'.dependencies]
jemallocator = { version = "0.3", features = ["disable_initial_exec_tls"] }

[build-dependencies]
Expand Down
10 changes: 5 additions & 5 deletions packages/jieba/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ Tag 246568 words bench suite: Fastest is @node-rs/jieba

## Support matrix

| | node 10 | node12 | node13 | node14 |
| ----------------- | ------- | ------ | ------ | ------ |
| Windows 64 latest | ✓ | ✓ | ✓ | ✓ |
| macOS latest | ✓ | ✓ | ✓ | ✓ |
| Linux | ✓ | ✓ | ✓ | ✓ |
| | node 10 | node12 | node14 |
| ----------------- | ------- | ------ | ------ |
| Windows 64 latest | ✓ | ✓ | ✓ |
| macOS latest | ✓ | ✓ | ✓ |
| Linux | ✓ | ✓ | ✓ |

## Usage

Expand Down
4 changes: 2 additions & 2 deletions packages/jieba/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { locateBinding } = require('@node-rs/helper')
const { loadBinding } = require('@node-rs/helper')

const native = require(locateBinding(__dirname, 'jieba'))
const native = loadBinding(__dirname, 'jieba')

module.exports = {
...native,
Expand Down
3 changes: 2 additions & 1 deletion packages/jieba/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"scripts": {
"bench": "cross-env NODE_ENV=production node benchmark/jieba.js",
"build": "napi --release ./jieba",
"build:debug": "napi ./jieba"
"build:debug": "napi ./jieba",
"test:simple": "node ./simple-test.js"
},
"bugs": {
"url": "https://github.com/Brooooooklyn/node-rs/issues"
Expand Down
6 changes: 6 additions & 0 deletions packages/jieba/simple-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { cut } = require('./index')

const [first, second] = cut('武汉市长江大桥')

console.assert(first === '武汉市')
console.assert(second === '长江大桥')
2 changes: 1 addition & 1 deletion packages/jieba/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use once_cell::sync::OnceCell;
use std::convert::TryInto;
use std::str;

#[cfg(unix)]
#[cfg(all(unix, not(target_env = "musl")))]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

Expand Down
2 changes: 1 addition & 1 deletion scripts/mv-artifacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const supporttedPlatforms = require('./platforms')

const MOVE_ALL = process.env.MOVE_TARGET === 'all'

const platforms = MOVE_ALL ? supporttedPlatforms : [platform()]
const platforms = MOVE_ALL ? supporttedPlatforms : [process.env.MOVE_TARGET || platform()]

/**
* @param {string[]} _platforms platforms
Expand Down
2 changes: 1 addition & 1 deletion scripts/platforms.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = ['linux', 'darwin', 'win32']
module.exports = ['linux', 'musl', 'darwin', 'win32']
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5215,10 +5215,10 @@ nanomatch@^1.2.9:
snapdragon "^0.8.1"
to-regex "^3.0.1"

napi-rs@^0.2.3:
version "0.2.3"
resolved "https://registry.yarnpkg.com/napi-rs/-/napi-rs-0.2.3.tgz#808e14fd4ff39f91b619d5ca42cc4ae7d762d73d"
integrity sha512-F+VNgo7uxUyB2DyCtMAPZT0r06jipFD89nnn1mkEvVsaC5p05Pwz78sd9Jz/NN2tC2FT7PlsY5Ep6EB0VY6C5Q==
napi-rs@^0.2.4:
version "0.2.4"
resolved "https://registry.yarnpkg.com/napi-rs/-/napi-rs-0.2.4.tgz#81efcbb04ccc43e4e2713bfb9d1bad79b4ee8a7e"
integrity sha512-xJtETX+08wI0eQdvZRqumOxYlW5dyALCrK12N5h2UaiNMrVXlNDGTmpq274SotP8WZFVc/FB/uewtwGv58oa7Q==
dependencies:
minimist "^1.2.5"
toml "^3.0.0"
Expand Down Expand Up @@ -7349,7 +7349,7 @@ to-regex@^3.0.1, to-regex@^3.0.2:

toml@^3.0.0:
version "3.0.0"
resolved "https://registry.npmjs.org/toml/-/toml-3.0.0.tgz#342160f1af1904ec9d204d03a5d61222d762c5ee"
resolved "https://registry.yarnpkg.com/toml/-/toml-3.0.0.tgz#342160f1af1904ec9d204d03a5d61222d762c5ee"
integrity sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w==

tough-cookie@~2.5.0:
Expand Down