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

feat: add wildcard topic matcher, support * and # in routingKeys #668

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ packages/testing/*/lib
coverage/
tsconfig.build.tsbuildinfo
yarn-error.log
integration/rabbitmq/dist
integration/rabbitmq/dist
underfisk marked this conversation as resolved.
Show resolved Hide resolved
package-lock.json
20 changes: 10 additions & 10 deletions integration/rabbitmq/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@
"@types/express": "4.17.13",
"@types/jest": "27.0.3",
"@types/node": "^18.16.19",
"@types/supertest": "2.0.7",
"jest": "27.4.3",
"nodemon": "2.0.22",
"prettier": "2.8.8",
"supertest": "6.3.3",
"ts-jest": "28.0.8",
"ts-node": "10.9.1",
"tsconfig-paths": "4.2.0",
"tslint": "5.14.0",
"typescript": "4.9.5"
"@types/supertest": "^2.0.7",
"jest": "^28.0.0",
"nodemon": "^2.0.22",
"prettier": "^2.8.8",
"supertest": "^6.3.3",
"ts-jest": "^28.0.8",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"tslint": "^5.14.0",
"typescript": "^4.9.5"
},
"jest": {
"moduleFileExtensions": [
Expand Down
1 change: 1 addition & 0 deletions packages/rabbitmq/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"url": "https://github.com/golevelup/nestjs/issues"
},
"dependencies": {
"@artie-owlet/amqp-routing-match": "^1.0.0",
"@golevelup/nestjs-common": "^2.0.0",
"@golevelup/nestjs-discovery": "^4.0.0",
"@golevelup/nestjs-modules": "^0.7.0",
Expand Down
26 changes: 23 additions & 3 deletions packages/rabbitmq/src/amqp/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from 'rxjs';
import { catchError, filter, first, map, take, timeout } from 'rxjs/operators';
import { randomUUID } from 'crypto';
import { TopicPattern } from '@artie-owlet/amqp-routing-match';
import { defaultAssertQueueErrorHandler, RabbitMQQueueConfig } from '..';
import {
ConnectionInitOptions,
Expand Down Expand Up @@ -524,6 +525,15 @@ export class AmqpConnection {
): Promise<ConsumerTag> {
const queue = await this.setupQueue(rpcOptions, channel);

let topicPatterns: TopicPattern[];
underfisk marked this conversation as resolved.
Show resolved Hide resolved
if (Array.isArray(rpcOptions.routingKey)) {
topicPatterns = rpcOptions.routingKey.map(
(routingKey) => new TopicPattern(routingKey)
);
} else if (rpcOptions.routingKey) {
topicPatterns = [new TopicPattern(rpcOptions.routingKey)];
}

const { consumerTag }: { consumerTag: ConsumerTag } = await channel.consume(
queue,
async (msg) => {
Expand All @@ -532,9 +542,19 @@ export class AmqpConnection {
throw new Error('Received null message');
}

if (rpcOptions.routingKey !== msg.fields.routingKey) {
channel.nack(msg, false, true);
return;
if (
topicPatterns &&
!topicPatterns.some((topicPattern) =>
topicPattern.match(msg.fields.routingKey || '')
)
) {
throw new Error(
`Received message with routing key: '${
msg.fields.routingKey
}' which can't be handled due to defined routing keys: ${JSON.stringify(
rpcOptions.routingKey
)}`
);
}

const response = await this.handleMessage(
Expand Down
Loading
Loading