Code Monkey home page Code Monkey logo

Comments (4)

adenhertog avatar adenhertog commented on August 17, 2024

hi @vsabirov, I had a read through the rabbit implementation again. Generally one of the goals of this library is to abstract away transport-specific features so that they're largely interchangeable. With that said, the ability to specify custom headers is understandable and I wanted to confirm some behaviour with you.

If you were to do Bus.publish(yourEvent, { attributes: { 'x-delay': 100 } }) then you'd expect this to be added directly against the header.

Currently per rabbitmq-transport.ts:367 this is being serialized against a different key like attributes: JSON.stringify({ 'x-delay': 100 }) ie:

image

Would this work for you if all attributes were spread onto the headers? ie:

headers: {
  ...messageOptions.attributes
}

from bus.

vsabirov avatar vsabirov commented on August 17, 2024

@adenhertog Perhaps the ability to specify transport-agnostic attributes in a special header should be kept as is. Sure, it will work fine, however i'm afraid that the user can overwrite some special RMQ headers by accident and will be forced to rename their attributes to avoid collisions.

Perhaps something like

const rawHeaders = messageOptions.attributes.rawHeaders

and

headers: {
  attributes: messageOptions.attributes ? JSON.stringify(messageOptions.attributes) : undefined,
  stickyAttributes: messageOptions.stickyAttributes ? JSON.stringify(messageOptions.stickyAttributes) : undefined

  ...rawHeaders
}

And to use that you'll just have to specify a rawHeaders object in the message options that will be additionally spread into the headers.

from bus.

adenhertog avatar adenhertog commented on August 17, 2024

@vsabirov I'm happy with what you've proposed - the reasoning makes a lot of sense. I can't get around to this immediately, but I'm open to accepting PRs if you need this soon

from bus.

talentumtuum avatar talentumtuum commented on August 17, 2024

@adenhertog I propose to hide access to internal attributes from regular use with symbols. The user should understand that this is an internal API.

I see a solution to this problem as follows

@node-ts/bus-messages

export kInternalAttributes = Symbol('@node-ts/bus-messages/internal-attributes')

export type InternalAttributes<T> =  T

export interface MessageAttributes<
  AttributesType extends MessageAttributeMap = MessageAttributeMap,
  StickyAttributesType extends MessageAttributeMap = MessageAttributeMap,
  InternalAttributesType = InternalAttributes<unknown>
> 
    correlationId?: Uuid;
    attributes: AttributesType;
    stickyAttributes: StickyAttributesType;
    [kInternalAttributes]: InternalAttributesType
}

@node-ts/bus-rabbitmq

export const kRabbitMQHeaders = Symbol('@node-ts/bus-rabbitmq/rabbitmq-headers')

export interface RabbitMQInternalAttributes = {
  headers: MessageAttributeMap
}

private async publishMessage(
    message: Message,
    messageOptions: MessageAttributes<..., ..., RabbitMQInternalAttributes> = { attributes: {}, stickyAttributes: {}, internalAttributes: {} }
  ): Promise<void> {
    await this.assertExchange(message.$name)
    const payload = this.coreDependencies.messageSerializer.serialize(message)
    const internalAttributes = messageOptions[kInternalAttributes]
    this.channel.publish(message.$name, '', Buffer.from(payload), {
      correlationId: messageOptions.correlationId,
      messageId: uuid.v4(),
      headers: {
        attributes: messageOptions.attributes
          ? JSON.stringify(messageOptions.attributes)
          : undefined,
        stickyAttributes: messageOptions.stickyAttributes
          ? JSON.stringify(messageOptions.stickyAttributes)
          : undefined
        ...internalAttributes.headers
      }
    })
  }

Usage

import { kInternalAttributes } from '@node-ts/bus-messages'

bus.publish(new Message(), {
  [kInternalAttributes]: { // typed
    headers: {
      'x-delay': 5000
    }
  }
})

This is a draft solution, I'm just suggesting a concept

from bus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.