Skip to content

Run the session in WebAssembly instead of native addons - #23

Merged
thisnick merged 3 commits into
mainfrom
feat/wasm-core
Dec 3, 2026
Merged

thisnick merged 3 commits into
mainfrom
feat/wasm-core

Conversation

@thisnick

@thisnick thisnick commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

Neo's Rust session moves from four per-platform Node-API addons on wechat-protocol 2.2.0 to one WebAssembly build on wechat-protocol 3.8.0's async API with its pure-Rust crypto backend. One package now runs wherever Node.js 26 runs, and installing it compiles nothing. This follows the direction in #21: one wasm core, with Node as the first host.

Changes

  • wasm/ replaces native/. The session logic is unchanged, ported to the async calls: QR login, the push connection's heartbeat, sync and poll, sends, profile lookup, renewal and logout. An async lock serializes session access, as the Rust mutex did. The Rust tests move over unchanged in substance (11 pass).
  • The network and clock come from Node. src/host.ts opens TCP connections with node:net and implements the protocol's stream contract: bytes, end of stream, or timed out. It reports failures as timeout, interrupted or disconnected, so QR polling still retries after a dropped connection, and it supplies Node's clocks. Random bytes come from crypto.getRandomValues.
  • src/session.ts (was native.ts) loads the wasm-bindgen output from wasm/pkg. The platform, glibc and Windows-on-ARM checks are gone.
  • npm run build:wasm builds for wasm32-unknown-unknown, runs wasm-bindgen (checked against the crate version in Cargo.lock) and writes the third-party notices. No C compiler, Make or Perl is needed.
  • CI is one Linux job: fmt, clippy for the host and wasm32, Rust tests, npm test, then a clean install of the packed tarball with the package tests. It keeps that tarball for the unchanged publish job. The WebAssembly file and the Node APIs Neo uses behave the same on every platform, so macOS and Windows runners are no longer needed.
  • The npm tarball drops from 16.8 MB to 447 KB. The wasm file is 1.1 MB unpacked.

One gap: WebAssembly can't accept incoming connections, so a login that WeChat routes through its local-network check now fails instead of completing; the user scans again. Native 2.2.0 opened a local listener for this.

Testing

  • cargo fmt --check, clippy with -D warnings for the host and wasm32-unknown-unknown, and cargo test (11 pass).
  • npm test (9 pass, including the session test against the real wasm build) and npm run test:package on Node.js 26, linux-x64.
  • Smoke test with no account: the wasm session fetched a real login QR from WeChat over net.Socket, and a poll returned pending.
  • Live acceptance passed on an isolated test account, with Neo's own Ding Dong example installed from this PR's tarball on Node.js 26 (linux-x64). It logged in by QR with the nickname (Logged in as 昊罡.), then Ready. A direct ding and a group ding each got one dong, the group reply going to the group. Signing out from the phone printed Logged out: WeChat ended the session with no error event. Upstream CI only type-checks wechat-protocol for wasm32, so this is the first full session over wasm.

🤖 Generated with Claude Code

Neo's Rust session moves from four per-platform Node-API addons on
wechat-protocol 2.2.0 to one WebAssembly build on wechat-protocol 3.8.0's
async API with its pure-Rust crypto backend.

- wasm/ replaces native/. The session logic is the same, ported to the
  async calls: login, the push connection's heartbeat, sync and poll,
  sends, profile lookup, renewal and logout. An async lock serializes
  session access, as the Rust mutex did.
- The protocol's network and clock come from the JavaScript host. src/host.ts
  opens TCP connections with node:net, reports failures as timeout,
  interrupted or disconnected so login polling still retries, and supplies
  Node's clocks. Random bytes come from crypto.getRandomValues.
- src/session.ts (was native.ts) loads the wasm-bindgen output from
  wasm/pkg. The platform, glibc and Windows-on-ARM checks are gone: one file
  runs wherever Node.js 26 runs.
- npm run build:wasm builds the crate for wasm32-unknown-unknown, runs
  wasm-bindgen (the tool must match the crate version) and writes the
  third-party notices. No C compiler, Make or Perl is needed.
- CI builds once on Linux (fmt, clippy for the host and wasm32, Rust tests,
  npm test), installs the tarball on Linux x64 and ARM64, macOS ARM64 and
  Windows x64, then assembles the tarball for the unchanged publish job.
- The npm tarball drops from 16.8 MB to 447 KB.

WebAssembly cannot accept incoming connections, so a login that WeChat
routes through its local-network check now fails instead of completing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thisnick
thisnick marked this pull request as ready for review December 3, 2026 01:42
thisnick and others added 2 commits December 2, 2026 17:44
The WebAssembly file and the Node APIs Neo uses behave the same on every
platform, so one Linux job builds, tests, installs the tarball in a fresh
project and keeps it for publishing. macOS and Windows runners are no
longer needed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thisnick
thisnick merged commit 1237f1f into main Dec 3, 2026
5 checks passed
@thisnick
thisnick deleted the feat/wasm-core branch December 3, 2026 01:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant