pluginhost

package
v1.8.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package pluginhost embeds the Lesstruct WASM plugin host for use by programs that import Lesstruct as a library. A host builds a System from in-memory WASM bytes plus parsed manifests, scoped to one tenant, with its own database handle and HTTP client.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrHookNotFound           = errors.New("pluginhost: hook not found")
	ErrHookExecutionFailed    = errors.New("pluginhost: hook execution failed")
	ErrResourceLimitsExceeded = errors.New("pluginhost: plugin exceeded resource limits")
	ErrPluginInvalidFormat    = errors.New("pluginhost: invalid WASM format")
	ErrPluginLoadFailed       = errors.New("pluginhost: plugin load failed")
)

Functions

This section is empty.

Types

type Capabilities

type Capabilities struct {
	HTTP      []string        `toml:"http"`
	Database  []string        `toml:"database"`
	RateLimit RateLimitConfig `toml:"rate_limit"`
}

Capabilities mirrors the manifest capabilities section.

type DBExecutor

type DBExecutor interface {
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}

DBExecutor abstracts database access for host functions.

type FailureMode

type FailureMode int

FailureMode determines how hook failures are handled.

const (
	FailFast FailureMode = iota
	LogAndContinue
	Fallback

	DefaultFailureMode = FailFast
)

func ParseFailureMode

func ParseFailureMode(s string) FailureMode

ParseFailureMode parses a failure mode string.

func (FailureMode) String

func (f FailureMode) String() string

String returns FailureMode as a human-readable string.

type HookData

type HookData struct {
	ContentID    int            `json:"contentId"`
	UserID       int            `json:"userId"`
	Title        string         `json:"title"`
	Content      string         `json:"content"`
	Tags         []string       `json:"tags"`
	Status       string         `json:"status"`
	PostType     string         `json:"postType"`
	CustomFields map[string]any `json:"customFields,omitempty"`
}

HookData is the JSON payload sent to and received from plugin hooks.

type HookName

type HookName string

HookName identifies a content lifecycle hook.

const (
	HookBeforeSave     HookName = "BeforeSaveContent"
	HookAfterPublish   HookName = "AfterPublishContent"
	HookBeforeDelete   HookName = "BeforeDeleteContent"
	HookAfterCreate    HookName = "AfterCreateContent"
	HookAfterUnpublish HookName = "AfterUnpublishContent"
	HookOnPluginLoaded HookName = "OnPluginLoaded"

	DefaultPriority = 100
)

func (HookName) String

func (h HookName) String() string

String returns HookName as a string.

type HookOptions

type HookOptions struct {
	FailureMode FailureMode
	Priority    int
}

HookOptions controls hook registration priority and failure handling.

type HookResult

type HookResult struct {
	Title        *string        `json:"title"`
	CustomFields map[string]any `json:"customFields,omitempty"`
}

HookResult is the parse view of a before_save hook's returned JSON.

type HostConfig

type HostConfig struct {
	HTTPClient *http.Client
	DB         DBExecutor
}

HostConfig supplies per-tenant host resources.

type Manifest

type Manifest struct {
	Name         string       `toml:"name"`
	Version      string       `toml:"version"`
	MaxMemoryMB  int          `toml:"max_memory_mb"`
	Capabilities Capabilities `toml:"capabilities"`
}

Manifest declares a plugin's capabilities. MaxMemoryMB caps the plugin's linear memory and is enforced per plugin as the minimum of the tenant runtime limit and the manifest value when positive.

func ParseManifest

func ParseManifest(data []byte) (*Manifest, error)

ParseManifest parses a TOML capability manifest from bytes.

func (Manifest) HasDBPermission

func (m Manifest) HasDBPermission(perm string) bool

HasDBPermission checks whether Manifest allows a specific db permission.

func (Manifest) HasDatabase

func (m Manifest) HasDatabase() bool

HasDatabase reports whether Manifest declares database permissions.

func (Manifest) HasHTTP

func (m Manifest) HasHTTP() bool

HasHTTP reports whether Manifest declares HTTP patterns.

func (Manifest) IsHTTPURLAllowed

func (m Manifest) IsHTTPURLAllowed(url string) bool

IsHTTPURLAllowed checks whether url is covered by Manifest patterns.

func (Manifest) Validate

func (m Manifest) Validate() error

Validate checks that Manifest has required fields and valid permissions.

type PluginInfo

type PluginInfo struct {
	Name   string
	Status string
}

PluginInfo describes a loaded plugin.

type PluginSource

type PluginSource struct {
	Name     string
	Wasm     []byte
	Manifest *Manifest
}

PluginSource is one in-memory plugin plus its parsed manifest.

type RateLimitConfig

type RateLimitConfig struct {
	RequestsPerMinute int `toml:"requests_per_minute"`
}

RateLimitConfig is reserved for future use. RequestsPerMinute is parsed for forward compatibility but no rate limiting is enforced today.

type RuntimeConfig

type RuntimeConfig struct {
	MaxMemoryBytes   uint64
	MaxExecutionTime time.Duration
	Logger           func(string)
	CompilationCache wazero.CompilationCache
}

RuntimeConfig controls per-tenant WASM execution limits.

type System

type System struct {
	// contains filtered or unexported fields
}

System is a per-tenant plugin host built from in-memory sources.

func NewSystem

func NewSystem(
	ctx context.Context,
	rtConfig RuntimeConfig,
	hostConfig HostConfig,
	sources []PluginSource,
	hookOpts HookOptions,
) (*System, error)

NewSystem builds a per-tenant System from in-memory sources.

func (*System) Close

func (s *System) Close(ctx context.Context) error

Close releases all runtimes and unloads all plugins.

func (*System) Execute

func (s *System) Execute(
	ctx context.Context,
	hook HookName,
	data []byte,
) ([]byte, error)

Execute runs HookName with data through the registered chain.

func (*System) HasHook

func (s *System) HasHook(hook HookName) bool

HasHook reports whether HookName has any registered handler.

func (*System) Plugins

func (s *System) Plugins() []PluginInfo

Plugins returns the loaded plugins' metadata.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL