Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for bit writing along with tests #6

Merged
merged 7 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 161 additions & 1 deletion src/cjs/browser.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.compare = exports.fromHex = exports.toHex = exports.toUtf8 = void 0;
exports.readUInt64 = exports.readUInt32 = exports.readUInt16 = exports.readUInt8 = exports.writeUInt64 = exports.writeUInt32 = exports.writeUInt16 = exports.writeUInt8 = exports.compare = exports.fromHex = exports.toHex = exports.toUtf8 = void 0;
const HEX_STRINGS = "0123456789abcdefABCDEF";
const HEX_CODES = HEX_STRINGS.split("").map((c) => c.codePointAt(0));
const HEX_CODEPOINTS = Array(256)
Expand Down Expand Up @@ -71,3 +71,163 @@ function compare(v1, v2) {
return v1.length === v2.length ? 0 : v1.length > v2.length ? 1 : -1;
}
exports.compare = compare;
function writeUInt8(buffer, offset, value) {
if (offset + 1 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
if (value > 0xffn) {
throw new Error(`The value of "value" is out of range. It must be >= 0 and <= ${0xffn}. Received ${value}`);
}
buffer[offset] = value;
}
exports.writeUInt8 = writeUInt8;
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 > 0xffffn) {
throw new Error(`The value of "value" is out of range. It must be >= 0 and <= ${0xffffn}. 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;
}
}
exports.writeUInt16 = writeUInt16;
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 > 0xffffffffn) {
throw new Error(`The value of "value" is out of range. It must be >= 0 and <= ${0xffffffffn}. 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;
}
}
exports.writeUInt32 = writeUInt32;
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);
}
}
exports.writeUInt64 = writeUInt64;
function readUInt8(buffer, offset) {
if (offset + 1 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
return BigInt(buffer[offset]);
}
exports.readUInt8 = readUInt8;
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 = 0n;
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]);
return num;
}
}
exports.readUInt16 = readUInt16;
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 = 0n;
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]);
return num;
}
}
exports.readUInt32 = readUInt32;
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;
}
}
exports.readUInt64 = readUInt64;
109 changes: 108 additions & 1 deletion src/cjs/index.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.compare = exports.fromHex = exports.toHex = exports.toUtf8 = void 0;
exports.readUInt64 = exports.readUInt32 = exports.readUInt16 = exports.readUInt8 = exports.writeUInt64 = exports.writeUInt32 = exports.writeUInt16 = exports.writeUInt8 = exports.compare = exports.fromHex = exports.toHex = exports.toUtf8 = void 0;
function toUtf8(bytes) {
return Buffer.from(bytes || []).toString();
}
Expand All @@ -17,3 +17,110 @@ function compare(v1, v2) {
return Buffer.from(v1).compare(Buffer.from(v2));
}
exports.compare = compare;
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);
}
exports.writeUInt8 = writeUInt8;
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);
}
exports.writeUInt16 = writeUInt16;
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);
}
exports.writeUInt32 = writeUInt32;
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);
}
exports.writeUInt64 = writeUInt64;
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 BigInt(buf.readUInt8(offset));
}
exports.readUInt8 = readUInt8;
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 BigInt(buf.readUInt16LE(offset));
}
else {
return BigInt(buf.readUInt16BE(offset));
}
}
exports.readUInt16 = readUInt16;
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 BigInt(buf.readUInt32LE(offset));
}
else {
return BigInt(buf.readUInt32BE(offset));
}
}
exports.readUInt32 = readUInt32;
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);
}
}
exports.readUInt64 = readUInt64;
9 changes: 9 additions & 0 deletions src/cjs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@ export declare function toHex(bytes: Uint8Array): string;
export declare function fromHex(hexString: 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): bigint;
export declare function readUInt16(buffer: Uint8Array, offset: number, littleEndian: endian): bigint;
export declare function readUInt32(buffer: Uint8Array, offset: number, littleEndian: endian): bigint;
export declare function readUInt64(buffer: Uint8Array, offset: number, littleEndian: endian): bigint;
Loading
Loading