WASM Module#

The core module that implements basic WASM functionality in Angie: this includes support for loading alternative runtimes and WASM modules, as well as configuring their features and limits.

The other modules in this section extend this functionality, allowing you to flexibly configure and optimize the WASM features for various scenarios and requirements.

In our repositories, the module is built dynamically and is available as a separate package named angie-module-wasm.

Configuration Example#

# These directives load the core functionality
load_module modules/ngx_wasm_module.so;
load_module modules/ngx_wasm_core_module.so;

load_module modules/ngx_wasmtime_module.so;

# Available here: https://git.angie.software/web-server/angie-wasm
load_module modules/ngx_http_wasm_host_module.so;
load_module modules/ngx_http_wasm_content_module.so;
load_module modules/ngx_http_wasm_vars_module.so;

events {

}


wasm_modules {

    #use wasmtime;

    load ngx_http_handler.wasm id=handler;
    load ngx_http_vars.wasm id=vars type=reactor;
}


http {

    wasm_var vars "ngx:wasi/var-utils#sum-entry" $rvar $arg_a $arg_b $arg_c $arg_d;

    server {

        listen *:8080;

        location / {

            return 200 "sum('$arg_a','$arg_b','$arg_c','$arg_d')=$rvar\n";
        }

        location /wasm {

            client_max_body_size 20M;
            wasm_content handler "ngx:wasi/http-handler-entry#handle-request";
        }
    }
}

Directives#

load#

Syntax

load file id=id [fs=host_path:guest_path]... [api=api]... [type=command | reactor]

Default

Context

wasm_modules

Loads a module from a disk file and assigns it an id (required). During loading, the module is verified to ensure it can be instantiated.

The directive has the following parameters:

fs

Allows the guest to access a directory on the host. Can be specified multiple times for different directories.

api

Enables a whitelist mode for APIs available to the module and specifies these APIs. If the module attempts to use restricted APIs (i.e. not listed here), an "API not found" error will be returned.

By default, the module has access to all APIs.

type

Controls the lifecycle of the loaded module.

  • In command mode, the machine runs once and its state is destroyed after execution.

  • In reactor mode, the machine effectively runs indefinitely, allowing multiple code executions. This requires careful memory management: if resources aren't cleaned up, memory leaks can occur.

wasm_modules#

Syntax

wasm_modules { ... };

Default

Context

main

A top-level block directive that provides the configuration file context in which the WASM directives should be specified. It can contain directives for loading WASM modules and configuring runtime-specific settings.