MCPcopy Create free account
hub / github.com/nodejs/node / readAllBytes

Function readAllBytes

deps/undici/src/lib/web/fetch/util.js:988–1019  ·  view source on GitHub ↗

* @see https://streams.spec.whatwg.org/#readablestreamdefaultreader-read-all-bytes * @see https://streams.spec.whatwg.org/#read-loop * @param {ReadableStream >} reader * @param {(bytes: Uint8Array) => void} successSteps * @param {(error: Error) => void} failureSteps * @re

(reader, successSteps, failureSteps)

Source from the content-addressed store, hash-verified

986 * @returns {Promise<void>}
987 */
988async function readAllBytes (reader, successSteps, failureSteps) {
989 try {
990 const bytes = []
991 let byteLength = 0
992
993 do {
994 const { done, value: chunk } = await reader.read()
995
996 if (done) {
997 // 1. Call successSteps with bytes.
998 successSteps(Buffer.concat(bytes, byteLength))
999 return
1000 }
1001
1002 // 1. If chunk is not a Uint8Array object, call failureSteps
1003 // with a TypeError and abort these steps.
1004 if (!isUint8Array(chunk)) {
1005 failureSteps(new TypeError('Received non-Uint8Array chunk'))
1006 return
1007 }
1008
1009 // 2. Append the bytes represented by chunk to bytes.
1010 bytes.push(chunk)
1011 byteLength += chunk.length
1012
1013 // 3. Read-loop given reader, bytes, successSteps, and failureSteps.
1014 } while (true)
1015 } catch (e) {
1016 // 1. Call failureSteps with e.
1017 failureSteps(e)
1018 }
1019}
1020
1021/**
1022 * @see https://fetch.spec.whatwg.org/#is-local

Callers 2

fullyReadBodyFunction · 0.70
putMethod · 0.50

Calls 5

successStepsFunction · 0.85
isUint8ArrayFunction · 0.85
concatMethod · 0.80
readMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected