functions

package
v0.99.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2026 License: GPL-3.0 Imports: 20 Imported by: 0

Documentation

Overview

Package functions is the deep module for named function apply and invoke.

Index

Constants

View Source
const (
	// MaxSourceBytes rejects function source above this size (256KiB).
	MaxSourceBytes = 256 << 10
	// MaxJSONBytes is the function JSON in/out limit (64KiB).
	MaxJSONBytes = 64 << 10
	// DefaultTimeout limits a single function invocation.
	DefaultTimeout = 60 * time.Second
	// DefaultMaxConcurrency is the global in-flight invoke limit.
	DefaultMaxConcurrency = 4
)

Variables

This section is empty.

Functions

func AuthenticateCall

func AuthenticateCall(meta *Metadata, headerToken, queryToken, hmacSig string, body []byte) bool

AuthenticateCall validates a function HTTP call using metadata token and/or HMAC. Token auth accepts headerToken (X-Webhook-Token) or queryToken. HMAC uses X-Hub-Signature-256 style "sha256=<hex>" signatures over the raw body.

func LoadDir

func LoadDir(path string) (metadataYAML, entrypoint, source string, err error)

LoadDir loads a function directory artifact (metadata.yaml + exactly one entrypoint).

func ParseStdoutJSON

func ParseStdoutJSON(stdout string) (any, error)

ParseStdoutJSON trims stdout and decodes exactly one JSON value (≤ MaxJSONBytes).

func SetActiveService

func SetActiveService(svc *Service)

SetActiveService wires the package-level Service used by HTTP modules.

func ValidateMetadata

func ValidateMetadata(meta *Metadata) error

ValidateMetadata requires a valid name and token and/or hmac_secret.

func ValidateName

func ValidateName(name string) error

ValidateName reports whether name is a valid function identifier.

func VerifyHMACSHA256

func VerifyHMACSHA256(secret string, body []byte, signature string) bool

VerifyHMACSHA256 verifies an HMAC-SHA256 signature against the body. Accepts signatures of the form "sha256=<hex>" (case-insensitive prefix).

Types

type ApplyResult

type ApplyResult struct {
	Name    string `json:"name"`
	ID      int64  `json:"id"`
	Version int    `json:"version"`
	Status  string `json:"status"`
}

ApplyResult is returned after applying a function directory or bundle.

type Catalog

type Catalog interface {
	Create(ctx context.Context, name, metadata, entrypoint, source, createdBy string) error
	GetByName(ctx context.Context, name string) (*model.FunctionDefinition, error)
	UpdateDraft(ctx context.Context, name, metadata, entrypoint, source string, version int) (*model.FunctionDefinition, error)
	Publish(ctx context.Context, name string, version int) (*model.FunctionDefinition, error)
	Delete(ctx context.Context, name string) (int64, error)
	ListPublished(ctx context.Context) ([]*model.FunctionDefinition, error)
	ListAll(ctx context.Context) ([]*model.FunctionDefinition, error)
	GetVersion(ctx context.Context, name string, version int) (*model.FunctionDefinitionVersion, error)
	GetLatestPublished(ctx context.Context, name string) (*model.FunctionDefinitionVersion, error)
	CreateRun(ctx context.Context, name string, version int, idempotencyKey *string) (*model.FunctionRun, error)
	GetRunByIdempotency(ctx context.Context, name, key string) (*model.FunctionRun, error)
	CompleteRun(ctx context.Context, id int64, status string, durationMs int64, exitCode *int, errMsg string, resultJSON *string) (*model.FunctionRun, error)
	ListRuns(ctx context.Context, name string) ([]*model.FunctionRun, error)
}

Catalog loads and mutates named function definitions and runs.

type DraftView

type DraftView struct {
	Name                  string            `json:"name"`
	Status                string            `json:"status"`
	Version               int               `json:"version"`
	Entrypoint            string            `json:"entrypoint"`
	Source                string            `json:"source"`
	Env                   map[string]string `json:"env,omitempty"`
	Token                 string            `json:"token"`
	TokenSet              bool              `json:"token_set"`
	HMACSecret            string            `json:"hmac_secret"`
	HMACSet               bool              `json:"hmac_set"`
	PublishedVersion      *int              `json:"published_version,omitempty"`
	HasUnpublishedChanges bool              `json:"has_unpublished_changes"`
}

DraftView is a redacted draft snapshot for the management editor.

type ExecProvider

type ExecProvider interface {
	ExecConfig(ctx context.Context) (pkgexec.Config, error)
}

ExecProvider supplies an execution Config for ephemeral function runs.

type ExportBundle

type ExportBundle struct {
	Metadata   string `json:"metadata"`
	Entrypoint string `json:"entrypoint"`
	Source     string `json:"source"`
	Version    int    `json:"version"`
	Name       string `json:"name"`
}

ExportBundle is a published function snapshot including secrets.

type HTTPAuth

type HTTPAuth struct {
	Token      string `yaml:"token" json:"token,omitempty"`
	HMACSecret string `yaml:"hmac_secret" json:"hmac_secret,omitempty"`
}

HTTPAuth holds function HTTP authentication secrets.

type HTTPConfig

type HTTPConfig struct {
	Auth HTTPAuth `yaml:"auth" json:"auth"`
}

HTTPConfig holds function HTTP settings.

type InvokeRequest

type InvokeRequest struct {
	Name           string
	Version        *int
	Event          any
	IdempotencyKey string
	// RequireVersion rejects calls that omit Version (pipeline / capability path).
	RequireVersion bool
}

