@push.rocks/smartdb 1.0.1 → 2.1.1

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.
Files changed (126) hide show
  1. package/.smartconfig.json +18 -4
  2. package/dist_rust/rustdb_linux_amd64 +0 -0
  3. package/dist_rust/rustdb_linux_arm64 +0 -0
  4. package/dist_ts/00_commitinfo_data.js +3 -3
  5. package/dist_ts/index.d.ts +1 -0
  6. package/dist_ts/ts_local/classes.localsmartdb.d.ts +5 -5
  7. package/dist_ts/ts_local/classes.localsmartdb.js +7 -9
  8. package/dist_ts/ts_local/plugins.d.ts +1 -2
  9. package/dist_ts/ts_local/plugins.js +3 -3
  10. package/dist_ts/ts_smartdb/index.d.ts +2 -24
  11. package/dist_ts/ts_smartdb/index.js +4 -29
  12. package/dist_ts/ts_smartdb/plugins.d.ts +2 -10
  13. package/dist_ts/ts_smartdb/plugins.js +3 -13
  14. package/dist_ts/ts_smartdb/rust-db-bridge.d.ts +122 -0
  15. package/dist_ts/ts_smartdb/rust-db-bridge.js +113 -0
  16. package/dist_ts/ts_smartdb/server/SmartdbServer.d.ts +39 -37
  17. package/dist_ts/ts_smartdb/server/SmartdbServer.js +87 -206
  18. package/dist_ts/ts_smartdb/server/index.d.ts +0 -4
  19. package/dist_ts/ts_smartdb/server/index.js +1 -5
  20. package/dist_ts_debugserver/bundled.d.ts +4 -0
  21. package/dist_ts_debugserver/bundled.js +12 -0
  22. package/dist_ts_debugserver/classes.debugserver.d.ts +36 -0
  23. package/dist_ts_debugserver/classes.debugserver.js +95 -0
  24. package/dist_ts_debugserver/index.d.ts +2 -0
  25. package/dist_ts_debugserver/index.js +2 -0
  26. package/dist_ts_debugserver/plugins.d.ts +2 -0
  27. package/dist_ts_debugserver/plugins.js +3 -0
  28. package/dist_ts_debugui/index.d.ts +2 -0
  29. package/dist_ts_debugui/index.js +2 -0
  30. package/dist_ts_debugui/plugins.d.ts +1 -0
  31. package/dist_ts_debugui/plugins.js +2 -0
  32. package/dist_ts_debugui/smartdb-debugui.d.ts +62 -0
  33. package/dist_ts_debugui/smartdb-debugui.js +1132 -0
  34. package/license +3 -1
  35. package/package.json +14 -13
  36. package/readme.md +209 -177
  37. package/ts/00_commitinfo_data.ts +2 -2
  38. package/ts/index.ts +11 -0
  39. package/ts/ts_local/classes.localsmartdb.ts +5 -6
  40. package/ts/ts_local/plugins.ts +1 -3
  41. package/ts/ts_smartdb/index.ts +14 -41
  42. package/ts/ts_smartdb/plugins.ts +2 -15
  43. package/ts/ts_smartdb/rust-db-bridge.ts +262 -0
  44. package/ts/ts_smartdb/server/SmartdbServer.ts +115 -246
  45. package/ts/ts_smartdb/server/index.ts +0 -7
  46. package/dist_ts/ts_smartdb/engine/AggregationEngine.d.ts +0 -66
  47. package/dist_ts/ts_smartdb/engine/AggregationEngine.js +0 -189
  48. package/dist_ts/ts_smartdb/engine/IndexEngine.d.ts +0 -97
  49. package/dist_ts/ts_smartdb/engine/IndexEngine.js +0 -678
  50. package/dist_ts/ts_smartdb/engine/QueryEngine.d.ts +0 -54
  51. package/dist_ts/ts_smartdb/engine/QueryEngine.js +0 -271
  52. package/dist_ts/ts_smartdb/engine/QueryPlanner.d.ts +0 -64
  53. package/dist_ts/ts_smartdb/engine/QueryPlanner.js +0 -308
  54. package/dist_ts/ts_smartdb/engine/SessionEngine.d.ts +0 -117
  55. package/dist_ts/ts_smartdb/engine/SessionEngine.js +0 -232
  56. package/dist_ts/ts_smartdb/engine/TransactionEngine.d.ts +0 -85
  57. package/dist_ts/ts_smartdb/engine/TransactionEngine.js +0 -287
  58. package/dist_ts/ts_smartdb/engine/UpdateEngine.d.ts +0 -47
  59. package/dist_ts/ts_smartdb/engine/UpdateEngine.js +0 -461
  60. package/dist_ts/ts_smartdb/errors/SmartdbErrors.d.ts +0 -100
  61. package/dist_ts/ts_smartdb/errors/SmartdbErrors.js +0 -155
  62. package/dist_ts/ts_smartdb/server/CommandRouter.d.ts +0 -87
  63. package/dist_ts/ts_smartdb/server/CommandRouter.js +0 -222
  64. package/dist_ts/ts_smartdb/server/WireProtocol.d.ts +0 -117
  65. package/dist_ts/ts_smartdb/server/WireProtocol.js +0 -298
  66. package/dist_ts/ts_smartdb/server/handlers/AdminHandler.d.ts +0 -100
  67. package/dist_ts/ts_smartdb/server/handlers/AdminHandler.js +0 -668
  68. package/dist_ts/ts_smartdb/server/handlers/AggregateHandler.d.ts +0 -31
  69. package/dist_ts/ts_smartdb/server/handlers/AggregateHandler.js +0 -277
  70. package/dist_ts/ts_smartdb/server/handlers/DeleteHandler.d.ts +0 -8
  71. package/dist_ts/ts_smartdb/server/handlers/DeleteHandler.js +0 -95
  72. package/dist_ts/ts_smartdb/server/handlers/FindHandler.d.ts +0 -31
  73. package/dist_ts/ts_smartdb/server/handlers/FindHandler.js +0 -291
  74. package/dist_ts/ts_smartdb/server/handlers/HelloHandler.d.ts +0 -11
  75. package/dist_ts/ts_smartdb/server/handlers/HelloHandler.js +0 -62
  76. package/dist_ts/ts_smartdb/server/handlers/IndexHandler.d.ts +0 -20
  77. package/dist_ts/ts_smartdb/server/handlers/IndexHandler.js +0 -183
  78. package/dist_ts/ts_smartdb/server/handlers/InsertHandler.d.ts +0 -8
  79. package/dist_ts/ts_smartdb/server/handlers/InsertHandler.js +0 -79
  80. package/dist_ts/ts_smartdb/server/handlers/UpdateHandler.d.ts +0 -24
  81. package/dist_ts/ts_smartdb/server/handlers/UpdateHandler.js +0 -296
  82. package/dist_ts/ts_smartdb/server/handlers/index.d.ts +0 -8
  83. package/dist_ts/ts_smartdb/server/handlers/index.js +0 -10
  84. package/dist_ts/ts_smartdb/storage/FileStorageAdapter.d.ts +0 -85
  85. package/dist_ts/ts_smartdb/storage/FileStorageAdapter.js +0 -465
  86. package/dist_ts/ts_smartdb/storage/IStorageAdapter.d.ts +0 -145
  87. package/dist_ts/ts_smartdb/storage/IStorageAdapter.js +0 -2
  88. package/dist_ts/ts_smartdb/storage/MemoryStorageAdapter.d.ts +0 -67
  89. package/dist_ts/ts_smartdb/storage/MemoryStorageAdapter.js +0 -378
  90. package/dist_ts/ts_smartdb/storage/OpLog.d.ts +0 -93
  91. package/dist_ts/ts_smartdb/storage/OpLog.js +0 -221
  92. package/dist_ts/ts_smartdb/storage/WAL.d.ts +0 -117
  93. package/dist_ts/ts_smartdb/storage/WAL.js +0 -286
  94. package/dist_ts/ts_smartdb/types/interfaces.d.ts +0 -363
  95. package/dist_ts/ts_smartdb/types/interfaces.js +0 -2
  96. package/dist_ts/ts_smartdb/utils/checksum.d.ts +0 -30
  97. package/dist_ts/ts_smartdb/utils/checksum.js +0 -77
  98. package/dist_ts/ts_smartdb/utils/index.d.ts +0 -1
  99. package/dist_ts/ts_smartdb/utils/index.js +0 -2
  100. package/ts/ts_smartdb/engine/AggregationEngine.ts +0 -283
  101. package/ts/ts_smartdb/engine/IndexEngine.ts +0 -798
  102. package/ts/ts_smartdb/engine/QueryEngine.ts +0 -301
  103. package/ts/ts_smartdb/engine/QueryPlanner.ts +0 -393
  104. package/ts/ts_smartdb/engine/SessionEngine.ts +0 -292
  105. package/ts/ts_smartdb/engine/TransactionEngine.ts +0 -351
  106. package/ts/ts_smartdb/engine/UpdateEngine.ts +0 -506
  107. package/ts/ts_smartdb/errors/SmartdbErrors.ts +0 -181
  108. package/ts/ts_smartdb/server/CommandRouter.ts +0 -289
  109. package/ts/ts_smartdb/server/WireProtocol.ts +0 -416
  110. package/ts/ts_smartdb/server/handlers/AdminHandler.ts +0 -719
  111. package/ts/ts_smartdb/server/handlers/AggregateHandler.ts +0 -342
  112. package/ts/ts_smartdb/server/handlers/DeleteHandler.ts +0 -115
  113. package/ts/ts_smartdb/server/handlers/FindHandler.ts +0 -330
  114. package/ts/ts_smartdb/server/handlers/HelloHandler.ts +0 -78
  115. package/ts/ts_smartdb/server/handlers/IndexHandler.ts +0 -207
  116. package/ts/ts_smartdb/server/handlers/InsertHandler.ts +0 -97
  117. package/ts/ts_smartdb/server/handlers/UpdateHandler.ts +0 -344
  118. package/ts/ts_smartdb/server/handlers/index.ts +0 -10
  119. package/ts/ts_smartdb/storage/FileStorageAdapter.ts +0 -562
  120. package/ts/ts_smartdb/storage/IStorageAdapter.ts +0 -208
  121. package/ts/ts_smartdb/storage/MemoryStorageAdapter.ts +0 -455
  122. package/ts/ts_smartdb/storage/OpLog.ts +0 -282
  123. package/ts/ts_smartdb/storage/WAL.ts +0 -375
  124. package/ts/ts_smartdb/types/interfaces.ts +0 -433
  125. package/ts/ts_smartdb/utils/checksum.ts +0 -88
  126. package/ts/ts_smartdb/utils/index.ts +0 -1
