MCPcopy
hub / github.com/mongodb/node-mongodb-native / writeRemnant

Function writeRemnant

src/gridfs/upload.ts:491–530  ·  view source on GitHub ↗
(stream: GridFSBucketWriteStream, callback: Callback)

Source from the content-addressed store, hash-verified

489}
490
491function 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
532function isAborted(stream: GridFSBucketWriteStream, callback: Callback<void>): boolean {
533 if (stream.state.aborted) {

Callers 1

_finalMethod · 0.85

Calls 5

checkDoneFunction · 0.85
createChunkDocFunction · 0.85
isAbortedFunction · 0.85
insertOneMethod · 0.80
handleErrorFunction · 0.70

Tested by

no test coverage detected