A minimal Dataflow programming engine
Notice that you must implement your own nodes. For example a node "addition" could be implemented using BigInt
or some arbitrary-precision library, according to your needs. You can find example nodes implementing basic JavaScript features in the examples/nodes/ folder.
A node is a block of code that can have inputs and outputs.
A link connects an input to an output.
A graph represents a program. It can contain nodes and links. Nodes are executed, sorted by their connections.
A Dflow
represents a program as an executable graph.
A graph can contain nodes and links. Nodes are executed, sorted by their connections.
Dflow
constructor requires a list of node definitions.
import { Dflow, type DflowNode } from "dflow";
// Node definition.
const helloWorld: DflowNode = {
kind: "hello",
run: () => console.log("Hello, World!")
}
// Create a dflow instance.
const dflow = new Dflow([helloWorld]);
// Add a node to the graph.
dflow.node("hello")
// Run the dflow graph.
dflow.run()
Create a new node. Returns node id.
Create a new link and connect two nodes. Returns link id.
If source or target position is omitted, then it defaults to 0
i.e. the first position.
Create a new data node. Returns node id.
If value is not a valid DflowData
, it will be set to undefined
.
Execute all nodes, sorted by their connections.
Delete node or link with given id.
A graph contains nodes and links.
A DflowGraph
has the following attributes:
node: Record<string, string>
link: Record<string, DflowLink>
data: Record<string, DflowData>
Get error messages from last run, indexed by node id.
Get output data of last run, indexed by node id.
Helper to define inputs.
Helper to define outputs.
Define an output named π (PI) for a constant node
const MathPI: DflowNode = {
kind: "mathPI",
outputs: [Dflow.output("number", { name: "π" })],
run: () => Math.PI
}
Includes JSON data types and undefined
The DflowData
can be one of the following:
undefined
null
boolean
number
string
DflowArray
DflowObject
The DflowDataType
is a literal type; it can be one of the following:
"null"
"boolean"
"number"
"string"
"array"
"object"
A DflowInput
has the following attributes:
name?: string
types: DflowDataType[]
optional?: boolean
A DflowOutput
has the following attributes:
name?: string
types: DflowDataType[]
Connects two nodes in the graph.
Defines a block of code: it can have inputs and outputs.
A DflowNode
has the following attributes:
kind: string
inputs?: DflowInput[]
outputs?: DflowOutput[]
run(inputs): outputs
Define a "sum" node.
import { Dflow, type DflowNode } from "dflow";
const Sum: DflowNode = {
kind: "sum",
inputs: [Dflow.input("number"), Dflow.input("number")];
outputs: [Dflow.output("number")];
run(a: number, b: number) {
return a + b;
}
}