mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import { isNil } from '@nestjs/common/utils/shared.utils';
|
|
import { KafkaParserConfig } from '../interfaces';
|
|
|
|
export class KafkaParser {
|
|
protected readonly keepBinary: boolean;
|
|
|
|
constructor(config?: KafkaParserConfig) {
|
|
this.keepBinary = (config && config.keepBinary) || false;
|
|
}
|
|
|
|
public parse<T = any>(data: any): T {
|
|
if (!this.keepBinary) {
|
|
data.value = this.decode(data.value);
|
|
}
|
|
|
|
if (!isNil(data.key)) {
|
|
data.key = this.decode(data.key);
|
|
}
|
|
if (!isNil(data.headers)) {
|
|
const decodeHeaderByKey = (key: string) => {
|
|
data.headers[key] = this.decode(data.headers[key]);
|
|
};
|
|
Object.keys(data.headers).forEach(decodeHeaderByKey);
|
|
} else {
|
|
data.headers = {};
|
|
}
|
|
return data;
|
|
}
|
|
|
|
public decode(value: Buffer): object | string | null {
|
|
if (isNil(value)) {
|
|
return null;
|
|
}
|
|
|
|
let result = value.toString();
|
|
const startChar = result.charAt(0);
|
|
|
|
// only try to parse objects and arrays
|
|
if (startChar === '{' || startChar === '[') {
|
|
try {
|
|
result = JSON.parse(value.toString());
|
|
} catch (e) {}
|
|
}
|
|
return result;
|
|
}
|
|
}
|