Skip to content
Advertisement

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.

Advertisement

Answer

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.

Advertisement