@@ -21,7 +21,7 @@ module.exports = {
21
21
*/
22
22
function encode ( buffer ) {
23
23
if ( typeof buffer === 'string' ) {
24
- buffer = new Buffer ( buffer , 'utf-8' ) ;
24
+ buffer = Buffer . from ( buffer , 'utf-8' ) ;
25
25
}
26
26
27
27
return buffer . toString ( 'base64' ) ;
@@ -34,8 +34,8 @@ function encode(buffer) {
34
34
* @returns {Buffer } Decoded value
35
35
*/
36
36
function decode ( str ) {
37
- str = ( str || '' ) ;
38
- return new Buffer ( str , 'base64' ) ;
37
+ str = str || '' ;
38
+ return Buffer . from ( str , 'base64' ) ;
39
39
}
40
40
41
41
/**
@@ -82,10 +82,11 @@ function Encoder(options) {
82
82
util . inherits ( Encoder , Transform ) ;
83
83
84
84
Encoder . prototype . _transform = function ( chunk , encoding , done ) {
85
- var b64 , _self = this ;
85
+ var b64 ,
86
+ _self = this ;
86
87
87
88
if ( encoding !== 'buffer' ) {
88
- chunk = new Buffer ( chunk , encoding ) ;
89
+ chunk = Buffer . from ( chunk , encoding ) ;
89
90
}
90
91
91
92
if ( ! chunk || ! chunk . length ) {
@@ -166,14 +167,15 @@ Decoder.prototype._transform = function(chunk, encoding, done) {
166
167
167
168
this . inputBytes += chunk . length ;
168
169
169
- b64 = ( this . _curLine + chunk ) ;
170
+ b64 = this . _curLine + chunk ;
170
171
this . _curLine = '' ;
171
172
172
173
b64 = b64 . replace ( / [ ^ a - z A - Z 0 - 9 + \/ = ] / g, '' ) ;
173
174
174
175
if ( b64 . length % 4 ) {
175
176
this . _curLine = b64 . substr ( - b64 . length % 4 ) ;
176
- if ( this . _curLine . length == b64 . length ) {
177
+ if ( this . _curLine . length == b64 . length || this . _curLine . length < 4 ) {
178
+ this . _curLine = b64 ;
177
179
b64 = '' ;
178
180
} else {
179
181
b64 = b64 . substr ( 0 , this . _curLine . length ) ;
@@ -198,4 +200,4 @@ Decoder.prototype._flush = function(done) {
198
200
this . _curLine = '' ;
199
201
}
200
202
done ( ) ;
201
- } ;
203
+ } ;
0 commit comments