-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nilesh Mali
authored
May 14, 2020
1 parent
18e224a
commit e0eb4bd
Showing
12 changed files
with
229 additions
and
45 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 |
---|---|---|
|
@@ -65,3 +65,7 @@ typings/ | |
|
||
# lock files | ||
package-lock.json | ||
yarn.lock | ||
|
||
# IDE | ||
.vscode/ |
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,6 +1,7 @@ | ||
language: node_js | ||
|
||
node_js: | ||
- "14" | ||
- "12" | ||
- "10" | ||
|
||
|
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,42 @@ | ||
|
||
import { FastifyPlugin } from 'fastify'; | ||
import { ConsumerGlobalConfig, ConsumerTopicConfig, KafkaConsumer, Message, MetadataOptions, Producer, ProducerGlobalConfig, ProducerTopicConfig } from 'node-rdkafka'; | ||
|
||
declare module 'fastify' { | ||
interface FastifyKafkaMessage extends Pick<Message, 'topic' | 'partition' | 'key'> { | ||
payload: unknown; | ||
} | ||
|
||
interface FastifyKafkaProducer { | ||
producer: Producer; | ||
push(message: FastifyKafkaMessage): void; | ||
stop(done: () => void): void; | ||
} | ||
|
||
interface FastifyKafkaConsumer extends Pick<KafkaConsumer, 'consume' | 'subscribe'> { | ||
consumer: KafkaConsumer; | ||
stop(done: () => void): void; | ||
} | ||
|
||
interface Kafka extends Pick<FastifyKafkaProducer, 'push'>, Pick<FastifyKafkaConsumer, 'consume' | 'subscribe'> { | ||
producer?: FastifyKafkaProducer; | ||
consumer?: FastifyKafkaConsumer; | ||
} | ||
interface FastifyInstance { | ||
kafka: Kafka; | ||
} | ||
} | ||
|
||
declare namespace fastifyKafka { | ||
export interface FastifyKafkaOptions { | ||
producer?: ProducerGlobalConfig; | ||
consumer?: ConsumerGlobalConfig; | ||
producerTopicConf?: ProducerTopicConfig; | ||
consumerTopicConf?: ConsumerTopicConfig; | ||
metadataOptions?: MetadataOptions; | ||
} | ||
} | ||
|
||
declare const fastifyKafka: FastifyPlugin<fastifyKafka.FastifyKafkaOptions>; | ||
|
||
export default fastifyKafka; |
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
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,52 @@ | ||
'use strict' | ||
|
||
const t = require('tap') | ||
const log = require('abstract-logging') | ||
const test = t.test | ||
|
||
const Consumer = require('../lib/consumer') | ||
|
||
const options = { | ||
'metadata.broker.list': '192.0.2.1:9092', | ||
'fetch.wait.max.ms': 10, | ||
'fetch.error.backoff.ms': 50, | ||
'group.id': 'new-group-id' | ||
} | ||
|
||
test('unreachable brokers', t => { | ||
t.plan(1) | ||
const consumer = new Consumer(options, log, (err) => { | ||
t.ok(err) | ||
}, {}, { timeout: 200 }) | ||
consumer.on('ready', t.error) | ||
}) | ||
|
||
test('error event before connection', t => { | ||
t.plan(1) | ||
const consumer = new Consumer(options, log, (err) => { | ||
t.ok(err) | ||
}, {}, { timeout: 200 }) | ||
consumer.consumer.emit('event.error', new Error('Test Error')) | ||
}) | ||
|
||
test('error event after connection', t => { | ||
t.plan(2) | ||
const opts = { ...options, 'metadata.broker.list': '127.0.0.1:9092' } | ||
const consumer = new Consumer(opts, log, (err) => { | ||
t.error(err) | ||
consumer.consumer.emit('event.error', new Error('Test Error')) | ||
}) | ||
consumer.on('error', t.ok) | ||
t.tearDown(() => consumer.stop()) | ||
}) | ||
|
||
test('empty message with data event', t => { | ||
t.plan(3) | ||
const opts = { ...options, 'metadata.broker.list': '127.0.0.1:9092' } | ||
const consumer = new Consumer(opts, log, (err) => { | ||
t.error(err) | ||
t.throws(() => consumer.consumer.emit('data')) | ||
}) | ||
consumer.on('error', t.ok) | ||
t.tearDown(() => consumer.stop()) | ||
}) |
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,35 @@ | ||
import Fastify, { FastifyKafkaConsumer, FastifyKafkaProducer, Kafka } from 'fastify'; | ||
import { expectAssignable } from 'tsd'; | ||
import fastifyKafka from '..'; | ||
|
||
const app = Fastify() | ||
|
||
app.register(fastifyKafka, { | ||
producer: { | ||
'metadata.broker.list': '127.0.0.1:9092', | ||
dr_cb: true | ||
}, | ||
consumer: { | ||
'metadata.broker.list': '127.0.0.1:9092', | ||
'group.id': "new-group-1", | ||
'fetch.wait.max.ms': 10, | ||
'fetch.error.backoff.ms': 50, | ||
}, | ||
consumerTopicConf: { | ||
'auto.offset.reset': 'earliest' | ||
}, | ||
producerTopicConf: { | ||
"request.timeout.ms": 10 | ||
}, | ||
metadataOptions: { | ||
timeout: 1000 | ||
} | ||
}); | ||
|
||
// Check whether all properties are merged successfully or not | ||
expectAssignable<Kafka>(app.kafka) | ||
expectAssignable<FastifyKafkaProducer | undefined>(app.kafka.producer) | ||
expectAssignable<FastifyKafkaConsumer | undefined>(app.kafka.consumer) | ||
expectAssignable<Function>(app.kafka.push) | ||
expectAssignable<Function>(app.kafka.consume) | ||
expectAssignable<Function>(app.kafka.subscribe) |
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,46 @@ | ||
'use strict' | ||
|
||
const t = require('tap') | ||
const log = require('abstract-logging') | ||
const test = t.test | ||
|
||
const Producer = require('../lib/producer') | ||
|
||
const options = { | ||
'metadata.broker.list': '192.0.2.1:9092', | ||
'socket.timeout.ms': 10, | ||
dr_cb: true | ||
} | ||
|
||
test('unreachable brokers', t => { | ||
t.plan(2) | ||
const producer = new Producer(options, log, (err) => { | ||
t.ok(err) | ||
producer.on('error', t.ok) | ||
producer.push() | ||
}, {}, { timeout: 200 }) | ||
}) | ||
|
||
test('error event before connection', t => { | ||
t.plan(1) | ||
const producer = new Producer(options, log, (err) => { | ||
t.ok(err) | ||
}, {}, { timeout: 200 }) | ||
producer.producer.emit('event.error', new Error('Test Error')) | ||
}) | ||
|
||
test('error event after connection', t => { | ||
t.plan(3) | ||
const opts = { ...options, 'metadata.broker.list': '127.0.0.1:9092' } | ||
const producer = new Producer(opts, log, (err) => { | ||
t.error(err) | ||
producer.producer.emit('event.error', new Error('Test Error')) | ||
}) | ||
producer.on('error', t.ok) | ||
producer.push({ | ||
topic: 'test', | ||
payload: 'hello world!', | ||
key: 'testKey' | ||
}) | ||
t.tearDown(() => producer.stop()) | ||
}) |
Oops, something went wrong.