avvio
Asynchronous bootstrapping is hard, different things can go wrong, error handling and load order just to name a few. The aim of this module is to made it simple.
avvio
is fully reentrant and graph-based. You can load
components/plugins within plugins, and be still sure that things will
happen in the right order. At the end of the loading, your application will start.
Install
To install avvio
, simply use npm:
npm install avvio --save
Example
The example below can be found here and ran using node example.js
.
It demonstrates how to use avvio
to load functions / plugins in order.
'use strict'
const app = require('avvio')()
app
.use(first, { hello: 'world' })
.after((err, cb) => {
console.log('after first and second')
cb()
})
app.use(third)
app.ready(function () {
console.log('application booted!')
})
function first (instance, opts, cb) {
console.log('first loaded', opts)
instance.use(second)
cb()
}
function second (instance, opts, cb) {
console.log('second loaded')
process.nextTick(cb)
}
async function third (instance, opts) {
console.log('third loaded')
}
API
avvio([instance], [options], [started])
Starts the avvio sequence.
As the name suggest, instance
is the object representing your application.
Avvio will add the functions use
, after
and ready
to the instance.
const server = {}
require('avvio')(server)
server.use(function first (s, opts, cb) {
s.use(function second (s, opts, cb) {
cb()
})
cb()
}).after(function (err, cb) {
cb()
})
Options:
expose
: a key/value property to change how use
, after
and ready
are exposed.autostart
: do not start loading plugins automatically, but wait for
a call to .start()
or .ready()
.
Events:
'start'
when the application starts'preReady'
fired before the ready queue is run
The avvio
function can be used also as a
constructor to inherits from.
function Server () {}
const app = require('avvio')(new Server())
app.use(function (s, opts, done) {
done()
})
app.on('start', () => {
})
app.use(func, [opts])
Loads one or more functions asynchronously.
The function must have the signature: instance, options, done
Plugin example:
function plugin (server, opts, done) {
done()
}
app.use(plugin)
done
must be called only once, when your plugin is ready to go.
async/await is also supported:
async function plugin (server, opts) {
await sleep(10)
}
app.use(plugin)
use
returns the instance on which use
is called, to support a chainable API.
Error handling
In order to handle errors in the loading plugins, you must use the
.ready()
method, like so:
app.use(function (instance, opts, done) {
done(new Error('error'))
}, opts)
app.ready(function (err) {
if (err) throw err
})
When an error happens, the loading of plugins will stop until there is
an after
callback specified. Otherwise, it will be handled
in ready
.
app.after(func(error, [context], [done]))
Calls a function after all the previously defined plugins are loaded, including
all their dependencies. The 'start'
event is not emitted yet.
The callback changes basing on the parameters your are giving:
- If no parameter is given to the callback and there is an error, that error will be passed to the next error handler.
- If one parameter is given to the callback, that parameter will be the
error
object. - If two parameters are given to the callback, the first will be the
error
object, the second will be the done
callback. - If three parameters are given to the callback, the first will be the
error
object, the second will be the top level context
unless you have specified both server and override, in that case the context
will be what the override returns, and the third the done
callback.
In the "no parameter" and "one parameter" variants, the callback can also
return a Promise
.
const server = {}
const app = require('avvio')(server)
...
app.after(function (err) {
if (err) throw err
})
app.after(function (err, done) {
if (err) throw err
done()
})
app.after(function (err, context, done) {
if (err) throw err
assert.equal(context, server)
done()
})
app.after(async function (err) {
await sleep(10)
if (err) {
throw err
}
})
app.after(async function () {
await sleep(10)
})
done
must be called only once.
Returns the instance on which after
is called, to support a chainable API.
app.ready([func(error, [context], [done])])
Calls a function after all the plugins and after
call are completed, but before 'start'
is emitted. ready
callbacks are executed one at a time.
The callback changes basing on the parameters your are giving:
- If no parameter is given to the callback and there is an error, that error will be passed to the next error handler.
- If one parameter is given to the callback, that parameter will be the
error
object. - If two parameters are given to the callback, the first will be the
error
object, the second will be the done
callback. - If three parameters are given to the callback, the first will be the
error
object, the second will be the top level context
unless you have specified both server and override, in that case the context
will be what the override returns, and the third the done
callback.
If no callback is provided ready
will return a Promise that is resolved or rejected once plugins and after
calls are completed. On success context
is provided to the .then
callback, if an error occurs it is provided to the .catch
callback.
const server = {}
const app = require('avvio')(server)
...
app.ready(function (err) {
if (err) throw err
})
app.ready(function (err, done) {
if (err) throw err
done()
})
app.ready(function (err, context, done) {
if (err) throw err
assert.equal(context, server)
done()
})
app.ready()
.then(() => console.log('Ready'))
.catch(err => {
console.error(err)
process.exit(1)
})
async function main () [
try {
await app.ready()
console.log('Ready')
} catch(err) {
console.error(err)
process.exit(1)
}
}
done
must be called only once.
The callback form of this function has no return value.
If autostart: false
is passed as an option, calling .ready()
will
also start the boot sequence.
app.start()
Start the boot sequence, if it was not started yet.
Returns the app
instance.
avvio.express(app)
Same as:
const app = express()
const avvio = require('avvio')
avvio(app, {
expose: {
use: 'load'
}
})
app.override(server, plugin, options)
Allows to override the instance of the server for each loading plugin.
It allows the creation of an inheritance chain for the server instances.
The first parameter is the server instance and the second is the plugin function while the third is the options object that you give to use.
const assert = require('assert')
const server = { count: 0 }
const app = require('avvio')(server)
console.log(app !== server, 'override must be set on the Avvio instance')
app.override = function (s, fn, opts) {
const res = Object.create(s)
res.count = res.count + 1
return res
}
app.use(function first (s1, opts, cb) {
assert(s1 !== server)
assert(server.isPrototypeOf(s1))
assert(s1.count === 1)
s1.use(second)
cb()
function second (s2, opts, cb) {
assert(s2 !== s1)
assert(s1.isPrototypeOf(s2))
assert(s2.count === 2)
cb()
}
})
app.onClose(func([context], [done]))
Registers a new callback that will be fired once then close
api is called.
The callback changes basing on the parameters your are giving:
- If one parameter is given to the callback, that parameter will be the
context
. - If zero or one parameter is given, the callback may return a promise
- If two parameters are given to the callback, the first will be the top level
context
unless you have specified both server and override, in that case the context
will be what the override returns, the second will be the done
callback.
const server = {}
const app = require('avvio')(server)
...
app.onClose(function (context) {
})
app.onClose(function (context) {
return new Promise((resolve, reject) => {
})
})
app.onClose(async function (context) {
await ...
})
app.onClose(function (context, done) {
done()
})
If the callback returns a promise, the next onClose callback and the close callback won't run until the promise is either resolved or rejected.
done
must be called only once.
Returns the instance on which onClose
is called, to support a chainable API.
app.close(func(error, [context], [done]))
Starts the shutdown procedure, the callback is called once all the registered callbacks with onClose
has been executed.
The callback changes basing on the parameters your are giving:
- If one parameter is given to the callback, that parameter will be the
error
object. - If two parameters are given to the callback, the first will be the
error
object, the second will be the done
callback. - If three parameters are given to the callback, the first will be the
error
object, the second will be the top level context
unless you have specified both server and override, in that case the context
will be what the override returns, and the third the done
callback.
const server = {}
const app = require('avvio')(server)
...
app.close(function (err) {
if (err) throw err
})
app.close(function (err, done) {
if (err) throw err
done()
})
app.close(function (err, context, done) {
if (err) throw err
assert.equal(context, server)
done()
})
done
must be called only once.
Acknowledgements
This project was kindly sponsored by nearForm.
License
Copyright Matteo Collina 2016-2017, Licensed under MIT.