Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract loadPlugin #220

Merged
merged 2 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ const {
AVV_ERR_READY_TIMEOUT
} = require('./lib/errors')
const { TimeTree } = require('./lib/time-tree')
const Plugin = require('./plugin')
const { Plugin } = require('./plugin')
const { debug } = require('./lib/debug')
const { loadPlugin } = require('./lib/load-plugin')
const kAvvio = Symbol('kAvvio')
const kThenifyDoNotWrap = Symbol('kThenifyDoNotWrap')

Expand Down Expand Up @@ -154,7 +155,7 @@ function Boot (server, opts, done) {
})
})

Plugin.loadPlugin.call(this, this._root, (err) => {
loadPlugin(this, this._root, (err) => {
debug('root plugin ready')
try {
this.emit('preReady')
Expand Down
42 changes: 42 additions & 0 deletions lib/load-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict'

/**
* @callback LoadPluginCallback
* @param {Error} [err]
*/

/**
* Load a plugin
*
* @param {*} instance
* @param {*} plugin
* @param {LoadPluginCallback} callback
*/
function loadPlugin (instance, plugin, callback) {
if (typeof plugin.func.then === 'function') {
plugin.func.then((fn) => {
if (typeof fn.default === 'function') {
fn = fn.default
}
plugin.func = fn
loadPlugin(instance, plugin, callback)
}, callback)
return
}

const last = instance._current[0]

// place the plugin at the top of _current
instance._current.unshift(plugin)

plugin.exec((last && last.server) || instance._server, (err) => {
plugin.finish(err, (err) => {
instance._current.shift()
callback(err)
})
})
}

module.exports = {
loadPlugin
}
43 changes: 10 additions & 33 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fastq = require('fastq')
const EE = require('events').EventEmitter
const inherits = require('util').inherits
const { debug } = require('./lib/debug')
const { loadPlugin } = require('./lib/load-plugin')
const { createPromise } = require('./lib/create-promise')
const { AVV_ERR_READY_TIMEOUT } = require('./lib/errors')
const { getPluginName } = require('./lib/get-plugin-name')
Expand Down Expand Up @@ -213,40 +214,16 @@ Plugin.prototype.finish = function (err, cb) {
this.q.resume()
}

// delays plugin loading until the next tick to ensure any bound `_after` callbacks have a chance
// to run prior to executing the next plugin
function loadPluginNextTick (toLoad, cb) {
const parent = this
process.nextTick(loadPlugin.bind(parent), toLoad, cb)
}

// loads a plugin
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
this._current.unshift(toLoad)

toLoad.exec((last && last.server) || this._server, (err) => {
toLoad.finish(err, (err) => {
this._current.shift()
cb(err)
})
})
/**
* Delays plugin loading until the next tick to ensure any bound `_after` callbacks have a chance
* to run prior to executing the next plugin
*/
function loadPluginNextTick (plugin, callback) {
process.nextTick(loadPlugin, this, plugin, callback)
}

function noop () {}

module.exports = Plugin
module.exports.loadPlugin = loadPlugin
module.exports = {
Plugin
}
112 changes: 112 additions & 0 deletions test/lib/load-plugin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
'use strict'

const { test } = require('tap')
const boot = require('../..')
const { loadPlugin } = require('../../lib/load-plugin')
const { Plugin } = require('../../plugin')

test('successfully load a plugin with sync function', (t) => {
t.plan(1)
const app = boot({})

const plugin = new Plugin(app, function (instance, opts, done) {
done()
}, false, 0)

loadPlugin(app, plugin, function (err) {
t.equal(err, undefined)
})
})

test('catch an error when loading a plugin with sync function', (t) => {
t.plan(1)
const app = boot({})

const plugin = new Plugin(app, function (instance, opts, done) {
done(Error('ArbitraryError'))
}, false, 0)

loadPlugin(app, plugin, function (err) {
t.equal(err.message, 'ArbitraryError')
})
})

test('successfully load a plugin with async function', (t) => {
t.plan(1)
const app = boot({})

const plugin = new Plugin(app, async function (instance, opts) { }, false, 0)

loadPlugin(app, plugin, function (err) {
t.equal(err, undefined)
})
})

test('catch an error when loading a plugin with async function', (t) => {
t.plan(1)
const app = boot({})

const plugin = new Plugin(app, async function (instance, opts) {
throw Error('ArbitraryError')
}, false, 0)

loadPlugin(app, plugin, function (err) {
t.equal(err.message, 'ArbitraryError')
})
})

test('successfully load a plugin when function is a Promise, which resolves to a function', (t) => {
t.plan(1)
const app = boot({})

const plugin = new Plugin(app, new Promise(resolve => resolve(function (instance, opts, done) {
done()
})), false, 0)

loadPlugin(app, plugin, function (err) {
t.equal(err, undefined)
})
})

test('catch an error when loading a plugin when function is a Promise, which resolves to a function', (t) => {
t.plan(1)
const app = boot({})

const plugin = new Plugin(app, new Promise(resolve => resolve(function (instance, opts, done) {
done(Error('ArbitraryError'))
})), false, 0)

loadPlugin(app, plugin, function (err) {
t.equal(err.message, 'ArbitraryError')
})
})

test('successfully load a plugin when function is a Promise, which resolves to a function, which is wrapped in default', (t) => {
t.plan(1)
const app = boot({})

const plugin = new Plugin(app, new Promise(resolve => resolve({
default: function (instance, opts, done) {
done()
}
})), false, 0)

loadPlugin(app, plugin, function (err) {
t.equal(err, undefined)
})
})

test('catch an error when loading a plugin when function is a Promise, which resolves to a function, which is wrapped in default', (t) => {
t.plan(1)
const app = boot({})

const plugin = new Plugin(app, new Promise(resolve => resolve({
default: function (instance, opts, done) {
done(Error('ArbitraryError'))
}
})), false, 0)

loadPlugin(app, plugin, function (err) {
t.equal(err.message, 'ArbitraryError')
})
})