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

Update middleware typings #1300

Open
wants to merge 1 commit into
base: next
Choose a base branch
from

Conversation

fugufish
Copy link
Contributor

@fugufish fugufish commented Sep 16, 2024

📝 Description

This makes middleware typings more accurate. This makes it so when working in typescript a Middlware doesn't error out on things such as name being included, it alos makes it so that instead of doing something like this:

import { Middleware, ServiceBroker, Transporters } from "moleculer";
import { decrypt, encrypt } from "../util/encryption";

export const EncryptionMiddleware: Middleware = {
  name: "EncryptionMiddleware",

  transporterSend(next: Transporters.Base["send"]): Transporters.Base["send"] {
    return async function (topic: string, data: Buffer, opts: any): Promise<void> {
      const res = Buffer.from(await encrypt(data));
      return next(topic, res, opts);
    };
  },

  transporterReceive(next: Transporters.Base["receive"]): Transporters.Base["receive"] {
    return async function (this: Transporters.Base, cmd: string, data: Buffer, s: any): Promise<void> {
      try {
        const res = Buffer.from(await decrypt(data));
        return next(cmd, res, s);
      } catch (e) {
        this.broker.logger.error(e);
      }
    };
  }
};

you can do this:

import { Transporters, type Middleware } from "moleculer";
import { decrypt, encrypt } from "../util/encryption.ts";

/**
 * This middleware encrypts and decrypts all transit data. This is similar to Moleculer's built in
 * encryption middleware differing in that it does not require the initialization vector to be 
 * configured in the broker options, but instead using a random IV at encryption time.
 */
export const EncryptionMiddleware: Middleware = {
  name: "EncryptionMiddleware",

  transporterSend(next) {
    return async function (topic, data, opts) {
      const res = Buffer.from(await encrypt(data))
      return next(topic, res, opts)
    }
  },
  transporterReceive(next) {
    return async function (this: Transporters.Base, cmd, data, s) {
      try {
        const res = Buffer.from(await decrypt(data))
        return next(cmd, res, s)
      } catch (e) {
        this.broker.logger.error(e)
      }
    }
  }
}

As you can see the typing on this is much cleaner, and easy to use

💎 Type of change

  • Typescript change

@fugufish fugufish changed the base branch from master to next September 16, 2024 16:59
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

Successfully merging this pull request may close these issues.

1 participant