| 54 | } |
| 55 | |
| 56 | async function readStreamWithLimit( |
| 57 | stream: ReadableStream<Uint8Array>, |
| 58 | maxBytes: number, |
| 59 | ): Promise<string> { |
| 60 | const reader = stream.getReader(); |
| 61 | const chunks: Uint8Array[] = []; |
| 62 | let totalBytes = 0; |
| 63 | |
| 64 | try { |
| 65 | while (true) { |
| 66 | const { done, value } = await reader.read(); |
| 67 | if (done) break; |
| 68 | if (totalBytes < maxBytes) { |
| 69 | const remainingBytes = maxBytes - totalBytes; |
| 70 | const chunk = |
| 71 | value.length > remainingBytes |
| 72 | ? value.slice(0, remainingBytes) |
| 73 | : value; |
| 74 | chunks.push(chunk); |
| 75 | totalBytes += chunk.length; |
| 76 | } |
| 77 | } |
| 78 | } finally { |
| 79 | reader.releaseLock(); |
| 80 | } |
| 81 | |
| 82 | const decoder = new TextDecoder(); |
| 83 | return chunks |
| 84 | .map((chunk) => decoder.decode(chunk, { stream: true })) |
| 85 | .join(""); |
| 86 | } |
| 87 | |
| 88 | function redactUrl(value: string): string { |
| 89 | try { |