Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two fastify instances, two indexPattern that seems been ignored #298

Open
2 tasks done
diego-betto opened this issue Jan 25, 2023 · 2 comments
Open
2 tasks done

Two fastify instances, two indexPattern that seems been ignored #298

diego-betto opened this issue Jan 25, 2023 · 2 comments

Comments

@diego-betto
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.12.0

Plugin version

5.7.1

Node.js version

v18.12.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Ubuntu 22.04.1 LTS

Description

When starting the app it seems that indexPattern is ignored and all plugins are loaded also for appV1Private that has a different pattern regex.

Any idea?

Steps to Reproduce

These are the relevant parts of the code

Main server, two Fastify instances. Each instance with own routes and own port.

...
const server = fastify({})
const serverPrivate = fastify({})
...

server.register(appV1, { prefix: '/api/v1' })
serverPrivate.register(appV1Private, {prefix: '/api-private/v1'})

...

// public
server.listen({ port: 8443 }, (err, address) => {
  if (err) {
    console.error(err)
    process.exit(1)
  }
  console.log(`Public Server listening ${address}`)
})


// private
serverPrivate.listen({ port: 9443 }, (err, address) => {
  if (err) {
    console.error(err)
    process.exit(1)
  }
  console.log(`Private server listening ${address}`)
})

appV1
This should load only plugins with files like module.ts, right?

import { FastifyPluginCallback, HTTPMethods } from 'fastify'
import { join } from 'path'
import fastifyAutoload from '@fastify/autoload'
import logger from '@root/utils/log';

const app: FastifyPluginCallback = function (fastify, opts, next): void {
  /** AUTOLOAD PLUGINS */
  fastify.register(fastifyAutoload, {
    dir: join(__dirname, 'plugins'),
    dirNameRoutePrefix: false,
    indexPattern: /.*module(\.ts|\.js|\.cjs|\.mjs)$/,
    maxDepth: 1,
    encapsulate: false,
    logLevel: 'info',
  })

  next()
}

export default app;

appV1Private
This should load only plugins with files like modulePrivate.ts, right?

import { FastifyPluginCallback, HTTPMethods } from 'fastify'
import { join } from 'path'
import fastifyAutoload from '@fastify/autoload'
import logger from '@root/utils/log';

const app: FastifyPluginCallback = function (fastify, opts, next): void {
  /** AUTOLOAD PLUGINS */
  fastify.register(fastifyAutoload, {
    dir: join(__dirname, 'plugins'),
    dirNameRoutePrefix: false,
    indexPattern: /.*modulePrivate(\.ts|\.js|\.cjs|\.mjs)$/,
    maxDepth: 1,
    encapsulate: false,
    logLevel: 'info',
  })

  next()
}

export default app;

Inside plugins there are many Fastify plugins BUT only in one there is a file named modulePrivate.ts

When starting the app it seems that indexPattern is ignored and all plugins are loaded also for appV1Private

Any idea?

Expected Behavior

  • appV1 should load only plugins with module.ts inside
  • appV1Private should load only plugins with modulePrivate.ts inside.
@climba03003
Copy link
Member

Can you provide a repository that contain the runnable code to demonstrate the problem?
Files structures is important to distinguish the problem.

Basically, when a file is matched the indexPattern, it should only load that particular file.

@diego-betto
Copy link
Author

I will. For now I'm using this "fix" with matchFilter

matchFilter: (p) => p.split("/").at(-1).indexOf('modulePrivate.') === 0,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants