@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.mts +301 -9
- package/dist/index.d.ts +301 -9
- package/dist/index.js +141 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +135 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -4
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
5
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
|
+
});
|
|
7
|
+
|
|
1
8
|
// src/HLC.ts
|
|
2
9
|
var _HLC = class _HLC {
|
|
3
10
|
constructor(nodeId) {
|
|
@@ -87,7 +94,17 @@ _HLC.MAX_DRIFT = 6e4;
|
|
|
87
94
|
var HLC = _HLC;
|
|
88
95
|
|
|
89
96
|
// src/utils/hash.ts
|
|
90
|
-
|
|
97
|
+
var nativeHash = null;
|
|
98
|
+
var nativeLoadAttempted = false;
|
|
99
|
+
function tryLoadNative() {
|
|
100
|
+
if (nativeLoadAttempted) return;
|
|
101
|
+
nativeLoadAttempted = true;
|
|
102
|
+
try {
|
|
103
|
+
nativeHash = __require("@topgunbuild/native");
|
|
104
|
+
} catch {
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function fnv1aHash(str) {
|
|
91
108
|
let hash = 2166136261;
|
|
92
109
|
for (let i = 0; i < str.length; i++) {
|
|
93
110
|
hash ^= str.charCodeAt(i);
|
|
@@ -95,6 +112,13 @@ function hashString(str) {
|
|
|
95
112
|
}
|
|
96
113
|
return hash >>> 0;
|
|
97
114
|
}
|
|
115
|
+
function hashString(str) {
|
|
116
|
+
tryLoadNative();
|
|
117
|
+
if (nativeHash && nativeHash.isNativeHashAvailable()) {
|
|
118
|
+
return nativeHash.hashString(str);
|
|
119
|
+
}
|
|
120
|
+
return fnv1aHash(str);
|
|
121
|
+
}
|
|
98
122
|
function combineHashes(hashes) {
|
|
99
123
|
let result = 0;
|
|
100
124
|
for (const h of hashes) {
|
|
@@ -102,6 +126,18 @@ function combineHashes(hashes) {
|
|
|
102
126
|
}
|
|
103
127
|
return result >>> 0;
|
|
104
128
|
}
|
|
129
|
+
function isUsingNativeHash() {
|
|
130
|
+
tryLoadNative();
|
|
131
|
+
return nativeHash?.isNativeHashAvailable() === true;
|
|
132
|
+
}
|
|
133
|
+
function disableNativeHash() {
|
|
134
|
+
nativeHash = null;
|
|
135
|
+
nativeLoadAttempted = true;
|
|
136
|
+
}
|
|
137
|
+
function resetNativeHash() {
|
|
138
|
+
nativeHash = null;
|
|
139
|
+
nativeLoadAttempted = false;
|
|
140
|
+
}
|
|
105
141
|
|
|
106
142
|
// src/MerkleTree.ts
|
|
107
143
|
var MerkleTree = class {
|
|
@@ -925,12 +961,13 @@ var ORMap = class {
|
|
|
925
961
|
};
|
|
926
962
|
|
|
927
963
|
// src/serializer.ts
|
|
928
|
-
import {
|
|
964
|
+
import { pack, unpack } from "msgpackr";
|
|
929
965
|
function serialize(data) {
|
|
930
|
-
return
|
|
966
|
+
return pack(data);
|
|
931
967
|
}
|
|
932
968
|
function deserialize(data) {
|
|
933
|
-
|
|
969
|
+
const buffer = data instanceof ArrayBuffer ? new Uint8Array(data) : data;
|
|
970
|
+
return unpack(buffer);
|
|
934
971
|
}
|
|
935
972
|
|
|
936
973
|
// src/predicate.ts
|
|
@@ -1021,6 +1058,13 @@ function evaluatePredicate(predicate, data) {
|
|
|
1021
1058
|
|
|
1022
1059
|
// src/schemas.ts
|
|
1023
1060
|
import { z } from "zod";
|
|
1061
|
+
var WriteConcernSchema = z.enum([
|
|
1062
|
+
"FIRE_AND_FORGET",
|
|
1063
|
+
"MEMORY",
|
|
1064
|
+
"APPLIED",
|
|
1065
|
+
"REPLICATED",
|
|
1066
|
+
"PERSISTED"
|
|
1067
|
+
]);
|
|
1024
1068
|
var TimestampSchema = z.object({
|
|
1025
1069
|
millis: z.number(),
|
|
1026
1070
|
counter: z.number(),
|
|
@@ -1072,7 +1116,10 @@ var ClientOpSchema = z.object({
|
|
|
1072
1116
|
opType: z.string().optional(),
|
|
1073
1117
|
record: LWWRecordSchema.nullable().optional(),
|
|
1074
1118
|
orRecord: ORMapRecordSchema.nullable().optional(),
|
|
1075
|
-
orTag: z.string().nullable().optional()
|
|
1119
|
+
orTag: z.string().nullable().optional(),
|
|
1120
|
+
// Write Concern fields (Phase 5.01)
|
|
1121
|
+
writeConcern: WriteConcernSchema.optional(),
|
|
1122
|
+
timeout: z.number().optional()
|
|
1076
1123
|
});
|
|
1077
1124
|
var AuthMessageSchema = z.object({
|
|
1078
1125
|
type: z.literal("AUTH"),
|
|
@@ -1099,7 +1146,10 @@ var ClientOpMessageSchema = z.object({
|
|
|
1099
1146
|
var OpBatchMessageSchema = z.object({
|
|
1100
1147
|
type: z.literal("OP_BATCH"),
|
|
1101
1148
|
payload: z.object({
|
|
1102
|
-
ops: z.array(ClientOpSchema)
|
|
1149
|
+
ops: z.array(ClientOpSchema),
|
|
1150
|
+
// Batch-level Write Concern (can be overridden per-op)
|
|
1151
|
+
writeConcern: WriteConcernSchema.optional(),
|
|
1152
|
+
timeout: z.number().optional()
|
|
1103
1153
|
})
|
|
1104
1154
|
});
|
|
1105
1155
|
var SyncInitMessageSchema = z.object({
|
|
@@ -1197,6 +1247,11 @@ var PongMessageSchema = z.object({
|
|
|
1197
1247
|
serverTime: z.number()
|
|
1198
1248
|
// Server's Date.now() (for clock skew detection)
|
|
1199
1249
|
});
|
|
1250
|
+
var BatchMessageSchema = z.object({
|
|
1251
|
+
type: z.literal("BATCH"),
|
|
1252
|
+
count: z.number(),
|
|
1253
|
+
data: z.instanceof(Uint8Array)
|
|
1254
|
+
});
|
|
1200
1255
|
var ORMapSyncInitSchema = z.object({
|
|
1201
1256
|
type: z.literal("ORMAP_SYNC_INIT"),
|
|
1202
1257
|
mapName: z.string(),
|
|
@@ -1270,6 +1325,34 @@ var ORMapPushDiffSchema = z.object({
|
|
|
1270
1325
|
}))
|
|
1271
1326
|
})
|
|
1272
1327
|
});
|
|
1328
|
+
var OpResultSchema = z.object({
|
|
1329
|
+
opId: z.string(),
|
|
1330
|
+
success: z.boolean(),
|
|
1331
|
+
achievedLevel: WriteConcernSchema,
|
|
1332
|
+
error: z.string().optional()
|
|
1333
|
+
});
|
|
1334
|
+
var OpAckMessageSchema = z.object({
|
|
1335
|
+
type: z.literal("OP_ACK"),
|
|
1336
|
+
payload: z.object({
|
|
1337
|
+
/** ID of the last operation in the batch (for backwards compatibility) */
|
|
1338
|
+
lastId: z.string(),
|
|
1339
|
+
/** Write Concern level achieved (for simple ACKs) */
|
|
1340
|
+
achievedLevel: WriteConcernSchema.optional(),
|
|
1341
|
+
/** Per-operation results (for batch operations with mixed Write Concern) */
|
|
1342
|
+
results: z.array(OpResultSchema).optional()
|
|
1343
|
+
})
|
|
1344
|
+
});
|
|
1345
|
+
var OpRejectedMessageSchema = z.object({
|
|
1346
|
+
type: z.literal("OP_REJECTED"),
|
|
1347
|
+
payload: z.object({
|
|
1348
|
+
/** Operation ID that was rejected */
|
|
1349
|
+
opId: z.string(),
|
|
1350
|
+
/** Reason for rejection */
|
|
1351
|
+
reason: z.string(),
|
|
1352
|
+
/** Error code */
|
|
1353
|
+
code: z.number().optional()
|
|
1354
|
+
})
|
|
1355
|
+
});
|
|
1273
1356
|
var MessageSchema = z.discriminatedUnion("type", [
|
|
1274
1357
|
AuthMessageSchema,
|
|
1275
1358
|
QuerySubMessageSchema,
|
|
@@ -1298,10 +1381,45 @@ var MessageSchema = z.discriminatedUnion("type", [
|
|
|
1298
1381
|
ORMapDiffResponseSchema,
|
|
1299
1382
|
ORMapPushDiffSchema
|
|
1300
1383
|
]);
|
|
1384
|
+
|
|
1385
|
+
// src/types/WriteConcern.ts
|
|
1386
|
+
var WriteConcern = /* @__PURE__ */ ((WriteConcern2) => {
|
|
1387
|
+
WriteConcern2["FIRE_AND_FORGET"] = "FIRE_AND_FORGET";
|
|
1388
|
+
WriteConcern2["MEMORY"] = "MEMORY";
|
|
1389
|
+
WriteConcern2["APPLIED"] = "APPLIED";
|
|
1390
|
+
WriteConcern2["REPLICATED"] = "REPLICATED";
|
|
1391
|
+
WriteConcern2["PERSISTED"] = "PERSISTED";
|
|
1392
|
+
return WriteConcern2;
|
|
1393
|
+
})(WriteConcern || {});
|
|
1394
|
+
var DEFAULT_WRITE_CONCERN_TIMEOUT = 5e3;
|
|
1395
|
+
var WRITE_CONCERN_ORDER = [
|
|
1396
|
+
"FIRE_AND_FORGET" /* FIRE_AND_FORGET */,
|
|
1397
|
+
"MEMORY" /* MEMORY */,
|
|
1398
|
+
"APPLIED" /* APPLIED */,
|
|
1399
|
+
"REPLICATED" /* REPLICATED */,
|
|
1400
|
+
"PERSISTED" /* PERSISTED */
|
|
1401
|
+
];
|
|
1402
|
+
function isWriteConcernAchieved(achieved, target) {
|
|
1403
|
+
const targetIndex = WRITE_CONCERN_ORDER.indexOf(target);
|
|
1404
|
+
const achievedIndex = Math.max(
|
|
1405
|
+
...Array.from(achieved).map((l) => WRITE_CONCERN_ORDER.indexOf(l))
|
|
1406
|
+
);
|
|
1407
|
+
return achievedIndex >= targetIndex;
|
|
1408
|
+
}
|
|
1409
|
+
function getHighestWriteConcernLevel(achieved) {
|
|
1410
|
+
for (let i = WRITE_CONCERN_ORDER.length - 1; i >= 0; i--) {
|
|
1411
|
+
if (achieved.has(WRITE_CONCERN_ORDER[i])) {
|
|
1412
|
+
return WRITE_CONCERN_ORDER[i];
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
return "FIRE_AND_FORGET" /* FIRE_AND_FORGET */;
|
|
1416
|
+
}
|
|
1301
1417
|
export {
|
|
1302
1418
|
AuthMessageSchema,
|
|
1419
|
+
BatchMessageSchema,
|
|
1303
1420
|
ClientOpMessageSchema,
|
|
1304
1421
|
ClientOpSchema,
|
|
1422
|
+
DEFAULT_WRITE_CONCERN_TIMEOUT,
|
|
1305
1423
|
HLC,
|
|
1306
1424
|
LWWMap,
|
|
1307
1425
|
LWWRecordSchema,
|
|
@@ -1321,7 +1439,10 @@ export {
|
|
|
1321
1439
|
ORMapSyncRespBucketsSchema,
|
|
1322
1440
|
ORMapSyncRespLeafSchema,
|
|
1323
1441
|
ORMapSyncRespRootSchema,
|
|
1442
|
+
OpAckMessageSchema,
|
|
1324
1443
|
OpBatchMessageSchema,
|
|
1444
|
+
OpRejectedMessageSchema,
|
|
1445
|
+
OpResultSchema,
|
|
1325
1446
|
PingMessageSchema,
|
|
1326
1447
|
PongMessageSchema,
|
|
1327
1448
|
PredicateNodeSchema,
|
|
@@ -1339,13 +1460,21 @@ export {
|
|
|
1339
1460
|
TopicPubSchema,
|
|
1340
1461
|
TopicSubSchema,
|
|
1341
1462
|
TopicUnsubSchema,
|
|
1463
|
+
WRITE_CONCERN_ORDER,
|
|
1464
|
+
WriteConcern,
|
|
1465
|
+
WriteConcernSchema,
|
|
1342
1466
|
combineHashes,
|
|
1343
1467
|
compareTimestamps,
|
|
1344
1468
|
deserialize,
|
|
1469
|
+
disableNativeHash,
|
|
1345
1470
|
evaluatePredicate,
|
|
1471
|
+
getHighestWriteConcernLevel,
|
|
1346
1472
|
hashORMapEntry,
|
|
1347
1473
|
hashORMapRecord,
|
|
1348
1474
|
hashString,
|
|
1475
|
+
isUsingNativeHash,
|
|
1476
|
+
isWriteConcernAchieved,
|
|
1477
|
+
resetNativeHash,
|
|
1349
1478
|
serialize,
|
|
1350
1479
|
timestampToString
|
|
1351
1480
|
};
|