@hkdigital/lib-core 0.4.55 → 0.4.57
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/network/README.md +82 -1
- package/dist/network/http/json-request.d.ts +118 -0
- package/dist/network/http/json-request.js +321 -1
- package/dist/network/http/typedef.d.ts +146 -0
- package/dist/network/http/typedef.js +56 -0
- package/dist/services/README.md +57 -6
- package/dist/util/checksum/README.md +267 -0
- package/dist/util/checksum/blockCrc32.d.ts +44 -0
- package/dist/util/checksum/blockCrc32.js +174 -0
- package/dist/util/checksum/bufferCrc32.d.ts +11 -0
- package/dist/util/checksum/bufferCrc32.js +69 -0
- package/dist/util/checksum/crcTables.d.ts +31 -0
- package/dist/util/checksum/crcTables.js +79 -0
- package/dist/util/checksum/stringCrc32.d.ts +12 -0
- package/dist/util/checksum/stringCrc32.js +33 -0
- package/dist/util/checksum/typedef.d.ts +10 -0
- package/dist/util/checksum/typedef.js +11 -0
- package/dist/util/checksum.d.ts +4 -0
- package/dist/util/checksum.js +23 -0
- package/package.json +1 -1
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Standard buffer CRC32 calculation
|
|
3
|
+
*
|
|
4
|
+
* Implements IEEE 802.3 CRC32 over entire buffer.
|
|
5
|
+
* Compatible with ZIP, PNG, and other standard formats.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { getCrcTable, CRC32 } from './crcTables.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Calculate standard CRC32 for buffer
|
|
12
|
+
*
|
|
13
|
+
* @param {ArrayBuffer|DataView|Uint8Array} buffer - Buffer to checksum
|
|
14
|
+
* @param {number} [byteOffset=0] - Start offset in buffer
|
|
15
|
+
* @param {number} [byteLength] - Number of bytes to process
|
|
16
|
+
* @param {import('./crcTables.js').CrcVariant} [variant=CRC32] - CRC variant
|
|
17
|
+
*
|
|
18
|
+
* @returns {number} Unsigned 32-bit CRC32 value
|
|
19
|
+
*/
|
|
20
|
+
export function bufferToCrc32(buffer, byteOffset = 0, byteLength, variant = CRC32) {
|
|
21
|
+
const table = getCrcTable(variant);
|
|
22
|
+
|
|
23
|
+
/** @type {DataView} */
|
|
24
|
+
const dataView = createDataView(buffer, byteOffset, byteLength);
|
|
25
|
+
|
|
26
|
+
/** @type {number} */
|
|
27
|
+
let crc = 0 ^ (-1);
|
|
28
|
+
|
|
29
|
+
for (let j = 0; j < dataView.byteLength; j = j + 1) {
|
|
30
|
+
crc = (crc >>> 8) ^ table[(crc ^ dataView.getUint8(j)) & 0xFF];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return (crc ^ (-1)) >>> 0;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/* ------------------------------------------------------- Internal functions */
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Create DataView from various buffer types
|
|
40
|
+
*
|
|
41
|
+
* @param {ArrayBuffer|DataView|Uint8Array} buffer - Input buffer
|
|
42
|
+
* @param {number} [byteOffset=0] - Start offset
|
|
43
|
+
* @param {number} [byteLength] - Length to use
|
|
44
|
+
*
|
|
45
|
+
* @returns {DataView} DataView for the buffer
|
|
46
|
+
*/
|
|
47
|
+
function createDataView(buffer, byteOffset = 0, byteLength) {
|
|
48
|
+
if (buffer instanceof DataView) {
|
|
49
|
+
return new DataView(
|
|
50
|
+
buffer.buffer,
|
|
51
|
+
buffer.byteOffset + byteOffset,
|
|
52
|
+
byteLength ?? (buffer.byteLength - byteOffset)
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (buffer instanceof ArrayBuffer) {
|
|
57
|
+
return new DataView(buffer, byteOffset, byteLength);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (buffer instanceof Uint8Array) {
|
|
61
|
+
return new DataView(
|
|
62
|
+
buffer.buffer,
|
|
63
|
+
buffer.byteOffset + byteOffset,
|
|
64
|
+
byteLength ?? (buffer.byteLength - byteOffset)
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
throw new Error('Unsupported buffer type');
|
|
69
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get CRC lookup table for specified variant
|
|
3
|
+
*
|
|
4
|
+
* @param {CrcVariant} [variant=CRC32] - CRC variant
|
|
5
|
+
*
|
|
6
|
+
* @returns {CrcTable} The CRC lookup table
|
|
7
|
+
*/
|
|
8
|
+
export function getCrcTable(variant?: CrcVariant): CrcTable;
|
|
9
|
+
/**
|
|
10
|
+
* Shared CRC table generation and constants
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {import('./typedef').CrcVariant} CrcVariant
|
|
14
|
+
* Valid CRC32 variant types
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* @typedef {import('./typedef').CrcTable} CrcTable
|
|
18
|
+
* Lookup table for CRC calculations - array of 256 numbers
|
|
19
|
+
*/
|
|
20
|
+
/** @type {CrcVariant} */
|
|
21
|
+
export const CRC32: CrcVariant;
|
|
22
|
+
/** @type {CrcVariant} */
|
|
23
|
+
export const CRC32C: CrcVariant;
|
|
24
|
+
/**
|
|
25
|
+
* Valid CRC32 variant types
|
|
26
|
+
*/
|
|
27
|
+
export type CrcVariant = import("./typedef").CrcVariant;
|
|
28
|
+
/**
|
|
29
|
+
* Lookup table for CRC calculations - array of 256 numbers
|
|
30
|
+
*/
|
|
31
|
+
export type CrcTable = import("./typedef").CrcTable;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared CRC table generation and constants
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {import('./typedef').CrcVariant} CrcVariant
|
|
7
|
+
* Valid CRC32 variant types
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {import('./typedef').CrcTable} CrcTable
|
|
12
|
+
* Lookup table for CRC calculations - array of 256 numbers
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/* ---------------------------------------------------------------- Constants */
|
|
16
|
+
|
|
17
|
+
/** @type {CrcVariant} */
|
|
18
|
+
export const CRC32 = 'crc32';
|
|
19
|
+
|
|
20
|
+
/** @type {CrcVariant} */
|
|
21
|
+
export const CRC32C = 'crc32c';
|
|
22
|
+
|
|
23
|
+
/** @type {Record<CrcVariant, number>} */
|
|
24
|
+
const START_POLYNOMIALS = {
|
|
25
|
+
[CRC32]: 0xEDB88320,
|
|
26
|
+
[CRC32C]: 0x82f63b78
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** @type {Record<CrcVariant, CrcTable>} */
|
|
30
|
+
// @ts-ignore
|
|
31
|
+
const crcTables = {};
|
|
32
|
+
|
|
33
|
+
/* ---------------------------------------------------------------- Functions */
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Get CRC lookup table for specified variant
|
|
37
|
+
*
|
|
38
|
+
* @param {CrcVariant} [variant=CRC32] - CRC variant
|
|
39
|
+
*
|
|
40
|
+
* @returns {CrcTable} The CRC lookup table
|
|
41
|
+
*/
|
|
42
|
+
export function getCrcTable(variant = CRC32) {
|
|
43
|
+
return crcTables[variant] || (crcTables[variant] = makeCRCTable(variant));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/* ---------------------------------------------------------------- Internals */
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Create CRC lookup table for specified variant
|
|
50
|
+
*
|
|
51
|
+
* @param {CrcVariant} variant - CRC variant to create table for
|
|
52
|
+
*
|
|
53
|
+
* @returns {CrcTable} The generated CRC lookup table
|
|
54
|
+
*/
|
|
55
|
+
function makeCRCTable(variant) {
|
|
56
|
+
/** @type {number} */
|
|
57
|
+
let c;
|
|
58
|
+
|
|
59
|
+
/** @type {CrcTable} */
|
|
60
|
+
const crcTable = [];
|
|
61
|
+
|
|
62
|
+
/** @type {number} */
|
|
63
|
+
const startPolynomial = START_POLYNOMIALS[variant];
|
|
64
|
+
|
|
65
|
+
if (!startPolynomial) {
|
|
66
|
+
throw new Error(`Invalid variant [${variant}]`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
for (let n = 0; n < 256; n = n + 1) {
|
|
70
|
+
c = n;
|
|
71
|
+
|
|
72
|
+
for (let k = 0; k < 8; k = k + 1) {
|
|
73
|
+
c = ((c & 1) ? (startPolynomial ^ (c >>> 1)) : (c >>> 1));
|
|
74
|
+
}
|
|
75
|
+
crcTable[n] = c;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return crcTable;
|
|
79
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculate CRC32 hash for string input
|
|
3
|
+
*
|
|
4
|
+
* @param {string} str - String to calculate checksum for
|
|
5
|
+
* @param {import('./crcTables.js').CrcVariant} [variant=CRC32] - CRC variant
|
|
6
|
+
*
|
|
7
|
+
* @returns {number} Unsigned 32-bit CRC32 value
|
|
8
|
+
*/
|
|
9
|
+
export function stringToCrc32(str: string, variant?: import("./crcTables.js").CrcVariant): number;
|
|
10
|
+
import { CRC32 } from './crcTables.js';
|
|
11
|
+
import { CRC32C } from './crcTables.js';
|
|
12
|
+
export { CRC32, CRC32C };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* String CRC32 calculation utility
|
|
3
|
+
*
|
|
4
|
+
* Calculates CRC32 checksums for strings using standard IEEE 802.3 algorithm.
|
|
5
|
+
* CRC32 is fast but not cryptographically secure.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { getCrcTable, CRC32, CRC32C } from './crcTables.js';
|
|
9
|
+
|
|
10
|
+
/* ------------------------------------------------------------------ Exports */
|
|
11
|
+
|
|
12
|
+
export { CRC32, CRC32C };
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Calculate CRC32 hash for string input
|
|
16
|
+
*
|
|
17
|
+
* @param {string} str - String to calculate checksum for
|
|
18
|
+
* @param {import('./crcTables.js').CrcVariant} [variant=CRC32] - CRC variant
|
|
19
|
+
*
|
|
20
|
+
* @returns {number} Unsigned 32-bit CRC32 value
|
|
21
|
+
*/
|
|
22
|
+
export function stringToCrc32(str, variant = CRC32) {
|
|
23
|
+
const table = getCrcTable(variant);
|
|
24
|
+
|
|
25
|
+
/** @type {number} */
|
|
26
|
+
let crc = 0 ^ (-1);
|
|
27
|
+
|
|
28
|
+
for (let j = 0; j < str.length; j = j + 1) {
|
|
29
|
+
crc = (crc >>> 8) ^ table[(crc ^ str.charCodeAt(j)) & 0xFF];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return (crc ^ (-1)) >>> 0;
|
|
33
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { bufferToCrc32 } from "./checksum/bufferCrc32.js";
|
|
2
|
+
export { getCrcTable } from "./checksum/crcTables.js";
|
|
3
|
+
export { stringToCrc32, CRC32, CRC32C } from "./checksum/stringCrc32.js";
|
|
4
|
+
export { bufferToBlockCrc32, blockCrc32Iterator, bufferChecksumEquals, optimalBlockSize } from "./checksum/blockCrc32.js";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checksum utilities - CRC32 calculations
|
|
3
|
+
*
|
|
4
|
+
* Exports the main public-facing functions from the checksum module.
|
|
5
|
+
* Provides both standard IEEE 802.3 CRC32 and custom block-based processing.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// String CRC32 calculations
|
|
9
|
+
export { stringToCrc32, CRC32, CRC32C } from './checksum/stringCrc32.js';
|
|
10
|
+
|
|
11
|
+
// Standard buffer CRC32 (IEEE 802.3 compatible)
|
|
12
|
+
export { bufferToCrc32 } from './checksum/bufferCrc32.js';
|
|
13
|
+
|
|
14
|
+
// Custom block-based CRC32 for large files
|
|
15
|
+
export {
|
|
16
|
+
bufferToBlockCrc32,
|
|
17
|
+
blockCrc32Iterator,
|
|
18
|
+
bufferChecksumEquals,
|
|
19
|
+
optimalBlockSize
|
|
20
|
+
} from './checksum/blockCrc32.js';
|
|
21
|
+
|
|
22
|
+
// CRC table utilities (for advanced use)
|
|
23
|
+
export { getCrcTable } from './checksum/crcTables.js';
|