* @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)
| 986 | * @returns {Promise<void>} |
| 987 | */ |
| 988 | async 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 |
no test coverage detected