js

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2026 License: Apache-2.0 Imports: 12 Imported by: 0 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 js runs the Function node's JavaScript.

Node-RED uses Node's own `vm` module, which its documentation states plainly is "not a security mechanism" and must not be used to run untrusted code. Its trust model is that anyone who can deploy a flow already owns the box — the exec node is right there — which is a defensible position for a tool you run on your own laptop and a poor one for something on a customer's plant floor.

goja is a JavaScript interpreter written in Go. It has no host bindings at all unless you add them: no process, no require, no filesystem, no network. The escape surface is not "small", it is the set of things deliberately put into the runtime by the code below. It is also pure Go, which is what keeps CGO_ENABLED=0 and the distroless image true.

What that costs: no npm. A Function node that requires a package does not work here and cannot be made to without embedding Node. That is stated in the compatibility matrix and never papered over.

Index

Constants

View Source
const (
	DefaultTimeout        = 5 * time.Second
	DefaultMaxOutputBytes = 16 << 20 // 16 MiB
)

Defaults.

Variables

View Source
var ErrTimeout = errors.New("function timed out")

ErrTimeout is returned when a function exceeds its time limit.

Functions

func Normalise

func Normalise(v any) any

Normalise converts values coming out of the JavaScript runtime into the shapes the rest of the engine uses.

goja exports an integral number as int64 and a fractional one as float64. Everything else in the runtime speaks JSON, where every number is a float64 — a message decoded from MQTT, a row read from PostgreSQL, a value loaded from the flow file. Letting a function node be the one place that emits int64 means a payload of 42 compares, serialises and stores differently depending on whether it passed through JavaScript, which is exactly the sort of difference nobody finds until it matters.

Types

type Limits

type Limits struct {
	// Timeout bounds wall-clock time per message. Node-RED's equivalent is
	// optional and off by default; here there is always a ceiling, because an
	// infinite loop in a Function node must not be able to hold a runtime
	// goroutine forever.
	Timeout time.Duration

	// MaxOutputBytes bounds the JSON size of the messages one call may emit.
	// A function that builds an ever-growing array is a slow OOM otherwise.
	MaxOutputBytes int
}

Limits bound what one invocation may do.

type LogEntry

type LogEntry struct {
	Level   node.LogLevel
	Message string
}

LogEntry is one line from the function.

type Program

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

Program is a compiled function body, shared by every invocation of one node.

Compiling once at deploy rather than per message matters: goja's parser is the expensive part, and a flow at a few thousand messages a second would spend most of its time re-parsing the same source.

func Compile

func Compile(name, body, onStart, onStop string, limits Limits) (*Program, error)

Compile prepares a function body.

The user's code is wrapped in a function expression so that `return` works at the top level, which is how every Node-RED function is written.

func (*Program) Run

func (p *Program) Run(ctx context.Context, sb Sandbox) (*Result, error)

Run executes the function against a message.

func (*Program) RunLifecycle

func (p *Program) RunLifecycle(ctx context.Context, sb Sandbox, which string) (*Result, error)

RunLifecycle executes the setup or cleanup code.

type Result

type Result struct {
	// Messages, indexed by output port. A nil entry sends nothing on that port.
	ByPort [][]*engine.Msg
	// Status set by the function, if any.
	Status *node.Status
	// Logs emitted through node.log and friends.
	Logs []LogEntry
	// Errors raised through node.error.
	Errors []string
	// Done reports whether the function called node.done().
	Done bool
}

Result is what one invocation produced.

type Sandbox

type Sandbox struct {
	Msg      *engine.Msg
	Services node.Services
	Outputs  int
}

Sandbox is the host state one invocation may reach.

Jump to

Keyboard shortcuts

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