@fastify/autoload
Advanced tools
Comparing version 5.3.1 to 5.4.0
60
index.js
@@ -38,3 +38,3 @@ 'use strict' | ||
await Promise.all(pluginArray.map(({ file, type, prefix }) => { | ||
return loadPlugin(file, type, prefix, opts) | ||
return loadPlugin({ file, type, directoryPrefix: prefix, options: opts, log: fastify.log }) | ||
.then((plugin) => { | ||
@@ -234,3 +234,3 @@ if (plugin) { | ||
async function loadPlugin (file, type, directoryPrefix, options) { | ||
async function loadPlugin ({ file, type, directoryPrefix, options, log }) { | ||
const { options: overrideConfig, forceESM, encapsulate } = options | ||
@@ -244,2 +244,10 @@ let content | ||
if (isPluginOrModule(content) === false) { | ||
// We must have something that resembles a Fastify plugin function, or that | ||
// can be converted into one, that can eventually be passed to `avvio`. If | ||
// it is anything else, skip automatic loading of this item. | ||
log.debug({ file }, 'skipping autoloading of file because it does not export a Fastify plugin compatible shape') | ||
return | ||
} | ||
const plugin = wrapRoutes(content.default || content) | ||
@@ -255,2 +263,3 @@ const pluginConfig = (content.default && content.default.autoConfig) || content.autoConfig || {} | ||
if (plugin.autoload === false || content.autoload === false) { | ||
log.debug({ file }, 'skipping autoload due to `autoload: false` being set') | ||
return | ||
@@ -308,6 +317,47 @@ } | ||
/** | ||
* Used to determine if the contents of a required autoloaded file matches | ||
* the shape of a Fastify route configuration object. | ||
* | ||
* @param {*} input The data to check. | ||
* | ||
* @returns {boolean} True if the data represents a route configuration object. | ||
* False otherwise. | ||
*/ | ||
function isRouteObject (input) { | ||
if (input && | ||
Object.prototype.toString.call(input) === '[object Object]' && | ||
Object.prototype.hasOwnProperty.call(input, 'method')) { | ||
return true | ||
} | ||
return false | ||
} | ||
/** | ||
* Used to determine if the contents of a required autoloaded file is a valid | ||
* plugin or route configuration object. In the case of a route configuraton | ||
* object, it will later be wrapped into a plugin. | ||
* | ||
* @param {*} input The data to check. | ||
* | ||
* @returns {boolean} True if the object can be used by the autoload system by | ||
* eventually passing it into `avvio`. False otherwise. | ||
*/ | ||
function isPluginOrModule (input) { | ||
let result = false | ||
const inputType = Object.prototype.toString.call(input) | ||
if (/\[object (AsyncFunction|Function|Module)\]/.test(inputType) === true) { | ||
result = true | ||
} else if (Object.prototype.hasOwnProperty.call(input, 'default')) { | ||
result = isPluginOrModule(input.default) | ||
} else { | ||
result = isRouteObject(input) | ||
} | ||
return result | ||
} | ||
function wrapRoutes (content) { | ||
if (content && | ||
Object.prototype.toString.call(content) === '[object Object]' && | ||
Object.prototype.hasOwnProperty.call(content, 'method')) { | ||
if (isRouteObject(content) === true) { | ||
return async function (fastify, opts) { | ||
@@ -314,0 +364,0 @@ fastify.route(content) |
{ | ||
"name": "@fastify/autoload", | ||
"version": "5.3.1", | ||
"version": "5.4.0", | ||
"description": "Require all plugins in a directory", | ||
@@ -49,3 +49,3 @@ "main": "index.js", | ||
"@swc/register": "^0.1.9", | ||
"@types/jest": "^28.1.6", | ||
"@types/jest": "^29.0.0", | ||
"@types/node": "^18.0.0", | ||
@@ -52,0 +52,0 @@ "@types/tap": "^15.0.5", |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
113431
227
3301
7