Documentation
¶
Overview ¶
Package function contains the function node implementation.
Index ¶
- type FunctionConfig
- type FunctionNode
- func (n *FunctionNode) Activate(ctx context.Context) error
- func (n *FunctionNode) Execute(ctx context.Context, input nodestore.MessageInterface) (nodestore.MessageInterface, error)
- func (n *FunctionNode) SelectPorts(input, output nodestore.MessageInterface) []string
- func (n *FunctionNode) Stop() error
- func (n *FunctionNode) StopKey() string
- func (n *FunctionNode) Validate() error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FunctionConfig ¶
type FunctionConfig struct {
// Func is the JavaScript source body. It is wrapped in
// `function(msg, node, context, flow, global, env) { ... }` — return the msg to
// pass it on, return null (or call node.send only) to control
// outputs manually, or return an array for multi-output fan-out.
Func string `json:"func"`
// Outputs declares the number of output ports (default 1). The
// editor only draws a single static output today — extra ports are
// reachable in exported/imported flows but need dynamic Drawflow
// port support to be visible (documented limitation).
Outputs int `json:"outputs"`
// Timeout is the maximum script runtime in milliseconds
// (0 = no timeout, the Node-RED default). Expiry interrupts the VM
// via vm.Interrupt.
Timeout float64 `json:"timeout"`
// Initialize is the Node-RED "On Start" setup script — run once
// when the node activates (deploy). Returning a promise delays
// message processing until it settles (NR async-setup semantics).
Initialize string `json:"initialize"`
// Finalize is the Node-RED "On Stop" close script — run when the
// node stops (undeploy/redeploy).
Finalize string `json:"finalize"`
}
FunctionConfig holds configuration for the function node — the Node-RED function equivalent running arbitrary JavaScript via goja.
type FunctionNode ¶
FunctionNode executes arbitrary JavaScript over the message — the Node-RED `function` node.
SECURITY: the script is arbitrary user code. goja is a safe baseline: it is pure Go (cgo-free, matching the project constraint) and exposes NO filesystem/network/process access by default — the only reachable host surface is env.get (Node-RED parity: environment variables are intentionally readable, e.g. for credentials; do not run untrusted flows on hosts whose env carries secrets) and the context API — context/flow/global get/set backed by the host's ContextStore.
func NewFunctionNode ¶
func NewFunctionNode(node nodestore.NodeInterface, config *FunctionConfig, host nodebase.NodeHost) *FunctionNode
NewFunctionNode creates a new function node
func (*FunctionNode) Activate ¶ added in v0.7.0
func (n *FunctionNode) Activate(ctx context.Context) error
Activate implements nodebase.Activator: runs the Node-RED "On Start" (initialize) script on deploy. A returned promise is awaited — the script's setTimeout callbacks drive it — so DeployWorkflow blocks until async setup settles, matching Node-RED. The state pointer is cached for Stop because host state is wiped before stoppers run.
func (*FunctionNode) Execute ¶
func (n *FunctionNode) Execute(ctx context.Context, input nodestore.MessageInterface) (nodestore.MessageInterface, error)
Execute runs the configured script against the incoming message: msg.payload maps to Payload, msg._msgid maps to nodestore.MessageInterface.ID (Node-RED compat), remaining msg keys merge into Metadata. A non-null return value counts as a send on output_1; node.send pushes (port, msg) pairs — array indices select the output port. Nothing sent → the message is dropped (nil, nil). Additional sends beyond the first per port are emitted asynchronously port-scoped, so each output port delivers its own payload downstream.
func (*FunctionNode) SelectPorts ¶
func (n *FunctionNode) SelectPorts(input, output nodestore.MessageInterface) []string
SelectPorts restricts outgoing edges to the ports that received a first message — consistent with the payload Execute returns (the first send on the lowest-indexed port).
func (*FunctionNode) Stop ¶ added in v0.7.0
func (n *FunctionNode) Stop() error
Stop implements nodebase.Stopper: cancels pending script timers and runs the "On Stop" (finalize) script. It must not re-resolve host state — the cached pointer from Activate is the only safe reference.
func (*FunctionNode) StopKey ¶ added in v0.7.0
func (n *FunctionNode) StopKey() string
StopKey dedupes stopper registration: executors are rebuilt per message, but only the activation executor should stop the node.
func (*FunctionNode) Validate ¶
func (n *FunctionNode) Validate() error
Validate checks the function node configuration