-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: switched to typescript and improved api
- Loading branch information
1 parent
88f90af
commit 76b3d64
Showing
10 changed files
with
185 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import {connect, Channel, Connection, Options} from 'amqplib'; | ||
|
||
class TopicConsumer { | ||
private connection: Connection; | ||
private channel: Channel; | ||
private exchange: string; | ||
private exchangeOptions: Options.AssertExchange; | ||
private queue: string; | ||
private queueOptions: Options.AssertQueue; | ||
private topics: string[]; | ||
private topicFunctions = {}; | ||
|
||
static create(exchange, queue) { | ||
return new TopicConsumer(exchange, queue); | ||
} | ||
|
||
constructor(exchange = null, queue = '') { | ||
this.exchange = exchange; | ||
this.queue = queue; | ||
} | ||
|
||
setExchange(exchange: string, options: Options.AssertExchange) { | ||
this.exchange = exchange; | ||
this.exchangeOptions = options; | ||
|
||
return this; | ||
} | ||
|
||
setQueue(queue: string, options: Options.AssertQueue) { | ||
this.queue = queue; | ||
this.queueOptions = options; | ||
|
||
return this; | ||
} | ||
|
||
subscribe(topic: string, callback: any) { | ||
this.topics = [...this.topics, topic]; | ||
this.topicFunctions[topic] = callback; | ||
|
||
return this; | ||
} | ||
|
||
async start(amqpUrl: string) { | ||
this.connection = await connect(amqpUrl); | ||
|
||
this.channel = await this.connection.createChannel(); | ||
|
||
await this.channel.assertExchange(this.exchange, 'topic', this.exchangeOptions); | ||
|
||
const q = await this.channel.assertQueue(this.queue, this.queueOptions); | ||
|
||
const uniqueTopics = [...new Set(this.topics)]; | ||
|
||
await Promise.all( | ||
uniqueTopics.map(async topic => | ||
await this.channel.bindQueue(q.queue, this.exchange, topic) | ||
) | ||
); | ||
|
||
return this.channel.consume(q.queue, async msg => { | ||
const topic = msg.fields.routingKey; | ||
|
||
const [key] = Object.keys(this.topicFunctions).filter(t => { | ||
const regexString = t.replace(/\*/g, '[^.]+').replace(/#/g, '.*'); | ||
const regex = new RegExp('^' + regexString + '$'); | ||
|
||
return topic.match(regex); | ||
}); | ||
|
||
const callback = this.topicFunctions[key]; | ||
|
||
if (!callback) { | ||
this.channel.ack(msg); | ||
return; | ||
} | ||
|
||
try { | ||
await callback(topic, JSON.parse(msg.content.toString())); | ||
this.channel.ack(msg); | ||
} catch (error) { | ||
this.channel.nack(msg) | ||
} | ||
|
||
}, {noAck: false,}); | ||
} | ||
|
||
stop() { | ||
this.connection.close(); | ||
} | ||
|
||
} | ||
|
||
export default TopicConsumer; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import TopicConsumer from './consumer'; | ||
import TopicProducer from './producer'; | ||
|
||
module.exports = { | ||
TopicProducer, | ||
TopicConsumer, | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import {connect, Connection, Channel, Options} from 'amqplib'; | ||
|
||
|
||
class TopicProducer { | ||
private connection: Connection; | ||
private channel: Channel; | ||
private exchange: string; | ||
private exchangeOptions: Options.AssertExchange; | ||
|
||
static create(connection) { | ||
return new TopicProducer(connection); | ||
} | ||
|
||
constructor(connection: Connection) { | ||
this.connection = connection; | ||
} | ||
|
||
setExchange(name: string, options: Options.AssertExchange) { | ||
this.exchange = name; | ||
this.exchangeOptions = options; | ||
} | ||
|
||
async start(amqpUrl) { | ||
if (!this.connection) { | ||
this.connection = await connect(amqpUrl); | ||
} | ||
|
||
this.channel = await this.connection.createChannel(); | ||
await this.channel.assertExchange('events', 'topic', {durable: true}); | ||
|
||
return this; | ||
} | ||
|
||
publish(topic, data) { | ||
const serialised = JSON.stringify(data); | ||
this.channel.publish(this.exchange, topic, new Buffer(serialised)); | ||
} | ||
|
||
stop() { | ||
this.connection.close(); | ||
} | ||
|
||
} | ||
|
||
export default TopicProducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"compilerOptions": { | ||
"lib": [ "es2015" ], | ||
"downlevelIteration": true, | ||
"outDir": "./lib", | ||
"allowJs": true, | ||
"target": "es5" | ||
}, | ||
"include": [ | ||
"./src/**/*" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters