adonisjs / transmit

A Server-Sent-Event module for AdonisJS
MIT License
71 stars 5 forks source link

Silent auth middleware usage in RouteHandlerModifier config property #20

Closed leytonlyy closed 5 months ago

leytonlyy commented 5 months ago

Package version

1.0.0

Describe the bug

I'm trying to use the silent auth middleware in the RouteHandlerModifier property when configuring transmit but when i do so the app crashes on boot. I cloned the 'Transmit-demo' repository and added the code stated in the docs in the config/transmit.ts file:

import { defineConfig } from '@adonisjs/transmit'
import { middleware } from '#start/kernel'

export default defineConfig({
  routeHandlerModifier(route) {
    if (route.getPattern() === '/__transmit/events') {
      route.middleware(middleware.auth())
      return
    }
  }
})

This is the error i get:

   TypeError: Cannot read properties of undefined (reading 'errorHandler')

   at <anonymous> start/kernel.ts:18
   13|  
   14|  /**
   15|   * The error handler is used to convert an exception
   16|   * to a HTTP response.
   17|   */
 ❯ 18|  server.errorHandler(() => import('#exceptions/handler'))
   19|  
   20|  /**
   21|   * The server middleware stack runs middleware on all the HTTP
   22|   * requests, even if there is no route registered for
   23|   * the request URL.

It appears that interacting with the middleware within the makes the app crash:

import { middleware } from '#start/kernel'
import { defineConfig } from '@adonisjs/transmit'

export default defineConfig({
  pingInterval: false,
  transport: null,
  routeHandlerModifier(route) {
    if (route.getPattern() === '/__transmit/events') {
      console.log(middleware)
      return
    }
  },
})

Generates the same error.

Reproduction repo

https://github.com/RomainLanz/transmit-demo/

RomainLanz commented 5 months ago

Hey @leytonlyy! πŸ‘‹πŸ»

Can you try the same while important the middleware stack inside the routeHandlerModifier function?

import { defineConfig } from '@adonisjs/transmit'

export default defineConfig({
  pingInterval: false,
  transport: null,
  async routeHandlerModifier(route) {
    const { middleware } = await import('#start/kernel')

    if (route.getPattern() === '/__transmit/events') {
      console.log(middleware)
      return
    }
  },
})
leytonlyy commented 5 months ago

Hi @RomainLanz, it worked thank you ! Do you know why this is happening ?

Also noticed that getPattern method returns the route pattern as __transmit/events instead of /__transmit/events as stated in the Adonis documentation of transmit

RomainLanz commented 5 months ago

Documentation has been updated. πŸ‘πŸ»