| 43 | } |
| 44 | |
| 45 | function writeHeaders(socket, headers) { |
| 46 | const array = []; |
| 47 | const chunkSize = 100; |
| 48 | let last = 0; |
| 49 | |
| 50 | for (let i = 0; i < headers.length / chunkSize; i++) { |
| 51 | const current = (i + 1) * chunkSize; |
| 52 | array.push(headers.slice(last, current)); |
| 53 | last = current; |
| 54 | } |
| 55 | |
| 56 | // Safety check we are chunking correctly |
| 57 | assert.strictEqual(array.join(''), headers); |
| 58 | |
| 59 | next(); |
| 60 | |
| 61 | function next() { |
| 62 | if (socket.destroyed) { |
| 63 | console.log('socket was destroyed early, data left to write:', |
| 64 | array.join('').length); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | const chunk = array.shift(); |
| 69 | |
| 70 | if (chunk) { |
| 71 | console.log('writing chunk of size', chunk.length); |
| 72 | socket.write(chunk, next); |
| 73 | } else { |
| 74 | socket.end(); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | function test1() { |
| 80 | console.log('test1'); |