Comparing version 6.2.2 to 6.3.0
16
boot.js
@@ -182,9 +182,11 @@ 'use strict' | ||
function assertPlugin (plugin) { | ||
if (!(plugin && (typeof plugin === 'function' || typeof plugin.then === 'function'))) { | ||
throw new Error('plugin must be a function or a promise') | ||
} | ||
} | ||
// load a plugin | ||
Boot.prototype.use = function (plugin, opts) { | ||
if (typeof plugin === 'function') { | ||
this._addPlugin(plugin, opts, false) | ||
} else { | ||
throw new Error('plugin must be a function') | ||
} | ||
this._addPlugin(plugin, opts, false) | ||
@@ -195,5 +197,3 @@ return this | ||
Boot.prototype._addPlugin = function (plugin, opts, isAfter) { | ||
if (typeof plugin !== 'function') { | ||
throw new Error('plugin must be a function') | ||
} | ||
assertPlugin(plugin) | ||
opts = opts || {} | ||
@@ -200,0 +200,0 @@ |
{ | ||
"name": "avvio", | ||
"version": "6.2.2", | ||
"version": "6.3.0", | ||
"description": "Asynchronous bootstrapping of Node applications", | ||
@@ -37,3 +37,4 @@ "main": "boot.js", | ||
"pre-commit": "^1.2.2", | ||
"standard": "^12.0.1", | ||
"semver": "^6.3.0", | ||
"standard": "^14.0.0", | ||
"tap": "^12.0.0", | ||
@@ -40,0 +41,0 @@ "then-sleep": "^1.0.1", |
@@ -171,3 +171,15 @@ 'use strict' | ||
function loadPlugin (toLoad, cb) { | ||
if (typeof toLoad.func.then === 'function') { | ||
toLoad.func.then((fn) => { | ||
if (typeof fn.default === 'function') { | ||
fn = fn.default | ||
} | ||
toLoad.func = fn | ||
loadPlugin.call(this, toLoad, cb) | ||
}, cb) | ||
return | ||
} | ||
const last = this._current[0] | ||
// place the plugin at the top of _current | ||
@@ -174,0 +186,0 @@ this._current.unshift(toLoad) |
@@ -207,2 +207,11 @@ # avvio | ||
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')) | ||
``` | ||
------------------------------------------------------- | ||
@@ -209,0 +218,0 @@ <a name="error-handling"></a> |
'use strict' | ||
/* eslint no-prototype-builtins: off */ | ||
const test = require('tap').test | ||
@@ -229,1 +231,78 @@ const sleep = require('then-sleep') | ||
}) | ||
test('promise plugins', async (t) => { | ||
t.plan(14) | ||
const app = boot() | ||
let firstLoaded = false | ||
let secondLoaded = false | ||
let thirdLoaded = false | ||
app.use(first()) | ||
app.use(third()) | ||
async function first () { | ||
return async function (s, opts) { | ||
t.notOk(firstLoaded, 'first is not loaded') | ||
t.notOk(secondLoaded, 'second is not loaded') | ||
t.notOk(thirdLoaded, 'third is not loaded') | ||
firstLoaded = true | ||
s.use(second()) | ||
} | ||
} | ||
async function second () { | ||
return async function (s, opts) { | ||
t.ok(firstLoaded, 'first is loaded') | ||
t.notOk(secondLoaded, 'second is not loaded') | ||
t.notOk(thirdLoaded, 'third is not loaded') | ||
secondLoaded = true | ||
} | ||
} | ||
async function third () { | ||
return async function (s, opts) { | ||
t.ok(firstLoaded, 'first is loaded') | ||
t.ok(secondLoaded, 'second is loaded') | ||
t.notOk(thirdLoaded, 'third is not loaded') | ||
thirdLoaded = true | ||
} | ||
} | ||
const readyContext = await app.ready() | ||
t.equal(app, readyContext) | ||
t.ok(firstLoaded, 'first is loaded') | ||
t.ok(secondLoaded, 'second is loaded') | ||
t.ok(thirdLoaded, 'third is loaded') | ||
t.pass('booted') | ||
}) | ||
test('skip override with promise', (t) => { | ||
t.plan(3) | ||
const server = { my: 'server' } | ||
const app = boot(server) | ||
app.override = function (s, func) { | ||
t.pass('override called') | ||
if (func[Symbol.for('skip-override')]) { | ||
return s | ||
} | ||
return Object.create(s) | ||
} | ||
app.use(first()) | ||
async function first () { | ||
async function fn (s, opts) { | ||
t.equal(s, server) | ||
t.notOk(server.isPrototypeOf(s)) | ||
} | ||
fn[Symbol.for('skip-override')] = true | ||
return fn | ||
} | ||
}) |
@@ -9,3 +9,3 @@ 'use strict' | ||
let readyResult = boot() | ||
const readyResult = boot() | ||
.use(function (ctx, opts, done) { | ||
@@ -30,3 +30,3 @@ t.pass('1st plugin') | ||
let readyResult = app | ||
const readyResult = app | ||
.use(function (ctx, opts, done) { | ||
@@ -33,0 +33,0 @@ t.pass('1st plugin') |
'use strict' | ||
/* eslint no-prototype-builtins: off */ | ||
const test = require('tap').test | ||
@@ -4,0 +6,0 @@ const boot = require('..') |
@@ -7,3 +7,3 @@ 'use strict' | ||
test('pretty print', t => { | ||
t.plan(14) | ||
t.plan(19) | ||
@@ -14,8 +14,8 @@ const app = boot() | ||
.use(duplicate, { count: 3 }) | ||
.use(second) | ||
.use(second).after(afterUse).after(after) | ||
.use(duplicate, { count: 2 }) | ||
.use(third) | ||
.use(third).after(after) | ||
.use(duplicate, { count: 1 }) | ||
const linesExpected = [ /bound root \d+ ms/, | ||
const linesExpected = [/bound root \d+ ms/, | ||
/├── first \d+ ms/, | ||
@@ -27,2 +27,5 @@ /├─┬ duplicate \d+ ms/, | ||
/├── second \d+ ms/, | ||
/├─┬ bound _after \d+ ms/, | ||
/│ └── afterInsider \d+ ms/, | ||
/├── bound _after \d+ ms/, | ||
/├─┬ duplicate \d+ ms/, | ||
@@ -32,2 +35,3 @@ /│ └─┬ duplicate \d+ ms/, | ||
/├── third \d+ ms/, | ||
/├── bound _after \d+ ms/, | ||
/└─┬ duplicate \d+ ms/, | ||
@@ -40,3 +44,6 @@ / {2}└── duplicate \d+ ms/, | ||
const print = app.prettyPrint() | ||
print.split('\n').forEach((l, i) => { | ||
const lines = print.split('\n') | ||
t.equals(lines.length, linesExpected.length) | ||
lines.forEach((l, i) => { | ||
t.match(l, linesExpected[i]) | ||
@@ -55,3 +62,14 @@ }) | ||
} | ||
function after (err, cb) { | ||
cb(err) | ||
} | ||
function afterUse (err, cb) { | ||
app.use(afterInsider) | ||
cb(err) | ||
} | ||
function afterInsider (s, opts, done) { | ||
done() | ||
} | ||
function duplicate (instance, opts, cb) { | ||
@@ -58,0 +76,0 @@ if (opts.count > 0) { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
105836
36
3562
609
8