
Security News
GitHub Actions Supply Chain Attack Puts Thousands of Projects at Risk
A compromised GitHub Action exposed secrets in CI/CD logs, putting thousands of projects at risk and forcing developers to urgently secure their workflows.
uint8arrays
Advanced tools
The uint8arrays npm package provides utilities for working with Uint8Array objects in JavaScript. It includes functions for concatenation, comparison, conversion to and from strings, and more.
Concatenation
This feature allows you to concatenate multiple Uint8Array objects into a single Uint8Array.
const uint8arrays = require('uint8arrays');
const arr1 = new Uint8Array([1, 2, 3]);
const arr2 = new Uint8Array([4, 5, 6]);
const concatenated = uint8arrays.concat([arr1, arr2]);
console.log(concatenated); // Uint8Array [1, 2, 3, 4, 5, 6]
Comparison
This feature allows you to compare two Uint8Array objects for equality.
const uint8arrays = require('uint8arrays');
const arr1 = new Uint8Array([1, 2, 3]);
const arr2 = new Uint8Array([1, 2, 3]);
const arr3 = new Uint8Array([4, 5, 6]);
console.log(uint8arrays.equals(arr1, arr2)); // true
console.log(uint8arrays.equals(arr1, arr3)); // false
Conversion to and from Strings
This feature allows you to convert Uint8Array objects to and from strings using various encodings.
const uint8arrays = require('uint8arrays');
const arr = new Uint8Array([72, 101, 108, 108, 111]);
const str = uint8arrays.toString(arr, 'utf8');
console.log(str); // 'Hello'
const newArr = uint8arrays.fromString('Hello', 'utf8');
console.log(newArr); // Uint8Array [72, 101, 108, 108, 111]
The buffer package provides a way of handling binary data in Node.js. It includes many similar functionalities to uint8arrays, such as concatenation, comparison, and conversion to and from strings. However, buffer is more tightly integrated with Node.js and provides additional features like reading and writing to files.
The typedarray package provides polyfills for JavaScript's typed array functionality, including Uint8Array. While it doesn't offer as many utility functions as uint8arrays, it ensures compatibility with older JavaScript environments that do not support typed arrays natively.
Utility functions to make dealing with Uint8Arrays easier
Uint8Array
s bring memory-efficient(ish) byte handling to browsers - they are similar to Node.js Buffer
s but lack a lot of the utility methods present on that class.
This module exports a number of function that let you do common operations - joining Uint8Arrays together, seeing if they have the same contents etc.
Since Node.js Buffer
s are also Uint8Array
s, it falls back to Buffer
internally where it makes sense for performance reasons.
Create a new Uint8Array
. When running under Node.js, Buffer
will be used in preference to Uint8Array
.
import { alloc } from 'uint8arrays/alloc'
const buf = alloc(100)
Create a new Uint8Array
. When running under Node.js, Buffer
will be used in preference to Uint8Array
.
On platforms that support it, memory referenced by the returned Uint8Array
will not be initialized.
import { allocUnsafe } from 'uint8arrays/alloc'
const buf = allocUnsafe(100)
Compare two Uint8Arrays
import { compare } from 'uint8arrays/compare'
const arrays = [
Uint8Array.from([3, 4, 5]),
Uint8Array.from([0, 1, 2])
]
const sorted = arrays.sort(compare)
console.info(sorted)
// [
// Uint8Array[0, 1, 2]
// Uint8Array[3, 4, 5]
// ]
Concatenate one or more Uint8Array
s and return a Uint8Array
with their contents.
If you know the length of the arrays, pass it as a second parameter, otherwise it will be calculated by traversing the list of arrays.
import { concat } from 'uint8arrays/concat'
const arrays = [
Uint8Array.from([0, 1, 2]),
Uint8Array.from([3, 4, 5])
]
const all = concat(arrays, 6)
console.info(all)
// Uint8Array[0, 1, 2, 3, 4, 5]
Returns true if the two arrays are the same array or if they have the same length and contents.
import { equals } from 'uint8arrays/equals'
const a = Uint8Array.from([0, 1, 2])
const b = Uint8Array.from([3, 4, 5])
const c = Uint8Array.from([0, 1, 2])
console.info(equals(a, b)) // false
console.info(equals(a, c)) // true
console.info(equals(a, a)) // true
Returns a new Uint8Array
created from the passed string and interpreted as the passed encoding.
Supports utf8
and any of the multibase encodings as implemented by the multiformats module.
import { fromString } from 'uint8arrays/from-string'
console.info(fromString('hello world')) // Uint8Array[104, 101 ...
console.info(fromString('00010203aabbcc', 'base16')) // Uint8Array[0, 1 ...
console.info(fromString('AAECA6q7zA', 'base64')) // Uint8Array[0, 1 ...
console.info(fromString('01234', 'ascii')) // Uint8Array[48, 49 ...
Returns a string created from the passed Uint8Array
in the passed encoding.
Supports utf8
and any of the multibase encodings as implemented by the multiformats module.
import { toString } from 'uint8arrays/to-string'
console.info(toString(Uint8Array.from([104, 101...]))) // 'hello world'
console.info(toString(Uint8Array.from([0, 1, 2...]), 'base16')) // '00010203aabbcc'
console.info(toString(Uint8Array.from([0, 1, 2...]), 'base64')) // 'AAECA6q7zA'
console.info(toString(Uint8Array.from([48, 49, 50...]), 'ascii')) // '01234'
Returns a Uint8Array
containing a
and b
xored together.
import { xor } from 'uint8arrays/xor'
console.info(xor(Uint8Array.from([1, 0]), Uint8Array.from([0, 1]))) // Uint8Array[1, 1]
Compares the distances between two xor Uint8Array
s.
import { xor } from 'uint8arrays/xor'
import { xorCompare } from 'uint8arrays/xor-compare'
const target = Uint8Array.from([1, 1])
const val1 = Uint8Array.from([1, 0])
const xor1 = xor(target, val1)
const val2 = Uint8Array.from([0, 1])
const xor2 = xor(target, val2)
console.info(xorCompare(xor1, xor2)) // -1 or 0 or 1
$ npm i uint8arrays
<script>
tagLoading this module through a script tag will make it's exports available as Uint8arrays
in the global namespace.
<script src="https://unpkg.com/uint8arrays/dist/index.min.js"></script>
Licensed under either of
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
FAQs
Utility functions to make dealing with Uint8Arrays easier
The npm package uint8arrays receives a total of 852,475 weekly downloads. As such, uint8arrays popularity was classified as popular.
We found that uint8arrays demonstrated a healthy version release cadence and project activity because the last version was released less than 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
A compromised GitHub Action exposed secrets in CI/CD logs, putting thousands of projects at risk and forcing developers to urgently secure their workflows.
Research
Security News
A malicious Maven package typosquatting a popular library is secretly stealing OAuth credentials on the 15th of each month, putting Java developers at risk.
Security News
Socket and Seal Security collaborate to fix a critical npm overrides bug, resolving a three-year security issue in the JavaScript ecosystem's most popular package manager.