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 getPluginName #217

Merged
merged 4 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 28 additions & 0 deletions lib/get-plugin-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

// this symbol is assigned by fastify-plugin
const kPluginMeta = Symbol.for('plugin-meta')

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
}
29 changes: 4 additions & 25 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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()
Expand Down
66 changes: 66 additions & 0 deletions test/lib/get-plugin-name.test.js
Original file line number Diff line number Diff line change
@@ -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(function aPlugin () { }), 'aPlugin')
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
})

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')
})