Documentation
¶
Index ¶
- func WithCallerContext(ctx context.Context, cc CallerContext) context.Context
- func WithFlagContext(ctx context.Context, flagCtx string) context.Context
- type CallerContext
- type Engine
- func (e *Engine) Close(ctx context.Context)
- func (e *Engine) Invoke(ctx context.Context, wasmPath string, timeout time.Duration) ([]byte, error)
- func (e *Engine) InvokeWASI(ctx context.Context, wasmBytes []byte, timeout time.Duration, deps *HostDeps, ...) ([]byte, error)
- func (e *Engine) InvokeWASIStream(ctx context.Context, wasmBytes []byte, timeout time.Duration, deps *HostDeps, ...) error
- type Function
- type FunctionInvocation
- type FunctionVersion
- type HostDeps
- type Store
- func (s *Store) Create(ctx context.Context, name, description, language, source string) (*Function, error)
- func (s *Store) Delete(ctx context.Context, id string) error
- func (s *Store) DeployVersion(ctx context.Context, id string, wasmBytes []byte) (*FunctionVersion, error)
- func (s *Store) Get(ctx context.Context, id string) (*Function, error)
- func (s *Store) GetVersion(ctx context.Context, functionID, versionID string) (*FunctionVersion, error)
- func (s *Store) Invoke(ctx context.Context, id string, timeout time.Duration) ([]byte, error)
- func (s *Store) InvokeStream(ctx context.Context, id string, timeout time.Duration, w io.Writer) error
- func (s *Store) InvokeStreamWithEvent(ctx context.Context, id string, timeout time.Duration, eventData []byte, ...) error
- func (s *Store) InvokeWithEvent(ctx context.Context, id string, timeout time.Duration, eventData []byte) ([]byte, error)
- func (s *Store) List(ctx context.Context) ([]Function, error)
- func (s *Store) ListInvocations(ctx context.Context, fnID string) ([]FunctionInvocation, error)
- func (s *Store) ListVersions(ctx context.Context, functionID string) ([]FunctionVersion, error)
- func (s *Store) Update(ctx context.Context, id, name, description, source string) (*Function, error)
- func (s *Store) Upload(ctx context.Context, name, description string, wasmBytes []byte) (*Function, error)
- func (s *Store) WithTables(t *table.Engine)
- func (s *Store) WithTracer(t *tracing.Recorder)
- type WASIMetrics
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithCallerContext ¶
func WithCallerContext(ctx context.Context, cc CallerContext) context.Context
WithCallerContext injects a CallerContext into the execution context.
Types ¶
type CallerContext ¶
type CallerContext struct {
UserID string `json:"user_id"`
Role string `json:"role"`
TenantID string `json:"tenant_id"`
Email string `json:"email"`
Groups []string `json:"groups"`
}
CallerContext holds the identity of the user who triggered a function invocation.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine wraps a Wazero runtime for executing sandboxed WASM functions. Each Invoke call instantiates a fresh module with a hard execution deadline, preventing runaway functions from starving shared CPU resources.
func NewEngine ¶
NewEngine creates an Engine. The caller must call Close when done. WithCloseOnContextDone enables hard interruption of tight WASM loops when the execution context deadline is exceeded.
func (*Engine) Invoke ¶
func (e *Engine) Invoke(ctx context.Context, wasmPath string, timeout time.Duration) ([]byte, error)
Invoke loads, instantiates, and calls the exported "handle" function in a WASM module at wasmPath. The execution is bounded by timeout to guard against infinite loops in user-supplied functions.
func (*Engine) InvokeWASI ¶
func (e *Engine) InvokeWASI(ctx context.Context, wasmBytes []byte, timeout time.Duration, deps *HostDeps, m *WASIMetrics) ([]byte, error)
InvokeWASI loads and runs a WASI preview1 WASM module. The module's main() is called automatically on instantiation; stdout is captured and returned. m is populated with compute metrics if non-nil.
func (*Engine) InvokeWASIStream ¶
func (e *Engine) InvokeWASIStream(ctx context.Context, wasmBytes []byte, timeout time.Duration, deps *HostDeps, w io.Writer, m *WASIMetrics) error
InvokeWASIStream runs a WASI WASM module and writes stdout directly to w as bytes arrive, enabling real-time streaming to callers. m is populated with compute metrics if non-nil.
type Function ¶
type Function struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Language string `json:"language"`
Source string `json:"source"`
Runtime string `json:"runtime"`
Status string `json:"status"`
Error string `json:"error,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
Function is a stored, potentially compiled, user function.
type FunctionInvocation ¶
type FunctionInvocation struct {
ID string `json:"id"`
FunctionID string `json:"function_id"`
StartedAt time.Time `json:"started_at"`
CompletedAt *time.Time `json:"completed_at,omitempty"`
Success bool `json:"success"`
Output string `json:"output,omitempty"`
Error string `json:"error,omitempty"`
ExecutionMs *int64 `json:"execution_ms,omitempty"`
PeakMemoryBytes int64 `json:"peak_memory_bytes"`
HostCalls int `json:"host_calls"`
OutputSizeBytes int `json:"output_size_bytes"`
TraceID string `json:"trace_id,omitempty"`
}
FunctionInvocation records a single execution of a function.
type FunctionVersion ¶
type FunctionVersion struct {
ID string `json:"id"`
FunctionID string `json:"function_id"`
Version int `json:"version"`
Source string `json:"source,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
FunctionVersion records a single deployment of a function.
type HostDeps ¶
type HostDeps struct {
Storage storage.BucketAdapter
Flags *feature.Engine
Store *Store // for fn_invoke; may be nil to break init cycle
Tables *table.Engine // may be nil when tables feature is not wired
Tracer *tracing.Recorder
DB *sql.DB // for custom metrics; may be nil
FnID string // current function ID (for metric tagging)
InvID string // current invocation ID (for metric tagging)
TraceID string // current trace ID propagated from context
}
HostDeps are the flagbase services exposed to WASM functions via the "flagbase" host module.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store manages function lifecycle: persistence, compilation, and invocation.
func (*Store) Create ¶
func (s *Store) Create(ctx context.Context, name, description, language, source string) (*Function, error)
Create persists a new JavaScript function record and validates it synchronously. For Go/WASM functions, use Upload instead.
func (*Store) DeployVersion ¶
func (s *Store) DeployVersion(ctx context.Context, id string, wasmBytes []byte) (*FunctionVersion, error)
DeployVersion uploads a new WASM artifact for an existing function, overwrites the active artifact, and records a new version entry.
func (*Store) GetVersion ¶
func (s *Store) GetVersion(ctx context.Context, functionID, versionID string) (*FunctionVersion, error)
GetVersion returns a single version record by ID, including source snapshot.
func (*Store) InvokeStream ¶
func (s *Store) InvokeStream(ctx context.Context, id string, timeout time.Duration, w io.Writer) error
InvokeStream executes a ready function, writing its stdout output to w as bytes arrive. This enables real-time streaming to HTTP clients.
func (*Store) InvokeStreamWithEvent ¶
func (s *Store) InvokeStreamWithEvent(ctx context.Context, id string, timeout time.Duration, eventData []byte, w io.Writer) error
InvokeStreamWithEvent executes a function with an event payload, streaming stdout to w in real-time.
func (*Store) InvokeWithEvent ¶
func (s *Store) InvokeWithEvent(ctx context.Context, id string, timeout time.Duration, eventData []byte) ([]byte, error)
InvokeWithEvent executes a function with an event payload readable via the event_read host call.
func (*Store) ListInvocations ¶
ListInvocations returns the 100 most recent invocations for a function.
func (*Store) ListVersions ¶
ListVersions returns all deployment versions for a function, oldest first.
func (*Store) Update ¶
func (s *Store) Update(ctx context.Context, id, name, description, source string) (*Function, error)
Update recompiles a JavaScript function from new source and stores a new version.
func (*Store) Upload ¶
func (s *Store) Upload(ctx context.Context, name, description string, wasmBytes []byte) (*Function, error)
Upload persists a pre-compiled WASM artifact. The caller is responsible for compiling Go source to WASI preview1 WASM (e.g. via `flagbase fn build`).
func (*Store) WithTables ¶
WithTables injects the table engine so WASM functions can call table host functions.
func (*Store) WithTracer ¶
WithTracer injects the distributed tracing recorder.
type WASIMetrics ¶
WASIMetrics holds performance counters captured during a single WASM invocation.