Repository: fastify/fastify
File: lib/pluginUtils.js (JavaScript)

Code before:
function checkVersion (fn) {
  const meta = getMeta(fn)
  if (!meta) return

  const requiredVersion = meta.fastify

  const fastifyRc = /-rc.+$/.test(this.version)
  if (fastifyRc === true && semver.gt(this.version, semver.coerce(requiredVersion)) === true) {
    // A Fastify release candidate phase is taking place. In order to reduce
    // the effort needed to test plugins with the RC, we allow plugins targeting
    // the prior Fastify release to be loaded.
    return
  }
  if (requiredVersion && semver.satisfies(this.version, requiredVersion, { includePrerelease: fastifyRc }) === false) {
    // We are not in a release candidate phase. Thus, we must honor the semver
    // ranges defined by the plugin's metadata. Which is to say, if the plugin
    // expects an older version of Fastify than the _current_ version, we will
    // throw an error.
    throw new FST_ERR_PLUGIN_VERSION_MISMATCH(meta.name, requiredVersion, this.version)
  }
}

function registerPluginName (fn) {
  const meta = getMeta(fn)

  const name = meta?.name ?? 'anonymous'

  this[kRegisteredPlugins].push(name)
}

function checkPluginHealthiness (fn) {
  if (fn.constructor.name === 'AsyncFunction' && fn.length === 3) {
    console.log(this[kRegisteredPlugins])
    warning.emit('FSTWRN002', this[kRegisteredPlugins][this[kRegisteredPlugins].length - 1])
  }
}

function registerPlugin (fn) {
  registerPluginName.call(this, fn)
  checkPluginHealthiness.call(this, fn)
  checkVersion.call(this, fn)
  checkDecorators.call(this, fn)
  checkDependencies.call(this, fn)
  return shouldSkipOverride(fn)
}

module.exports = {
  getPluginName,
  getFuncPreview,
  kRegisteredPlugins,
  getDisplayName,

Review comment (suggestion):
not 100% sure here - I remember some logic somewhere where we read the `function.toString()` payload when the name is empty and setting it as `anonymous` may bypass that chack, need to check.

Instead of pushing the string everytime, I would just return the `name` (if any) and then pass it to `checkPluginHealthiness(fn, name || 'anonymous')`

Code after:
function checkVersion (fn) {
  const meta = getMeta(fn)
  if (!meta) return

  const requiredVersion = meta.fastify

  const fastifyRc = /-rc.+$/.test(this.version)
  if (fastifyRc === true && semver.gt(this.version, semver.coerce(requiredVersion)) === true) {
    // A Fastify release candidate phase is taking place. In order to reduce
    // the effort needed to test plugins with the RC, we allow plugins targeting
    // the prior Fastify release to be loaded.
    return
  }
  if (requiredVersion && semver.satisfies(this.version, requiredVersion, { includePrerelease: fastifyRc }) === false) {
    // We are not in a release candidate phase. Thus, we must honor the semver
    // ranges defined by the plugin's metadata. Which is to say, if the plugin
    // expects an older version of Fastify than the _current_ version, we will
    // throw an error.
    throw new FST_ERR_PLUGIN_VERSION_MISMATCH(meta.name, requiredVersion, this.version)
  }
}

function registerPluginName (fn) {
  const meta = getMeta(fn)
  if (!meta) return

  const name = meta.name
  if (!name) return
  this[kRegisteredPlugins].push(name)
  return name
}

function checkPluginHealthiness (fn, pluginName = 'anonymous') {
  if (fn.constructor.name === 'AsyncFunction' && fn.length === 3) {
    warning.emit('FSTWRN002', pluginName)
  }
}

function registerPlugin (fn) {
  const pluginName = registerPluginName.call(this, fn)
  checkPluginHealthiness.call(this, fn, pluginName)
  checkVersion.call(this, fn)
  checkDecorators.call(this, fn)
  checkDependencies.call(this, fn)
  return shouldSkipOverride(fn)
}

module.exports = {
  getPluginName,
  getFuncPreview,
  kRegisteredPlugins,