Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

to-buffer

Package Overview
Dependencies
Maintainers
2
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

to-buffer - npm Package Compare versions

Comparing version
1.2.0
to
1.2.1
+6
-0
.eslintrc

@@ -7,5 +7,11 @@ {

"globals": {
"Float64Array": "readonly",
"Uint8Array": "readonly",
"Uint16Array": "readonly",
},
"rules": {
"complexity": "off",
"max-lines-per-function": "off",
},
}

@@ -8,2 +8,11 @@ # Changelog

## [v1.2.1](https://github.com/browserify/to-buffer/compare/v1.2.0...v1.2.1) - 2025-06-19
### Commits
- [Fix] handle non-Uint8Arrays in node < 3 [`7f8a881`](https://github.com/browserify/to-buffer/commit/7f8a881929133935f8e15ffd60d6dbbc513b2c5f)
- [Tests] add coverage [`286c96a`](https://github.com/browserify/to-buffer/commit/286c96a52cfeee14a2ba974d78071bdd667e9360)
- [Fix] provide a fallback for engines without `ArrayBuffer.isView` [`e336166`](https://github.com/browserify/to-buffer/commit/e336166b8f4bf13860bafa191ee1ec53fca2e331)
- [Fix] correct error message [`b45247e`](https://github.com/browserify/to-buffer/commit/b45247ed337fb44b2c8d74a14e8f86d985119fb9)
## [v1.2.0](https://github.com/browserify/to-buffer/compare/v1.1.1...v1.2.0) - 2025-06-17

@@ -10,0 +19,0 @@

+34
-11

@@ -5,8 +5,17 @@ 'use strict';

var isArray = require('isarray');
var typedArrayBuffer = require('typed-array-buffer');
var isView = ArrayBuffer.isView || function isView(obj) {
try {
typedArrayBuffer(obj);
return true;
} catch (e) {
return false;
}
};
var useUint8Array = typeof Uint8Array !== 'undefined';
var useArrayBuffer = typeof ArrayBuffer !== 'undefined'
&& typeof Uint8Array !== 'undefined'
&& ArrayBuffer.isView
&& (Buffer.prototype instanceof Uint8Array || Buffer.TYPED_ARRAY_SUPPORT);
&& typeof Uint8Array !== 'undefined';
var useFromArrayBuffer = useArrayBuffer && (Buffer.prototype instanceof Uint8Array || Buffer.TYPED_ARRAY_SUPPORT);

@@ -22,3 +31,2 @@ module.exports = function toBuffer(data, encoding) {

// Convert strings to Buffer
if (typeof data === 'string') {

@@ -32,3 +40,3 @@ return Buffer.from(data, encoding);

*/
if (useArrayBuffer && ArrayBuffer.isView(data)) {
if (useArrayBuffer && isView(data)) {
// Bug in Node.js <6.3.1, which treats this as out-of-bounds

@@ -39,9 +47,24 @@ if (data.byteLength === 0) {

var res = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
// When Buffer is based on Uint8Array, we can just construct it from ArrayBuffer
if (useFromArrayBuffer) {
var res = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
/*
* Recheck result size, as offset/length doesn't work on Node.js <5.10
* We just go to Uint8Array case if this fails
*/
if (res.byteLength === data.byteLength) {
return res;
}
}
// Convert to Uint8Array bytes and then to Buffer
var uint8 = data instanceof Uint8Array ? data : new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
var result = Buffer.from(uint8);
/*
* Recheck result size, as offset/length doesn't work on Node.js <5.10
* We just go to Uint8Array case if this fails
* Let's recheck that conversion succeeded
* We have .length but not .byteLength when useFromArrayBuffer is false
*/
if (res.byteLength === data.byteLength) {
return res;
if (result.length === data.byteLength) {
return result;
}

@@ -89,3 +112,3 @@ }

throw new TypeError('The "data" argument must be a string, an Array, a Buffer, a TypedArray, or a DataView.');
throw new TypeError('The "data" argument must be a string, an Array, a Buffer, a Uint8Array, or a DataView.');
};
{
"name": "to-buffer",
"version": "1.2.0",
"version": "1.2.1",
"description": "Pass in a string, array, Buffer, Data View, or Uint8Array, and get a Buffer back.",

@@ -29,3 +29,4 @@ "main": "index.js",

"isarray": "^2.0.5",
"safe-buffer": "^5.2.1"
"safe-buffer": "^5.2.1",
"typed-array-buffer": "^1.0.3"
},

@@ -32,0 +33,0 @@ "devDependencies": {