Prerequisites
The Chainloop JavaScript SDK for WASM policies is built on top of the Extism JavaScript PDK, which enables JavaScript code to run inside WebAssembly with QuickJS. Required tools:Node.js (16 or later)
Node.js (16 or later)
For package management and building.
extism-js compiler
extism-js compiler
Compiles JavaScript to WebAssembly.
@chainloop-dev/policy-sdk- Chainloop WASM Policy SDK (npm package)@extism/js-pdk- Extism JavaScript PDK (auto-installed)esbuild- JavaScript bundler (dev dependency)
Project Setup
Install Dependencies
Configure Build
esbuild.js:Complete Example
API Quick Reference
The JavaScript SDK provides functions for:- Execution:
run()- Entry point wrapper - Material Extraction:
getMaterialJSON(),getMaterialString(),getMaterialBytes() - Arguments:
getArgs(),getArgString(),getArgStringDefault() - Results:
success(),fail(),skip(),outputResult(),addViolation(),hasViolations() - Logging:
logInfo(),logDebug(),logWarn(),logError() - HTTP Requests:
httpGet(),httpGetJSON(),httpPost(),httpPostJSON() - Artifact Discovery:
discover(),discoverByDigest()
Building
- esbuild - Bundles policy.js and SDK
- extism-js - Compiles bundle to WASM
dist/policy.js- Bundled JavaScript (~7KB)policy.wasm- Compiled WASM (~2.1MB, includes QuickJS runtime)
Testing
test.sh:JavaScript Compatibility
Supported:- ✅ ES2020 JavaScript features
- ✅ JSON parsing and manipulation
- ✅ String operations and regex
- ✅ Arrays, objects, and basic types
- ✅ Synchronous operations
- ❌ async/await (no Promise support)
- ❌ Node.js built-ins (fs, path, http)
- ❌ ES modules (use CommonJS)
- ❌ Browser APIs (fetch, localStorage)
- ❌ setTimeout/setInterval
- ❌ Symbols and WeakMaps
Best Practices
- Use simple data structures - Plain objects and arrays
- Validate early - Check material format first
- Clear violation messages - Include specific details
- Use skip appropriately - Don’t fail for wrong material types
- Test with real data - Use actual artifacts
- Log progress - Aid debugging
- Handle errors - Use try-catch for parsing
