Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@fastify/autoload

Package Overview
Dependencies
Maintainers
19
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastify/autoload - npm Package Compare versions

Comparing version 5.3.1 to 5.4.0

test/commonjs/non-plugin.js

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)

4

package.json
{
"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",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc