@topgunbuild/core 0.2.0-alpha → 0.2.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.
package/dist/index.d.mts CHANGED
@@ -403,29 +403,59 @@ declare function hashORMapRecord<V>(record: ORMapRecord<V>): number;
403
403
  declare function compareTimestamps(a: Timestamp, b: Timestamp): number;
404
404
 
405
405
  /**
406
- * FNV-1a Hash implementation for strings.
407
- * Fast, non-cryptographic, synchronous.
408
- * Good enough for data synchronization checks.
406
+ * Hash utilities for TopGun
407
+ *
408
+ * Uses native xxHash64 when available (via @topgunbuild/native),
409
+ * falls back to FNV-1a for JS-only environments.
410
+ *
411
+ * Phase 3.05: Native Hash Integration
412
+ */
413
+ /**
414
+ * Hash a string to a 32-bit unsigned integer.
415
+ *
416
+ * Uses native xxHash64 (truncated to 32 bits) when available,
417
+ * otherwise falls back to FNV-1a.
418
+ *
419
+ * @param str - String to hash
420
+ * @returns 32-bit unsigned integer hash
409
421
  */
410
422
  declare function hashString(str: string): number;
411
423
  /**
412
424
  * Combines multiple hash numbers into one order-independent hash.
413
- * Used for combining bucket hashes.
414
- * XOR is simple and effective for order-independent combination,
415
- * but for Merkle trees we usually want position dependence if it's a Trie,
416
- * or order-independence if it's a Set.
417
- * Here we simply sum or XOR.
425
+ * Used for combining bucket hashes in Merkle trees.
426
+ *
427
+ * Uses simple sum (with overflow handling) for order-independence.
428
+ *
429
+ * @param hashes - Array of hash values to combine
430
+ * @returns Combined hash as 32-bit unsigned integer
418
431
  */
419
432
  declare function combineHashes(hashes: number[]): number;
433
+ /**
434
+ * Check if native hash module is being used.
435
+ * Useful for diagnostics and testing.
436
+ */
437
+ declare function isUsingNativeHash(): boolean;
438
+ /**
439
+ * Force use of FNV-1a hash (for testing/compatibility).
440
+ * After calling this, hashString will always use FNV-1a.
441
+ */
442
+ declare function disableNativeHash(): void;
443
+ /**
444
+ * Re-enable native hash loading (for testing).
445
+ * Resets the load state so native module can be loaded again.
446
+ */
447
+ declare function resetNativeHash(): void;
420
448
 
421
449
  /**
422
450
  * Serializes a JavaScript object to MessagePack binary format.
451
+ * Uses msgpackr for 2-5x faster serialization compared to @msgpack/msgpack.
423
452
  * @param data The data to serialize.
424
453
  * @returns A Uint8Array containing the serialized data.
425
454
  */
426
455
  declare function serialize(data: unknown): Uint8Array;
427
456
  /**
428
457
  * Deserializes MessagePack binary data to a JavaScript object.
458
+ * Uses msgpackr for 2-5x faster deserialization compared to @msgpack/msgpack.
429
459
  * @param data The binary data to deserialize (Uint8Array or ArrayBuffer).
430
460
  * @returns The deserialized object.
431
461
  */
@@ -467,6 +497,17 @@ interface Principal {
467
497
  [key: string]: any;
468
498
  }
469
499
 
500
+ /**
501
+ * Write Concern schema - defines when an operation is considered acknowledged.
502
+ */
503
+ declare const WriteConcernSchema: z.ZodEnum<{
504
+ FIRE_AND_FORGET: "FIRE_AND_FORGET";
505
+ MEMORY: "MEMORY";
506
+ APPLIED: "APPLIED";
507
+ REPLICATED: "REPLICATED";
508
+ PERSISTED: "PERSISTED";
509
+ }>;
510
+ type WriteConcernValue = z.infer<typeof WriteConcernSchema>;
470
511
  declare const TimestampSchema: z.ZodObject<{
471
512
  millis: z.ZodNumber;
472
513
  counter: z.ZodNumber;
@@ -540,6 +581,14 @@ declare const ClientOpSchema: z.ZodObject<{
540
581
  ttlMs: z.ZodOptional<z.ZodNumber>;
541
582
  }, z.core.$strip>>>;
542
583
  orTag: z.ZodOptional<z.ZodNullable<z.ZodString>>;
584
+ writeConcern: z.ZodOptional<z.ZodEnum<{
585
+ FIRE_AND_FORGET: "FIRE_AND_FORGET";
586
+ MEMORY: "MEMORY";
587
+ APPLIED: "APPLIED";
588
+ REPLICATED: "REPLICATED";
589
+ PERSISTED: "PERSISTED";
590
+ }>>;
591
+ timeout: z.ZodOptional<z.ZodNumber>;
543
592
  }, z.core.$strip>;
544
593
  declare const AuthMessageSchema: z.ZodObject<{
545
594
  type: z.ZodLiteral<"AUTH">;
@@ -595,6 +644,14 @@ declare const ClientOpMessageSchema: z.ZodObject<{
595
644
  ttlMs: z.ZodOptional<z.ZodNumber>;
596
645
  }, z.core.$strip>>>;
597
646
  orTag: z.ZodOptional<z.ZodNullable<z.ZodString>>;
647
+ writeConcern: z.ZodOptional<z.ZodEnum<{
648
+ FIRE_AND_FORGET: "FIRE_AND_FORGET";
649
+ MEMORY: "MEMORY";
650
+ APPLIED: "APPLIED";
651
+ REPLICATED: "REPLICATED";
652
+ PERSISTED: "PERSISTED";
653
+ }>>;
654
+ timeout: z.ZodOptional<z.ZodNumber>;
598
655
  }, z.core.$strip>;
599
656
  }, z.core.$strip>;
600
657
  declare const OpBatchMessageSchema: z.ZodObject<{
@@ -625,7 +682,23 @@ declare const OpBatchMessageSchema: z.ZodObject<{
625
682
  ttlMs: z.ZodOptional<z.ZodNumber>;
626
683
  }, z.core.$strip>>>;
627
684
  orTag: z.ZodOptional<z.ZodNullable<z.ZodString>>;
685
+ writeConcern: z.ZodOptional<z.ZodEnum<{
686
+ FIRE_AND_FORGET: "FIRE_AND_FORGET";
687
+ MEMORY: "MEMORY";
688
+ APPLIED: "APPLIED";
689
+ REPLICATED: "REPLICATED";
690
+ PERSISTED: "PERSISTED";
691
+ }>>;
692
+ timeout: z.ZodOptional<z.ZodNumber>;
628
693
  }, z.core.$strip>>;
694
+ writeConcern: z.ZodOptional<z.ZodEnum<{
695
+ FIRE_AND_FORGET: "FIRE_AND_FORGET";
696
+ MEMORY: "MEMORY";
697
+ APPLIED: "APPLIED";
698
+ REPLICATED: "REPLICATED";
699
+ PERSISTED: "PERSISTED";
700
+ }>>;
701
+ timeout: z.ZodOptional<z.ZodNumber>;
629
702
  }, z.core.$strip>;
630
703
  }, z.core.$strip>;
631
704
  declare const SyncInitMessageSchema: z.ZodObject<{
@@ -732,6 +805,16 @@ declare const PongMessageSchema: z.ZodObject<{
732
805
  timestamp: z.ZodNumber;
733
806
  serverTime: z.ZodNumber;
734
807
  }, z.core.$strip>;
808
+ /**
809
+ * BATCH: Server sends multiple messages batched together.
810
+ * Uses length-prefixed binary format for efficiency.
811
+ * Format: [4 bytes: count][4 bytes: len1][msg1][4 bytes: len2][msg2]...
812
+ */
813
+ declare const BatchMessageSchema: z.ZodObject<{
814
+ type: z.ZodLiteral<"BATCH">;
815
+ count: z.ZodNumber;
816
+ data: z.ZodCustom<Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>>;
817
+ }, z.core.$strip>;
735
818
  /**
736
819
  * ORMAP_SYNC_INIT: Client initiates ORMap sync
737
820
  * Sends root hash and bucket hashes to server
@@ -859,6 +942,61 @@ declare const ORMapPushDiffSchema: z.ZodObject<{
859
942
  }, z.core.$strip>>;
860
943
  }, z.core.$strip>;
861
944
  }, z.core.$strip>;
945
+ /**
946
+ * Individual operation result within a batch ACK
947
+ */
948
+ declare const OpResultSchema: z.ZodObject<{
949
+ opId: z.ZodString;
950
+ success: z.ZodBoolean;
951
+ achievedLevel: z.ZodEnum<{
952
+ FIRE_AND_FORGET: "FIRE_AND_FORGET";
953
+ MEMORY: "MEMORY";
954
+ APPLIED: "APPLIED";
955
+ REPLICATED: "REPLICATED";
956
+ PERSISTED: "PERSISTED";
957
+ }>;
958
+ error: z.ZodOptional<z.ZodString>;
959
+ }, z.core.$strip>;
960
+ /**
961
+ * OP_ACK: Server acknowledges write operations
962
+ * Extended to support Write Concern levels
963
+ */
964
+ declare const OpAckMessageSchema: z.ZodObject<{
965
+ type: z.ZodLiteral<"OP_ACK">;
966
+ payload: z.ZodObject<{
967
+ lastId: z.ZodString;
968
+ achievedLevel: z.ZodOptional<z.ZodEnum<{
969
+ FIRE_AND_FORGET: "FIRE_AND_FORGET";
970
+ MEMORY: "MEMORY";
971
+ APPLIED: "APPLIED";
972
+ REPLICATED: "REPLICATED";
973
+ PERSISTED: "PERSISTED";
974
+ }>>;
975
+ results: z.ZodOptional<z.ZodArray<z.ZodObject<{
976
+ opId: z.ZodString;
977
+ success: z.ZodBoolean;
978
+ achievedLevel: z.ZodEnum<{
979
+ FIRE_AND_FORGET: "FIRE_AND_FORGET";
980
+ MEMORY: "MEMORY";
981
+ APPLIED: "APPLIED";
982
+ REPLICATED: "REPLICATED";
983
+ PERSISTED: "PERSISTED";
984
+ }>;
985
+ error: z.ZodOptional<z.ZodString>;
986
+ }, z.core.$strip>>>;
987
+ }, z.core.$strip>;
988
+ }, z.core.$strip>;
989
+ /**
990
+ * OP_REJECTED: Server rejects a write operation
991
+ */
992
+ declare const OpRejectedMessageSchema: z.ZodObject<{
993
+ type: z.ZodLiteral<"OP_REJECTED">;
994
+ payload: z.ZodObject<{
995
+ opId: z.ZodString;
996
+ reason: z.ZodString;
997
+ code: z.ZodOptional<z.ZodNumber>;
998
+ }, z.core.$strip>;
999
+ }, z.core.$strip>;
862
1000
  declare const MessageSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
863
1001
  type: z.ZodLiteral<"AUTH">;
864
1002
  token: z.ZodString;
@@ -910,6 +1048,14 @@ declare const MessageSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
910
1048
  ttlMs: z.ZodOptional<z.ZodNumber>;
911
1049
  }, z.core.$strip>>>;
912
1050
  orTag: z.ZodOptional<z.ZodNullable<z.ZodString>>;
1051
+ writeConcern: z.ZodOptional<z.ZodEnum<{
1052
+ FIRE_AND_FORGET: "FIRE_AND_FORGET";
1053
+ MEMORY: "MEMORY";
1054
+ APPLIED: "APPLIED";
1055
+ REPLICATED: "REPLICATED";
1056
+ PERSISTED: "PERSISTED";
1057
+ }>>;
1058
+ timeout: z.ZodOptional<z.ZodNumber>;
913
1059
  }, z.core.$strip>;
914
1060
  }, z.core.$strip>, z.ZodObject<{
915
1061
  type: z.ZodLiteral<"OP_BATCH">;
@@ -939,7 +1085,23 @@ declare const MessageSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
939
1085
  ttlMs: z.ZodOptional<z.ZodNumber>;
940
1086
  }, z.core.$strip>>>;
941
1087
  orTag: z.ZodOptional<z.ZodNullable<z.ZodString>>;
1088
+ writeConcern: z.ZodOptional<z.ZodEnum<{
1089
+ FIRE_AND_FORGET: "FIRE_AND_FORGET";
1090
+ MEMORY: "MEMORY";
1091
+ APPLIED: "APPLIED";
1092
+ REPLICATED: "REPLICATED";
1093
+ PERSISTED: "PERSISTED";
1094
+ }>>;
1095
+ timeout: z.ZodOptional<z.ZodNumber>;
942
1096
  }, z.core.$strip>>;
1097
+ writeConcern: z.ZodOptional<z.ZodEnum<{
1098
+ FIRE_AND_FORGET: "FIRE_AND_FORGET";
1099
+ MEMORY: "MEMORY";
1100
+ APPLIED: "APPLIED";
1101
+ REPLICATED: "REPLICATED";
1102
+ PERSISTED: "PERSISTED";
1103
+ }>>;
1104
+ timeout: z.ZodOptional<z.ZodNumber>;
943
1105
  }, z.core.$strip>;
944
1106
  }, z.core.$strip>, z.ZodObject<{
945
1107
  type: z.ZodLiteral<"SYNC_INIT">;
@@ -1124,5 +1286,135 @@ type ClientOp = z.infer<typeof ClientOpSchema>;
1124
1286
  type Message = z.infer<typeof MessageSchema>;
1125
1287
  type PingMessage = z.infer<typeof PingMessageSchema>;
1126
1288
  type PongMessage = z.infer<typeof PongMessageSchema>;
1289
+ type BatchMessage = z.infer<typeof BatchMessageSchema>;
1290
+ type OpAckMessage = z.infer<typeof OpAckMessageSchema>;
1291
+ type OpRejectedMessage = z.infer<typeof OpRejectedMessageSchema>;
1292
+ type OpResult = z.infer<typeof OpResultSchema>;
1293
+
1294
+ /**
1295
+ * Write Concern - Configurable Acknowledgment Levels
1296
+ *
1297
+ * Write Concern defines when a write operation is considered successful.
1298
+ * Similar to MongoDB's writeConcern, Kafka's acks, and Cassandra's consistency levels.
1299
+ */
1300
+ /**
1301
+ * Write Concern levels - determines when an operation is acknowledged.
1302
+ *
1303
+ * Levels are ordered by durability guarantee (lowest to highest):
1304
+ * FIRE_AND_FORGET < MEMORY < APPLIED < REPLICATED < PERSISTED
1305
+ */
1306
+ declare enum WriteConcern {
1307
+ /**
1308
+ * FIRE_AND_FORGET (acks=0)
1309
+ * - ACK sent immediately after server receives the message
1310
+ * - Operation may be lost if server crashes before processing
1311
+ * - Maximum throughput, minimum latency
1312
+ * - Use case: metrics, logs, non-critical data
1313
+ */
1314
+ FIRE_AND_FORGET = "FIRE_AND_FORGET",
1315
+ /**
1316
+ * MEMORY (acks=1, default) - current Early ACK behavior
1317
+ * - ACK sent after validation and queuing for processing
1318
+ * - Operation is in memory but not yet applied to CRDT
1319
+ * - Use case: most operations, real-time updates
1320
+ */
1321
+ MEMORY = "MEMORY",
1322
+ /**
1323
+ * APPLIED
1324
+ * - ACK sent after operation is applied to CRDT in memory
1325
+ * - Data is readable on this node immediately after ACK
1326
+ * - Use case: operations requiring immediate consistency on the node
1327
+ */
1328
+ APPLIED = "APPLIED",
1329
+ /**
1330
+ * REPLICATED
1331
+ * - ACK sent after operation is broadcast to cluster (CLUSTER_EVENT sent)
1332
+ * - Data survives primary node failure
1333
+ * - Use case: important data requiring cluster durability
1334
+ */
1335
+ REPLICATED = "REPLICATED",
1336
+ /**
1337
+ * PERSISTED
1338
+ * - ACK sent after operation is written to storage on primary node
1339
+ * - Data survives node restart
1340
+ * - Use case: critical data (financial transactions, audit logs)
1341
+ */
1342
+ PERSISTED = "PERSISTED"
1343
+ }
1344
+ /**
1345
+ * Default timeout for Write Concern acknowledgments (ms)
1346
+ */
1347
+ declare const DEFAULT_WRITE_CONCERN_TIMEOUT = 5000;
1348
+ /**
1349
+ * Write options for PUT/REMOVE operations
1350
+ */
1351
+ interface WriteOptions {
1352
+ /**
1353
+ * Write acknowledgment level.
1354
+ * @default WriteConcern.MEMORY
1355
+ */
1356
+ writeConcern?: WriteConcern;
1357
+ /**
1358
+ * Timeout for waiting for acknowledgment (ms).
1359
+ * Applies to APPLIED, REPLICATED, PERSISTED levels.
1360
+ * @default 5000
1361
+ */
1362
+ timeout?: number;
1363
+ }
1364
+ /**
1365
+ * Result of a write operation
1366
+ */
1367
+ interface WriteResult {
1368
+ /** Whether the operation achieved the requested Write Concern level */
1369
+ success: boolean;
1370
+ /** Operation ID */
1371
+ opId: string;
1372
+ /** The Write Concern level actually achieved */
1373
+ achievedLevel: WriteConcern;
1374
+ /** Time taken to achieve the level (ms) */
1375
+ latencyMs: number;
1376
+ /** Error message if success=false */
1377
+ error?: string;
1378
+ }
1379
+ /**
1380
+ * Internal structure for tracking pending write acknowledgments
1381
+ */
1382
+ interface PendingWrite {
1383
+ /** Operation ID */
1384
+ opId: string;
1385
+ /** Target Write Concern level */
1386
+ writeConcern: WriteConcern;
1387
+ /** Timestamp when operation was registered */
1388
+ timestamp: number;
1389
+ /** Timeout duration (ms) */
1390
+ timeout: number;
1391
+ /** Promise resolve callback */
1392
+ resolve: (result: WriteResult) => void;
1393
+ /** Promise reject callback */
1394
+ reject: (error: Error) => void;
1395
+ /** Timeout handle for cleanup */
1396
+ timeoutHandle?: ReturnType<typeof setTimeout>;
1397
+ /** Set of levels that have been achieved */
1398
+ achievedLevels: Set<WriteConcern>;
1399
+ }
1400
+ /**
1401
+ * Ordered list of Write Concern levels (lowest to highest)
1402
+ */
1403
+ declare const WRITE_CONCERN_ORDER: readonly WriteConcern[];
1404
+ /**
1405
+ * Check if a target Write Concern level is achieved based on achieved levels.
1406
+ *
1407
+ * @param achieved - Set of achieved Write Concern levels
1408
+ * @param target - Target Write Concern level to check
1409
+ * @returns true if target level (or higher) is achieved
1410
+ */
1411
+ declare function isWriteConcernAchieved(achieved: Set<WriteConcern>, target: WriteConcern): boolean;
1412
+ /**
1413
+ * Get the highest achieved Write Concern level from a set of achieved levels.
1414
+ *
1415
+ * @param achieved - Set of achieved Write Concern levels
1416
+ * @returns The highest achieved level
1417
+ */
1418
+ declare function getHighestWriteConcernLevel(achieved: Set<WriteConcern>): WriteConcern;
1127
1419
 
1128
- export { AuthMessageSchema, type ClientOp, ClientOpMessageSchema, ClientOpSchema, HLC, LWWMap, type LWWRecord, LWWRecordSchema, LockReleaseSchema, LockRequestSchema, type MergeKeyResult, MerkleReqBucketMessageSchema, MerkleTree, type Message, MessageSchema, ORMap, ORMapDiffRequestSchema, ORMapDiffResponseSchema, type ORMapMerkleNode, ORMapMerkleReqBucketSchema, ORMapMerkleTree, ORMapPushDiffSchema, type ORMapRecord, ORMapRecordSchema, type ORMapSnapshot, ORMapSyncInitSchema, ORMapSyncRespBucketsSchema, ORMapSyncRespLeafSchema, ORMapSyncRespRootSchema, OpBatchMessageSchema, type PermissionPolicy, type PermissionType, type PingMessage, PingMessageSchema, type PongMessage, PongMessageSchema, type PredicateNode, PredicateNodeSchema, type PredicateOp, PredicateOpSchema, Predicates, type Principal, type Query, QuerySchema, QuerySubMessageSchema, QueryUnsubMessageSchema, SyncInitMessageSchema, SyncRespBucketsMessageSchema, SyncRespLeafMessageSchema, SyncRespRootMessageSchema, type Timestamp, TimestampSchema, TopicMessageEventSchema, TopicPubSchema, TopicSubSchema, TopicUnsubSchema, combineHashes, compareTimestamps, deserialize, evaluatePredicate, hashORMapEntry, hashORMapRecord, hashString, serialize, timestampToString };
1420
+ export { AuthMessageSchema, type BatchMessage, BatchMessageSchema, type ClientOp, ClientOpMessageSchema, ClientOpSchema, DEFAULT_WRITE_CONCERN_TIMEOUT, HLC, LWWMap, type LWWRecord, LWWRecordSchema, LockReleaseSchema, LockRequestSchema, type MergeKeyResult, MerkleReqBucketMessageSchema, MerkleTree, type Message, MessageSchema, ORMap, ORMapDiffRequestSchema, ORMapDiffResponseSchema, type ORMapMerkleNode, ORMapMerkleReqBucketSchema, ORMapMerkleTree, ORMapPushDiffSchema, type ORMapRecord, ORMapRecordSchema, type ORMapSnapshot, ORMapSyncInitSchema, ORMapSyncRespBucketsSchema, ORMapSyncRespLeafSchema, ORMapSyncRespRootSchema, type OpAckMessage, OpAckMessageSchema, OpBatchMessageSchema, type OpRejectedMessage, OpRejectedMessageSchema, type OpResult, OpResultSchema, type PendingWrite, type PermissionPolicy, type PermissionType, type PingMessage, PingMessageSchema, type PongMessage, PongMessageSchema, type PredicateNode, PredicateNodeSchema, type PredicateOp, PredicateOpSchema, Predicates, type Principal, type Query, QuerySchema, QuerySubMessageSchema, QueryUnsubMessageSchema, SyncInitMessageSchema, SyncRespBucketsMessageSchema, SyncRespLeafMessageSchema, SyncRespRootMessageSchema, type Timestamp, TimestampSchema, TopicMessageEventSchema, TopicPubSchema, TopicSubSchema, TopicUnsubSchema, WRITE_CONCERN_ORDER, WriteConcern, WriteConcernSchema, type WriteConcernValue, type WriteOptions, type WriteResult, combineHashes, compareTimestamps, deserialize, disableNativeHash, evaluatePredicate, getHighestWriteConcernLevel, hashORMapEntry, hashORMapRecord, hashString, isUsingNativeHash, isWriteConcernAchieved, resetNativeHash, serialize, timestampToString };