Schema Compiler
The Sia schema compiler (@timeleap/sia-schema) is a Node.js CLI tool that lets you define message types and RPC plugins in .sia files, then generate serialization code for TypeScript, Go, Python, and C++. The compiler itself requires Node.js, but the generated code is native to each target language with no Node.js dependency.
Instead of writing addString8 / readString8 calls by hand, you define your data structures once and let the compiler produce the encode/decode functions.
When to Use the Schema Compiler
- You have many message types and writing paired serialize/deserialize functions by hand is tedious
- You want cross-language compatibility guaranteed by generating code from the same schema
- You're building RPC plugins for the Timeleap network
If you only have a few simple messages, writing the serialization code by hand is simpler and gives you full control.
Installation
npm install -g @timeleap/sia-schema
Or use it without installing:
npx @timeleap/sia-schema compile schema.sia -o schema.ts
The CLI binary is named sia.
A Quick Example
Given this schema file:
schema User {
name string8
age uint8
email string16
active bool
}
Run the compiler:
sia compile user.sia -o user.ts
The generated code includes paired encode/decode functions:
import { Sia } from "@timeleap/sia";
export interface User {
name: string;
age: number;
email: string;
active: boolean;
}
export function encodeUser(sia: Sia, user: User): Sia {
sia.addString8(user.name);
sia.addUInt8(user.age);
sia.addString16(user.email);
sia.addBool(user.active);
return sia;
}
export function decodeUser(sia: Sia): User {
return {
name: sia.readString8(),
age: sia.readUInt8(),
email: sia.readString16(),
active: sia.readBool(),
};
}
CLI Commands
sia compile
Compile a .sia file to a target language:
sia compile <file> [options]
| Option | Description |
|---|---|
-s, --string | Print generated code to stdout |
-o, --output <file> | Write generated code to file |
-e, --extension <ext> | Target language: ts, go, py, cpp |
If you don't specify -e, the compiler infers the target from the output file extension. If there's no extension, it auto-detects by looking for project files (tsconfig.json for TypeScript, go.sum for Go, pyproject.toml for Python, CMakeLists.txt for C++).
sia ir
Compile a .sia file and output the intermediate representation (JSON):
sia ir <file> [options]
| Option | Description |
|---|---|
-s, --string | Print formatted JSON to stdout |
-o, --output <file> | Write JSON to file |
This is useful for debugging schemas or building custom tooling on top of the IR.
Using the Generated Code
The generated code is ready to import and use — no glue code required:
import { Sia } from "@timeleap/sia";
import { encodeUser, decodeUser, type User } from "./user";
const alice: User = {
name: "Alice",
age: 30,
email: "alice@example.com",
active: true,
};
// Encode to bytes
const bytes = encodeUser(new Sia(), alice).toUint8Array();
// Decode from bytes
const decoded = decodeUser(new Sia(bytes));
console.log(decoded.name); // "Alice"
For a complete walkthrough including sending data over the wire and working with nested schemas, see the Schema Quick Start.
Sections
Repository
- Package:
@timeleap/sia-schema - Source: TimeleapLabs/sia-schema