Duplex stream back-pressure: MaxListenersExceededWarning: Possible EventEmitter memory leak detected

I’ve got the following duplex stream. Input is a CSV string. When the limit of CSV lines is reached, a new CSV is created and added to a ZIP archive. So the pipeline flow is this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>CSV STRING ---> MY DUPLEX ---> ZIP ARCHIVE STREAM CONTAINING N CSV FFILES ---> DO SOMETHING (e. g. upload to S3)
</code>
<code>CSV STRING ---> MY DUPLEX ---> ZIP ARCHIVE STREAM CONTAINING N CSV FFILES ---> DO SOMETHING (e. g. upload to S3) </code>
CSV STRING ---> MY DUPLEX ---> ZIP ARCHIVE STREAM CONTAINING N CSV FFILES ---> DO SOMETHING (e. g. upload to S3)

When running this with large files where I pipe the result (the zip archive stream) into an S3 upload, I get the following warning MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 drain listeners added to [PassThrough].

Here is my Duplex stream. I’m pretty sure I got something confused about the backpressure handling. So my question is: How can I solve backpressure issue while getting rid of the warning / potential memory leak? And do I actually need to handle backpressure myself or not?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { Duplex, PassThrough } from "stream";
import archiver from "archiver";
export class ArchiveSplitter extends Duplex {
private _currentFile: PassThrough | undefined;
private _readable: archiver.Archiver;
private _fileCounter: number = 1;
private _lineCounter: number = 1;
constructor(
private readonly basename: string,
private readonly limit = 90000,
) {
super();
const _readable = archiver("zip");
this._readable = _readable;
_readable.pause();
this.once("finish", async () => {
this._currentFile && this._currentFile.end();
await this._readable.finalize();
this._readable.end();
});
_readable.on("end", async () => {
void this.push(null);
});
// forward errors
_readable.on("error", (err) => void this.emit("error", err));
}
_construct(callback: (error?: Error | null) => void): void {
callback(null);
}
_destroy(err: Error) {
this._readable.destroy(err);
}
_write(chunk: any, _: BufferEncoding, callback: (error?: Error | null) => void) {
if (!this._currentFile) {
this.startFile();
}
const ok = this._currentFile!.write(chunk);
if (!ok) {
this._currentFile?.once("drain", callback);
}
if (this._lineCounter++ === this.limit && !!this._currentFile) {
this._currentFile.end();
this._currentFile = undefined;
}
callback(null);
}
_read(size?: number) {
const chunk = this._readable.read(size);
if (chunk !== null) this.push(chunk);
else this._readable.once("readable", () => void this._read());
}
private startFile() {
this._currentFile = new PassThrough();
const name = `${this.basename}-${this._fileCounter++}.csv`;
this._readable = this._readable.append(this._currentFile, { name });
this._lineCounter = 1;
}
}
</code>
<code>import { Duplex, PassThrough } from "stream"; import archiver from "archiver"; export class ArchiveSplitter extends Duplex { private _currentFile: PassThrough | undefined; private _readable: archiver.Archiver; private _fileCounter: number = 1; private _lineCounter: number = 1; constructor( private readonly basename: string, private readonly limit = 90000, ) { super(); const _readable = archiver("zip"); this._readable = _readable; _readable.pause(); this.once("finish", async () => { this._currentFile && this._currentFile.end(); await this._readable.finalize(); this._readable.end(); }); _readable.on("end", async () => { void this.push(null); }); // forward errors _readable.on("error", (err) => void this.emit("error", err)); } _construct(callback: (error?: Error | null) => void): void { callback(null); } _destroy(err: Error) { this._readable.destroy(err); } _write(chunk: any, _: BufferEncoding, callback: (error?: Error | null) => void) { if (!this._currentFile) { this.startFile(); } const ok = this._currentFile!.write(chunk); if (!ok) { this._currentFile?.once("drain", callback); } if (this._lineCounter++ === this.limit && !!this._currentFile) { this._currentFile.end(); this._currentFile = undefined; } callback(null); } _read(size?: number) { const chunk = this._readable.read(size); if (chunk !== null) this.push(chunk); else this._readable.once("readable", () => void this._read()); } private startFile() { this._currentFile = new PassThrough(); const name = `${this.basename}-${this._fileCounter++}.csv`; this._readable = this._readable.append(this._currentFile, { name }); this._lineCounter = 1; } } </code>
import { Duplex, PassThrough } from "stream";
import archiver from "archiver";

export class ArchiveSplitter extends Duplex {
  private _currentFile: PassThrough | undefined;
  private _readable: archiver.Archiver;
  private _fileCounter: number = 1;
  private _lineCounter: number = 1;

  constructor(
    private readonly basename: string,
    private readonly limit = 90000,
  ) {
    super();

    const _readable = archiver("zip");
    this._readable = _readable;

    _readable.pause();

    this.once("finish", async () => {
      this._currentFile && this._currentFile.end();
      await this._readable.finalize();
      this._readable.end();
    });

    _readable.on("end", async () => {
      void this.push(null);
    });

    // forward errors
    _readable.on("error", (err) => void this.emit("error", err));
  }

  _construct(callback: (error?: Error | null) => void): void {
    callback(null);
  }

  _destroy(err: Error) {
    this._readable.destroy(err);
  }

  _write(chunk: any, _: BufferEncoding, callback: (error?: Error | null) => void) {
    if (!this._currentFile) {
      this.startFile();
    }

    const ok = this._currentFile!.write(chunk);

    if (!ok) {
      this._currentFile?.once("drain", callback);
    }

    if (this._lineCounter++ === this.limit && !!this._currentFile) {
      this._currentFile.end();
      this._currentFile = undefined;
    }

    callback(null);
  }

  _read(size?: number) {
    const chunk = this._readable.read(size);
    if (chunk !== null) this.push(chunk);
    else this._readable.once("readable", () => void this._read());
  }

  private startFile() {
    this._currentFile = new PassThrough();
    const name = `${this.basename}-${this._fileCounter++}.csv`;
    this._readable = this._readable.append(this._currentFile, { name });
    this._lineCounter = 1;
  }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật