Spaces:
Runtime error
Runtime error
File size: 17,497 Bytes
23ac194 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 | # avvio

[](https://www.npmjs.com/package/avvio)
[](https://standardjs.com/)
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 make 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](#install)
* [Example](#example)
* [API](#api)
* [Acknowledgements](#acknowledgements)
* [License](#license)
<a name="install"></a>
## Install
To install `avvio`, simply use npm:
```
npm i avvio
```
<a name="example"></a>
## Example
The example below can be found [here][example] and run using `node example.js`.
It demonstrates how to use `avvio` to load functions / plugins in order.
```js
'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 (err) {
// the error must be handled somehow
if (err) {
throw err
}
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/await or Promise support
async function third (instance, opts) {
console.log('third loaded')
}
```
<a name="api"></a>
## API
* <a href="#constructor"><code><b>avvio()</b></code></a>
* <a href="#use"><code>instance.<b>use()</b></code></a>
* <a href="#after"><code>instance.<b>after()</b></code></a>
* <a href="#await-after"><code>await instance.<b>after()</b></code></a>
* <a href="#ready"><code>instance.<b>ready()</b></code></a>
* <a href="#start"><code>instance.<b>start()</b></code></a>
* <a href="#override"><code>instance.<b>override()</b></code></a>
* <a href="#onClose"><code>instance.<b>onClose()</b></code></a>
* <a href="#close"><code>instance.<b>close()</b></code></a>
* <a href="#toJSON"><code>avvio.<b>toJSON()</b></code></a>
* <a href="#prettyPrint"><code>avvio.<b>prettyPrint()</b></code></a>
-------------------------------------------------------
<a name="constructor"></a>
### avvio([instance], [options], [started])
Starts the avvio sequence.
As the name suggests, `instance` is the object representing your application.
Avvio will add the functions `use`, `after` and `ready` to the instance.
```js
const server = {}
require('avvio')(server)
server.use(function first (s, opts, cb) {
// s is the same of server
s.use(function second (s, opts, cb) {
cb()
})
cb()
}).after(function (err, cb) {
// after first and second are finished
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()`](#start) or [`.ready()`](#ready).
* `timeout`: the number of millis to wait for a plugin to load after which
it will error with code `ERR_AVVIO_PLUGIN_TIMEOUT`. Default
`0` (disabled).
Events:
* `'start'` when the application starts
* `'preReady'` fired before the ready queue is run
The `avvio` function can also be used as a
constructor to inherit from.
```js
function Server () {}
const app = require('avvio')(new Server())
app.use(function (s, opts, done) {
// your code
done()
})
app.on('start', () => {
// you app can start
})
```
-------------------------------------------------------
<a name="use"></a>
### app.use(func, [optsOrFunc]) => Thenable
Loads one or more functions asynchronously.
The function **must** have the signature: `instance, options, done`
Plugin example:
```js
function plugin (server, opts, done) {
done()
}
app.use(plugin)
```
`done` should be called only once, when your plugin is ready to go. Additional calls to `done` are ignored.
If your plugin is ready to go immediately after the function is evaluated, you can omit `done` from the signature.
If the function returns a `Promise` (i.e. `async`), the above function signature is not required.
`use` returns a thenable wrapped instance on which `use` is called, to support a chainable API that can also be awaited.
This way, async/await is also supported and `use` can be awaited instead of using `after`.
Example using `after`:
```js
async function main () {
console.log('begin')
app.use(async function (server, opts) {
await sleep(10)
console.log('this first')
})
app.after(async (err) => {
if (err) throw err
console.log('then this')
})
await app.ready()
console.log('ready')
}
main().catch((err) => console.error(err))
```
Example using `await after`:
```js
async function main () {
console.log('begin')
app.use(async function (server, opts) {
await sleep(10)
console.log('this first')
})
await app.after()
console.log('then this')
await app.ready()
console.log('ready')
}
main().catch((err) => console.error(err))
```
Example using `await use`:
```js
async function main () {
console.log('begin')
await app.use(async function (server, opts) {
await sleep(10)
console.log('this first')
})
console.log('then this')
await app.ready()
console.log('ready')
}
main().catch((err) => console.error(err))
```
A function that returns the options argument instead of an object is supported as well:
```js
function first (server, opts, done) {
server.foo = 'bar'
done()
}
function second (server, opts, done) {
console.log(opts.foo === 'bar') // Evaluates to true
done()
}
/**
* If the options argument is a function, it has access to the parent
* instance via the first positional variable
*/
const func = parent => {
return {
foo: parent.foo
}
}
app.use(first)
app.use(second, func)
```
This is useful in cases where an injected variable from a plugin needs to be made available to another.
It is also possible to use [esm](https://nodejs.org/api/esm.html) with `import('./file.mjs')`:
```js
import boot from 'avvio'
const app = boot()
app.use(import('./fixtures/esm.mjs'))
```
-------------------------------------------------------
<a name="error-handling"></a>
#### Error handling
In order to handle errors in the loading plugins, you must use the
`.ready()` method, like so:
```js
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`](#after) callback specified. Otherwise, it will be handled
in [`ready`](#ready).
-------------------------------------------------------
<a name="after"></a>
### 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.
Note: `await after` can be used as an awaitable alternative to `after(func)`, or `await use` can be also as a shorthand for `use(plugin); await after()`.
The callback changes based on the parameters you give:
1. If no parameter is given to the callback and there is an error, that error will be passed to the next error handler.
2. If one parameter is given to the callback, that parameter will be the `error` object.
3. If two parameters are given to the callback, the first will be the `error` object, the second will be the `done` callback.
4. If three parameters are given to the callback, the first will be the `error` object, the second will be the top level `context` and the third the `done` callback.
In the "no parameter" and "one parameter" variants, the callback can return a `Promise`.
```js
const server = {}
const app = require('avvio')(server)
...
// after with one parameter
app.after(function (err) {
if (err) throw err
})
// after with two parameter
app.after(function (err, done) {
if (err) throw err
done()
})
// after with three parameters
app.after(function (err, context, done) {
if (err) throw err
assert.equal(context, server)
done()
})
// async after with one parameter
app.after(async function (err) {
await sleep(10)
if (err) {
throw err
}
})
// async after with no parameter
app.after(async function () {
await sleep(10)
})
```
`done` must be called only once.
If called with a function, it returns the instance on which `after` is called, to support a chainable API.
-------------------------------------------------------
<a name="await-after"></a>
### await app.after() | app.after() => Promise
Calling after with no function argument loads any plugins previously registered via `use` and returns a promise, which resolves when all plugins registered so far have loaded.
```js
async function main () {
app.use(async function (server, opts) {
await sleep(10)
console.log('this first')
})
app.use(async function (server, opts) {
await sleep(10)
console.log('this second')
})
console.log('before after')
await app.after()
console.log('after after')
app.use(async function (server, opts) {
await sleep(10)
console.log('this third')
})
await app.ready()
console.log('ready')
}
main().catch((err) => console.error(err))
```
Unlike `after` and `use`, `await after` is *not* chainable.
-------------------------------------------------------
<a name="ready"></a>
### 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 based on the parameters you give:
1. If no parameter is given to the callback and there is an error, that error will be passed to the next error handler.
2. If one parameter is given to the callback, that parameter will be the `error` object.
3. If two parameters are given to the callback, the first will be the `error` object, the second will be the `done` callback.
4. 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.
```js
const server = {}
const app = require('avvio')(server)
...
// ready with one parameter
app.ready(function (err) {
if (err) throw err
})
// ready with two parameter
app.ready(function (err, done) {
if (err) throw err
done()
})
// ready with three parameters
app.ready(function (err, context, done) {
if (err) throw err
assert.equal(context, server)
done()
})
// ready with Promise
app.ready()
.then(() => console.log('Ready'))
.catch(err => {
console.error(err)
process.exit(1)
})
// await ready from an async function.
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.
-------------------------------------------------------
<a name="start"></a>
### app.start()
Start the boot sequence, if it was not started yet.
Returns the `app` instance.
-------------------------------------------------------
<a name="override"></a>
### app.override(server, plugin, options)
Allows overriding 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.
```js
const assert = require('node: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) {
// create a new instance with the
// server as the prototype
const res = Object.create(s)
res.count = res.count + 1
return res
}
app.use(function first (s1, opts, cb) {
assert(s1 !== server)
assert(Object.prototype.isPrototypeOf.call(server, s1))
assert(s1.count === 1)
s1.use(second)
cb()
function second (s2, opts, cb) {
assert(s2 !== s1)
assert(Object.prototype.isPrototypeOf.isPrototypeOf.call(s1, s2))
assert(s2.count === 2)
cb()
}
})
```
-------------------------------------------------------
<a name="onClose"></a>
### 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 you give:
1. If one parameter is given to the callback, that parameter will be the `context`.
2. If zero or one parameter is given, the callback may return a promise
3. 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.
```js
const server = {}
const app = require('avvio')(server)
...
// onClose with one parameter
app.onClose(function (context) {
// ...
})
// onClose with one parameter, returning a promise
app.onClose(function (context) {
return new Promise((resolve, reject) => {
// ...
})
})
// async onClose with one parameter
app.onClose(async function (context) {
// ...
await ...
})
// onClose with two parameter
app.onClose(function (context, done) {
// ...
done()
})
```
If the callback returns a promise, the next onClose callback and the close callback will not 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.
-------------------------------------------------------
<a name="close"></a>
### 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 based on the parameters you give:
1. If one parameter is given to the callback, that parameter will be the `error` object.
2. If two parameters are given to the callback, the first will be the `error` object, the second will be the `done` callback.
3. 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 `close` will return a Promise.
```js
const server = {}
const app = require('avvio')(server)
...
// close with one parameter
app.close(function (err) {
if (err) throw err
})
// close with two parameter
app.close(function (err, done) {
if (err) throw err
done()
})
// close with three parameters
app.close(function (err, context, done) {
if (err) throw err
assert.equal(context, server)
done()
})
// close with Promise
app.close()
.then(() => console.log('Closed'))
.catch(err => {
console.error(err)
process.exit(1)
})
```
`done` must be called only once.
-------------------------------------------------------
<a name="toJSON"></a>
### avvio.toJSON()
Return a JSON tree representing the state of the plugins and the loading time.
Call it on `preReady` to get the complete tree.
```js
const avvio = require('avvio')()
avvio.on('preReady', () => {
avvio.toJSON()
})
```
The output is like this:
```json
{
"label": "root",
"start": 1550245184665,
"nodes": [
{
"parent": "root",
"start": 1550245184665,
"label": "first",
"nodes": [
{
"parent": "first",
"start": 1550245184708,
"label": "second",
"nodes": [],
"stop": 1550245184709,
"diff": 1
}
],
"stop": 1550245184709,
"diff": 44
},
{
"parent": "root",
"start": 1550245184709,
"label": "third",
"nodes": [],
"stop": 1550245184709,
"diff": 0
}
],
"stop": 1550245184709,
"diff": 44
}
```
-------------------------------------------------------
<a name="prettyPrint"></a>
### avvio.prettyPrint()
This method will return a printable string with the tree returned by the `toJSON()` method.
```js
const avvio = require('avvio')()
avvio.on('preReady', () => {
console.log(avvio.prettyPrint())
})
```
The output will be like:
```
avvio 56 ms
├── first 52 ms
├── second 1 ms
└── third 2 ms
```
-------------------------------------------------------
## Acknowledgements
This project was kindly sponsored by [nearForm](https://nearform.com).
## License
Copyright Matteo Collina 2016-2020, Licensed under [MIT][].
[MIT]: ./LICENSE
[example]: ./examples/example.js
|