buffer-from
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
"Buffer-from" is an npm package that acts as a ponyfill for the Buffer.from
function in JavaScript. Essentially, it uses the native implementation of Buffer.from
if available but provides a fallback version if not, making it a very useful tool in backwards compatibility context. Its purpose is to allocate a new Buffer using various types of input, such as an array of octets, an ArrayBuffer and a specific range within it, an existing Buffer, or a particular JavaScript string with a specified encoding.
To use "buffer-from", you'd first need to install it using npm with the command npm install --save buffer-from
. Once installed, you'd require it in your JavaScript file like so: const bufferFrom = require('buffer-from')
. "Buffer-from" provides different use-cases and ways to manipulate data to obtain Buffer objects, which can give you the exact Byte data that you need. Here are some examples:
If you want to create a buffer from an array of octets, you would use:
const bufferFrom = require('buffer-from')
console.log(bufferFrom([1, 2, 3, 4]))
//=> <Buffer 01 02 03 04>
To use a TypedArray or ArrayBuffer and specify a memory range, you could use:
const arr = new Uint8Array([1, 2, 3, 4])
console.log(bufferFrom(arr.buffer, 1, 2))
//=> <Buffer 02 03>
Creating a new buffer from a string with specific encoding looks like this:
console.log(bufferFrom('test', 'utf8'))
//=> <Buffer 74 65 73 74>
And copying data from an existing buffer to a new buffer would look like this:
const buf = bufferFrom('test')
console.log(bufferFrom(buf))
//=> <Buffer 74 65 73 74>
The documentation for "buffer-from" can be found within the README file in the package's GitHub repository, which is hosted at https://github.com/LinusU/buffer-from. The README provides detailed installation and usage instructions, along with descriptions of the API methods and the parameters they accept.