@indigoai-us/hq-cli 5.108.26 → 5.109.0
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/CHANGELOG.md +20 -0
- package/dist/commands/__fixtures__/access-vault.d.ts +93 -0
- package/dist/commands/__fixtures__/access-vault.js +166 -0
- package/dist/commands/access.d.ts +158 -0
- package/dist/commands/access.js +783 -0
- package/dist/commands/cloud.js +11 -1
- package/dist/commands/files-browse.d.ts +25 -1
- package/dist/commands/files-browse.js +81 -17
- package/dist/commands/files.js +15 -5
- package/dist/commands/sync-mode.js +12 -1
- package/dist/commands/sync-narrow.js +12 -1
- package/dist/register-all.js +3 -0
- package/dist/utils/access-denied-hint.d.ts +32 -0
- package/dist/utils/access-denied-hint.js +139 -0
- package/dist/utils/access-requests.d.ts +28 -0
- package/dist/utils/access-requests.js +98 -0
- package/package.json +1 -1
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
export const ACCESS_REQUEST_DEDUPE_MS = 24 * 60 * 60 * 1000;
|
|
4
|
+
export function accessRequestsPath(hqRoot) {
|
|
5
|
+
return path.join(hqRoot, ".hq", "access-requests.json");
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Read the ledger. A missing file is the empty ledger; any other failure
|
|
9
|
+
* (unreadable, corrupt JSON, non-array shape) is rethrown with the ledger path
|
|
10
|
+
* so a corrupt file is never silently replaced by the next write.
|
|
11
|
+
*/
|
|
12
|
+
export function readAccessRequests(hqRoot) {
|
|
13
|
+
const file = accessRequestsPath(hqRoot);
|
|
14
|
+
let raw;
|
|
15
|
+
try {
|
|
16
|
+
raw = fs.readFileSync(file, "utf-8");
|
|
17
|
+
}
|
|
18
|
+
catch (err) {
|
|
19
|
+
if (err.code === "ENOENT")
|
|
20
|
+
return [];
|
|
21
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
22
|
+
throw new Error(`Cannot read access-request ledger at ${file}: ${message}`);
|
|
23
|
+
}
|
|
24
|
+
let parsed;
|
|
25
|
+
try {
|
|
26
|
+
parsed = JSON.parse(raw);
|
|
27
|
+
}
|
|
28
|
+
catch (err) {
|
|
29
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
30
|
+
throw new Error(`Corrupt access-request ledger at ${file}: ${message}. Repair or remove the file; it was not overwritten.`);
|
|
31
|
+
}
|
|
32
|
+
if (!Array.isArray(parsed)) {
|
|
33
|
+
throw new Error(`Corrupt access-request ledger at ${file}: expected a JSON array. Repair or remove the file; it was not overwritten.`);
|
|
34
|
+
}
|
|
35
|
+
return parsed;
|
|
36
|
+
}
|
|
37
|
+
export function recordAccessRequest(hqRoot, rec) {
|
|
38
|
+
// Read first: a corrupt ledger throws here, before anything is written.
|
|
39
|
+
const existing = readAccessRequests(hqRoot);
|
|
40
|
+
existing.push(rec);
|
|
41
|
+
const file = accessRequestsPath(hqRoot);
|
|
42
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
43
|
+
// Atomic replace: write a sibling tmp file, then rename over the ledger so a
|
|
44
|
+
// crash mid-write can never leave a truncated file behind.
|
|
45
|
+
const tmp = `${file}.${process.pid}.${Date.now()}.tmp`;
|
|
46
|
+
try {
|
|
47
|
+
fs.writeFileSync(tmp, JSON.stringify(existing, null, 2), "utf-8");
|
|
48
|
+
fs.renameSync(tmp, file);
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
// A failed rename must not strand the tmp file next to the ledger.
|
|
52
|
+
if (fs.existsSync(tmp))
|
|
53
|
+
fs.unlinkSync(tmp);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export function findRecentAccessRequest(hqRoot, args) {
|
|
57
|
+
const windowMs = args.windowMs ?? ACCESS_REQUEST_DEDUPE_MS;
|
|
58
|
+
const rows = readAccessRequests(hqRoot);
|
|
59
|
+
let latest = null;
|
|
60
|
+
for (const row of rows) {
|
|
61
|
+
if (row.requester !== args.requester ||
|
|
62
|
+
row.prefix !== args.prefix ||
|
|
63
|
+
row.company !== args.company ||
|
|
64
|
+
row.grantor !== args.grantor) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const t = Date.parse(row.sentAt);
|
|
68
|
+
if (!Number.isFinite(t))
|
|
69
|
+
continue;
|
|
70
|
+
if (args.now - t > windowMs)
|
|
71
|
+
continue;
|
|
72
|
+
if (!latest || Date.parse(latest.sentAt) < t)
|
|
73
|
+
latest = row;
|
|
74
|
+
}
|
|
75
|
+
return latest;
|
|
76
|
+
}
|
|
77
|
+
export function formatTimeAgo(iso, nowMs) {
|
|
78
|
+
const t = Date.parse(iso);
|
|
79
|
+
if (!Number.isFinite(t))
|
|
80
|
+
return "just now";
|
|
81
|
+
const delta = Math.max(0, nowMs - t);
|
|
82
|
+
const minute = 60 * 1000;
|
|
83
|
+
const hour = 60 * minute;
|
|
84
|
+
const day = 24 * hour;
|
|
85
|
+
if (delta < minute)
|
|
86
|
+
return "just now";
|
|
87
|
+
if (delta < hour) {
|
|
88
|
+
const n = Math.floor(delta / minute);
|
|
89
|
+
return `${n} minute${n === 1 ? "" : "s"} ago`;
|
|
90
|
+
}
|
|
91
|
+
if (delta < day) {
|
|
92
|
+
const n = Math.floor(delta / hour);
|
|
93
|
+
return `${n} hour${n === 1 ? "" : "s"} ago`;
|
|
94
|
+
}
|
|
95
|
+
const n = Math.floor(delta / day);
|
|
96
|
+
return `${n} day${n === 1 ? "" : "s"} ago`;
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=access-requests.js.map
|