| 1 | module.exports = Base64Encode; |
| 2 | |
| 3 | var Transform = require('stream').Transform || require('readable-stream').Transform; |
| 4 | require('util').inherits(Base64Encode, Transform); |
| 5 | |
| 6 | /** |
| 7 | * Transforms a Buffer stream of binary data to a stream of Base64 text. Note that this will |
| 8 | * also work on a stream of pure strings, as the Writeable base class will automatically decode |
| 9 | * text string chunks into Buffers. |
| 10 | * @returns {Base64Encode} |
| 11 | * @constructor |
| 12 | */ |
| 13 | function Base64Encode(lineLength) { |
| 14 | if ( !(this instanceof Base64Encode) ) |
| 15 | return new Base64Encode(lineLength); |
| 16 | |
| 17 | Transform.call(this); |
| 18 | |
| 19 | // Any extra chars from the last chunk |
| 20 | this.extra = null; |
| 21 | |
| 22 | this.lineLength = lineLength; |
| 23 | |
| 24 | this.currLineLength = 0; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Adds \r\n as needed to the data chunk to ensure that the output Base64 string meets |
| 29 | * the maximum line length requirement. |
| 30 | * @param {string} chunk |
| 31 | * @returns {string} |
| 32 | * @private |
| 33 | */ |
| 34 | Base64Encode.prototype._fixLineLength = function (chunk) { |
| 35 | // If we care about line length, add line breaks |
| 36 | if ( !this.lineLength ) |
| 37 | return chunk; |
| 38 | |
| 39 | var size = chunk.length; |
| 40 | var needed = this.lineLength - this.currLineLength; |
| 41 | var start, end; |
| 42 | |
| 43 | var _chunk = ''; |
| 44 | for ( start = 0, end = needed; end < size; start = end, end += this.lineLength ) { |
| 45 | _chunk += chunk.slice(start, end); |
| 46 | _chunk += '\r\n'; |
| 47 | } |
| 48 | |
| 49 | var left = chunk.slice(start); |
| 50 | this.currLineLength = left.length; |
| 51 | |
| 52 | _chunk += left; |
| 53 | |
| 54 | return _chunk; |
| 55 | }; |
| 56 | |
| 57 | /** |
| 58 | * Transforms a Buffer chunk of data to a Base64 string chunk. |
| 59 | * @param {Buffer} chunk |
| 60 | * @param {string} encoding - unused since chunk is always a Buffer |
| 61 | * @param cb |
| 62 | * @private |
| 63 | */ |
| 64 | Base64Encode.prototype._transform = function (chunk, encoding, cb) { |
| 65 | // Add any previous extra bytes to the chunk |
| 66 | if ( this.extra ) { |
| 67 | chunk = Buffer.concat([this.extra, chunk]); |
| 68 | this.extra = null; |
| 69 | } |
| 70 | |
| 71 | // 3 bytes are represented by 4 characters, so we can only encode in groups of 3 bytes |
| 72 | var remaining = chunk.length % 3; |
| 73 | |
| 74 | if ( remaining !== 0 ) { |
| 75 | // Store the extra bytes for later |
| 76 | this.extra = chunk.slice(chunk.length - remaining); |
| 77 | chunk = chunk.slice(0, chunk.length - remaining); |
| 78 | } |
| 79 | |
| 80 | // Convert chunk to a base 64 string |
| 81 | chunk = chunk.toString('base64'); |
| 82 | |
| 83 | // Push the chunk |
| 84 | this.push(this._fixLineLength(chunk)); |
| 85 | cb(); |
| 86 | }; |
| 87 | |
| 88 | /** |
| 89 | * Emits 0 or 4 extra characters of Base64 data. |
| 90 | * @param cb |
| 91 | * @private |
| 92 | */ |
| 93 | Base64Encode.prototype._flush = function (cb) { |
| 94 | if ( this.extra ) |
| 95 | this.push(this._fixLineLength(this.extra.toString('base64'))); |
| 96 | |
| 97 | cb(); |
| 98 | }; |