From 28109cf0172463352a3823e3721ef230eb102597 Mon Sep 17 00:00:00 2001 From: huodoo Date: Wed, 22 Jul 2026 21:47:54 +0800 Subject: [PATCH 1/2] fix: pluginsMeta is shared among all nodes, which leads to an error in the findCommonHooksPrefix logic --- index.js | 4 +-- test/commonjs/autohooks-basic-mini.js | 32 +++++++++++++++++++ test/commonjs/autohooks/basic-mini.js | 16 ++++++++++ .../autohooks/routes-mini/.autohooks.js | 6 ++++ .../autohooks/routes-mini/child/routes.js | 7 ++++ test/commonjs/autohooks/routes-mini/routes.js | 7 ++++ 6 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 test/commonjs/autohooks-basic-mini.js create mode 100644 test/commonjs/autohooks/basic-mini.js create mode 100644 test/commonjs/autohooks/routes-mini/.autohooks.js create mode 100644 test/commonjs/autohooks/routes-mini/child/routes.js create mode 100644 test/commonjs/autohooks/routes-mini/routes.js diff --git a/index.js b/index.js index ffaa2cbe..7392b264 100644 --- a/index.js +++ b/index.js @@ -159,8 +159,8 @@ function registerNode (node, fastify) { } function findCommonHooksPrefix (node) { - const prefixes = Object.values(node.pluginsMeta) - .map((meta) => meta?.options?.prefix) + const prefixes = node.plugins + .map((plugin) => node.pluginsMeta[plugin.file]?.options?.prefix) .filter((prefix) => typeof prefix === 'string' && prefix.length > 0) if (prefixes.length === 0) { diff --git a/test/commonjs/autohooks-basic-mini.js b/test/commonjs/autohooks-basic-mini.js new file mode 100644 index 00000000..58d452ee --- /dev/null +++ b/test/commonjs/autohooks-basic-mini.js @@ -0,0 +1,32 @@ +'use strict' + +const { after, before, describe, it } = require('node:test') +const assert = require('node:assert') +const Fastify = require('fastify') + +describe('Node test suite for autohooks-basic', function () { + const app = Fastify() + before(async function () { + app.register(require('./autohooks/basic-mini')) + app.decorateRequest('hooked', "") + await app.ready() + }) + + after(async function () { + await app.close() + }) + + it('should respond correctly to /', async function () { + const res = await app.inject({ url: '/' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { hooked: ['root'] }) + }) + + it('should respond correctly to /child', async function () { + const res = await app.inject({ url: '/child' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { hooked: "" }) + }) +}) diff --git a/test/commonjs/autohooks/basic-mini.js b/test/commonjs/autohooks/basic-mini.js new file mode 100644 index 00000000..2c7c81f3 --- /dev/null +++ b/test/commonjs/autohooks/basic-mini.js @@ -0,0 +1,16 @@ +'use strict' + +const path = require('node:path') +const autoLoad = require('../../../') + +module.exports = function (fastify, opts, next) { + fastify.log.error(__dirname); + + fastify.register(autoLoad, { + dir: path.join(__dirname, 'routes-mini'), + autoHooks: true + }) + + next() +} + diff --git a/test/commonjs/autohooks/routes-mini/.autohooks.js b/test/commonjs/autohooks/routes-mini/.autohooks.js new file mode 100644 index 00000000..1e551aef --- /dev/null +++ b/test/commonjs/autohooks/routes-mini/.autohooks.js @@ -0,0 +1,6 @@ +module.exports = async function (app, opts) { + app.addHook('onRequest', async (req, reply) => { + req.hooked = req.hooked || [] + req.hooked.push('root') + }) +} diff --git a/test/commonjs/autohooks/routes-mini/child/routes.js b/test/commonjs/autohooks/routes-mini/child/routes.js new file mode 100644 index 00000000..521b6527 --- /dev/null +++ b/test/commonjs/autohooks/routes-mini/child/routes.js @@ -0,0 +1,7 @@ +'use strict' + +module.exports = async function (app, opts) { + app.get('/', async function (req, reply) { + reply.status(200).send({ hooked: req.hooked }) + }) +} diff --git a/test/commonjs/autohooks/routes-mini/routes.js b/test/commonjs/autohooks/routes-mini/routes.js new file mode 100644 index 00000000..521b6527 --- /dev/null +++ b/test/commonjs/autohooks/routes-mini/routes.js @@ -0,0 +1,7 @@ +'use strict' + +module.exports = async function (app, opts) { + app.get('/', async function (req, reply) { + reply.status(200).send({ hooked: req.hooked }) + }) +} From d76f4c74d9aa9fada7e684dc742b5c7899d08b1d Mon Sep 17 00:00:00 2001 From: huodoo Date: Wed, 22 Jul 2026 22:18:13 +0800 Subject: [PATCH 2/2] style: lint fix --- test/commonjs/autohooks-basic-mini.js | 4 ++-- test/commonjs/autohooks/basic-mini.js | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/test/commonjs/autohooks-basic-mini.js b/test/commonjs/autohooks-basic-mini.js index 58d452ee..2fff595f 100644 --- a/test/commonjs/autohooks-basic-mini.js +++ b/test/commonjs/autohooks-basic-mini.js @@ -8,7 +8,7 @@ describe('Node test suite for autohooks-basic', function () { const app = Fastify() before(async function () { app.register(require('./autohooks/basic-mini')) - app.decorateRequest('hooked', "") + app.decorateRequest('hooked', '') await app.ready() }) @@ -27,6 +27,6 @@ describe('Node test suite for autohooks-basic', function () { const res = await app.inject({ url: '/child' }) assert.strictEqual(res.statusCode, 200) - assert.deepStrictEqual(JSON.parse(res.payload), { hooked: "" }) + assert.deepStrictEqual(JSON.parse(res.payload), { hooked: '' }) }) }) diff --git a/test/commonjs/autohooks/basic-mini.js b/test/commonjs/autohooks/basic-mini.js index 2c7c81f3..fc4a8ac3 100644 --- a/test/commonjs/autohooks/basic-mini.js +++ b/test/commonjs/autohooks/basic-mini.js @@ -4,8 +4,8 @@ const path = require('node:path') const autoLoad = require('../../../') module.exports = function (fastify, opts, next) { - fastify.log.error(__dirname); - + fastify.log.error(__dirname) + fastify.register(autoLoad, { dir: path.join(__dirname, 'routes-mini'), autoHooks: true @@ -13,4 +13,3 @@ module.exports = function (fastify, opts, next) { next() } -