@topgunbuild/core 0.2.0 → 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.ts 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 };
package/dist/index.js CHANGED
@@ -21,8 +21,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  AuthMessageSchema: () => AuthMessageSchema,
24
+ BatchMessageSchema: () => BatchMessageSchema,
24
25
  ClientOpMessageSchema: () => ClientOpMessageSchema,
25
26
  ClientOpSchema: () => ClientOpSchema,
27
+ DEFAULT_WRITE_CONCERN_TIMEOUT: () => DEFAULT_WRITE_CONCERN_TIMEOUT,
26
28
  HLC: () => HLC,
27
29
  LWWMap: () => LWWMap,
28
30
  LWWRecordSchema: () => LWWRecordSchema,
@@ -42,7 +44,10 @@ __export(index_exports, {
42
44
  ORMapSyncRespBucketsSchema: () => ORMapSyncRespBucketsSchema,
43
45
  ORMapSyncRespLeafSchema: () => ORMapSyncRespLeafSchema,
44
46
  ORMapSyncRespRootSchema: () => ORMapSyncRespRootSchema,
47
+ OpAckMessageSchema: () => OpAckMessageSchema,
45
48
  OpBatchMessageSchema: () => OpBatchMessageSchema,
49
+ OpRejectedMessageSchema: () => OpRejectedMessageSchema,
50
+ OpResultSchema: () => OpResultSchema,
46
51
  PingMessageSchema: () => PingMessageSchema,
47
52
  PongMessageSchema: () => PongMessageSchema,
48
53
  PredicateNodeSchema: () => PredicateNodeSchema,
@@ -60,13 +65,21 @@ __export(index_exports, {
60
65
  TopicPubSchema: () => TopicPubSchema,
61
66
  TopicSubSchema: () => TopicSubSchema,
62
67
  TopicUnsubSchema: () => TopicUnsubSchema,
68
+ WRITE_CONCERN_ORDER: () => WRITE_CONCERN_ORDER,
69
+ WriteConcern: () => WriteConcern,
70
+ WriteConcernSchema: () => WriteConcernSchema,
63
71
  combineHashes: () => combineHashes,
64
72
  compareTimestamps: () => compareTimestamps,
65
73
  deserialize: () => deserialize,
74
+ disableNativeHash: () => disableNativeHash,
66
75
  evaluatePredicate: () => evaluatePredicate,
76
+ getHighestWriteConcernLevel: () => getHighestWriteConcernLevel,
67
77
  hashORMapEntry: () => hashORMapEntry,
68
78
  hashORMapRecord: () => hashORMapRecord,
69
79
  hashString: () => hashString,
80
+ isUsingNativeHash: () => isUsingNativeHash,
81
+ isWriteConcernAchieved: () => isWriteConcernAchieved,
82
+ resetNativeHash: () => resetNativeHash,
70
83
  serialize: () => serialize,
71
84
  timestampToString: () => timestampToString
72
85
  });
@@ -161,7 +174,17 @@ _HLC.MAX_DRIFT = 6e4;
161
174
  var HLC = _HLC;
162
175
 
163
176
  // src/utils/hash.ts
164
- function hashString(str) {
177
+ var nativeHash = null;
178
+ var nativeLoadAttempted = false;
179
+ function tryLoadNative() {
180
+ if (nativeLoadAttempted) return;
181
+ nativeLoadAttempted = true;
182
+ try {
183
+ nativeHash = require("@topgunbuild/native");
184
+ } catch {
185
+ }
186
+ }
187
+ function fnv1aHash(str) {
165
188
  let hash = 2166136261;
166
189
  for (let i = 0; i < str.length; i++) {
167
190
  hash ^= str.charCodeAt(i);
@@ -169,6 +192,13 @@ function hashString(str) {
169
192
  }
170
193
  return hash >>> 0;
171
194
  }
195
+ function hashString(str) {
196
+ tryLoadNative();
197
+ if (nativeHash && nativeHash.isNativeHashAvailable()) {
198
+ return nativeHash.hashString(str);
199
+ }
200
+ return fnv1aHash(str);
201
+ }
172
202
  function combineHashes(hashes) {
173
203
  let result = 0;
174
204
  for (const h of hashes) {
@@ -176,6 +206,18 @@ function combineHashes(hashes) {
176
206
  }
177
207
  return result >>> 0;
178
208
  }
209
+ function isUsingNativeHash() {
210
+ tryLoadNative();
211
+ return nativeHash?.isNativeHashAvailable() === true;
212
+ }
213
+ function disableNativeHash() {
214
+ nativeHash = null;
215
+ nativeLoadAttempted = true;
216
+ }
217
+ function resetNativeHash() {
218
+ nativeHash = null;
219
+ nativeLoadAttempted = false;
220
+ }
179
221
 
180
222
  // src/MerkleTree.ts
181
223
  var MerkleTree = class {
@@ -999,12 +1041,13 @@ var ORMap = class {
999
1041
  };
1000
1042
 
1001
1043
  // src/serializer.ts
1002
- var import_msgpack = require("@msgpack/msgpack");
1044
+ var import_msgpackr = require("msgpackr");
1003
1045
  function serialize(data) {
1004
- return (0, import_msgpack.encode)(data);
1046
+ return (0, import_msgpackr.pack)(data);
1005
1047
  }
1006
1048
  function deserialize(data) {
1007
- return (0, import_msgpack.decode)(data);
1049
+ const buffer = data instanceof ArrayBuffer ? new Uint8Array(data) : data;
1050
+ return (0, import_msgpackr.unpack)(buffer);
1008
1051
  }
1009
1052
 
1010
1053
  // src/predicate.ts
@@ -1095,6 +1138,13 @@ function evaluatePredicate(predicate, data) {
1095
1138
 
1096
1139
  // src/schemas.ts
1097
1140
  var import_zod = require("zod");
1141
+ var WriteConcernSchema = import_zod.z.enum([
1142
+ "FIRE_AND_FORGET",
1143
+ "MEMORY",
1144
+ "APPLIED",
1145
+ "REPLICATED",
1146
+ "PERSISTED"
1147
+ ]);
1098
1148
  var TimestampSchema = import_zod.z.object({
1099
1149
  millis: import_zod.z.number(),
1100
1150
  counter: import_zod.z.number(),
@@ -1146,7 +1196,10 @@ var ClientOpSchema = import_zod.z.object({
1146
1196
  opType: import_zod.z.string().optional(),
1147
1197
  record: LWWRecordSchema.nullable().optional(),
1148
1198
  orRecord: ORMapRecordSchema.nullable().optional(),
1149
- orTag: import_zod.z.string().nullable().optional()
1199
+ orTag: import_zod.z.string().nullable().optional(),
1200
+ // Write Concern fields (Phase 5.01)
1201
+ writeConcern: WriteConcernSchema.optional(),
1202
+ timeout: import_zod.z.number().optional()
1150
1203
  });
1151
1204
  var AuthMessageSchema = import_zod.z.object({
1152
1205
  type: import_zod.z.literal("AUTH"),
@@ -1173,7 +1226,10 @@ var ClientOpMessageSchema = import_zod.z.object({
1173
1226
  var OpBatchMessageSchema = import_zod.z.object({
1174
1227
  type: import_zod.z.literal("OP_BATCH"),
1175
1228
  payload: import_zod.z.object({
1176
- ops: import_zod.z.array(ClientOpSchema)
1229
+ ops: import_zod.z.array(ClientOpSchema),
1230
+ // Batch-level Write Concern (can be overridden per-op)
1231
+ writeConcern: WriteConcernSchema.optional(),
1232
+ timeout: import_zod.z.number().optional()
1177
1233
  })
1178
1234
  });
1179
1235
  var SyncInitMessageSchema = import_zod.z.object({
@@ -1271,6 +1327,11 @@ var PongMessageSchema = import_zod.z.object({
1271
1327
  serverTime: import_zod.z.number()
1272
1328
  // Server's Date.now() (for clock skew detection)
1273
1329
  });
1330
+ var BatchMessageSchema = import_zod.z.object({
1331
+ type: import_zod.z.literal("BATCH"),
1332
+ count: import_zod.z.number(),
1333
+ data: import_zod.z.instanceof(Uint8Array)
1334
+ });
1274
1335
  var ORMapSyncInitSchema = import_zod.z.object({
1275
1336
  type: import_zod.z.literal("ORMAP_SYNC_INIT"),
1276
1337
  mapName: import_zod.z.string(),
@@ -1344,6 +1405,34 @@ var ORMapPushDiffSchema = import_zod.z.object({
1344
1405
  }))
1345
1406
  })
1346
1407
  });
1408
+ var OpResultSchema = import_zod.z.object({
1409
+ opId: import_zod.z.string(),
1410
+ success: import_zod.z.boolean(),
1411
+ achievedLevel: WriteConcernSchema,
1412
+ error: import_zod.z.string().optional()
1413
+ });
1414
+ var OpAckMessageSchema = import_zod.z.object({
1415
+ type: import_zod.z.literal("OP_ACK"),
1416
+ payload: import_zod.z.object({
1417
+ /** ID of the last operation in the batch (for backwards compatibility) */
1418
+ lastId: import_zod.z.string(),
1419
+ /** Write Concern level achieved (for simple ACKs) */
1420
+ achievedLevel: WriteConcernSchema.optional(),
1421
+ /** Per-operation results (for batch operations with mixed Write Concern) */
1422
+ results: import_zod.z.array(OpResultSchema).optional()
1423
+ })
1424
+ });
1425
+ var OpRejectedMessageSchema = import_zod.z.object({
1426
+ type: import_zod.z.literal("OP_REJECTED"),
1427
+ payload: import_zod.z.object({
1428
+ /** Operation ID that was rejected */
1429
+ opId: import_zod.z.string(),
1430
+ /** Reason for rejection */
1431
+ reason: import_zod.z.string(),
1432
+ /** Error code */
1433
+ code: import_zod.z.number().optional()
1434
+ })
1435
+ });
1347
1436
  var MessageSchema = import_zod.z.discriminatedUnion("type", [
1348
1437
  AuthMessageSchema,
1349
1438
  QuerySubMessageSchema,
@@ -1372,11 +1461,46 @@ var MessageSchema = import_zod.z.discriminatedUnion("type", [
1372
1461
  ORMapDiffResponseSchema,
1373
1462
  ORMapPushDiffSchema
1374
1463
  ]);
1464
+
1465
+ // src/types/WriteConcern.ts
1466
+ var WriteConcern = /* @__PURE__ */ ((WriteConcern2) => {
1467
+ WriteConcern2["FIRE_AND_FORGET"] = "FIRE_AND_FORGET";
1468
+ WriteConcern2["MEMORY"] = "MEMORY";
1469
+ WriteConcern2["APPLIED"] = "APPLIED";
1470
+ WriteConcern2["REPLICATED"] = "REPLICATED";
1471
+ WriteConcern2["PERSISTED"] = "PERSISTED";
1472
+ return WriteConcern2;
1473
+ })(WriteConcern || {});
1474
+ var DEFAULT_WRITE_CONCERN_TIMEOUT = 5e3;
1475
+ var WRITE_CONCERN_ORDER = [
1476
+ "FIRE_AND_FORGET" /* FIRE_AND_FORGET */,
1477
+ "MEMORY" /* MEMORY */,
1478
+ "APPLIED" /* APPLIED */,
1479
+ "REPLICATED" /* REPLICATED */,
1480
+ "PERSISTED" /* PERSISTED */
1481
+ ];
1482
+ function isWriteConcernAchieved(achieved, target) {
1483
+ const targetIndex = WRITE_CONCERN_ORDER.indexOf(target);
1484
+ const achievedIndex = Math.max(
1485
+ ...Array.from(achieved).map((l) => WRITE_CONCERN_ORDER.indexOf(l))
1486
+ );
1487
+ return achievedIndex >= targetIndex;
1488
+ }
1489
+ function getHighestWriteConcernLevel(achieved) {
1490
+ for (let i = WRITE_CONCERN_ORDER.length - 1; i >= 0; i--) {
1491
+ if (achieved.has(WRITE_CONCERN_ORDER[i])) {
1492
+ return WRITE_CONCERN_ORDER[i];
1493
+ }
1494
+ }
1495
+ return "FIRE_AND_FORGET" /* FIRE_AND_FORGET */;
1496
+ }
1375
1497
  // Annotate the CommonJS export names for ESM import in node:
1376
1498
  0 && (module.exports = {
1377
1499
  AuthMessageSchema,
1500
+ BatchMessageSchema,
1378
1501
  ClientOpMessageSchema,
1379
1502
  ClientOpSchema,
1503
+ DEFAULT_WRITE_CONCERN_TIMEOUT,
1380
1504
  HLC,
1381
1505
  LWWMap,
1382
1506
  LWWRecordSchema,
@@ -1396,7 +1520,10 @@ var MessageSchema = import_zod.z.discriminatedUnion("type", [
1396
1520
  ORMapSyncRespBucketsSchema,
1397
1521
  ORMapSyncRespLeafSchema,
1398
1522
  ORMapSyncRespRootSchema,
1523
+ OpAckMessageSchema,
1399
1524
  OpBatchMessageSchema,
1525
+ OpRejectedMessageSchema,
1526
+ OpResultSchema,
1400
1527
  PingMessageSchema,
1401
1528
  PongMessageSchema,
1402
1529
  PredicateNodeSchema,
@@ -1414,13 +1541,21 @@ var MessageSchema = import_zod.z.discriminatedUnion("type", [
1414
1541
  TopicPubSchema,
1415
1542
  TopicSubSchema,
1416
1543
  TopicUnsubSchema,
1544
+ WRITE_CONCERN_ORDER,
1545
+ WriteConcern,
1546
+ WriteConcernSchema,
1417
1547
  combineHashes,
1418
1548
  compareTimestamps,
1419
1549
  deserialize,
1550
+ disableNativeHash,
1420
1551
  evaluatePredicate,
1552
+ getHighestWriteConcernLevel,
1421
1553
  hashORMapEntry,
1422
1554
  hashORMapRecord,
1423
1555
  hashString,
1556
+ isUsingNativeHash,
1557
+ isWriteConcernAchieved,
1558
+ resetNativeHash,
1424
1559
  serialize,
1425
1560
  timestampToString
1426
1561
  });