Agent-based task flow framework for Node.js
npm install -save transgate
- Gate is an endpoint of Input/Output. For example, file storage, database, queue or API service.
- Agent is a worker to process an item between Input/Output gates and does not know anything opposite gates.
- Item is an entity as each task target, to be exchanged between gates, and an Object or a JSON.
nullindicates the terminator.
- A agent receives an item from a gate for Input.
- The agent processes an item.
- According to need the Agent sends the results as next item(s) to some gate(s) for Output.
If the agent receives null as an item, sends null to all next gates and closes itself.
- All agents must be given gates for input and output in the constructor and run independently.
- A gate is easy to replace because the interface of the item is a simple Object.
- A gate for Input must implement
receivePromise method that resolves an item. - A gate for Output must implement
sendPromise method with argument an item.sendresolves when completes to write the item.
- An agent inherits
Agentclass and overridesasync main(item, outGate)method to process anitemand send it tooutGate. - To create an agent instance is to call
new YourAgent(inGate, outGate). If theoutGateis plural, it need be replaced with key-value-based{ outGate1, outGate2 }in order to haveasync main(item, { outGate1, outGate2 }). - If the agent uses any daemon, overrides
async before()to launch it andasync after()to shut down it.
To start agents is calling Agent.all(...yourAgents) Promise method. If you use any daemons in some gates, They should be shut down after all Promise has done.
There are the following gates in this library.
- MemoryGate - Supports both Input/Output for test
- ReadFileGate - Reading each line as an item from a file for Input
- WriteFileGate - Writing sended items to a file for Output
- HttpServerGate - Receiving an item that is a body POSTed by HTTP client for Input
- HttpClientGate - Sending an item as POST request body to fixed HTTP server endpoint for Output
- IntervalGate - Providing an item contains the time every interval passing for Input
- StdinGate - Reading each line as an item from stdin for Input
- StdoutGate - Writing sended items to stdout for Output
- JointGate - Pipes an agent to another one
- mixer - Receiving items from some gates for Input
- duplicator - Sending an item the multiple gates for Output
- (No name) : item.value x 2
ValueAdd1: item.value + 1
- memory --> Agent1 --> joint
- joint --> Agent2 --> stdout
const {
Agent,
JointGate,
MemoryGate,
StdoutGate,
} = require('transgate');
class ValueAdd1 extends Agent {
async main(item, stdoutGate) {
item.value += 1;
stdoutGate.send(item);
}
}
const inputGate = new MemoryGate([
{ value: 1 }, { value: 2 }, { value: 3 }, { value: 4 }
]);
const joint = new JointGate();
const stdoutGate = new StdoutGate();
Agent.all(
Agent.create(inputGate, joint, async (item) => {
item.value *= 2;
return item;
}),
new ValueAdd1(joint, stdoutGate),
)
.catch(err => {
console.error(err);
});{"value":3}
{"value":5}
{"value":7}
{"value":9}