From ccd80fe00e17e3d1716ed86a51dc138e78c49aa9 Mon Sep 17 00:00:00 2001 From: saying Date: Wed, 27 Aug 2025 22:20:49 +0800 Subject: [PATCH] feat(ci): add release-npm --- .github/workflows/release-npm.yml | 269 ++++++++++++++++++++++++++++ bindgen/node/README.md | 88 +-------- bindgen/node/__test__/index.spec.ts | 8 + bindgen/node/__test__/package.json | 3 + bindgen/node/__test__/tsconfig.json | 11 ++ 5 files changed, 294 insertions(+), 85 deletions(-) create mode 100644 .github/workflows/release-npm.yml create mode 100644 bindgen/node/__test__/index.spec.ts create mode 100644 bindgen/node/__test__/package.json create mode 100644 bindgen/node/__test__/tsconfig.json diff --git a/.github/workflows/release-npm.yml b/.github/workflows/release-npm.yml new file mode 100644 index 0000000..9436119 --- /dev/null +++ b/.github/workflows/release-npm.yml @@ -0,0 +1,269 @@ +name: Node +env: + DEBUG: napi:* + APP_NAME: decrypt-cookies + MACOSX_DEPLOYMENT_TARGET: "10.13" + CARGO_INCREMENTAL: "1" +on: + push: + tags: + - "decrypt-cookies-node-v[0-9]+.[0-9]+.[0-9]+*" +defaults: + run: + working-directory: ./bindgen/node +jobs: + lint: + name: Lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v5 + - name: Setup node + uses: actions/setup-node@v4 + with: + node-version: 22 + cache: yarn + - name: Install + uses: dtolnay/rust-toolchain@stable + with: + components: clippy, rustfmt + - name: Install dependencies + run: yarn install + - name: Oxlint + run: yarn lint + build: + strategy: + fail-fast: false + matrix: + settings: + - host: macos-latest + target: x86_64-apple-darwin + build: yarn build --target x86_64-apple-darwin + - host: windows-latest + build: yarn build --target x86_64-pc-windows-msvc + target: x86_64-pc-windows-msvc + - host: ubuntu-latest + target: x86_64-unknown-linux-gnu + build: yarn build --target x86_64-unknown-linux-gnu --use-napi-cross + - host: ubuntu-latest + target: x86_64-unknown-linux-musl + build: yarn build --target x86_64-unknown-linux-musl -x + - host: macos-latest + target: aarch64-apple-darwin + build: yarn build --target aarch64-apple-darwin + - host: ubuntu-latest + target: aarch64-unknown-linux-gnu + build: yarn build --target aarch64-unknown-linux-gnu --use-napi-cross + - host: windows-latest + target: aarch64-pc-windows-msvc + build: yarn build --target aarch64-pc-windows-msvc + name: stable - ${{ matrix.settings.target }} - node@22 + runs-on: ${{ matrix.settings.host }} + steps: + - uses: actions/checkout@v5 + - name: Setup node + uses: actions/setup-node@v4 + with: + node-version: 22 + cache: yarn + - name: Install + uses: dtolnay/rust-toolchain@stable + with: + toolchain: stable + targets: ${{ matrix.settings.target }} + - name: Cache cargo + uses: actions/cache@v4 + with: + path: | + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + ~/.napi-rs + .cargo-cache + target/ + key: ${{ matrix.settings.target }}-cargo-${{ matrix.settings.host }} + - uses: mlugg/setup-zig@v2 + if: ${{ contains(matrix.settings.target, 'musl') }} + with: + version: 0.14.1 + - name: Install cargo-zigbuild + uses: taiki-e/install-action@v2 + if: ${{ contains(matrix.settings.target, 'musl') }} + env: + GITHUB_TOKEN: ${{ github.token }} + with: + tool: cargo-zigbuild + - name: Setup toolchain + run: ${{ matrix.settings.setup }} + if: ${{ matrix.settings.setup }} + shell: bash + - name: Install dependencies + run: yarn install + - name: Build + run: ${{ matrix.settings.build }} + shell: bash + - name: Upload artifact + uses: actions/upload-artifact@v4 + with: + name: bindings-${{ matrix.settings.target }} + path: | + ${{ env.APP_NAME }}.*.node + ${{ env.APP_NAME }}.*.wasm + if-no-files-found: error + test-macOS-windows-binding: + name: Test bindings on ${{ matrix.settings.target }} - node@${{ matrix.node }} + needs: + - build + strategy: + fail-fast: false + matrix: + settings: + - host: windows-latest + target: x86_64-pc-windows-msvc + architecture: x64 + - host: windows-11-arm + target: aarch64-pc-windows-msvc + architecture: arm64 + - host: macos-latest + target: aarch64-apple-darwin + architecture: arm64 + - host: macos-latest + target: x86_64-apple-darwin + architecture: x64 + node: + - "20" + - "22" + runs-on: ${{ matrix.settings.host }} + steps: + - uses: actions/checkout@v5 + - name: Setup node + uses: actions/setup-node@v4 + with: + node-version: ${{ matrix.node }} + cache: yarn + architecture: ${{ matrix.settings.architecture }} + - name: Install dependencies + run: yarn install + - name: Download artifacts + uses: actions/download-artifact@v5 + with: + name: bindings-${{ matrix.settings.target }} + path: . + - name: List packages + run: ls -R . + shell: bash + - name: Test bindings + run: yarn test + test-linux-binding: + name: Test ${{ matrix.target }} - node@${{ matrix.node }} + needs: + - build + strategy: + fail-fast: false + matrix: + target: + - x86_64-unknown-linux-gnu + - x86_64-unknown-linux-musl + - aarch64-unknown-linux-gnu + node: + - "20" + - "22" + runs-on: ${{ contains(matrix.target, 'aarch64') && 'ubuntu-24.04-arm' || 'ubuntu-latest' }} + steps: + - uses: actions/checkout@v5 + - name: Setup node + uses: actions/setup-node@v4 + with: + node-version: ${{ matrix.node }} + cache: yarn + - name: Output docker params + id: docker + run: | + node -e " + if ('${{ matrix.target }}'.startsWith('aarch64')) { + console.log('PLATFORM=linux/arm64') + } else if ('${{ matrix.target }}'.startsWith('armv7')) { + console.log('PLATFORM=linux/arm/v7') + } else { + console.log('PLATFORM=linux/amd64') + } + " >> $GITHUB_OUTPUT + node -e " + if ('${{ matrix.target }}'.endsWith('-musl')) { + console.log('IMAGE=node:${{ matrix.node }}-alpine') + } else { + console.log('IMAGE=node:${{ matrix.node }}-slim') + } + " >> $GITHUB_OUTPUT + - name: Install dependencies + run: | + yarn config set --json supportedArchitectures.cpu '["current", "arm64", "x64", "arm"]' + yarn config set --json supportedArchitectures.libc '["current", "musl", "gnu"]' + yarn install + - name: Download artifacts + uses: actions/download-artifact@v5 + with: + name: bindings-${{ matrix.target }} + path: . + - name: List packages + run: ls -R . + shell: bash + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + if: ${{ contains(matrix.target, 'armv7') }} + with: + platforms: all + - run: docker run --rm --privileged multiarch/qemu-user-static --reset -p yes + if: ${{ contains(matrix.target, 'armv7') }} + - name: Test bindings + uses: addnab/docker-run-action@v3 + with: + image: ${{ steps.docker.outputs.IMAGE }} + options: "-v ${{ github.workspace }}:${{ github.workspace }} -w ${{ github.workspace }} --platform ${{ steps.docker.outputs.PLATFORM }}" + run: yarn test + publish: + name: Publish + runs-on: ubuntu-latest + permissions: + contents: write + id-token: write + needs: + - lint + - test-macOS-windows-binding + - test-linux-binding + steps: + - uses: actions/checkout@v5 + - name: Setup node + uses: actions/setup-node@v4 + with: + node-version: 22 + cache: yarn + - name: Install dependencies + run: yarn install + - name: create npm dirs + run: yarn napi create-npm-dirs + - name: Download all artifacts + uses: actions/download-artifact@v5 + with: + path: ./bindgen/node/artifacts + - name: Move artifacts + run: yarn artifacts + - name: List packages + run: ls -R ./npm + shell: bash + - name: Publish + run: | + npm config set provenance true + if git log -1 --pretty=%B | grep "^[0-9]\+\.[0-9]\+\.[0-9]\+$"; + then + echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc + npm publish --access public + elif git log -1 --pretty=%B | grep "^[0-9]\+\.[0-9]\+\.[0-9]\+"; + then + echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc + npm publish --tag next --access public + else + echo "Not a release, skipping publish" + fi + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/bindgen/node/README.md b/bindgen/node/README.md index 92523e0..7c2ae1f 100644 --- a/bindgen/node/README.md +++ b/bindgen/node/README.md @@ -1,87 +1,5 @@ -# `@napi-rs/package-template` +# Decrypt Cookies -![https://github.com/napi-rs/package-template/actions](https://github.com/napi-rs/package-template/workflows/CI/badge.svg) +Rust crate python bindgen. -> Template project for writing node packages with napi-rs. - -# Usage - -1. Click **Use this template**. -2. **Clone** your project. -3. Run `yarn install` to install dependencies. -4. Run `yarn napi rename -n [@your-scope/package-name] -b [binary-name]` command under the project folder to rename your package. - -## Install this test package - -```bash -yarn add @napi-rs/package-template -``` - -## Ability - -### Build - -After `yarn build/npm run build` command, you can see `package-template.[darwin|win32|linux].node` file in project root. This is the native addon built from [lib.rs](./src/lib.rs). - -### Test - -With [ava](https://github.com/avajs/ava), run `yarn test/npm run test` to testing native addon. You can also switch to another testing framework if you want. - -### CI - -With GitHub Actions, each commit and pull request will be built and tested automatically in [`node@20`, `@node22`] x [`macOS`, `Linux`, `Windows`] matrix. You will never be afraid of the native addon broken in these platforms. - -### Release - -Release native package is very difficult in old days. Native packages may ask developers who use it to install `build toolchain` like `gcc/llvm`, `node-gyp` or something more. - -With `GitHub actions`, we can easily prebuild a `binary` for major platforms. And with `N-API`, we should never be afraid of **ABI Compatible**. - -The other problem is how to deliver prebuild `binary` to users. Downloading it in `postinstall` script is a common way that most packages do it right now. The problem with this solution is it introduced many other packages to download binary that has not been used by `runtime codes`. The other problem is some users may not easily download the binary from `GitHub/CDN` if they are behind a private network (But in most cases, they have a private NPM mirror). - -In this package, we choose a better way to solve this problem. We release different `npm packages` for different platforms. And add it to `optionalDependencies` before releasing the `Major` package to npm. - -`NPM` will choose which native package should download from `registry` automatically. You can see [npm](./npm) dir for details. And you can also run `yarn add @napi-rs/package-template` to see how it works. - -## Develop requirements - -- Install the latest `Rust` -- Install `Node.js@10+` which fully supported `Node-API` -- Install `yarn@1.x` - -## Test in local - -- yarn -- yarn build -- yarn test - -And you will see: - -```bash -$ ava --verbose - - ✔ sync function from native code - ✔ sleep function from native code (201ms) - ─ - - 2 tests passed -✨ Done in 1.12s. -``` - -## Release package - -Ensure you have set your **NPM_TOKEN** in the `GitHub` project setting. - -In `Settings -> Secrets`, add **NPM_TOKEN** into it. - -When you want to release the package: - -```bash -npm version [ | major | minor | patch | premajor | preminor | prepatch | prerelease [--preid=] | from-git] - -git push -``` - -GitHub actions will do the rest job for you. - -> WARN: Don't run `npm publish` manually. +Easily make a request using the authorization data from your browser. diff --git a/bindgen/node/__test__/index.spec.ts b/bindgen/node/__test__/index.spec.ts new file mode 100644 index 0000000..4a8ffb2 --- /dev/null +++ b/bindgen/node/__test__/index.spec.ts @@ -0,0 +1,8 @@ +import test from "ava"; + +import { ChromiumGetter, FirefoxGetter } from "../index.js"; + +test("node test", (t) => { + t.is(ChromiumGetter.name, "ChromiumGetter"); + t.is(FirefoxGetter.name, "FirefoxGetter"); +}); diff --git a/bindgen/node/__test__/package.json b/bindgen/node/__test__/package.json new file mode 100644 index 0000000..3dbc1ca --- /dev/null +++ b/bindgen/node/__test__/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/bindgen/node/__test__/tsconfig.json b/bindgen/node/__test__/tsconfig.json new file mode 100644 index 0000000..f36973a --- /dev/null +++ b/bindgen/node/__test__/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "module": "ESNext", + "moduleResolution": "Bundler", + "outDir": "lib", + "rootDir": "." + }, + "include": ["*.ts"], + "exclude": ["lib"] +}