Vanya Agnesandra

Veilid Wasm API


Veilid | Veilid WASM

Getting Started2

The default export of veilid_wasm.js is bound to an async function called __wbg_init. Await this to initialize the wasm runtime, but don’t use its return value.

First, call initialize_veilid_wasm to set the panic hook - if veilid explodes, the output will go to the browser console.

Second, call initialize_veilid_core. This sets up logging, and takes a serialized (stringified) json config. Here’s a typedef:

type VeilidLogLevel = 
| 'Off'
| 'Error'
| 'Warn'
| 'Info'
| 'Debug'
| 'Trace';

type VeilidWASMConfig = {
    logging: {
        performance: {
            enabled: boolean;
            level: VeilidLogLevel;
            logs_in_timings: boolean;
            logs_in_console: boolean;
        };
        api: {
            enabled: boolean;
            level: VeilidLogLevel;
        };
    };
};

Third, await the async function startup_veilid_core. This takes a callback, and another serialized config. The config is huge, so store it in a separate file. The callback receives serialized json events - typedef below.

Fourth - call attach. This is non-async, non-blocking, and takes no parameters. This lets the core start talking on the network to find peers and do its thing.

When you’re done, it is polite, but not required, to call detach.

Events Typedef

type Event = LogEvent | ConfigEvent | AttachmentEvent | NetworkEvent;

type LogEvent = {
    kind: "Log";
    log_level: VeilidLogLevel;
    message: string;
    backtrace: string | null;
};

// ConfigEvents just mirror your config back at you

type AttachmentEvent = {
    kind: "Attachment";
    state: "Attached" | "Detached";
    public_internet_ready: boolean;
    local_network_ready: boolean;
};

type TransferStats = {
    // Numbers (bytes (per second)) as strings
    total: string;
    maximum: string;
    average: string;
    minimum: string;
};

type NetworkEvent = {
    kind: "Network";
    started: boolean;
    bps_down: string; // number as a string
    bps_up: string; // number as a string
    peers: {
        node_ids: string[];
        peer_address: string; // protocol:address:port
        peer_stats: {
            time_added: string; // microseconds
            rpc_stats: {
                messages_sent: number;
                messages_rcvd: number;
                questions_in_flight: number;
                last_question_ts: string; // microseconds
                last_seen_ts: string // microseconds
                first_consecutive_seen_ts: string;
                recent_lost_answers: number;
                failed_to_send: number;
            };
            latency: {
                // All Microseconds
                fastest: string;
                average: string;
                slowest: string;
            };
            transfer: {
                up: TransferStats;
                down: TransferStats;
            };
        }
    }[];
};

Endpoints

Exports start around line 240

Routing Contexts and DHT

There are a bunch of routing context functions. Looks like we need them for DHT.

Private Routes

App call reply?

Table DB stuff

Crypto Kinds

Meta Functions