@@ -54,7 +54,7 @@ enum DownloadHandlerState {
54
54
55
55
const MAX_CHUNK_BYTES = 3 * 1024 ;
56
56
const invalidBodyRegex = / [ ^ \n \r A - Z a - z 0 - 9 / + : = ] / ;
57
-
57
+ const ONE_KILOBYTE = 1024 ;
58
58
59
59
class DownloadSession {
60
60
@@ -69,6 +69,7 @@ class DownloadSession {
69
69
70
70
onCreatedBulkFile : Event < BulkFileHandle > ;
71
71
private _onCreatedBulkFileEventEmitter = new EventEmitter < BulkFileHandle > ( ) ;
72
+ private _createdEventFired = false ;
72
73
73
74
constructor ( private _emulator : TermApi . EmulatorApi , private _broker : BulkFileBroker ) {
74
75
this . _log = getLogger ( "DownloadApplicationModeHandler DownloadSession" , this ) ;
@@ -130,14 +131,24 @@ class DownloadSession {
130
131
this . _fileHandle = this . _broker . createWriteableBulkFileHandle ( metadata , filesize ) ;
131
132
this . _fileHandle . ref ( ) ;
132
133
this . _availableWriteBufferSizeChangedDisposable = this . _fileHandle . onAvailableWriteBufferSizeChange ( this . _handleAvailableWriteBufferSizeChange . bind ( this ) ) ;
133
- this . _onCreatedBulkFileEventEmitter . fire ( this . _fileHandle ) ;
134
134
135
135
this . _state = DownloadHandlerState . BODY ;
136
136
return this . _handleBody ( "" ) ;
137
137
}
138
138
return { action : TermApi . ApplicationModeResponseAction . CONTINUE } ;
139
139
}
140
140
141
+ private _fireOnCreatedEvent ( force = false ) : void {
142
+ if ( this . _fileHandle == null || this . _createdEventFired ) {
143
+ return ;
144
+ }
145
+
146
+ if ( force || this . _fileHandle . getAvailableSize ( ) >= ONE_KILOBYTE ) {
147
+ this . _onCreatedBulkFileEventEmitter . fire ( this . _fileHandle ) ;
148
+ this . _createdEventFired = true ;
149
+ }
150
+ }
151
+
141
152
private _closeFileHandle ( success : boolean ) : void {
142
153
if ( this . _fileHandle !== null ) {
143
154
this . _fileHandle . close ( success ) ;
@@ -160,6 +171,7 @@ class DownloadSession {
160
171
if ( invalidBodyRegex . test ( chunk ) ) {
161
172
this . _log . warn ( "Chunk contains illegal characters. Aborting." ) ;
162
173
this . _state = DownloadHandlerState . ERROR ;
174
+ this . _fireOnCreatedEvent ( true ) ;
163
175
this . _closeFileHandle ( false ) ;
164
176
return { action : TermApi . ApplicationModeResponseAction . ABORT , remainingData : this . _encodedDataBuffer } ;
165
177
}
@@ -177,6 +189,7 @@ class DownloadSession {
177
189
if ( chunk . charAt ( 1 ) !== ":" || chunk . charAt ( lastColonIndex ) !== ":" || ( commandChar !== "D" && commandChar !== "E" ) ) {
178
190
this . _log . warn ( "Data chunk is malformed. Aborting." ) ;
179
191
this . _state = DownloadHandlerState . ERROR ;
192
+ this . _fireOnCreatedEvent ( true ) ;
180
193
this . _closeFileHandle ( false ) ;
181
194
return { action : TermApi . ApplicationModeResponseAction . ABORT , remainingData : this . _encodedDataBuffer } ;
182
195
}
@@ -196,6 +209,7 @@ class DownloadSession {
196
209
if ( this . _previousHash . toString ( "hex" ) !== hashHex ) {
197
210
this . _log . warn ( "Data chunk hash is incorrect." ) ;
198
211
this . _state = DownloadHandlerState . ERROR ;
212
+ this . _fireOnCreatedEvent ( true ) ;
199
213
this . _closeFileHandle ( false ) ;
200
214
return { action : TermApi . ApplicationModeResponseAction . ABORT , remainingData : this . _encodedDataBuffer } ;
201
215
}
@@ -211,6 +225,8 @@ class DownloadSession {
211
225
212
226
this . _flushBuffer ( ) ;
213
227
228
+ this . _fireOnCreatedEvent ( ) ;
229
+
214
230
if ( this . _state === DownloadHandlerState . BODY && this . _fileHandle . getAvailableWriteBufferSize ( ) <= 0 ) {
215
231
return { action : TermApi . ApplicationModeResponseAction . PAUSE } ;
216
232
}
@@ -272,6 +288,7 @@ class DownloadSession {
272
288
}
273
289
274
290
if ( this . _decodedDataBuffers . length === 0 && this . _state === DownloadHandlerState . COMPLETE ) {
291
+ this . _fireOnCreatedEvent ( true ) ;
275
292
this . _closeFileHandle ( true ) ;
276
293
}
277
294
}
@@ -280,6 +297,7 @@ class DownloadSession {
280
297
let response : TermApi . ApplicationModeResponse = null ;
281
298
282
299
this . _flushBuffer ( ) ;
300
+ this . _fireOnCreatedEvent ( true ) ;
283
301
284
302
if ( this . _state !== DownloadHandlerState . COMPLETE ) {
285
303
this . _state = DownloadHandlerState . ERROR ;
0 commit comments