@synetic/et 1.0.3 → 1.3.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/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const axios = require('axios');
2
- const yargs = require('yargs')
2
+ const argv = require('yargs/yargs')(process.argv.slice(2)).argv;
3
3
 
4
4
  const { encrypt } = require('./lib/encypt');
5
5
  const { collect } = require('./lib/collector')
@@ -7,26 +7,31 @@ const KEY = process.env.SYNETIC_ET_KEY || '';
7
7
 
8
8
  const HOME_URL = process.env.SYNETIC_ET_HOME || '';
9
9
 
10
- const phoneHome = () => {
10
+ const phoneHome = async () => {
11
11
  console.log(`collecting information to phone home (${HOME_URL})`);
12
- const data = collect();
12
+ const data = await collect();
13
13
  const payload = {
14
- version: '0.0.1',
14
+ version: '1.3.0',
15
15
  data: encrypt(JSON.stringify(data), KEY)
16
16
  }
17
17
 
18
- if (yargs.argv.dryRun) {
18
+ if (argv.dryRun) {
19
19
  console.log('dry running locally');
20
20
  console.log(payload);
21
- if (yargs.argv.reveal) {
22
- console.log(data);
21
+ if (argv.reveal) {
22
+ console.log(JSON.stringify(data));
23
23
  }
24
24
  process.exit();
25
25
  }
26
26
 
27
+ if (!HOME_URL || !KEY) {
28
+ console.log('Missing Phone Home URL or Key.');
29
+ process.exit(1);
30
+ }
31
+
27
32
  axios.post(HOME_URL, payload, {
28
33
  headers: {
29
- 'User-Agent': 'Synetic/ET (NodeJS)'
34
+ 'User-Agent': `Synetic/ET ${payload.version} (NodeJS)`
30
35
  }
31
36
  })
32
37
  .then(() => {
package/lib/collector.js CHANGED
@@ -1,42 +1,164 @@
1
1
  const fs = require('fs');
2
2
  const os = require('os');
3
+ const Redis = require('ioredis');
4
+
5
+ const getLockFilePackages = () => {
6
+ const lock = getPackageJsonLock();
7
+ const isV1 = lock.lockfileVersion === 1;
8
+ return isV1 ? lock.dependencies : lock.packages;
9
+ };
10
+
11
+ const getPackageVersionString = (packageName) => {
12
+ const packages = getLockFilePackages();
13
+ return `${packageName} ${(packages[packageName] || packages[`node_modules/${packageName}`]).version}`;
14
+ };
3
15
 
4
16
  const framework = () => {
5
- if (Object.keys(packageJson().dependencies).indexOf('vue') > -1) {
6
- return `vue ${packageJsonLock().packages['node_modules/vue'].version}`;
17
+ const packageJson = getPackageJson();
18
+
19
+ if (Object.keys(packageJson.dependencies).indexOf('nuxt') > -1) {
20
+ return getPackageVersionString('nuxt');
21
+ }
22
+ if (Object.keys(packageJson.dependencies).indexOf('vue') > -1) {
23
+ return getPackageVersionString('vue');
7
24
  }
8
- if (Object.keys(packageJson().dependencies).indexOf('react') > -1) {
9
- return `react ${packageJsonLock().packages['node_modules/react'].version}`;
25
+ if (Object.keys(packageJson.dependencies).indexOf('react') > -1) {
26
+ return getPackageVersionString('react');
10
27
  }
11
28
  return '';
12
29
  }
13
30
 
14
- const packageJsonLock = () => {
15
- const packageJson = fs.readFileSync(`${process.cwd()}/package-lock.json`)
16
- return JSON.parse(packageJson);
31
+ const getPackageJsonLock = () => {
32
+ try {
33
+ const packageJson = fs.readFileSync(`${process.cwd()}/package-lock.json`)
34
+ return JSON.parse(packageJson);
35
+ } catch (e) {
36
+ return {};
37
+ }
17
38
  }
18
- const packageJson = () => {
19
- const packageJson = fs.readFileSync(`${process.cwd()}/package-lock.json`)
20
- return JSON.parse(packageJson);
39
+ const getPackageJson = () => {
40
+ try {
41
+ const packageJson = fs.readFileSync(`${process.cwd()}/package.json`)
42
+ return JSON.parse(packageJson);
43
+ } catch (e) {
44
+ return { dependencies: {} };
45
+ }
21
46
  }
22
47
 
23
- const data = {
24
- version: {
25
- engine: `nodejs ${process.version}`,
26
- framework: framework()
27
- },
28
- nodeMeta: {
29
- packages: packageJsonLock().packages,
30
- },
31
- systemMeta: {
32
- identifier: os.hostname(),
33
- ip: '',
34
- uptime: os.uptime(),
35
- cpuCount: os.cpus().length,
36
- load: os.loadavg()
48
+ const getRedisInfo = () => {
49
+ return new Promise((resolve) => {
50
+ const host = process.env.REDIS_SERVICE_HOST;
51
+ const port = process.env.REDIS_SERVICE_PORT;
52
+ const password = process.env.REDIS_SERVICE_PASSWORD;
53
+
54
+ if (!host || !port) {
55
+ return resolve(null);
56
+ }
57
+
58
+ const redis = new Redis({
59
+ host: host,
60
+ port: port,
61
+ password: password,
62
+ connectTimeout: 2000,
63
+ maxRetriesPerRequest: 0,
64
+ retryStrategy: () => null // Disable retry
65
+ });
66
+
67
+ redis.info()
68
+ .then((info) => {
69
+ redis.quit();
70
+ resolve(parseRedisInfo(info));
71
+ })
72
+ .catch((err) => {
73
+ redis.quit();
74
+ resolve({ error: err.message });
75
+ });
76
+ });
77
+ };
78
+
79
+ const parseRedisInfo = (infoString) => {
80
+ if (!infoString) return null;
81
+ const lines = infoString.split('\r\n');
82
+
83
+ // Check for Redis error
84
+ if (lines.length > 0 && lines[0].startsWith('-')) {
85
+ return { error: lines[0].substring(1) };
37
86
  }
38
- }
87
+
88
+ const result = {};
89
+ let currentSection = 'default';
90
+
91
+ for (const line of lines) {
92
+ if (line.trim() === '' || line.startsWith('+') || line.startsWith('$') || line.startsWith('*')) continue;
93
+
94
+ if (line.startsWith('#')) {
95
+ currentSection = line.substring(1).trim().toLowerCase();
96
+ result[currentSection] = {};
97
+ continue;
98
+ }
99
+
100
+ const parts = line.split(':');
101
+ if (parts.length === 2) {
102
+ if (!result[currentSection]) {
103
+ result[currentSection] = {};
104
+ }
105
+ result[currentSection][parts[0]] = parts[1].trim();
106
+ }
107
+ }
108
+ return result;
109
+ };
110
+
111
+ const collect = async () => {
112
+ const redisInfo = await getRedisInfo();
113
+
114
+ const services = {};
115
+ if (redisInfo) {
116
+ let version = '';
117
+ if (redisInfo.server && redisInfo.server.redis_version) {
118
+ version = redisInfo.server.redis_version;
119
+ }
120
+
121
+ let type = 'redis';
122
+ if (redisInfo.server && redisInfo.server.server_name === 'valkey') {
123
+ type = 'valkey';
124
+ }
125
+
126
+ services.redis = {
127
+ type: type,
128
+ version: version,
129
+ meta: redisInfo
130
+ };
131
+ }
132
+
133
+ const data = {
134
+ environment: {
135
+ version: process.env.APP_ENV || process.env.LAGOON_ENVIRONMENT || process.env.LAGOON_ENVIRONMENT_TYPE || ''
136
+ },
137
+ engine: {
138
+ version: `nodejs ${process.version}`,
139
+ },
140
+ framework: {
141
+ version: framework(),
142
+ meta: {
143
+ packages: getLockFilePackages(),
144
+ }
145
+ },
146
+ system: {
147
+ version: os.release(),
148
+ meta: {
149
+ identifier: os.hostname(),
150
+ ip: '',
151
+ uptime: os.uptime(),
152
+ cpuCount: os.cpus().length,
153
+ load: os.loadavg()
154
+ }
155
+ },
156
+ services: services
157
+ };
158
+
159
+ return data;
160
+ };
39
161
 
40
162
  module.exports = {
41
- collect: () => data,
163
+ collect,
42
164
  };
package/lib/encypt.js CHANGED
@@ -9,9 +9,16 @@ const signHmacSha512 = (text, key) => {
9
9
  return hmac.update(Buffer.from(text)).digest('hex')
10
10
  }
11
11
 
12
+ const getCipherInfo = (cipher) => {
13
+ if (crypto.getCipherInfo) {
14
+ return crypto.getCipherInfo(cipher).ivLength;
15
+ }
16
+ return 16;
17
+ };
18
+
12
19
  module.exports = {
13
20
  encrypt: (text, secretString) => {
14
- const keyLength = crypto.getCipherInfo(CIPHER).ivLength;
21
+ const keyLength = getCipherInfo(CIPHER);
15
22
  const paddedSecretKey = secretString.substring(0, keyLength).padEnd(keyLength, KEYPAD_CHAR);
16
23
  const ivBuffer = crypto.randomBytes(keyLength);
17
24
  const encryptor = crypto.createCipheriv(CIPHER, Buffer.from(paddedSecretKey), ivBuffer);
package/package.json CHANGED
@@ -1,20 +1,22 @@
1
1
  {
2
2
  "name": "@synetic/et",
3
- "version": "1.0.3",
3
+ "version": "1.3.0",
4
4
  "description": "The Synetic ET collector for NodeJs",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
+ "dev": "./entrypoint.js --dry-run --reveal",
7
8
  "start": "./entrypoint.js"
8
9
  },
9
10
  "bin": "./entrypoint.js",
10
11
  "author": "Synetic <info@synetic.nl>",
11
12
  "license": "ISC",
12
13
  "engines": {
13
- "node": ">=16",
14
- "npm": ">=8"
14
+ "node": ">=12",
15
+ "npm": ">=6"
15
16
  },
16
17
  "dependencies": {
17
- "axios": "^0.27.2",
18
- "yargs": "^17.5.1"
18
+ "axios": "^1.0.0",
19
+ "ioredis": "^5.9.3",
20
+ "yargs": "^18.0.0"
19
21
  }
20
22
  }