Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What can cause string length to be incorrectly reported in Node JavaScript?

On inspection in the debugger, the value of html in the code below ends with </html>\n as expected, but as received in browser the last six characters (/html>) are missing.

let html = this.code!.asHtml();
response.writeHead(200, {
    "Content-Type": "text/html; charset=utf-8",
    "Content-Length": html.length
});
response.end(html);

When I comment out the content length header like so

let html = this.code!.asHtml();
response.writeHead(200, {
    "Content-Type": "text/html; charset=utf-8",
    //"Content-Length": html.length
});
response.end(html);

The delivered page is no longer truncated. WTF?! This implies that html.length is not reporting the correct number of characters. Probably this has something to do with character sets, but I’m not sure how to proceed and would appreciate advice.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Content-Length is the number of bytes of the body not the number of chars.

html.length is not reporting the correct number of characters.

.length is reporting the number of characters (if it is UTF-8) or more precisely UTF-16 code units.

The length property of a String object contains the length of the string, in UTF-16 code units. length is a read-only data property of string instances. …

Not relevant for you case but also important to know:

… it’s possible for the value returned by length to not match the actual number of characters in the string

Back to your problem: In UTF-8 or 16 one character may be encoded by more than one byte. So html.length can indeed be too small. Use Buffer.byteLength(html, 'utf8') instead.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading