@zukhruf/checkpoint 0.0.0-stage → 0.8.166

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.
@@ -0,0 +1,110 @@
1
+ /**
2
+ * The batches that a reader handles out of order, in the order that it read
3
+ * them, after Go's `Jeffail/checkpoint` (ADR 0010). The highest position is
4
+ * the last position below which each batch is done.
5
+ */
6
+ class Batches {
7
+ #open = [];
8
+ #done = new Set();
9
+ #released = new WeakSet();
10
+ #highest = { kind: 'none' };
11
+ #pending = 0;
12
+ get pending() {
13
+ return this.#pending;
14
+ }
15
+ highest() {
16
+ return this.#highest.kind === 'at' ? this.#highest.position : undefined;
17
+ }
18
+ track(position, size) {
19
+ const batch = { position, size };
20
+ this.#open.push(batch);
21
+ this.#pending += size;
22
+ return () => {
23
+ if (!this.#released.has(batch)) {
24
+ this.#released.add(batch);
25
+ this.#done.add(batch);
26
+ this.#pending -= batch.size;
27
+ this.#advance();
28
+ }
29
+ return this.highest();
30
+ };
31
+ }
32
+ #advance() {
33
+ for (;;) {
34
+ const first = this.#open[0];
35
+ if (first === undefined || !this.#done.has(first))
36
+ return;
37
+ this.#open.shift();
38
+ this.#done.delete(first);
39
+ this.#highest = { kind: 'at', position: first.position };
40
+ }
41
+ }
42
+ }
43
+ /** A tracker that never waits (`Jeffail/checkpoint`'s `Uncapped`). */
44
+ export class UncappedTracker {
45
+ #batches = new Batches();
46
+ /** The size of the batches that are not done. */
47
+ get pending() {
48
+ return this.#batches.pending;
49
+ }
50
+ /** The last position below which each batch is done, if one is. */
51
+ highest() {
52
+ return this.#batches.highest();
53
+ }
54
+ /**
55
+ * Records a batch in the order that the reader read it. The function marks
56
+ * it done, and gives the highest position.
57
+ */
58
+ track(position, size) {
59
+ return this.#batches.track(position, size);
60
+ }
61
+ }
62
+ /**
63
+ * A tracker that waits while more than its capacity is not done
64
+ * (`Jeffail/checkpoint`'s `Capped`). A batch larger than the capacity waits
65
+ * until no batch is open.
66
+ */
67
+ export class CappedTracker {
68
+ #batches = new Batches();
69
+ #capacity;
70
+ #waiting = [];
71
+ constructor(capacity) {
72
+ this.#capacity = capacity;
73
+ }
74
+ get pending() {
75
+ return this.#batches.pending;
76
+ }
77
+ highest() {
78
+ return this.#batches.highest();
79
+ }
80
+ async track(position, size, options) {
81
+ while (!this.#fits(size))
82
+ await this.#wait(options?.signal);
83
+ options?.signal?.throwIfAborted();
84
+ const done = this.#batches.track(position, size);
85
+ return () => {
86
+ const highest = done();
87
+ for (const wake of this.#waiting.splice(0))
88
+ wake();
89
+ return highest;
90
+ };
91
+ }
92
+ #fits(size) {
93
+ const pending = this.#batches.pending;
94
+ return pending === 0 || pending + size <= this.#capacity;
95
+ }
96
+ async #wait(signal) {
97
+ signal?.throwIfAborted();
98
+ const { promise, resolve, reject } = Promise.withResolvers();
99
+ const abort = () => reject(signal?.reason);
100
+ signal?.addEventListener('abort', abort, { once: true });
101
+ this.#waiting.push(resolve);
102
+ try {
103
+ await promise;
104
+ }
105
+ finally {
106
+ signal?.removeEventListener('abort', abort);
107
+ }
108
+ }
109
+ }
110
+ //# sourceMappingURL=tracker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracker.js","sourceRoot":"","sources":["../src/tracker.ts"],"names":[],"mappings":"AAUA;;;;GAIG;AACH,MAAM,OAAO;IACF,KAAK,GAAe,EAAE,CAAC;IACvB,KAAK,GAAG,IAAI,GAAG,EAAY,CAAC;IAC5B,SAAS,GAAG,IAAI,OAAO,EAAY,CAAC;IAC7C,QAAQ,GAAe,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;IACxC,QAAQ,GAAG,CAAC,CAAC;IAEb,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED,OAAO;QACL,OAAO,IAAI,CAAC,QAAQ,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,C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OAAO,CAAC;QACtC,OAAO,OAAO,KAAK,CAAC,IAAI,OAAO,GAAG,IAAI,IAAI,IAAI,CAAC,SAAS,CAAC;IAC3D,CAAC;IAED,KAAK,CAAC,KAAK,CAAC,MAA+B;QACzC,MAAM,EAAE,cAAc,EAAE,CAAC;QACzB,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,aAAa,EAAQ,CAAC;QACnE,MAAM,KAAK,GAAG,GAAG,EAAE,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC3C,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;QACzD,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC5B,IAAI,CAAC;YACH,MAAM,OAAO,CAAC;QAChB,CAAC;gBAAS,CAAC;YACT,MAAM,EAAE,mBAAmB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QAC9C,CAAC;IACH,CAAC;CACF"}
package/package.json CHANGED
@@ -1,6 +1,50 @@
1
1
  {
2
2
  "name": "@zukhruf/checkpoint",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.8.166",
4
+ "description": "Keep how far a reader of a stream got: a checkpoint for each consumer group, stream and partition, with an opaque versioned position and map states that commit all or nothing, fenced against older claims, in memory, SQLite, Postgres or SQL Server.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/JanuaryLabs/zukhruf",
9
+ "directory": "packages/checkpoint"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/JanuaryLabs/zukhruf/issues"
13
+ },
14
+ "homepage": "https://github.com/JanuaryLabs/zukhruf/tree/main/packages/checkpoint#readme",
15
+ "keywords": [
16
+ "checkpoint",
17
+ "stream",
18
+ "offset",
19
+ "consumer-group",
20
+ "partition",
21
+ "resume",
22
+ "fencing",
23
+ "state"
24
+ ],
25
+ "type": "module",
26
+ "exports": {
27
+ "./package.json": "./package.json",
28
+ ".": {
29
+ "@zukhruf/source": "./src/index.ts",
30
+ "types": "./dist/index.d.ts",
31
+ "import": "./dist/index.js",
32
+ "default": "./dist/index.js"
33
+ }
34
+ },
35
+ "files": [
36
+ "dist",
37
+ "!dist/spec/**",
38
+ "!**/*.tsbuildinfo",
39
+ "!**/*.test.*"
40
+ ],
41
+ "publishConfig": {
42
+ "access": "public"
43
+ },
44
+ "dependencies": {
45
+ "@zukhruf/fencing": "^0.8.166"
46
+ },
47
+ "devDependencies": {
48
+ "@zukhruf/parameterized-test": "^0.8.166"
49
+ }
50
+ }