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

Method read

src/utils.ts:840–873  ·  view source on GitHub ↗

Reads the requested number of bytes, optionally consuming them

(size: number)

Source from the content-addressed store, hash-verified

838
839 /** Reads the requested number of bytes, optionally consuming them */
840 read(size: number): Uint8Array {
841 if (typeof size !== 'number' || size < 0) {
842 throw new MongoInvalidArgumentError('Argument "size" must be a non-negative number');
843 }
844
845 // oversized request returns empty buffer
846 if (size > this.totalByteLength) {
847 return ByteUtils.allocate(0);
848 }
849
850 // We know we have enough, we just don't know how it is spread across chunks
851 // TODO(NODE-4732): alloc API should change based on raw option
852 const result = ByteUtils.allocateUnsafe(size);
853
854 for (let bytesRead = 0; bytesRead < size; ) {
855 const buffer = this.buffers.shift();
856 if (buffer == null) {
857 break;
858 }
859 const bytesRemaining = size - bytesRead;
860 const bytesReadable = Math.min(bytesRemaining, buffer.byteLength);
861 const bytes = buffer.subarray(0, bytesReadable);
862
863 result.set(bytes, bytesRead);
864
865 bytesRead += bytesReadable;
866 this.totalByteLength -= bytesReadable;
867 if (bytesReadable < buffer.byteLength) {
868 this.buffers.unshift(buffer.subarray(bytesReadable));
869 }
870 }
871
872 return result;
873 }
874}
875
876/** @public */

Callers 7

getInt32Method · 0.95
kmsRequestMethod · 0.95
utils.test.tsFile · 0.80
connection.test.tsFile · 0.80
templateFunction · 0.80
_transformMethod · 0.80

Calls 4

setMethod · 0.80
unshiftMethod · 0.80
shiftMethod · 0.45
minMethod · 0.45

Tested by 1

templateFunction · 0.64