Skip to main content

Component Model

TrailBase uses the WebAssembly Component Model, which provides:
  • Language interoperability: Components written in different languages can work together
  • Interface types: Rich type system with records, variants, and resources
  • Composition: Components can import and export interfaces
  • Versioning: Built-in support for API versioning

WIT (WebAssembly Interface Types)

Components are defined using WIT files that specify their interfaces. TrailBase provides the trailbase:component package:

Creating a Rust Component

1

Set up your project

Create a new library crate:
Add dependencies to Cargo.toml:
2

Implement the Guest trait

Create your component in src/lib.rs:
3

Build the component

Compile to WASM:
Convert to a component:
4

Deploy to TrailBase

Copy the component to your TrailBase data directory:
TrailBase will automatically load it on startup or restart.

Creating a TypeScript Component

1

Set up your project

Create a new project:
Configure package.json:
2

Write your component

Create src/index.ts:
3

Build configuration

Create build.js to compile TypeScript to WASM:
4

Build and deploy

Creating a JavaScript Component

JavaScript components use the same approach as TypeScript but without type annotations.

Component Initialization

Components implement the Guest trait with three optional methods:

Request Handling

HTTP Routes

Define routes with path parameters:

Query Parameters

Response Types

Multiple response types are supported:

Error Handling

Hot Reloading

In development mode, TrailBase can watch for component changes and reload them automatically.
Create a watcher script:

Next Steps

Custom Endpoints

Build HTTP endpoints with WASM

Server-Side Rendering

Render HTML dynamically

Jobs Scheduler

Create scheduled tasks

WASM Overview

Learn about the runtime