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.1.1
to
1.2.0
+10
.eslintrc
{
"root": true,
"extends": "@ljharb",
"globals": {
"Uint8Array": "readonly",
"Uint16Array": "readonly",
},
}
# These are supported funding model platforms
github: [ljharb, mafintosh]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: npm/to-buffer
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']

Sorry, the diff of this file is not supported yet

# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [v1.2.0](https://github.com/browserify/to-buffer/compare/v1.1.1...v1.2.0) - 2025-06-17
### Commits
- [New] replace with implementation from cipher-base [`970adb5`](https://github.com/browserify/to-buffer/commit/970adb5523efdaa13f5ecb82967fc9f617865549)
- [Tests] migrate from travis to GHA [`8084393`](https://github.com/browserify/to-buffer/commit/808439337ca9ac3dbb8399079aaa2a4cb738627c)
- [eslint] fix whitespace [`a62e651`](https://github.com/browserify/to-buffer/commit/a62e651b661adf98c17e9e486b55bb8ff0ffb8c9)
- [eslint] fix semicolon usage [`4d85c63`](https://github.com/browserify/to-buffer/commit/4d85c6318c72feae8d19037937154f9b99ba266f)
- [meta] add `auto-changelog` [`aa0279c`](https://github.com/browserify/to-buffer/commit/aa0279c5199ca7fe39acfb950a4c824e97f06232)
- [readme] update URLs, add badges [`ff77d90`](https://github.com/browserify/to-buffer/commit/ff77d90b89de7b02538ecb2d6a89da086093bed8)
- [lint] switch to eslint [`e45f467`](https://github.com/browserify/to-buffer/commit/e45f467c7229e632cd3c10fc02895ba4d3204bbe)
- [Fix] validate that arrays contain valid byte values [`c2fb75e`](https://github.com/browserify/to-buffer/commit/c2fb75edf2fb113d58599e990f86574b4dfa62d8)
- [Fix] restore previous implementation Array behavior [`cb93b75`](https://github.com/browserify/to-buffer/commit/cb93b75a79caa9897f6c29ecde91f4ae35f704fe)
- [Tests] add nyc for coverage [`ab7026e`](https://github.com/browserify/to-buffer/commit/ab7026e36e3716c8101229f426e7f4571e55794b)
- [Refactor] use `safe-buffer`.from instead of `buffer-from` [`8e01307`](https://github.com/browserify/to-buffer/commit/8e01307191245044469e47695c5c2675b85e84e9)
- [Fix] Replace Buffer.from with `buffer-from` [`d652e54`](https://github.com/browserify/to-buffer/commit/d652e54e2396a47358a553c447e0f338b4c2dc67)
- [Tests] use `deepEqual` over `same` alias [`66a5548`](https://github.com/browserify/to-buffer/commit/66a55480258011bb5d81c5aad1360468f418d0b4)
- [meta] add `npmignore` [`90ce602`](https://github.com/browserify/to-buffer/commit/90ce6023737d50521aff44d87063db1e3f7e352a)
- [Tests] move into a test dir, update tape [`08aea81`](https://github.com/browserify/to-buffer/commit/08aea81b61b90d1fcb7e9275b5b0ba718531d9a8)
- Only apps should have lockfiles [`16ccceb`](https://github.com/browserify/to-buffer/commit/16ccceb23f350be16e80111188c90a3492916f5d)
- [Tests] add coverage [`d2cba2e`](https://github.com/browserify/to-buffer/commit/d2cba2ec76ed43c83e2a7a91f58fa7640aeb61e9)
- [meta] update description [`2cf2a20`](https://github.com/browserify/to-buffer/commit/2cf2a200a31f9543d2e8a24b5ea0e8bd843166c3)
- [Fix] add `safe-buffer`, missing from 970adb5 [`d9a0dea`](https://github.com/browserify/to-buffer/commit/d9a0dead7c638d188f8b48cdf2b5fd6cfa886071)
- [meta] temporarily limit support to node v0.10 [`8dca458`](https://github.com/browserify/to-buffer/commit/8dca458bd9a2c6b84d6e3a1996e41b242fe1c49a)
- [meta] add missing `engines.node` [`35bdfcb`](https://github.com/browserify/to-buffer/commit/35bdfcb3a71dfbd5b35d35250c9dac19c99f4197)
- [Dev Deps] add missing peer dep [`220143f`](https://github.com/browserify/to-buffer/commit/220143f1f6e47154380c27a2d88ce300104007fa)
- [meta] add `sideEffects` flag [`cd37473`](https://github.com/browserify/to-buffer/commit/cd374738d24b22b029862b3c27b9e247d4e62daf)
## [v1.1.1](https://github.com/browserify/to-buffer/compare/v1.1.0...v1.1.1) - 2018-04-26
### Commits
- use Buffer.from when avail [`eebe20e`](https://github.com/browserify/to-buffer/commit/eebe20e0603e2c6a542b00316f1661741fdf1124)
## [v1.1.0](https://github.com/browserify/to-buffer/compare/v1.0.1...v1.1.0) - 2017-04-12
### Merged
- Fix typo [`#2`](https://github.com/browserify/to-buffer/pull/2)
### Commits
- support arrays as well [`ef98c82`](https://github.com/browserify/to-buffer/commit/ef98c82791d71601077577e84c4614ec2d05f086)
## [v1.0.1](https://github.com/browserify/to-buffer/compare/v1.0.0...v1.0.1) - 2016-02-15
### Commits
- fix desc [`7d50a7c`](https://github.com/browserify/to-buffer/commit/7d50a7c69c3eef77448893744ada16601c44af6a)
## v1.0.0 - 2016-02-15
### Commits
- first commit [`8361941`](https://github.com/browserify/to-buffer/commit/8361941d7acb3b82c732ecd10bdb047da5af2028)
- add travis [`de911b5`](https://github.com/browserify/to-buffer/commit/de911b5364558561d84b4ec9e43c6a0fe1c8e904)
+83
-11

@@ -1,14 +0,86 @@

module.exports = toBuffer
'use strict';
var makeBuffer = Buffer.from && Buffer.from !== Uint8Array.from ? Buffer.from : bufferFrom
var Buffer = require('safe-buffer').Buffer;
var isArray = require('isarray');
function bufferFrom (buf, enc) {
return new Buffer(buf, enc)
}
var useUint8Array = typeof Uint8Array !== 'undefined';
var useArrayBuffer = typeof ArrayBuffer !== 'undefined'
&& typeof Uint8Array !== 'undefined'
&& ArrayBuffer.isView
&& (Buffer.prototype instanceof Uint8Array || Buffer.TYPED_ARRAY_SUPPORT);
function toBuffer (buf, enc) {
if (Buffer.isBuffer(buf)) return buf
if (typeof buf === 'string') return makeBuffer(buf, enc)
if (Array.isArray(buf)) return makeBuffer(buf)
throw new Error('Input should be a buffer or a string')
}
module.exports = function toBuffer(data, encoding) {
/*
* No need to do anything for exact instance
* This is only valid when safe-buffer.Buffer === buffer.Buffer, i.e. when Buffer.from/Buffer.alloc existed
*/
if (data instanceof Buffer) {
return data;
}
// Convert strings to Buffer
if (typeof data === 'string') {
return Buffer.from(data, encoding);
}
/*
* Wrap any TypedArray instances and DataViews
* Makes sense only on engines with full TypedArray support -- let Buffer detect that
*/
if (useArrayBuffer && ArrayBuffer.isView(data)) {
// Bug in Node.js <6.3.1, which treats this as out-of-bounds
if (data.byteLength === 0) {
return Buffer.alloc(0);
}
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;
}
}
/*
* Uint8Array in engines where Buffer.from might not work with ArrayBuffer, just copy over
* Doesn't make sense with other TypedArray instances
*/
if (useUint8Array && data instanceof Uint8Array) {
return Buffer.from(data);
}
var isArr = isArray(data);
if (isArr) {
for (var i = 0; i < data.length; i += 1) {
var x = data[i];
if (
typeof x !== 'number'
|| x < 0
|| x > 255
|| ~~x !== x // NaN and integer check
) {
throw new RangeError('Array items must be numbers in the range 0-255.');
}
}
}
/*
* Old Buffer polyfill on an engine that doesn't have TypedArray support
* Also, this is from a different Buffer polyfill implementation then we have, as instanceof check failed
* Convert to our current Buffer implementation
*/
if (
isArr || (
Buffer.isBuffer(data)
&& data.constructor
&& typeof data.constructor.isBuffer === 'function'
&& data.constructor.isBuffer(data)
)
) {
return Buffer.from(data);
}
throw new TypeError('The "data" argument must be a string, an Array, a Buffer, a TypedArray, or a DataView.');
};
+57
-22
{
"name": "to-buffer",
"version": "1.1.1",
"description": "Pass in a string, get a buffer back. Pass in a buffer, get the same buffer back",
"main": "index.js",
"dependencies": {},
"devDependencies": {
"standard": "^6.0.5",
"tape": "^4.4.0"
},
"scripts": {
"test": "standard && tape test.js"
},
"repository": {
"type": "git",
"url": "https://github.com/mafintosh/to-buffer.git"
},
"author": "Mathias Buus (@mafintosh)",
"license": "MIT",
"bugs": {
"url": "https://github.com/mafintosh/to-buffer/issues"
},
"homepage": "https://github.com/mafintosh/to-buffer"
"name": "to-buffer",
"version": "1.2.0",
"description": "Pass in a string, array, Buffer, Data View, or Uint8Array, and get a Buffer back.",
"main": "index.js",
"sideEffects": false,
"scripts": {
"prepack": "npmignore --auto --commentLines=autogenerated",
"lint": "eslint --ext=js,mjs .",
"pretest": "npm run lint",
"tests-only": "nyc tape 'test/**/*'",
"test": "npm run tests-only",
"posttest": "npx npm@\">= 10.2\" audit --production",
"version": "auto-changelog && git add CHANGELOG.md",
"postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\""
},
"repository": {
"type": "git",
"url": "https://github.com/browserify/to-buffer.git"
},
"author": "Mathias Buus (@mafintosh)",
"license": "MIT",
"bugs": {
"url": "https://github.com/browserify/to-buffer/issues"
},
"homepage": "https://github.com/browserify/to-buffer",
"dependencies": {
"isarray": "^2.0.5",
"safe-buffer": "^5.2.1"
},
"devDependencies": {
"@ljharb/eslint-config": "^21.1.1",
"auto-changelog": "^2.5.0",
"available-typed-arrays": "^1.0.7",
"encoding": "^0.1.13",
"eslint": "=8.8.0",
"for-each": "^0.3.5",
"npmignore": "^0.3.1",
"nyc": "^10.3.2",
"tape": "^5.9.0"
},
"engines": {
"node": ">= 0.4"
},
"publishConfig": {
"ignore": [
".github/workflows",
"test"
]
},
"auto-changelog": {
"output": "CHANGELOG.md",
"template": "keepachangelog",
"unreleased": false,
"commitLimit": false,
"backfillLimit": false,
"hideCredit": true
}
}
+33
-12

@@ -1,5 +0,15 @@

# to-buffer
# to-buffer <sup>[![Version Badge][2]][1]</sup>
Pass in a string, get a buffer back. Pass in a buffer, get the same buffer back.
Pass in a string, array, Buffer, Data View, or Uint8Array, and get a Buffer back.
[![github actions][actions-image]][actions-url]
[![coverage][codecov-image]][codecov-url]
[![dependency status][5]][6]
[![dev dependency status][7]][8]
[![License][license-image]][license-url]
[![Downloads][downloads-image]][downloads-url]
[![npm badge][11]][1]
```

@@ -9,16 +19,27 @@ npm install to-buffer

[![build status](https://travis-ci.org/mafintosh/to-buffer.svg?branch=master)](https://travis-ci.org/mafintosh/to-buffer)
## Usage
``` js
var toBuffer = require('to-buffer')
console.log(toBuffer('hi')) // <Buffer 68 69>
console.log(toBuffer(Buffer('hi'))) // <Buffer 68 69>
console.log(toBuffer('6869', 'hex')) // <Buffer 68 69>
console.log(toBuffer(43)) // throws
var toBuffer = require('to-buffer');
console.log(toBuffer('hi')); // <Buffer 68 69>
console.log(toBuffer(Buffer('hi'))); // <Buffer 68 69>
console.log(toBuffer('6869', 'hex')); // <Buffer 68 69>
console.log(toBuffer(43)); // throws
```
## License
MIT
[1]: https://npmjs.org/package/to-buffer
[2]: https://versionbadg.es/browserify/to-buffer.svg
[5]: https://david-dm.org/browserify/to-buffer.svg
[6]: https://david-dm.org/browserify/to-buffer
[7]: https://david-dm.org/browserify/to-buffer/dev-status.svg
[8]: https://david-dm.org/browserify/to-buffer#info=devDependencies
[11]: https://nodei.co/npm/to-buffer.png?downloads=true&stars=true
[license-image]: https://img.shields.io/npm/l/to-buffer.svg
[license-url]: LICENSE
[downloads-image]: https://img.shields.io/npm/dm/to-buffer.svg
[downloads-url]: https://npm-stat.com/charts.html?package=to-buffer
[codecov-image]: https://codecov.io/gh/browserify/to-buffer/branch/main/graphs/badge.svg
[codecov-url]: https://app.codecov.io/gh/browserify/to-buffer/
[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/browserify/to-buffer
[actions-url]: https://github.com/browserify/to-buffer/actions
sudo: false
language: node_js
node_js:
- "5"
- "4"
- "0.12"
- "0.10"
var tape = require('tape')
var toBuffer = require('./')
tape('buffer returns buffer', function (t) {
t.same(toBuffer(Buffer('hi')), Buffer('hi'))
t.end()
})
tape('string returns buffer', function (t) {
t.same(toBuffer('hi'), Buffer('hi'))
t.end()
})
tape('string + enc returns buffer', function (t) {
t.same(toBuffer('6869', 'hex'), Buffer('hi'))
t.end()
})
tape('other input throws', function (t) {
try {
toBuffer(42)
} catch (err) {
t.same(err.message, 'Input should be a buffer or a string')
t.end()
}
})