koajs / koa

Expressive middleware for node.js using ES2017 async functions
https://koajs.com
MIT License
35.2k stars 3.23k forks source link

Content length response header incorrectly parsed #1275

Closed kerim1 closed 5 years ago

kerim1 commented 5 years ago

When requesting the length using ctx.length, Koa parses the content length response header using the double tilde operator: https://github.com/koajs/koa/blob/master/lib/response.js#L214 However, this operation also performs a 32-bit conversion, resulting in incorrect and weird results when serving large files. Isn't it therefore better to use the Math.trunc() function instead to parse the content length response header?

fl0w commented 5 years ago

Or BigInt for node >=10.4.

tpscrpt commented 5 years ago

using Math.trunc is 5% slower on 100 000 000 iterations of res.length when it's set to 164 chars.

fl0w commented 5 years ago

Fixed in 9be858312553002841725b617050aaff3c48951d