@@ -1,433 +0,0 @@
1
- import type * as plugins from '../plugins.js';
2
-
3
- // ============================================================================
4
- // Document Types
5
- // ============================================================================
6
-
7
- export type Document = Record<string, any>;
8
-
9
- export interface WithId<TSchema> {
10
- _id: plugins.bson.ObjectId;
11
- }
12
-
13
- // ============================================================================
14
- // Client Options
15
- // ============================================================================
16
-
17
- export interface ISmartdbClientOptions {
18
- /** Storage adapter type: 'memory' or 'file' */
19
- storageType?: 'memory' | 'file';
20
- /** Path for file-based storage */
21
- storagePath?: string;
22
- /** Enable persistence for memory adapter */
23
- persist?: boolean;
24
- /** Path for persistence file when using memory adapter */
25
- persistPath?: string;
26
- }
27
-
28
- // ============================================================================
29
- // Connection String Parsing
30
- // ============================================================================
31
-
32
- export interface IParsedConnectionString {
33
- protocol: 'smartdb';
34
- storageType: 'memory' | 'file';
35
- options: {
36
- persist?: string;
37
- path?: string;
38
- };
39
- }
40
-
41
- // ============================================================================
42
- // CRUD Operation Options
43
- // ============================================================================
44
-
45
- export interface IInsertOneOptions {
46
- /** Session for transaction support */
47
- session?: IClientSession;
48
- /** Custom write concern */
49
- writeConcern?: IWriteConcern;
50
- }
51
-
52
- export interface IInsertManyOptions extends IInsertOneOptions {
53
- /** If true, inserts are ordered and stop on first error */
54
- ordered?: boolean;
55
- }
56
-
57
- export interface IFindOptions<TSchema = Document> {
58
- /** Projection to apply */
59
- projection?: Partial<Record<keyof TSchema | string, 0 | 1 | boolean>>;
60
- /** Sort specification */
61
- sort?: ISortSpecification;
62
- /** Number of documents to skip */
63
- skip?: number;
64
- /** Maximum number of documents to return */
65
- limit?: number;
66
- /** Session for transaction support */
67
- session?: IClientSession;
68
- /** Hint for index usage */
69
- hint?: string | Document;
70
- }
71
-
72
- export interface IUpdateOptions {
73
- /** Create document if it doesn't exist */
74
- upsert?: boolean;
75
- /** Session for transaction support */
76
- session?: IClientSession;
77
- /** Array filters for positional updates */
78
- arrayFilters?: Document[];
79
- /** Custom write concern */
80
- writeConcern?: IWriteConcern;
81
- /** Hint for index usage */
82
- hint?: string | Document;
83
- }
84
-
85
- export interface IReplaceOptions extends IUpdateOptions {}
86
-
87
- export interface IDeleteOptions {
88
- /** Session for transaction support */
89
- session?: IClientSession;
90
- /** Custom write concern */
91
- writeConcern?: IWriteConcern;
92
- /** Hint for index usage */
93
- hint?: string | Document;
94
- }
95
-
96
- export interface IFindOneAndUpdateOptions extends IUpdateOptions {
97
- /** Return the document before or after the update */
98
- returnDocument?: 'before' | 'after';
99
- /** Projection to apply */
100
- projection?: Document;
101
- /** Sort specification to determine which document to modify */
102
- sort?: ISortSpecification;
103
- }
104
-
105
- export interface IFindOneAndReplaceOptions extends IFindOneAndUpdateOptions {}
106
-
107
- export interface IFindOneAndDeleteOptions {
108
- /** Projection to apply */
109
- projection?: Document;
110
- /** Sort specification to determine which document to delete */
111
- sort?: ISortSpecification;
112
- /** Session for transaction support */
113
- session?: IClientSession;
114
- }
115
-
116
- // ============================================================================
117
- // CRUD Results
118
- // ============================================================================
119
-
120
- export interface IInsertOneResult {
121
- acknowledged: boolean;
122
- insertedId: plugins.bson.ObjectId;
123
- }
124
-
125
- export interface IInsertManyResult {
126
- acknowledged: boolean;
127
- insertedCount: number;
128
- insertedIds: Record<number, plugins.bson.ObjectId>;
129
- }
130
-
131
- export interface IUpdateResult {
132
- acknowledged: boolean;
133
- matchedCount: number;
134
- modifiedCount: number;
135
- upsertedCount: number;
136
- upsertedId: plugins.bson.ObjectId | null;
137
- }
138
-
139
- export interface IDeleteResult {
140
- acknowledged: boolean;
141
- deletedCount: number;
142
- }
143
-
144
- export interface IModifyResult<TSchema> {
145
- value: TSchema | null;
146
- ok: 1 | 0;
147
- lastErrorObject?: {
148
- n: number;
149
- updatedExisting?: boolean;
150
- upserted?: plugins.bson.ObjectId;
151
- };
152
- }
153
-
154
- // ============================================================================
155
- // Sort and Index Types
156
- // ============================================================================
157
-
158
- export type ISortDirection = 1 | -1 | 'asc' | 'desc' | 'ascending' | 'descending';
159
-
160
- export type ISortSpecification = Record<string, ISortDirection> | [string, ISortDirection][];
161
-
162
- export interface IIndexSpecification {
163
- key: Record<string, 1 | -1 | 'text' | '2dsphere'>;
164
- name?: string;
165
- unique?: boolean;
166
- sparse?: boolean;
167
- expireAfterSeconds?: number;
168
- background?: boolean;
169
- partialFilterExpression?: Document;
170
- }
171
-
172
- export interface IIndexInfo {
173
- v: number;
174
- key: Record<string, 1 | -1 | string>;
175
- name: string;
176
- unique?: boolean;
177
- sparse?: boolean;
178
- expireAfterSeconds?: number;
179
- }
180
-
181
- export interface ICreateIndexOptions {
182
- unique?: boolean;
183
- sparse?: boolean;
184
- expireAfterSeconds?: number;
185
- name?: string;
186
- background?: boolean;
187
- partialFilterExpression?: Document;
188
- }
189
-
190
- // ============================================================================
191
- // Write Concern
192
- // ============================================================================
193
-
194
- export interface IWriteConcern {
195
- w?: number | 'majority';
196
- j?: boolean;
197
- wtimeout?: number;
198
- }
199
-
200
- // ============================================================================
201
- // Aggregation Types
202
- // ============================================================================
203
-
204
- export interface IAggregateOptions {
205
- /** Allow disk use for large aggregations */
206
- allowDiskUse?: boolean;
207
- /** Maximum time in ms */
208
- maxTimeMS?: number;
209
- /** Session for transaction support */
210
- session?: IClientSession;
211
- /** Batch size for cursor */
212
- batchSize?: number;
213
- /** Collation settings */
214
- collation?: ICollation;
215
- /** Hint for index usage */
216
- hint?: string | Document;
217
- /** Comment for profiling */
218
- comment?: string;
219
- }
220
-
221
- export interface ICollation {
222
- locale: string;
223
- caseLevel?: boolean;
224
- caseFirst?: string;
225
- strength?: number;
226
- numericOrdering?: boolean;
227
- alternate?: string;
228
- maxVariable?: string;
229
- backwards?: boolean;
230
- }
231
-
232
- // ============================================================================
233
- // Change Stream Types
234
- // ============================================================================
235
-
236
- export interface IChangeStreamOptions {
237
- /** Resume after this token */
238
- resumeAfter?: IResumeToken;
239
- /** Start at this operation time */
240
- startAtOperationTime?: plugins.bson.Timestamp;
241
- /** Start after this token */
242
- startAfter?: IResumeToken;
243
- /** Full document lookup mode */
244
- fullDocument?: 'default' | 'updateLookup' | 'whenAvailable' | 'required';
245
- /** Full document before change */
246
- fullDocumentBeforeChange?: 'off' | 'whenAvailable' | 'required';
247
- /** Batch size */
248
- batchSize?: number;
249
- /** Maximum await time in ms */
250
- maxAwaitTimeMS?: number;
251
- }
252
-
253
- export interface IResumeToken {
254
- _data: string;
255
- }
256
-
257
- export type ChangeStreamOperationType =
258
- | 'insert'
259
- | 'update'
260
- | 'replace'
261
- | 'delete'
262
- | 'drop'
263
- | 'rename'
264
- | 'dropDatabase'
265
- | 'invalidate';
266
-
267
- export interface IChangeStreamDocument<TSchema = Document> {
268
- _id: IResumeToken;
269
- operationType: ChangeStreamOperationType;
270
- fullDocument?: TSchema;
271
- fullDocumentBeforeChange?: TSchema;
272
- ns: {
273
- db: string;
274
- coll?: string;
275
- };
276
- documentKey?: { _id: plugins.bson.ObjectId };
277
- updateDescription?: {
278
- updatedFields?: Document;
279
- removedFields?: string[];
280
- truncatedArrays?: Array<{ field: string; newSize: number }>;
281
- };
282
- clusterTime?: plugins.bson.Timestamp;
283
- txnNumber?: number;
284
- lsid?: { id: plugins.bson.Binary; uid: plugins.bson.Binary };
285
- }
286
-
287
- // ============================================================================
288
- // Transaction Types
289
- // ============================================================================
290
-
291
- export interface IClientSession {
292
- id: { id: plugins.bson.Binary };
293
- inTransaction(): boolean;
294
- startTransaction(options?: ITransactionOptions): void;
295
- commitTransaction(): Promise<void>;
296
- abortTransaction(): Promise<void>;
297
- withTransaction<T>(fn: () => Promise<T>, options?: ITransactionOptions): Promise<T>;
298
- endSession(): Promise<void>;
299
- }
300
-
301
- export interface ITransactionOptions {
302
- readConcern?: IReadConcern;
303
- writeConcern?: IWriteConcern;
304
- readPreference?: string;
305
- maxCommitTimeMS?: number;
306
- }
307
-
308
- export interface IReadConcern {
309
- level: 'local' | 'available' | 'majority' | 'linearizable' | 'snapshot';
310
- }
311
-
312
- // ============================================================================
313
- // Bulk Operation Types
314
- // ============================================================================
315
-
316
- export interface IBulkWriteOptions {
317
- ordered?: boolean;
318
- session?: IClientSession;
319
- writeConcern?: IWriteConcern;
320
- }
321
-
322
- export interface IBulkWriteOperation<TSchema = Document> {
323
- insertOne?: { document: TSchema };
324
- updateOne?: { filter: Document; update: Document; upsert?: boolean; arrayFilters?: Document[]; hint?: Document | string };
325
- updateMany?: { filter: Document; update: Document; upsert?: boolean; arrayFilters?: Document[]; hint?: Document | string };
326
- replaceOne?: { filter: Document; replacement: TSchema; upsert?: boolean; hint?: Document | string };
327
- deleteOne?: { filter: Document; hint?: Document | string };
328
- deleteMany?: { filter: Document; hint?: Document | string };
329
- }
330
-
331
- export interface IBulkWriteResult {
332
- acknowledged: boolean;
333
- insertedCount: number;
334
- matchedCount: number;
335
- modifiedCount: number;
336
- deletedCount: number;
337
- upsertedCount: number;
338
- insertedIds: Record<number, plugins.bson.ObjectId>;
339
- upsertedIds: Record<number, plugins.bson.ObjectId>;
340
- }
341
-
342
- // ============================================================================
343
- // Storage Types
344
- // ============================================================================
345
-
346
- export interface IStoredDocument extends Document {
347
- _id: plugins.bson.ObjectId;
348
- }
349
-
350
- export interface IOpLogEntry {
351
- ts: plugins.bson.Timestamp;
352
- op: 'i' | 'u' | 'd' | 'c' | 'n';
353
- ns: string;
354
- o: Document;
355
- o2?: Document;
356
- txnNumber?: number;
357
- lsid?: { id: plugins.bson.Binary };
358
- }
359
-
360
- // ============================================================================
361
- // Admin Types
362
- // ============================================================================
363
-
364
- export interface IDatabaseInfo {
365
- name: string;
366
- sizeOnDisk: number;
367
- empty: boolean;
368
- }
369
-
370
- export interface ICollectionInfo {
371
- name: string;
372
- type: 'collection' | 'view';
373
- options: Document;
374
- info: {
375
- readOnly: boolean;
376
- uuid?: plugins.bson.Binary;
377
- };
378
- idIndex?: IIndexInfo;
379
- }
380
-
381
- export interface IServerStatus {
382
- host: string;
383
- version: string;
384
- process: string;
385
- pid: number;
386
- uptime: number;
387
- uptimeMillis: number;
388
- uptimeEstimate: number;
389
- localTime: Date;
390
- mem: {
391
- resident: number;
392
- virtual: number;
393
- };
394
- connections: {
395
- current: number;
396
- available: number;
397
- totalCreated: number;
398
- };
399
- ok: 1;
400
- }
401
-
402
- export interface ICollectionStats {
403
- ns: string;
404
- count: number;
405
- size: number;
406
- avgObjSize: number;
407
- storageSize: number;
408
- totalIndexSize: number;
409
- indexSizes: Record<string, number>;
410
- nindexes: number;
411
- ok: 1;
412
- }
413
-
414
- // ============================================================================
415
- // Count Types
416
- // ============================================================================
417
-
418
- export interface ICountDocumentsOptions {
419
- skip?: number;
420
- limit?: number;
421
- session?: IClientSession;
422
- hint?: string | Document;
423
- maxTimeMS?: number;
424
- }
425
-
426
- export interface IEstimatedDocumentCountOptions {
427
- maxTimeMS?: number;
428
- }
429
-
430
- export interface IDistinctOptions {
431
- session?: IClientSession;
432
- maxTimeMS?: number;
433
- }
@@ -1,88 +0,0 @@
1
- /**
2
- * CRC32 checksum utilities for data integrity
3
- */
4
-
5
- // CRC32 lookup table
6
- const CRC32_TABLE: number[] = [];
7
-
8
- // Initialize the CRC32 table
9
- function initCRC32Table(): void {
10
- if (CRC32_TABLE.length > 0) return;
11
-
12
- for (let i = 0; i < 256; i++) {
13
- let crc = i;
14
- for (let j = 0; j < 8; j++) {
15
- crc = (crc & 1) ? (0xEDB88320 ^ (crc >>> 1)) : (crc >>> 1);
16
- }
17
- CRC32_TABLE[i] = crc >>> 0;
18
- }
19
- }
20
-
21
- /**
22
- * Calculate CRC32 checksum for a string
23
- */
24
- export function calculateCRC32(data: string): number {
25
- initCRC32Table();
26
-
27
- let crc = 0xFFFFFFFF;
28
- for (let i = 0; i < data.length; i++) {
29
- const byte = data.charCodeAt(i) & 0xFF;
30
- crc = CRC32_TABLE[(crc ^ byte) & 0xFF] ^ (crc >>> 8);
31
- }
32
- return (~crc) >>> 0;
33
- }
34
-
35
- /**
36
- * Calculate CRC32 checksum for a Buffer
37
- */
38
- export function calculateCRC32Buffer(data: Buffer): number {
39
- initCRC32Table();
40
-
41
- let crc = 0xFFFFFFFF;
42
- for (let i = 0; i < data.length; i++) {
43
- crc = CRC32_TABLE[(crc ^ data[i]) & 0xFF] ^ (crc >>> 8);
44
- }
45
- return (~crc) >>> 0;
46
- }
47
-
48
- /**
49
- * Calculate checksum for a document (serialized as JSON)
50
- */
51
- export function calculateDocumentChecksum(doc: Record<string, any>): number {
52
- // Exclude _checksum field from calculation
53
- const { _checksum, ...docWithoutChecksum } = doc;
54
- const json = JSON.stringify(docWithoutChecksum);
55
- return calculateCRC32(json);
56
- }
57
-
58
- /**
59
- * Add checksum to a document
60
- */
61
- export function addChecksum<T extends Record<string, any>>(doc: T): T & { _checksum: number } {
62
- const checksum = calculateDocumentChecksum(doc);
63
- return { ...doc, _checksum: checksum };
64
- }
65
-
66
- /**
67
- * Verify checksum of a document
68
- * Returns true if checksum is valid or if document has no checksum
69
- */
70
- export function verifyChecksum(doc: Record<string, any>): boolean {
71
- if (!('_checksum' in doc)) {
72
- // No checksum to verify
73
- return true;
74
- }
75
-
76
- const storedChecksum = doc._checksum;
77
- const calculatedChecksum = calculateDocumentChecksum(doc);
78
-
79
- return storedChecksum === calculatedChecksum;
80
- }
81
-
82
- /**
83
- * Remove checksum from a document
84
- */
85
- export function removeChecksum<T extends Record<string, any>>(doc: T): Omit<T, '_checksum'> {
86
- const { _checksum, ...docWithoutChecksum } = doc;
87
- return docWithoutChecksum as Omit<T, '_checksum'>;
88
- }
@@ -1 +0,0 @@
1
- export * from './checksum.js';