| 491 | // PATCH - THIS IS WHERE THE PATCH IS - FIXES BOUNDARY CHUNKING ISSUE |
| 492 | // See https://github.com/node-fetch/node-fetch/issues/1576 |
| 493 | const onData = (buf) => { |
| 494 | properLastChunkReceived = Buffer.compare(buf.slice(-5), LAST_CHUNK) === 0; |
| 495 | |
| 496 | // Sometimes final 0-length chunk and end of message code are in separate packets |
| 497 | if (!properLastChunkReceived && previousChunk) { |
| 498 | if (buf.length < 5) { |
| 499 | properLastChunkReceived = |
| 500 | Buffer.compare( |
| 501 | Buffer.from([...previousChunk.slice(-5), ...buf]).slice(-5), |
| 502 | LAST_CHUNK, |
| 503 | ) === 0; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | previousChunk = buf; |
| 508 | }; |
| 509 | |
| 510 | socket.prependListener("close", onSocketClose); |
| 511 | socket.on("data", onData); |