@topgunbuild/core 0.2.0 → 0.3.0

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.js CHANGED
@@ -21,8 +21,17 @@ 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
+ ConsistencyLevel: () => ConsistencyLevel,
28
+ DEFAULT_BACKUP_COUNT: () => DEFAULT_BACKUP_COUNT,
29
+ DEFAULT_CIRCUIT_BREAKER_CONFIG: () => DEFAULT_CIRCUIT_BREAKER_CONFIG,
30
+ DEFAULT_CONNECTION_POOL_CONFIG: () => DEFAULT_CONNECTION_POOL_CONFIG,
31
+ DEFAULT_MIGRATION_CONFIG: () => DEFAULT_MIGRATION_CONFIG,
32
+ DEFAULT_PARTITION_ROUTER_CONFIG: () => DEFAULT_PARTITION_ROUTER_CONFIG,
33
+ DEFAULT_REPLICATION_CONFIG: () => DEFAULT_REPLICATION_CONFIG,
34
+ DEFAULT_WRITE_CONCERN_TIMEOUT: () => DEFAULT_WRITE_CONCERN_TIMEOUT,
26
35
  HLC: () => HLC,
27
36
  LWWMap: () => LWWMap,
28
37
  LWWRecordSchema: () => LWWRecordSchema,
@@ -42,7 +51,13 @@ __export(index_exports, {
42
51
  ORMapSyncRespBucketsSchema: () => ORMapSyncRespBucketsSchema,
43
52
  ORMapSyncRespLeafSchema: () => ORMapSyncRespLeafSchema,
44
53
  ORMapSyncRespRootSchema: () => ORMapSyncRespRootSchema,
54
+ OpAckMessageSchema: () => OpAckMessageSchema,
45
55
  OpBatchMessageSchema: () => OpBatchMessageSchema,
56
+ OpRejectedMessageSchema: () => OpRejectedMessageSchema,
57
+ OpResultSchema: () => OpResultSchema,
58
+ PARTITION_COUNT: () => PARTITION_COUNT,
59
+ PartitionMapRequestSchema: () => PartitionMapRequestSchema,
60
+ PartitionState: () => PartitionState,
46
61
  PingMessageSchema: () => PingMessageSchema,
47
62
  PongMessageSchema: () => PongMessageSchema,
48
63
  PredicateNodeSchema: () => PredicateNodeSchema,
@@ -60,13 +75,21 @@ __export(index_exports, {
60
75
  TopicPubSchema: () => TopicPubSchema,
61
76
  TopicSubSchema: () => TopicSubSchema,
62
77
  TopicUnsubSchema: () => TopicUnsubSchema,
78
+ WRITE_CONCERN_ORDER: () => WRITE_CONCERN_ORDER,
79
+ WriteConcern: () => WriteConcern,
80
+ WriteConcernSchema: () => WriteConcernSchema,
63
81
  combineHashes: () => combineHashes,
64
82
  compareTimestamps: () => compareTimestamps,
65
83
  deserialize: () => deserialize,
84
+ disableNativeHash: () => disableNativeHash,
66
85
  evaluatePredicate: () => evaluatePredicate,
86
+ getHighestWriteConcernLevel: () => getHighestWriteConcernLevel,
67
87
  hashORMapEntry: () => hashORMapEntry,
68
88
  hashORMapRecord: () => hashORMapRecord,
69
89
  hashString: () => hashString,
90
+ isUsingNativeHash: () => isUsingNativeHash,
91
+ isWriteConcernAchieved: () => isWriteConcernAchieved,
92
+ resetNativeHash: () => resetNativeHash,
70
93
  serialize: () => serialize,
71
94
  timestampToString: () => timestampToString
72
95
  });
@@ -161,7 +184,17 @@ _HLC.MAX_DRIFT = 6e4;
161
184
  var HLC = _HLC;
162
185
 
163
186
  // src/utils/hash.ts
164
- function hashString(str) {
187
+ var nativeHash = null;
188
+ var nativeLoadAttempted = false;
189
+ function tryLoadNative() {
190
+ if (nativeLoadAttempted) return;
191
+ nativeLoadAttempted = true;
192
+ try {
193
+ nativeHash = require("@topgunbuild/native");
194
+ } catch {
195
+ }
196
+ }
197
+ function fnv1aHash(str) {
165
198
  let hash = 2166136261;
166
199
  for (let i = 0; i < str.length; i++) {
167
200
  hash ^= str.charCodeAt(i);
@@ -169,6 +202,13 @@ function hashString(str) {
169
202
  }
170
203
  return hash >>> 0;
171
204
  }
205
+ function hashString(str) {
206
+ tryLoadNative();
207
+ if (nativeHash && nativeHash.isNativeHashAvailable()) {
208
+ return nativeHash.hashString(str);
209
+ }
210
+ return fnv1aHash(str);
211
+ }
172
212
  function combineHashes(hashes) {
173
213
  let result = 0;
174
214
  for (const h of hashes) {
@@ -176,6 +216,18 @@ function combineHashes(hashes) {
176
216
  }
177
217
  return result >>> 0;
178
218
  }
219
+ function isUsingNativeHash() {
220
+ tryLoadNative();
221
+ return nativeHash?.isNativeHashAvailable() === true;
222
+ }
223
+ function disableNativeHash() {
224
+ nativeHash = null;
225
+ nativeLoadAttempted = true;
226
+ }
227
+ function resetNativeHash() {
228
+ nativeHash = null;
229
+ nativeLoadAttempted = false;
230
+ }
179
231
 
180
232
  // src/MerkleTree.ts
181
233
  var MerkleTree = class {
@@ -999,12 +1051,13 @@ var ORMap = class {
999
1051
  };
1000
1052
 
1001
1053
  // src/serializer.ts
1002
- var import_msgpack = require("@msgpack/msgpack");
1054
+ var import_msgpackr = require("msgpackr");
1003
1055
  function serialize(data) {
1004
- return (0, import_msgpack.encode)(data);
1056
+ return (0, import_msgpackr.pack)(data);
1005
1057
  }
1006
1058
  function deserialize(data) {
1007
- return (0, import_msgpack.decode)(data);
1059
+ const buffer = data instanceof ArrayBuffer ? new Uint8Array(data) : data;
1060
+ return (0, import_msgpackr.unpack)(buffer);
1008
1061
  }
1009
1062
 
1010
1063
  // src/predicate.ts
@@ -1095,9 +1148,16 @@ function evaluatePredicate(predicate, data) {
1095
1148
 
1096
1149
  // src/schemas.ts
1097
1150
  var import_zod = require("zod");
1151
+ var WriteConcernSchema = import_zod.z.enum([
1152
+ "FIRE_AND_FORGET",
1153
+ "MEMORY",
1154
+ "APPLIED",
1155
+ "REPLICATED",
1156
+ "PERSISTED"
1157
+ ]);
1098
1158
  var TimestampSchema = import_zod.z.object({
1099
- millis: import_zod.z.number(),
1100
- counter: import_zod.z.number(),
1159
+ millis: import_zod.z.union([import_zod.z.number(), import_zod.z.bigint()]).transform(Number),
1160
+ counter: import_zod.z.union([import_zod.z.number(), import_zod.z.bigint()]).transform(Number),
1101
1161
  nodeId: import_zod.z.string()
1102
1162
  });
1103
1163
  var LWWRecordSchema = import_zod.z.object({
@@ -1146,7 +1206,10 @@ var ClientOpSchema = import_zod.z.object({
1146
1206
  opType: import_zod.z.string().optional(),
1147
1207
  record: LWWRecordSchema.nullable().optional(),
1148
1208
  orRecord: ORMapRecordSchema.nullable().optional(),
1149
- orTag: import_zod.z.string().nullable().optional()
1209
+ orTag: import_zod.z.string().nullable().optional(),
1210
+ // Write Concern fields (Phase 5.01)
1211
+ writeConcern: WriteConcernSchema.optional(),
1212
+ timeout: import_zod.z.number().optional()
1150
1213
  });
1151
1214
  var AuthMessageSchema = import_zod.z.object({
1152
1215
  type: import_zod.z.literal("AUTH"),
@@ -1173,7 +1236,10 @@ var ClientOpMessageSchema = import_zod.z.object({
1173
1236
  var OpBatchMessageSchema = import_zod.z.object({
1174
1237
  type: import_zod.z.literal("OP_BATCH"),
1175
1238
  payload: import_zod.z.object({
1176
- ops: import_zod.z.array(ClientOpSchema)
1239
+ ops: import_zod.z.array(ClientOpSchema),
1240
+ // Batch-level Write Concern (can be overridden per-op)
1241
+ writeConcern: WriteConcernSchema.optional(),
1242
+ timeout: import_zod.z.number().optional()
1177
1243
  })
1178
1244
  });
1179
1245
  var SyncInitMessageSchema = import_zod.z.object({
@@ -1271,6 +1337,11 @@ var PongMessageSchema = import_zod.z.object({
1271
1337
  serverTime: import_zod.z.number()
1272
1338
  // Server's Date.now() (for clock skew detection)
1273
1339
  });
1340
+ var BatchMessageSchema = import_zod.z.object({
1341
+ type: import_zod.z.literal("BATCH"),
1342
+ count: import_zod.z.number(),
1343
+ data: import_zod.z.instanceof(Uint8Array)
1344
+ });
1274
1345
  var ORMapSyncInitSchema = import_zod.z.object({
1275
1346
  type: import_zod.z.literal("ORMAP_SYNC_INIT"),
1276
1347
  mapName: import_zod.z.string(),
@@ -1344,6 +1415,40 @@ var ORMapPushDiffSchema = import_zod.z.object({
1344
1415
  }))
1345
1416
  })
1346
1417
  });
1418
+ var PartitionMapRequestSchema = import_zod.z.object({
1419
+ type: import_zod.z.literal("PARTITION_MAP_REQUEST"),
1420
+ payload: import_zod.z.object({
1421
+ currentVersion: import_zod.z.number().optional()
1422
+ }).optional()
1423
+ });
1424
+ var OpResultSchema = import_zod.z.object({
1425
+ opId: import_zod.z.string(),
1426
+ success: import_zod.z.boolean(),
1427
+ achievedLevel: WriteConcernSchema,
1428
+ error: import_zod.z.string().optional()
1429
+ });
1430
+ var OpAckMessageSchema = import_zod.z.object({
1431
+ type: import_zod.z.literal("OP_ACK"),
1432
+ payload: import_zod.z.object({
1433
+ /** ID of the last operation in the batch (for backwards compatibility) */
1434
+ lastId: import_zod.z.string(),
1435
+ /** Write Concern level achieved (for simple ACKs) */
1436
+ achievedLevel: WriteConcernSchema.optional(),
1437
+ /** Per-operation results (for batch operations with mixed Write Concern) */
1438
+ results: import_zod.z.array(OpResultSchema).optional()
1439
+ })
1440
+ });
1441
+ var OpRejectedMessageSchema = import_zod.z.object({
1442
+ type: import_zod.z.literal("OP_REJECTED"),
1443
+ payload: import_zod.z.object({
1444
+ /** Operation ID that was rejected */
1445
+ opId: import_zod.z.string(),
1446
+ /** Reason for rejection */
1447
+ reason: import_zod.z.string(),
1448
+ /** Error code */
1449
+ code: import_zod.z.number().optional()
1450
+ })
1451
+ });
1347
1452
  var MessageSchema = import_zod.z.discriminatedUnion("type", [
1348
1453
  AuthMessageSchema,
1349
1454
  QuerySubMessageSchema,
@@ -1370,13 +1475,108 @@ var MessageSchema = import_zod.z.discriminatedUnion("type", [
1370
1475
  ORMapSyncRespLeafSchema,
1371
1476
  ORMapDiffRequestSchema,
1372
1477
  ORMapDiffResponseSchema,
1373
- ORMapPushDiffSchema
1478
+ ORMapPushDiffSchema,
1479
+ // Phase 4: Partition Map
1480
+ PartitionMapRequestSchema
1374
1481
  ]);
1482
+
1483
+ // src/types/WriteConcern.ts
1484
+ var WriteConcern = /* @__PURE__ */ ((WriteConcern2) => {
1485
+ WriteConcern2["FIRE_AND_FORGET"] = "FIRE_AND_FORGET";
1486
+ WriteConcern2["MEMORY"] = "MEMORY";
1487
+ WriteConcern2["APPLIED"] = "APPLIED";
1488
+ WriteConcern2["REPLICATED"] = "REPLICATED";
1489
+ WriteConcern2["PERSISTED"] = "PERSISTED";
1490
+ return WriteConcern2;
1491
+ })(WriteConcern || {});
1492
+ var DEFAULT_WRITE_CONCERN_TIMEOUT = 5e3;
1493
+ var WRITE_CONCERN_ORDER = [
1494
+ "FIRE_AND_FORGET" /* FIRE_AND_FORGET */,
1495
+ "MEMORY" /* MEMORY */,
1496
+ "APPLIED" /* APPLIED */,
1497
+ "REPLICATED" /* REPLICATED */,
1498
+ "PERSISTED" /* PERSISTED */
1499
+ ];
1500
+ function isWriteConcernAchieved(achieved, target) {
1501
+ const targetIndex = WRITE_CONCERN_ORDER.indexOf(target);
1502
+ const achievedIndex = Math.max(
1503
+ ...Array.from(achieved).map((l) => WRITE_CONCERN_ORDER.indexOf(l))
1504
+ );
1505
+ return achievedIndex >= targetIndex;
1506
+ }
1507
+ function getHighestWriteConcernLevel(achieved) {
1508
+ for (let i = WRITE_CONCERN_ORDER.length - 1; i >= 0; i--) {
1509
+ if (achieved.has(WRITE_CONCERN_ORDER[i])) {
1510
+ return WRITE_CONCERN_ORDER[i];
1511
+ }
1512
+ }
1513
+ return "FIRE_AND_FORGET" /* FIRE_AND_FORGET */;
1514
+ }
1515
+
1516
+ // src/types/cluster.ts
1517
+ var DEFAULT_CONNECTION_POOL_CONFIG = {
1518
+ maxConnectionsPerNode: 1,
1519
+ connectionTimeoutMs: 5e3,
1520
+ healthCheckIntervalMs: 1e4,
1521
+ reconnectDelayMs: 1e3,
1522
+ maxReconnectDelayMs: 3e4,
1523
+ maxReconnectAttempts: 5
1524
+ };
1525
+ var DEFAULT_PARTITION_ROUTER_CONFIG = {
1526
+ fallbackMode: "forward",
1527
+ mapRefreshIntervalMs: 3e4,
1528
+ maxMapStalenessMs: 6e4
1529
+ };
1530
+ var DEFAULT_CIRCUIT_BREAKER_CONFIG = {
1531
+ failureThreshold: 5,
1532
+ resetTimeoutMs: 3e4
1533
+ };
1534
+ var PartitionState = /* @__PURE__ */ ((PartitionState2) => {
1535
+ PartitionState2["STABLE"] = "STABLE";
1536
+ PartitionState2["MIGRATING"] = "MIGRATING";
1537
+ PartitionState2["SYNC"] = "SYNC";
1538
+ PartitionState2["FAILED"] = "FAILED";
1539
+ return PartitionState2;
1540
+ })(PartitionState || {});
1541
+ var DEFAULT_MIGRATION_CONFIG = {
1542
+ batchSize: 10,
1543
+ batchIntervalMs: 5e3,
1544
+ transferChunkSize: 65536,
1545
+ // 64KB
1546
+ maxRetries: 3,
1547
+ syncTimeoutMs: 3e4,
1548
+ parallelTransfers: 4
1549
+ };
1550
+ var ConsistencyLevel = /* @__PURE__ */ ((ConsistencyLevel2) => {
1551
+ ConsistencyLevel2["STRONG"] = "STRONG";
1552
+ ConsistencyLevel2["QUORUM"] = "QUORUM";
1553
+ ConsistencyLevel2["EVENTUAL"] = "EVENTUAL";
1554
+ return ConsistencyLevel2;
1555
+ })(ConsistencyLevel || {});
1556
+ var DEFAULT_REPLICATION_CONFIG = {
1557
+ defaultConsistency: "EVENTUAL" /* EVENTUAL */,
1558
+ queueSizeLimit: 1e4,
1559
+ batchSize: 100,
1560
+ batchIntervalMs: 50,
1561
+ ackTimeoutMs: 5e3,
1562
+ maxRetries: 3
1563
+ };
1564
+ var PARTITION_COUNT = 271;
1565
+ var DEFAULT_BACKUP_COUNT = 1;
1375
1566
  // Annotate the CommonJS export names for ESM import in node:
1376
1567
  0 && (module.exports = {
1377
1568
  AuthMessageSchema,
1569
+ BatchMessageSchema,
1378
1570
  ClientOpMessageSchema,
1379
1571
  ClientOpSchema,
1572
+ ConsistencyLevel,
1573
+ DEFAULT_BACKUP_COUNT,
1574
+ DEFAULT_CIRCUIT_BREAKER_CONFIG,
1575
+ DEFAULT_CONNECTION_POOL_CONFIG,
1576
+ DEFAULT_MIGRATION_CONFIG,
1577
+ DEFAULT_PARTITION_ROUTER_CONFIG,
1578
+ DEFAULT_REPLICATION_CONFIG,
1579
+ DEFAULT_WRITE_CONCERN_TIMEOUT,
1380
1580
  HLC,
1381
1581
  LWWMap,
1382
1582
  LWWRecordSchema,
@@ -1396,7 +1596,13 @@ var MessageSchema = import_zod.z.discriminatedUnion("type", [
1396
1596
  ORMapSyncRespBucketsSchema,
1397
1597
  ORMapSyncRespLeafSchema,
1398
1598
  ORMapSyncRespRootSchema,
1599
+ OpAckMessageSchema,
1399
1600
  OpBatchMessageSchema,
1601
+ OpRejectedMessageSchema,
1602
+ OpResultSchema,
1603
+ PARTITION_COUNT,
1604
+ PartitionMapRequestSchema,
1605
+ PartitionState,
1400
1606
  PingMessageSchema,
1401
1607
  PongMessageSchema,
1402
1608
  PredicateNodeSchema,
@@ -1414,13 +1620,21 @@ var MessageSchema = import_zod.z.discriminatedUnion("type", [
1414
1620
  TopicPubSchema,
1415
1621
  TopicSubSchema,
1416
1622
  TopicUnsubSchema,
1623
+ WRITE_CONCERN_ORDER,
1624
+ WriteConcern,
1625
+ WriteConcernSchema,
1417
1626
  combineHashes,
1418
1627
  compareTimestamps,
1419
1628
  deserialize,
1629
+ disableNativeHash,
1420
1630
  evaluatePredicate,
1631
+ getHighestWriteConcernLevel,
1421
1632
  hashORMapEntry,
1422
1633
  hashORMapRecord,
1423
1634
  hashString,
1635
+ isUsingNativeHash,
1636
+ isWriteConcernAchieved,
1637
+ resetNativeHash,
1424
1638
  serialize,
1425
1639
  timestampToString
1426
1640
  });