Skip to content

Commit 7d371d8

Browse files
authored
feat: add js-wasm bindings and formalise WASM runtime support (VirusTotal#598)
This PR adds a browser-facing JavaScript package for yara-x and the supporting library changes needed to run it on wasm32-unknown-unknown. The main library change is a refactor of the internal WASM runtime layer so that non-Wasm targets continue to use wasmtime, while browser builds use a small compatibility shim over the host WebAssembly runtime. The browser-specific runtime code is isolated under lib/src/wasm/runtime/, which keeps the rest of the library largely unchanged and avoids spreading target-specific conditionals across the codebase. On top of that, this PR adds a new in-tree js-wasm package. It exposes an object-based JavaScript API built around `Compiler`, `Rules`, and `Scanner`, and is packaged using a wasm-pack-based workflow.
1 parent b836443 commit 7d371d8

File tree

26 files changed

+4921
-906
lines changed

26 files changed

+4921
-906
lines changed

.github/workflows/tests.yaml

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,72 @@ jobs:
178178
- name: Run doc tests
179179
run: cargo doc --no-deps
180180
env:
181-
RUSTDOCFLAGS: ${{ matrix.rust_flags }}
181+
RUSTDOCFLAGS: ${{ matrix.rust_flags }}
182+
183+
browser-wasm:
184+
name: Browser WASM
185+
runs-on: ubuntu-latest
186+
env:
187+
CARGO_TERM_COLOR: always
188+
steps:
189+
- name: Checkout sources
190+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
191+
192+
- name: Install Rust toolchain
193+
uses: dtolnay/rust-toolchain@master
194+
with:
195+
toolchain: stable
196+
target: wasm32-unknown-unknown
197+
198+
- name: Install Node.js
199+
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
200+
with:
201+
node-version: 22
202+
203+
- name: Install Chrome and ChromeDriver
204+
id: setup-chrome
205+
uses: browser-actions/setup-chrome@v2
206+
with:
207+
chrome-version: stable
208+
install-dependencies: true
209+
install-chromedriver: true
210+
211+
- name: Add ChromeDriver to PATH
212+
run: echo "$(dirname '${{ steps.setup-chrome.outputs.chromedriver-path }}')" >> "$GITHUB_PATH"
213+
214+
- name: Install wasm-pack
215+
run: cargo install --locked wasm-pack --version 0.14.0
216+
217+
- name: Install wasm-bindgen CLI
218+
run: cargo install --locked wasm-bindgen-cli --version 0.2.113
219+
220+
- name: Install terser
221+
run: npm install --global terser
222+
223+
- name: Write webdriver.json
224+
run: |
225+
cat > js-wasm/webdriver.json <<EOF
226+
{
227+
"goog:chromeOptions": {
228+
"binary": "${{ steps.setup-chrome.outputs.chrome-path }}"
229+
}
230+
}
231+
EOF
232+
233+
- name: Run Node-targeted Rust tests
234+
run: npm --prefix js-wasm run test:wasm-node
235+
236+
- name: Run headless browser Rust tests
237+
run: npm --prefix js-wasm run test:wasm-browser
238+
env:
239+
CHROMEDRIVER: ${{ steps.setup-chrome.outputs.chromedriver-path }}
240+
WASM_BINDGEN_TEST_WEBDRIVER_JSON: ${{ github.workspace }}/js-wasm/webdriver.json
241+
242+
- name: Build browser package
243+
run: npm --prefix js-wasm run build:web
244+
245+
- name: Run JS integration tests
246+
run: npm --prefix js-wasm run test:js
247+
248+
- name: Validate npm package contents
249+
run: npm --prefix js-wasm run pack:dry-run

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.DS_Store
12
.env
23
.idea
34
.pytest_cache
@@ -6,6 +7,8 @@
67
*.profraw
78
*.swp
89
target
10+
js-wasm/dist
11+
js-wasm/pkg
912
macros/target
1013
site/node_modules
1114
site/public

0 commit comments

Comments
 (0)