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
3 changes: 2 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"ghcr.io/devcontainers/features/rust:1": {
"version": "1.93.1",
"profile": "minimal",
"components": "rustfmt,clippy"
"components": "rustfmt,clippy",
"targets": "wasm32-unknown-unknown"
},
"ghcr.io/devcontainers/features/java:1": { "version": "17" },

Expand Down
90 changes: 14 additions & 76 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,91 +5,29 @@ on:
permissions:
contents: read
jobs:
native:
name: Native / ${{ matrix.platform }}
strategy:
fail-fast: false
matrix:
include:
- platform: linux-x64
node-arch: x64
runner: ubuntu-24.04
target: x86_64-unknown-linux-gnu
- platform: linux-arm64
node-arch: arm64
runner: ubuntu-24.04-arm
target: aarch64-unknown-linux-gnu
- platform: darwin-arm64
node-arch: arm64
runner: macos-latest
target: aarch64-apple-darwin
- platform: win32-x64
node-arch: x64
runner: windows-latest
target: x86_64-pc-windows-msvc
runs-on: ${{ matrix.runner }}
build:
name: Build and test
runs-on: ubuntu-24.04
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 26
architecture: ${{ matrix.node-arch }}
cache: npm
- name: Check the Node architecture
env:
NEO_PLATFORM: ${{ matrix.platform }}
run: node -e "if (process.platform + '-' + process.arch !== process.env.NEO_PLATFORM) process.exit(1)"
- run: rustup toolchain install 1.93.1 --profile minimal --component rustfmt --component clippy --target ${{ matrix.target }}
- uses: ilammy/msvc-dev-cmd@v1
if: runner.os == 'Windows'
- run: rustup toolchain install 1.93.1 --profile minimal --component rustfmt --component clippy --target wasm32-unknown-unknown
# The command-line tool must match the wasm-bindgen crate version in wasm/Cargo.lock.
- uses: taiki-e/install-action@v2
with:
arch: amd64
- name: Select Perl for vendored OpenSSL
if: runner.os == 'Windows'
shell: pwsh
run: |
$perlDir = 'C:\Strawberry\perl\bin'
if (-not (Test-Path "$perlDir\perl.exe")) {
choco install strawberryperl --no-progress -y
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
}
$perlDir | Out-File -FilePath $env:GITHUB_PATH -Append
- run: cargo fmt --check --manifest-path native/Cargo.toml
- run: cargo clippy --locked --all-targets --manifest-path native/Cargo.toml --target ${{ matrix.target }} -- -D warnings
- run: cargo test --locked --manifest-path native/Cargo.toml --target ${{ matrix.target }}
tool: wasm-bindgen@0.2.130
- run: cargo fmt --check --manifest-path wasm/Cargo.toml
- run: cargo clippy --locked --all-targets --manifest-path wasm/Cargo.toml -- -D warnings
- run: cargo clippy --locked --manifest-path wasm/Cargo.toml --target wasm32-unknown-unknown -- -D warnings
- run: cargo test --locked --manifest-path wasm/Cargo.toml
- run: npm ci --ignore-scripts
- run: npm run build:native
- run: npm run build:wasm
- run: npm test
- run: npm run test:package
- uses: actions/upload-artifact@v4
with:
name: neo-native-${{ matrix.platform }}
path: native/${{ matrix.platform }}/
if-no-files-found: error

