Skip to content

Instantly share code, notes, and snippets.

@aliaksandr-kazarez
Created December 28, 2018 09:24
Show Gist options
  • Save aliaksandr-kazarez/41840ee38d07a4bdb21c864d73c296e7 to your computer and use it in GitHub Desktop.
Save aliaksandr-kazarez/41840ee38d07a4bdb21c864d73c296e7 to your computer and use it in GitHub Desktop.
Mock Duplex implementation
class MockResponse extends Duplex {
private chunks = Buffer.alloc(0);
_write(chunk: Buffer, encoding: string, cb: Function) {
this.chunks = Buffer.concat([this.chunks, chunk]);
cb();
}
_read(size: number) {
let result = true;
while (result && this.chunks.length > 0) {
const end = size > this.chunks.length ? this.chunks.length : size;
const chunk = this.chunks.slice(0, end);
this.chunks = this.chunks.slice(end, this.chunks.length);
result = this.push(chunk);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment