Help us shape the future of Go! Take the 2026 Go Developer Survey

jsruntime

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 7 Imported by: 0

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

View Source
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

View Source
var ErrModule = errors.New("unsupported module syntax")

ErrModule marks unsupported ES module syntax.

View Source
var ErrNoFunction = errors.New("function not defined")

ErrNoFunction means the requested global function is not defined.

View Source
var ErrNotJSON = errors.New("value is not JSON-serializable")

ErrNotJSON means a value could not be converted to or from JSON.

Functions

func RewriteModule

func RewriteModule(code string, modules map[string]string) (string, error)

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 Kind

type Kind int

Kind classifies a script failure.

const (
	// KindOther is any failure that is neither a timeout nor a JS exception.
	KindOther Kind = iota
	// KindTimeout is a deadline interrupt.
	KindTimeout
	// KindException is an uncaught JS exception.
	KindException
)

func Classify

func Classify(err error) (Kind, string)

Classify returns the failure kind and its message (the thrown value's string form for exceptions).

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

type Sandbox struct {
	VM *goja.Runtime
	// contains filtered or unexported fields
}

Sandbox is one single-use goja VM with a deadline.

func New

func New(opts Options) *Sandbox

New creates a Sandbox; call Close when done.

func (*Sandbox) Call

func (s *Sandbox) Call(name string, args ...goja.Value) (goja.Value, error)

Call invokes the global function name with args.

func (*Sandbox) Close

func (s *Sandbox) Close()

Close stops the deadline timer.

func (*Sandbox) Elapsed

func (s *Sandbox) Elapsed() time.Duration

Elapsed is the time since the sandbox was created.

func (*Sandbox) FromJS

func (s *Sandbox) FromJS(v goja.Value) (any, error)

FromJS converts a JS value to plain JSON data; undefined and null give nil.

func (*Sandbox) Logs

func (s *Sandbox) Logs() []string

Logs returns the console lines captured so far.

func (*Sandbox) Rearm

func (s *Sandbox) Rearm(timeout time.Duration)

Rearm restarts the deadline and Elapsed clock with a fresh timeout, discarding any pending interrupt.

func (*Sandbox) Run

func (s *Sandbox) Run(src string) (goja.Value, error)

Run evaluates src and returns its completion value.

func (*Sandbox) Set

func (s *Sandbox) Set(name string, v any)

Set binds a Go value as a global.

func (*Sandbox) Stringify

func (s *Sandbox) Stringify(v goja.Value) (string, bool, error)

Stringify returns JSON.stringify(v); ok is false for undefined results.

func (*Sandbox) ToJS

func (s *Sandbox) ToJS(v any) (goja.Value, error)

ToJS builds a fresh JS value from plain Go data via JSON.parse.

func (*Sandbox) ToJSString

func (s *Sandbox) ToJSString(doc string) (goja.Value, error)

ToJSString parses a JSON document into a JS value.

Jump to

Keyboard shortcuts

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