Closed LeBenLeBen closed 9 months ago
I left a comment in #1658 but as this is the same problem with mine, I'll copy it here.
I have an issue with
2.2.0+
version of MSW, when I put CJK(Chinese, Japanese, Korean character) in MSW response. Untilmsw@2.1.0
, it was fine. I checked out what was changed in 2.2.0, and I found this commit could be the problem.For example, the length of string '조현영' (which is Korean) is 9 bytes in network, whereas 'abc' is 3 bytes. But both are 3 lengths in JavaScript. I think current implementation which calculate length by just
string
.length can be problem. I think usingnew TextEncoder().encode(str).length
would be better to calculateContent-Length
. Thank you
Right now, we are relying on the string length to determine the value of the automatic Content-Length
response header.
Instead, we should convert the string to buffer and measure its byte length. There are a couple of ways to do that:
new TextEncoder().encode(text).byteLength
new Blob([text]).size
Pull requests are welcome! I'd start with a failing test for special characters right here:
This has been released in v2.2.2!
Make sure to always update to the latest version (npm i msw@latest
) to get the newest features and bug fixes.
Predictable release automation by @ossjs/release.
Prerequisites
Environment check
msw
versionNode.js version
20.9.0
Reproduction repository
https://github.com/LeBenLeBen/msw-truncated
Reproduction steps
Clone the reproduction repo, then:
npm install
npm start
Current behavior
The JSON response is truncated a few characters before the end and cannot be parsed by the browser. It’s caused by the special char
é
in the response. The problem does not occur before MSW 2.2.0, so I’m guessing it’s a regression caused by #1658Expected behavior
The JSON response should be complete.