11import { inspect } from "node:util"
22import { isDate } from "node:util/types"
3- import { Message , MessageApplicationProperties , MessageProperties } from "../publisher"
3+ import {
4+ AmqpByte ,
5+ Message ,
6+ MessageAnnotations ,
7+ MessageAnnotationsValue ,
8+ MessageApplicationProperties ,
9+ MessageProperties ,
10+ } from "../publisher"
411import { DataWriter } from "../requests/data_writer"
512
613const FormatCodeType = {
@@ -23,6 +30,7 @@ const FormatCode = {
2330 Null : 0x40 ,
2431 SmallUlong : 0x53 ,
2532 Uint : 0x70 ,
33+ Ubyte : 0x50 ,
2634 Int : 0x71 ,
2735 Timestamp : 0x83 ,
2836} as const
@@ -35,14 +43,14 @@ const PropertySizeDescription =
3543
3644type MessageApplicationPropertiesList = { key : string ; value : string | number } [ ]
3745
38- type MessageAnnotationsList = { key : string ; value : string | number } [ ]
46+ type MessageAnnotationsList = { key : string ; value : MessageAnnotationsValue } [ ]
3947
4048export function amqpEncode (
4149 writer : DataWriter ,
4250 { content, messageProperties, applicationProperties, messageAnnotations } : Message
4351) : void {
4452 writer . writeUInt32 ( messageSize ( { content, messageProperties, applicationProperties, messageAnnotations } ) )
45- writeMessageAnnotations ( writer , toList ( messageAnnotations ) )
53+ writeMessageAnnotations ( writer , toAnnotationsList ( messageAnnotations ) )
4654 writeProperties ( writer , messageProperties )
4755 writeApplicationProperties ( writer , toList ( applicationProperties ) )
4856 writeContent ( writer , content )
@@ -53,7 +61,7 @@ export function messageSize({ content, messageProperties, applicationProperties,
5361 lengthOfContent ( content ) +
5462 lengthOfProperties ( messageProperties ) +
5563 lengthOfApplicationProperties ( toList ( applicationProperties ) ) +
56- lengthOfMessageAnnotations ( toList ( messageAnnotations ) )
64+ lengthOfMessageAnnotations ( toAnnotationsList ( messageAnnotations ) )
5765 )
5866}
5967
@@ -121,7 +129,11 @@ function writeMessageAnnotations(writer: DataWriter, messageAnnotationsList: Mes
121129 . filter ( ( elem ) => elem . key )
122130 . forEach ( ( elem ) => {
123131 amqpWriteString ( writer , elem . key )
124- typeof elem . value === "string" ? amqpWriteString ( writer , elem . value ) : amqpWriteIntNumber ( writer , elem . value )
132+ if ( elem . value instanceof AmqpByte ) {
133+ amqpWriteByte ( writer , elem . value )
134+ } else {
135+ typeof elem . value === "string" ? amqpWriteString ( writer , elem . value ) : amqpWriteIntNumber ( writer , elem . value )
136+ }
125137 } )
126138}
127139
@@ -188,8 +200,12 @@ function getPropertySize(properties: MessageProperties): number {
188200 )
189201}
190202
191- function getListSize ( list : MessageApplicationPropertiesList | MessageAnnotationsList ) : number {
192- return list . reduce ( ( sum , elem ) => sum + getSizeOf ( elem . key ) + getSizeOf ( elem . value ) , 0 )
203+ function getListSize ( list : MessageAnnotationsList ) : number {
204+ return list . reduce (
205+ ( sum : number , elem : { key : string ; value : MessageAnnotationsValue } ) =>
206+ sum + getSizeOf ( elem . key ) + getSizeOf ( elem . value ) ,
207+ 0
208+ )
193209}
194210
195211function amqpWriteString ( writer : DataWriter , data ?: string ) : void {
@@ -242,6 +258,11 @@ function amqpWriteIntNumber(writer: DataWriter, data?: number): void {
242258 writer . writeInt32 ( data )
243259}
244260
261+ function amqpWriteByte ( writer : DataWriter , data : AmqpByte ) : void {
262+ writer . writeByte ( FormatCode . Ubyte )
263+ writer . writeByte ( data . byteValue )
264+ }
265+
245266function amqpWriteBuffer ( writer : DataWriter , data ?: Buffer ) : void {
246267 if ( ! data || ! data . length ) {
247268 return amqpWriteNull ( writer )
@@ -269,11 +290,15 @@ function amqpWriteDate(writer: DataWriter, date?: Date): void {
269290 writer . writeUInt64 ( BigInt ( date . getTime ( ) ) )
270291}
271292
272- function getSizeOf ( value ?: string | Date | number | Buffer ) : number {
293+ function getSizeOf ( value ?: string | Date | number | Buffer | AmqpByte ) : number {
273294 if ( ! value ) {
274295 return 1
275296 }
276297
298+ if ( value instanceof AmqpByte ) {
299+ return 1 + 1
300+ }
301+
277302 if ( typeof value === "string" ) {
278303 const count = Buffer . from ( value ) . length
279304 return count <= 255 ? 1 + 1 + count : 1 + 4 + count
@@ -300,3 +325,10 @@ function toList(applicationProperties?: MessageApplicationProperties): MessageAp
300325 return { key : elem [ 0 ] , value : elem [ 1 ] }
301326 } )
302327}
328+
329+ function toAnnotationsList ( annotations ?: MessageAnnotations ) : MessageAnnotationsList {
330+ if ( ! annotations ) return [ ]
331+ return Object . entries ( annotations ) . map ( ( elem ) => {
332+ return { key : elem [ 0 ] , value : elem [ 1 ] }
333+ } )
334+ }
0 commit comments