Created
December 28, 2018 09:24
-
-
Save aliaksandr-kazarez/41840ee38d07a4bdb21c864d73c296e7 to your computer and use it in GitHub Desktop.
Mock Duplex implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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