{"library":"string_decoder","title":"StringDecoder for Userland","type":"library","description":"The `string_decoder` package provides a userland implementation of the Node.js core `string_decoder` module. It is designed to correctly decode `Buffer` objects into strings, specifically handling multi-byte UTF-8 and UTF-16 characters that may span across multiple buffer chunks. This prevents issues like malformed characters when processing streamed or chunked data. Maintained by the Node.js Streams Working Group, it offers a stable and reliable solution for character decoding outside of the core Node.js environment. The current stable version is 1.3.0, released 7 years ago, indicating its maturity. Prior to version 1.0.0, its versions mirrored those of Node.js core; since 1.0.0, it adheres to Semantic Versioning. Its key differentiator is being a direct, semantically versioned mirror of the high-performance Node.js core implementation, making it suitable for environments where Node's built-in module is not directly available or a specific version parity is required, such as in browserify bundles.","language":"javascript","status":"maintenance","last_verified":"Sun Apr 19","install":{"commands":["npm install string_decoder"],"cli":null},"imports":["import { StringDecoder } from 'string_decoder';","const { StringDecoder } = require('string_decoder');"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":null,"github":"https://github.com/nodejs/string_decoder","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/string_decoder","openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"import { StringDecoder } from 'string_decoder';\nimport { Buffer } from 'buffer';\n\nconst decoder = new StringDecoder('utf8');\n\n// Imagine receiving a multi-byte character (like '€') split across network packets.\n// The Euro symbol (€) is U+20AC, which is E2 82 AC in UTF-8.\n\nconst chunk1 = Buffer.from([0xE2]); // First byte of '€'\nconst chunk2 = Buffer.from([0x82]); // Second byte of '€'\nconst chunk3 = Buffer.from([0xAC, 0x61, 0x62]); // Third byte of '€' plus 'ab'\n\nlet decodedString = '';\ndecodedString += decoder.write(chunk1); // Should output '' (incomplete char buffered)\ndecodedString += decoder.write(chunk2); // Should output '' (still incomplete)\ndecodedString += decoder.write(chunk3); // Should output '€ab' (now complete and subsequent chars)\ndecodedString += decoder.end(); // Any remaining buffered characters are flushed\n\nconsole.log(decodedString);\n// Expected output: '€ab'\n\n// Without StringDecoder, a simple buffer.toString() on chunks could lead to replacement characters.\nconst simpleConcat = Buffer.concat([chunk1, chunk2, chunk3]).toString('utf8');\nconsole.log(simpleConcat); \n// Expected output: '€ab' (for this specific example, but not reliable with *any* partial data)\n","lang":"javascript","description":"Demonstrates how to use `StringDecoder` to correctly handle multi-byte UTF-8 characters split across multiple `Buffer` chunks, preventing data corruption.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}