@nestjstools/messaging-google-pubsub-extension
Version:
Extension to handle messages and dispatch them over Google PubSub
51 lines • 2.76 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GooglePubSubMessagingConsumer = void 0;
const google_pub_sub_channel_1 = require("../channel/google-pub-sub.channel");
const messaging_1 = require("@nestjstools/messaging");
const common_1 = require("@nestjs/common");
const messaging_2 = require("@nestjstools/messaging");
const const_1 = require("../const");
let GooglePubSubMessagingConsumer = class GooglePubSubMessagingConsumer {
channel;
async consume(dispatcher, channel) {
this.channel = channel;
const manager = this.channel.pubSubManager;
const [topics] = await manager.getTopics();
const topicExists = topics.some(t => t.name.endsWith(channel.config.topicName));
if (!topicExists && channel.config.autoCreate) {
await manager.createTopic(channel.config.topicName);
}
const topic = manager.topic(channel.config.topicName);
const [subscriptions] = await topic.getSubscriptions();
const subscriptionExists = subscriptions.some(sub => sub.name.endsWith(channel.config.subscriptionName));
if (!subscriptionExists && channel.config.autoCreate) {
await topic.createSubscription(channel.config.subscriptionName);
}
manager.subscription(this.channel.config.subscriptionName).on('message', message => {
dispatcher.dispatch(new messaging_1.ConsumerMessage(JSON.parse(message.data.toString()), message.attributes[const_1.ROUTING_KEY_ATTRIBUTE_NAME]));
message.ack();
});
return Promise.resolve();
}
async onError(errored, channel) {
return Promise.resolve();
}
async onApplicationShutdown(signal) {
if (this.channel) {
await this.channel.pubSubManager.close();
}
}
};
exports.GooglePubSubMessagingConsumer = GooglePubSubMessagingConsumer;
exports.GooglePubSubMessagingConsumer = GooglePubSubMessagingConsumer = __decorate([
(0, common_1.Injectable)(),
(0, messaging_2.MessageConsumer)(google_pub_sub_channel_1.GooglePubSubChannel)
], GooglePubSubMessagingConsumer);
//# sourceMappingURL=google-pub-sub-messaging.consumer.js.map