-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added avro serializer. Added E2E test to test avro serializer and des…
…erializer
- Loading branch information
Showing
14 changed files
with
234 additions
and
58 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,16 @@ | ||
{ | ||
"presets": [ | ||
[ | ||
"@babel/preset-env", { | ||
"targets": { | ||
"node": "current" | ||
}, | ||
"debug": true | ||
}, | ||
"@babel/preset-typescript" | ||
] | ||
], | ||
"plugins": [ | ||
["@babel/plugin-proposal-decorators", { "legacy": true }], | ||
] | ||
} |
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 |
---|---|---|
|
@@ -2,5 +2,7 @@ src/ | |
tsconfig.json | ||
.github/ | ||
.gitignore | ||
.prettierrc | ||
kafka/ | ||
babel.config.js | ||
.babelrc | ||
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"singleQuote": true, | ||
"trailingComma": "all" | ||
} |
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 |
---|---|---|
|
@@ -18,7 +18,7 @@ export class KafkaResponseDeserializer | |
} | ||
|
||
return { | ||
id, | ||
key: id, | ||
response, | ||
timestamp, | ||
offset | ||
|
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,78 @@ | ||
import { Serializer } from "@nestjs/microservices"; | ||
import { Logger } from '@nestjs/common/services/logger.service'; | ||
import { ProducerRecord } from "kafkajs"; | ||
import { SchemaRegistry, readAVSC } from "@kafkajs/confluent-schema-registry"; | ||
import { SchemaRegistryAPIClientArgs } from "@kafkajs/confluent-schema-registry/dist/api" | ||
import { KafkaMessageSend, KafkaMessageObject } from "../interfaces"; | ||
|
||
type KafkaAvroRequestSerializerSchema = { | ||
topic: string; | ||
key?: string; | ||
value: string; | ||
} | ||
|
||
export type KafkaAvroRequestSerializerConfig = { | ||
schemas: KafkaAvroRequestSerializerSchema[], | ||
config: SchemaRegistryAPIClientArgs; | ||
schemaSeparator?: string; | ||
} | ||
|
||
export class KafkaAvroRequestSerializer | ||
implements Serializer<KafkaMessageSend, Promise<KafkaMessageSend>> { | ||
|
||
protected registry: SchemaRegistry; | ||
protected logger = new Logger(KafkaAvroRequestSerializer.name); | ||
protected schemas = new Map(); | ||
protected separator: string; | ||
|
||
constructor(options: KafkaAvroRequestSerializerConfig) { | ||
this.registry = new SchemaRegistry(options.config); | ||
// this.separator = options.schemaSeparator || '-'; | ||
let keySchema = null; | ||
options.schemas.forEach((obj) => { | ||
if (obj.key) { | ||
keySchema = readAVSC(obj.key); | ||
} | ||
// test.topic-Value | ||
const valueSchema = readAVSC(obj.value); | ||
// const valueSubject = valueSchema.namespace + this.separator + valueSchema.name; | ||
|
||
const a = { | ||
key: keySchema, | ||
value: valueSchema | ||
} | ||
|
||
this.schemas.set(obj.topic, a); | ||
}); | ||
|
||
} | ||
|
||
async serialize(value: KafkaMessageSend): Promise<KafkaMessageSend> { | ||
const outgoingMessage = value; | ||
|
||
try { | ||
const schemas = this.schemas.get(value.topic); | ||
|
||
// @todo - need to work out a way to better get the schema based on topic. | ||
const keyId = await this.registry.getLatestSchemaId(value.topic + '-Key') | ||
const valueId = await this.registry.getLatestSchemaId(value.topic + '-Value') | ||
|
||
const messages: Promise<KafkaMessageObject>[] = value.messages.map(async(origMessage) => { | ||
const encodedValue = await this.registry.encode(valueId, origMessage.value) | ||
const encodedKey = await this.registry.encode(keyId, origMessage.key) | ||
return { | ||
...origMessage, | ||
value: encodedValue, | ||
key: encodedKey | ||
}; | ||
}); | ||
|
||
const results = await Promise.all(messages); | ||
outgoingMessage.messages = results; | ||
} catch (e) { | ||
this.logger.error(e); | ||
} | ||
return outgoingMessage; | ||
} | ||
|
||
} |
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
Oops, something went wrong.