# bytecodealliance/jco architecture

> Architecture diagram of the GitHub repository bytecodealliance/jco, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bytecodealliance/jco
- Repository: https://github.com/bytecodealliance/jco

## Overview

##### Project type and purpose
- This is an open-source, multi-tool CLI and library for working with WebAssembly Components from JavaScript/TypeScript.
- It combines a JS/Node CLI (`packages/jco`), a runtime shim for WASI preview2 (`packages/preview2-shim`), and several Rust crates that compile to WebAssembly components (`crates/js-component-bindgen`, `crates/js-component-bindgen-component`, `crates/wasm-tools-component`).

##### File structure
- Top-level “packages” directory holds the JavaScript/TypeScript npm packages:
- `jco` – the CLI and JS API
- `preview2-shim` – a runtime library mapping WASI Preview2 to Node/browser
- “crates” directory holds Rust subprojects:
- `js-component-bindgen` – core Rust logic for generating JS bindings
- `js-component-bindgen-component` – the Rust crate compiled as a WebAssembly Component to expose `js-component-bindgen` at runtime
- `wasm-tools-component` – exposes selected pieces of `wasm-tools` as a WebAssembly Component
- `xtask` – a Rust build/task runner orchestrating code-gen, tests, and packaging
- `examples` / `docs` hold usage samples and documentation
- `scripts` contain ancillary automation (e.g., IDL generation, versioning)

##### README for additional insights
- The README’s “Organization” table explicitly maps each subproject to its role
- The core workflows: build components, transpile to ES modules, run components, reuse workflows, and invoke wasm-tools from JS

##### Main components
a. CLI & JS API (packages/jco)
b. Runtime Shim (packages/preview2-shim)
c. Wasm-based Components:
- js-component-bindgen-component.wasm
- wasm-tools-component.wasm
d. Rust Build & Code-gen Tasks (crates/xtask + submodules: wit-bindgen, wasmtime, wasm-tools, WASI-Virt)
e. External JS library: componentize-js (dynamically imported at runtime)
f. Host Environments: Node.js and Browser

##### Relationships and data flows
- User invokes `jco` CLI → CLI parses commands → calls into the JS API
- JS API dynamically imports/instantiates the appropriate WebAssembly Component(s) under the WASI Preview2 environment provided by `preview2-shim`
- For “transpile” and “componentize” commands:
- JS API instantiates `js-component-bindgen-component.wasm` → marshals arguments (WIT, source files) → receives back a WebAssembly Component or JS binding code
- JS API instantiates `wasm-tools-component.wasm` for lower-level wasm-tools operations (e.g., optimization, validate)
- Results flow back to the JS API → CLI prints output or writes generated files
- Build-time: `xtask` orchestrates compiling the Rust crates (producing `.wasm` files), generating bindings/tests, and copying outputs into `packages/*` for npm packaging

##### Architectural patterns and design principles
- Polyglot monorepo: JS/TS packages alongside Rust crates
- Plugin-style extensibility: functionality shipped as wasm “plugins” (components) that the CLI can load on demand
- Clear separation of concerns:
- CLI & orchestration (JS)
- Runtime environment abstraction (preview2-shim)
- Core logic in Rust, exposed via WebAssembly Components
- Build orchestration via `xtask`
- Reuse of standard WASI Preview2 and WebAssembly Component Model interoperability

##### Elements
- User → CLI → JS API → instantiate(WASM component) → host environment
- JS API → dynamic import(`componentize-js`)
- `xtask` → cargo build → `.wasm` artifacts → npm package
- JS/TS packages (e.g. blue rectangles)
- Runtime shim (green)
- WASM Components (yellow)
- Rust build tasks (grey)
- External dependencies (light border)

