-
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.
[wip] rebase to new @davidkhala/pubsub
- Loading branch information
1 parent
22017c4
commit 7f94c75
Showing
9 changed files
with
152 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test |
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 |
---|---|---|
@@ -1,34 +1,77 @@ | ||
import DB from '@davidkhala/db'; | ||
import DB, {Connectable} from '@davidkhala/db'; | ||
|
||
export default class MQ extends DB { | ||
constructor({domain, port, name, username, password, dialect, driver}, connectionString, logger) { | ||
super({domain, port, name, username, password, dialect, driver}, connectionString, logger); | ||
} | ||
|
||
/** | ||
* @abstract | ||
* @return {Promise<Pub>|Pub} | ||
*/ | ||
get producer() { | ||
|
||
} | ||
|
||
/** | ||
* @abstract | ||
* @return {Promise<Sub>|Sub} | ||
*/ | ||
get consumer() { | ||
|
||
} | ||
|
||
/** | ||
* | ||
* @param {string} topic | ||
* @param {string} message | ||
* @param [options] | ||
* @abstract | ||
*/ | ||
* @return {Promise<Admin>|Admin} | ||
*/ | ||
get admin() { | ||
|
||
} | ||
|
||
} | ||
|
||
export class Pub extends Connectable { | ||
/** | ||
* | ||
* @abstract | ||
* @param {string} topic | ||
* @param {string} message | ||
* @param [options] | ||
*/ | ||
async send(topic, message, ...options) { | ||
|
||
} | ||
|
||
} | ||
|
||
export class Sub extends Connectable { | ||
/** | ||
* | ||
* @param {string} topic | ||
* @param [options] | ||
* | ||
* @abstract | ||
*/ | ||
* @param {string} topic | ||
* @param [options] | ||
*/ | ||
async subscribe(topic, ...options) { | ||
|
||
} | ||
|
||
/** | ||
* | ||
* @param {string} message | ||
* | ||
* @abstract | ||
*/ | ||
* @param {string} message | ||
*/ | ||
async acknowledge(message) { | ||
|
||
} | ||
|
||
} | ||
|
||
export class Admin extends Connectable { | ||
/** | ||
* @abstract | ||
*/ | ||
async listTopics() { | ||
|
||
} | ||
} |
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 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,4 @@ | ||
import '../index.js' | ||
it('OK', function (){ | ||
|
||
}) |
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 |
---|---|---|
@@ -1,55 +1,75 @@ | ||
import {Kafka} from 'kafkajs' | ||
|
||
|
||
export default class KafkaManager { | ||
|
||
/** | ||
* | ||
* @param {string[]} brokers | ||
* @param {string} [username] | ||
* @param {string} [password] | ||
* @param {string} [clientId] default to "kafkajs" | ||
*/ | ||
constructor(brokers, {username, password, clientId} = {}) { | ||
const config = { | ||
clientId, | ||
brokers, | ||
} | ||
if (username) { | ||
Object.assign(config, { | ||
ssl: true, | ||
sasl: { | ||
mechanism: 'plain', // scram-sha-256 or scram-sha-512 | ||
username, | ||
password | ||
}, | ||
}) | ||
} | ||
this.kafkaManager = new Kafka(config) | ||
} | ||
|
||
get admin() { | ||
if (!this._admin) { | ||
this._admin = this.kafkaManager.admin() | ||
} | ||
return this._admin | ||
} | ||
|
||
async connect() { | ||
await this.admin.connect() | ||
} | ||
|
||
async disconnect() { | ||
await this.admin.disconnect() | ||
} | ||
|
||
async listTopics() { | ||
return this.admin.listTopics() | ||
} | ||
|
||
producer() { | ||
return this.kafkaManager.producer() | ||
} | ||
import kafkajs from 'kafkajs'; | ||
import PubSub, {Admin as AbstractAdmin} from '@davidkhala/pubsub'; | ||
const {Admin, Kafka} = kafkajs | ||
|
||
export default class KafkaManager extends PubSub { | ||
|
||
/** | ||
* | ||
* @param {string[]} brokers | ||
* @param {string} [username] | ||
* @param {string} [password] | ||
* @param {string} [clientId] default to "kafkajs" | ||
* @param logger | ||
*/ | ||
constructor(brokers, {username, password, clientId} = {}, logger) { | ||
super({name: clientId, username, password}, undefined, logger); | ||
const config = { | ||
clientId, | ||
brokers, | ||
}; | ||
if (username) { | ||
Object.assign(config, { | ||
ssl: true, | ||
sasl: { | ||
mechanism: 'plain', // scram-sha-256 or scram-sha-512 | ||
username, | ||
password | ||
}, | ||
}); | ||
} | ||
this.kafkaManager = new Kafka(config); | ||
} | ||
|
||
get admin() { | ||
if (!this._admin) { | ||
this._admin = new KafkaAdmin(this.kafkaManager.admin()); | ||
} | ||
return this._admin; | ||
} | ||
|
||
|
||
get producer() { | ||
return this.kafkaManager.producer(); | ||
} | ||
|
||
get consumer() { | ||
return this.kafkaManager.consumer(); | ||
} | ||
|
||
} | ||
|
||
export class KafkaAdmin extends AbstractAdmin { | ||
/** | ||
* | ||
* @param {Admin} admin | ||
*/ | ||
constructor(admin) { | ||
super(); | ||
this.admin = admin; | ||
} | ||
|
||
async listTopics() { | ||
return this.admin.listTopics(); | ||
} | ||
|
||
async _connect() { | ||
await this.admin.connect(); | ||
return true; | ||
} | ||
|
||
async disconnect() { | ||
await this.admin.disconnect(); | ||
} | ||
|
||
} |
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 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 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 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