Created
October 10, 2012 04:25
-
-
Save mnutt/3863153 to your computer and use it in GitHub Desktop.
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
Stream = require 'stream' | |
class CachedStream extends Stream | |
constructor: -> | |
@buffers = [] | |
readable: true | |
writable: true | |
write: (buffer) -> | |
@buffers.push buffer | |
@emit 'data', buffer | |
end: -> | |
@emit 'end' | |
pipeCached: (dest) -> | |
bufferIndex = 0 | |
sendData = => | |
if bufferIndex < @buffers.length | |
if dest.write @buffers[bufferIndex], 'binary' | |
bufferIndex += 1 | |
process.nextTick sendData | |
else | |
# write buffer is full, wait for drain to resume sending | |
dest.once 'drain', sendData | |
else | |
dest.end() | |
sendData() | |
module.exports = CachedStream |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment