moleculerjs / moleculer

:rocket: Progressive microservices framework for Node.js
https://moleculer.services/
MIT License
6.16k stars 586 forks source link

Export middleware order array to maintain consistency with internal middlewares ordering #1034

Closed georgedbarr closed 2 years ago

georgedbarr commented 2 years ago

Is your feature request related to a problem? Please describe.

Started using the recent updates to manage middleware ordering and adding custom middleware in place of an existing one as described in https://github.com/moleculerjs/moleculer/pull/1012

Previously we used all the internal Moleculer middlewares and overwriting the ErrorHandler with our own middleware like so:

const { Middlewares } = require('moleculer');
Middlewares.ErrorHandler = ErrorHandler;

This meant that ordering was maintained with the internal middlewares ordering.

Now with the new reordering middleware config, we can define our custom middleware like so:

const middlewares = [
  'ActionHook',
  'Validator',
  'Bulkhead',
  'Cacher',
  'ContextTracker',
  'CircuitBreaker',
  'Timeout',
  'Retry',
  'Fallback',
  { ...ErrorHandler() },
  'Tracing',
  'Metrics',
  'Debounce',
  'Throttle',
];

The concern is that if for whatever reason, the internal middleware was updated and the internal ordering was changed, then this could lead to issues where the list we have defined is now out of sync and could lead to ordering issues.

Describe the solution you'd like

Moleculer package to export the default middleware ordering array

icebob commented 2 years ago

In https://github.com/moleculerjs/moleculer/commit/b48ec4f3d39951ad433a64c4c36ecdc2af824c24 it will be available as

require("moleculer").INTERNAL_MIDDLEWARES
// or 
ServiceBroker.INTERNAL_MIDDLEWARES

And it contains:

[
    "ActionHook",
    "Validator",
    "Bulkhead",
    "Cacher",
    "ContextTracker",
    "CircuitBreaker",
    "Timeout",
    "Retry",
    "Fallback",
    "ErrorHandler",
    "Tracing",
    "Metrics",
    "Debounce",
    "Throttle"
];
georgedbarr commented 2 years ago

Brilliant, thanks!