@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
|
@@ -71,6 +71,62 @@
|
|
|
71
71
|
* @property {boolean} [cacheEnabled] Enable or disabled automatic caching
|
|
72
72
|
*/
|
|
73
73
|
|
|
74
|
+
/**
|
|
75
|
+
* @typedef {Object} JsonPutOptions
|
|
76
|
+
* @property {string|URL} url URL string or URL object for the request
|
|
77
|
+
* @property {*} body Request body (will be sent as JSON)
|
|
78
|
+
*
|
|
79
|
+
* @property {Object|URLSearchParams} [urlSearchParams]
|
|
80
|
+
* Parameters to add to the URL
|
|
81
|
+
*
|
|
82
|
+
* @property {Record<string, string>} [headers] HTTP headers as name-value pairs
|
|
83
|
+
* @property {boolean} [withCredentials] Whether to include credentials
|
|
84
|
+
* @property {number} [timeoutMs] Request timeout in milliseconds
|
|
85
|
+
* @property {RequestHandler} [requestHandler] Handler for abort/timeout control
|
|
86
|
+
* @property {string} [mode] CORS mode ('cors', 'no-cors', 'same-origin')
|
|
87
|
+
* @property {string} [cache] Cache mode ('default', 'no-cache', etc.)
|
|
88
|
+
* @property {string} [redirect] Redirect mode ('follow', 'error', 'manual')
|
|
89
|
+
* @property {string} [referrerPolicy] Referrer policy
|
|
90
|
+
* @property {boolean} [cacheEnabled] Enable or disabled automatic caching
|
|
91
|
+
*/
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @typedef {Object} JsonPatchOptions
|
|
95
|
+
* @property {string|URL} url URL string or URL object for the request
|
|
96
|
+
* @property {*} body Request body (will be sent as JSON)
|
|
97
|
+
*
|
|
98
|
+
* @property {Object|URLSearchParams} [urlSearchParams]
|
|
99
|
+
* Parameters to add to the URL
|
|
100
|
+
*
|
|
101
|
+
* @property {Record<string, string>} [headers] HTTP headers as name-value pairs
|
|
102
|
+
* @property {boolean} [withCredentials] Whether to include credentials
|
|
103
|
+
* @property {number} [timeoutMs] Request timeout in milliseconds
|
|
104
|
+
* @property {RequestHandler} [requestHandler] Handler for abort/timeout control
|
|
105
|
+
* @property {string} [mode] CORS mode ('cors', 'no-cors', 'same-origin')
|
|
106
|
+
* @property {string} [cache] Cache mode ('default', 'no-cache', etc.)
|
|
107
|
+
* @property {string} [redirect] Redirect mode ('follow', 'error', 'manual')
|
|
108
|
+
* @property {string} [referrerPolicy] Referrer policy
|
|
109
|
+
* @property {boolean} [cacheEnabled] Enable or disabled automatic caching
|
|
110
|
+
*/
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* @typedef {Object} JsonDeleteOptions
|
|
114
|
+
* @property {string|URL} url URL string or URL object for the request
|
|
115
|
+
*
|
|
116
|
+
* @property {Object|URLSearchParams} [urlSearchParams]
|
|
117
|
+
* Parameters to add to the URL
|
|
118
|
+
*
|
|
119
|
+
* @property {Record<string, string>} [headers] HTTP headers as name-value pairs
|
|
120
|
+
* @property {boolean} [withCredentials] Whether to include credentials
|
|
121
|
+
* @property {number} [timeoutMs] Request timeout in milliseconds
|
|
122
|
+
* @property {RequestHandler} [requestHandler] Handler for abort/timeout control
|
|
123
|
+
* @property {string} [mode] CORS mode ('cors', 'no-cors', 'same-origin')
|
|
124
|
+
* @property {string} [cache] Cache mode ('default', 'no-cache', etc.')
|
|
125
|
+
* @property {string} [redirect] Redirect mode ('follow', 'error', 'manual')
|
|
126
|
+
* @property {string} [referrerPolicy] Referrer policy
|
|
127
|
+
* @property {boolean} [cacheEnabled] Enable or disabled automatic caching
|
|
128
|
+
*/
|
|
129
|
+
|
|
74
130
|
/**
|
|
75
131
|
* @typedef {Object} StaleInfo
|
|
76
132
|
* @property {boolean} isStale Whether the response contains stale data
|
package/dist/services/README.md
CHANGED
|
@@ -204,22 +204,31 @@ await manager.stopAll();
|
|
|
204
204
|
Services receive helpful utilities in their constructor options for accessing other services:
|
|
205
205
|
|
|
206
206
|
```javascript
|
|
207
|
+
/**
|
|
208
|
+
* Example service that depends on other services
|
|
209
|
+
*/
|
|
207
210
|
class AuthService extends ServiceBase {
|
|
211
|
+
/** @type {(<T>(serviceName: string) => T)} */
|
|
212
|
+
#getService;
|
|
213
|
+
|
|
214
|
+
/** @type {() => import('$hklib-core/services/index.js').ServiceManager} */
|
|
215
|
+
#getManager;
|
|
216
|
+
|
|
208
217
|
constructor(serviceName, options) {
|
|
209
218
|
super(serviceName, options);
|
|
210
219
|
|
|
211
|
-
// Store service access utilities
|
|
212
|
-
this
|
|
213
|
-
this
|
|
220
|
+
// Store service access utilities as private methods
|
|
221
|
+
this.#getService = options.getService; // Bound getService function
|
|
222
|
+
this.#getManager = options.getManager; // Function to get manager (lazy)
|
|
214
223
|
}
|
|
215
224
|
|
|
216
225
|
async authenticateUser(credentials) {
|
|
217
226
|
// Access other services with full type safety and error checking
|
|
218
|
-
const database = this
|
|
227
|
+
const database = this.#getService('database');
|
|
219
228
|
const user = await database.findUser(credentials.username);
|
|
220
229
|
|
|
221
|
-
// Access manager for advanced operations
|
|
222
|
-
const manager = this
|
|
230
|
+
// Access manager for advanced operations when needed
|
|
231
|
+
const manager = this.#getManager();
|
|
223
232
|
const health = await manager.checkHealth();
|
|
224
233
|
|
|
225
234
|
return user;
|
|
@@ -227,6 +236,48 @@ class AuthService extends ServiceBase {
|
|
|
227
236
|
}
|
|
228
237
|
```
|
|
229
238
|
|
|
239
|
+
**Best Practice Pattern:**
|
|
240
|
+
|
|
241
|
+
The recommended approach is to store service access functions as **private methods** using the hash prefix. This pattern:
|
|
242
|
+
|
|
243
|
+
- **Keeps the API clean** - No public getService/getManager methods exposed
|
|
244
|
+
- **Prevents serialization issues** - Private fields don't serialize to JSON
|
|
245
|
+
- **Enforces proper encapsulation** - Service dependencies stay internal
|
|
246
|
+
- **Provides type safety** - Full generic support with `this.#getService<DatabaseService>('database')`
|
|
247
|
+
|
|
248
|
+
```javascript
|
|
249
|
+
/**
|
|
250
|
+
* Unified service for tracking complete player data including progress and
|
|
251
|
+
* profile matches
|
|
252
|
+
*/
|
|
253
|
+
export default class PlayerService extends ServiceBase {
|
|
254
|
+
|
|
255
|
+
/** @type {(<T>(serviceName: string) => T)} */
|
|
256
|
+
#getService;
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* @param {string} serviceName
|
|
260
|
+
* @param {import('$hklib-core/services/typedef.js').ServiceOptions} [options]
|
|
261
|
+
*/
|
|
262
|
+
constructor(serviceName, options) {
|
|
263
|
+
super(serviceName, options);
|
|
264
|
+
|
|
265
|
+
this.#getService = options?.getService;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async getPlayerProfile(playerId) {
|
|
269
|
+
// Access dependent services cleanly
|
|
270
|
+
const database = this.#getService('database');
|
|
271
|
+
const analytics = this.#getService('analytics');
|
|
272
|
+
|
|
273
|
+
const profile = await database.getPlayer(playerId);
|
|
274
|
+
const stats = await analytics.getPlayerStats(playerId);
|
|
275
|
+
|
|
276
|
+
return { ...profile, stats };
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
```
|
|
280
|
+
|
|
230
281
|
**Service Access Methods:**
|
|
231
282
|
|
|
232
283
|
```javascript
|
|
@@ -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;
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom block-based CRC32 calculation
|
|
3
|
+
*
|
|
4
|
+
* Splits large buffers into blocks and concatenates individual CRC32 values.
|
|
5
|
+
* Creates proprietary checksum format for efficient large file processing.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { base58fromNumber } from '../bases.js';
|
|
9
|
+
|
|
10
|
+
import { bufferToCrc32 } from './bufferCrc32.js';
|
|
11
|
+
import { CRC32 } from './crcTables.js';
|
|
12
|
+
|
|
13
|
+
/* ---------------------------------------------------------------- Constants */
|
|
14
|
+
|
|
15
|
+
/** @type {number} */
|
|
16
|
+
const MIN_BUFFER_BLOCK_SIZE = 100000;
|
|
17
|
+
|
|
18
|
+
/** @type {number} */
|
|
19
|
+
const MAX_BUFFER_BLOCKS = 20;
|
|
20
|
+
|
|
21
|
+
/** @type {number} */
|
|
22
|
+
const OPTIMAL_BLOCK_SIZE_FACTOR = 0.95;
|
|
23
|
+
|
|
24
|
+
/* ---------------------------------------------------------------- Functions */
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Calculate block-based CRC32 for buffer
|
|
28
|
+
*
|
|
29
|
+
* @param {ArrayBuffer|DataView|Uint8Array} buffer - Buffer to process
|
|
30
|
+
* @param {number} [blockSize] - Block size (auto-calculated if not provided)
|
|
31
|
+
* @param {import('./crcTables.js').CrcVariant} [variant=CRC32] - CRC variant
|
|
32
|
+
*
|
|
33
|
+
* @returns {object} Block checksum result
|
|
34
|
+
*/
|
|
35
|
+
export function bufferToBlockCrc32(buffer, blockSize, variant = CRC32) {
|
|
36
|
+
if (!blockSize) {
|
|
37
|
+
blockSize = optimalBlockSize(buffer);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** @type {number[]} */
|
|
41
|
+
const checksumList = [];
|
|
42
|
+
|
|
43
|
+
for (const block of blockCrc32Iterator(buffer, blockSize, variant)) {
|
|
44
|
+
checksumList.push(block.value);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
checksumList,
|
|
49
|
+
blockSize,
|
|
50
|
+
toBase58: () => checksumsToBase58(checksumList)
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Iterate over buffer blocks and yield CRC32 for each
|
|
56
|
+
*
|
|
57
|
+
* @param {ArrayBuffer|DataView|Uint8Array} buffer - Buffer to process
|
|
58
|
+
* @param {number} [blockSize] - Block size
|
|
59
|
+
* @param {import('./crcTables.js').CrcVariant} [variant=CRC32] - CRC variant
|
|
60
|
+
*
|
|
61
|
+
* @yields {object} Block checksum with value and encoding methods
|
|
62
|
+
*/
|
|
63
|
+
export function* blockCrc32Iterator(buffer, blockSize, variant = CRC32) {
|
|
64
|
+
if (!blockSize) {
|
|
65
|
+
blockSize = optimalBlockSize(buffer);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const bufferLength = getBufferLength(buffer);
|
|
69
|
+
|
|
70
|
+
for (let offset = 0; offset < bufferLength; offset += blockSize) {
|
|
71
|
+
const currentBlockSize = Math.min(blockSize, bufferLength - offset);
|
|
72
|
+
|
|
73
|
+
/** @type {number} */
|
|
74
|
+
const value = bufferToCrc32(buffer, offset, currentBlockSize, variant);
|
|
75
|
+
|
|
76
|
+
yield {
|
|
77
|
+
value,
|
|
78
|
+
offset,
|
|
79
|
+
size: currentBlockSize,
|
|
80
|
+
toBase58: () => base58fromNumber(value)
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Check if buffer checksum matches expected value
|
|
87
|
+
*
|
|
88
|
+
* @param {ArrayBuffer|DataView|Uint8Array} buffer - Buffer to verify
|
|
89
|
+
* @param {string} expectedChecksum - Expected Base58 checksum string
|
|
90
|
+
* @param {number} [blockSize] - Block size
|
|
91
|
+
* @param {import('./crcTables.js').CrcVariant} [variant=CRC32] - CRC variant
|
|
92
|
+
*
|
|
93
|
+
* @returns {boolean} True if checksums match
|
|
94
|
+
*/
|
|
95
|
+
export function bufferChecksumEquals(
|
|
96
|
+
buffer,
|
|
97
|
+
expectedChecksum,
|
|
98
|
+
blockSize,
|
|
99
|
+
variant = CRC32
|
|
100
|
+
) {
|
|
101
|
+
if (!blockSize) {
|
|
102
|
+
blockSize = optimalBlockSize(buffer);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** @type {string} */
|
|
106
|
+
let remaining = expectedChecksum;
|
|
107
|
+
|
|
108
|
+
for (const block of blockCrc32Iterator(buffer, blockSize, variant)) {
|
|
109
|
+
const blockChecksum = block.toBase58();
|
|
110
|
+
|
|
111
|
+
if (!remaining.startsWith(blockChecksum)) {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
remaining = remaining.slice(blockChecksum.length);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return remaining.length === 0;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Calculate optimal block size for buffer
|
|
123
|
+
*
|
|
124
|
+
* @param {ArrayBuffer|DataView|Uint8Array} buffer - Buffer to analyze
|
|
125
|
+
*
|
|
126
|
+
* @returns {number} Optimal block size
|
|
127
|
+
*/
|
|
128
|
+
export function optimalBlockSize(buffer) {
|
|
129
|
+
/** @type {number} */
|
|
130
|
+
const bufferByteLength = getBufferLength(buffer);
|
|
131
|
+
|
|
132
|
+
if (bufferByteLength < MIN_BUFFER_BLOCK_SIZE) {
|
|
133
|
+
return bufferByteLength;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** @type {number} */
|
|
137
|
+
let blockSize = bufferByteLength / MAX_BUFFER_BLOCKS;
|
|
138
|
+
|
|
139
|
+
if (blockSize < MIN_BUFFER_BLOCK_SIZE) {
|
|
140
|
+
return MIN_BUFFER_BLOCK_SIZE;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return Math.ceil(
|
|
144
|
+
blockSize * OPTIMAL_BLOCK_SIZE_FACTOR +
|
|
145
|
+
MIN_BUFFER_BLOCK_SIZE * (1 - OPTIMAL_BLOCK_SIZE_FACTOR)
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/* ------------------------------------------------------- Internal functions */
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Get buffer length safely for different buffer types
|
|
153
|
+
*
|
|
154
|
+
* @param {ArrayBuffer|DataView|Uint8Array} buffer - Buffer to measure
|
|
155
|
+
*
|
|
156
|
+
* @returns {number} Buffer length in bytes
|
|
157
|
+
*/
|
|
158
|
+
function getBufferLength(buffer) {
|
|
159
|
+
if (buffer instanceof ArrayBuffer || buffer instanceof DataView) {
|
|
160
|
+
return buffer.byteLength;
|
|
161
|
+
}
|
|
162
|
+
return buffer.length;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Convert checksum array to concatenated Base58 string
|
|
167
|
+
*
|
|
168
|
+
* @param {number[]} checksumList - Array of CRC32 values
|
|
169
|
+
*
|
|
170
|
+
* @returns {string} Concatenated Base58 string
|
|
171
|
+
*/
|
|
172
|
+
function checksumsToBase58(checksumList) {
|
|
173
|
+
return checksumList.map(crc => base58fromNumber(crc)).join('');
|
|
174
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculate standard CRC32 for buffer
|
|
3
|
+
*
|
|
4
|
+
* @param {ArrayBuffer|DataView|Uint8Array} buffer - Buffer to checksum
|
|
5
|
+
* @param {number} [byteOffset=0] - Start offset in buffer
|
|
6
|
+
* @param {number} [byteLength] - Number of bytes to process
|
|
7
|
+
* @param {import('./crcTables.js').CrcVariant} [variant=CRC32] - CRC variant
|
|
8
|
+
*
|
|
9
|
+
* @returns {number} Unsigned 32-bit CRC32 value
|
|
10
|
+
*/
|
|
11
|
+
export function bufferToCrc32(buffer: ArrayBuffer | DataView | Uint8Array, byteOffset?: number, byteLength?: number, variant?: import("./crcTables.js").CrcVariant): number;
|