@samitouri / QOSami-HFS / commits / 6a48599a

revert to node 18.5.0 (postponed)

Massimo Melina committed Jul 7, 2024 at 23:39 UTC 6a48599ac9b55c70a65b3ecf3863d8d0339d3153
2 files changed +20 -12
package.json
+1 -1
@@ -118,7 +118,7 @@
118 "mocha": "^9.1.3",
119 "nm-prune": "^5.0.0",
120 "nodemon": "^2.0.15",
121 - "@yao-pkg/pkg": "^5.12.0",
121 + "pkg": "^5.7.0",
122 "tough-cookie": "^4.0.0",
123 "ts-node": "^10.4.0"
124 }
src/util-os.ts
+19 -11
@@ -1,18 +1,23 @@
1 -import { resolve } from 'path'
2 -import { statfsSync } from 'fs'
3 -import { statfs } from 'node:fs/promises'
4 -import { exec, ExecOptions } from 'child_process'
5 -import { haveTimeout, onlyTruthy, prefix, splitAt } from './misc'
1 +import { dirname, resolve } from 'path'
2 +import { existsSync } from 'fs'
3 +import { exec, ExecOptions, execSync, spawnSync } from 'child_process'
4 +import { exists, onlyTruthy, prefix, splitAt } from './misc'
5 import _ from 'lodash'
6 import { pid } from 'node:process'
7 import { promisify } from 'util'
8 import { IS_WINDOWS } from './const'
9
10 const DF_TIMEOUT = 2000
11 +const LOGICALDISK_TIMEOUT = DF_TIMEOUT
12
13 +// not using statfsSync because it's not available in node 18.5.0 (latest version with pkg)
14 export function getDiskSpaceSync(path: string) {
14 - const s = statfsSync(path)
15 - return { free: s.bavail * s.bsize, total: s.blocks * s.bsize }
15 + if (IS_WINDOWS)
16 + return parseLogicaldisk(execSync(makeLogicaldisk(path), { timeout: LOGICALDISK_TIMEOUT }).toString())[0]
17 + while (path && !existsSync(path))
18 + path = dirname(path)
19 + try { return parseDfResult(spawnSync('df', ['-k', path], { timeout: DF_TIMEOUT }).stdout.toString())[0] }
20 + catch(e: any) { throw parseDfResult(e) }
21 }
22
23 export function bashEscape(par: string) {
@@ -24,14 +29,17 @@ export function cmdEscape(par: string) {
29 }
30
31 export async function getDiskSpace(path: string) {
27 - const s = await haveTimeout(DF_TIMEOUT, statfs(path))
28 - return { name: path, free: s.bavail * s.bsize, total: s.blocks * s.bsize }
32 + if (IS_WINDOWS)
33 + return parseLogicaldisk(await runCmd(makeLogicaldisk(path), [], { timeout: LOGICALDISK_TIMEOUT }))[0]
34 + while (path && !await exists(path))
35 + path = dirname(path)
36 + return parseDfResult(await promisify(exec)(`df -k`, { timeout: DF_TIMEOUT }).then(x => x.stdout, e => e))[0]
37 }
38
39 export async function getDiskSpaces(): Promise<{ name: string, free: number, total: number, description?: string }[]> {
40 if (IS_WINDOWS) {
33 - const drives = await getDrives()
34 - return onlyTruthy(await Promise.all(drives.map(x => getDiskSpace(x).catch(() => {}))))
41 + const drives = await getDrives() // since network-drives can hang 'wmic' for many seconds checking disk space (issue#648), and a single timeout would make whole operation fail, so we fork the job on each drive
42 + return onlyTruthy(await Promise.all(drives.map(getDiskSpace)))
43 }
44 return parseDfResult(await promisify(exec)(`df -k`, { timeout: DF_TIMEOUT }).then(x => x.stdout, e => e))
45 }