Created
November 22, 2012 17:19
-
-
Save ralphholzmann/4132223 to your computer and use it in GitHub Desktop.
Base64Stream
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 Base64Stream extends Stream | |
_queue: "" | |
_emitChunk: ( chunk ) -> | |
@emit "data", new Buffer chunk, "base64" | |
write: ( chunk ) -> | |
if chunk.length % 4 != 0 | |
@_queue += chunk | |
if @_queue.length % 4 == 0 | |
@_emitChunk @_queue | |
@_queue = "" | |
else | |
@_emitChunk chunk | |
end: ( chunk ) -> | |
if arguments.length | |
@write.apply @, arguments | |
if @_queue.length | |
@_emitChunk @_queue | |
@emit "end" | |
module.exports = Base64Stream |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment