(stream: GridFSBucketWriteStream, callback: Callback)
| 489 | } |
| 490 | |
| 491 | function writeRemnant(stream: GridFSBucketWriteStream, callback: Callback): void { |
| 492 | // Buffer is empty, so don't bother to insert |
| 493 | if (stream.pos === 0) { |
| 494 | return checkDone(stream, callback); |
| 495 | } |
| 496 | |
| 497 | // Create a new buffer to make sure the buffer isn't bigger than it needs |
| 498 | // to be. |
| 499 | const remnant = ByteUtils.allocate(stream.pos); |
| 500 | ByteUtils.copy(stream.bufToStore, remnant, 0, 0, stream.pos); |
| 501 | const doc = createChunkDoc(stream.id, stream.n, remnant); |
| 502 | |
| 503 | // If the stream was aborted, do not write remnant |
| 504 | if (isAborted(stream, callback)) { |
| 505 | return; |
| 506 | } |
| 507 | |
| 508 | const remainingTimeMS = stream.timeoutContext?.remainingTimeMS; |
| 509 | if (remainingTimeMS != null && remainingTimeMS <= 0) { |
| 510 | return handleError( |
| 511 | stream, |
| 512 | new MongoOperationTimeoutError( |
| 513 | `Upload timed out after ${stream.timeoutContext?.timeoutMS}ms` |
| 514 | ), |
| 515 | callback |
| 516 | ); |
| 517 | } |
| 518 | ++stream.state.outstandingRequests; |
| 519 | stream.chunks |
| 520 | .insertOne(doc, { writeConcern: stream.writeConcern, timeoutMS: remainingTimeMS }) |
| 521 | .then( |
| 522 | () => { |
| 523 | --stream.state.outstandingRequests; |
| 524 | checkDone(stream, callback); |
| 525 | }, |
| 526 | error => { |
| 527 | return handleError(stream, error, callback); |
| 528 | } |
| 529 | ); |
| 530 | } |
| 531 | |
| 532 | function isAborted(stream: GridFSBucketWriteStream, callback: Callback<void>): boolean { |
| 533 | if (stream.state.aborted) { |
no test coverage detected