@atolis-hq/wake 0.3.30 → 0.3.31
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.
|
@@ -3,6 +3,7 @@ import { join } from 'node:path';
|
|
|
3
3
|
import { atomicJson, encode } from './file-projection-store.js';
|
|
4
4
|
export class FileCheckpointStore {
|
|
5
5
|
root;
|
|
6
|
+
mutations = new Map();
|
|
6
7
|
constructor(root) {
|
|
7
8
|
this.root = root;
|
|
8
9
|
}
|
|
@@ -22,12 +23,26 @@ export class FileCheckpointStore {
|
|
|
22
23
|
}
|
|
23
24
|
}
|
|
24
25
|
async save(consumer, globalPosition) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
await this.mutate(consumer, async () => {
|
|
27
|
+
if (globalPosition < (await this.load(consumer)))
|
|
28
|
+
throw new Error(`Checkpoint regression for ${consumer}`);
|
|
29
|
+
await atomicJson(this.path(consumer), { consumer, globalPosition });
|
|
30
|
+
});
|
|
28
31
|
}
|
|
29
32
|
async reset(consumer) {
|
|
30
|
-
await rm(this.path(consumer), { force: true });
|
|
33
|
+
await this.mutate(consumer, () => rm(this.path(consumer), { force: true }));
|
|
34
|
+
}
|
|
35
|
+
async mutate(consumer, operation) {
|
|
36
|
+
const prior = this.mutations.get(consumer) ?? Promise.resolve();
|
|
37
|
+
const current = prior.catch(() => undefined).then(operation);
|
|
38
|
+
this.mutations.set(consumer, current);
|
|
39
|
+
try {
|
|
40
|
+
return await current;
|
|
41
|
+
}
|
|
42
|
+
finally {
|
|
43
|
+
if (this.mutations.get(consumer) === current)
|
|
44
|
+
this.mutations.delete(consumer);
|
|
45
|
+
}
|
|
31
46
|
}
|
|
32
47
|
path(consumer) {
|
|
33
48
|
return join(this.root, 'checkpoints', `${encode(consumer)}.json`);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
1
2
|
import { mkdir, open, readFile, readdir, rename, rm } from 'node:fs/promises';
|
|
2
3
|
import { dirname, join } from 'node:path';
|
|
3
4
|
export class FileProjectionStore {
|
|
@@ -46,7 +47,7 @@ export function encode(value) {
|
|
|
46
47
|
}
|
|
47
48
|
export async function atomicJson(path, value) {
|
|
48
49
|
await mkdir(dirname(path), { recursive: true });
|
|
49
|
-
const temporary = `${path}.${process.pid}.${Date.now()}.tmp`;
|
|
50
|
+
const temporary = `${path}.${process.pid}.${Date.now()}.${randomUUID()}.tmp`;
|
|
50
51
|
try {
|
|
51
52
|
const handle = await open(temporary, 'wx');
|
|
52
53
|
try {
|