@procedures/mongo 0.52.3 → 0.53.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +53 -0
- package/README.md +1 -0
- package/dist/.tsbuildinfo +1 -1
- package/dist/storage/database-changes.d.ts +6 -1
- package/dist/storage/database-changes.d.ts.map +1 -1
- package/dist/storage/database-changes.js +15 -3
- package/dist/storage/database-changes.js.map +1 -1
- package/dist/storage/document-storage.d.ts.map +1 -1
- package/dist/storage/document-storage.js +5 -6
- package/dist/storage/document-storage.js.map +1 -1
- package/dist/storage/materializer.d.ts +6 -0
- package/dist/storage/materializer.d.ts.map +1 -1
- package/dist/storage/materializer.js +12 -5
- package/dist/storage/materializer.js.map +1 -1
- package/dist/storage/storage-hints.d.ts +9 -71
- package/dist/storage/storage-hints.d.ts.map +1 -1
- package/dist/storage/storage-hints.js +22 -9
- package/dist/storage/storage-hints.js.map +1 -1
- package/dist/storage/token-age.d.ts +30 -0
- package/dist/storage/token-age.d.ts.map +1 -0
- package/dist/storage/token-age.js +55 -0
- package/dist/storage/token-age.js.map +1 -0
- package/package.json +10 -8
- package/src/storage/database-changes.ts +22 -3
- package/src/storage/document-storage.ts +5 -7
- package/src/storage/materializer.ts +13 -5
- package/src/storage/storage-hints.ts +27 -9
- package/src/storage/token-age.ts +63 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The age of the resume token of a change watcher (roadmap CLI-110). A token moves when its cluster time is later
|
|
3
|
+
* than the cluster time of the token before it. The same token again, or a new token at the same cluster time,
|
|
4
|
+
* does not move it. A user reads a stalled feed from the age, without a sampler of its own.
|
|
5
|
+
*
|
|
6
|
+
* The package does not export this module. `watchDatabaseChanges()` and `watchStorageChanges()` give the age as
|
|
7
|
+
* `tokenAgeMs` on their handles.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { Clock } from "@procedures/client";
|
|
11
|
+
|
|
12
|
+
/** The type byte of a timestamp at the start of the key of a resume token of the server. */
|
|
13
|
+
const TIMESTAMP_TYPE = "82";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* This function gives the cluster time of a resume token as 16 hex digits, or undefined for a token of another form.
|
|
17
|
+
* The key of a token of the server starts with the type byte `82` and the 8 bytes of the timestamp, high byte first.
|
|
18
|
+
* So the order of two of these texts is the order of their cluster times.
|
|
19
|
+
*/
|
|
20
|
+
export function clusterTimeOfToken(token: unknown): string | undefined {
|
|
21
|
+
const data = typeof token === "object" && token !== null ? (token as { _data?: unknown })._data : undefined;
|
|
22
|
+
if (typeof data !== "string" || !data.startsWith(TIMESTAMP_TYPE) || !/^[0-9a-f]{16}$/i.test(data.slice(2, 18))) return undefined;
|
|
23
|
+
return data.slice(2, 18).toLowerCase();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** This function tells if `next` is later than `before`. A token without a cluster time moves when it is a different token. */
|
|
27
|
+
function moves(next: unknown, before: unknown): boolean {
|
|
28
|
+
if (next === undefined || next === null) return false;
|
|
29
|
+
if (before === undefined || before === null) return true;
|
|
30
|
+
const nextTime = clusterTimeOfToken(next);
|
|
31
|
+
const beforeTime = clusterTimeOfToken(before);
|
|
32
|
+
if (nextTime !== undefined && beforeTime !== undefined) return nextTime > beforeTime;
|
|
33
|
+
return JSON.stringify(next) !== JSON.stringify(before);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The age of the token of one watcher. */
|
|
37
|
+
export interface TokenAge {
|
|
38
|
+
/** This method notes a token of the watcher. It moves the age to 0 when the token is later than the last token that moved it. */
|
|
39
|
+
note(token: unknown): void;
|
|
40
|
+
/** This method gives the milliseconds of the clock since the token last moved, or since the start before the first move. */
|
|
41
|
+
ageMs(): number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* This function starts the age of the token of one watcher at the time of the clock.
|
|
46
|
+
*
|
|
47
|
+
* @param clock - The clock of the watcher
|
|
48
|
+
* @param start - The start token of the watcher, or undefined. A later token moves the age.
|
|
49
|
+
*/
|
|
50
|
+
export function tokenAge(clock: Clock, start?: unknown): TokenAge {
|
|
51
|
+
let last: unknown = start;
|
|
52
|
+
let movedAt = clock.now();
|
|
53
|
+
return {
|
|
54
|
+
note(token: unknown): void {
|
|
55
|
+
if (!moves(token, last)) return;
|
|
56
|
+
last = token;
|
|
57
|
+
movedAt = clock.now();
|
|
58
|
+
},
|
|
59
|
+
ageMs(): number {
|
|
60
|
+
return clock.now() - movedAt;
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|