
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
Async flow control library which uses event or queue approach to avoid callback hell
Flui is an async flow control for Node and browsers which uses event or queue approach. With Flui you can completely avoid callback hell. With events, each action is loosely coupled to others so it allows you to compose them very flexibly. Queue approach is more straightforward and it allows you to write asynchronous code similar to synchronous way. Enough talking, let's see an example.
Imagine you want to implement simple CRON in Node. Our script will execute all files in our directory tree in an interval. We won't check if the file is executable because it is unnecessary for our purpose.
var fs = require('fs');
var spawn = require('child_process').spawn;
var path = require('path');
var flui = require('flui');
var dir = 'path/to';
setInterval(function () {
fs.readdir(dir, function (err, files) {
if (err) console.log(err);
files.forEach(function (file) {
fs.stat(path.join(dir, file), function (err, stats) {
if (err) console.log(err);
if (stats.isFile()) {
spawn(path.join(dir, file));
}
});
});
});
}, 60 * 60 * 1000);
var fs = require('fs');
var spawn = require('child_process').spawn;
var path = require('path');
var flui = require('flui');
var context = flui.context();
var dir = 'path/to';
setInterval(context.then('tick'), 60 * 60 * 1000);
context.on('tick', function () {
fs.readdir(dir, context.then('files'));
});
context.on('files', function (err, files) {
if (err) context.trigger('error', err);
files.forEach(context.then('file'));
});
context.on('file', function (file) {
fs.stat(path.join(dir, file), function (err, stats) {
context.trigger('stats', err, stats, file)
});
});
context.on('stats', function (err, stats, file) {
if (err) context.trigger('error', err);
if (stats.isFile()) {
spawn(path.join(dir, file));
}
});
context.on('error', function (err) {
//one error hadler for all errors
console.log(err);
});
Queue is useful for sequence of asynchronous operations. Assume you want to write method to authenticate a user.
var db = require('db');
var bcrypt = require('bcrypt');
var flui = require('flui');
db.
model('users').
findOne(req.param('username')).
exec(function (err, user) {
if (err) res.send(err);
if (user) {
bcrypt.compare(req.param('password'), user.password, function (err, valid) {
if (err) res.send(err);
if (valid) {
user.online = true;
user.save(function (err, user) {
req.session.auth = true;
res.send(user);
});
}
}
}
});
var db = require('db');
var bcrypt = require('bcrypt');
var flui = require('flui');
var queue = flui.queue([
function (username) {
db.
model('users').
findOne(username).
exec(queue.wait());
},
function (err, user) {
if (err) throw err;
if (user) {
bcrypt.compare(req.param('password'), user.password, function (err, valid) {
if (err) queue.raiseError(err);
queue.next(valid, user);
});
}
},
function (valid, user) {
if (valid) {
user.online = true;
user.save(queue.wait());
}
},
function (err, user) {
req.session.auth = true;
res.send(user);
}
]).
error(function (err) {
//one handler for all errors
res.send(err);
}).
run(req.param('username'));
function UserService(url) {
this.url = url;
this.session = {};
var self = this;
this.on('_login', function (result) {
if (result.error) self.trigger(['login:error', 'error'], result.error);
else {
self.trigger(['login:success', 'success'], result);
self.session = result;
socket.subscribe(result.id, self.then('_subscription'));
}
});
this.on('_subscription', function (subscription) {
subscription.onMessage('logout', self.then('_logout'));
subscription.onMessage('friend:online', self.then('friend:online'));
subscription.onMessage('friend:offline', self.then('friend:offline'));
});
this.on('_logout', function (user) {
socket.unsubscribe(user.id);
self.session = {};
self.trigger(['logout:success', 'success'], user);
});
}
UserService.prototype.login = function (credentials) {
http.post(this.url + '/login', credentials, this.then('_login'));
};
UserService.prototype.logout = function () {
http.post(this.url + '/logout', this.then('_logout'));
};
//extend service
flui.eventable(UserService);
var user = new UserService('/api/users');
//profile component
user.on('login:success', function (user) {
//display user info
});
//storage component
user.on('login:success', function (user) {
//set user into session storage
});
//flash component
user.on('login:error', function (error) {
//display error message
});
user.on('logout:success', function () {
//display logout message
});
//chat messages component
user.on('friend:online', function (friend) {
//add friend to collection
});
user.on('friend:offline', function (friend) {
//remove friend from collection
});
npm install flui
<script type="text/javascript" src="path/to/flui.js"></script>
Creates new event context. You can bind and trigger events on it.
Creates a queue of actions which will be executed in diven order. It also allows to add actions later or stop the execution.
Extends given object/class to bind and trigger events on it. It will get the same API as in flui.context(). The argument could be object, instance of function or function (class).
Tipograph is MIT licensed. Feel free to use it, contribute or spread the word. Created with love by Petr Nevyhoštěný (Twitter).
FAQs
Async flow control library which uses event or queue approach to avoid callback hell
The npm package flui receives a total of 1 weekly downloads. As such, flui popularity was classified as not popular.
We found that flui demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.