@korajs/core 1.0.0-beta.11 → 1.0.0-beta.13

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.
@@ -1,164 +0,0 @@
1
- import { P as TimeSource, H as HLCTimestamp, x as OperationInput, f as Operation } from './events-BPbYUbrE.cjs';
2
-
3
- /**
4
- * Highest logical counter value that fits the 5-digit zero-padded slot in the
5
- * serialized timestamp form. The serialized string must sort lexicographically
6
- * in exactly the same order as {@link HybridLogicalClock.compare}; a 6-digit
7
- * logical value would shift the padding and silently break that invariant for
8
- * every stored `_version` column and operation timestamp. When the counter
9
- * would exceed this cap, the clock carries into wallTime instead
10
- * (wallTime + 1, logical = 0), which preserves both monotonicity and the
11
- * serialized ordering.
12
- */
13
- declare const MAX_LOGICAL = 99999;
14
- /**
15
- * Hybrid Logical Clock implementation based on Kulkarni et al.
16
- *
17
- * Provides a total order that respects causality without requiring synchronized clocks.
18
- * Each call to now() returns a timestamp strictly greater than the previous one.
19
- *
20
- * @example
21
- * ```typescript
22
- * const clock = new HybridLogicalClock('node-1')
23
- * const ts1 = clock.now()
24
- * const ts2 = clock.now()
25
- * // HybridLogicalClock.compare(ts1, ts2) < 0 (ts1 is earlier)
26
- * ```
27
- */
28
- declare class HybridLogicalClock {
29
- private readonly nodeId;
30
- private readonly timeSource;
31
- private readonly onDriftWarning?;
32
- private readonly onDriftError?;
33
- private wallTime;
34
- private logical;
35
- private referenceOffsetMs;
36
- constructor(nodeId: string, timeSource?: TimeSource, onDriftWarning?: ((driftMs: number) => void) | undefined, onDriftError?: ((driftMs: number) => void) | undefined);
37
- /**
38
- * Records the known offset between an external time reference (usually the
39
- * sync server, learned at handshake) and this device's physical clock:
40
- * `referenceTime - localPhysicalTime`. Once set, drift evaluation and remote
41
- * timestamp validation are performed against reference-corrected time, so a
42
- * device with a wrong local clock still validates remote timestamps correctly.
43
- */
44
- setReferenceOffset(offsetMs: number): void;
45
- getReferenceOffset(): number | null;
46
- /** Physical time corrected by the known reference offset, when available. */
47
- private effectiveTime;
48
- /**
49
- * Generate a new timestamp for a local event.
50
- * Guarantees monotonicity: each call returns a timestamp strictly greater than the previous.
51
- *
52
- * Never throws and never blocks a local write: if the physical clock has
53
- * fallen behind the HLC (e.g. the user corrected a fast clock), the HLC
54
- * freezes wallTime and advances the logical counter, and drift is reported
55
- * through the onDriftWarning / onDriftError callbacks instead.
56
- */
57
- now(): HLCTimestamp;
58
- /**
59
- * Update clock on receiving a remote timestamp.
60
- * Merges the remote clock state with the local state to maintain causal ordering.
61
- *
62
- * @throws {InvalidTimestampError} If the remote timestamp has non-integer or
63
- * negative wallTime/logical, or logical beyond {@link MAX_LOGICAL}. Checked
64
- * BEFORE any state change, so malformed input cannot corrupt this clock.
65
- * @throws {RemoteClockDriftError} If the remote timestamp is more than 5 minutes
66
- * ahead of reference-corrected local time. Validation happens BEFORE any state
67
- * is adopted, so a rejected timestamp cannot poison this clock. When no
68
- * reference offset is known and this clock is uninitialized (cold start with
69
- * a possibly-wrong local clock), validation is skipped, matching the previous
70
- * cold-start behavior but now without a corrupting failure mode afterward.
71
- */
72
- receive(remote: HLCTimestamp): HLCTimestamp;
73
- /**
74
- * Advance this clock to at least the given timestamp.
75
- *
76
- * Used after a timestamp rebase rewrites unsynced operations: future `now()`
77
- * timestamps must sort after every rebased operation, otherwise a write issued
78
- * immediately after the rebase could interleave with (or precede) rebased ops
79
- * and break the log's total order. Never moves the clock backward — a
80
- * timestamp at or before the current state is a no-op, preserving the
81
- * monotonicity guarantee of `now()`.
82
- *
83
- * Inputs with logical > MAX_LOGICAL are normalized deterministically by
84
- * carrying the excess into wallTime, so the adopted state always leaves room
85
- * for the next increment inside the serializable range.
86
- */
87
- advanceTo(ts: HLCTimestamp): void;
88
- /**
89
- * Carry the logical counter into wallTime when an increment pushed it past
90
- * MAX_LOGICAL. Bumping wallTime by 1ms keeps the timestamp strictly greater
91
- * than everything issued before (monotonicity) while keeping the logical
92
- * counter inside the 5-digit slot the serialized form depends on.
93
- */
94
- private carryLogicalOverflow;
95
- /**
96
- * Compare two timestamps. Returns negative if a < b, positive if a > b, zero if equal.
97
- * Total order: wallTime first, then logical, then nodeId (lexicographic).
98
- */
99
- static compare(a: HLCTimestamp, b: HLCTimestamp): number;
100
- /**
101
- * Serialize an HLC timestamp to a string that sorts lexicographically.
102
- * Format: zero-padded wallTime:logical:nodeId
103
- *
104
- * @throws {InvalidTimestampError} If wallTime does not fit 15 digits or
105
- * logical does not fit 5 digits (or either is negative/non-integer). Such a
106
- * value would overflow its zero-padded slot and the serialized string would
107
- * no longer sort in the same order as {@link HybridLogicalClock.compare} —
108
- * silently corrupting every LWW comparison on stored `_version` columns.
109
- * Internal clocks can no longer produce such values (the logical counter
110
- * carries into wallTime); this guards against hand-built timestamps.
111
- */
112
- static serialize(ts: HLCTimestamp): string;
113
- /**
114
- * Deserialize an HLC timestamp from its serialized string form.
115
- */
116
- static deserialize(s: string): HLCTimestamp;
117
- /**
118
- * Reports drift between the HLC and (reference-corrected) physical time.
119
- * Reporting only: local timestamp generation is never blocked, because the
120
- * user's data always outranks the quality of its timestamps.
121
- */
122
- private checkDrift;
123
- }
124
-
125
- /**
126
- * Creates an immutable, content-addressed Operation from the given parameters.
127
- * The operation is deep-frozen after creation — it cannot be modified.
128
- *
129
- * @param input - The operation parameters (without id, which is computed)
130
- * @param clock - The HLC clock to generate the timestamp
131
- * @returns A frozen Operation with a content-addressed id
132
- *
133
- * @example
134
- * ```typescript
135
- * const op = await createOperation({
136
- * nodeId: 'device-1',
137
- * type: 'insert',
138
- * collection: 'todos',
139
- * recordId: 'rec-1',
140
- * data: { title: 'Ship it' },
141
- * previousData: null,
142
- * sequenceNumber: 1,
143
- * causalDeps: [],
144
- * schemaVersion: 1,
145
- * }, clock)
146
- * ```
147
- */
148
- declare function createOperation(input: OperationInput, clock: HybridLogicalClock): Promise<Operation>;
149
- /**
150
- * Validates operation input parameters. Throws OperationError with
151
- * contextual information on validation failure.
152
- */
153
- declare function validateOperationParams(input: OperationInput): void;
154
- /**
155
- * Verify the integrity of an operation by recomputing its content hash.
156
- * Returns true if the id matches the recomputed hash.
157
- */
158
- declare function verifyOperationIntegrity(op: Operation): Promise<boolean>;
159
- /**
160
- * Type guard for Operation interface.
161
- */
162
- declare function isValidOperation(value: unknown): value is Operation;
163
-
164
- export { HybridLogicalClock as H, MAX_LOGICAL as M, validateOperationParams as a, createOperation as c, isValidOperation as i, verifyOperationIntegrity as v };
@@ -1,164 +0,0 @@
1
- import { P as TimeSource, H as HLCTimestamp, x as OperationInput, f as Operation } from './events-BPbYUbrE.js';
2
-
3
- /**
4
- * Highest logical counter value that fits the 5-digit zero-padded slot in the
5
- * serialized timestamp form. The serialized string must sort lexicographically
6
- * in exactly the same order as {@link HybridLogicalClock.compare}; a 6-digit
7
- * logical value would shift the padding and silently break that invariant for
8
- * every stored `_version` column and operation timestamp. When the counter
9
- * would exceed this cap, the clock carries into wallTime instead
10
- * (wallTime + 1, logical = 0), which preserves both monotonicity and the
11
- * serialized ordering.
12
- */
13
- declare const MAX_LOGICAL = 99999;
14
- /**
15
- * Hybrid Logical Clock implementation based on Kulkarni et al.
16
- *
17
- * Provides a total order that respects causality without requiring synchronized clocks.
18
- * Each call to now() returns a timestamp strictly greater than the previous one.
19
- *
20
- * @example
21
- * ```typescript
22
- * const clock = new HybridLogicalClock('node-1')
23
- * const ts1 = clock.now()
24
- * const ts2 = clock.now()
25
- * // HybridLogicalClock.compare(ts1, ts2) < 0 (ts1 is earlier)
26
- * ```
27
- */
28
- declare class HybridLogicalClock {
29
- private readonly nodeId;
30
- private readonly timeSource;
31
- private readonly onDriftWarning?;
32
- private readonly onDriftError?;
33
- private wallTime;
34
- private logical;
35
- private referenceOffsetMs;
36
- constructor(nodeId: string, timeSource?: TimeSource, onDriftWarning?: ((driftMs: number) => void) | undefined, onDriftError?: ((driftMs: number) => void) | undefined);
37
- /**
38
- * Records the known offset between an external time reference (usually the
39
- * sync server, learned at handshake) and this device's physical clock:
40
- * `referenceTime - localPhysicalTime`. Once set, drift evaluation and remote
41
- * timestamp validation are performed against reference-corrected time, so a
42
- * device with a wrong local clock still validates remote timestamps correctly.
43
- */
44
- setReferenceOffset(offsetMs: number): void;
45
- getReferenceOffset(): number | null;
46
- /** Physical time corrected by the known reference offset, when available. */
47
- private effectiveTime;
48
- /**
49
- * Generate a new timestamp for a local event.
50
- * Guarantees monotonicity: each call returns a timestamp strictly greater than the previous.
51
- *
52
- * Never throws and never blocks a local write: if the physical clock has
53
- * fallen behind the HLC (e.g. the user corrected a fast clock), the HLC
54
- * freezes wallTime and advances the logical counter, and drift is reported
55
- * through the onDriftWarning / onDriftError callbacks instead.
56
- */
57
- now(): HLCTimestamp;
58
- /**
59
- * Update clock on receiving a remote timestamp.
60
- * Merges the remote clock state with the local state to maintain causal ordering.
61
- *
62
- * @throws {InvalidTimestampError} If the remote timestamp has non-integer or
63
- * negative wallTime/logical, or logical beyond {@link MAX_LOGICAL}. Checked
64
- * BEFORE any state change, so malformed input cannot corrupt this clock.
65
- * @throws {RemoteClockDriftError} If the remote timestamp is more than 5 minutes
66
- * ahead of reference-corrected local time. Validation happens BEFORE any state
67
- * is adopted, so a rejected timestamp cannot poison this clock. When no
68
- * reference offset is known and this clock is uninitialized (cold start with
69
- * a possibly-wrong local clock), validation is skipped, matching the previous
70
- * cold-start behavior but now without a corrupting failure mode afterward.
71
- */
72
- receive(remote: HLCTimestamp): HLCTimestamp;
73
- /**
74
- * Advance this clock to at least the given timestamp.
75
- *
76
- * Used after a timestamp rebase rewrites unsynced operations: future `now()`
77
- * timestamps must sort after every rebased operation, otherwise a write issued
78
- * immediately after the rebase could interleave with (or precede) rebased ops
79
- * and break the log's total order. Never moves the clock backward — a
80
- * timestamp at or before the current state is a no-op, preserving the
81
- * monotonicity guarantee of `now()`.
82
- *
83
- * Inputs with logical > MAX_LOGICAL are normalized deterministically by
84
- * carrying the excess into wallTime, so the adopted state always leaves room
85
- * for the next increment inside the serializable range.
86
- */
87
- advanceTo(ts: HLCTimestamp): void;
88
- /**
89
- * Carry the logical counter into wallTime when an increment pushed it past
90
- * MAX_LOGICAL. Bumping wallTime by 1ms keeps the timestamp strictly greater
91
- * than everything issued before (monotonicity) while keeping the logical
92
- * counter inside the 5-digit slot the serialized form depends on.
93
- */
94
- private carryLogicalOverflow;
95
- /**
96
- * Compare two timestamps. Returns negative if a < b, positive if a > b, zero if equal.
97
- * Total order: wallTime first, then logical, then nodeId (lexicographic).
98
- */
99
- static compare(a: HLCTimestamp, b: HLCTimestamp): number;
100
- /**
101
- * Serialize an HLC timestamp to a string that sorts lexicographically.
102
- * Format: zero-padded wallTime:logical:nodeId
103
- *
104
- * @throws {InvalidTimestampError} If wallTime does not fit 15 digits or
105
- * logical does not fit 5 digits (or either is negative/non-integer). Such a
106
- * value would overflow its zero-padded slot and the serialized string would
107
- * no longer sort in the same order as {@link HybridLogicalClock.compare} —
108
- * silently corrupting every LWW comparison on stored `_version` columns.
109
- * Internal clocks can no longer produce such values (the logical counter
110
- * carries into wallTime); this guards against hand-built timestamps.
111
- */
112
- static serialize(ts: HLCTimestamp): string;
113
- /**
114
- * Deserialize an HLC timestamp from its serialized string form.
115
- */
116
- static deserialize(s: string): HLCTimestamp;
117
- /**
118
- * Reports drift between the HLC and (reference-corrected) physical time.
119
- * Reporting only: local timestamp generation is never blocked, because the
120
- * user's data always outranks the quality of its timestamps.
121
- */
122
- private checkDrift;
123
- }
124
-
125
- /**
126
- * Creates an immutable, content-addressed Operation from the given parameters.
127
- * The operation is deep-frozen after creation — it cannot be modified.
128
- *
129
- * @param input - The operation parameters (without id, which is computed)
130
- * @param clock - The HLC clock to generate the timestamp
131
- * @returns A frozen Operation with a content-addressed id
132
- *
133
- * @example
134
- * ```typescript
135
- * const op = await createOperation({
136
- * nodeId: 'device-1',
137
- * type: 'insert',
138
- * collection: 'todos',
139
- * recordId: 'rec-1',
140
- * data: { title: 'Ship it' },
141
- * previousData: null,
142
- * sequenceNumber: 1,
143
- * causalDeps: [],
144
- * schemaVersion: 1,
145
- * }, clock)
146
- * ```
147
- */
148
- declare function createOperation(input: OperationInput, clock: HybridLogicalClock): Promise<Operation>;
149
- /**
150
- * Validates operation input parameters. Throws OperationError with
151
- * contextual information on validation failure.
152
- */
153
- declare function validateOperationParams(input: OperationInput): void;
154
- /**
155
- * Verify the integrity of an operation by recomputing its content hash.
156
- * Returns true if the id matches the recomputed hash.
157
- */
158
- declare function verifyOperationIntegrity(op: Operation): Promise<boolean>;
159
- /**
160
- * Type guard for Operation interface.
161
- */
162
- declare function isValidOperation(value: unknown): value is Operation;
163
-
164
- export { HybridLogicalClock as H, MAX_LOGICAL as M, validateOperationParams as a, createOperation as c, isValidOperation as i, verifyOperationIntegrity as v };