-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.ts
More file actions
64 lines (58 loc) · 2.68 KB
/
Copy pathstate.ts
File metadata and controls
64 lines (58 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { mkdir, open, readFile, rename, rm } from "node:fs/promises";
import { join } from "node:path";
import { randomUUID } from "node:crypto";
import lockfile from "proper-lockfile";
import { z } from "zod";
export interface Store<T> { load(): Promise<T>; save(value: T): Promise<void> }
export class JsonStore<T> implements Store<T> {
constructor(readonly file: string, private schema: z.ZodType<T>, private initial: () => T) {}
async load(): Promise<T> {
try { return this.schema.parse(JSON.parse(await readFile(this.file, "utf8"))); }
catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") return this.initial();
throw error; // Never reset a corrupt queue silently.
}
}
async save(value: T): Promise<void> {
const validated = this.schema.parse(value);
const temporary = `${this.file}.${randomUUID()}.tmp`;
const handle = await open(temporary, "wx", 0o600);
try {
await handle.writeFile(JSON.stringify(validated, null, 2));
await handle.sync();
} finally { await handle.close(); }
try { await rename(temporary, this.file); }
finally { await rm(temporary, { force: true }); }
}
}
export async function acquire(directory: string, name: string, compromised: (error: Error) => void, waitForPrevious = false) {
await mkdir(directory, { recursive: true, mode: 0o700 });
return lockfile.lock(join(directory, name), {
realpath: false, stale: 30_000, update: 10_000,
// Location invalidation can start a replacement before the old plugin's
// finalizers finish. Wait for release, never remove a live owner's lock.
retries: waitForPrevious ? { retries: 30, factor: 1, minTimeout: 500, maxTimeout: 500 } : 0,
onCompromised: compromised,
});
}
export class Serial {
private tail: Promise<unknown> = Promise.resolve();
run<T>(action: () => Promise<T>): Promise<T> {
const next = this.tail.then(action);
this.tail = next.catch(() => {});
return next;
}
}
export function redact(error: unknown, secrets: string[] = []): string {
let message = describeError(error);
for (const secret of secrets.filter(Boolean)) message = message.split(secret).join("[REDACTED]");
return message.slice(0, 2000);
}
function describeError(error: unknown, depth = 0): string {
if (!error || typeof error !== "object") return String(error);
if (depth > 3) return "Nested error";
const value = error as Record<string, unknown>;
const parts = [...new Set([value._tag, value.type, value.name, value.message].filter(v => typeof v === "string" && v.trim()))];
if (value.cause) parts.push(describeError(value.cause, depth + 1));
return parts.join(": ") || `Unknown error (${Object.keys(value).join(", ") || "no details"})`;
}