Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

uint8array-tools

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

uint8array-tools - npm Package Compare versions

Comparing version 0.0.7 to 0.0.8

8

package.json
{
"name": "uint8array-tools",
"version": "0.0.7",
"version": "0.0.8",
"description": "A library for dealing with Uint8Arrays.",

@@ -18,6 +18,8 @@ "homepage": "https://github.com/bitcoinjs/uint8array-tools#readme",

"require": "./src/cjs/index.cjs",
"import": "./src/mjs/index.js"
"import": "./src/mjs/index.js",
"types": "./src/cjs/index.d.ts"
},
"browser": "./src/mjs/browser.js",
"default": "./src/mjs/browser.js"
"default": "./src/mjs/browser.js",
"types": "./src/cjs/index.d.ts"
},

@@ -24,0 +26,0 @@ "types": "src/cjs/index.d.ts",

@@ -12,4 +12,4 @@ # Uint8Array Tools

```js
import * as uint8arraytools from 'uint8array-tools';
uint8arraytools.fromHex('ff');
import * as uint8arraytools from "uint8array-tools";
uint8arraytools.fromHex("ff");
// Uint8Array(1) [ 255 ]

@@ -24,2 +24,19 @@ uint8arraytools.toHex(Uint8Array.from([0xff]));

// -1
```
uint8arraytools.fromUtf8("tools");
// Uint8Array(5) [ 116, 111, 111, 108, 115 ]
uint8arraytools.toUtf8(Uint8Array.from([116, 111, 111, 108, 115]));
// tools
uint8arraytools.concat([Uint8Array.from([1]), Uint8Array.from([2])]);
// Uint8Array(2) [ 1, 2 ]
uint8arraytools.fromBase64("dG9vbHM=");
// Uint8Array(3) [ 182, 138, 37 ]
uint8arraytools.toBase64(Uint8Array.from([116, 111, 111, 108, 115]));
// dG9vbHM=
const uint8array = new Uint8Array(2);
uint8arraytools.writeUInt16(uint8array, 0, 0xffff - 1, "LE");
uint8array;
// Uint8Array(2) [ 254, 255 ]
uint8arraytools.readUInt16(uint8array, 0, "LE");
// 65534
```

@@ -0,4 +1,18 @@

export declare function toUtf8(bytes: Uint8Array): string;
export declare function fromUtf8(s: string): Uint8Array;
export declare function concat(arrays: Uint8Array[]): Uint8Array;
export declare function toHex(bytes: Uint8Array): string;
export declare function fromHex(hexString: string): Uint8Array;
export declare function toBase64(bytes: Uint8Array): string;
export declare function fromBase64(base64: string): Uint8Array;
export declare type CompareResult = -1 | 0 | 1;
export declare function compare(v1: Uint8Array, v2: Uint8Array): CompareResult;
export declare type endian = "LE" | "BE" | "le" | "be";
export declare function writeUInt8(buffer: Uint8Array, offset: number, value: number): void;
export declare function writeUInt16(buffer: Uint8Array, offset: number, value: number, littleEndian: endian): void;
export declare function writeUInt32(buffer: Uint8Array, offset: number, value: number, littleEndian: endian): void;
export declare function writeUInt64(buffer: Uint8Array, offset: number, value: bigint, littleEndian: endian): void;
export declare function readUInt8(buffer: Uint8Array, offset: number): number;
export declare function readUInt16(buffer: Uint8Array, offset: number, littleEndian: endian): number;
export declare function readUInt32(buffer: Uint8Array, offset: number, littleEndian: endian): number;
export declare function readUInt64(buffer: Uint8Array, offset: number, littleEndian: endian): bigint;

@@ -12,3 +12,19 @@ const HEX_STRINGS = "0123456789abcdefABCDEF";

const ENCODER = new TextEncoder();
const DECODER = new TextDecoder("ascii");
const DECODER = new TextDecoder();
export function toUtf8(bytes) {
return DECODER.decode(bytes);
}
export function fromUtf8(s) {
return ENCODER.encode(s);
}
export function concat(arrays) {
const totalLength = arrays.reduce((a, b) => a + b.length, 0);
const result = new Uint8Array(totalLength);
let offset = 0;
for (const array of arrays) {
result.set(array, offset);
offset += array.length;
}
return result;
}
// There are two implementations.

@@ -55,2 +71,13 @@ // One optimizes for length of the bytes, and uses TextDecoder.

}
export function toBase64(bytes) {
return btoa(String.fromCharCode(...bytes));
}
export function fromBase64(base64) {
const binaryString = atob(base64);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes;
}
// Same behavior as Buffer.compare()

@@ -66,1 +93,153 @@ export function compare(v1, v2) {

}
export function writeUInt8(buffer, offset, value) {
if (offset + 1 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
if (value > 0xff) {
throw new Error(`The value of "value" is out of range. It must be >= 0 and <= ${0xff}. Received ${value}`);
}
buffer[offset] = value;
}
export function writeUInt16(buffer, offset, value, littleEndian) {
if (offset + 2 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
if (value > 0xffff) {
throw new Error(`The value of "value" is out of range. It must be >= 0 and <= ${0xffff}. Received ${value}`);
}
if (littleEndian === "LE") {
buffer[offset] = value & 0xff;
buffer[offset + 1] = (value >> 8) & 0xff;
}
else {
buffer[offset] = (value >> 8) & 0xff;
buffer[offset + 1] = value & 0xff;
}
}
export function writeUInt32(buffer, offset, value, littleEndian) {
if (offset + 4 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
if (value > 0xffffffff) {
throw new Error(`The value of "value" is out of range. It must be >= 0 and <= ${0xffffffff}. Received ${value}`);
}
if (littleEndian === "LE") {
buffer[offset] = value & 0xff;
buffer[offset + 1] = (value >> 8) & 0xff;
buffer[offset + 2] = (value >> 16) & 0xff;
buffer[offset + 3] = (value >> 24) & 0xff;
}
else {
buffer[offset] = (value >> 24) & 0xff;
buffer[offset + 1] = (value >> 16) & 0xff;
buffer[offset + 2] = (value >> 8) & 0xff;
buffer[offset + 3] = value & 0xff;
}
}
export function writeUInt64(buffer, offset, value, littleEndian) {
if (offset + 8 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
if (value > 0xffffffffffffffffn) {
throw new Error(`The value of "value" is out of range. It must be >= 0 and <= ${0xffffffffffffffffn}. Received ${value}`);
}
if (littleEndian === "LE") {
buffer[offset] = Number(value & 0xffn);
buffer[offset + 1] = Number((value >> 8n) & 0xffn);
buffer[offset + 2] = Number((value >> 16n) & 0xffn);
buffer[offset + 3] = Number((value >> 24n) & 0xffn);
buffer[offset + 4] = Number((value >> 32n) & 0xffn);
buffer[offset + 5] = Number((value >> 40n) & 0xffn);
buffer[offset + 6] = Number((value >> 48n) & 0xffn);
buffer[offset + 7] = Number((value >> 56n) & 0xffn);
}
else {
buffer[offset] = Number((value >> 56n) & 0xffn);
buffer[offset + 1] = Number((value >> 48n) & 0xffn);
buffer[offset + 2] = Number((value >> 40n) & 0xffn);
buffer[offset + 3] = Number((value >> 32n) & 0xffn);
buffer[offset + 4] = Number((value >> 24n) & 0xffn);
buffer[offset + 5] = Number((value >> 16n) & 0xffn);
buffer[offset + 6] = Number((value >> 8n) & 0xffn);
buffer[offset + 7] = Number(value & 0xffn);
}
}
export function readUInt8(buffer, offset) {
if (offset + 1 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
return buffer[offset];
}
export function readUInt16(buffer, offset, littleEndian) {
if (offset + 2 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
if (littleEndian === "LE") {
let num = 0;
num = (num << 8) + buffer[offset + 1];
num = (num << 8) + buffer[offset];
return num;
}
else {
let num = 0;
num = (num << 8) + buffer[offset];
num = (num << 8) + buffer[offset + 1];
return num;
}
}
export function readUInt32(buffer, offset, littleEndian) {
if (offset + 4 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
if (littleEndian === "LE") {
let num = 0;
num = ((num << 8) + buffer[offset + 3]) >>> 0;
num = ((num << 8) + buffer[offset + 2]) >>> 0;
num = ((num << 8) + buffer[offset + 1]) >>> 0;
num = ((num << 8) + buffer[offset]) >>> 0;
return num;
}
else {
let num = 0;
num = ((num << 8) + buffer[offset]) >>> 0;
num = ((num << 8) + buffer[offset + 1]) >>> 0;
num = ((num << 8) + buffer[offset + 2]) >>> 0;
num = ((num << 8) + buffer[offset + 3]) >>> 0;
return num;
}
}
export function readUInt64(buffer, offset, littleEndian) {
if (offset + 8 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
if (littleEndian === "LE") {
let num = 0n;
num = (num << 8n) + BigInt(buffer[offset + 7]);
num = (num << 8n) + BigInt(buffer[offset + 6]);
num = (num << 8n) + BigInt(buffer[offset + 5]);
num = (num << 8n) + BigInt(buffer[offset + 4]);
num = (num << 8n) + BigInt(buffer[offset + 3]);
num = (num << 8n) + BigInt(buffer[offset + 2]);
num = (num << 8n) + BigInt(buffer[offset + 1]);
num = (num << 8n) + BigInt(buffer[offset]);
return num;
}
else {
let num = 0n;
num = (num << 8n) + BigInt(buffer[offset]);
num = (num << 8n) + BigInt(buffer[offset + 1]);
num = (num << 8n) + BigInt(buffer[offset + 2]);
num = (num << 8n) + BigInt(buffer[offset + 3]);
num = (num << 8n) + BigInt(buffer[offset + 4]);
num = (num << 8n) + BigInt(buffer[offset + 5]);
num = (num << 8n) + BigInt(buffer[offset + 6]);
num = (num << 8n) + BigInt(buffer[offset + 7]);
return num;
}
}

@@ -0,1 +1,10 @@

export function toUtf8(bytes) {
return Buffer.from(bytes || []).toString();
}
export function fromUtf8(s) {
return Uint8Array.from(Buffer.from(s || "", "utf8"));
}
export function concat(arrays) {
return Uint8Array.from(Buffer.concat(arrays));
}
export function toHex(bytes) {

@@ -7,4 +16,109 @@ return Buffer.from(bytes || []).toString("hex");

}
export function toBase64(bytes) {
return Buffer.from(bytes).toString("base64");
}
export function fromBase64(base64) {
return Uint8Array.from(Buffer.from(base64 || "", "base64"));
}
export function compare(v1, v2) {
return Buffer.from(v1).compare(Buffer.from(v2));
}
export function writeUInt8(buffer, offset, value) {
if (offset + 1 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
const buf = Buffer.alloc(1);
buf.writeUInt8(value, 0);
buffer.set(Uint8Array.from(buf), offset);
}
export function writeUInt16(buffer, offset, value, littleEndian) {
if (offset + 2 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
const buf = Buffer.alloc(2);
if (littleEndian === "LE") {
buf.writeUInt16LE(value, 0);
}
else {
buf.writeUInt16BE(value, 0);
}
buffer.set(Uint8Array.from(buf), offset);
}
export function writeUInt32(buffer, offset, value, littleEndian) {
if (offset + 4 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
const buf = Buffer.alloc(4);
if (littleEndian === "LE") {
buf.writeUInt32LE(value, 0);
}
else {
buf.writeUInt32BE(value, 0);
}
buffer.set(Uint8Array.from(buf), offset);
}
export function writeUInt64(buffer, offset, value, littleEndian) {
if (offset + 8 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
const buf = Buffer.alloc(8);
if (value > 0xffffffffffffffffn) {
throw new Error(`The value of "value" is out of range. It must be >= 0 and <= ${0xffffffffffffffffn}. Received ${value}`);
}
if (littleEndian === "LE") {
buf.writeBigUInt64LE(value, 0);
}
else {
buf.writeBigUInt64BE(value, 0);
}
buffer.set(Uint8Array.from(buf), offset);
}
export function readUInt8(buffer, offset) {
if (offset + 1 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
const buf = Buffer.from(buffer);
return buf.readUInt8(offset);
}
export function readUInt16(buffer, offset, littleEndian) {
if (offset + 2 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
const buf = Buffer.from(buffer);
if (littleEndian === "LE") {
return buf.readUInt16LE(offset);
}
else {
return buf.readUInt16BE(offset);
}
}
export function readUInt32(buffer, offset, littleEndian) {
if (offset + 4 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
const buf = Buffer.from(buffer);
if (littleEndian === "LE") {
return buf.readUInt32LE(offset);
}
else {
return buf.readUInt32BE(offset);
}
}
export function readUInt64(buffer, offset, littleEndian) {
if (offset + 8 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
const buf = Buffer.from(buffer);
if (littleEndian === "LE") {
return buf.readBigUInt64LE(offset);
}
else {
return buf.readBigUInt64BE(offset);
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc