@verdant-web/common 1.15.2 → 1.15.3-next.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.
Files changed (163) hide show
  1. package/dist/cjs/EventSubscriber.d.ts +17 -17
  2. package/dist/cjs/EventSubscriber.js +66 -66
  3. package/dist/cjs/baseline.d.ts +5 -5
  4. package/dist/cjs/baseline.js +2 -2
  5. package/dist/cjs/batching.d.ts +42 -42
  6. package/dist/cjs/batching.js +91 -91
  7. package/dist/cjs/files.d.ts +19 -19
  8. package/dist/cjs/files.js +18 -18
  9. package/dist/cjs/files.test.d.ts +1 -0
  10. package/dist/cjs/files.test.js +81 -0
  11. package/dist/cjs/files.test.js.map +1 -0
  12. package/dist/cjs/index.d.ts +19 -18
  13. package/dist/cjs/index.js +41 -36
  14. package/dist/cjs/index.js.map +1 -1
  15. package/dist/cjs/indexes.d.ts +17 -17
  16. package/dist/cjs/indexes.js +142 -142
  17. package/dist/cjs/indexes.test.d.ts +1 -1
  18. package/dist/cjs/indexes.test.js +127 -127
  19. package/dist/cjs/migration.d.ts +172 -172
  20. package/dist/cjs/migration.js +301 -301
  21. package/dist/cjs/oids.d.ts +100 -96
  22. package/dist/cjs/oids.js +457 -439
  23. package/dist/cjs/oids.js.map +1 -1
  24. package/dist/cjs/oids.test.d.ts +1 -1
  25. package/dist/cjs/oids.test.js +348 -272
  26. package/dist/cjs/oids.test.js.map +1 -1
  27. package/dist/cjs/operation.d.ts +139 -139
  28. package/dist/cjs/operation.js +545 -545
  29. package/dist/cjs/operation.test.d.ts +1 -1
  30. package/dist/cjs/operation.test.js +529 -529
  31. package/dist/cjs/patch.d.ts +26 -26
  32. package/dist/cjs/patch.js +226 -226
  33. package/dist/cjs/presence.d.ts +27 -27
  34. package/dist/cjs/presence.js +2 -2
  35. package/dist/cjs/protocol.d.ts +145 -145
  36. package/dist/cjs/protocol.js +3 -3
  37. package/dist/cjs/refs.d.ts +7 -5
  38. package/dist/cjs/refs.js +29 -21
  39. package/dist/cjs/refs.js.map +1 -1
  40. package/dist/cjs/replica.d.ts +28 -28
  41. package/dist/cjs/replica.js +28 -28
  42. package/dist/cjs/schema/defaults.d.ts +4 -0
  43. package/dist/cjs/schema/defaults.js +72 -0
  44. package/dist/cjs/schema/defaults.js.map +1 -0
  45. package/dist/cjs/schema/defaults.test.d.ts +1 -1
  46. package/dist/cjs/schema/defaults.test.js +20 -20
  47. package/dist/cjs/schema/fields.d.ts +9 -9
  48. package/dist/cjs/schema/fields.js +144 -144
  49. package/dist/cjs/schema/index.d.ts +8 -8
  50. package/dist/cjs/schema/index.js +51 -51
  51. package/dist/cjs/schema/indexFilters.d.ts +6 -6
  52. package/dist/cjs/schema/indexFilters.js +30 -30
  53. package/dist/cjs/schema/types/collection.d.ts +23 -23
  54. package/dist/cjs/schema/types/collection.js +2 -2
  55. package/dist/cjs/schema/types/compounds.d.ts +11 -11
  56. package/dist/cjs/schema/types/compounds.js +2 -2
  57. package/dist/cjs/schema/types/fields.d.ts +75 -75
  58. package/dist/cjs/schema/types/fields.js +2 -2
  59. package/dist/cjs/schema/types/filters.d.ts +29 -29
  60. package/dist/cjs/schema/types/filters.js +2 -2
  61. package/dist/cjs/schema/types/shapes.d.ts +21 -21
  62. package/dist/cjs/schema/types/shapes.js +2 -2
  63. package/dist/cjs/schema/types/synthetics.d.ts +37 -37
  64. package/dist/cjs/schema/types/synthetics.js +2 -2
  65. package/dist/cjs/schema/types.d.ts +18 -18
  66. package/dist/cjs/schema/types.js +22 -22
  67. package/dist/cjs/schema/validation.d.ts +3 -3
  68. package/dist/cjs/schema/validation.js +86 -86
  69. package/dist/cjs/timestamp.d.ts +50 -50
  70. package/dist/cjs/timestamp.js +251 -251
  71. package/dist/cjs/timestamp.test.d.ts +1 -1
  72. package/dist/cjs/timestamp.test.js +104 -104
  73. package/dist/cjs/undo.d.ts +3 -3
  74. package/dist/cjs/undo.js +198 -198
  75. package/dist/cjs/utils.d.ts +20 -20
  76. package/dist/cjs/utils.js +145 -145
  77. package/dist/cjs/utils.test.d.ts +1 -1
  78. package/dist/cjs/utils.test.js +36 -36
  79. package/dist/esm/EventSubscriber.d.ts +17 -17
  80. package/dist/esm/EventSubscriber.js +62 -62
  81. package/dist/esm/baseline.d.ts +5 -5
  82. package/dist/esm/baseline.js +1 -1
  83. package/dist/esm/batching.d.ts +42 -42
  84. package/dist/esm/batching.js +86 -86
  85. package/dist/esm/files.d.ts +19 -19
  86. package/dist/esm/files.js +12 -12
  87. package/dist/esm/files.test.d.ts +1 -0
  88. package/dist/esm/files.test.js +79 -0
  89. package/dist/esm/files.test.js.map +1 -0
  90. package/dist/esm/index.d.ts +19 -18
  91. package/dist/esm/index.js +16 -15
  92. package/dist/esm/index.js.map +1 -1
  93. package/dist/esm/indexes.d.ts +17 -17
  94. package/dist/esm/indexes.js +130 -130
  95. package/dist/esm/indexes.test.d.ts +1 -1
  96. package/dist/esm/indexes.test.js +125 -125
  97. package/dist/esm/migration.d.ts +172 -172
  98. package/dist/esm/migration.js +294 -294
  99. package/dist/esm/oids.d.ts +100 -96
  100. package/dist/esm/oids.js +422 -407
  101. package/dist/esm/oids.js.map +1 -1
  102. package/dist/esm/oids.test.d.ts +1 -1
  103. package/dist/esm/oids.test.js +346 -270
  104. package/dist/esm/oids.test.js.map +1 -1
  105. package/dist/esm/operation.d.ts +139 -139
  106. package/dist/esm/operation.js +530 -530
  107. package/dist/esm/operation.test.d.ts +1 -1
  108. package/dist/esm/operation.test.js +527 -527
  109. package/dist/esm/patch.d.ts +26 -26
  110. package/dist/esm/patch.js +222 -222
  111. package/dist/esm/presence.d.ts +27 -27
  112. package/dist/esm/presence.js +1 -1
  113. package/dist/esm/protocol.d.ts +145 -145
  114. package/dist/esm/protocol.js +2 -2
  115. package/dist/esm/refs.d.ts +7 -5
  116. package/dist/esm/refs.js +22 -16
  117. package/dist/esm/refs.js.map +1 -1
  118. package/dist/esm/replica.d.ts +28 -28
  119. package/dist/esm/replica.js +25 -25
  120. package/dist/esm/schema/defaults.d.ts +4 -0
  121. package/dist/esm/schema/defaults.js +66 -0
  122. package/dist/esm/schema/defaults.js.map +1 -0
  123. package/dist/esm/schema/defaults.test.d.ts +1 -1
  124. package/dist/esm/schema/defaults.test.js +18 -18
  125. package/dist/esm/schema/fields.d.ts +9 -9
  126. package/dist/esm/schema/fields.js +133 -133
  127. package/dist/esm/schema/index.d.ts +8 -8
  128. package/dist/esm/schema/index.js +32 -32
  129. package/dist/esm/schema/indexFilters.d.ts +6 -6
  130. package/dist/esm/schema/indexFilters.js +22 -22
  131. package/dist/esm/schema/types/collection.d.ts +23 -23
  132. package/dist/esm/schema/types/collection.js +1 -1
  133. package/dist/esm/schema/types/compounds.d.ts +11 -11
  134. package/dist/esm/schema/types/compounds.js +1 -1
  135. package/dist/esm/schema/types/fields.d.ts +75 -75
  136. package/dist/esm/schema/types/fields.js +1 -1
  137. package/dist/esm/schema/types/filters.d.ts +29 -29
  138. package/dist/esm/schema/types/filters.js +1 -1
  139. package/dist/esm/schema/types/shapes.d.ts +21 -21
  140. package/dist/esm/schema/types/shapes.js +1 -1
  141. package/dist/esm/schema/types/synthetics.d.ts +37 -37
  142. package/dist/esm/schema/types/synthetics.js +1 -1
  143. package/dist/esm/schema/types.d.ts +18 -18
  144. package/dist/esm/schema/types.js +6 -6
  145. package/dist/esm/schema/validation.d.ts +3 -3
  146. package/dist/esm/schema/validation.js +81 -81
  147. package/dist/esm/timestamp.d.ts +50 -50
  148. package/dist/esm/timestamp.js +234 -234
  149. package/dist/esm/timestamp.test.d.ts +1 -1
  150. package/dist/esm/timestamp.test.js +102 -102
  151. package/dist/esm/undo.d.ts +3 -3
  152. package/dist/esm/undo.js +194 -194
  153. package/dist/esm/utils.d.ts +20 -20
  154. package/dist/esm/utils.js +127 -127
  155. package/dist/esm/utils.test.d.ts +1 -1
  156. package/dist/esm/utils.test.js +34 -34
  157. package/dist/tsconfig-cjs.tsbuildinfo +1 -1
  158. package/dist/tsconfig.tsbuildinfo +1 -1
  159. package/package.json +1 -1
  160. package/src/index.ts +1 -0
  161. package/src/oids.test.ts +82 -0
  162. package/src/oids.ts +17 -0
  163. package/src/refs.ts +8 -0
@@ -1,235 +1,235 @@
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
- export function getTimestampSchemaVersion(timestamp) {
230
- return parseInt(timestamp.slice(0, VERSION_BLOCK_LENGTH), 36);
231
- }
232
- export function compareTimestampSchemaVersions(a, b) {
233
- return getTimestampSchemaVersion(a) - getTimestampSchemaVersion(b);
234
- }
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
+ export function getTimestampSchemaVersion(timestamp) {
230
+ return parseInt(timestamp.slice(0, VERSION_BLOCK_LENGTH), 36);
231
+ }
232
+ export function compareTimestampSchemaVersions(a, b) {
233
+ return getTimestampSchemaVersion(a) - getTimestampSchemaVersion(b);
234
+ }
235
235
  //# sourceMappingURL=timestamp.js.map
@@ -1 +1 @@
1
- export {};
1
+ export {};