package:
name: Assemble all-platform npm tarball
needs: native
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 26
cache: npm
- uses: actions/download-artifact@v4
with:
pattern: neo-native-*
path: native
- name: Stage each native build
run: |
for addon in native/neo-native-*; do
platform=${addon#native/neo-native-}
mv "$addon" "native/$platform"
done
- run: npm ci --ignore-scripts
- run: npm run build
# Installs the tarball in a fresh project, runs the package tests and keeps the tarball.
- run: npm run test:package -- artifacts
- uses: actions/upload-artifact@v4
with:
Expand All @@ -99,7 +37,7 @@ jobs:

publish:
name: Publish to npm
needs: package
needs: build
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-24.04
environment: npm
Expand Down
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
node_modules/
dist/
native/target/
native/**/*.node
wasm/target/
wasm/pkg/
*.tgz
*.memory-card.json
.env*
native/**/THIRD-PARTY.txt
26 changes: 10 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,22 @@ The 30-second live terminal demo shows QR login and the standalone TypeScript ex

https://github.com/user-attachments/assets/e7e40c19-ad49-4061-be53-cef1e1c4c978

Neo builds native addons for Linux x64 and ARM64, macOS on Apple Silicon, and Windows x64. CI tests an npm install on each target and assembles one tarball with all four addons. The prebuilt Linux addons require glibc 2.38 or newer. Neo loads the addon for the architecture of the running Node process. Use ARM64 Node.js on macOS.
Neo's protocol session is compiled to WebAssembly, so one package runs wherever Node.js 26 runs, with no native addon and nothing to compile on install. Node opens the TCP connections to WeChat's servers. CI builds it on Linux, installs the package in a fresh project and runs its tests there.

On Windows 11 on ARM, install **x64 Node.js** to use the x64 addon under Windows emulation. Run `node -p process.arch` to check that it prints `x64`. [Microsoft's emulation documentation](https://learn.microsoft.com/en-us/windows/arm/apps-on-arm-x86-emulation)

To build from source, use Node.js 26 and Rust 1.93.1 with the matching Rust target.

| Platform | Build prerequisites |
| --- | --- |
| Linux | C compiler, Make, Perl |
| macOS on Apple Silicon | Xcode Command Line Tools, Make, Perl |
| Windows x64 | MSVC x64 C++ tools, Windows SDK, Perl |
To build from source, use Node.js 26, Rust 1.93.1 with the `wasm32-unknown-unknown` target, and the `wasm-bindgen` command-line tool at the version in `wasm/Cargo.lock`. No C compiler or Perl is needed.

```sh
rustup target add wasm32-unknown-unknown
cargo install --locked wasm-bindgen-cli --version 0.2.130
npm ci
npm run build:native
npm run build:wasm
npm run build
node examples/ding-dong-bot.ts
```

Scan the terminal QR and confirm the login on your phone. The example prints the account's nickname. After `Ready` appears, send `ding` from another WeChat account, directly or in a group that includes this account. The example replies with `dong` in the same chat and prints the received message and reply. Signing the device out from the phone, or calling `puppet.logout()`, emits `logout` with the reason. Stop with Ctrl+C. Starting it again displays a new QR.

Node runs the TypeScript example directly. The commands above build the package and native addon from source. To use a prebuilt tarball in another project, install the tarball and copy its `examples/ding-dong-bot.ts` into that project before running it.
Node runs the TypeScript example directly. The commands above build the package and its WebAssembly session from source. To use a prebuilt tarball in another project, install the tarball and copy its `examples/ding-dong-bot.ts` into that project before running it.

Run `npm test` for type checking and the offline tests, and `npm run test:package` to check a clean tarball installation.

Expand All @@ -38,16 +32,16 @@ Run `npm test` for type checking and the offline tests, and `npm run test:packag
CI publishes to npm when a `v<version>` tag on `main` is pushed and every platform build and package test passes. The tag must match the `package.json` version. An odd minor version, such as 0.1.x, publishes on the `next` dist-tag; an even minor version publishes on `latest`, following the [Wechaty versioning conventions](https://github.com/wechaty/wechaty/issues/905). The tarball carries npm provenance from this workflow, which publishes through npm trusted publishing (OIDC) with no npm token.

```sh
git tag v0.1.2
git push origin v0.1.2
git tag v0.1.3
git push origin v0.1.3
```

### Development container

A dev container is a reproducible development workspace described in the repository. VS Code's Dev Containers extension can open the project inside that environment so contributors use the same OS and toolchain without installing Node.js or Rust on the host.

The configuration in [`.devcontainer/devcontainer.json`](.devcontainer/devcontainer.json) is based on the [Ship.Fail devcontainer template](https://github.com/ShipFail/devcontainer). It uses Ubuntu 24.04, Node.js 26, Rust 1.93.1 with `rustfmt` and `clippy`, and the C compiler, Make, and Perl needed for the native addon. It runs `npm ci` after container creation. It also retains the template's supporting CLI tools and mounts host Claude, Codex, Gemini, and Git configuration into the container; the GitHub CLI helper can use the host Git credential helper. Only use these host mounts when you trust the repository and its development tooling.
The configuration in [`.devcontainer/devcontainer.json`](.devcontainer/devcontainer.json) is based on the [Ship.Fail devcontainer template](https://github.com/ShipFail/devcontainer). It uses Ubuntu 24.04, Node.js 26, and Rust 1.93.1 with `rustfmt`, `clippy` and the `wasm32-unknown-unknown` target. Install `wasm-bindgen-cli` as shown above before `npm run build:wasm`. It runs `npm ci` after container creation. It also retains the template's supporting CLI tools and mounts host Claude, Codex, Gemini, and Git configuration into the container; the GitHub CLI helper can use the host Git credential helper. Only use these host mounts when you trust the repository and its development tooling.

Windows contributors must use WSL to open the repository and run the dev container; native Windows host initialization is unsupported. Install the VS Code Dev Containers extension, then run **Dev Containers: Reopen in Container** from the Command Palette.

For live acceptance, use an isolated test account and a confirmed peer. The example replies to direct `ding` messages, including messages received during initial sync. Verify `dong` on the peer's device. Real-account testing has covered Linux x64. CI on the other targets covers addon loading, npm installation, and scripted Puppet behavior.
For live acceptance, use an isolated test account and a confirmed peer. The example replies to direct `ding` messages, including messages received during initial sync. Verify `dong` on the peer's device. Real-account testing has covered Linux x64. CI covers loading the WebAssembly session, npm installation, and scripted Puppet behavior.
23 changes: 12 additions & 11 deletions docs/v1-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Puppet Neo is a local WeChat protocol Puppet for Wechaty. Its long-term goal is

## First acceptance gate

Use an isolated Linux x64 test account and a confirmed separate peer. Build the addon and package, then run the repository example.
Use an isolated test account and a confirmed separate peer. Build the WebAssembly session and package, then run the repository example.

```sh
npm ci
npm run build:native
npm run build:wasm
npm run build
node examples/ding-dong-bot.ts
```
Expand All @@ -28,16 +28,17 @@ The example listens for Puppet events, retrieves messages with `messagePayload()
```text
ding-dong-bot.ts
-> PuppetNeo (TypeScript, Puppet events and payload lookup)
-> NeoSession (.node, Rust account credentials and protocol operations)
-> wechat-protocol = 2.2.0 (published Cargo source)
-> NeoSession (WebAssembly, Rust account credentials and protocol operations)
-> wechat-protocol = 3.8.0 (published Cargo source, pure-Rust crypto)
-> NeoHost (Node.js TCP connections and clocks)
-> WeChat servers
```

Neo builds the addon from pinned crate sources. Cargo.lock and package-lock.json pin build inputs. Native CI builds and tests Linux x64 and ARM64 with glibc, macOS ARM64 on `macos-latest`, and Windows x64 on `windows-latest`. The combined npm tarball contains all four native builds and selects one using Node's platform and architecture. The CI-built Linux addons require glibc 2.38 or newer.
Neo builds the WebAssembly session from pinned crate sources with the protocol's pure-Rust crypto backend, so no OpenSSL or C toolchain is involved. Cargo.lock and package-lock.json pin build inputs, and the `wasm-bindgen` tool must match the crate version in Cargo.lock. CI builds the session on Linux, installs the tarball in a fresh project and runs the package tests there; the WebAssembly file and the Node APIs Neo uses behave the same on every platform. The npm tarball contains one WebAssembly file for every platform.

Blocking native calls run off the Node event loop, and a Rust lock serializes session access. TypeScript schedules login polling, sync and heartbeat, then maps incoming text to Puppet payloads and events. After authentication, Neo opens the account's incoming connection, drains initial sync, and requests NewSync when that connection or a heartbeat signals message work.
Session calls are asynchronous and run on the Node event loop. The WebAssembly session asks Node for TCP connections, reads, writes and the time, and an async lock serializes session access. TypeScript schedules login polling, sync and heartbeat, then maps incoming text to Puppet payloads and events. After authentication, Neo opens the account's incoming connection, drains initial sync, and requests NewSync when that connection or a heartbeat signals message work.

For protocol contributors, follow `messageSendText()` in [the Puppet](../src/puppet-neo.ts) into the [native session](../native/src/lib.rs), then see `AccountRequest::send_text` and `wire::text_send_request` in the [published Rust source](https://docs.rs/crate/wechat-protocol/2.2.0/source/src/). After login, the session moves the accepted credentials into one `NativeClient` account owner; the push connection and sends share it. Incoming text follows the reverse direction through NewSync and AddMsg decoding.
For protocol contributors, follow `messageSendText()` in [the Puppet](../src/puppet-neo.ts) into the [WebAssembly session](../wasm/src/lib.rs), then see `NativeClient::send_text` and `wire::text_send_request` in the [published Rust source](https://docs.rs/crate/wechat-protocol/3.8.0/source/src/). After login, the session moves the accepted credentials into one `NativeClient` account owner; the push connection and sends share it. Incoming text follows the reverse direction through NewSync and AddMsg decoding.

## State and errors

Expand Down Expand Up @@ -68,7 +69,7 @@ Each session emits `logout` once:
| WeChat rejects the session (`-13`) and also rejects one automatic renewal, for example after the device is signed out from the phone | `WeChat ended the session` |
| Any other failure after login stops the session loop. An `error` event comes first | `Neo session failed` |

Signing out from the phone can close the socket before WeChat returns any code. So if the notification socket closes, or a sync or heartbeat fails without a service code, Neo reconnects once and repeats the read, and WeChat's answer decides whether the session is still valid. Neo uses the crate's typed errors for these decisions, never the error text. Sends are never repeated after a renewal. After a logout the puppet stays started; stop and start it to log in with a new QR. The base Puppet clears `currentUserId` one event-loop turn after emitting `logout`, so listeners can still read it.
Signing out from the phone can close the socket before WeChat returns any code. So if the notification socket closes, or a sync or heartbeat fails without a service code, Neo reconnects once and repeats the read, and WeChat's answer decides whether the session is still valid. Neo uses the crate's typed errors for these decisions, never the error text. Sends are never repeated after a renewal. After a logout the puppet stays started; stop and start it to log in with a new QR. The base Puppet clears `currentUserId` on the next timer tick after emitting `logout`, so listeners can still read it. WebAssembly cannot accept incoming connections, so a login that WeChat routes through its local-network check fails instead of completing; scan the QR again.

## Validation and release gates

Expand All @@ -78,8 +79,8 @@ Signing out from the phone can close the socket before WeChat returns any code.
| Puppet API | Scripted session drives QR login, text payload lookup, reply, clean stop and a fresh login |
| Login and logout | Login after the profile, `ready` after the initial sync, nickname and fallback names, and one logout for stop, `logout()`, a session ended by WeChat during heartbeat or sync, and an unexpected failure |
| Example | The actual TypeScript example runs against a scripted session and replies through PuppetNeo, directly and in a group |
| Native boundary | Load the addon without a storage key and reject unauthenticated operations |
| Package | Install a tarball in a fresh project and run the Puppet, native and example tests |
| WebAssembly boundary | Load the WebAssembly session and reject unauthenticated operations |
| Package | Install a tarball in a fresh project and run the Puppet, session and example tests |
| Live acceptance | Fresh QR login showing the nickname, Ding Dong directly and in a group with an isolated test account, `logout` after signing out from the phone, then repeat after restarting |

Start with an npm development release on `next` after acceptance. Follow Wechaty's odd-minor development and even-minor stable convention. Use the combined tarball after its four native package checks pass and report live acceptance separately. Review the repository, installed example and release workflow with the Wechaty maintainers before expanding scope or promoting a stable release. Automatic publication follows that review.
Start with an npm development release on `next` after acceptance. Follow Wechaty's odd-minor development and even-minor stable convention. Use the tarball after its package checks pass and report live acceptance separately. Review the repository, installed example and release workflow with the Wechaty maintainers before expanding scope or promoting a stable release. Automatic publication follows that review.
22 changes: 0 additions & 22 deletions native/Cargo.toml

This file was deleted.

3 changes: 0 additions & 3 deletions native/build.rs

This file was deleted.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wechaty-puppet-neo",
"version": "0.1.2",
"version": "0.1.3",
"description": "A standalone WeChat protocol Puppet for Wechaty",
"type": "module",
"main": "./dist/index.js",
Expand All @@ -13,8 +13,9 @@
},
"files": [
"dist",
"native/*/neo.node",
"native/*/THIRD-PARTY.txt",
"wasm/pkg/neo.js",
"wasm/pkg/neo_bg.wasm",
"wasm/pkg/THIRD-PARTY.txt",
"examples/ding-dong-bot.ts",
"docs/v1-spec.md"
],
Expand All @@ -29,7 +30,7 @@
},
"scripts": {
"build": "tsc",
"build:native": "node scripts/build-native.mjs",
"build:wasm": "node scripts/build-wasm.mjs",
"typecheck": "tsc --project tsconfig.check.json",
"test": "npm run build && npm run typecheck && node --test test/*.test.ts",
"ding-dong": "node examples/ding-dong-bot.ts",
Expand Down
1 change: 1 addition & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[toolchain]
channel = "1.93.1"
components = ["rustfmt", "clippy"]
targets = ["wasm32-unknown-unknown"]
Loading
Loading