+6
-0
@@ -7,5 +7,11 @@ { | ||
| "globals": { | ||
| "Float64Array": "readonly", | ||
| "Uint8Array": "readonly", | ||
| "Uint16Array": "readonly", | ||
| }, | ||
| "rules": { | ||
| "complexity": "off", | ||
| "max-lines-per-function": "off", | ||
| }, | ||
| } |
+9
-0
@@ -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.'); | ||
| }; |
+3
-2
| { | ||
| "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": { |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
13669
12.39%96
26.32%0
-100%3
50%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added