Skip to content

Commit

Permalink
[Reporting] Improve _read code in ContentStream (elastic#113237)
Browse files Browse the repository at this point in the history
  • Loading branch information
watson authored Oct 4, 2021
1 parent 006e371 commit d8b4f4b
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions x-pack/plugins/reporting/server/lib/content_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,28 +179,27 @@ export class ContentStream extends Duplex {
return this.jobSize != null && this.bytesRead >= this.jobSize;
}

async _read() {
try {
const content = this.chunksRead ? await this.readChunk() : await this.readHead();
if (!content) {
this.logger.debug(`Chunk is empty.`);
this.push(null);
return;
}

const buffer = this.decode(content);

this.push(buffer);
this.chunksRead++;
this.bytesRead += buffer.byteLength;

if (this.isRead()) {
this.logger.debug(`Read ${this.bytesRead} of ${this.jobSize} bytes.`);
this.push(null);
}
} catch (error) {
this.destroy(error);
}
_read() {
(this.chunksRead ? this.readChunk() : this.readHead())
.then((content) => {
if (!content) {
this.logger.debug(`Chunk is empty.`);
this.push(null);
return;
}

const buffer = this.decode(content);

this.push(buffer);
this.chunksRead++;
this.bytesRead += buffer.byteLength;

if (this.isRead()) {
this.logger.debug(`Read ${this.bytesRead} of ${this.jobSize} bytes.`);
this.push(null);
}
})
.catch((err) => this.destroy(err));
}

private async removeChunks() {
Expand Down

0 comments on commit d8b4f4b

Please sign in to comment.