• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

suculent / thinx-device-api / #252645633

21 Aug 2023 12:53PM UTC coverage: 1.776%. First build
#252645633

push

suculent
Merge branch 'main' of github.com:suculent/thinx-device-api

* 'main' of github.com:suculent/thinx-device-api: (127 commits)
  all tests passing; additional messenger fix; ready to merge
  version bump, removal of legacy redis client copy; spec fix
  from 3 failures; making sure Redis.expire will be called after Redis.set, all errors were caused by /device/firmware –> Redis refactoring fix
  from 1 failure (spec only, build log not fetched... may not exist in this suite); re-enabling all integration suites to target remaining issues
  from 25 failures; switching back to legacyMode in global options for specs
  from 28 failures, fixing missed Redis implementations
  from 101 failures; using v4 API for remaining Redis clients
  from 116 failures, switching to v4 redis interface
  using Redis client in legacy mode for redis-session
  with 198 failures; disabling non-essential integration tests (Redis-JWT seems to fail, maybe also async test spec inits)
  unit specs passed, re-enabling full integration test suite
  from 1 issue with failing notifier (test will have to be disabled for integration)
  adding ssh-agent start
  from 4 failures; notifier edge cases with invalid data, git fails to work with askpass -- needs in-container test
  from 4 failures; four fixes
  from 3 failures; proper notifier validation fix
  non-functional fixes
  from 4 failures, notifier fix
  apienv ref fixes; from 204 failures; disabling integration-tests causing about 200 of them
  from 4 (recurring) failures (Notifier undefined outfile, 3x sources); adding Integration tests for better issue targeting
  ...

# Conflicts:
#	package-lock.json
#	services/console

2 of 513 branches covered (0.0%)

Branch coverage included in aggregate %.

2 of 305 new or added lines in 4 files covered. (0.66%)

27 of 1120 relevant lines covered (2.41%)

0.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

4.95
/lib/thinx/globals.js
1
// Prefix equals static globals
2

