@hkdigital/lib-core 0.4.56 → 0.4.58
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/auth/jwt/README.md +275 -0
- package/dist/auth/jwt/util.d.ts +16 -0
- package/dist/auth/jwt/util.js +48 -0
- package/dist/auth/jwt.js +1 -1
- package/dist/generic/data/typedef.d.ts +1 -0
- package/dist/generic/data/typedef.js +2 -0
- package/dist/generic/data/util/README.md +252 -0
- package/dist/generic/data/util/flat-tree.d.ts +18 -0
- package/dist/generic/data/util/flat-tree.js +261 -0
- package/dist/generic/data/util/typedef.d.ts +21 -0
- package/dist/generic/data/util/typedef.js +14 -0
- package/dist/generic/data.d.ts +1 -0
- package/dist/generic/data.js +1 -0
- 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,261 @@
|
|
|
1
|
+
/* ------------------------------------------------------------------ Imports */
|
|
2
|
+
|
|
3
|
+
import * as expect from '../../../util/expect.js';
|
|
4
|
+
|
|
5
|
+
/* ------------------------------------------------------------------ Exports */
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Build a hierarchical tree from ft1 flat tree format
|
|
9
|
+
*
|
|
10
|
+
* @template {object} T
|
|
11
|
+
* @param {import('./typedef.js').FlatTree<T>} flatTree - Flat tree data
|
|
12
|
+
*
|
|
13
|
+
* @returns {T|null} reconstructed hierarchical tree or null
|
|
14
|
+
*/
|
|
15
|
+
export function buildTree(flatTree) {
|
|
16
|
+
expect.object(flatTree);
|
|
17
|
+
|
|
18
|
+
const { format, properties, nodes, edges } = flatTree;
|
|
19
|
+
|
|
20
|
+
if (format !== 'ft1') {
|
|
21
|
+
throw new Error(`Unsupported format: ${format}. Expected 'ft1'`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (!nodes || !nodes.length) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (!edges || !edges.length) {
|
|
29
|
+
return { ...nodes[0] };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (edges.length % 3 !== 0) {
|
|
33
|
+
throw new Error('Invalid edges array: length must be multiple of 3');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Create copies of all nodes to avoid mutating original data
|
|
37
|
+
const nodesCopy = nodes.map(node => ({ ...node }));
|
|
38
|
+
|
|
39
|
+
// Process edges in groups of 3: [from, prop, to]
|
|
40
|
+
for (let i = 0; i < edges.length; i += 3) {
|
|
41
|
+
const fromIndex = edges[i];
|
|
42
|
+
const propIndex = edges[i + 1];
|
|
43
|
+
const toIndex = edges[i + 2];
|
|
44
|
+
|
|
45
|
+
// Validate indices
|
|
46
|
+
if (fromIndex >= nodesCopy.length || toIndex >= nodesCopy.length) {
|
|
47
|
+
throw new Error(`Invalid node index in edge [${fromIndex}, ${propIndex}, ${toIndex}]`);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (propIndex >= properties.length) {
|
|
51
|
+
throw new Error(`Invalid property index: ${propIndex}`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const fromNode = nodesCopy[fromIndex];
|
|
55
|
+
const toNode = nodesCopy[toIndex];
|
|
56
|
+
const propertyName = properties[propIndex];
|
|
57
|
+
|
|
58
|
+
// Add child to parent's property
|
|
59
|
+
/** @type {any} */
|
|
60
|
+
const dynamicFromNode = fromNode;
|
|
61
|
+
|
|
62
|
+
if (!dynamicFromNode[propertyName]) {
|
|
63
|
+
dynamicFromNode[propertyName] = [toNode];
|
|
64
|
+
} else if (Array.isArray(dynamicFromNode[propertyName])) {
|
|
65
|
+
// Check if this node is already in the array (shared reference)
|
|
66
|
+
if (!dynamicFromNode[propertyName].includes(toNode)) {
|
|
67
|
+
dynamicFromNode[propertyName].push(toNode);
|
|
68
|
+
}
|
|
69
|
+
} else {
|
|
70
|
+
// Convert single child to array
|
|
71
|
+
dynamicFromNode[propertyName] = [dynamicFromNode[propertyName], toNode];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return nodesCopy[0];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Flatten a hierarchical tree into ft1 flat tree format
|
|
80
|
+
*
|
|
81
|
+
* @template {object} T
|
|
82
|
+
* @param {T} hierarchicalTree - Hierarchical tree with nested children
|
|
83
|
+
*
|
|
84
|
+
* @returns {import('./typedef.js').FlatTree<T>} flat tree data
|
|
85
|
+
*/
|
|
86
|
+
export function flattenTree(hierarchicalTree) {
|
|
87
|
+
expect.object(hierarchicalTree);
|
|
88
|
+
|
|
89
|
+
/** @type {T[]} */
|
|
90
|
+
const nodes = [];
|
|
91
|
+
|
|
92
|
+
/** @type {number[]} */
|
|
93
|
+
const edges = [];
|
|
94
|
+
|
|
95
|
+
/** @type {Set<string>} */
|
|
96
|
+
const propertySet = new Set();
|
|
97
|
+
|
|
98
|
+
/** @type {WeakMap<object, number>} */
|
|
99
|
+
const objectToIndex = new WeakMap();
|
|
100
|
+
|
|
101
|
+
// First pass: collect all child-containing properties
|
|
102
|
+
const childProperties = new Set();
|
|
103
|
+
findChildProperties(hierarchicalTree, childProperties);
|
|
104
|
+
|
|
105
|
+
// Add root node (always index 0)
|
|
106
|
+
const rootCopy = extractNodeData(hierarchicalTree, childProperties);
|
|
107
|
+
nodes.push(rootCopy);
|
|
108
|
+
objectToIndex.set(hierarchicalTree, 0);
|
|
109
|
+
|
|
110
|
+
// Process children recursively
|
|
111
|
+
processChildrenFt1(
|
|
112
|
+
hierarchicalTree,
|
|
113
|
+
0,
|
|
114
|
+
nodes,
|
|
115
|
+
edges,
|
|
116
|
+
propertySet,
|
|
117
|
+
objectToIndex,
|
|
118
|
+
childProperties
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
// Convert property set to sorted array for consistent output
|
|
122
|
+
const properties = Array.from(propertySet).sort();
|
|
123
|
+
|
|
124
|
+
// Convert property names to indices in edges array
|
|
125
|
+
for (let i = 1; i < edges.length; i += 3) {
|
|
126
|
+
const propertyName = /** @type {string} */ (edges[i]);
|
|
127
|
+
edges[i] = properties.indexOf(propertyName);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return /** @type {import('./typedef.js').FlatTree<T>} */ ({
|
|
131
|
+
format: 'ft1',
|
|
132
|
+
properties,
|
|
133
|
+
nodes,
|
|
134
|
+
edges
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/* ---------------------------------------------------------- Private methods */
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Extract node data excluding children properties
|
|
142
|
+
*
|
|
143
|
+
* @param {object} node - Source node
|
|
144
|
+
* @param {Set<string>} propertiesToRemove - Set of property names to remove
|
|
145
|
+
*
|
|
146
|
+
* @returns {object} node data without children properties
|
|
147
|
+
*/
|
|
148
|
+
function extractNodeData(node, propertiesToRemove) {
|
|
149
|
+
/** @type {any} */
|
|
150
|
+
const nodeData = { ...node };
|
|
151
|
+
|
|
152
|
+
// Remove all child-containing properties
|
|
153
|
+
for (const key of propertiesToRemove) {
|
|
154
|
+
delete nodeData[key];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return nodeData;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Find all properties that contain child objects
|
|
162
|
+
*
|
|
163
|
+
* @param {object} node - Node to analyze
|
|
164
|
+
* @param {Set<string>} childProperties - Set to collect property names
|
|
165
|
+
*/
|
|
166
|
+
function findChildProperties(node, childProperties) {
|
|
167
|
+
// Find array properties that contain objects (potential children)
|
|
168
|
+
for (const [key, value] of Object.entries(node)) {
|
|
169
|
+
if (Array.isArray(value) && value.length > 0) {
|
|
170
|
+
// Check if array contains objects (potential children)
|
|
171
|
+
const hasObjects = value.some(item => item && typeof item === 'object');
|
|
172
|
+
if (hasObjects) {
|
|
173
|
+
childProperties.add(key);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Recursively check child nodes
|
|
179
|
+
for (const key of [...childProperties]) { // Copy set to avoid modification during iteration
|
|
180
|
+
const children = node[key];
|
|
181
|
+
if (Array.isArray(children)) {
|
|
182
|
+
for (const child of children) {
|
|
183
|
+
if (child && typeof child === 'object') {
|
|
184
|
+
findChildProperties(child, childProperties);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Recursively process children for ft1 format
|
|
193
|
+
*
|
|
194
|
+
* @param {object} parentNode - Parent node with children
|
|
195
|
+
* @param {number} parentIndex - Parent node index
|
|
196
|
+
* @param {object[]} nodes - Nodes array to populate
|
|
197
|
+
* @param {(number|string)[]} edges - Edges array to populate (mixed types temporarily)
|
|
198
|
+
* @param {Set<string>} propertySet - Set of property names
|
|
199
|
+
* @param {WeakMap<object, number>} objectToIndex - Map of objects to indices
|
|
200
|
+
* @param {Set<string>} childProperties - Set of all child-containing properties
|
|
201
|
+
*/
|
|
202
|
+
function processChildrenFt1(parentNode, parentIndex, nodes, edges, propertySet, objectToIndex, childProperties) {
|
|
203
|
+
// Process all child-containing properties
|
|
204
|
+
for (const property of childProperties) {
|
|
205
|
+
const children = parentNode[property];
|
|
206
|
+
|
|
207
|
+
if (!children) {
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (Array.isArray(children)) {
|
|
212
|
+
for (const child of children) {
|
|
213
|
+
if (child && typeof child === 'object') {
|
|
214
|
+
processChildFt1(child, parentIndex, property, nodes, edges, propertySet, objectToIndex, childProperties);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
} else if (children && typeof children === 'object') {
|
|
218
|
+
processChildFt1(children, parentIndex, property, nodes, edges, propertySet, objectToIndex, childProperties);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Process a single child node for ft1 format
|
|
225
|
+
*
|
|
226
|
+
* @param {object} child - Child node
|
|
227
|
+
* @param {number} parentIndex - Parent node index
|
|
228
|
+
* @param {string} property - Property name where this child belongs
|
|
229
|
+
* @param {object[]} nodes - Nodes array to populate
|
|
230
|
+
* @param {(number|string)[]} edges - Edges array to populate (mixed types temporarily)
|
|
231
|
+
* @param {Set<string>} propertySet - Set of property names
|
|
232
|
+
* @param {WeakMap<object, number>} objectToIndex - Map of objects to indices
|
|
233
|
+
* @param {Set<string>} childProperties - Set of all child-containing properties
|
|
234
|
+
*/
|
|
235
|
+
function processChildFt1(child, parentIndex, property, nodes, edges, propertySet, objectToIndex, childProperties) {
|
|
236
|
+
if (!child || typeof child !== 'object') {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Track property name
|
|
241
|
+
propertySet.add(property);
|
|
242
|
+
|
|
243
|
+
// Check if we've seen this object before
|
|
244
|
+
let childIndex = objectToIndex.get(child);
|
|
245
|
+
|
|
246
|
+
if (childIndex === undefined) {
|
|
247
|
+
// First time seeing this object - add it to nodes
|
|
248
|
+
childIndex = nodes.length;
|
|
249
|
+
const childCopy = extractNodeData(child, childProperties);
|
|
250
|
+
nodes.push(childCopy);
|
|
251
|
+
objectToIndex.set(child, childIndex);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Add edge (temporarily store property name as string, will convert to index later)
|
|
255
|
+
edges.push(parentIndex, property, childIndex);
|
|
256
|
+
|
|
257
|
+
// If this is a new object, recursively process its children
|
|
258
|
+
if (objectToIndex.get(child) === childIndex) {
|
|
259
|
+
processChildrenFt1(child, childIndex, nodes, edges, propertySet, objectToIndex, childProperties);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flat tree data structure in ft1 format
|
|
3
|
+
*/
|
|
4
|
+
export type FlatTree<T extends object> = {
|
|
5
|
+
/**
|
|
6
|
+
* - Format version ('ft1')
|
|
7
|
+
*/
|
|
8
|
+
format: string;
|
|
9
|
+
/**
|
|
10
|
+
* - Array of property names used in edges
|
|
11
|
+
*/
|
|
12
|
+
properties: string[];
|
|
13
|
+
/**
|
|
14
|
+
* - Array of node objects (index 0 = root)
|
|
15
|
+
*/
|
|
16
|
+
nodes: T[];
|
|
17
|
+
/**
|
|
18
|
+
* - Flat array: [from, prop, to, from, prop, to, ...]
|
|
19
|
+
*/
|
|
20
|
+
edges: number[];
|
|
21
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/* ------------------------------------------------------------------ Typedef */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Flat tree data structure in ft1 format
|
|
5
|
+
*
|
|
6
|
+
* @template {object} T
|
|
7
|
+
* @typedef {object} FlatTree
|
|
8
|
+
* @property {string} format - Format version ('ft1')
|
|
9
|
+
* @property {string[]} properties - Array of property names used in edges
|
|
10
|
+
* @property {T[]} nodes - Array of node objects (index 0 = root)
|
|
11
|
+
* @property {number[]} edges - Flat array: [from, prop, to, from, prop, to, ...]
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export {};
|
package/dist/generic/data.d.ts
CHANGED
package/dist/generic/data.js
CHANGED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
# CRC32 Checksum Utilities
|
|
2
|
+
|
|
3
|
+
Fast, modular CRC32 implementation with support for strings, buffers, and
|
|
4
|
+
large file processing.
|
|
5
|
+
|
|
6
|
+
## Features
|
|
7
|
+
|
|
8
|
+
- **Standard CRC32**: IEEE 802.3 compatible (ZIP, PNG, Ethernet)
|
|
9
|
+
- **CRC32C variant**: Castagnoli polynomial for improved performance
|
|
10
|
+
- **Buffer support**: ArrayBuffer, DataView, Uint8Array
|
|
11
|
+
- **Block processing**: Custom format for large files
|
|
12
|
+
- **Zero dependencies**: Pure JavaScript implementation
|
|
13
|
+
- **TypeScript ready**: Full JSDoc type annotations
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
import { stringToCrc32 } from './stringCrc32.js';
|
|
19
|
+
import { bufferToCrc32 } from './bufferCrc32.js';
|
|
20
|
+
import { bufferToBlockCrc32 } from './blockCrc32.js';
|
|
21
|
+
import { CRC32, CRC32C } from './crcTables.js';
|
|
22
|
+
|
|
23
|
+
// String checksums (IEEE 802.3 standard)
|
|
24
|
+
const crc = stringToCrc32('hello world');
|
|
25
|
+
console.log(crc); // 222957957 (0xd4a1185)
|
|
26
|
+
|
|
27
|
+
// Standard test vector verification
|
|
28
|
+
console.log(stringToCrc32('123456789')); // 3421780262 (0xcbf43926)
|
|
29
|
+
console.log(stringToCrc32('123456789', CRC32C)); // 3808858755 (0xe3069283)
|
|
30
|
+
|
|
31
|
+
// Buffer checksums (standard)
|
|
32
|
+
const buffer = new TextEncoder().encode('hello world');
|
|
33
|
+
const bufferCrc = bufferToCrc32(buffer);
|
|
34
|
+
console.log(bufferCrc); // 222957957 (same as string)
|
|
35
|
+
|
|
36
|
+
// Large file processing (custom format)
|
|
37
|
+
const result = bufferToBlockCrc32(buffer, 1024);
|
|
38
|
+
console.log(result.checksumList); // [222957957]
|
|
39
|
+
console.log(result.toBase58()); // Base58-encoded result
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Module Overview
|
|
43
|
+
|
|
44
|
+
### `crcTables.js`
|
|
45
|
+
Shared CRC table generation and constants.
|
|
46
|
+
|
|
47
|
+
```javascript
|
|
48
|
+
import { getCrcTable, CRC32, CRC32C } from './crcTables.js';
|
|
49
|
+
|
|
50
|
+
const table = getCrcTable(CRC32); // Get lookup table
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### `stringCrc32.js`
|
|
54
|
+
String to CRC32 conversion.
|
|
55
|
+
|
|
56
|
+
```javascript
|
|
57
|
+
import { stringToCrc32, CRC32, CRC32C } from './stringCrc32.js';
|
|
58
|
+
|
|
59
|
+
const crc = stringToCrc32('text', CRC32); // Standard CRC32
|
|
60
|
+
const crcC = stringToCrc32('text', CRC32C); // CRC32C variant
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### `bufferCrc32.js`
|
|
64
|
+
Standard buffer CRC32 - compatible with external tools.
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
import { bufferToCrc32 } from './bufferCrc32.js';
|
|
68
|
+
|
|
69
|
+
// Works with all buffer types
|
|
70
|
+
const arrayBuffer = new ArrayBuffer(100);
|
|
71
|
+
const uint8Array = new Uint8Array(100);
|
|
72
|
+
const dataView = new DataView(arrayBuffer);
|
|
73
|
+
|
|
74
|
+
const crc1 = bufferToCrc32(arrayBuffer);
|
|
75
|
+
const crc2 = bufferToCrc32(uint8Array, 10, 50); // Offset & length
|
|
76
|
+
const crc3 = bufferToCrc32(dataView);
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### `blockCrc32.js`
|
|
80
|
+
Custom block-based processing for large files.
|
|
81
|
+
|
|
82
|
+
```javascript
|
|
83
|
+
import {
|
|
84
|
+
bufferToBlockCrc32,
|
|
85
|
+
blockCrc32Iterator,
|
|
86
|
+
bufferChecksumEquals,
|
|
87
|
+
optimalBlockSize
|
|
88
|
+
} from './blockCrc32.js';
|
|
89
|
+
|
|
90
|
+
// Process large buffer in blocks
|
|
91
|
+
const result = bufferToBlockCrc32(largeBuffer);
|
|
92
|
+
console.log(result.checksumList); // Array of CRC32 values
|
|
93
|
+
console.log(result.toBase58()); // Concatenated Base58 string
|
|
94
|
+
|
|
95
|
+
// Stream processing
|
|
96
|
+
for (const block of blockCrc32Iterator(buffer, 1024)) {
|
|
97
|
+
console.log(`Block at ${block.offset}: ${block.toBase58()}`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Verification
|
|
101
|
+
const isValid = bufferChecksumEquals(buffer, expectedChecksum);
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## CRC32 Variants
|
|
105
|
+
|
|
106
|
+
### CRC32 (IEEE 802.3)
|
|
107
|
+
- **Use case**: General purpose, file integrity
|
|
108
|
+
- **Compatible with**: ZIP files, PNG images, Ethernet frames
|
|
109
|
+
- **Polynomial**: `0xEDB88320` (reversed)
|
|
110
|
+
- **Standard test**: `"123456789"` → `0xcbf43926` ✅
|
|
111
|
+
|
|
112
|
+
### CRC32C (Castagnoli)
|
|
113
|
+
- **Use case**: High-performance applications
|
|
114
|
+
- **Compatible with**: iSCSI, SCTP, certain databases
|
|
115
|
+
- **Polynomial**: `0x82f63b78` (reversed)
|
|
116
|
+
- **Standard test**: `"123456789"` → `0xe3069283` ✅
|
|
117
|
+
|
|
118
|
+
```javascript
|
|
119
|
+
import { CRC32, CRC32C } from './crcTables.js';
|
|
120
|
+
|
|
121
|
+
// All functions accept variant parameter
|
|
122
|
+
stringToCrc32('data', CRC32); // Default
|
|
123
|
+
stringToCrc32('data', CRC32C); // Castagnoli
|
|
124
|
+
|
|
125
|
+
// Verify against standard test vectors
|
|
126
|
+
console.assert(stringToCrc32('123456789', CRC32) === 0xcbf43926);
|
|
127
|
+
console.assert(stringToCrc32('123456789', CRC32C) === 0xe3069283);
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Block Processing Details
|
|
131
|
+
|
|
132
|
+
### Standard vs Block Format
|
|
133
|
+
|
|
134
|
+
**Standard CRC32** (compatible):
|
|
135
|
+
```
|
|
136
|
+
Input: [large file data]
|
|
137
|
+
Output: 222957957 (single 32-bit value)
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
**Block CRC32** (proprietary):
|
|
141
|
+
```
|
|
142
|
+
Input: [large file data split into blocks]
|
|
143
|
+
Block 1: 222957957 → Base58 encoded
|
|
144
|
+
Block 2: 891347246 → Base58 encoded
|
|
145
|
+
Block 3: 234567890 → Base58 encoded
|
|
146
|
+
Output: [concatenated Base58 string]
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Block Size Selection
|
|
150
|
+
|
|
151
|
+
```javascript
|
|
152
|
+
// Automatic sizing
|
|
153
|
+
const blockSize = optimalBlockSize(buffer);
|
|
154
|
+
|
|
155
|
+
// Manual sizing
|
|
156
|
+
const result = bufferToBlockCrc32(buffer, 65536); // 64KB blocks
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
**Size Guidelines:**
|
|
160
|
+
- **Small files** (< 100KB): Single block
|
|
161
|
+
- **Medium files** (100KB - 2MB): 100KB blocks
|
|
162
|
+
- **Large files** (> 2MB): Adaptive sizing (max 20 blocks)
|
|
163
|
+
|
|
164
|
+
### Use Cases
|
|
165
|
+
|
|
166
|
+
**Standard CRC32:**
|
|
167
|
+
- File integrity verification
|
|
168
|
+
- Compatibility with external tools
|
|
169
|
+
- Single checksum for entire data
|
|
170
|
+
|
|
171
|
+
**Block CRC32:**
|
|
172
|
+
- Large file streaming
|
|
173
|
+
- Partial corruption detection
|
|
174
|
+
- Progress tracking during transfer
|
|
175
|
+
- Resume capability for interrupted uploads
|
|
176
|
+
|
|
177
|
+
## Error Handling
|
|
178
|
+
|
|
179
|
+
```javascript
|
|
180
|
+
// Invalid CRC variant
|
|
181
|
+
getCrcTable('invalid'); // throws "Invalid variant [invalid]"
|
|
182
|
+
|
|
183
|
+
// Unsupported buffer type
|
|
184
|
+
bufferToCrc32('string'); // throws "Unsupported buffer type"
|
|
185
|
+
|
|
186
|
+
// All functions validate inputs and provide clear error messages
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## Standards Compliance
|
|
190
|
+
|
|
191
|
+
✅ **Verified Implementation**: Both CRC32 and CRC32C pass standard test vectors
|
|
192
|
+
- CRC32 IEEE 802.3: `"123456789"` → `0xcbf43926`
|
|
193
|
+
- CRC32C Castagnoli: `"123456789"` → `0xe3069283`
|
|
194
|
+
|
|
195
|
+
✅ **Full Compatibility**:
|
|
196
|
+
- ZIP/PNG/Ethernet frames (CRC32)
|
|
197
|
+
- iSCSI/SCTP protocols (CRC32C)
|
|
198
|
+
|
|
199
|
+
## Performance Notes
|
|
200
|
+
|
|
201
|
+
- **Table caching**: CRC tables generated once per variant
|
|
202
|
+
- **Memory efficient**: Block processing doesn't load entire files
|
|
203
|
+
- **Browser compatible**: Works in all modern browsers
|
|
204
|
+
- **Standards compliant**: Verified against authoritative test vectors
|
|
205
|
+
- **No dependencies**: Pure JavaScript implementation
|
|
206
|
+
|
|
207
|
+
## Testing
|
|
208
|
+
|
|
209
|
+
Run tests for all modules:
|
|
210
|
+
|
|
211
|
+
```bash
|
|
212
|
+
pnpm test:file src/lib/util/checksum/
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
Run specific test files:
|
|
216
|
+
|
|
217
|
+
```bash
|
|
218
|
+
pnpm test:file src/lib/util/checksum/stringCrc32.test.js
|
|
219
|
+
pnpm test:file src/lib/util/checksum/bufferCrc32.test.js
|
|
220
|
+
pnpm test:file src/lib/util/checksum/blockCrc32.test.js
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## TypeScript Support
|
|
224
|
+
|
|
225
|
+
All modules include comprehensive JSDoc type annotations:
|
|
226
|
+
|
|
227
|
+
```javascript
|
|
228
|
+
/**
|
|
229
|
+
* @param {string} str - String to calculate checksum for
|
|
230
|
+
* @param {import('./crcTables.js').CrcVariant} [variant=CRC32] - CRC variant
|
|
231
|
+
* @returns {number} Unsigned 32-bit CRC32 value
|
|
232
|
+
*/
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
## Migration Guide
|
|
236
|
+
|
|
237
|
+
### From Original Implementation
|
|
238
|
+
|
|
239
|
+
**Old:**
|
|
240
|
+
```javascript
|
|
241
|
+
import { stringToCrc32 } from './crc32.js';
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
**New:**
|
|
245
|
+
```javascript
|
|
246
|
+
import { stringToCrc32 } from './stringCrc32.js';
|
|
247
|
+
// Same API, no changes needed
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### Adding Block Processing
|
|
251
|
+
|
|
252
|
+
**Before:**
|
|
253
|
+
```javascript
|
|
254
|
+
// Single CRC32 for entire file
|
|
255
|
+
const crc = bufferToCrc32(largeFile);
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
**After:**
|
|
259
|
+
```javascript
|
|
260
|
+
// Block-based processing
|
|
261
|
+
const result = bufferToBlockCrc32(largeFile);
|
|
262
|
+
const checksum = result.toBase58(); // Custom format
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
## License
|
|
266
|
+
|
|
267
|
+
Part of HKdigital library core utilities.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculate block-based CRC32 for buffer
|
|
3
|
+
*
|
|
4
|
+
* @param {ArrayBuffer|DataView|Uint8Array} buffer - Buffer to process
|
|
5
|
+
* @param {number} [blockSize] - Block size (auto-calculated if not provided)
|
|
6
|
+
* @param {import('./crcTables.js').CrcVariant} [variant=CRC32] - CRC variant
|
|
7
|
+
*
|
|
8
|
+
* @returns {object} Block checksum result
|
|
9
|
+
*/
|
|
10
|
+
export function bufferToBlockCrc32(buffer: ArrayBuffer | DataView | Uint8Array, blockSize?: number, variant?: import("./crcTables.js").CrcVariant): object;
|
|
11
|
+
/**
|
|
12
|
+
* Iterate over buffer blocks and yield CRC32 for each
|
|
13
|
+
*
|
|
14
|
+
* @param {ArrayBuffer|DataView|Uint8Array} buffer - Buffer to process
|
|
15
|
+
* @param {number} [blockSize] - Block size
|
|
16
|
+
* @param {import('./crcTables.js').CrcVariant} [variant=CRC32] - CRC variant
|
|
17
|
+
*
|
|
18
|
+
* @yields {object} Block checksum with value and encoding methods
|
|
19
|
+
*/
|
|
20
|
+
export function blockCrc32Iterator(buffer: ArrayBuffer | DataView | Uint8Array, blockSize?: number, variant?: import("./crcTables.js").CrcVariant): Generator<{
|
|
21
|
+
value: number;
|
|
22
|
+
offset: number;
|
|
23
|
+
size: number;
|
|
24
|
+
toBase58: () => string;
|
|
25
|
+
}, void, unknown>;
|
|
26
|
+
/**
|
|
27
|
+
* Check if buffer checksum matches expected value
|
|
28
|
+
*
|
|
29
|
+
* @param {ArrayBuffer|DataView|Uint8Array} buffer - Buffer to verify
|
|
30
|
+
* @param {string} expectedChecksum - Expected Base58 checksum string
|
|
31
|
+
* @param {number} [blockSize] - Block size
|
|
32
|
+
* @param {import('./crcTables.js').CrcVariant} [variant=CRC32] - CRC variant
|
|
33
|
+
*
|
|
34
|
+
* @returns {boolean} True if checksums match
|
|
35
|
+
*/
|
|
36
|
+
export function bufferChecksumEquals(buffer: ArrayBuffer | DataView | Uint8Array, expectedChecksum: string, blockSize?: number, variant?: import("./crcTables.js").CrcVariant): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Calculate optimal block size for buffer
|
|
39
|
+
*
|
|
40
|
+
* @param {ArrayBuffer|DataView|Uint8Array} buffer - Buffer to analyze
|
|
41
|
+
*
|
|
42
|
+
* @returns {number} Optimal block size
|
|
43
|
+
*/
|
|
44
|
+
export function optimalBlockSize(buffer: ArrayBuffer | DataView | Uint8Array): number;
|