@@ -7,19 +7,38 @@ import {
77 SenderOptions ,
88 EventContext ,
99 Message ,
10+ Dictionary ,
1011} from "rhea"
11- import { openLink } from "./utils.js"
12- import { createAddressFrom } from "./message.js"
12+ import {
13+ Offset ,
14+ openLink ,
15+ SourceFilter ,
16+ STREAM_FILTER_MATCH_UNFILTERED ,
17+ STREAM_FILTER_SPEC ,
18+ STREAM_OFFSET_SPEC ,
19+ } from "./utils.js"
20+ import { createConsumerAddressFrom } from "./message.js"
21+ import { QueueOptions } from "./message.js"
1322
1423export type ConsumerMessageHandler = ( message : Message ) => void
1524
16- export type CreateConsumerParams = {
25+ export type StreamOptions = {
26+ name : string
27+ offset ?: Offset
28+ filterValues ?: string [ ]
29+ matchUnfiltered ?: boolean
30+ }
31+
32+ export type SourceOptions = { stream : StreamOptions } | { queue : QueueOptions }
33+
34+ export type CreateConsumerParams = SourceOptions & {
1735 messageHandler : ConsumerMessageHandler
1836}
1937
2038const getConsumerReceiverLinkConfigurationFrom = (
2139 address : string ,
22- consumerId : string
40+ consumerId : string ,
41+ filter ?: SourceFilter
2342) : SenderOptions | ReceiverOptions => ( {
2443 snd_settle_mode : 0 ,
2544 rcv_settle_mode : 0 ,
@@ -31,6 +50,7 @@ const getConsumerReceiverLinkConfigurationFrom = (
3150 timeout : 0 ,
3251 dynamic : false ,
3352 durable : 0 ,
53+ filter,
3454 } ,
3555} )
3656
@@ -41,27 +61,28 @@ export interface Consumer {
4161}
4262
4363export class AmqpConsumer implements Consumer {
44- static async createFrom (
45- connection : Connection ,
46- consumersList : Map < string , Consumer > ,
47- queueName : string ,
48- params : CreateConsumerParams
49- ) {
64+ static async createFrom ( connection : Connection , consumersList : Map < string , Consumer > , params : CreateConsumerParams ) {
5065 const id = generate_uuid ( )
51- const address = createAddressFrom ( { queue : { name : queueName } } )
66+ const address = createConsumerAddressFrom ( params )
67+ const filter = createConsumerFilterFrom ( params )
5268 if ( ! address ) throw new Error ( "Consumer must have an address" )
5369
54- const receiverLink = await AmqpConsumer . openReceiver ( connection , address , id )
70+ const receiverLink = await AmqpConsumer . openReceiver ( connection , address , id , filter )
5571 return new AmqpConsumer ( id , connection , consumersList , receiverLink , params )
5672 }
5773
58- private static async openReceiver ( connection : Connection , address : string , consumerId : string ) : Promise < Receiver > {
74+ private static async openReceiver (
75+ connection : Connection ,
76+ address : string ,
77+ consumerId : string ,
78+ filter ?: SourceFilter
79+ ) : Promise < Receiver > {
5980 return openLink < Receiver > (
6081 connection ,
6182 ReceiverEvents . receiverOpen ,
6283 ReceiverEvents . receiverError ,
6384 connection . open_receiver . bind ( connection ) ,
64- getConsumerReceiverLinkConfigurationFrom ( address , consumerId )
85+ getConsumerReceiverLinkConfigurationFrom ( address , consumerId , filter )
6586 )
6687 }
6788
@@ -98,3 +119,25 @@ export class AmqpConsumer implements Consumer {
98119 if ( this . consumersList . has ( this . _id ) ) this . consumersList . delete ( this . _id )
99120 }
100121}
122+
123+ function createConsumerFilterFrom ( params : CreateConsumerParams ) : SourceFilter | undefined {
124+ if ( "queue" in params ) {
125+ return undefined
126+ }
127+ if ( ! params . stream . offset && ! params . stream . filterValues ) {
128+ throw new Error ( "At least one between offset and filterValues must be set when using filtering" )
129+ }
130+
131+ const filters : Dictionary < string | bigint | boolean | string [ ] > = { }
132+ if ( params . stream . offset ) {
133+ filters [ STREAM_OFFSET_SPEC ] = params . stream . offset . toValue ( )
134+ }
135+ if ( params . stream . filterValues ) {
136+ filters [ STREAM_FILTER_SPEC ] = params . stream . filterValues
137+ }
138+ if ( params . stream . matchUnfiltered ) {
139+ filters [ STREAM_FILTER_MATCH_UNFILTERED ] = params . stream . matchUnfiltered
140+ }
141+
142+ return filters
143+ }
0 commit comments