Workflow base code of n8n providing foundational workflow execution engine for automation platform
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Core workflow creation, execution, and management functionality including node orchestration, data flow control, and workflow lifecycle management.
Main workflow execution engine that manages node execution, data flow, and workflow state.
/**
* Main workflow execution engine class
*/
class Workflow {
id?: string;
name: string;
nodes: INode[];
connections: IConnections;
active: boolean;
nodeTypes: INodeTypes;
staticData: IDataObject;
settings: IWorkflowSettings;
constructor(parameters: IWorkflowBase);
/**
* Execute the workflow
* @param mode - Execution mode (manual, trigger, etc.)
* @param startNode - Optional starting node name
* @returns Execution result data
*/
execute(
mode: WorkflowExecuteMode,
startNode?: string
): Promise<IRunExecutionData>;
/**
* Get a specific node by name
* @param nodeName - Name of the node to retrieve
* @returns Node object or null if not found
*/
getNode(nodeName: string): INode | null;
/**
* Get all nodes in the workflow
* @returns Array of all nodes
*/
getNodes(): INode[];
/**
* Get all connections in the workflow
* @returns Connections object
*/
getConnections(): IConnections;
/**
* Check if workflow is active
* @returns Boolean indicating if workflow is active
*/
isActive(): boolean;
/**
* Rename a node and update all references
* @param currentName - Current node name
* @param newName - New node name
*/
renameNode(currentName: string, newName: string): void;
}Core interfaces for defining workflow structure and behavior.
interface IWorkflowBase {
id?: string;
name: string;
nodes: INode[];
connections: IConnections;
active: boolean;
nodeTypes: INodeTypes;
staticData?: IDataObject;
settings?: IWorkflowSettings;
}
interface INode {
id?: string;
name: string;
type: string;
typeVersion: number;
position: [number, number];
parameters: INodeParameters;
credentials?: INodeCredentialsDetails;
disabled?: boolean;
notes?: string;
continueOnFail?: boolean;
alwaysOutputData?: boolean;
executeOnce?: boolean;
retryOnFail?: boolean;
maxTries?: number;
waitBetweenTries?: number;
onError?: string;
}
interface IConnections {
[key: string]: {
[type: string]: IConnection[][];
};
}
interface IConnection {
node: string;
type: NodeConnectionType;
index: number;
}
interface IWorkflowSettings {
timezone?: string;
saveDataErrorExecution?: string;
saveDataSuccessExecution?: string;
saveManualExecutions?: boolean;
callerPolicy?: string;
errorWorkflow?: string;
executionTimeout?: number;
}Interfaces for defining node types and their parameters.
interface INodeTypes {
nodeTypes: {
[key: string]: INodeType;
};
init?(nodeTypes?: INodeTypes): Promise<void>;
}
interface INodeType {
description: INodeTypeDescription;
execute?(this: IExecuteFunctions): Promise<INodeExecutionData[][]>;
poll?(this: IPollFunctions): Promise<INodeExecutionData[][] | null>;
trigger?(this: ITriggerFunctions): Promise<ITriggerResponse | undefined>;
webhook?(this: IWebhookFunctions): Promise<IWebhookResponseData>;
}
interface INodeParameters {
[key: string]:
| string
| number
| boolean
| undefined
| null
| IDataObject
| INodeParameters
| INodeParameters[]
| NodeParameterValue
| NodeParameterValue[];
}Data structures for workflow and node execution results.
interface IRunExecutionData {
resultData: IRunData;
startData: IStartRunData;
executionData?: IExecutionData;
}
interface IRunData {
[key: string]: ITaskData[];
}
interface ITaskData {
startTime: number;
executionTime: number;
executionStatus?: ExecutionStatus;
data?: ITaskDataConnections;
error?: ExecutionError;
source: Array<ISourceData | null> | null;
}
interface INodeExecutionData {
[key: string]: any;
json: IDataObject;
binary?: IBinaryKeyData;
pairedItem?: IPairedItemData | IPairedItemData[] | number;
error?: ExecutionError;
}Usage Examples:
import { Workflow, INode, IConnections } from "n8n-workflow";
// Create nodes
const nodes: INode[] = [
{
id: "start-node",
name: "Start",
type: "n8n-nodes-base.start",
typeVersion: 1,
position: [100, 200],
parameters: {}
},
{
id: "webhook-node",
name: "Webhook",
type: "n8n-nodes-base.webhook",
typeVersion: 1,
position: [300, 200],
parameters: {
httpMethod: "GET",
path: "webhook-test"
}
}
];
// Define connections
const connections: IConnections = {
"Start": {
"main": [
[{ "node": "Webhook", "type": "main", "index": 0 }]
]
}
};
// Create workflow
const workflow = new Workflow({
id: "example-workflow",
name: "Example Workflow",
nodes,
connections,
active: true,
nodeTypes: nodeTypesInstance,
staticData: {}
});
// Get specific node
const startNode = workflow.getNode("Start");
if (startNode) {
console.log(`Found node: ${startNode.name}`);
}
// Check if workflow is active
if (workflow.isActive()) {
console.log("Workflow is active and ready to execute");
}
// Execute workflow
const executionResult = await workflow.execute("manual");
console.log("Execution completed:", executionResult);