-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisk.ts
More file actions
185 lines (164 loc) · 4.41 KB
/
Copy pathdisk.ts
File metadata and controls
185 lines (164 loc) · 4.41 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { constants } from "node:fs";
import { access, statfs } from "node:fs/promises";
export type ProcessDiskUsage = {
availableBytes: number;
freeBytes: number;
path: string;
totalBytes: number;
usedBytes: number;
usedPercent: number;
};
export type ProcessDiskUsageOptions = {
path?: string;
};
export type ProcessDiskUsageCheckOptions = ProcessDiskUsageOptions & {
maxUsedPercent?: number;
minAvailableBytes?: number;
};
export type ProcessDiskUsedPercentCheck = {
maxPercent: number;
message?: string;
ratio?: number;
status: "error" | "ok";
usedPercent: number;
};
export type ProcessDiskAvailableBytesCheck = {
availableBytes: number;
message?: string;
minBytes: number;
ratio?: number;
status: "error" | "ok";
};
export type ProcessDiskUsageCheckResult = ProcessDiskUsage & {
checks: {
availableBytes?: ProcessDiskAvailableBytesCheck;
usedPercent?: ProcessDiskUsedPercentCheck;
};
status: "error" | "ok";
};
/**
* Read filesystem usage for a path after verifying process access.
*
* @example
* ```ts
* const usage = await getProcessDiskUsage({ path: process.cwd() });
* ```
*/
export async function getProcessDiskUsage(
options: ProcessDiskUsageOptions = {},
): Promise<ProcessDiskUsage> {
const path = await checkProcessDiskAccess(options.path);
const stats = await statfs(path);
const totalBytes = stats.blocks * stats.bsize;
const freeBytes = stats.bfree * stats.bsize;
const availableBytes = stats.bavail * stats.bsize;
const usedBytes = Math.max(0, totalBytes - freeBytes);
return {
availableBytes,
freeBytes,
path,
totalBytes,
usedBytes,
usedPercent: totalBytes > 0 ? usedBytes / totalBytes : 0,
};
}
/**
* Read filesystem usage and compare it with optional process thresholds.
*
* @example
* ```ts
* const result = await checkProcessDiskUsage({ maxUsedPercent: 0.9 });
* ```
*/
export async function checkProcessDiskUsage(
options: ProcessDiskUsageCheckOptions = {},
): Promise<ProcessDiskUsageCheckResult> {
assertDiskThresholds(options);
const usage = await getProcessDiskUsage(options);
const checks: ProcessDiskUsageCheckResult["checks"] = {};
if (options.maxUsedPercent !== undefined) {
checks.usedPercent = createDiskUsedPercentCheck(
usage.usedPercent,
options.maxUsedPercent,
);
}
if (options.minAvailableBytes !== undefined) {
checks.availableBytes = createDiskAvailableBytesCheck(
usage.availableBytes,
options.minAvailableBytes,
);
}
const status = hasFailedDiskChecks(checks) ? "error" : "ok";
return {
...usage,
checks,
status,
};
}
/**
* Verify that the current process can read and write a disk path.
*
* @example
* ```ts
* const diskPath = await checkProcessDiskAccess(process.cwd());
* ```
*/
async function checkProcessDiskAccess(
diskPath: string = process.cwd(),
): Promise<string> {
await access(diskPath, constants.R_OK | constants.W_OK);
return diskPath;
}
function assertDiskThresholds(options: ProcessDiskUsageCheckOptions): void {
if (
options.maxUsedPercent !== undefined &&
(options.maxUsedPercent < 0 || options.maxUsedPercent > 1)
) {
throw new RangeError("maxUsedPercent must be between 0 and 1");
}
if (
options.minAvailableBytes !== undefined &&
options.minAvailableBytes < 0
) {
throw new RangeError(
"minAvailableBytes must be greater than or equal to 0",
);
}
}
function createDiskUsedPercentCheck(
usedPercent: number,
maxPercent: number,
): ProcessDiskUsedPercentCheck {
const status = usedPercent > maxPercent ? "error" : "ok";
return {
maxPercent,
message:
status === "error"
? `disk used percent ${usedPercent} exceeds ${maxPercent}`
: undefined,
ratio: maxPercent > 0 ? usedPercent / maxPercent : undefined,
status,
usedPercent,
};
}
function createDiskAvailableBytesCheck(
availableBytes: number,
minBytes: number,
): ProcessDiskAvailableBytesCheck {
const status = availableBytes < minBytes ? "error" : "ok";
return {
availableBytes,
message:
status === "error"
? `disk available bytes ${availableBytes} is below ${minBytes}`
: undefined,
minBytes,
ratio: minBytes > 0 ? availableBytes / minBytes : undefined,
status,
};
}
function hasFailedDiskChecks(
checks: ProcessDiskUsageCheckResult["checks"],
): boolean {
return Object.values(checks).some((check) => check?.status === "error");
}