@fastify/autoload
Advanced tools
| export default async function (app) { | ||
| app.get('/api', async function (req, reply) { | ||
| reply.status(200).send({ path: req.url }) | ||
| }) | ||
| } |
| // cjs empty file |
| // empty file |
| // mjs empty file |
| // file with something but not a plugin export | ||
| export const hello = () => { | ||
| // do something | ||
| } |
| // export default an empty object | ||
| export default {} |
| 'use strict' | ||
| const { after, before, describe, it, mock } = require('node:test') | ||
| const path = require('node:path') | ||
| const Fastify = require('fastify') | ||
| const autoLoad = require('../../../') | ||
| const assert = require('node:assert') | ||
| describe('Issue 462: ignore invalid and/or empty .js and .mjs files when using esm', function () { | ||
| const app = Fastify() | ||
| before(async function () { | ||
| app.register(autoLoad, { | ||
| dir: path.join(__dirname, 'routes'), | ||
| forceESM: true, | ||
| }) | ||
| mock.method(app.log, 'debug') | ||
| await app.ready() | ||
| }) | ||
| after(async function () { | ||
| await app.close() | ||
| }) | ||
| it('should respond correctly to /api', async function () { | ||
| const res = await app.inject({ url: '/api' }) | ||
| assert.strictEqual(res.statusCode, 200) | ||
| assert.deepStrictEqual(res.json(), { path: '/api' }) | ||
| // check debug call when importing empty/invalid esm files | ||
| // 5 debug log including: empty.cjs, empty.js, empty.mjs, hello.mjs, object.mjs | ||
| // api.mjs pass the check so it will not trigger the debug log | ||
| // increasing this number if there are more empty/invalid esm files you want to test | ||
| assert.strictEqual(app.log.debug.mock.callCount(), 5) | ||
| }) | ||
| }) |
+5
-3
@@ -70,4 +70,5 @@ 'use strict' | ||
| const { options: overrideConfig, forceESM, encapsulate } = options | ||
| const importedAsModule = forceESM || type === 'module' || runtime.forceESM | ||
| let content | ||
| if (forceESM || type === 'module' || runtime.forceESM) { | ||
| if (importedAsModule) { | ||
| content = await import(pathToFileURL(file).href) | ||
@@ -78,3 +79,3 @@ } else { | ||
| if (isPluginOrModule(content) === false) { | ||
| if (isPluginOrModule(importedAsModule ? content.default : content) === false) { | ||
| // We must have something that resembles a Fastify plugin function, or that | ||
@@ -261,4 +262,5 @@ // can be converted into one, that can eventually be passed to `avvio`. If | ||
| function isPluginOrModule (input) { | ||
| if (!input) return false | ||
| let result = false | ||
| const inputType = Object.prototype.toString.call(input) | ||
@@ -265,0 +267,0 @@ if (pluginOrModulePattern.test(inputType) === true) { |
+3
-3
| { | ||
| "name": "@fastify/autoload", | ||
| "version": "6.3.0", | ||
| "version": "6.3.1", | ||
| "description": "Require all plugins in a directory", | ||
@@ -83,3 +83,3 @@ "main": "index.js", | ||
| "@types/node": "^22.0.0", | ||
| "borp": "^0.19.0", | ||
| "borp": "^0.20.0", | ||
| "esbuild": "^0.25.0", | ||
@@ -95,3 +95,3 @@ "esbuild-register": "^3.5.0", | ||
| "ts-node-dev": "^2.0.0", | ||
| "tsd": "^0.31.1", | ||
| "tsd": "^0.32.0", | ||
| "tsimp": "^2.0.11", | ||
@@ -98,0 +98,0 @@ "tsm": "^2.3.0", |
Potential vulnerability
Supply chain riskInitial human review suggests the presence of a vulnerability in this package. It is pending further analysis and confirmation.
Potential vulnerability
Supply chain riskInitial human review suggests the presence of a vulnerability in this package. It is pending further analysis and confirmation.
174807
0.94%291
2.46%4626
1%