Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 25 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cldmv/uuid",
"version": "1.1.7",
"version": "1.2.0",
"description": "Extended RFC 4122 and RFC 9562 UUID implementation with custom variant structures, issuer-based identification, and timestamp variants",
"main": "./index.cjs",
"module": "./index.mjs",
Expand All @@ -17,6 +17,30 @@
},
"import": "./dist/uuid.mjs"
},
"./rng": {
"uuid-dev": {
"browser": "./src/lib/rng-browser.mjs",
"import": "./src/lib/rng.mjs"
},
"browser": "./dist/lib/rng-browser.mjs",
"import": "./dist/lib/rng.mjs"
},
"./bytes": {
"uuid-dev": {
"browser": "./src/lib/bytes-browser.mjs",
"import": "./src/lib/bytes.mjs"
},
"browser": "./dist/lib/bytes-browser.mjs",
"import": "./dist/lib/bytes.mjs"
},
"./hash": {
"uuid-dev": {
"browser": "./src/lib/hash-browser.mjs",
"import": "./src/lib/hash.mjs"
},
"browser": "./dist/lib/hash-browser.mjs",
"import": "./dist/lib/hash.mjs"
},
"./package.json": "./package.json"
},
"type": "module",
Expand Down
34 changes: 17 additions & 17 deletions src/lib/bit-utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
export class BitUtils {
/**
* Set specific bits in a buffer
* @param {Buffer} buffer - Target buffer
* @param {Uint8Array} buffer - Target buffer
* @param {number} startBit - Starting bit position (0-based)
* @param {number} bitCount - Number of bits to set
* @param {number|BigInt} value - Value to set
Expand Down Expand Up @@ -58,7 +58,7 @@ export class BitUtils {

/**
* Get specific bits from a buffer
* @param {Buffer} buffer - Source buffer
* @param {Uint8Array} buffer - Source buffer
* @param {number} startBit - Starting bit position (0-based)
* @param {number} bitCount - Number of bits to get
* @returns {BigInt} The extracted value as BigInt
Expand All @@ -82,7 +82,7 @@ export class BitUtils {

/**
* Get specific bits from a buffer as a regular number (for values <= 32 bits)
* @param {Buffer} buffer - Source buffer
* @param {Uint8Array} buffer - Source buffer
* @param {number} startBit - Starting bit position (0-based)
* @param {number} bitCount - Number of bits to get (max 32)
* @returns {number} The extracted value as number
Expand All @@ -96,7 +96,7 @@ export class BitUtils {

/**
* Clear specific bits in a buffer
* @param {Buffer} buffer - Target buffer
* @param {Uint8Array} buffer - Target buffer
* @param {number} startBit - Starting bit position (0-based)
* @param {number} bitCount - Number of bits to clear
*/
Expand All @@ -106,7 +106,7 @@ export class BitUtils {

/**
* Toggle specific bits in a buffer
* @param {Buffer} buffer - Target buffer
* @param {Uint8Array} buffer - Target buffer
* @param {number} startBit - Starting bit position (0-based)
* @param {number} bitCount - Number of bits to toggle
*/
Expand All @@ -123,11 +123,11 @@ export class BitUtils {
* Create a bit mask for specific bit positions
* @param {number} totalBits - Total number of bits in the mask
* @param {Array<number>} bitPositions - Array of bit positions to set
* @returns {Buffer} Buffer containing the bit mask
* @returns {Uint8Array} Buffer containing the bit mask
*/
static createBitMask(totalBits, bitPositions) {
const byteCount = Math.ceil(totalBits / 8);
const mask = Buffer.alloc(byteCount, 0);
const mask = new Uint8Array(byteCount);

for (const bitPos of bitPositions) {
if (bitPos < 0 || bitPos >= totalBits) {
Expand All @@ -144,8 +144,8 @@ export class BitUtils {

/**
* Apply a bit mask to a buffer (AND operation)
* @param {Buffer} buffer - Target buffer to modify
* @param {Buffer} mask - Bit mask to apply
* @param {Uint8Array} buffer - Target buffer to modify
* @param {Uint8Array} mask - Bit mask to apply
*/
static applyMask(buffer, mask) {
if (buffer.length !== mask.length) {
Expand All @@ -159,8 +159,8 @@ export class BitUtils {

/**
* Apply an inverted bit mask to a buffer (clear masked bits)
* @param {Buffer} buffer - Target buffer to modify
* @param {Buffer} mask - Bit mask to invert and apply
* @param {Uint8Array} buffer - Target buffer to modify
* @param {Uint8Array} mask - Bit mask to invert and apply
*/
static applyInvertedMask(buffer, mask) {
if (buffer.length !== mask.length) {
Expand All @@ -174,7 +174,7 @@ export class BitUtils {

/**
* Count the number of set bits in a buffer
* @param {Buffer} buffer - Buffer to count bits in
* @param {Uint8Array} buffer - Buffer to count bits in
* @returns {number} Number of set bits
*/
static countSetBits(buffer) {
Expand All @@ -191,7 +191,7 @@ export class BitUtils {

/**
* Convert a buffer to a binary string representation
* @param {Buffer} buffer - Buffer to convert
* @param {Uint8Array} buffer - Buffer to convert
* @param {boolean} includeSeparators - Whether to include byte separators
* @returns {string} Binary string representation
*/
Expand All @@ -207,7 +207,7 @@ export class BitUtils {
* Convert a binary string to a buffer
* @param {string} binaryString - Binary string (without separators)
* @param {number} byteCount - Expected number of bytes
* @returns {Buffer} Resulting buffer
* @returns {Uint8Array} Resulting buffer
*/
static fromBinaryString(binaryString, byteCount) {
const cleanBinary = binaryString.replace(/\s/g, ""); // Remove any spaces
Expand All @@ -216,7 +216,7 @@ export class BitUtils {
throw new Error(`Binary string length ${cleanBinary.length} doesn't match expected ${byteCount * 8} bits`);
}

const buffer = Buffer.alloc(byteCount);
const buffer = new Uint8Array(byteCount);
for (let i = 0; i < byteCount; i++) {
const byteString = cleanBinary.slice(i * 8, (i + 1) * 8);
buffer[i] = parseInt(byteString, 2);
Expand All @@ -227,7 +227,7 @@ export class BitUtils {

/**
* Validate that a bit position is within valid range for a buffer
* @param {Buffer} buffer - Buffer to validate against
* @param {Uint8Array} buffer - Buffer to validate against
* @param {number} bitPosition - Bit position to validate
* @throws {Error} If bit position is invalid
*/
Expand All @@ -240,7 +240,7 @@ export class BitUtils {

/**
* Get a human-readable representation of specific bit fields
* @param {Buffer} buffer - Buffer to analyze
* @param {Uint8Array} buffer - Buffer to analyze
* @param {Object} fieldDefinitions - Object mapping field names to {start, length} definitions
* @returns {Object} Object with field names as keys and their values
*/
Expand Down
68 changes: 68 additions & 0 deletions src/lib/bytes-browser.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* @Project: @cldmv/slothlet
* @Filename: /src/lib/bytes-browser.mjs
* @Date: 2026-08-17T00:00:00-08:00 (0)
* @Author: Nate Corcoran <CLDMV>
* @Email: <Shinrai@users.noreply.github.com>
* -----
* @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
* @Last modified time: 2026-08-17T00:00:00-08:00 (0)
* -----
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
*/

/**
* Browser hex/byte helpers (selected via the "browser" export condition on ./bytes).
* Pure Uint8Array implementation, no Node Buffer.
*
* Note: unlike Node's Buffer.from(hex, "hex") (which silently stops at the first
* invalid hex character), fromHex() here throws on malformed input. This is a
* deliberate, safety-improving divergence for the browser path only — it never
* changes Node's existing behavior.
*/

const HEX_CHARS = "0123456789abcdef";

/**
* Parse a hex string into bytes.
* @param {string} hex - Hex string (no separators)
* @returns {Uint8Array} Parsed bytes
*/
export function fromHex(hex) {
if (hex.length % 2 !== 0) {
throw new TypeError("Invalid hex string length");
}

const out = new Uint8Array(hex.length / 2);
for (let i = 0; i < out.length; i++) {
const byte = parseInt(hex.substr(i * 2, 2), 16);
if (Number.isNaN(byte)) {
throw new TypeError("Invalid hex string");
}
out[i] = byte;
}
return out;
}

/**
* Encode bytes as a lowercase hex string.
* @param {Uint8Array} bytes - Bytes to encode
* @returns {string} Hex string
*/
export function toHex(bytes) {
let hex = "";
for (let i = 0; i < bytes.length; i++) {
hex += HEX_CHARS[(bytes[i] >> 4) & 0xf] + HEX_CHARS[bytes[i] & 0xf];
}
return hex;
}

/**
* Return a copy of `bytes` as the runtime's native Buffer-like type.
* There is no Buffer in the browser, so this is a plain Uint8Array copy.
* @param {Uint8Array} bytes - Bytes to wrap
* @returns {Uint8Array} Copy of the bytes
*/
export function toBufferLike(bytes) {
return Uint8Array.from(bytes);
}
46 changes: 46 additions & 0 deletions src/lib/bytes.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @Project: @cldmv/slothlet
* @Filename: /src/lib/bytes.mjs
* @Date: 2026-08-17T00:00:00-08:00 (0)
* @Author: Nate Corcoran <CLDMV>
* @Email: <Shinrai@users.noreply.github.com>
* -----
* @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
* @Last modified time: 2026-08-17T00:00:00-08:00 (0)
* -----
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
*/

/**
* Node hex/byte helpers (selected via the "browser" export condition on ./bytes).
* Backed by Node's native Buffer for speed; accepts either a Buffer or a plain
* Uint8Array as input since internal UUID buffers are plain Uint8Array.
*/

/**
* Parse a hex string into bytes.
* @param {string} hex - Hex string (no separators)
* @returns {Uint8Array} Parsed bytes
*/
export function fromHex(hex) {
return Buffer.from(hex, "hex");
}

/**
* Encode bytes as a lowercase hex string.
* @param {Uint8Array} bytes - Bytes to encode
* @returns {string} Hex string
*/
export function toHex(bytes) {
return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString("hex");
}

/**
* Return a copy of `bytes` as the runtime's native Buffer-like type.
* In Node this is a real Buffer (preserves the pre-existing toBuffer() contract).
* @param {Uint8Array} bytes - Bytes to wrap
* @returns {Buffer} Copy of the bytes as a Buffer
*/
export function toBufferLike(bytes) {
return Buffer.from(bytes);
}
Loading
Loading