( stream: ReadableStream<Uint8Array>, maxBytes: number, )
| 853 | } |
| 854 | |
| 855 | async function readStreamWithLimit( |
| 856 | stream: ReadableStream<Uint8Array>, |
| 857 | maxBytes: number, |
| 858 | ): Promise<string> { |
| 859 | const reader = stream.getReader(); |
| 860 | const chunks: Uint8Array[] = []; |
| 861 | let totalBytes = 0; |
| 862 | |
| 863 | try { |
| 864 | while (true) { |
| 865 | const { done, value } = await reader.read(); |
| 866 | if (done) break; |
| 867 | if (totalBytes < maxBytes) { |
| 868 | const remainingBytes = maxBytes - totalBytes; |
| 869 | const chunk = |
| 870 | value.length > remainingBytes |
| 871 | ? value.slice(0, remainingBytes) |
| 872 | : value; |
| 873 | chunks.push(chunk); |
| 874 | totalBytes += chunk.length; |
| 875 | } |
| 876 | } |
| 877 | } finally { |
| 878 | reader.releaseLock(); |
| 879 | } |
| 880 | |
| 881 | const decoder = new TextDecoder(); |
| 882 | return chunks |
| 883 | .map((chunk) => decoder.decode(chunk, { stream: true })) |
| 884 | .join(""); |
| 885 | } |
| 886 | |
| 887 | async function storageResponseError( |
| 888 | prefix: string, |
no test coverage detected