Using WASI interfaces from JavaScript
Golem implements and exports a subset of the WASI (opens in a new tab) interfaces, as well as its own runtime interfaces.
The Golem JavaScript SDK provides idiomatic wrappers on a subset of these interfaces, but it is also possible to use the generated bindings directly.
WIT specifications
The full set of WIT specifications Golem implements is available in the following public repository:
https://github.com/golemcloud/golem-wit/tree/main/wit/deps (opens in a new tab)
The following table lists all packages provided by Golem:
Package | Description |
---|---|
golem:api | Golem's Runtime API |
golem:rpc | Provides support for Worker to Worker communication |
wasi:blobstore | Interface for storing and retrieving large binary data |
wasi:cli | Interface for environment variables and standard I/O |
wasi:clocks | Interface for querying the system time |
wasi:filesystem | Interface for working with files and directories |
wasi:http | Interface for making HTTP requests |
wasi:io | Interface for working with futures and streams |
wasi:keyvalue | Interface for storing and retrieving key-value pairs - only partially implemented |
wasi:logging | Interface for logging messages |
wasi:random | Interface for generating random numbers |
wasi:sockets | Interface for working with TCP and UDP sockets (currently not supporting durable execution) |
Bindings
The JavaScript bindings for the WASI and Golem-specific interfaces are provided in the golem:api/host@0.2.0
module. You can import and use these bindings in your JavaScript code.
Additional Golem runtime APIs
This section describes Golem-specific functionalities which are available through the Golem runtime API.
Generate an idempotency key
Golem provides a function to generate an idempotency key (a UUID) which can be passed to external systems to ensure that the same request is not processed multiple times.
It is guaranteed that this idempotency key will always be the same (per occurrence) even if the worker is restarted due to a crash.
To generate an idempotency key:
import { generateIdempotencyKey } from "golem:api/host@1.1.0"
const key = generateIdempotencyKey()
Get worker metadata
It is possible to query metadata for Golem workers. This metadata includes information such as worker ID, arguments, environment variables, status, component version, and retry count.
There are two exported functions to query worker metadata:
getSelfMetadata()
returns the metadata for the current workergetWorkerMetadata(workerId)
returns the metadata for a specific worker given by itsworkerId
Enumerate workers
Worker enumeration is a feature of Golem available both through the public HTTP API and using the WIT interfaces.
Enumerating workers of a component is a slow operation and should not be used as part of the application logic.
The following example demonstrates how to use the worker enumeration API:
import { GetWorkers } from "golem:api/host@1.1.0"
const componentId = {
/* ... */
}
const filter = {
filters: [
{
filters: [
{
tag: "status",
val: {
comparator: "equal",
value: "idle",
},
},
],
},
],
}
const workers = []
const getter = new GetWorkers(componentId, filter, true)
let batch
while ((batch = getter.getNext()) !== undefined) {
workers.push(...batch)
}
console.log("Retrieved workers:", workers)
The third parameter of the GetWorkers
constructor enables precise
mode. In this mode, Golem will calculate the latest metadata for each returned worker; otherwise, it uses only the last cached values.
Update a worker
To trigger an update for a given worker from one component version to another, use the updateWorker
function:
import { updateWorker } from "golem:api/host@1.1.0"
const workerId = {
/* ... */
}
const targetVersion = 1n
updateWorker(workerId, targetVersion, "automatic")
To learn more about updating workers, see the updating workers page in the documentation.
The WASI Key-Value store interface
Although Golem workers can store their state completely in their own memory, it is possible to use the wasi:keyvalue
interface to store key-value pairs in a Golem managed key value storage.
This can be useful if state needs to be shared between different workers or if the size of this state is too large to be stored in memory.
The WASI Blob Store interface
The wasi:blobstore
interface provides a way to store and retrieve large binary data. This can be useful for storing large files or other binary data that is too large to be stored in the worker's memory.