forked from openclaw/openclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask-registry.sqlite.shared.ts
More file actions
58 lines (53 loc) · 1.89 KB
/
Copy pathtask-registry.sqlite.shared.ts
File metadata and controls
58 lines (53 loc) · 1.89 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
import { chmodSync, existsSync, mkdirSync } from "node:fs";
import { isRecord } from "../utils.js";
import { normalizeDeliveryContext } from "../utils/delivery-context.shared.js";
import type { DeliveryContext } from "../utils/delivery-context.types.js";
export const SQLITE_SIDECAR_SUFFIXES = ["", "-shm", "-wal"] as const;
export function normalizeSqliteNumber(value: number | bigint | null): number | undefined {
if (typeof value === "bigint") {
return Number(value);
}
return typeof value === "number" ? value : undefined;
}
// oxlint-disable-next-line typescript/no-unnecessary-type-parameters -- Persisted JSON columns are typed by the receiving field.
export function parseSqliteJsonValue<T>(raw: string | null): T | undefined {
if (!raw?.trim()) {
return undefined;
}
try {
return JSON.parse(raw) as T;
} catch {
return undefined;
}
}
export function parseDeliveryContextJson(raw: string | null): DeliveryContext | undefined {
const parsed = parseSqliteJsonValue<unknown>(raw);
if (!isRecord(parsed)) {
return undefined;
}
return normalizeDeliveryContext({
channel: typeof parsed.channel === "string" ? parsed.channel : undefined,
to: typeof parsed.to === "string" ? parsed.to : undefined,
accountId: typeof parsed.accountId === "string" ? parsed.accountId : undefined,
threadId:
typeof parsed.threadId === "string" || typeof parsed.threadId === "number"
? parsed.threadId
: undefined,
});
}
export function ensureSqliteStorePermissions(params: {
dir: string;
pathname: string;
dirMode: number;
fileMode: number;
}) {
mkdirSync(params.dir, { recursive: true, mode: params.dirMode });
chmodSync(params.dir, params.dirMode);
for (const suffix of SQLITE_SIDECAR_SUFFIXES) {
const candidate = `${params.pathname}${suffix}`;
if (!existsSync(candidate)) {
continue;
}
chmodSync(candidate, params.fileMode);
}
}