##### Detailed capture of essential elements
- Don’t omit the two wasm-component crates or the preview2-shim—these are what truly differentiate Jco from a plain JS CLI
- Call out that `js-component-bindgen-component.wasm` and `wasm-tools-component.wasm` are first-class runtime modules
- “xtask” as the orchestrator that ties together Cargo, submodules, and packaging into the npm packages

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Runtime Pipeline
    User["User"]:::external
    JCO["CLI & JS API"]:::js
    API["JS API"]:::js
    CJ["componentize-js"]:::external
    BIND_CMP["js-component-bindgen-component.wasm"]:::wasm
    WT_CMP["wasm-tools-component.wasm"]:::wasm
    SHIM["Runtime Shim (WASI Preview2)"]:::shim
    NODE["Node.js Env"]:::external
    BROWSER["Browser Env"]:::external

    User -->|"invoke jco"| JCO
    JCO -->|"calls"| API
    API -->|"dynamic import"| CJ
    API -->|"instantiate"| BIND_CMP
    API -->|"instantiate"| WT_CMP
    BIND_CMP -->|"runs under"| SHIM
    WT_CMP -->|"runs under"| SHIM
    SHIM -->|"provides env"| NODE
    SHIM -->|"provides env"| BROWSER

    %% Build Pipeline
    subgraph "Build Pipeline"
        XTASK["xtask (Rust build orchestrator)"]:::rust
        JS_BINDGEN["js-component-bindgen crate"]:::rust
        JS_BINDGEN_CMP["js-component-bindgen-component"]:::wasm
        WT_CMP_BUILD["wasm-tools-component"]:::wasm
        PKG_JCO["jco (npm package)"]:::js
        PKG_SHIM["preview2-shim (npm package)"]:::shim
        SCRIPTS["scripts"]:::external

        XTASK -->|"cargo build"| JS_BINDGEN
        XTASK -->|"cargo build"| JS_BINDGEN_CMP
        XTASK -->|"cargo build"| WT_CMP_BUILD
        XTASK -->|"bundle"| PKG_JCO
        XTASK -->|"bundle"| PKG_SHIM
        SCRIPTS -->|"assist"| XTASK
    end

    %% Resources
    subgraph "Resources"
        EXAMPLES["examples"]:::external
        DOCS["docs/src"]:::external
    end

    JCO -->|"uses examples"| EXAMPLES
    JCO -->|"references docs"| DOCS

    %% Click Events
    click JCO "https://github.com/bytecodealliance/jco/tree/main/packages/jco"
    click SHIM "https://github.com/bytecodealliance/jco/tree/main/packages/preview2-shim"
    click JS_BINDGEN "https://github.com/bytecodealliance/jco/tree/main/crates/js-component-bindgen"
    click JS_BINDGEN_CMP "https://github.com/bytecodealliance/jco/tree/main/crates/js-component-bindgen-component"
    click WT_CMP_BUILD "https://github.com/bytecodealliance/jco/tree/main/crates/wasm-tools-component"
    click XTASK "https://github.com/bytecodealliance/jco/tree/main/crates/xtask"
    click BROWSER "https://github.com/bytecodealliance/jco/tree/main/packages/preview2-shim/lib/browser"
    click NODE "https://github.com/bytecodealliance/jco/tree/main/packages/preview2-shim/lib/nodejs"
    click EXAMPLES "https://github.com/bytecodealliance/jco/tree/main/examples"
    click DOCS "https://github.com/bytecodealliance/jco/tree/main/docs/src"
    click SCRIPTS "https://github.com/bytecodealliance/jco/tree/main/scripts"
    click WT_CMP "https://github.com/bytecodealliance/jco/tree/main/crates/wasm-tools-component"

    %% Styles
    classDef js fill:#ADD8E6,stroke:#000,color:#000
    classDef shim fill:#90EE90,stroke:#000,color:#000
    classDef wasm fill:#FFFF99,stroke:#000,color:#000
    classDef rust fill:#D3D3D3,stroke:#000,color:#000
    classDef external fill:#FFFFFF,stroke:#CCCCCC,color:#000
```
