diff --git a/lib/get-plugin-name.js b/lib/get-plugin-name.js new file mode 100644 index 0000000..8624479 --- /dev/null +++ b/lib/get-plugin-name.js @@ -0,0 +1,34 @@ +'use strict' + +// this symbol is assigned by fastify-plugin +const kPluginMeta = Symbol.for('plugin-meta') + +/** + * @param {function} plugin + * @param {object} [options] + * @param {string} [options.name] + * @returns {string} + */ +function getPluginName (plugin, options) { + // use explicit function metadata if set + if (plugin[kPluginMeta] && plugin[kPluginMeta].name) { + return plugin[kPluginMeta].name + } + + // use explicit name option if set + if (options && options.name) { + return options.name + } + + // determine from the function + if (plugin.name) { + return plugin.name + } else { + // takes the first two lines of the function if nothing else works + return plugin.toString().split('\n').slice(0, 2).map(s => s.trim()).join(' -- ') + } +} + +module.exports = { + getPluginName +} diff --git a/plugin.js b/plugin.js index da3380f..e38f558 100644 --- a/plugin.js +++ b/plugin.js @@ -5,28 +5,7 @@ const EE = require('events').EventEmitter const inherits = require('util').inherits const { debug } = require('./lib/debug') const { AVV_ERR_READY_TIMEOUT } = require('./lib/errors') - -// this symbol is assigned by fastify-plugin -const kPluginMeta = Symbol.for('plugin-meta') - -function getName (func, optsOrFunc) { - // use explicit function metadata if set - if (func[kPluginMeta] && func[kPluginMeta].name) { - return func[kPluginMeta].name - } - - if (typeof optsOrFunc !== 'undefined' && typeof optsOrFunc !== 'function' && optsOrFunc.name) { - return optsOrFunc.name - } - - // use the function name if it exists - if (func.name) { - return func.name - } - - // takes the first two lines of the function if nothing else works - return func.toString().split('\n').slice(0, 2).map(s => s.trim()).join(' -- ') -} +const { getPluginName } = require('./lib/get-plugin-name') function promise () { const obj = {} @@ -39,14 +18,14 @@ function promise () { return obj } -function Plugin (parent, func, optsOrFunc, isAfter, timeout) { +function Plugin (parent, func, options, isAfter, timeout) { this.started = false this.func = func - this.opts = optsOrFunc + this.opts = options this.onFinish = null this.parent = parent this.timeout = timeout === undefined ? parent._timeout : timeout - this.name = getName(func, optsOrFunc) + this.name = getPluginName(func, options) this.isAfter = isAfter this.q = fastq(parent, loadPluginNextTick, 1) this.q.pause() diff --git a/test/lib/get-plugin-name.test.js b/test/lib/get-plugin-name.test.js new file mode 100644 index 0000000..28e7e5e --- /dev/null +++ b/test/lib/get-plugin-name.test.js @@ -0,0 +1,66 @@ +'use strict' + +const { test } = require('tap') +const { getPluginName } = require('../../lib/get-plugin-name') + +test('getPluginName of function', (t) => { + t.plan(1) + + t.equal(getPluginName(function aPlugin () { }), 'aPlugin') +}) + +test('getPluginName of async function', (t) => { + t.plan(1) + + t.equal(getPluginName(async function aPlugin () { }), 'aPlugin') +}) + +test('getPluginName of arrow function without name', (t) => { + t.plan(2) + + t.equal(getPluginName(() => { }), '() => { }') + t.equal(getPluginName(() => { return 'random' }), '() => { return \'random\' }') +}) + +test('getPluginName of arrow function assigned to variable', (t) => { + t.plan(1) + + const namedArrowFunction = () => { } + t.equal(getPluginName(namedArrowFunction), 'namedArrowFunction') +}) + +test("getPluginName based on Symbol 'plugin-meta' /1", (t) => { + t.plan(1) + + function plugin () { + + } + + plugin[Symbol.for('plugin-meta')] = {} + t.equal(getPluginName(plugin), 'plugin') +}) + +test("getPluginName based on Symbol 'plugin-meta' /2", (t) => { + t.plan(1) + + function plugin () { + + } + + plugin[Symbol.for('plugin-meta')] = { + name: 'fastify-non-existent' + } + t.equal(getPluginName(plugin), 'fastify-non-existent') +}) + +test('getPluginName if null is provided as options', (t) => { + t.plan(1) + + t.equal(getPluginName(function a () {}, null), 'a') +}) + +test('getPluginName if name is provided in options', (t) => { + t.plan(1) + + t.equal(getPluginName(function defaultName () {}, { name: 'providedName' }), 'providedName') +})