InvokeRequest configures one function invocation.

type InvokeResult

type InvokeResult struct {
	Name     string `json:"name"`
	Version  int    `json:"version"`
	RunID    int64  `json:"run_id"`
	Status   string `json:"status"`
	Result   any    `json:"result,omitempty"`
	Error    string `json:"error,omitempty"`
	ExitCode *int   `json:"exit_code,omitempty"`
	Replayed bool   `json:"replayed,omitempty"`
}

InvokeResult is the outcome of a successful or replayed invoke.

type ListAllInfo

type ListAllInfo struct {
	Name                  string `json:"name"`
	Status                string `json:"status"`
	Version               int    `json:"version"`
	PublishedVersion      *int   `json:"published_version,omitempty"`
	HasUnpublishedChanges bool   `json:"has_unpublished_changes"`
}

ListAllInfo is a function summary for management list UIs (draft + published).

type ListInfo

type ListInfo struct {
	Name    string `json:"name"`
	Version int    `json:"version"`
	Status  string `json:"status"`
}

ListInfo is a published function summary for list APIs.

type Metadata

type Metadata struct {
	Name string            `yaml:"name" json:"name"`
	HTTP HTTPConfig        `yaml:"http" json:"http"`
	Env  map[string]string `yaml:"env" json:"env,omitempty"`
}

Metadata is the function directory metadata.yaml document.

func ParseMetadataYAML

func ParseMetadataYAML(data string) (*Metadata, error)

ParseMetadataYAML unmarshals and validates function metadata YAML.

type Service

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

Service orchestrates DB-backed function apply/invoke.

func ActiveService

func ActiveService() *Service

ActiveService returns the wired Service, or nil if not set.

func NewService

func NewService(catalog Catalog, execProvider ExecProvider) *Service

NewService creates a function Service with default concurrency.

func NewServiceWithLimits

func NewServiceWithLimits(catalog Catalog, execProvider ExecProvider, maxConcurrent int) *Service

NewServiceWithLimits creates a function Service with a custom global concurrency limit.

func (*Service) ApplyBundle

func (s *Service) ApplyBundle(ctx context.Context, metadata, entrypoint, source, createdBy string) (*ApplyResult, error)

ApplyBundle validates metadata + entrypoint source, writes draft, and publishes.

func (*Service) ApplyDir

func (s *Service) ApplyDir(ctx context.Context, dir, createdBy string) (*ApplyResult, error)

ApplyDir loads a function directory, writes draft, and publishes immediately.

func (*Service) Create

func (s *Service) Create(ctx context.Context, name, entrypoint, createdBy string) (*DraftView, error)

Create creates a draft-only function with a generated HTTP token and stub source.

func (*Service) Delete

func (s *Service) Delete(ctx context.Context, name string) error

Delete removes a function definition.

func (*Service) Export

func (s *Service) Export(ctx context.Context, name string) (*ExportBundle, error)

Export returns published metadata+entrypoint+source including secrets.

func (*Service) ExportVersion

func (s *Service) ExportVersion(ctx context.Context, name string, version int) (*ExportBundle, error)

ExportVersion returns a specific published version snapshot including secrets.

func (*Service) Get

func (s *Service) Get(ctx context.Context, name string) (map[string]any, error)

Get returns published function metadata (including secrets) for management APIs.

func (*Service) GetDraft

func (s *Service) GetDraft(ctx context.Context, name string) (*DraftView, error)

GetDraft returns a redacted draft for the management editor.

func (*Service) GetPublic

func (s *Service) GetPublic(ctx context.Context, name string, version *int) (map[string]any, error)

GetPublic returns published function metadata without auth secrets or source.

func (*Service) Invoke

func (s *Service) Invoke(ctx context.Context, req InvokeRequest) (*InvokeResult, error)

Invoke runs a published function version with concurrency and idempotency controls.

func (*Service) LatestPublishedVersion

func (s *Service) LatestPublishedVersion(ctx context.Context, name string) (int, error)

LatestPublishedVersion returns the version number of the latest published snapshot.

func (*Service) List

func (s *Service) List(ctx context.Context) ([]ListInfo, error)

List returns published function summaries.

func (*Service) ListAll

func (s *Service) ListAll(ctx context.Context) ([]ListAllInfo, error)

ListAll returns draft and published function summaries for management UIs.

func (*Service) ListRuns

func (s *Service) ListRuns(ctx context.Context, name string) ([]*model.FunctionRun, error)

ListRuns returns recent runs for a function name.

func (*Service) Publish

func (s *Service) Publish(ctx context.Context, name string, expectedVersion int) (*ApplyResult, error)

Publish publishes the current draft with optimistic locking.

func (*Service) PublishedMetadata

func (s *Service) PublishedMetadata(ctx context.Context, name string, version *int) (*Metadata, error)

PublishedMetadata loads published function metadata for HTTP call authentication.

func (*Service) Ready

func (s *Service) Ready() bool

Ready reports whether the service has a catalog and exec provider configured.

func (*Service) SaveDraft

func (s *Service) SaveDraft(ctx context.Context, name, metadata, entrypoint, source string, expectedVersion int) (*DraftView, error)

SaveDraft merges secrets, validates, and updates draft with optimistic locking.

func (*Service) SetChecker

func (s *Service) SetChecker(c dcg.Checker)

SetChecker overrides the dcg checker (nil uses dcg.DefaultChecker).

Jump to

Keyboard shortcuts

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