@korajs/core 0.1.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/chunk-ZMUY7AVV.js +421 -0
- package/dist/chunk-ZMUY7AVV.js.map +1 -0
- package/dist/events-D_kDPDC9.d.cts +324 -0
- package/dist/events-D_kDPDC9.d.ts +324 -0
- package/dist/index.cjs +1153 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +401 -0
- package/dist/index.d.ts +401 -0
- package/dist/index.js +707 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.cjs +397 -0
- package/dist/internal.cjs.map +1 -0
- package/dist/internal.d.cts +58 -0
- package/dist/internal.d.ts +58 -0
- package/dist/internal.js +55 -0
- package/dist/internal.js.map +1 -0
- package/package.json +49 -0
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
// src/errors/errors.ts
|
|
2
|
+
var KoraError = class extends Error {
|
|
3
|
+
constructor(message, code, context) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.code = code;
|
|
6
|
+
this.context = context;
|
|
7
|
+
this.name = "KoraError";
|
|
8
|
+
}
|
|
9
|
+
code;
|
|
10
|
+
context;
|
|
11
|
+
};
|
|
12
|
+
var SchemaValidationError = class extends KoraError {
|
|
13
|
+
constructor(message, context) {
|
|
14
|
+
super(message, "SCHEMA_VALIDATION", context);
|
|
15
|
+
this.name = "SchemaValidationError";
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var OperationError = class extends KoraError {
|
|
19
|
+
constructor(message, context) {
|
|
20
|
+
super(message, "OPERATION_ERROR", context);
|
|
21
|
+
this.name = "OperationError";
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
var MergeConflictError = class extends KoraError {
|
|
25
|
+
constructor(operationA, operationB, field) {
|
|
26
|
+
super(
|
|
27
|
+
`Merge conflict on field "${field}" in collection "${operationA.collection}"`,
|
|
28
|
+
"MERGE_CONFLICT",
|
|
29
|
+
{ operationA: operationA.id, operationB: operationB.id, field }
|
|
30
|
+
);
|
|
31
|
+
this.operationA = operationA;
|
|
32
|
+
this.operationB = operationB;
|
|
33
|
+
this.field = field;
|
|
34
|
+
this.name = "MergeConflictError";
|
|
35
|
+
}
|
|
36
|
+
operationA;
|
|
37
|
+
operationB;
|
|
38
|
+
field;
|
|
39
|
+
};
|
|
40
|
+
var SyncError = class extends KoraError {
|
|
41
|
+
constructor(message, context) {
|
|
42
|
+
super(message, "SYNC_ERROR", context);
|
|
43
|
+
this.name = "SyncError";
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
var StorageError = class extends KoraError {
|
|
47
|
+
constructor(message, context) {
|
|
48
|
+
super(message, "STORAGE_ERROR", context);
|
|
49
|
+
this.name = "StorageError";
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var ClockDriftError = class extends KoraError {
|
|
53
|
+
constructor(currentHlcTime, physicalTime) {
|
|
54
|
+
const driftSeconds = Math.round((currentHlcTime - physicalTime) / 1e3);
|
|
55
|
+
super(
|
|
56
|
+
`Clock drift of ${driftSeconds}s detected. Physical time is behind HLC by more than 5 minutes. This indicates a severe clock issue.`,
|
|
57
|
+
"CLOCK_DRIFT",
|
|
58
|
+
{ currentHlcTime, physicalTime, driftSeconds }
|
|
59
|
+
);
|
|
60
|
+
this.currentHlcTime = currentHlcTime;
|
|
61
|
+
this.physicalTime = physicalTime;
|
|
62
|
+
this.name = "ClockDriftError";
|
|
63
|
+
}
|
|
64
|
+
currentHlcTime;
|
|
65
|
+
physicalTime;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// src/clock/hlc.ts
|
|
69
|
+
var systemTimeSource = { now: () => Date.now() };
|
|
70
|
+
var DRIFT_WARN_MS = 6e4;
|
|
71
|
+
var DRIFT_ERROR_MS = 5 * 6e4;
|
|
72
|
+
var HybridLogicalClock = class {
|
|
73
|
+
constructor(nodeId, timeSource = systemTimeSource, onDriftWarning) {
|
|
74
|
+
this.nodeId = nodeId;
|
|
75
|
+
this.timeSource = timeSource;
|
|
76
|
+
this.onDriftWarning = onDriftWarning;
|
|
77
|
+
}
|
|
78
|
+
nodeId;
|
|
79
|
+
timeSource;
|
|
80
|
+
onDriftWarning;
|
|
81
|
+
wallTime = 0;
|
|
82
|
+
logical = 0;
|
|
83
|
+
/**
|
|
84
|
+
* Generate a new timestamp for a local event.
|
|
85
|
+
* Guarantees monotonicity: each call returns a timestamp strictly greater than the previous.
|
|
86
|
+
*
|
|
87
|
+
* @throws {ClockDriftError} If physical time is more than 5 minutes behind the HLC wallTime
|
|
88
|
+
*/
|
|
89
|
+
now() {
|
|
90
|
+
const physicalTime = this.timeSource.now();
|
|
91
|
+
this.checkDrift(physicalTime);
|
|
92
|
+
if (physicalTime > this.wallTime) {
|
|
93
|
+
this.wallTime = physicalTime;
|
|
94
|
+
this.logical = 0;
|
|
95
|
+
} else {
|
|
96
|
+
this.logical++;
|
|
97
|
+
}
|
|
98
|
+
return { wallTime: this.wallTime, logical: this.logical, nodeId: this.nodeId };
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Update clock on receiving a remote timestamp.
|
|
102
|
+
* Merges the remote clock state with the local state to maintain causal ordering.
|
|
103
|
+
*
|
|
104
|
+
* @throws {ClockDriftError} If physical time is more than 5 minutes behind the resulting wallTime
|
|
105
|
+
*/
|
|
106
|
+
receive(remote) {
|
|
107
|
+
const physicalTime = this.timeSource.now();
|
|
108
|
+
if (physicalTime > this.wallTime && physicalTime > remote.wallTime) {
|
|
109
|
+
this.wallTime = physicalTime;
|
|
110
|
+
this.logical = 0;
|
|
111
|
+
} else if (remote.wallTime > this.wallTime) {
|
|
112
|
+
this.wallTime = remote.wallTime;
|
|
113
|
+
this.logical = remote.logical + 1;
|
|
114
|
+
} else if (this.wallTime === remote.wallTime) {
|
|
115
|
+
this.logical = Math.max(this.logical, remote.logical) + 1;
|
|
116
|
+
} else {
|
|
117
|
+
this.logical++;
|
|
118
|
+
}
|
|
119
|
+
this.checkDrift(physicalTime);
|
|
120
|
+
return { wallTime: this.wallTime, logical: this.logical, nodeId: this.nodeId };
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Compare two timestamps. Returns negative if a < b, positive if a > b, zero if equal.
|
|
124
|
+
* Total order: wallTime first, then logical, then nodeId (lexicographic).
|
|
125
|
+
*/
|
|
126
|
+
static compare(a, b) {
|
|
127
|
+
if (a.wallTime !== b.wallTime) return a.wallTime - b.wallTime;
|
|
128
|
+
if (a.logical !== b.logical) return a.logical - b.logical;
|
|
129
|
+
if (a.nodeId < b.nodeId) return -1;
|
|
130
|
+
if (a.nodeId > b.nodeId) return 1;
|
|
131
|
+
return 0;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Serialize an HLC timestamp to a string that sorts lexicographically.
|
|
135
|
+
* Format: zero-padded wallTime:logical:nodeId
|
|
136
|
+
*/
|
|
137
|
+
static serialize(ts) {
|
|
138
|
+
const wall = ts.wallTime.toString().padStart(15, "0");
|
|
139
|
+
const log = ts.logical.toString().padStart(5, "0");
|
|
140
|
+
return `${wall}:${log}:${ts.nodeId}`;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Deserialize an HLC timestamp from its serialized string form.
|
|
144
|
+
*/
|
|
145
|
+
static deserialize(s) {
|
|
146
|
+
const parts = s.split(":");
|
|
147
|
+
if (parts.length < 3) {
|
|
148
|
+
throw new Error(`Invalid HLC timestamp string: "${s}"`);
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
wallTime: Number.parseInt(parts[0] ?? "0", 10),
|
|
152
|
+
logical: Number.parseInt(parts[1] ?? "0", 10),
|
|
153
|
+
// nodeId may contain colons, so rejoin remaining parts
|
|
154
|
+
nodeId: parts.slice(2).join(":")
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
checkDrift(physicalTime) {
|
|
158
|
+
const drift = this.wallTime - physicalTime;
|
|
159
|
+
if (drift > DRIFT_ERROR_MS) {
|
|
160
|
+
throw new ClockDriftError(this.wallTime, physicalTime);
|
|
161
|
+
}
|
|
162
|
+
if (drift > DRIFT_WARN_MS) {
|
|
163
|
+
this.onDriftWarning?.(drift);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
// src/operations/content-hash.ts
|
|
169
|
+
async function computeOperationId(input, timestamp) {
|
|
170
|
+
const canonical = canonicalize({
|
|
171
|
+
type: input.type,
|
|
172
|
+
collection: input.collection,
|
|
173
|
+
recordId: input.recordId,
|
|
174
|
+
data: input.data,
|
|
175
|
+
timestamp,
|
|
176
|
+
nodeId: input.nodeId
|
|
177
|
+
});
|
|
178
|
+
const encoded = new TextEncoder().encode(canonical);
|
|
179
|
+
const hashBuffer = await globalThis.crypto.subtle.digest("SHA-256", encoded);
|
|
180
|
+
return bufferToHex(hashBuffer);
|
|
181
|
+
}
|
|
182
|
+
function canonicalize(obj) {
|
|
183
|
+
if (obj === null || obj === void 0) {
|
|
184
|
+
return JSON.stringify(obj);
|
|
185
|
+
}
|
|
186
|
+
if (typeof obj !== "object") {
|
|
187
|
+
return JSON.stringify(obj);
|
|
188
|
+
}
|
|
189
|
+
if (Array.isArray(obj)) {
|
|
190
|
+
const items = obj.map((item) => canonicalize(item));
|
|
191
|
+
return `[${items.join(",")}]`;
|
|
192
|
+
}
|
|
193
|
+
const keys = Object.keys(obj).sort();
|
|
194
|
+
const pairs = keys.map((key) => {
|
|
195
|
+
const value = obj[key];
|
|
196
|
+
return `${JSON.stringify(key)}:${canonicalize(value)}`;
|
|
197
|
+
});
|
|
198
|
+
return `{${pairs.join(",")}}`;
|
|
199
|
+
}
|
|
200
|
+
function bufferToHex(buffer) {
|
|
201
|
+
const bytes = new Uint8Array(buffer);
|
|
202
|
+
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// src/operations/operation.ts
|
|
206
|
+
async function createOperation(input, clock) {
|
|
207
|
+
validateOperationParams(input);
|
|
208
|
+
const timestamp = clock.now();
|
|
209
|
+
const serializedTs = HybridLogicalClock.serialize(timestamp);
|
|
210
|
+
const id = await computeOperationId(input, serializedTs);
|
|
211
|
+
const operation = {
|
|
212
|
+
id,
|
|
213
|
+
nodeId: input.nodeId,
|
|
214
|
+
type: input.type,
|
|
215
|
+
collection: input.collection,
|
|
216
|
+
recordId: input.recordId,
|
|
217
|
+
data: input.data ? { ...input.data } : null,
|
|
218
|
+
previousData: input.previousData ? { ...input.previousData } : null,
|
|
219
|
+
timestamp,
|
|
220
|
+
sequenceNumber: input.sequenceNumber,
|
|
221
|
+
causalDeps: [...input.causalDeps],
|
|
222
|
+
schemaVersion: input.schemaVersion
|
|
223
|
+
};
|
|
224
|
+
return deepFreeze(operation);
|
|
225
|
+
}
|
|
226
|
+
function validateOperationParams(input) {
|
|
227
|
+
if (!input.nodeId || typeof input.nodeId !== "string") {
|
|
228
|
+
throw new OperationError("nodeId is required and must be a non-empty string", {
|
|
229
|
+
received: input.nodeId
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
if (!input.type || !["insert", "update", "delete"].includes(input.type)) {
|
|
233
|
+
throw new OperationError('type must be "insert", "update", or "delete"', {
|
|
234
|
+
received: input.type
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
if (!input.collection || typeof input.collection !== "string") {
|
|
238
|
+
throw new OperationError("collection is required and must be a non-empty string", {
|
|
239
|
+
received: input.collection
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
if (!input.recordId || typeof input.recordId !== "string") {
|
|
243
|
+
throw new OperationError("recordId is required and must be a non-empty string", {
|
|
244
|
+
received: input.recordId
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
if (input.type === "insert" && input.data === null) {
|
|
248
|
+
throw new OperationError("insert operations must include data", {
|
|
249
|
+
type: input.type,
|
|
250
|
+
collection: input.collection
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
if (input.type === "update" && input.data === null) {
|
|
254
|
+
throw new OperationError("update operations must include data with changed fields", {
|
|
255
|
+
type: input.type,
|
|
256
|
+
collection: input.collection
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
if (input.type === "update" && input.previousData === null) {
|
|
260
|
+
throw new OperationError(
|
|
261
|
+
"update operations must include previousData for 3-way merge support",
|
|
262
|
+
{
|
|
263
|
+
type: input.type,
|
|
264
|
+
collection: input.collection
|
|
265
|
+
}
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
if (input.type === "delete" && input.data !== null) {
|
|
269
|
+
throw new OperationError("delete operations must have null data", {
|
|
270
|
+
type: input.type,
|
|
271
|
+
collection: input.collection
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
if (typeof input.sequenceNumber !== "number" || input.sequenceNumber < 0) {
|
|
275
|
+
throw new OperationError("sequenceNumber must be a non-negative number", {
|
|
276
|
+
received: input.sequenceNumber
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
if (!Array.isArray(input.causalDeps)) {
|
|
280
|
+
throw new OperationError("causalDeps must be an array of operation IDs", {
|
|
281
|
+
received: typeof input.causalDeps
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
if (typeof input.schemaVersion !== "number" || input.schemaVersion < 1) {
|
|
285
|
+
throw new OperationError("schemaVersion must be a positive number", {
|
|
286
|
+
received: input.schemaVersion
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
async function verifyOperationIntegrity(op) {
|
|
291
|
+
const input = {
|
|
292
|
+
nodeId: op.nodeId,
|
|
293
|
+
type: op.type,
|
|
294
|
+
collection: op.collection,
|
|
295
|
+
recordId: op.recordId,
|
|
296
|
+
data: op.data,
|
|
297
|
+
previousData: op.previousData,
|
|
298
|
+
sequenceNumber: op.sequenceNumber,
|
|
299
|
+
causalDeps: op.causalDeps,
|
|
300
|
+
schemaVersion: op.schemaVersion
|
|
301
|
+
};
|
|
302
|
+
const serializedTs = HybridLogicalClock.serialize(op.timestamp);
|
|
303
|
+
const expectedId = await computeOperationId(input, serializedTs);
|
|
304
|
+
return op.id === expectedId;
|
|
305
|
+
}
|
|
306
|
+
function isValidOperation(value) {
|
|
307
|
+
if (typeof value !== "object" || value === null) return false;
|
|
308
|
+
const op = value;
|
|
309
|
+
return typeof op.id === "string" && typeof op.nodeId === "string" && (op.type === "insert" || op.type === "update" || op.type === "delete") && typeof op.collection === "string" && typeof op.recordId === "string" && typeof op.sequenceNumber === "number" && Array.isArray(op.causalDeps) && typeof op.schemaVersion === "number" && typeof op.timestamp === "object" && op.timestamp !== null;
|
|
310
|
+
}
|
|
311
|
+
function deepFreeze(obj) {
|
|
312
|
+
if (typeof obj !== "object" || obj === null) return obj;
|
|
313
|
+
Object.freeze(obj);
|
|
314
|
+
for (const value of Object.values(obj)) {
|
|
315
|
+
if (typeof value === "object" && value !== null && !Object.isFrozen(value)) {
|
|
316
|
+
deepFreeze(value);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return obj;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// src/version-vector/topological-sort.ts
|
|
323
|
+
function topologicalSort(operations) {
|
|
324
|
+
if (operations.length <= 1) return [...operations];
|
|
325
|
+
const opMap = /* @__PURE__ */ new Map();
|
|
326
|
+
for (const op of operations) {
|
|
327
|
+
opMap.set(op.id, op);
|
|
328
|
+
}
|
|
329
|
+
const inDegree = /* @__PURE__ */ new Map();
|
|
330
|
+
const dependents = /* @__PURE__ */ new Map();
|
|
331
|
+
for (const op of operations) {
|
|
332
|
+
if (!inDegree.has(op.id)) {
|
|
333
|
+
inDegree.set(op.id, 0);
|
|
334
|
+
}
|
|
335
|
+
if (!dependents.has(op.id)) {
|
|
336
|
+
dependents.set(op.id, []);
|
|
337
|
+
}
|
|
338
|
+
for (const depId of op.causalDeps) {
|
|
339
|
+
if (opMap.has(depId)) {
|
|
340
|
+
inDegree.set(op.id, (inDegree.get(op.id) ?? 0) + 1);
|
|
341
|
+
const deps = dependents.get(depId);
|
|
342
|
+
if (deps) {
|
|
343
|
+
deps.push(op.id);
|
|
344
|
+
} else {
|
|
345
|
+
dependents.set(depId, [op.id]);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
const queue = [];
|
|
351
|
+
for (const op of operations) {
|
|
352
|
+
if ((inDegree.get(op.id) ?? 0) === 0) {
|
|
353
|
+
queue.push(op);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
queue.sort(compareByTimestamp);
|
|
357
|
+
const result = [];
|
|
358
|
+
while (queue.length > 0) {
|
|
359
|
+
const current = queue.shift();
|
|
360
|
+
if (!current) break;
|
|
361
|
+
result.push(current);
|
|
362
|
+
const deps = dependents.get(current.id) ?? [];
|
|
363
|
+
const newlyReady = [];
|
|
364
|
+
for (const depId of deps) {
|
|
365
|
+
const deg = (inDegree.get(depId) ?? 0) - 1;
|
|
366
|
+
inDegree.set(depId, deg);
|
|
367
|
+
if (deg === 0) {
|
|
368
|
+
const op = opMap.get(depId);
|
|
369
|
+
if (op) newlyReady.push(op);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (newlyReady.length > 0) {
|
|
373
|
+
newlyReady.sort(compareByTimestamp);
|
|
374
|
+
mergeIntoSorted(queue, newlyReady);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
if (result.length !== operations.length) {
|
|
378
|
+
throw new OperationError(
|
|
379
|
+
`Cycle detected in operation dependency graph. Sorted ${result.length} of ${operations.length} operations.`,
|
|
380
|
+
{
|
|
381
|
+
sortedCount: result.length,
|
|
382
|
+
totalCount: operations.length
|
|
383
|
+
}
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
return result;
|
|
387
|
+
}
|
|
388
|
+
function compareByTimestamp(a, b) {
|
|
389
|
+
return HybridLogicalClock.compare(a.timestamp, b.timestamp);
|
|
390
|
+
}
|
|
391
|
+
function mergeIntoSorted(target, items) {
|
|
392
|
+
let insertIndex = 0;
|
|
393
|
+
for (const item of items) {
|
|
394
|
+
while (insertIndex < target.length) {
|
|
395
|
+
const existing = target[insertIndex];
|
|
396
|
+
if (existing && compareByTimestamp(item, existing) <= 0) break;
|
|
397
|
+
insertIndex++;
|
|
398
|
+
}
|
|
399
|
+
target.splice(insertIndex, 0, item);
|
|
400
|
+
insertIndex++;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
export {
|
|
405
|
+
KoraError,
|
|
406
|
+
SchemaValidationError,
|
|
407
|
+
OperationError,
|
|
408
|
+
MergeConflictError,
|
|
409
|
+
SyncError,
|
|
410
|
+
StorageError,
|
|
411
|
+
ClockDriftError,
|
|
412
|
+
HybridLogicalClock,
|
|
413
|
+
computeOperationId,
|
|
414
|
+
canonicalize,
|
|
415
|
+
createOperation,
|
|
416
|
+
validateOperationParams,
|
|
417
|
+
verifyOperationIntegrity,
|
|
418
|
+
isValidOperation,
|
|
419
|
+
topologicalSort
|
|
420
|
+
};
|
|
421
|
+
//# sourceMappingURL=chunk-ZMUY7AVV.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/errors/errors.ts","../src/clock/hlc.ts","../src/operations/content-hash.ts","../src/operations/operation.ts","../src/version-vector/topological-sort.ts"],"sourcesContent":["/**\n * Base error class for all Kora errors.\n * Every error includes a machine-readable code and optional context for debugging.\n */\nexport class KoraError extends Error {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic readonly code: string,\n\t\tpublic readonly context?: Record<string, unknown>,\n\t) {\n\t\tsuper(message)\n\t\tthis.name = 'KoraError'\n\t}\n}\n\n/**\n * Thrown when schema validation fails during defineSchema() or at app initialization.\n */\nexport class SchemaValidationError extends KoraError {\n\tconstructor(message: string, context?: Record<string, unknown>) {\n\t\tsuper(message, 'SCHEMA_VALIDATION', context)\n\t\tthis.name = 'SchemaValidationError'\n\t}\n}\n\n/**\n * Thrown when an operation is invalid or cannot be created.\n */\nexport class OperationError extends KoraError {\n\tconstructor(message: string, context?: Record<string, unknown>) {\n\t\tsuper(message, 'OPERATION_ERROR', context)\n\t\tthis.name = 'OperationError'\n\t}\n}\n\n/**\n * Thrown when a merge conflict cannot be automatically resolved.\n */\nexport class MergeConflictError extends KoraError {\n\tconstructor(\n\t\tpublic readonly operationA: { id: string; collection: string },\n\t\tpublic readonly operationB: { id: string; collection: string },\n\t\tpublic readonly field: string,\n\t) {\n\t\tsuper(\n\t\t\t`Merge conflict on field \"${field}\" in collection \"${operationA.collection}\"`,\n\t\t\t'MERGE_CONFLICT',\n\t\t\t{ operationA: operationA.id, operationB: operationB.id, field },\n\t\t)\n\t\tthis.name = 'MergeConflictError'\n\t}\n}\n\n/**\n * Thrown when a sync error occurs.\n */\nexport class SyncError extends KoraError {\n\tconstructor(message: string, context?: Record<string, unknown>) {\n\t\tsuper(message, 'SYNC_ERROR', context)\n\t\tthis.name = 'SyncError'\n\t}\n}\n\n/**\n * Thrown when a storage operation fails.\n */\nexport class StorageError extends KoraError {\n\tconstructor(message: string, context?: Record<string, unknown>) {\n\t\tsuper(message, 'STORAGE_ERROR', context)\n\t\tthis.name = 'StorageError'\n\t}\n}\n\n/**\n * Thrown when the HLC detects excessive clock drift.\n * Drift > 60s: warning. Drift > 5min: this error is thrown, refusing to generate timestamps.\n */\nexport class ClockDriftError extends KoraError {\n\tconstructor(\n\t\tpublic readonly currentHlcTime: number,\n\t\tpublic readonly physicalTime: number,\n\t) {\n\t\tconst driftSeconds = Math.round((currentHlcTime - physicalTime) / 1000)\n\t\tsuper(\n\t\t\t`Clock drift of ${driftSeconds}s detected. Physical time is behind HLC by more than 5 minutes. This indicates a severe clock issue.`,\n\t\t\t'CLOCK_DRIFT',\n\t\t\t{ currentHlcTime, physicalTime, driftSeconds },\n\t\t)\n\t\tthis.name = 'ClockDriftError'\n\t}\n}\n","import { ClockDriftError } from '../errors/errors'\nimport type { HLCTimestamp, TimeSource } from '../types'\n\n/** Default time source using the system clock */\nconst systemTimeSource: TimeSource = { now: () => Date.now() }\n\n/** Maximum allowed drift before warning (60 seconds) */\nconst DRIFT_WARN_MS = 60_000\n\n/** Maximum allowed drift before refusing to generate timestamps (5 minutes) */\nconst DRIFT_ERROR_MS = 5 * 60_000\n\n/**\n * Hybrid Logical Clock implementation based on Kulkarni et al.\n *\n * Provides a total order that respects causality without requiring synchronized clocks.\n * Each call to now() returns a timestamp strictly greater than the previous one.\n *\n * @example\n * ```typescript\n * const clock = new HybridLogicalClock('node-1')\n * const ts1 = clock.now()\n * const ts2 = clock.now()\n * // HybridLogicalClock.compare(ts1, ts2) < 0 (ts1 is earlier)\n * ```\n */\nexport class HybridLogicalClock {\n\tprivate wallTime = 0\n\tprivate logical = 0\n\n\tconstructor(\n\t\tprivate readonly nodeId: string,\n\t\tprivate readonly timeSource: TimeSource = systemTimeSource,\n\t\tprivate readonly onDriftWarning?: (driftMs: number) => void,\n\t) {}\n\n\t/**\n\t * Generate a new timestamp for a local event.\n\t * Guarantees monotonicity: each call returns a timestamp strictly greater than the previous.\n\t *\n\t * @throws {ClockDriftError} If physical time is more than 5 minutes behind the HLC wallTime\n\t */\n\tnow(): HLCTimestamp {\n\t\tconst physicalTime = this.timeSource.now()\n\t\tthis.checkDrift(physicalTime)\n\n\t\tif (physicalTime > this.wallTime) {\n\t\t\tthis.wallTime = physicalTime\n\t\t\tthis.logical = 0\n\t\t} else {\n\t\t\tthis.logical++\n\t\t}\n\n\t\treturn { wallTime: this.wallTime, logical: this.logical, nodeId: this.nodeId }\n\t}\n\n\t/**\n\t * Update clock on receiving a remote timestamp.\n\t * Merges the remote clock state with the local state to maintain causal ordering.\n\t *\n\t * @throws {ClockDriftError} If physical time is more than 5 minutes behind the resulting wallTime\n\t */\n\treceive(remote: HLCTimestamp): HLCTimestamp {\n\t\tconst physicalTime = this.timeSource.now()\n\n\t\tif (physicalTime > this.wallTime && physicalTime > remote.wallTime) {\n\t\t\tthis.wallTime = physicalTime\n\t\t\tthis.logical = 0\n\t\t} else if (remote.wallTime > this.wallTime) {\n\t\t\tthis.wallTime = remote.wallTime\n\t\t\tthis.logical = remote.logical + 1\n\t\t} else if (this.wallTime === remote.wallTime) {\n\t\t\tthis.logical = Math.max(this.logical, remote.logical) + 1\n\t\t} else {\n\t\t\t// this.wallTime > remote.wallTime && this.wallTime >= physicalTime\n\t\t\tthis.logical++\n\t\t}\n\n\t\tthis.checkDrift(physicalTime)\n\n\t\treturn { wallTime: this.wallTime, logical: this.logical, nodeId: this.nodeId }\n\t}\n\n\t/**\n\t * Compare two timestamps. Returns negative if a < b, positive if a > b, zero if equal.\n\t * Total order: wallTime first, then logical, then nodeId (lexicographic).\n\t */\n\tstatic compare(a: HLCTimestamp, b: HLCTimestamp): number {\n\t\tif (a.wallTime !== b.wallTime) return a.wallTime - b.wallTime\n\t\tif (a.logical !== b.logical) return a.logical - b.logical\n\t\tif (a.nodeId < b.nodeId) return -1\n\t\tif (a.nodeId > b.nodeId) return 1\n\t\treturn 0\n\t}\n\n\t/**\n\t * Serialize an HLC timestamp to a string that sorts lexicographically.\n\t * Format: zero-padded wallTime:logical:nodeId\n\t */\n\tstatic serialize(ts: HLCTimestamp): string {\n\t\tconst wall = ts.wallTime.toString().padStart(15, '0')\n\t\tconst log = ts.logical.toString().padStart(5, '0')\n\t\treturn `${wall}:${log}:${ts.nodeId}`\n\t}\n\n\t/**\n\t * Deserialize an HLC timestamp from its serialized string form.\n\t */\n\tstatic deserialize(s: string): HLCTimestamp {\n\t\tconst parts = s.split(':')\n\t\tif (parts.length < 3) {\n\t\t\tthrow new Error(`Invalid HLC timestamp string: \"${s}\"`)\n\t\t}\n\t\treturn {\n\t\t\twallTime: Number.parseInt(parts[0] ?? '0', 10),\n\t\t\tlogical: Number.parseInt(parts[1] ?? '0', 10),\n\t\t\t// nodeId may contain colons, so rejoin remaining parts\n\t\t\tnodeId: parts.slice(2).join(':'),\n\t\t}\n\t}\n\n\tprivate checkDrift(physicalTime: number): void {\n\t\tconst drift = this.wallTime - physicalTime\n\t\tif (drift > DRIFT_ERROR_MS) {\n\t\t\tthrow new ClockDriftError(this.wallTime, physicalTime)\n\t\t}\n\t\tif (drift > DRIFT_WARN_MS) {\n\t\t\tthis.onDriftWarning?.(drift)\n\t\t}\n\t}\n}\n","import type { OperationInput } from '../types'\n\n/**\n * Compute the content-addressed ID for an operation using SHA-256.\n * The same operation content always produces the same hash, ensuring deduplication.\n *\n * @param input - The operation input (without id/timestamp, which are assigned separately)\n * @param timestamp - The HLC timestamp serialized as a string\n * @returns A hex-encoded SHA-256 hash\n */\nexport async function computeOperationId(\n\tinput: OperationInput,\n\ttimestamp: string,\n): Promise<string> {\n\tconst canonical = canonicalize({\n\t\ttype: input.type,\n\t\tcollection: input.collection,\n\t\trecordId: input.recordId,\n\t\tdata: input.data,\n\t\ttimestamp,\n\t\tnodeId: input.nodeId,\n\t})\n\tconst encoded = new TextEncoder().encode(canonical)\n\tconst hashBuffer = await globalThis.crypto.subtle.digest('SHA-256', encoded)\n\treturn bufferToHex(hashBuffer)\n}\n\n/**\n * Deterministic JSON serialization with sorted keys.\n * Ensures identical objects always produce identical strings regardless of property insertion order.\n *\n * @param obj - The value to serialize\n * @returns A deterministic JSON string\n */\nexport function canonicalize(obj: unknown): string {\n\tif (obj === null || obj === undefined) {\n\t\treturn JSON.stringify(obj)\n\t}\n\n\tif (typeof obj !== 'object') {\n\t\treturn JSON.stringify(obj)\n\t}\n\n\tif (Array.isArray(obj)) {\n\t\tconst items = obj.map((item) => canonicalize(item))\n\t\treturn `[${items.join(',')}]`\n\t}\n\n\tconst keys = Object.keys(obj as Record<string, unknown>).sort()\n\tconst pairs = keys.map((key) => {\n\t\tconst value = (obj as Record<string, unknown>)[key]\n\t\treturn `${JSON.stringify(key)}:${canonicalize(value)}`\n\t})\n\treturn `{${pairs.join(',')}}`\n}\n\nfunction bufferToHex(buffer: ArrayBuffer): string {\n\tconst bytes = new Uint8Array(buffer)\n\treturn Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('')\n}\n","import { HybridLogicalClock } from '../clock/hlc'\nimport { OperationError } from '../errors/errors'\nimport type { HLCTimestamp, Operation, OperationInput } from '../types'\nimport { computeOperationId } from './content-hash'\n\n/**\n * Creates an immutable, content-addressed Operation from the given parameters.\n * The operation is deep-frozen after creation — it cannot be modified.\n *\n * @param input - The operation parameters (without id, which is computed)\n * @param clock - The HLC clock to generate the timestamp\n * @returns A frozen Operation with a content-addressed id\n *\n * @example\n * ```typescript\n * const op = await createOperation({\n * nodeId: 'device-1',\n * type: 'insert',\n * collection: 'todos',\n * recordId: 'rec-1',\n * data: { title: 'Ship it' },\n * previousData: null,\n * sequenceNumber: 1,\n * causalDeps: [],\n * schemaVersion: 1,\n * }, clock)\n * ```\n */\nexport async function createOperation(\n\tinput: OperationInput,\n\tclock: HybridLogicalClock,\n): Promise<Operation> {\n\tvalidateOperationParams(input)\n\n\tconst timestamp = clock.now()\n\tconst serializedTs = HybridLogicalClock.serialize(timestamp)\n\tconst id = await computeOperationId(input, serializedTs)\n\n\tconst operation: Operation = {\n\t\tid,\n\t\tnodeId: input.nodeId,\n\t\ttype: input.type,\n\t\tcollection: input.collection,\n\t\trecordId: input.recordId,\n\t\tdata: input.data ? { ...input.data } : null,\n\t\tpreviousData: input.previousData ? { ...input.previousData } : null,\n\t\ttimestamp,\n\t\tsequenceNumber: input.sequenceNumber,\n\t\tcausalDeps: [...input.causalDeps],\n\t\tschemaVersion: input.schemaVersion,\n\t}\n\n\treturn deepFreeze(operation)\n}\n\n/**\n * Validates operation input parameters. Throws OperationError with\n * contextual information on validation failure.\n */\nexport function validateOperationParams(input: OperationInput): void {\n\tif (!input.nodeId || typeof input.nodeId !== 'string') {\n\t\tthrow new OperationError('nodeId is required and must be a non-empty string', {\n\t\t\treceived: input.nodeId,\n\t\t})\n\t}\n\n\tif (!input.type || !['insert', 'update', 'delete'].includes(input.type)) {\n\t\tthrow new OperationError('type must be \"insert\", \"update\", or \"delete\"', {\n\t\t\treceived: input.type,\n\t\t})\n\t}\n\n\tif (!input.collection || typeof input.collection !== 'string') {\n\t\tthrow new OperationError('collection is required and must be a non-empty string', {\n\t\t\treceived: input.collection,\n\t\t})\n\t}\n\n\tif (!input.recordId || typeof input.recordId !== 'string') {\n\t\tthrow new OperationError('recordId is required and must be a non-empty string', {\n\t\t\treceived: input.recordId,\n\t\t})\n\t}\n\n\tif (input.type === 'insert' && input.data === null) {\n\t\tthrow new OperationError('insert operations must include data', {\n\t\t\ttype: input.type,\n\t\t\tcollection: input.collection,\n\t\t})\n\t}\n\n\tif (input.type === 'update' && input.data === null) {\n\t\tthrow new OperationError('update operations must include data with changed fields', {\n\t\t\ttype: input.type,\n\t\t\tcollection: input.collection,\n\t\t})\n\t}\n\n\tif (input.type === 'update' && input.previousData === null) {\n\t\tthrow new OperationError(\n\t\t\t'update operations must include previousData for 3-way merge support',\n\t\t\t{\n\t\t\t\ttype: input.type,\n\t\t\t\tcollection: input.collection,\n\t\t\t},\n\t\t)\n\t}\n\n\tif (input.type === 'delete' && input.data !== null) {\n\t\tthrow new OperationError('delete operations must have null data', {\n\t\t\ttype: input.type,\n\t\t\tcollection: input.collection,\n\t\t})\n\t}\n\n\tif (typeof input.sequenceNumber !== 'number' || input.sequenceNumber < 0) {\n\t\tthrow new OperationError('sequenceNumber must be a non-negative number', {\n\t\t\treceived: input.sequenceNumber,\n\t\t})\n\t}\n\n\tif (!Array.isArray(input.causalDeps)) {\n\t\tthrow new OperationError('causalDeps must be an array of operation IDs', {\n\t\t\treceived: typeof input.causalDeps,\n\t\t})\n\t}\n\n\tif (typeof input.schemaVersion !== 'number' || input.schemaVersion < 1) {\n\t\tthrow new OperationError('schemaVersion must be a positive number', {\n\t\t\treceived: input.schemaVersion,\n\t\t})\n\t}\n}\n\n/**\n * Verify the integrity of an operation by recomputing its content hash.\n * Returns true if the id matches the recomputed hash.\n */\nexport async function verifyOperationIntegrity(op: Operation): Promise<boolean> {\n\tconst input: OperationInput = {\n\t\tnodeId: op.nodeId,\n\t\ttype: op.type,\n\t\tcollection: op.collection,\n\t\trecordId: op.recordId,\n\t\tdata: op.data,\n\t\tpreviousData: op.previousData,\n\t\tsequenceNumber: op.sequenceNumber,\n\t\tcausalDeps: op.causalDeps,\n\t\tschemaVersion: op.schemaVersion,\n\t}\n\tconst serializedTs = HybridLogicalClock.serialize(op.timestamp)\n\tconst expectedId = await computeOperationId(input, serializedTs)\n\treturn op.id === expectedId\n}\n\n/**\n * Type guard for Operation interface.\n */\nexport function isValidOperation(value: unknown): value is Operation {\n\tif (typeof value !== 'object' || value === null) return false\n\tconst op = value as Record<string, unknown>\n\treturn (\n\t\ttypeof op.id === 'string' &&\n\t\ttypeof op.nodeId === 'string' &&\n\t\t(op.type === 'insert' || op.type === 'update' || op.type === 'delete') &&\n\t\ttypeof op.collection === 'string' &&\n\t\ttypeof op.recordId === 'string' &&\n\t\ttypeof op.sequenceNumber === 'number' &&\n\t\tArray.isArray(op.causalDeps) &&\n\t\ttypeof op.schemaVersion === 'number' &&\n\t\ttypeof op.timestamp === 'object' &&\n\t\top.timestamp !== null\n\t)\n}\n\nfunction deepFreeze<T>(obj: T): T {\n\tif (typeof obj !== 'object' || obj === null) return obj\n\tObject.freeze(obj)\n\tfor (const value of Object.values(obj)) {\n\t\tif (typeof value === 'object' && value !== null && !Object.isFrozen(value)) {\n\t\t\tdeepFreeze(value)\n\t\t}\n\t}\n\treturn obj\n}\n","import { HybridLogicalClock } from '../clock/hlc'\nimport { OperationError } from '../errors/errors'\nimport type { Operation } from '../types'\n\n/**\n * Topological sort of operations based on their causal dependency DAG.\n * Uses Kahn's algorithm with deterministic tie-breaking via HLC timestamp.\n * Time complexity: O(V + E) where V = operations, E = causal dependency edges.\n *\n * @param operations - The operations to sort\n * @returns Operations in causal order (dependencies before dependents)\n * @throws {OperationError} If a cycle is detected in the dependency graph\n */\nexport function topologicalSort(operations: Operation[]): Operation[] {\n\tif (operations.length <= 1) return [...operations]\n\n\t// Build adjacency list and in-degree map\n\tconst opMap = new Map<string, Operation>()\n\tfor (const op of operations) {\n\t\topMap.set(op.id, op)\n\t}\n\n\t// Only count edges where both ends are in the operation set\n\tconst inDegree = new Map<string, number>()\n\tconst dependents = new Map<string, string[]>()\n\n\tfor (const op of operations) {\n\t\tif (!inDegree.has(op.id)) {\n\t\t\tinDegree.set(op.id, 0)\n\t\t}\n\t\tif (!dependents.has(op.id)) {\n\t\t\tdependents.set(op.id, [])\n\t\t}\n\n\t\tfor (const depId of op.causalDeps) {\n\t\t\tif (opMap.has(depId)) {\n\t\t\t\t// depId -> op.id edge (depId must come before op.id)\n\t\t\t\tinDegree.set(op.id, (inDegree.get(op.id) ?? 0) + 1)\n\t\t\t\tconst deps = dependents.get(depId)\n\t\t\t\tif (deps) {\n\t\t\t\t\tdeps.push(op.id)\n\t\t\t\t} else {\n\t\t\t\t\tdependents.set(depId, [op.id])\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t// Initialize queue with nodes that have no in-set dependencies\n\t// Use a sorted array for deterministic ordering (by HLC timestamp)\n\tconst queue: Operation[] = []\n\tfor (const op of operations) {\n\t\tif ((inDegree.get(op.id) ?? 0) === 0) {\n\t\t\tqueue.push(op)\n\t\t}\n\t}\n\tqueue.sort(compareByTimestamp)\n\n\tconst result: Operation[] = []\n\n\twhile (queue.length > 0) {\n\t\t// Take the earliest operation (deterministic tie-breaking by HLC)\n\t\tconst current = queue.shift()\n\t\tif (!current) break\n\t\tresult.push(current)\n\n\t\tconst deps = dependents.get(current.id) ?? []\n\t\tconst newlyReady: Operation[] = []\n\n\t\tfor (const depId of deps) {\n\t\t\tconst deg = (inDegree.get(depId) ?? 0) - 1\n\t\t\tinDegree.set(depId, deg)\n\t\t\tif (deg === 0) {\n\t\t\t\tconst op = opMap.get(depId)\n\t\t\t\tif (op) newlyReady.push(op)\n\t\t\t}\n\t\t}\n\n\t\t// Sort newly ready operations and merge into queue maintaining sort order\n\t\tif (newlyReady.length > 0) {\n\t\t\tnewlyReady.sort(compareByTimestamp)\n\t\t\tmergeIntoSorted(queue, newlyReady)\n\t\t}\n\t}\n\n\tif (result.length !== operations.length) {\n\t\tthrow new OperationError(\n\t\t\t`Cycle detected in operation dependency graph. Sorted ${result.length} of ${operations.length} operations.`,\n\t\t\t{\n\t\t\t\tsortedCount: result.length,\n\t\t\t\ttotalCount: operations.length,\n\t\t\t},\n\t\t)\n\t}\n\n\treturn result\n}\n\nfunction compareByTimestamp(a: Operation, b: Operation): number {\n\treturn HybridLogicalClock.compare(a.timestamp, b.timestamp)\n}\n\n/** Merge sorted `items` into an already-sorted `target` array, maintaining sort order. */\nfunction mergeIntoSorted(target: Operation[], items: Operation[]): void {\n\tlet insertIndex = 0\n\tfor (const item of items) {\n\t\twhile (insertIndex < target.length) {\n\t\t\tconst existing = target[insertIndex]\n\t\t\tif (existing && compareByTimestamp(item, existing) <= 0) break\n\t\t\tinsertIndex++\n\t\t}\n\t\ttarget.splice(insertIndex, 0, item)\n\t\tinsertIndex++\n\t}\n}\n"],"mappings":";AAIO,IAAM,YAAN,cAAwB,MAAM;AAAA,EACpC,YACC,SACgB,MACA,SACf;AACD,UAAM,OAAO;AAHG;AACA;AAGhB,SAAK,OAAO;AAAA,EACb;AAAA,EALiB;AAAA,EACA;AAKlB;AAKO,IAAM,wBAAN,cAAoC,UAAU;AAAA,EACpD,YAAY,SAAiB,SAAmC;AAC/D,UAAM,SAAS,qBAAqB,OAAO;AAC3C,SAAK,OAAO;AAAA,EACb;AACD;AAKO,IAAM,iBAAN,cAA6B,UAAU;AAAA,EAC7C,YAAY,SAAiB,SAAmC;AAC/D,UAAM,SAAS,mBAAmB,OAAO;AACzC,SAAK,OAAO;AAAA,EACb;AACD;AAKO,IAAM,qBAAN,cAAiC,UAAU;AAAA,EACjD,YACiB,YACA,YACA,OACf;AACD;AAAA,MACC,4BAA4B,KAAK,oBAAoB,WAAW,UAAU;AAAA,MAC1E;AAAA,MACA,EAAE,YAAY,WAAW,IAAI,YAAY,WAAW,IAAI,MAAM;AAA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