@h1r-ai/croche 0.6.0 → 0.6.2
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/index.workerd.js
CHANGED
package/dist/pal/file-lock.d.ts
CHANGED
|
@@ -14,20 +14,23 @@ export type LockRecord = {
|
|
|
14
14
|
start?: string;
|
|
15
15
|
};
|
|
16
16
|
export type Incarnation = {
|
|
17
|
-
os: 'linux' | 'darwin';
|
|
17
|
+
os: 'linux' | 'darwin' | 'win32';
|
|
18
18
|
boot: string;
|
|
19
19
|
pidns: string;
|
|
20
20
|
start?: string;
|
|
21
21
|
};
|
|
22
22
|
/** The PID namespace macOS records name: it has none, so every process shares this one. */
|
|
23
23
|
export declare const DARWIN_PIDNS = "darwin";
|
|
24
|
-
/**
|
|
25
|
-
|
|
24
|
+
/** Windows's scope: one PID space per host, and no boot id. The host is checked in `keeps`. */
|
|
25
|
+
export declare const WIN32_SCOPE = "win32";
|
|
26
|
+
/** This process's scope and incarnation, or null where the OS cannot say (anything here
|
|
27
|
+
* failing): then no held record is taken over.
|
|
26
28
|
* - Linux (/proc): the boot id (/proc/sys/kernel/random/boot_id), the PID namespace (the inode in
|
|
27
29
|
* readlink /proc/self/ns/pid, `pid:[N]`), and the start in clock ticks since boot
|
|
28
30
|
* (/proc/self/stat field 22).
|
|
29
31
|
* - macOS: kern.bootsessionuuid (sysctl), and the one PID namespace. No start: the one the OS
|
|
30
32
|
* gives is wall-clock text in the reader's time zone, so the same pid is always this process.
|
|
33
|
+
* - Windows: the fixed WIN32_SCOPE for boot and PID space, and no start, as on macOS.
|
|
31
34
|
* None moves with the wall clock, and every thread of the process reads the same values. */
|
|
32
35
|
export declare function incarnation(): Incarnation | null;
|
|
33
36
|
/** The generations of `path` present now, highest first. */
|
|
@@ -35,8 +38,7 @@ export declare function generations(path: string): Promise<number[]>;
|
|
|
35
38
|
/** Record `generation` of `path`: 'absent' if it is gone, null if it cannot be read as one. */
|
|
36
39
|
export declare function lockRecord(path: string, generation: number): Promise<LockRecord | null | 'absent'>;
|
|
37
40
|
export type FileLock = {
|
|
38
|
-
/**
|
|
39
|
-
* `released`. */
|
|
41
|
+
/** Runs `released`, then publishes a `free` record above this owner's while it is still on top. */
|
|
40
42
|
release: () => Promise<void>;
|
|
41
43
|
/** The fence: throws (no code) unless this owner's record (generation and nonce) is still
|
|
42
44
|
* the one on top. */
|
package/dist/pal/file-lock.js
CHANGED
|
@@ -36,7 +36,11 @@ import { randomUUID } from 'node:crypto';
|
|
|
36
36
|
// UUID per boot, and there are no PID namespaces. The host is not part of it there: a Mac's
|
|
37
37
|
// hostname follows the network it is on, and equal per-boot UUIDs already name one machine.
|
|
38
38
|
// A record names its OS, and a Linux record and a macOS record are never in one scope (a
|
|
39
|
-
// directory shared with a container).
|
|
39
|
+
// directory shared with a container).
|
|
40
|
+
// - Windows has a scope too: the host, and the one PID space. There is no boot id to read without
|
|
41
|
+
// a clock, so a pid reused after a reboot reads as alive and keeps the lock (the safe
|
|
42
|
+
// direction), and a dead pid is taken over as on Linux. Without it, every Ctrl+C or crash left
|
|
43
|
+
// the store `held` until the records were removed by hand (Keel, 0.6.1 Windows audit).
|
|
40
44
|
/** Lock records this module holds, by lock path: the generation and its nonce. */
|
|
41
45
|
const held = new Map();
|
|
42
46
|
let exitHook = false;
|
|
@@ -44,18 +48,22 @@ let exitHook = false;
|
|
|
44
48
|
const ATTEMPTS = 5;
|
|
45
49
|
/** The PID namespace macOS records name: it has none, so every process shares this one. */
|
|
46
50
|
export const DARWIN_PIDNS = 'darwin';
|
|
47
|
-
/**
|
|
48
|
-
|
|
51
|
+
/** Windows's scope: one PID space per host, and no boot id. The host is checked in `keeps`. */
|
|
52
|
+
export const WIN32_SCOPE = 'win32';
|
|
53
|
+
/** This process's scope and incarnation, or null where the OS cannot say (anything here
|
|
54
|
+
* failing): then no held record is taken over.
|
|
49
55
|
* - Linux (/proc): the boot id (/proc/sys/kernel/random/boot_id), the PID namespace (the inode in
|
|
50
56
|
* readlink /proc/self/ns/pid, `pid:[N]`), and the start in clock ticks since boot
|
|
51
57
|
* (/proc/self/stat field 22).
|
|
52
58
|
* - macOS: kern.bootsessionuuid (sysctl), and the one PID namespace. No start: the one the OS
|
|
53
59
|
* gives is wall-clock text in the reader's time zone, so the same pid is always this process.
|
|
60
|
+
* - Windows: the fixed WIN32_SCOPE for boot and PID space, and no start, as on macOS.
|
|
54
61
|
* None moves with the wall clock, and every thread of the process reads the same values. */
|
|
55
62
|
export function incarnation() {
|
|
56
63
|
if (cached !== undefined)
|
|
57
64
|
return cached;
|
|
58
|
-
cached = process.platform === 'darwin' ? darwin() : process.platform === 'linux' ? linux()
|
|
65
|
+
cached = process.platform === 'darwin' ? darwin() : process.platform === 'linux' ? linux()
|
|
66
|
+
: process.platform === 'win32' ? { os: 'win32', boot: WIN32_SCOPE, pidns: WIN32_SCOPE } : null;
|
|
59
67
|
return cached;
|
|
60
68
|
}
|
|
61
69
|
let cached;
|
|
@@ -288,6 +296,7 @@ export async function acquireFileLock(path, released) {
|
|
|
288
296
|
};
|
|
289
297
|
const release = async () => {
|
|
290
298
|
held.delete(path);
|
|
299
|
+
released();
|
|
291
300
|
try {
|
|
292
301
|
const [top = 0] = await generations(path);
|
|
293
302
|
const found = top === generation ? await lockRecord(path, generation) : null;
|
|
@@ -296,7 +305,6 @@ export async function acquireFileLock(path, released) {
|
|
|
296
305
|
}
|
|
297
306
|
}
|
|
298
307
|
catch { /* the next opener checks the pid */ }
|
|
299
|
-
released();
|
|
300
308
|
};
|
|
301
309
|
return { release, verify };
|
|
302
310
|
}
|
package/dist/pal/file-storage.js
CHANGED
|
@@ -46,8 +46,15 @@ export class FileStorage {
|
|
|
46
46
|
if (owner !== undefined)
|
|
47
47
|
await this.held.get(owner).verify();
|
|
48
48
|
else
|
|
49
|
-
for (const lock of this.held
|
|
50
|
-
|
|
49
|
+
for (const [name, lock] of this.held) {
|
|
50
|
+
try {
|
|
51
|
+
await lock.verify();
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
if (this.held.get(name) === lock)
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
51
58
|
}
|
|
52
59
|
async get(dir, key) {
|
|
53
60
|
try {
|