2026-05-12 19:38:23 +07:00
|
|
|
import { Transform, TransformCallback } from "stream";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Transform stream untuk memfilter audio packets yang terlalu kecil
|
|
|
|
|
* Packet yang terlalu kecil kemungkinan gagal didekripsi oleh Discord
|
|
|
|
|
*/
|
|
|
|
|
export class PacketFilter extends Transform {
|
2026-05-13 15:54:54 +07:00
|
|
|
private minPacketSize: number;
|
|
|
|
|
private filteredCount: number = 0;
|
|
|
|
|
private totalCount: number = 0;
|
2026-05-12 19:38:23 +07:00
|
|
|
|
2026-05-13 15:54:54 +07:00
|
|
|
constructor(minPacketSize: number = 10) {
|
|
|
|
|
super();
|
|
|
|
|
this.minPacketSize = minPacketSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_transform(
|
|
|
|
|
chunk: Buffer,
|
|
|
|
|
encoding: string,
|
|
|
|
|
callback: TransformCallback,
|
|
|
|
|
): void {
|
|
|
|
|
this.totalCount++;
|
|
|
|
|
|
|
|
|
|
// Filter packet yang terlalu kecil
|
|
|
|
|
if (chunk.length >= this.minPacketSize) {
|
|
|
|
|
this.push(chunk);
|
|
|
|
|
} else {
|
|
|
|
|
this.filteredCount++;
|
|
|
|
|
if (this.filteredCount % 10 === 0) {
|
|
|
|
|
// console.log(`[packet-filter] Filtered ${this.filteredCount} small packets (size < ${this.minPacketSize} bytes)`);
|
|
|
|
|
}
|
2026-05-12 19:38:23 +07:00
|
|
|
}
|
|
|
|
|
|
2026-05-13 15:54:54 +07:00
|
|
|
callback();
|
|
|
|
|
}
|
2026-05-12 19:38:23 +07:00
|
|
|
|
2026-05-13 15:54:54 +07:00
|
|
|
_flush(callback: TransformCallback): void {
|
|
|
|
|
// console.log(`[packet-filter] Total packets: ${this.totalCount}, filtered: ${this.filteredCount}, passed: ${this.totalCount - this.filteredCount}`);
|
|
|
|
|
callback();
|
|
|
|
|
}
|
2026-05-12 19:38:23 +07:00
|
|
|
}
|