
Security News
Open Source Maintainers Feeling the Weight of the EU’s Cyber Resilience Act
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
typedarray-to-buffer
Advanced tools
The typedarray-to-buffer npm package is a simple utility that converts a TypedArray or ArrayBuffer to a Node.js Buffer without copying the underlying memory. It's particularly useful when you need to work with Node.js buffers but are starting with a TypedArray (like Uint8Array) or an ArrayBuffer that you've obtained from a different context, such as the Web API or another library that doesn't use Node.js buffers.
Convert TypedArray to Buffer
This feature allows you to convert a TypedArray, such as Uint8Array, to a Node.js Buffer instance. The conversion is done without copying the data, making it a fast operation.
var toBuffer = require('typedarray-to-buffer');
var uint8array = new Uint8Array([1, 2, 3]);
var buffer = toBuffer(uint8array);
This package provides a way to create a new Buffer instance from a variety of inputs including TypedArrays, Buffers, strings, and arrays. It's similar to typedarray-to-buffer but offers more input options and creates a new Buffer with copied data.
The to-arraybuffer package is designed to convert a Buffer or a string to an ArrayBuffer. It's similar in that it deals with conversions between Buffer and ArrayBuffer, but it goes in the opposite direction compared to typedarray-to-buffer.
Say you're using the 'buffer' module on npm, or browserify and you're working with lots of binary data.
Unfortunately, sometimes the browser or someone else's API gives you an ArrayBuffer
or a typed array like Uint8Array
to work with and you need to convert it to a
Buffer
. What do you do?
Of course: new Buffer(uint8array)
But, alas, every time you do new Buffer(uint8array)
the entire array gets copied.
The Buffer
constructor does a copy; this is
defined by the node docs and the 'buffer' module
matches the node API exactly.
So, how can we avoid this expensive copy in performance critical applications?
Simply use this module, of course!
npm install typedarray-to-buffer
To convert a typed array to a Buffer
without a copy, do this:
var toBuffer = require('typedarray-to-buffer')
var arr = new Uint8Array([1, 2, 3])
arr = toBuffer(arr)
// arr is a buffer now!
arr.toString() // '\u0001\u0002\u0003'
arr.readUInt16BE(0) // 258
If the browser supports typed arrays, then toBuffer
will augment the Uint8Array you
pass in with the Buffer
methods and return it. See
how does Buffer work? for more about
how augmentation works.
If the browser doesn't support typed arrays, then toBuffer
will create a new Buffer
object, copy the data into it, and return it. There's no simple performance optimization
we can do for old browsers. Oh well.
If this module is used in node, then it will just call new Buffer
. This is just for
the convenience of modules that work in both node and the browser.
MIT. Copyright (C) Feross Aboukhadijeh.
FAQs
Convert a typed array to a Buffer without a copy
The npm package typedarray-to-buffer receives a total of 18,655,861 weekly downloads. As such, typedarray-to-buffer popularity was classified as popular.
We found that typedarray-to-buffer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.