@open-wa/utils 5.0.0 → 5.2.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/package.json +2 -1
- package/src/configLoader.ts +40 -0
- package/src/file.ts +79 -0
- package/src/index.ts +3 -0
- package/src/system.ts +18 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-wa/utils",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.2.0",
|
|
4
4
|
"description": "Shared utility functions for open-wa",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"files": [
|
|
17
|
+
"src",
|
|
17
18
|
"dist"
|
|
18
19
|
],
|
|
19
20
|
"dependencies": {
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as path from 'path';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import JSON5 from 'json5';
|
|
4
|
+
|
|
5
|
+
export const tryOpenFileAsObject = async (fileLocation: string, needArray = false): Promise<any> => {
|
|
6
|
+
let res: any = undefined;
|
|
7
|
+
const relativePath = path.join(path.resolve(process.cwd(), fileLocation || ''));
|
|
8
|
+
const isJs = fileLocation.endsWith('.js') || fileLocation.endsWith('.cjs') || fileLocation.endsWith('.mjs');
|
|
9
|
+
|
|
10
|
+
if (fs.existsSync(fileLocation) || fs.existsSync(relativePath)) {
|
|
11
|
+
const fp: string = isJs
|
|
12
|
+
? (fs.existsSync(relativePath) ? relativePath : fileLocation)
|
|
13
|
+
: (fs.existsSync(fileLocation) ? fileLocation : relativePath);
|
|
14
|
+
|
|
15
|
+
try {
|
|
16
|
+
let data;
|
|
17
|
+
if (isJs) {
|
|
18
|
+
// We use dynamic import for JS files in ESM context
|
|
19
|
+
const imported = await import(path.resolve(fp));
|
|
20
|
+
data = imported.default || imported;
|
|
21
|
+
} else {
|
|
22
|
+
data = JSON5.parse(fs.readFileSync(fp, 'utf8'));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (data && (Array.isArray(data) === needArray)) res = data;
|
|
26
|
+
if (data && typeof data === 'function') {
|
|
27
|
+
res = await data(process.env.CURRENT_SESSION_ID || 'session');
|
|
28
|
+
}
|
|
29
|
+
} catch (error) {
|
|
30
|
+
throw new Error(`Unable to parse config file. Please make sure ${fp} is valid. Error: ${error}`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return res ? {
|
|
34
|
+
...res,
|
|
35
|
+
confPath: fp
|
|
36
|
+
} : undefined;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return undefined;
|
|
40
|
+
};
|
package/src/file.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import * as crypto from 'crypto';
|
|
4
|
+
import { tmpdir } from 'os';
|
|
5
|
+
import { Readable } from 'stream';
|
|
6
|
+
|
|
7
|
+
export const isUrl = (s: string) => {
|
|
8
|
+
try {
|
|
9
|
+
new URL(s);
|
|
10
|
+
return true;
|
|
11
|
+
} catch {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const isDataUrl = (s: string) => typeof s === 'string' && !!s.match(/^data:((?:\w+\/(?:(?!;).)+)?)((?:;[\w\W]*?[^;])*),(.+)$/g);
|
|
17
|
+
|
|
18
|
+
export const isBase64 = (s: string) => {
|
|
19
|
+
if (typeof s !== 'string') return false;
|
|
20
|
+
const len = s.length;
|
|
21
|
+
if (!len || len % 4 !== 0 || /[^A-Z0-9+/=]/i.test(s)) return false;
|
|
22
|
+
const firstPaddingChar = s.indexOf('=');
|
|
23
|
+
return firstPaddingChar === -1 || firstPaddingChar === len - 1 || (firstPaddingChar === len - 2 && s[len - 1] === '=');
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const getDUrl = async (url: string): Promise<string> => {
|
|
27
|
+
const res = await fetch(url);
|
|
28
|
+
if (!res.ok) throw new Error(`Failed to fetch ${url}: ${res.statusText}`);
|
|
29
|
+
const mime = res.headers.get('content-type') || 'application/octet-stream';
|
|
30
|
+
const arrayBuffer = await res.arrayBuffer();
|
|
31
|
+
const base64 = Buffer.from(arrayBuffer).toString('base64');
|
|
32
|
+
return `data:${mime};base64,${base64}`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const assertFile = async (input: any, outputType: string): Promise<any> => {
|
|
36
|
+
let dUrl: string;
|
|
37
|
+
|
|
38
|
+
if (Buffer.isBuffer(input)) {
|
|
39
|
+
dUrl = `data:application/octet-stream;base64,${input.toString('base64')}`;
|
|
40
|
+
} else if (typeof input === 'string') {
|
|
41
|
+
if (isDataUrl(input)) dUrl = input;
|
|
42
|
+
else if (isBase64(input)) dUrl = `data:application/octet-stream;base64,${input}`;
|
|
43
|
+
else if (isUrl(input)) dUrl = await getDUrl(input);
|
|
44
|
+
else {
|
|
45
|
+
const resolvedPath = path.resolve(process.cwd(), input);
|
|
46
|
+
if (fs.existsSync(resolvedPath)) {
|
|
47
|
+
const buffer = await fs.promises.readFile(resolvedPath);
|
|
48
|
+
dUrl = `data:application/octet-stream;base64,${buffer.toString('base64')}`;
|
|
49
|
+
} else {
|
|
50
|
+
throw new Error(`Cannot find file or process input: ${input.slice(0, 25)}`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
} else {
|
|
54
|
+
throw new Error('Invalid file input');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (dUrl.includes('data:') && dUrl.includes('undefined')) {
|
|
58
|
+
dUrl = dUrl.replace('undefined', 'application/octet-stream');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const base64 = dUrl.split(',')[1] || dUrl;
|
|
62
|
+
|
|
63
|
+
switch (outputType) {
|
|
64
|
+
case 'DATA_URL':
|
|
65
|
+
return dUrl;
|
|
66
|
+
case 'BASE_64':
|
|
67
|
+
return base64;
|
|
68
|
+
case 'BUFFER':
|
|
69
|
+
return Buffer.from(base64, 'base64');
|
|
70
|
+
case 'TEMP_FILE_PATH':
|
|
71
|
+
const tempFilePath = path.join(tmpdir(), `${crypto.randomBytes(6).toString('hex')}.tmp`);
|
|
72
|
+
await fs.promises.writeFile(tempFilePath, Buffer.from(base64, 'base64'));
|
|
73
|
+
return tempFilePath;
|
|
74
|
+
case 'READ_STREAM':
|
|
75
|
+
return Readable.from(Buffer.from(base64, 'base64'));
|
|
76
|
+
default:
|
|
77
|
+
return input;
|
|
78
|
+
}
|
|
79
|
+
};
|
package/src/index.ts
ADDED
package/src/system.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import pidtree from 'pidtree';
|
|
2
|
+
import pidusage from 'pidusage';
|
|
3
|
+
|
|
4
|
+
type PidStat = { cpu: number; memory: number; ppid: number; pid: number; ctime: number; elapsed: number; timestamp: number };
|
|
5
|
+
|
|
6
|
+
export const pidTreeUsage = async (pid: number | number[]): Promise<{ [key: string]: PidStat } | Record<string, never>> => {
|
|
7
|
+
let pidArray = Array.isArray(pid) ? pid : [pid];
|
|
8
|
+
try {
|
|
9
|
+
const pids: number[] = (await Promise.all(pidArray.map(p => pidtree(p)))).flat() as number[];
|
|
10
|
+
// Include the original pids in the list for measurement
|
|
11
|
+
const allPids = Array.from(new Set([...pidArray, ...pids]));
|
|
12
|
+
const stats = await pidusage(allPids);
|
|
13
|
+
return stats;
|
|
14
|
+
} catch (error) {
|
|
15
|
+
console.error('Error fetching PID tree usage:', error);
|
|
16
|
+
return {};
|
|
17
|
+
}
|
|
18
|
+
};
|