@waiting/shared-core
Advanced tools
Comparing version 13.0.0 to 13.1.0
@@ -5,3 +5,3 @@ /** | ||
* | ||
* @version 12.0.0 | ||
* @version 13.0.0 | ||
* @author waiting | ||
@@ -419,40 +419,30 @@ * @license MIT | ||
*/ | ||
async function retrieveProcInfo() { | ||
const ret = { | ||
cpuinfo: {}, | ||
meminfo: {}, | ||
stat: {}, | ||
}; | ||
async function retrieveProcInfo(items = ['cpuinfo', 'meminfo', 'diskstats', 'stat']) { | ||
const info = {}; | ||
if (process.platform !== 'linux') { | ||
return ret; | ||
return info; | ||
} | ||
const arr = ['cpuinfo', 'meminfo']; | ||
const pms = []; | ||
for (const name of arr) { | ||
items.forEach((name) => { | ||
const path = `/proc/${name}`; | ||
const pm = promises.readFile(path, 'utf-8').catch(() => ''); | ||
const pm = promises.readFile(path, 'utf-8') | ||
.catch(() => '') | ||
.then((str) => { | ||
const data = str.includes(':') | ||
? processInfoWithColon(str) | ||
: processInfoWithSpace(str); | ||
// @ts-expect-error | ||
info[name] = data; | ||
}); | ||
pms.push(pm); | ||
} | ||
const arr2 = ['stat']; | ||
for (const name of arr2) { | ||
const path = `/proc/${name}`; | ||
const pm = promises.readFile(path, 'utf-8').catch(() => ''); | ||
pms.push(pm); | ||
} | ||
const info = Promise.all(pms) | ||
.then((data) => { | ||
const cpuinfo = processCpuAndMemInfo(data[0]); | ||
const meminfo = processCpuAndMemInfo(data[1]); | ||
const stat = processStatInfo(data[2]); | ||
const res = { | ||
cpuinfo, | ||
meminfo, | ||
stat, | ||
}; | ||
return res; | ||
}); | ||
return info; | ||
const ret = Promise.all(pms) | ||
.then(() => { | ||
return info; | ||
}); | ||
return ret; | ||
} | ||
function processCpuAndMemInfo(str) { | ||
function processInfoWithColon(input) { | ||
const row = {}; | ||
const str = input.trim(); | ||
if (!str.length) { | ||
@@ -475,4 +465,5 @@ return row; | ||
} | ||
function processStatInfo(str) { | ||
function processInfoWithSpace(input) { | ||
const row = {}; | ||
const str = input.trim(); | ||
if (!str.length) { | ||
@@ -489,3 +480,3 @@ return row; | ||
} | ||
row[k1] = value.join(' '); | ||
row[k1] = value.join(' ').trim(); | ||
} | ||
@@ -492,0 +483,0 @@ }); |
@@ -15,5 +15,6 @@ /// <reference types="node" /> | ||
export declare function humanMemoryUsage(digits?: number, sep?: string): HumanMemoryUsage; | ||
export declare type ProcInfoItem = keyof ProcInfo; | ||
/** | ||
* Retrieve info from /proc/ for linux | ||
*/ | ||
export declare function retrieveProcInfo(): Promise<ProcInfo>; | ||
export declare function retrieveProcInfo(items?: ProcInfoItem[]): Promise<ProcInfo>; |
@@ -23,40 +23,30 @@ import { readFile } from 'fs/promises'; | ||
*/ | ||
export async function retrieveProcInfo() { | ||
const ret = { | ||
cpuinfo: {}, | ||
meminfo: {}, | ||
stat: {}, | ||
}; | ||
export async function retrieveProcInfo(items = ['cpuinfo', 'meminfo', 'diskstats', 'stat']) { | ||
const info = {}; | ||
if (process.platform !== 'linux') { | ||
return ret; | ||
return info; | ||
} | ||
const arr = ['cpuinfo', 'meminfo']; | ||
const pms = []; | ||
for (const name of arr) { | ||
items.forEach((name) => { | ||
const path = `/proc/${name}`; | ||
const pm = readFile(path, 'utf-8').catch(() => ''); | ||
const pm = readFile(path, 'utf-8') | ||
.catch(() => '') | ||
.then((str) => { | ||
const data = str.includes(':') | ||
? processInfoWithColon(str) | ||
: processInfoWithSpace(str); | ||
// @ts-expect-error | ||
info[name] = data; | ||
}); | ||
pms.push(pm); | ||
} | ||
const arr2 = ['stat']; | ||
for (const name of arr2) { | ||
const path = `/proc/${name}`; | ||
const pm = readFile(path, 'utf-8').catch(() => ''); | ||
pms.push(pm); | ||
} | ||
const info = Promise.all(pms) | ||
.then((data) => { | ||
const cpuinfo = processCpuAndMemInfo(data[0]); | ||
const meminfo = processCpuAndMemInfo(data[1]); | ||
const stat = processStatInfo(data[2]); | ||
const res = { | ||
cpuinfo, | ||
meminfo, | ||
stat, | ||
}; | ||
return res; | ||
}); | ||
return info; | ||
const ret = Promise.all(pms) | ||
.then(() => { | ||
return info; | ||
}); | ||
return ret; | ||
} | ||
function processCpuAndMemInfo(str) { | ||
function processInfoWithColon(input) { | ||
const row = {}; | ||
const str = input.trim(); | ||
if (!str.length) { | ||
@@ -79,4 +69,5 @@ return row; | ||
} | ||
function processStatInfo(str) { | ||
function processInfoWithSpace(input) { | ||
const row = {}; | ||
const str = input.trim(); | ||
if (!str.length) { | ||
@@ -93,3 +84,3 @@ return row; | ||
} | ||
row[k1] = value.join(' '); | ||
row[k1] = value.join(' ').trim(); | ||
} | ||
@@ -96,0 +87,0 @@ }); |
{ | ||
"name": "@waiting/shared-core", | ||
"author": "waiting", | ||
"version": "13.0.0", | ||
"version": "13.1.0", | ||
"description": "node core function re export with Promise or Observable", | ||
@@ -13,5 +13,5 @@ "keywords": [ | ||
"es2015": "", | ||
"main": "./dist/index.cjs.js", | ||
"module": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"main": "dist/index.cjs.js", | ||
"module": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"bugs": { | ||
@@ -27,3 +27,3 @@ "url": "https://github.com/waitingsong/shared/issues" | ||
"dependencies": { | ||
"@waiting/shared-types": "^13.0.0", | ||
"@waiting/shared-types": "^13.1.0", | ||
"rxjs": "7" | ||
@@ -96,3 +96,3 @@ }, | ||
}, | ||
"gitHead": "ef3bcb04ca25f5d5c4736deb9d297535da971117" | ||
"gitHead": "cf66a07b62298feed11d97360450c913837351e5" | ||
} |
import { readFile } from 'fs/promises' | ||
import { ProcCpuinfo, ProcInfo, ProcMeminfo, ProcStat } from '@waiting/shared-types' | ||
import { | ||
ProcCpuinfo, | ||
ProcInfo, | ||
ProcMeminfo, | ||
ProcDiskstats, | ||
ProcStat, | ||
} from '@waiting/shared-types' | ||
@@ -37,48 +43,42 @@ import { defaultPropDescriptor } from './consts' | ||
export type ProcInfoItem = keyof ProcInfo | ||
/** | ||
* Retrieve info from /proc/ for linux | ||
*/ | ||
export async function retrieveProcInfo(): Promise<ProcInfo> { | ||
const ret = { | ||
cpuinfo: {}, | ||
meminfo: {}, | ||
stat: {}, | ||
} as ProcInfo | ||
export async function retrieveProcInfo( | ||
items: ProcInfoItem[] = ['cpuinfo', 'meminfo', 'diskstats', 'stat'], | ||
): Promise<ProcInfo> { | ||
const info = { } as ProcInfo | ||
if (process.platform !== 'linux') { | ||
return ret | ||
return info | ||
} | ||
const arr: (keyof ProcInfo)[] = ['cpuinfo', 'meminfo'] | ||
const pms = [] as unknown as [Promise<string>, Promise<string>, Promise<string>] | ||
for (const name of arr) { | ||
const path = `/proc/${name}` | ||
const pm = readFile(path, 'utf-8').catch(() => '') | ||
pms.push(pm) | ||
} | ||
const pms: Promise<void>[] = [] | ||
const arr2: (keyof ProcInfo)[] = ['stat'] | ||
for (const name of arr2) { | ||
items.forEach((name) => { | ||
const path = `/proc/${name}` | ||
const pm = readFile(path, 'utf-8').catch(() => '') | ||
const pm = readFile(path, 'utf-8') | ||
.catch(() => '') | ||
.then((str) => { | ||
const data = str.includes(':') | ||
? processInfoWithColon(str) | ||
: processInfoWithSpace(str) | ||
// @ts-expect-error | ||
info[name] = data | ||
}) | ||
pms.push(pm) | ||
} | ||
}) | ||
const info: Promise<ProcInfo> = Promise.all(pms) | ||
.then((data) => { | ||
const cpuinfo = processCpuAndMemInfo(data[0]) as ProcCpuinfo | ||
const meminfo = processCpuAndMemInfo(data[1]) as ProcMeminfo | ||
const stat = processStatInfo(data[2]) | ||
const res = { | ||
cpuinfo, | ||
meminfo, | ||
stat, | ||
} | ||
return res | ||
const ret: Promise<ProcInfo> = Promise.all(pms) | ||
.then(() => { | ||
return info | ||
}) | ||
return info | ||
return ret | ||
} | ||
function processCpuAndMemInfo(str: string): ProcCpuinfo | ProcMeminfo { | ||
function processInfoWithColon(input: string): ProcCpuinfo | ProcMeminfo { | ||
const row = {} as ProcCpuinfo | ProcMeminfo | ||
const str = input.trim() | ||
if (! str.length) { | ||
@@ -100,4 +100,5 @@ return row | ||
} | ||
function processStatInfo(str: string): ProcStat { | ||
function processInfoWithSpace(input: string): ProcDiskstats | ProcStat { | ||
const row = {} as ProcStat | ||
const str = input.trim() | ||
if (! str.length) { | ||
@@ -113,3 +114,3 @@ return row | ||
row[k1] = value.join(' ') | ||
row[k1] = value.join(' ').trim() | ||
} | ||
@@ -116,0 +117,0 @@ }) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
75630
2015