{"modulePath":"github.com/dracory/nodebase","version":"v0.7.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package function\n\nPackage function contains the function node implementation.\n\n## Types\n\n```go\ntype FunctionConfig struct {\n\t// Func is the JavaScript source body. It is wrapped in\n\t// `function(msg, node, context, flow, global, env) { ... }` — return the msg to\n\t// pass it on, return null (or call node.send only) to control\n\t// outputs manually, or return an array for multi-output fan-out.\n\tFunc string `json:\"func\"`\n\t// Outputs declares the number of output ports (default 1). The\n\t// editor only draws a single static output today — extra ports are\n\t// reachable in exported/imported flows but need dynamic Drawflow\n\t// port support to be visible (documented limitation).\n\tOutputs int `json:\"outputs\"`\n\t// Timeout is the maximum script runtime in milliseconds\n\t// (0 = no timeout, the Node-RED default). Expiry interrupts the VM\n\t// via vm.Interrupt.\n\tTimeout float64 `json:\"timeout\"`\n\t// Initialize is the Node-RED \"On Start\" setup script — run once\n\t// when the node activates (deploy). Returning a promise delays\n\t// message processing until it settles (NR async-setup semantics).\n\tInitialize string `json:\"initialize\"`\n\t// Finalize is the Node-RED \"On Stop\" close script — run when the\n\t// node stops (undeploy/redeploy).\n\tFinalize string `json:\"finalize\"`\n}\n```\nFunctionConfig holds configuration for the function node — the Node-RED function equivalent running arbitrary JavaScript via goja.\n\n```go\ntype FunctionNode struct {\n\t*nodebase.BaseNode\n\t// contains filtered or unexported fields\n}\n```\nFunctionNode executes arbitrary JavaScript over the message — the Node-RED \\`function\\` node.\n\nSECURITY: 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.\n\n```go\nfunc NewFunctionNode(node nodestore.NodeInterface, config *FunctionConfig, host nodebase.NodeHost) *FunctionNode\n```\nNewFunctionNode creates a new function node\n\n```go\nfunc (n *FunctionNode) Activate(ctx context.Context) error\n```\nActivate 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.\n\n```go\nfunc (n *FunctionNode) Execute(ctx context.Context, input nodestore.MessageInterface) (nodestore.MessageInterface, error)\n```\nExecute 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.\n\n```go\nfunc (n *FunctionNode) SelectPorts(input, output nodestore.MessageInterface) []string\n```\nSelectPorts 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).\n\n```go\nfunc (n *FunctionNode) Stop() error\n```\nStop 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.\n\n```go\nfunc (n *FunctionNode) StopKey() string\n```\nStopKey dedupes stopper registration: executors are rebuilt per message, but only the activation executor should stop the node.\n\n```go\nfunc (n *FunctionNode) Validate() error\n```\nValidate checks the function node configuration\n\n","path":"github.com/dracory/nodebase/function","name":"function","synopsis":"Package function contains the function node implementation.","isRedistributable":true}
