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 ¶
const ( DefaultTimeout = 5 * time.Second DefaultMaxOutputBytes = 16 << 20 // 16 MiB )
Defaults.
Variables ¶
var ErrTimeout = errors.New("function timed out")
ErrTimeout is returned when a function exceeds its time limit.
Functions ¶
func Normalise ¶
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 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 ¶
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.
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.