3
var Globals = (function () {
1✔
4

5
  var Rollbar = require("rollbar");
1✔
6
  var fs = require("fs-extra");
1✔
7
  var crypto = require("crypto");
1✔
8

9
  let CONFIG_ROOT = "/mnt/data/conf";
1✔
10

11
  if (process.env.ENVIRONMENT == "development") {
1!
12
    CONFIG_ROOT = __dirname + "/../../spec/mnt/data/conf";
×
13
  }
14

15
  var CONFIG_PATH = CONFIG_ROOT + "/config.json";
1✔
16

17
  if (!fs.existsSync(CONFIG_PATH)) {
1!
18
    throw new Error(`Config not found in ${CONFIG_PATH}`);
1✔
19
  }
20

21
  // all cached
22
  var _prefix = "";
×
23
  var _app_config = null;
×
24
  var _github_ocfg = null;
×
25
  var _google_ocfg = null;
×
26
  var _rollbar = null;
×
27

28
  const redis_reconnect_strategy = function (options) {
×
29

30
    var max_attempts = 10;
×
31
    var retry_time = 5 * 60 * 1000;
×
32

33
    // for test environment, limits are much shorter.
34
    if (typeof (process.env.CIRCLE_USERNAME) !== "undefined") {
×
35
      max_attempts = 5;
×
36
      retry_time = 1000 * 60;
×
37
    }
38

39
    if (options.error) {
×
40
      if (options.error.code === 'ECONNREFUSED') {
×
41
        // End reconnecting on a specific error and flush all commands with a individual error
42
        return new Error('The server refused the connection');
×
43
      }
44
      if (options.error.code === 'ECONNRESET') {
×
45
        return new Error('The server reset the connection');
×
46
      }
47
      if (options.error.code === 'ETIMEDOUT') {
×
48
        return new Error('The server timeouted the connection');
×
49
      }
50
    }
51
    if (options.total_retry_time > retry_time) {
×
52
      // End reconnecting after a specific timeout and flush all commands with a individual error
53
      return new Error('Retry time exhausted');
×
54
    }
55
    if (options.attempt > max_attempts) {
×
56
      // End reconnecting with built in error
57
      return new Error('Retry attempts ended');
×
58
    }
59
    // reconnect after
60
    return 1000;
×
61
  };
62

63
  var _public = {
×
64

65
    redis_options: function () {
66

67
      if ((typeof (_app_config) === "undefined") || (_app_config === null)) {
×
68
        console.log("CRITICAL CONFIGURATION ERROR: missing conf/config.json");
×
69
        return retval;
×
70
      }
71

72
      if ((typeof (_app_config.redis) === "undefined") || (_app_config.redis === null)) {
×
73
        console.log("CRITICAL CONFIGURATION ERROR: `redis` block missing in conf/config.json");
×
74
        return retval;
×
75
      }
76

77
      let config = _app_config.redis;
×
78
      config.retry_strategy = redis_reconnect_strategy;
×
79

80
      // Take the Redis password from environment variable to make sure it's in sync
81
      if (typeof (process.env.REDIS_PASSWORD) !== "undefined") {
×
82
        config.password = process.env.REDIS_PASSWORD;
×
83
      }
84

NEW
85
      config.legacyMode = true;
×
86

87
      return config;
×
88
    },
89

90
    load: function () {
91

92
      if (fs.existsSync(CONFIG_ROOT + '/google-oauth.json')) {
×
93
        _google_ocfg = require(CONFIG_ROOT + '/google-oauth.json');
×
94
      }
95

96
      if (fs.existsSync(CONFIG_ROOT + '/github-oauth.json')) {
×
97
        _github_ocfg = require(CONFIG_ROOT + '/github-oauth.json');
×
98
      }
99

100
      if (fs.existsSync(CONFIG_ROOT + '/config.json')) {
×
NEW
101
        let path = CONFIG_ROOT + '/config.json';
×
NEW
102
        console.log("Config path being used:", path);
×
NEW
103
        _app_config = require(path);
×
104
      }
105

106
      if ((typeof (process.env.ROLLBAR_ACCESS_TOKEN) !== "undefined") && (process.env.ROLLBAR_ACCESS_TOKEN !== null)) {
×
107
        _rollbar = new Rollbar({
×
108
          accessToken: process.env.ROLLBAR_ACCESS_TOKEN,
109
          handleUncaughtExceptions: true,
110
          handleUnhandledRejections: true,
111
          revision: process.env.REVISION || "latest"
×
112
        });
113
      }
114

115
      _public.load_or_create_prefix();
×
116

117
    },
118

119
    load_or_create_prefix: function () {
120
      var pfx_path = CONFIG_ROOT + '/.thx_prefix'; // old
×
121
      if (!fs.existsSync(pfx_path)) {
×
122
        console.log("[globals.js] Prefix file not found at (1)", pfx_path);
×
123
        pfx_path = _app_config.data_root + '/conf/.thx_prefix'; // new
×
124
        if (!fs.existsSync(pfx_path)) {
×
125
          console.log("[globals.js] Prefix file not found at (2)", pfx_path);
×
126
          _prefix = null;
×
127
        }
128
      }
129
      if (fs.existsSync(pfx_path)) {
×
130
        _prefix = (fs.readFileSync(pfx_path).toString()).replace("\n", "");
×
131
        return;
×
132
      }
133
      console.log("[globals.js] Prefix file not found...");
×
134
      _public.save_new_prefix(pfx_path);
×
135
    },
136

137
    save_new_prefix: function (pfx_path) {
138
      fs.ensureFile(pfx_path, function (e) {
×
139
        if (e) {
×
140
          console.log(`☣️ [error] creating thx_prefix: ${e}`);
×
141
          return;
×
142
        }
143
        crypto.randomBytes(12, function (_cerr, buffer) {
×
144
          _prefix = buffer.toString('hex');
×
145
          fs.writeFile(pfx_path, _prefix, "", function (werr) {
×
146
            if (werr) {
×
147
              console.log(`☣️ [error] writing thx_prefix: ${werr}`);
×
148
            } else {
149
              console.log(`ℹ️ [info] [globals.js] Created new prefix ${_prefix}`);
×
150
            }
151
          });
152
        });
153
      });
154
    },
155

156
    generate_prefix: function () {
157
      let pfx_path = _app_config.data_root + '/conf/.thx_prefix';
×
158
      console.log("Generating prefix at " + pfx_path);
×
159
      fs.ensureFile(pfx_path, function (e) {
×
160
        if (e) {
×
161
          console.log("error creating thx_prefix: " + e);
×
162
          return null;
×
163
        } else {
164
          crypto.randomBytes(12, function (_cerr, buffer) {
×
165
            var prefix = buffer.toString('hex');
×
166
            fs.writeFile(prefix, "", function (werr) {
×
167
              if (werr) {
×
168
                console.log("error writing thx_prefix: " + werr);
×
169
                return null;
×
170
              } else {
171
                console.log("Returning new prefix: " + _prefix);
×
172
                return _prefix;
×
173
              }
174
            });
175
          });
176
        }
177
      });
178
    },
179

180
    prefix: function () {
181

182
      if (_prefix === null) {
×
183
        console.log("prefix:_prefix==null; loading");
×
184
        _public.load();
×
185
      } else {
186
        return _prefix;
×
187
      }
188

189
      var pfx_path;
190
      pfx_path = CONFIG_ROOT + '/.thx_prefix';
×
191
      if (!fs.existsSync(pfx_path)) {
×
192
        pfx_path = _app_config.data_root + '/conf/.thx_prefix';
×
193
        if (!fs.existsSync(pfx_path)) {
×
194
          _prefix = null;
×
195
        }
196
      }
197

198
      try {
×
199
        console.log("Re-loading " + pfx_path);
×
200
        if (fs.existsSync(pfx_path)) {
×
201
          console.log("xists");
×
202
          _prefix = (fs.readFileSync(pfx_path).toString()).replace("\n", "");
×
203
          return _prefix; // allow empty prefix as well
×
204
        } else {
205
          console.log("does not exist " + pfx_path);
×
206
          _prefix = null;
×
207
        }
208
      } catch (e) {
209
        // create .thx_prefix with random key on first run!
210
        console.log("[globals] thx_prefix_exception (2) skipped " + e);
×
211
        _prefix = _public.generate_prefix(); // may cause loop, investigate...
×
212
      }
213

214
      if (_prefix === null) {
×
215
        console.log("No prefix found(!), generating...");
×
216
        _prefix = _public.generate_prefix();
×
217
      } else {
218
        console.log("Expected empty prefix: " + _prefix);
×
219
      }
220

221
      console.log("Globals returning prefix '" + _prefix + "'");
×
222
      return _prefix;
×
223
    },
224

225
    app_config: function () {
226

227
      if (_app_config === null) {
×
228
        console.log("re-ladung publik konfigurazion");
×
229
        _public.load();
×
230
      }
231

232
      if (_app_config === null) {
×
233
        console.log("_public.load still failed, hardkernfixoperazione...");
×
234
        _app_config = require(CONFIG_ROOT + '/config.json'); // not compatible with test!
×
235
      }
236

237
      if (process.env.ENVIRONMENT == "development") {
×
238
        _app_config.ssh_keys = process.env.HOME + "/.ssh";
×
239
      }
240

241
      return _app_config;
×
242
    },
243

244
    google_ocfg: function () {
245
      if (_prefix === null) {
×
246
        _public.load();
×
247
      }
248
      return _google_ocfg;
×
249
    },
250

251
    github_ocfg: function () {
252
      if (_prefix === null) {
×
253
        _public.load();
×
254
      }
255
      return _github_ocfg;
×
256
    },
257

258
    rollbar: function () {
259
      if (_prefix === null) {
×
260
        _public.load();
×
261
      }
262
      return _rollbar;
×
263
    }
264

265
  };
266

267
  _public.load();
×
268

269
  return _public;
×
270

271
})();
272

273
exports.prefix = Globals.prefix;
×
274
exports.save_new_prefix = Globals.save_new_prefix;
×
275
exports.load_or_create_prefix = Globals.load_or_create_prefix;
×
276
exports.app_config = Globals.app_config;
×
277
exports.google_ocfg = Globals.google_ocfg;
×
278
exports.github_ocfg = Globals.github_ocfg;
×
279
exports.rollbar = Globals.rollbar;
×
280
exports.redis_options = Globals.redis_options;
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc