()
| 250 | } |
| 251 | |
| 252 | private mapStreamToEmitter () { |
| 253 | const stream = this.fileStream |
| 254 | |
| 255 | if ( !stream || !this.file || !this.format ) { |
| 256 | return |
| 257 | } |
| 258 | |
| 259 | const buffers: NodeBuffer[] = [] |
| 260 | let dataLength = 0 |
| 261 | |
| 262 | stream.on( 'error', ( error: Error ) => { |
| 263 | if ( this.readyState === this.DONE ) { |
| 264 | return |
| 265 | } |
| 266 | |
| 267 | this.readyState = this.DONE |
| 268 | this.error = error |
| 269 | this.dispatchEvent( 'error', { |
| 270 | target: { |
| 271 | error |
| 272 | } |
| 273 | } ) |
| 274 | } ) |
| 275 | |
| 276 | stream.on( 'data', ( data: NodeBuffer ) => { |
| 277 | if ( this.readyState === this.DONE ) { |
| 278 | return |
| 279 | } |
| 280 | |
| 281 | dataLength += data.length |
| 282 | buffers.push( data ) |
| 283 | |
| 284 | this.dispatchEvent( 'progress', { |
| 285 | lengthComputable: !Number.isNaN( this.file?.size ), |
| 286 | loaded: dataLength, |
| 287 | total: this.file?.size |
| 288 | } ) |
| 289 | } ) |
| 290 | |
| 291 | stream.on( 'end', () => { |
| 292 | if ( this.readyState === this.DONE ) { |
| 293 | return |
| 294 | } |
| 295 | |
| 296 | const data = buffers.length > 1 |
| 297 | ? Buffer.concat( buffers as unknown as readonly Uint8Array[] ) as NodeBuffer |
| 298 | : ( buffers[ 0 ] || Buffer.alloc( 0 ) ) |
| 299 | |
| 300 | this.readyState = this.DONE |
| 301 | this.result = mapDataToFormat( this.file!, data, this.format!, this.encoding ) |
| 302 | |
| 303 | const event = { |
| 304 | target: { |
| 305 | nodeBufferResult: data, |
| 306 | result: this.result |
| 307 | } |
| 308 | } |
| 309 |
no test coverage detected