Documentation
¶
Overview ¶
Package jsruntime is the shared sandboxed JavaScript runtime (goja) behind APPSYNC_JS, CloudFront Functions and Cosmos stored procedures. A Sandbox exposes only the ECMAScript builtins plus what the caller binds: no require, process, fs or network.
Index ¶
- Constants
- Variables
- func RewriteModule(code string, modules map[string]string) (string, error)
- type Kind
- type Options
- type Sandbox
- func (s *Sandbox) Call(name string, args ...goja.Value) (goja.Value, error)
- func (s *Sandbox) Close()
- func (s *Sandbox) Elapsed() time.Duration
- func (s *Sandbox) FromJS(v goja.Value) (any, error)
- func (s *Sandbox) Logs() []string
- func (s *Sandbox) Rearm(timeout time.Duration)
- func (s *Sandbox) Run(src string) (goja.Value, error)
- func (s *Sandbox) Set(name string, v any)
- func (s *Sandbox) Stringify(v goja.Value) (string, bool, error)
- func (s *Sandbox) ToJS(v any) (goja.Value, error)
- func (s *Sandbox) ToJSString(doc string) (goja.Value, error)
Constants ¶
const ( // DefaultTimeout bounds one script execution when Options.Timeout is zero. DefaultTimeout = 750 * time.Millisecond // DefaultMaxCallStack bounds JS recursion when Options.MaxCallStack is zero. DefaultMaxCallStack = 200 // DefaultMaxLogLines caps captured console lines when Options.MaxLogLines is zero. DefaultMaxLogLines = 100 )
Variables ¶
var ErrModule = errors.New("unsupported module syntax")
ErrModule marks unsupported ES module syntax.
var ErrNoFunction = errors.New("function not defined")
ErrNoFunction means the requested global function is not defined.
var ErrNotJSON = errors.New("value is not JSON-serializable")
ErrNotJSON means a value could not be converted to or from JSON.
Functions ¶
func RewriteModule ¶
RewriteModule turns ES module source into a plain script. modules maps an importable specifier to the global holding its exports; other specifiers are rejected. Default (import x from), namespace (import * as x) and named (import { a as b }) forms are supported; export keywords are dropped.
Types ¶
type Options ¶
type Options struct {
// InterruptMsg is the message carried by the timeout interrupt.
InterruptMsg string
// Timeout is the wall-clock limit for the sandbox lifetime.
Timeout time.Duration
// MaxCallStack is the maximum JS call depth.
MaxCallStack int
// MaxLogLines caps captured console lines.
MaxLogLines int
// Console installs a capturing console object when true.
Console bool
}
Options tunes a Sandbox.
type Sandbox ¶
Sandbox is one single-use goja VM with a deadline.
func (*Sandbox) FromJS ¶
FromJS converts a JS value to plain JSON data; undefined and null give nil.
func (*Sandbox) Rearm ¶
Rearm restarts the deadline and Elapsed clock with a fresh timeout, discarding any pending interrupt.