chore: update to version 14.0.11 and align website URLs

This commit is contained in:
2026-01-11 11:34:13 +03:00
parent ef74383349
commit 19c8d69bd8
1499 changed files with 53633 additions and 89 deletions
+13
View File
@@ -0,0 +1,13 @@
/**
* @name compactAddLength
* @description Adds a length prefix to the input value
* @example
* <BR>
*
* ```javascript
* import { compactAddLength } from '@pezkuwi/util';
*
* console.log(compactAddLength(new Uint8Array([0xde, 0xad, 0xbe, 0xef]))); // Uint8Array([4 << 2, 0xde, 0xad, 0xbe, 0xef])
* ```
*/
export declare function compactAddLength(input: Uint8Array): Uint8Array;
+20
View File
@@ -0,0 +1,20 @@
import { u8aConcatStrict } from '../u8a/index.js';
import { compactToU8a } from './toU8a.js';
/**
* @name compactAddLength
* @description Adds a length prefix to the input value
* @example
* <BR>
*
* ```javascript
* import { compactAddLength } from '@pezkuwi/util';
*
* console.log(compactAddLength(new Uint8Array([0xde, 0xad, 0xbe, 0xef]))); // Uint8Array([4 << 2, 0xde, 0xad, 0xbe, 0xef])
* ```
*/
export function compactAddLength(input) {
return u8aConcatStrict([
compactToU8a(input.length),
input
]);
}
+3
View File
@@ -0,0 +1,3 @@
import type { BitLength } from './types.js';
/** @internal */
export declare const DEFAULT_BITLENGTH: BitLength;
+2
View File
@@ -0,0 +1,2 @@
/** @internal */
export const DEFAULT_BITLENGTH = 32;
+22
View File
@@ -0,0 +1,22 @@
import type { U8aLike } from '../types.js';
import { BN } from '../bn/index.js';
/**
* @name compactFromU8a
* @description Retrives the offset and encoded length from a compact-prefixed value
* @example
* <BR>
*
* ```javascript
* import { compactFromU8a } from '@pezkuwi/util';
*
* const [offset, length] = compactFromU8a(new Uint8Array([254, 255, 3, 0]));
*
* console.log('value offset=', offset, 'length=', length); // 4, 0xffff
* ```
*/
export declare function compactFromU8a(input: U8aLike): [number, BN];
/**
* @name compactFromU8aLim
* @description A limited version of [[compactFromU8a]], accepting only Uint8Array inputs for values <= 48 bits
*/
export declare function compactFromU8aLim(u8a: Uint8Array): [number, number];
+88
View File
@@ -0,0 +1,88 @@
import { BN } from '../bn/index.js';
import { u8aToBn, u8aToU8a } from '../u8a/index.js';
/**
* @name compactFromU8a
* @description Retrives the offset and encoded length from a compact-prefixed value
* @example
* <BR>
*
* ```javascript
* import { compactFromU8a } from '@pezkuwi/util';
*
* const [offset, length] = compactFromU8a(new Uint8Array([254, 255, 3, 0]));
*
* console.log('value offset=', offset, 'length=', length); // 4, 0xffff
* ```
*/
export function compactFromU8a(input) {
const u8a = u8aToU8a(input);
// The u8a is manually converted here for 1, 2 & 4 lengths, it is 2x faster
// than doing an additional call to u8aToBn (as with variable length)
switch (u8a[0] & 0b11) {
case 0b00:
return [1, new BN(u8a[0] >>> 2)];
case 0b01:
return [2, new BN((u8a[0] + (u8a[1] << 8)) >>> 2)];
case 0b10:
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
return [4, new BN((u8a[0] + (u8a[1] << 8) + (u8a[2] << 16) + (u8a[3] * 0x1_00_00_00)) >>> 2)];
// 0b11
default: {
// add 5 to shifted (4 for base length, 1 for this byte)
const offset = (u8a[0] >>> 2) + 5;
// we unroll the loop
switch (offset) {
// there still could be 4 bytes data, similar to 0b10 above (with offsets)
case 5:
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
return [5, new BN(u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + (u8a[4] * 0x1_00_00_00))];
case 6:
return [6, new BN(u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + ((u8a[4] + (u8a[5] << 8)) * 0x1_00_00_00))];
// 6 bytes data is the maximum, 48 bits (56 would overflow)
case 7:
return [7, new BN(u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + ((u8a[4] + (u8a[5] << 8) + (u8a[6] << 16)) * 0x1_00_00_00))];
// for anything else, use the non-unrolled version
default:
return [offset, u8aToBn(u8a.subarray(1, offset))];
}
}
}
}
/**
* @name compactFromU8aLim
* @description A limited version of [[compactFromU8a]], accepting only Uint8Array inputs for values <= 48 bits
*/
export function compactFromU8aLim(u8a) {
// The u8a is manually converted here for 1, 2 & 4 lengths, it is 2x faster
// than doing an additional call to u8aToBn (as with variable length)
switch (u8a[0] & 0b11) {
case 0b00:
return [1, u8a[0] >>> 2];
case 0b01:
return [2, (u8a[0] + (u8a[1] << 8)) >>> 2];
case 0b10:
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
return [4, (u8a[0] + (u8a[1] << 8) + (u8a[2] << 16) + (u8a[3] * 0x1_00_00_00)) >>> 2];
// 0b11
default: {
// add 5 to shifted (4 for base length, 1 for this byte)
// we unroll the loop
switch ((u8a[0] >>> 2) + 5) {
// there still could be 4 bytes data, similar to 0b10 above (with offsets)
case 5:
return [5, u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + (u8a[4] * 0x1_00_00_00)];
case 6:
return [6, u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + ((u8a[4] + (u8a[5] << 8)) * 0x1_00_00_00)];
// 6 bytes data is the maximum, 48 bits (56 would overflow)
case 7:
return [7, u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + ((u8a[4] + (u8a[5] << 8) + (u8a[6] << 16)) * 0x1_00_00_00)];
// for anything else, we are above the actual MAX_SAFE_INTEGER - bail out
default:
throw new Error('Compact input is > Number.MAX_SAFE_INTEGER');
}
}
}
}
+24
View File
@@ -0,0 +1,24 @@
/**
* @description
* Encoding and decoding of parity-codec compact numbers. The codec is created
* to take up the least amount of space for a specific number. It performs the
* same function as Length, however differs in that it uses a variable number of
* bytes to do the actual encoding. From the Rust implementation for compact
* encoding:
*
* 0b00 00 00 00 / 00 00 00 00 / 00 00 00 00 / 00 00 00 00
* (0 ... 2**6 - 1) (u8)
* xx xx xx 00
* (2**6 ... 2**14 - 1) (u8, u16) low LH high
* yL yL yL 01 / yH yH yH yL
* (2**14 ... 2**30 - 1) (u16, u32) low LMMH high
* zL zL zL 10 / zM zM zM zL / zM zM zM zM / zH zH zH zM
* (2**30 ... 2**536 - 1) (u32, u64, u128, U256, U512, U520) straight LE-encoded
* nn nn nn 11 [ / zz zz zz zz ]{4 + n}
*
* Note: we use *LOW BITS* of the LSB in LE encoding to encode the 2 bit key.
*/
export { compactAddLength } from './addLength.js';
export { compactFromU8a, compactFromU8aLim } from './fromU8a.js';
export { compactStripLength } from './stripLength.js';
export { compactToU8a } from './toU8a.js';
+24
View File
@@ -0,0 +1,24 @@
/**
* @description
* Encoding and decoding of parity-codec compact numbers. The codec is created
* to take up the least amount of space for a specific number. It performs the
* same function as Length, however differs in that it uses a variable number of
* bytes to do the actual encoding. From the Rust implementation for compact
* encoding:
*
* 0b00 00 00 00 / 00 00 00 00 / 00 00 00 00 / 00 00 00 00
* (0 ... 2**6 - 1) (u8)
* xx xx xx 00
* (2**6 ... 2**14 - 1) (u8, u16) low LH high
* yL yL yL 01 / yH yH yH yL
* (2**14 ... 2**30 - 1) (u16, u32) low LMMH high
* zL zL zL 10 / zM zM zM zL / zM zM zM zM / zH zH zH zM
* (2**30 ... 2**536 - 1) (u32, u64, u128, U256, U512, U520) straight LE-encoded
* nn nn nn 11 [ / zz zz zz zz ]{4 + n}
*
* Note: we use *LOW BITS* of the LSB in LE encoding to encode the 2 bit key.
*/
export { compactAddLength } from './addLength.js';
export { compactFromU8a, compactFromU8aLim } from './fromU8a.js';
export { compactStripLength } from './stripLength.js';
export { compactToU8a } from './toU8a.js';
+13
View File
@@ -0,0 +1,13 @@
/**
* @name compactStripLength
* @description Removes the length prefix, returning both the total length (including the value + compact encoding) and the decoded value with the correct length
* @example
* <BR>
*
* ```javascript
* import { compactStripLength } from '@pezkuwi/util';
*
* console.log(compactStripLength(new Uint8Array([2 << 2, 0xde, 0xad]))); // [2, Uint8Array[0xde, 0xad]]
* ```
*/
export declare function compactStripLength(input: Uint8Array): [number, Uint8Array];
+21
View File
@@ -0,0 +1,21 @@
import { compactFromU8a } from './fromU8a.js';
/**
* @name compactStripLength
* @description Removes the length prefix, returning both the total length (including the value + compact encoding) and the decoded value with the correct length
* @example
* <BR>
*
* ```javascript
* import { compactStripLength } from '@pezkuwi/util';
*
* console.log(compactStripLength(new Uint8Array([2 << 2, 0xde, 0xad]))); // [2, Uint8Array[0xde, 0xad]]
* ```
*/
export function compactStripLength(input) {
const [offset, length] = compactFromU8a(input);
const total = offset + length.toNumber();
return [
total,
input.subarray(offset, total)
];
}
+14
View File
@@ -0,0 +1,14 @@
import { BN } from '../bn/index.js';
/**
* @name compactToU8a
* @description Encodes a number into a compact representation
* @example
* <BR>
*
* ```javascript
* import { compactToU8a } from '@pezkuwi/util';
*
* console.log(compactToU8a(511, 32)); // Uint8Array([0b11111101, 0b00000111])
* ```
*/
export declare function compactToU8a(value: BN | bigint | number): Uint8Array;
+45
View File
@@ -0,0 +1,45 @@
import { BN, BN_ONE, BN_TWO, bnToBn, bnToU8a } from '../bn/index.js';
import { u8aConcatStrict } from '../u8a/index.js';
const MAX_U8 = BN_TWO.pow(new BN(8 - 2)).isub(BN_ONE);
const MAX_U16 = BN_TWO.pow(new BN(16 - 2)).isub(BN_ONE);
const MAX_U32 = BN_TWO.pow(new BN(32 - 2)).isub(BN_ONE);
const BL_16 = { bitLength: 16 };
const BL_32 = { bitLength: 32 };
/**
* @name compactToU8a
* @description Encodes a number into a compact representation
* @example
* <BR>
*
* ```javascript
* import { compactToU8a } from '@pezkuwi/util';
*
* console.log(compactToU8a(511, 32)); // Uint8Array([0b11111101, 0b00000111])
* ```
*/
export function compactToU8a(value) {
const bn = bnToBn(value);
if (bn.lte(MAX_U8)) {
return new Uint8Array([bn.toNumber() << 2]);
}
else if (bn.lte(MAX_U16)) {
return bnToU8a(bn.shln(2).iadd(BN_ONE), BL_16);
}
else if (bn.lte(MAX_U32)) {
return bnToU8a(bn.shln(2).iadd(BN_TWO), BL_32);
}
const u8a = bnToU8a(bn);
let length = u8a.length;
// adjust to the minimum number of bytes
while (u8a[length - 1] === 0) {
length--;
}
if (length < 4) {
throw new Error('Invalid length, previous checks match anything less than 2^30');
}
return u8aConcatStrict([
// subtract 4 as minimum (also catered for in decoding)
new Uint8Array([((length - 4) << 2) + 0b11]),
u8a.subarray(0, length)
]);
}
+1
View File
@@ -0,0 +1 @@
export type BitLength = 8 | 16 | 32 | 64 | 128 | 256;
+1
View File
@@ -0,0 +1 @@
export {};