@verdant-web/common 1.11.0 → 1.12.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/cjs/EventSubscriber.d.ts +17 -17
- package/dist/cjs/EventSubscriber.js +66 -66
- package/dist/cjs/baseline.d.ts +5 -5
- package/dist/cjs/baseline.js +2 -2
- package/dist/cjs/batching.d.ts +41 -41
- package/dist/cjs/batching.js +88 -88
- package/dist/cjs/files.d.ts +19 -19
- package/dist/cjs/files.js +18 -18
- package/dist/cjs/index.d.ts +18 -18
- package/dist/cjs/index.js +35 -35
- package/dist/cjs/indexes.d.ts +14 -14
- package/dist/cjs/indexes.js +115 -115
- package/dist/cjs/indexes.test.d.ts +1 -1
- package/dist/cjs/indexes.test.js +127 -127
- package/dist/cjs/migration.d.ts +98 -98
- package/dist/cjs/migration.js +127 -127
- package/dist/cjs/oids.d.ts +85 -87
- package/dist/cjs/oids.js +363 -374
- package/dist/cjs/oids.js.map +1 -1
- package/dist/cjs/oids.test.d.ts +1 -1
- package/dist/cjs/oids.test.js +269 -251
- package/dist/cjs/oids.test.js.map +1 -1
- package/dist/cjs/operation.d.ts +139 -139
- package/dist/cjs/operation.js +532 -532
- package/dist/cjs/operation.js.map +1 -1
- package/dist/cjs/operation.test.d.ts +1 -1
- package/dist/cjs/operation.test.js +505 -505
- package/dist/cjs/operation.test.js.map +1 -1
- package/dist/cjs/patch.d.ts +26 -26
- package/dist/cjs/patch.js +226 -226
- package/dist/cjs/patch.js.map +1 -1
- package/dist/cjs/presence.d.ts +27 -27
- package/dist/cjs/presence.js +2 -2
- package/dist/cjs/protocol.d.ts +145 -145
- package/dist/cjs/protocol.js +3 -3
- package/dist/cjs/refs.d.ts +5 -5
- package/dist/cjs/refs.js +21 -21
- package/dist/cjs/replica.d.ts +28 -28
- package/dist/cjs/replica.js +28 -28
- package/dist/cjs/schema/defaults.d.ts +4 -4
- package/dist/cjs/schema/defaults.js +71 -71
- package/dist/cjs/schema/defaults.test.d.ts +1 -1
- package/dist/cjs/schema/defaults.test.js +20 -20
- package/dist/cjs/schema/defaults.test.js.map +1 -1
- package/dist/cjs/schema/index.d.ts +8 -8
- package/dist/cjs/schema/index.js +28 -28
- package/dist/cjs/schema/indexFilters.d.ts +6 -6
- package/dist/cjs/schema/indexFilters.js +30 -30
- package/dist/cjs/schema/types/collection.d.ts +51 -51
- package/dist/cjs/schema/types/collection.js +2 -2
- package/dist/cjs/schema/types/compounds.d.ts +12 -12
- package/dist/cjs/schema/types/compounds.js +2 -2
- package/dist/cjs/schema/types/fields.d.ts +59 -59
- package/dist/cjs/schema/types/fields.js +2 -2
- package/dist/cjs/schema/types/filters.d.ts +29 -29
- package/dist/cjs/schema/types/filters.js +2 -2
- package/dist/cjs/schema/types/shapes.d.ts +21 -21
- package/dist/cjs/schema/types/shapes.js +2 -2
- package/dist/cjs/schema/types/synthetics.d.ts +28 -28
- package/dist/cjs/schema/types/synthetics.js +2 -2
- package/dist/cjs/schema/types.d.ts +18 -18
- package/dist/cjs/schema/types.js +22 -22
- package/dist/cjs/timestamp.d.ts +48 -48
- package/dist/cjs/timestamp.js +243 -243
- package/dist/cjs/timestamp.test.d.ts +1 -1
- package/dist/cjs/timestamp.test.js +93 -93
- package/dist/cjs/undo.d.ts +3 -3
- package/dist/cjs/undo.js +198 -198
- package/dist/cjs/utils.d.ts +15 -15
- package/dist/cjs/utils.js +127 -127
- package/dist/cjs/utils.test.d.ts +1 -1
- package/dist/cjs/utils.test.js +36 -36
- package/dist/esm/EventSubscriber.d.ts +17 -17
- package/dist/esm/EventSubscriber.js +62 -62
- package/dist/esm/baseline.d.ts +5 -5
- package/dist/esm/baseline.js +1 -1
- package/dist/esm/batching.d.ts +41 -41
- package/dist/esm/batching.js +83 -83
- package/dist/esm/files.d.ts +19 -19
- package/dist/esm/files.js +12 -12
- package/dist/esm/index.d.ts +18 -18
- package/dist/esm/index.js +15 -15
- package/dist/esm/indexes.d.ts +14 -14
- package/dist/esm/indexes.js +105 -105
- package/dist/esm/indexes.test.d.ts +1 -1
- package/dist/esm/indexes.test.js +125 -125
- package/dist/esm/migration.d.ts +98 -98
- package/dist/esm/migration.js +121 -121
- package/dist/esm/oids.d.ts +85 -87
- package/dist/esm/oids.js +336 -347
- package/dist/esm/oids.js.map +1 -1
- package/dist/esm/oids.test.d.ts +1 -1
- package/dist/esm/oids.test.js +267 -249
- package/dist/esm/oids.test.js.map +1 -1
- package/dist/esm/operation.d.ts +139 -139
- package/dist/esm/operation.js +517 -517
- package/dist/esm/operation.js.map +1 -1
- package/dist/esm/operation.test.d.ts +1 -1
- package/dist/esm/operation.test.js +503 -503
- package/dist/esm/operation.test.js.map +1 -1
- package/dist/esm/patch.d.ts +26 -26
- package/dist/esm/patch.js +222 -222
- package/dist/esm/patch.js.map +1 -1
- package/dist/esm/presence.d.ts +27 -27
- package/dist/esm/presence.js +1 -1
- package/dist/esm/protocol.d.ts +145 -145
- package/dist/esm/protocol.js +2 -2
- package/dist/esm/refs.d.ts +5 -5
- package/dist/esm/refs.js +16 -16
- package/dist/esm/replica.d.ts +28 -28
- package/dist/esm/replica.js +25 -25
- package/dist/esm/schema/defaults.d.ts +4 -4
- package/dist/esm/schema/defaults.js +65 -65
- package/dist/esm/schema/defaults.test.d.ts +1 -1
- package/dist/esm/schema/defaults.test.js +18 -18
- package/dist/esm/schema/defaults.test.js.map +1 -1
- package/dist/esm/schema/index.d.ts +8 -8
- package/dist/esm/schema/index.js +9 -9
- package/dist/esm/schema/indexFilters.d.ts +6 -6
- package/dist/esm/schema/indexFilters.js +22 -22
- package/dist/esm/schema/types/collection.d.ts +51 -51
- package/dist/esm/schema/types/collection.js +1 -1
- package/dist/esm/schema/types/compounds.d.ts +12 -12
- package/dist/esm/schema/types/compounds.js +1 -1
- package/dist/esm/schema/types/fields.d.ts +59 -59
- package/dist/esm/schema/types/fields.js +1 -1
- package/dist/esm/schema/types/filters.d.ts +29 -29
- package/dist/esm/schema/types/filters.js +1 -1
- package/dist/esm/schema/types/shapes.d.ts +21 -21
- package/dist/esm/schema/types/shapes.js +1 -1
- package/dist/esm/schema/types/synthetics.d.ts +28 -28
- package/dist/esm/schema/types/synthetics.js +1 -1
- package/dist/esm/schema/types.d.ts +18 -18
- package/dist/esm/schema/types.js +6 -6
- package/dist/esm/timestamp.d.ts +48 -48
- package/dist/esm/timestamp.js +228 -228
- package/dist/esm/timestamp.test.d.ts +1 -1
- package/dist/esm/timestamp.test.js +91 -91
- package/dist/esm/undo.d.ts +3 -3
- package/dist/esm/undo.js +194 -194
- package/dist/esm/utils.d.ts +15 -15
- package/dist/esm/utils.js +113 -113
- package/dist/esm/utils.test.d.ts +1 -1
- package/dist/esm/utils.test.js +34 -34
- package/dist/tsconfig-cjs.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/oids.test.ts +62 -51
- package/src/oids.ts +7 -26
- package/src/operation.test.ts +32 -40
- package/src/operation.ts +4 -16
- package/src/patch.ts +5 -15
- package/src/schema/defaults.test.ts +1 -1
- package/dist/cjs/files.test.d.ts +0 -1
- package/dist/cjs/files.test.js +0 -81
- package/dist/cjs/files.test.js.map +0 -1
- package/dist/esm/files.test.d.ts +0 -1
- package/dist/esm/files.test.js +0 -79
- package/dist/esm/files.test.js.map +0 -1
package/dist/esm/timestamp.d.ts
CHANGED
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
export interface TimestampProvider {
|
|
2
|
-
now(version: number): string;
|
|
3
|
-
update(remoteTimestamp: string): void;
|
|
4
|
-
zero(version: number): string;
|
|
5
|
-
getWallClockTime(timestamp: string): number;
|
|
6
|
-
}
|
|
7
|
-
export declare function encodeVersion(version: number | string): string;
|
|
8
|
-
export declare function OLD_encodeVersion(version: number | string): string;
|
|
9
|
-
export declare class NaiveTimestampProvider implements TimestampProvider {
|
|
10
|
-
counter: number;
|
|
11
|
-
now: (version: number | string) => string;
|
|
12
|
-
update: () => void;
|
|
13
|
-
zero: (version: number | string) => string;
|
|
14
|
-
getWallClockTime: (timestamp: string) => number;
|
|
15
|
-
}
|
|
16
|
-
export declare class HybridLogicalClockTimestampProvider implements TimestampProvider {
|
|
17
|
-
private latest;
|
|
18
|
-
private zeroCounter;
|
|
19
|
-
now: (version: string | number) => string;
|
|
20
|
-
/**
|
|
21
|
-
* @deprecated - use now() instead and update to latest format
|
|
22
|
-
*/
|
|
23
|
-
OLD_now: (version: string | number) => string;
|
|
24
|
-
/** Get the current timer state. Does not increment counter. */
|
|
25
|
-
timerState: () => HLCTimestamp;
|
|
26
|
-
update: (remoteTimestamp: string) => void;
|
|
27
|
-
get: (version: string | number, raw: HLCTimestamp) => string;
|
|
28
|
-
zero: (version: string | number) => string;
|
|
29
|
-
getWallClockTime: (timestamp: string) => number;
|
|
30
|
-
}
|
|
31
|
-
interface HLCTimestamp {
|
|
32
|
-
time: number;
|
|
33
|
-
counter: number;
|
|
34
|
-
node: string;
|
|
35
|
-
}
|
|
36
|
-
export declare function serializeHlcTimestamp(ts: HLCTimestamp): string;
|
|
37
|
-
export declare function deserializeHlcTimestamp(clock: string): HLCTimestamp;
|
|
38
|
-
/**
|
|
39
|
-
* Below, converters for old-style timestamps are defined.
|
|
40
|
-
* These are for migrating older clients to the new format.
|
|
41
|
-
*/
|
|
42
|
-
/**
|
|
43
|
-
* Converts a timestamp from the old format to the new format.
|
|
44
|
-
*/
|
|
45
|
-
export declare function convertOldHlcTimestamp(ts: string): string;
|
|
46
|
-
export declare function OLD_serializeHlcTimestamp(ts: HLCTimestamp): string;
|
|
47
|
-
export declare function OLD_deserializeHlcTimestamp(clock: string): HLCTimestamp;
|
|
48
|
-
export {};
|
|
1
|
+
export interface TimestampProvider {
|
|
2
|
+
now(version: number): string;
|
|
3
|
+
update(remoteTimestamp: string): void;
|
|
4
|
+
zero(version: number): string;
|
|
5
|
+
getWallClockTime(timestamp: string): number;
|
|
6
|
+
}
|
|
7
|
+
export declare function encodeVersion(version: number | string): string;
|
|
8
|
+
export declare function OLD_encodeVersion(version: number | string): string;
|
|
9
|
+
export declare class NaiveTimestampProvider implements TimestampProvider {
|
|
10
|
+
counter: number;
|
|
11
|
+
now: (version: number | string) => string;
|
|
12
|
+
update: () => void;
|
|
13
|
+
zero: (version: number | string) => string;
|
|
14
|
+
getWallClockTime: (timestamp: string) => number;
|
|
15
|
+
}
|
|
16
|
+
export declare class HybridLogicalClockTimestampProvider implements TimestampProvider {
|
|
17
|
+
private latest;
|
|
18
|
+
private zeroCounter;
|
|
19
|
+
now: (version: string | number) => string;
|
|
20
|
+
/**
|
|
21
|
+
* @deprecated - use now() instead and update to latest format
|
|
22
|
+
*/
|
|
23
|
+
OLD_now: (version: string | number) => string;
|
|
24
|
+
/** Get the current timer state. Does not increment counter. */
|
|
25
|
+
timerState: () => HLCTimestamp;
|
|
26
|
+
update: (remoteTimestamp: string) => void;
|
|
27
|
+
get: (version: string | number, raw: HLCTimestamp) => string;
|
|
28
|
+
zero: (version: string | number) => string;
|
|
29
|
+
getWallClockTime: (timestamp: string) => number;
|
|
30
|
+
}
|
|
31
|
+
interface HLCTimestamp {
|
|
32
|
+
time: number;
|
|
33
|
+
counter: number;
|
|
34
|
+
node: string;
|
|
35
|
+
}
|
|
36
|
+
export declare function serializeHlcTimestamp(ts: HLCTimestamp): string;
|
|
37
|
+
export declare function deserializeHlcTimestamp(clock: string): HLCTimestamp;
|
|
38
|
+
/**
|
|
39
|
+
* Below, converters for old-style timestamps are defined.
|
|
40
|
+
* These are for migrating older clients to the new format.
|
|
41
|
+
*/
|
|
42
|
+
/**
|
|
43
|
+
* Converts a timestamp from the old format to the new format.
|
|
44
|
+
*/
|
|
45
|
+
export declare function convertOldHlcTimestamp(ts: string): string;
|
|
46
|
+
export declare function OLD_serializeHlcTimestamp(ts: HLCTimestamp): string;
|
|
47
|
+
export declare function OLD_deserializeHlcTimestamp(clock: string): HLCTimestamp;
|
|
48
|
+
export {};
|
package/dist/esm/timestamp.js
CHANGED
|
@@ -1,229 +1,229 @@
|
|
|
1
|
-
import cuid from 'cuid';
|
|
2
|
-
const VERSION_BLOCK_LENGTH = 4;
|
|
3
|
-
const ENCODING_NUMBER_RADIX = 36;
|
|
4
|
-
export function encodeVersion(version) {
|
|
5
|
-
return version
|
|
6
|
-
.toString(ENCODING_NUMBER_RADIX)
|
|
7
|
-
.padStart(VERSION_BLOCK_LENGTH, '0');
|
|
8
|
-
}
|
|
9
|
-
export function OLD_encodeVersion(version) {
|
|
10
|
-
return version.toString().padStart(6, '0');
|
|
11
|
-
}
|
|
12
|
-
export class NaiveTimestampProvider {
|
|
13
|
-
constructor() {
|
|
14
|
-
this.counter = 0;
|
|
15
|
-
this.now = (version) => {
|
|
16
|
-
return (encodeVersion(version) + Date.now().toString() + '-' + this.counter++);
|
|
17
|
-
};
|
|
18
|
-
this.update = () => {
|
|
19
|
-
this.counter = 0;
|
|
20
|
-
};
|
|
21
|
-
this.zero = (version) => {
|
|
22
|
-
return encodeVersion(version) + '0' + '-' + this.counter++;
|
|
23
|
-
};
|
|
24
|
-
this.getWallClockTime = (timestamp) => {
|
|
25
|
-
return parseInt(timestamp.slice(VERSION_BLOCK_LENGTH).split('-')[0], 10);
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
export class HybridLogicalClockTimestampProvider {
|
|
30
|
-
constructor() {
|
|
31
|
-
this.latest = {
|
|
32
|
-
time: Date.now(),
|
|
33
|
-
counter: 0,
|
|
34
|
-
node: generateNodeId(),
|
|
35
|
-
};
|
|
36
|
-
this.zeroCounter = 0;
|
|
37
|
-
this.now = (version) => {
|
|
38
|
-
this.latest = getHlcNow(this.latest);
|
|
39
|
-
return this.get(version, this.latest);
|
|
40
|
-
};
|
|
41
|
-
/**
|
|
42
|
-
* @deprecated - use now() instead and update to latest format
|
|
43
|
-
*/
|
|
44
|
-
this.OLD_now = (version) => {
|
|
45
|
-
this.latest = getHlcNow(this.latest);
|
|
46
|
-
return OLD_encodeVersion(version) + OLD_serializeHlcTimestamp(this.latest);
|
|
47
|
-
};
|
|
48
|
-
/** Get the current timer state. Does not increment counter. */
|
|
49
|
-
this.timerState = () => {
|
|
50
|
-
return this.latest;
|
|
51
|
-
};
|
|
52
|
-
this.update = (remoteTimestamp) => {
|
|
53
|
-
// strip version from remote timestamp
|
|
54
|
-
const hlcString = remoteTimestamp.slice(VERSION_BLOCK_LENGTH);
|
|
55
|
-
this.latest = updateFromRemote(this.latest, deserializeHlcTimestamp(hlcString));
|
|
56
|
-
};
|
|
57
|
-
this.get = (version, raw) => {
|
|
58
|
-
return encodeVersion(version) + serializeHlcTimestamp(raw);
|
|
59
|
-
};
|
|
60
|
-
this.zero = (version) => {
|
|
61
|
-
return (encodeVersion(version) +
|
|
62
|
-
serializeHlcTimestamp({
|
|
63
|
-
time: 0,
|
|
64
|
-
// to keep zero timestamps unique, we use a counter here as well
|
|
65
|
-
counter: this.zeroCounter++,
|
|
66
|
-
node: this.latest.node,
|
|
67
|
-
}));
|
|
68
|
-
};
|
|
69
|
-
this.getWallClockTime = (timestamp) => {
|
|
70
|
-
return deserializeHlcTimestamp(timestamp.slice(VERSION_BLOCK_LENGTH)).time;
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
class ClockDriftError extends Error {
|
|
75
|
-
constructor(...args) {
|
|
76
|
-
super();
|
|
77
|
-
this.type = 'ClockDriftError';
|
|
78
|
-
this.message = ['maximum clock drift exceeded'].concat(args).join(' ');
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
class OverflowError extends Error {
|
|
82
|
-
constructor() {
|
|
83
|
-
super();
|
|
84
|
-
this.type = 'OverflowError';
|
|
85
|
-
this.message = 'timestamp counter overflow';
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
const COUNTER_BLOCK_LENGTH = 4;
|
|
89
|
-
const NODE_BLOCK_LENGTH = 7;
|
|
90
|
-
const MAX_CLOCK_DRIFT = 60 * 1000;
|
|
91
|
-
// 9 base36 characters should last until the year 5000
|
|
92
|
-
// when encoding a unix MS timestamp
|
|
93
|
-
const TIME_BLOCK_LENGTH = 9;
|
|
94
|
-
function generateNodeId() {
|
|
95
|
-
return cuid
|
|
96
|
-
.slug()
|
|
97
|
-
.padStart(NODE_BLOCK_LENGTH, '0')
|
|
98
|
-
.slice(0, NODE_BLOCK_LENGTH);
|
|
99
|
-
}
|
|
100
|
-
export function serializeHlcTimestamp(ts) {
|
|
101
|
-
// string representation of the time
|
|
102
|
-
const dateString = new Date(ts.time)
|
|
103
|
-
.getTime()
|
|
104
|
-
.toString(ENCODING_NUMBER_RADIX)
|
|
105
|
-
.padStart(TIME_BLOCK_LENGTH, '0');
|
|
106
|
-
// counter, 336, padded to 4 characters
|
|
107
|
-
const counter = ts.counter
|
|
108
|
-
.toString(ENCODING_NUMBER_RADIX)
|
|
109
|
-
.padStart(COUNTER_BLOCK_LENGTH, '0');
|
|
110
|
-
// node id padded to 16 characters
|
|
111
|
-
const node = ts.node.padStart(NODE_BLOCK_LENGTH, '0');
|
|
112
|
-
return `${dateString}${counter}${node}`;
|
|
113
|
-
}
|
|
114
|
-
function getHlcNow(prev) {
|
|
115
|
-
const wallTime = Date.now();
|
|
116
|
-
// pick prev's time if it's later than our local device
|
|
117
|
-
const newWallTime = Math.max(prev.time, wallTime);
|
|
118
|
-
// reset counter if wall time changed
|
|
119
|
-
const newCounter = prev.time === newWallTime ? prev.counter + 1 : 0;
|
|
120
|
-
// check for drift
|
|
121
|
-
if (newWallTime - wallTime > MAX_CLOCK_DRIFT) {
|
|
122
|
-
throw new ClockDriftError(newWallTime, wallTime, MAX_CLOCK_DRIFT);
|
|
123
|
-
}
|
|
124
|
-
// check for counter overflow (max is 4 bytes)
|
|
125
|
-
if (newCounter > 65535) {
|
|
126
|
-
throw new OverflowError();
|
|
127
|
-
}
|
|
128
|
-
return {
|
|
129
|
-
time: newWallTime,
|
|
130
|
-
counter: newCounter,
|
|
131
|
-
node: prev.node,
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
function updateFromRemote(local, remote) {
|
|
135
|
-
const wallTime = Date.now();
|
|
136
|
-
// pick remote's time if it's later than our local device
|
|
137
|
-
const newTime = Math.max(wallTime, Math.max(local.time, remote.time));
|
|
138
|
-
const maxCounter = Math.max(local.counter, remote.counter);
|
|
139
|
-
let newCounter;
|
|
140
|
-
// if all clocks are the same, increment the counter
|
|
141
|
-
if (local.time === newTime && remote.time === newTime) {
|
|
142
|
-
newCounter = maxCounter + 1;
|
|
143
|
-
}
|
|
144
|
-
// if local time is the same as new time, increment our local counter
|
|
145
|
-
else if (local.time === newTime) {
|
|
146
|
-
newCounter = local.counter + 1;
|
|
147
|
-
}
|
|
148
|
-
// if remote time is the same as new time, increment remote counter
|
|
149
|
-
else if (remote.time === newTime) {
|
|
150
|
-
newCounter = remote.counter + 1;
|
|
151
|
-
}
|
|
152
|
-
// otherwise, reset the counter
|
|
153
|
-
else {
|
|
154
|
-
newCounter = 0;
|
|
155
|
-
}
|
|
156
|
-
// check for drift
|
|
157
|
-
if (newTime - wallTime > MAX_CLOCK_DRIFT) {
|
|
158
|
-
throw new ClockDriftError(newTime, wallTime, MAX_CLOCK_DRIFT);
|
|
159
|
-
}
|
|
160
|
-
if (newCounter > 65535) {
|
|
161
|
-
throw new OverflowError();
|
|
162
|
-
}
|
|
163
|
-
return {
|
|
164
|
-
time: newTime,
|
|
165
|
-
counter: newCounter,
|
|
166
|
-
node: local.node,
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
export function deserializeHlcTimestamp(clock) {
|
|
170
|
-
const dateString = clock.slice(0, TIME_BLOCK_LENGTH);
|
|
171
|
-
const counter = clock.slice(TIME_BLOCK_LENGTH, TIME_BLOCK_LENGTH + COUNTER_BLOCK_LENGTH);
|
|
172
|
-
const node = clock.slice(TIME_BLOCK_LENGTH + COUNTER_BLOCK_LENGTH);
|
|
173
|
-
const time = parseInt(dateString, ENCODING_NUMBER_RADIX);
|
|
174
|
-
const counterNum = parseInt(counter, ENCODING_NUMBER_RADIX);
|
|
175
|
-
if (isNaN(time) || isNaN(counterNum)) {
|
|
176
|
-
throw new Error('invalid clock format');
|
|
177
|
-
}
|
|
178
|
-
return {
|
|
179
|
-
time,
|
|
180
|
-
counter: counterNum,
|
|
181
|
-
node,
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
function removeLeadingZeroes(str) {
|
|
185
|
-
return str.replace(/^0+/, '');
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Below, converters for old-style timestamps are defined.
|
|
189
|
-
* These are for migrating older clients to the new format.
|
|
190
|
-
*/
|
|
191
|
-
/**
|
|
192
|
-
* Converts a timestamp from the old format to the new format.
|
|
193
|
-
*/
|
|
194
|
-
export function convertOldHlcTimestamp(ts) {
|
|
195
|
-
const versionBlock = ts.slice(0, 6);
|
|
196
|
-
// no jokes please
|
|
197
|
-
const clockBlock = ts.slice(6);
|
|
198
|
-
const deserialized = OLD_deserializeHlcTimestamp(clockBlock);
|
|
199
|
-
// version was base 10 and now is base 36
|
|
200
|
-
const versionNumber = parseInt(versionBlock, 10);
|
|
201
|
-
const encodedVersion = encodeVersion(versionNumber);
|
|
202
|
-
return encodedVersion + serializeHlcTimestamp(deserialized);
|
|
203
|
-
}
|
|
204
|
-
export function OLD_serializeHlcTimestamp(ts) {
|
|
205
|
-
// ISO string representation of the time
|
|
206
|
-
const dateString = new Date(ts.time).toISOString();
|
|
207
|
-
// counter, base16, padded to 4 characters
|
|
208
|
-
const counter = ts.counter.toString(16).toUpperCase().padStart(4, '0');
|
|
209
|
-
// node id padded to 16 characters
|
|
210
|
-
const node = ts.node.padStart(16, '0');
|
|
211
|
-
return `${dateString}-${counter}-${node}`;
|
|
212
|
-
}
|
|
213
|
-
export function OLD_deserializeHlcTimestamp(clock) {
|
|
214
|
-
// ISO string representation of the time
|
|
215
|
-
const dateString = clock.slice(0, 24);
|
|
216
|
-
const counter = clock.slice(25, 25 + 4);
|
|
217
|
-
const node = clock.slice(25 + 4 + 1);
|
|
218
|
-
const time = new Date(dateString).getTime();
|
|
219
|
-
const counterNum = parseInt(counter, 16);
|
|
220
|
-
if (isNaN(time) || isNaN(counterNum)) {
|
|
221
|
-
throw new Error('invalid clock format');
|
|
222
|
-
}
|
|
223
|
-
return {
|
|
224
|
-
time,
|
|
225
|
-
counter: counterNum,
|
|
226
|
-
node: node.slice(node.length - 7),
|
|
227
|
-
};
|
|
228
|
-
}
|
|
1
|
+
import cuid from 'cuid';
|
|
2
|
+
const VERSION_BLOCK_LENGTH = 4;
|
|
3
|
+
const ENCODING_NUMBER_RADIX = 36;
|
|
4
|
+
export function encodeVersion(version) {
|
|
5
|
+
return version
|
|
6
|
+
.toString(ENCODING_NUMBER_RADIX)
|
|
7
|
+
.padStart(VERSION_BLOCK_LENGTH, '0');
|
|
8
|
+
}
|
|
9
|
+
export function OLD_encodeVersion(version) {
|
|
10
|
+
return version.toString().padStart(6, '0');
|
|
11
|
+
}
|
|
12
|
+
export class NaiveTimestampProvider {
|
|
13
|
+
constructor() {
|
|
14
|
+
this.counter = 0;
|
|
15
|
+
this.now = (version) => {
|
|
16
|
+
return (encodeVersion(version) + Date.now().toString() + '-' + this.counter++);
|
|
17
|
+
};
|
|
18
|
+
this.update = () => {
|
|
19
|
+
this.counter = 0;
|
|
20
|
+
};
|
|
21
|
+
this.zero = (version) => {
|
|
22
|
+
return encodeVersion(version) + '0' + '-' + this.counter++;
|
|
23
|
+
};
|
|
24
|
+
this.getWallClockTime = (timestamp) => {
|
|
25
|
+
return parseInt(timestamp.slice(VERSION_BLOCK_LENGTH).split('-')[0], 10);
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export class HybridLogicalClockTimestampProvider {
|
|
30
|
+
constructor() {
|
|
31
|
+
this.latest = {
|
|
32
|
+
time: Date.now(),
|
|
33
|
+
counter: 0,
|
|
34
|
+
node: generateNodeId(),
|
|
35
|
+
};
|
|
36
|
+
this.zeroCounter = 0;
|
|
37
|
+
this.now = (version) => {
|
|
38
|
+
this.latest = getHlcNow(this.latest);
|
|
39
|
+
return this.get(version, this.latest);
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* @deprecated - use now() instead and update to latest format
|
|
43
|
+
*/
|
|
44
|
+
this.OLD_now = (version) => {
|
|
45
|
+
this.latest = getHlcNow(this.latest);
|
|
46
|
+
return OLD_encodeVersion(version) + OLD_serializeHlcTimestamp(this.latest);
|
|
47
|
+
};
|
|
48
|
+
/** Get the current timer state. Does not increment counter. */
|
|
49
|
+
this.timerState = () => {
|
|
50
|
+
return this.latest;
|
|
51
|
+
};
|
|
52
|
+
this.update = (remoteTimestamp) => {
|
|
53
|
+
// strip version from remote timestamp
|
|
54
|
+
const hlcString = remoteTimestamp.slice(VERSION_BLOCK_LENGTH);
|
|
55
|
+
this.latest = updateFromRemote(this.latest, deserializeHlcTimestamp(hlcString));
|
|
56
|
+
};
|
|
57
|
+
this.get = (version, raw) => {
|
|
58
|
+
return encodeVersion(version) + serializeHlcTimestamp(raw);
|
|
59
|
+
};
|
|
60
|
+
this.zero = (version) => {
|
|
61
|
+
return (encodeVersion(version) +
|
|
62
|
+
serializeHlcTimestamp({
|
|
63
|
+
time: 0,
|
|
64
|
+
// to keep zero timestamps unique, we use a counter here as well
|
|
65
|
+
counter: this.zeroCounter++,
|
|
66
|
+
node: this.latest.node,
|
|
67
|
+
}));
|
|
68
|
+
};
|
|
69
|
+
this.getWallClockTime = (timestamp) => {
|
|
70
|
+
return deserializeHlcTimestamp(timestamp.slice(VERSION_BLOCK_LENGTH)).time;
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
class ClockDriftError extends Error {
|
|
75
|
+
constructor(...args) {
|
|
76
|
+
super();
|
|
77
|
+
this.type = 'ClockDriftError';
|
|
78
|
+
this.message = ['maximum clock drift exceeded'].concat(args).join(' ');
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
class OverflowError extends Error {
|
|
82
|
+
constructor() {
|
|
83
|
+
super();
|
|
84
|
+
this.type = 'OverflowError';
|
|
85
|
+
this.message = 'timestamp counter overflow';
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const COUNTER_BLOCK_LENGTH = 4;
|
|
89
|
+
const NODE_BLOCK_LENGTH = 7;
|
|
90
|
+
const MAX_CLOCK_DRIFT = 60 * 1000;
|
|
91
|
+
// 9 base36 characters should last until the year 5000
|
|
92
|
+
// when encoding a unix MS timestamp
|
|
93
|
+
const TIME_BLOCK_LENGTH = 9;
|
|
94
|
+
function generateNodeId() {
|
|
95
|
+
return cuid
|
|
96
|
+
.slug()
|
|
97
|
+
.padStart(NODE_BLOCK_LENGTH, '0')
|
|
98
|
+
.slice(0, NODE_BLOCK_LENGTH);
|
|
99
|
+
}
|
|
100
|
+
export function serializeHlcTimestamp(ts) {
|
|
101
|
+
// string representation of the time
|
|
102
|
+
const dateString = new Date(ts.time)
|
|
103
|
+
.getTime()
|
|
104
|
+
.toString(ENCODING_NUMBER_RADIX)
|
|
105
|
+
.padStart(TIME_BLOCK_LENGTH, '0');
|
|
106
|
+
// counter, 336, padded to 4 characters
|
|
107
|
+
const counter = ts.counter
|
|
108
|
+
.toString(ENCODING_NUMBER_RADIX)
|
|
109
|
+
.padStart(COUNTER_BLOCK_LENGTH, '0');
|
|
110
|
+
// node id padded to 16 characters
|
|
111
|
+
const node = ts.node.padStart(NODE_BLOCK_LENGTH, '0');
|
|
112
|
+
return `${dateString}${counter}${node}`;
|
|
113
|
+
}
|
|
114
|
+
function getHlcNow(prev) {
|
|
115
|
+
const wallTime = Date.now();
|
|
116
|
+
// pick prev's time if it's later than our local device
|
|
117
|
+
const newWallTime = Math.max(prev.time, wallTime);
|
|
118
|
+
// reset counter if wall time changed
|
|
119
|
+
const newCounter = prev.time === newWallTime ? prev.counter + 1 : 0;
|
|
120
|
+
// check for drift
|
|
121
|
+
if (newWallTime - wallTime > MAX_CLOCK_DRIFT) {
|
|
122
|
+
throw new ClockDriftError(newWallTime, wallTime, MAX_CLOCK_DRIFT);
|
|
123
|
+
}
|
|
124
|
+
// check for counter overflow (max is 4 bytes)
|
|
125
|
+
if (newCounter > 65535) {
|
|
126
|
+
throw new OverflowError();
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
time: newWallTime,
|
|
130
|
+
counter: newCounter,
|
|
131
|
+
node: prev.node,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function updateFromRemote(local, remote) {
|
|
135
|
+
const wallTime = Date.now();
|
|
136
|
+
// pick remote's time if it's later than our local device
|
|
137
|
+
const newTime = Math.max(wallTime, Math.max(local.time, remote.time));
|
|
138
|
+
const maxCounter = Math.max(local.counter, remote.counter);
|
|
139
|
+
let newCounter;
|
|
140
|
+
// if all clocks are the same, increment the counter
|
|
141
|
+
if (local.time === newTime && remote.time === newTime) {
|
|
142
|
+
newCounter = maxCounter + 1;
|
|
143
|
+
}
|
|
144
|
+
// if local time is the same as new time, increment our local counter
|
|
145
|
+
else if (local.time === newTime) {
|
|
146
|
+
newCounter = local.counter + 1;
|
|
147
|
+
}
|
|
148
|
+
// if remote time is the same as new time, increment remote counter
|
|
149
|
+
else if (remote.time === newTime) {
|
|
150
|
+
newCounter = remote.counter + 1;
|
|
151
|
+
}
|
|
152
|
+
// otherwise, reset the counter
|
|
153
|
+
else {
|
|
154
|
+
newCounter = 0;
|
|
155
|
+
}
|
|
156
|
+
// check for drift
|
|
157
|
+
if (newTime - wallTime > MAX_CLOCK_DRIFT) {
|
|
158
|
+
throw new ClockDriftError(newTime, wallTime, MAX_CLOCK_DRIFT);
|
|
159
|
+
}
|
|
160
|
+
if (newCounter > 65535) {
|
|
161
|
+
throw new OverflowError();
|
|
162
|
+
}
|
|
163
|
+
return {
|
|
164
|
+
time: newTime,
|
|
165
|
+
counter: newCounter,
|
|
166
|
+
node: local.node,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
export function deserializeHlcTimestamp(clock) {
|
|
170
|
+
const dateString = clock.slice(0, TIME_BLOCK_LENGTH);
|
|
171
|
+
const counter = clock.slice(TIME_BLOCK_LENGTH, TIME_BLOCK_LENGTH + COUNTER_BLOCK_LENGTH);
|
|
172
|
+
const node = clock.slice(TIME_BLOCK_LENGTH + COUNTER_BLOCK_LENGTH);
|
|
173
|
+
const time = parseInt(dateString, ENCODING_NUMBER_RADIX);
|
|
174
|
+
const counterNum = parseInt(counter, ENCODING_NUMBER_RADIX);
|
|
175
|
+
if (isNaN(time) || isNaN(counterNum)) {
|
|
176
|
+
throw new Error('invalid clock format');
|
|
177
|
+
}
|
|
178
|
+
return {
|
|
179
|
+
time,
|
|
180
|
+
counter: counterNum,
|
|
181
|
+
node,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
function removeLeadingZeroes(str) {
|
|
185
|
+
return str.replace(/^0+/, '');
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Below, converters for old-style timestamps are defined.
|
|
189
|
+
* These are for migrating older clients to the new format.
|
|
190
|
+
*/
|
|
191
|
+
/**
|
|
192
|
+
* Converts a timestamp from the old format to the new format.
|
|
193
|
+
*/
|
|
194
|
+
export function convertOldHlcTimestamp(ts) {
|
|
195
|
+
const versionBlock = ts.slice(0, 6);
|
|
196
|
+
// no jokes please
|
|
197
|
+
const clockBlock = ts.slice(6);
|
|
198
|
+
const deserialized = OLD_deserializeHlcTimestamp(clockBlock);
|
|
199
|
+
// version was base 10 and now is base 36
|
|
200
|
+
const versionNumber = parseInt(versionBlock, 10);
|
|
201
|
+
const encodedVersion = encodeVersion(versionNumber);
|
|
202
|
+
return encodedVersion + serializeHlcTimestamp(deserialized);
|
|
203
|
+
}
|
|
204
|
+
export function OLD_serializeHlcTimestamp(ts) {
|
|
205
|
+
// ISO string representation of the time
|
|
206
|
+
const dateString = new Date(ts.time).toISOString();
|
|
207
|
+
// counter, base16, padded to 4 characters
|
|
208
|
+
const counter = ts.counter.toString(16).toUpperCase().padStart(4, '0');
|
|
209
|
+
// node id padded to 16 characters
|
|
210
|
+
const node = ts.node.padStart(16, '0');
|
|
211
|
+
return `${dateString}-${counter}-${node}`;
|
|
212
|
+
}
|
|
213
|
+
export function OLD_deserializeHlcTimestamp(clock) {
|
|
214
|
+
// ISO string representation of the time
|
|
215
|
+
const dateString = clock.slice(0, 24);
|
|
216
|
+
const counter = clock.slice(25, 25 + 4);
|
|
217
|
+
const node = clock.slice(25 + 4 + 1);
|
|
218
|
+
const time = new Date(dateString).getTime();
|
|
219
|
+
const counterNum = parseInt(counter, 16);
|
|
220
|
+
if (isNaN(time) || isNaN(counterNum)) {
|
|
221
|
+
throw new Error('invalid clock format');
|
|
222
|
+
}
|
|
223
|
+
return {
|
|
224
|
+
time,
|
|
225
|
+
counter: counterNum,
|
|
226
|
+
node: node.slice(node.length - 7),
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
229
|
//# sourceMappingURL=timestamp.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export {};
|