moleculerjs / moleculer

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

Can't use events RegExp without '?' symbol #833

Closed zeruk closed 3 years ago

zeruk commented 3 years ago

Prerequisites

Current Behavior

It is impossible to use RegExps which do not contain question sign(?) in events.

Expected Behavior

It is possible to use RegExps in events

Failure Information

It corresponds to code in https://github.com/moleculerjs/moleculer/blob/2f7d3d0d1a39511bc6bb9b71c6729326a3e8afad/src/utils.js#L268

Steps to Reproduce

Example: 1) try to emit event 'qwer.asd.zxc' 2) try to catch it at 'qwer.(asd|fgh).zxc'. 3) ??? (No, you can't)

Reproduce code snippet

https://codesandbox.io/s/moleculer-sample-forked-8rrk9?file=/index.js

const { ServiceBroker } = require("moleculer");

const broker = new ServiceBroker();

broker.createService({
  name: "greeter",
  actions: {},
  events: {
    "qwer.asd.zxc": {
      handler() {
        this.logger.info("====== qwer.asd.zxc pattern is working ======");
      }
    },
    "qwer.(asd|fgh).zxc": {
      handler() {
        this.logger.info("====== qwer.(asd|fgh).zxc pattern is working ======"); // <= isn't
      }
    }
  }
});

broker.start().then(() => {
  setTimeout(() => {
    broker.emit("qwer.asd.zxc", { name: "CodeSandbox" });
  }, 500);
});
icebob commented 3 years ago

The event subscription didn't design to support regex, so it's not a bug, it can be an enhancement to support it.

zeruk commented 3 years ago

But it supports :D Example: 'qwer.(asd|fgh)?zxc'

after all the replacements it becomes '^qwer.(asd|fgh).zxc$'

Just from the code I attached in issue the regexp must have '?' sign, which then leads to be replaced with '.'