(chunk: any, encoding: BufferEncoding | undefined, callback: (error?: Error | null) => void, request: ClientRequest | undefined = this._request)
| 2439 | } |
| 2440 | |
| 2441 | private _writeRequest(chunk: any, encoding: BufferEncoding | undefined, callback: (error?: Error | null) => void, request: ClientRequest | undefined = this._request): void { // eslint-disable-line @typescript-eslint/no-restricted-types |
| 2442 | if (!request || request.destroyed) { |
| 2443 | // When there's no request (e.g., using cached response from beforeRequest hook), |
| 2444 | // we still need to call the callback to allow the stream to finish properly. |
| 2445 | callback(); |
| 2446 | return; |
| 2447 | } |
| 2448 | |
| 2449 | request.write(chunk, encoding as any, (error?: Error | null) => { // eslint-disable-line @typescript-eslint/no-restricted-types |
| 2450 | // The `!destroyed` check is required to prevent `uploadProgress` being emitted after the stream was destroyed. |
| 2451 | // The `this._request === request` check prevents stale write callbacks from a pre-redirect request from incrementing `_uploadedSize` after it's been reset. |
| 2452 | if (!error && !request.destroyed && this._request === request) { |
| 2453 | // For strings, encode them first to measure the actual bytes that will be sent |
| 2454 | const bytes = typeof chunk === 'string' ? Buffer.from(chunk, encoding) : chunk; |
| 2455 | this._uploadedSize += byteLength(bytes); |
| 2456 | |
| 2457 | const progress = this.uploadProgress; |
| 2458 | |
| 2459 | if (progress.percent < 1) { |
| 2460 | this.emit('uploadProgress', progress); |
| 2461 | } |
| 2462 | } |
| 2463 | |
| 2464 | callback(error); |
| 2465 | }); |
| 2466 | } |
| 2467 | |
| 2468 | /** |
| 2469 | The remote IP address. |
no test coverage detected