Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
truncate-utf8-bytes
Advanced tools
The 'truncate-utf8-bytes' npm package is designed to truncate a string to a specified number of bytes, ensuring that the resulting string is valid UTF-8. This is particularly useful when dealing with systems that have byte-length constraints for strings, such as databases or network protocols.
Truncate a string to a specified number of bytes
This feature allows you to truncate a string to a specified number of bytes. In this example, the string 'Hello, world!' is truncated to 5 bytes, resulting in 'Hello'.
const truncate = require('truncate-utf8-bytes');
const str = 'Hello, world!';
const truncatedStr = truncate(str, 5);
console.log(truncatedStr); // Output: 'Hello'
Handle multi-byte characters correctly
This feature ensures that multi-byte characters are handled correctly. In this example, the Japanese string 'こんにちは' is truncated to 6 bytes, resulting in 'こん', which is a valid UTF-8 string.
const truncate = require('truncate-utf8-bytes');
const str = 'こんにちは'; // 'Hello' in Japanese
const truncatedStr = truncate(str, 6);
console.log(truncatedStr); // Output: 'こん'
The 'utf8-byte-length' package calculates the byte length of a UTF-8 string but does not provide truncation functionality. It is useful for determining the byte length of a string before performing operations that require byte-length constraints.
The 'utf8' package provides utilities for encoding and decoding UTF-8 strings but does not include truncation functionality. It is more focused on converting between UTF-8 and other encodings.
The 'string-byte-length' package calculates the byte length of a string and can also truncate it to a specified byte length. However, it may not handle multi-byte characters as gracefully as 'truncate-utf8-bytes'.
Truncate a string to the given length in bytes. Correctly handles multi-byte characters and surrogate pairs.
A browser implementation that doesn't use Buffer.byteLength
is
provided to minimize build size.
var truncate = require("truncate-utf8-bytes")
var str = "a☃" // a = 1 byte, ☃ = 3 bytes
console.log(truncate(str, 2))
// -> "a"
var truncate = require("truncate-utf8-bytes")
When using browserify or webpack, this automatically resolves to an
implementation that does not use Buffer.byteLength
.
truncate(string, length)
Returns string
truncated to at most length
bytes in length.
FAQs
Truncate string to given length in bytes
We found that truncate-utf8-bytes demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.