()
| 47 | let remaining = size; |
| 48 | |
| 49 | function sendChunk() { |
| 50 | while (remaining > 0) { |
| 51 | const toWrite = Math.min(CHUNK_SIZE, remaining); |
| 52 | const chunk = crypto.randomBytes(toWrite); |
| 53 | remaining -= toWrite; |
| 54 | |
| 55 | if (!res.write(chunk)) { |
| 56 | // TCP backpressure - wait for drain |
| 57 | res.once('drain', sendChunk); |
| 58 | return; |
| 59 | } |
| 60 | } |
| 61 | res.end(); |
| 62 | console.log(` [Server] Finished sending ${size} bytes`); |
| 63 | } |
| 64 | |
| 65 | sendChunk(); |
| 66 | }); |