@@ -68,21 +68,50 @@ function wrap(str, lineLength) {
68
68
class Encoder extends Transform {
69
69
constructor ( options ) {
70
70
super ( ) ;
71
-
72
71
// init Transform
73
72
this . options = options || { } ;
74
73
75
74
if ( this . options . lineLength !== false ) {
76
75
this . options . lineLength = this . options . lineLength || 76 ;
77
76
}
78
77
78
+ this . skipStartBytes = this . options . skipStartBytes || 0 ;
79
+ this . limitOutbutBytes = this . options . limitOutbutBytes || 0 ;
80
+
79
81
this . _curLine = '' ;
80
82
this . _remainingBytes = false ;
81
83
82
84
this . inputBytes = 0 ;
83
85
this . outputBytes = 0 ;
84
86
}
85
87
88
+ _writeChunk ( chunk /*, isFinal */ ) {
89
+ if ( this . skipStartBytes ) {
90
+ if ( chunk . length <= this . skipStartBytes ) {
91
+ this . skipStartBytes -= chunk . length ;
92
+ return ;
93
+ }
94
+
95
+ chunk = chunk . slice ( this . skipStartBytes ) ;
96
+ this . skipStartBytes = 0 ;
97
+ }
98
+
99
+ if ( this . limitOutbutBytes ) {
100
+ if ( this . outputBytes + chunk . length <= this . limitOutbutBytes ) {
101
+ // ignore, can use entire chunk
102
+ } else if ( this . outputBytes >= this . limitOutbutBytes ) {
103
+ // chunks already processed
104
+ return ;
105
+ } else {
106
+ // use partial chunk
107
+ chunk = chunk . slice ( 0 , this . limitOutbutBytes - this . outputBytes ) ;
108
+ }
109
+ }
110
+
111
+ this . outputBytes += chunk . length ;
112
+ this . push ( chunk ) ;
113
+ }
114
+
86
115
_transform ( chunk , encoding , done ) {
87
116
if ( encoding !== 'buffer' ) {
88
117
chunk = Buffer . from ( chunk , encoding ) ;
@@ -100,8 +129,8 @@ class Encoder extends Transform {
100
129
}
101
130
102
131
if ( chunk . length % 3 ) {
103
- this . _remainingBytes = chunk . slice ( chunk . length - chunk . length % 3 ) ;
104
- chunk = chunk . slice ( 0 , chunk . length - chunk . length % 3 ) ;
132
+ this . _remainingBytes = chunk . slice ( chunk . length - ( chunk . length % 3 ) ) ;
133
+ chunk = chunk . slice ( 0 , chunk . length - ( chunk . length % 3 ) ) ;
105
134
} else {
106
135
this . _remainingBytes = false ;
107
136
}
@@ -125,8 +154,7 @@ class Encoder extends Transform {
125
154
}
126
155
127
156
if ( b64 ) {
128
- this . outputBytes += b64 . length ;
129
- this . push ( Buffer . from ( b64 , 'ascii' ) ) ;
157
+ this . _writeChunk ( Buffer . from ( b64 , 'ascii' ) , false ) ;
130
158
}
131
159
132
160
setImmediate ( done ) ;
@@ -136,13 +164,13 @@ class Encoder extends Transform {
136
164
if ( this . _remainingBytes && this . _remainingBytes . length ) {
137
165
this . _curLine += encode ( this . _remainingBytes ) ;
138
166
}
167
+
139
168
if ( this . _curLine ) {
140
169
this . _curLine = wrap ( this . _curLine , this . options . lineLength ) ;
141
- this . outputBytes += this . _curLine . length ;
142
- this . push ( Buffer . from ( this . _curLine , 'ascii' ) ) ;
170
+ this . _writeChunk ( Buffer . from ( this . _curLine , 'ascii' ) , true ) ;
143
171
this . _curLine = '' ;
144
172
}
145
- setImmediate ( done ) ;
173
+ done ( ) ;
146
174
}
147
175
}
148
176
0 commit comments