script

package
v0.47.1 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 22 Imported by: 1 module, 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

Index

Constants

View Source
const (
	// FingerprintText compares the text without // comments: fast, but another indentation or
	// spacing is another script
	FingerprintText = "text"
	// FingerprintTree compares the syntax tree goja parses: copies that differ in any formatting
	// match, at the cost of a parse per script
	FingerprintTree = "tree"
)

The ways to compute a fingerprint (SCRIPT_FINGERPRINT).

View Source
const MaxDepth = 5000

MaxDepth is the deepest syntax tree a script may have. goja's compiler recurses over the tree without a limit, and its parser limits nesting, not a chain like a+a+...+a: a few million terms overflow the stack of the goroutine, which ends the process, also when a model is only validated. Compiling a chain takes time quadratic in its length besides.

Variables

View Source
var ErrBlocked = errors.New("the script is blocked")

ErrBlocked is the error of a script on the blocklist.

View Source
var ErrTimeout = errors.New("script execution timeout")
View Source
var ErrTooDeep = fmt.Errorf("the script is nested more than %v levels deep", MaxDepth)

ErrTooDeep refuses a script whose syntax tree is deeper than MaxDepth.

View Source
var GojaPatches = goja.EnginePatches()

GojaPatches are the patches the engine applies to goja (third_party/goja-patches). Referring to them keeps the engine from building with goja unpatched, e.g. without the replace in go.mod.

View Source
var MaxNativeDepth int

MaxNativeDepth bounds the recursion of goja's own Go code over values a script builds (goja.SetMaxNativeDepth); zero is goja's default. Past it, the run ends with an error instead of a stack overflow that ends the process. Tests that need such a crash raise it.

Functions

func CachedFingerprint added in v0.31.0

func CachedFingerprint(src string) (string, bool)

CachedFingerprint returns the fingerprint of src if it is known already, without computing it.

func Check

func Check(src string) error

Check compiles the source without running it.

func Compile added in v0.31.0

func Compile(src string) (*goja.Program, error)

Compile compiles a script as goja.Compile, but without following a sourceMappingURL comment: goja would read the file it names before the script runs, without a limit, which makes file:///dev/zero end the process when its memory runs out, and before the crash brake knows. A script deeper than MaxDepth is refused before goja's compiler recurses over it.

The result is cached: a program is bound to no runtime and runs in several at once, so that a script is parsed and compiled once, not at every run. With tree fingerprints, the walk over the parsed script that measures the depth computes the fingerprint as well, which the blocklist then finds cached.

func Fingerprint added in v0.31.0

func Fingerprint(src string) string

Fingerprint identifies a script for the blocklist (EXPR-03), in the mode SetFingerprintMode chose: by its text without // comments (textFingerprint) or by its syntax tree (treeFingerprint). It is cached by a 64-bit hash of the source, not by the source, which a module script makes kilobytes long.

func JoinParts added in v0.31.0

func JoinParts(prefix string, inputs map[string]any) string

JoinParts joins the string inputs whose name starts with prefix, in the order of their names, as the pre- and postscripts of module tasks run (TASK-28).

func LenientJsonParse added in v0.7.1

func LenientJsonParse(vm *goja.Runtime) error

LenientJsonParse makes JSON.parse return a value that is no string as it is. Camunda handed some variables over as JSON text that the engine keeps as objects, e.g. the value of a device event, and models parse them; parsing an object would read "[object Object]".

func NewRuntime added in v0.31.0

func NewRuntime() *goja.Runtime

NewRuntime returns a goja runtime that compiles, also for eval and new Function, as Compile, and bounds its native recursion by MaxNativeDepth.

func Normalize

func Normalize(value any, maxBytes int) (any, error)

Normalize converts a Go value into its JSON form and enforces maxBytes if positive.

func SetFingerprintMode added in v0.31.0

func SetFingerprintMode(mode string) error

SetFingerprintMode chooses how Fingerprint compares scripts, FingerprintText or FingerprintTree; empty is FingerprintText. It is set once at the start, before any script is fingerprinted: entries of the blocklist computed in the other mode match only through their source.

func Truthy

func Truthy(value any) (bool, error)

Truthy reports whether a condition result selects a sequence flow; only booleans are accepted.

Types

type Assignment

type Assignment struct {
	Name  string
	Value any
}

type Blocklist added in v0.31.0

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

Blocklist holds the fingerprints of blocked scripts with the reason (EXPR-03). It is safe for concurrent use; a nil Blocklist blocks nothing, and an empty one computes no fingerprint.

func NewBlocklist added in v0.31.0

func NewBlocklist() *Blocklist

func (*Blocklist) Add added in v0.31.0

func (this *Blocklist) Add(fingerprint string, reason string)

Add blocks one more fingerprint.

func (*Blocklist) Blocked added in v0.31.0

func (this *Blocklist) Blocked(src string) (string, bool)

Blocked returns the reason a script is blocked for, if it is.

func (*Blocklist) Check added in v0.31.0

func (this *Blocklist) Check(src string) error

Check returns ErrBlocked for a blocked script.

func (*Blocklist) Replace added in v0.31.0

func (this *Blocklist) Replace(entries map[string]string)

Replace sets the entries, fingerprint to reason.

type Error

type Error struct {
	Err  error
	Logs []string
}

Error is a script failure; Logs holds what the script wrote before it failed.

func (*Error) Error

func (this *Error) Error() string

func (*Error) Unwrap

func (this *Error) Unwrap() error

type Result

type Result struct {
	// JSON compatible value: nil, bool, float64, string, []any or map[string]any (EXPR-05)
	Value       any
	Logs        []string
	Assignments []Assignment
}

type Sandbox

type Sandbox struct {
	Timeout        time.Duration
	MaxResultBytes int
	MaxLogEntries  int
	MaxCallStack   int
	// scripts that ended the engine; nil blocks nothing (EXPR-03)
	Blocklist *Blocklist
}

Sandbox evaluates JavaScript, the only expression and script language of the engine (EXPR-01). Scripts see the variables as globals plus log(...) and setVariable(name, value) (EXPR-04). They have no access to engine internals, the file system or the network, because goja provides none of these by default and nothing else is registered (EXPR-03).

goja has no memory limit. The timeout and the result size limit bound what a script can do, but a script can allocate memory until the timeout interrupts it.

func NewSandbox

func NewSandbox() *Sandbox

func (*Sandbox) Run

func (this *Sandbox) Run(src string, variables map[string]any) (result Result, err error)

Run evaluates src; the value of the last expression statement is the result.

Jump to

Keyboard shortcuts

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