futurestudio / hapi-rate-limitor

A hapi plugin for rate limiting. Simple and easy.
MIT License
43 stars 8 forks source link

HapiJs onPreResponse request lifecycle event getting skipped #238

Closed ak-notify closed 6 months ago

ak-notify commented 6 months ago

If an Error is thrown from http handler with hapi-rate-limitor in use, HapiJs onPreResponse request lifecycle event is not triggered for subsequent registered event handlers.

Although if you alter the order of plugins, you can get access to the event.

marcuspoehls commented 6 months ago

Hey, thank you for reaching out. Can you please share a small reproduction or code samples when the issue comes up and how to solve it by changing the order of plugins?

ak-notify commented 6 months ago

pluginA.js

  const pluginA = {
    name: "plugin-a",
    register: (server) => {
      server.ext("onPreResponse", (request, h) => {
          if(request.response.isBoom) {
             // do something here
          }
      }
    }

plugins.js

       // import hapi-rate-limitor
       // import pluginA

       /*
        * if plugins used in this order,
        * plugins after hapi-rate-limitor listening to `onPreResponse`  
        * won't be triggered if Error is thrown from http handler
        * because hapi-rate-limitor does not use h.continue from its `onPreResponse` handler
        */
       return [hapi-rate-limitor, pluginA]; 

      /*
       * Hack -  register hapi-rate-limitor as the last plugin
       */
      return [pluginA, hapi-rate-limitor]; 

@marcuspoehls - I have made the necessary changes to fix this, I can raise it as PR if you are ok.

marcuspoehls commented 6 months ago

@ak-notify a PR would be great, please send one 🙂

marcuspoehls commented 6 months ago

@ak-notify I’ve published version 4.0.0 to NPM. It contains the changes to proceed with the plugin onPreResponse chain. Thank you for your contribution! It’s very welcome 🙂