homegames-common 0.1.36 → 0.2.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.
Files changed (2) hide show
  1. package/index.js +100 -29
  2. package/package.json +3 -3
package/index.js CHANGED
@@ -117,17 +117,17 @@ const guaranteeCertFiles = (dir) => new Promise((resolve, reject) => {
117
117
 
118
118
  fs.readdir(dir, (err, files) => {
119
119
  files.forEach(file => {
120
- if (file === 'fullchain.pem') {
120
+ if (file === 'cert.pem') {
121
121
  certPath = path.join(dir, file);
122
122
  }
123
123
 
124
- if (file === 'privkey.pem') {
124
+ if (file === 'key.pem') {
125
125
  keyPath = path.join(dir, file);
126
126
  }
127
127
  });
128
128
 
129
129
  if (!certPath) {
130
- reject('Could not find fullchain.pem');
130
+ reject('Could not find cert.pem');
131
131
  }
132
132
 
133
133
  if (!keyPath) {
@@ -143,19 +143,21 @@ const guaranteeCertFiles = (dir) => new Promise((resolve, reject) => {
143
143
  });
144
144
 
145
145
  const validateCertData = (certPaths, username, accessToken) => new Promise((resolve, reject) => {
146
- postUrl('https://certifier.homegames.link', '/verify', {
147
- checksum: ''
148
- },
149
- {
150
- 'hg-username': username,
151
- 'hg-access-token': accessToken
152
- }).then(data => {
153
- resolve(data);
154
- }).catch(err => {
155
- reject({
156
- message: err.toString()
157
- });
158
- });
146
+ // one day
147
+ resolve(JSON.stringify({success: true}));
148
+ // postUrl('https://certifier.homegames.io', '/verify', {
149
+ // checksum: ''
150
+ // },
151
+ // {
152
+ // 'hg-username': username,
153
+ // 'hg-access-token': accessToken
154
+ // }).then(data => {
155
+ // resolve(data);
156
+ // }).catch(err => {
157
+ // reject({
158
+ // message: err.toString()
159
+ // });
160
+ // });
159
161
 
160
162
  });
161
163
 
@@ -226,7 +228,7 @@ const certInit = (certPath, loginPath) => new Promise((resolve, reject) => {
226
228
  validateExistingCerts(certPath).then((certData) => {
227
229
  }).catch(err => {
228
230
  validateLoginData(loginPath).then((loginData) => {
229
- getUrl('https://certifier.homegames.link/get-certs').then(data => {
231
+ getUrl('https://certifier.homegames.io/get-certs').then(data => {
230
232
  resolve(data);
231
233
  }).catch(err => {
232
234
  });
@@ -342,6 +344,7 @@ const getLoginInfo = (authPath) => new Promise((resolve, reject) => {
342
344
  });
343
345
 
344
346
  const getCertData = (username, accessToken) => new Promise((resolve, reject) => {
347
+
345
348
  getUrl('https://certifier.homegames.io/get-cert', {
346
349
 
347
350
  'hg-username': username,
@@ -349,8 +352,8 @@ const getCertData = (username, accessToken) => new Promise((resolve, reject) =>
349
352
  }).then(data => {
350
353
  resolve(data);
351
354
  }).catch(err => {
352
- console.log(err);
353
- reject();
355
+ console.log(err.toString());
356
+ console.log('that was an error');
354
357
  });
355
358
  });
356
359
 
@@ -403,19 +406,19 @@ const guaranteeCerts = (authPath, certPath) => new Promise((resolve, reject) =>
403
406
 
404
407
  authWorkflow(authPath).then(authInfo => {
405
408
  getCertData(authInfo.username, authInfo.tokens.accessToken).then(certData => {
406
- //validateCertData(certPath, authInfo.username, authInfo.tokens.accessToken).then((response) => {
407
- // const data = JSON.parse(response);
408
- // if (data.success) {
409
+ validateCertData(certPath, authInfo.username, authInfo.tokens.accessToken).then((response) => {
410
+ const data = JSON.parse(response);
411
+ if (data.success) {
409
412
  storeCertData(certData, certPath).then(() => {
410
413
  resolve({
411
- certPath: `${certPath}/fullchain.pem`,
412
- keyPath: `${certPath}/privkey.pem`,
414
+ certPath: `${certPath}/cert.pem`,
415
+ keyPath: `${certPath}/key.pem`,
413
416
  });
414
417
  });
415
- // } else {
416
- // reject(data);
417
- // }
418
- //});
418
+ } else {
419
+ reject(data);
420
+ }
421
+ });
419
422
  }).catch(err => {
420
423
  reject({message: err});
421
424
  });
@@ -465,6 +468,19 @@ const lockFile = (path) => new Promise((resolve, reject) => {
465
468
  const lockPath = `${path}.hglock`;
466
469
  fs.exists(lockPath, (exists) => {
467
470
  if (!exists) {
471
+ const pathPieces = path.split('/');
472
+ let pathParent = [];
473
+ for (let x in pathPieces) {
474
+ if (x == pathPieces.length - 1) {
475
+ break
476
+ } else {
477
+ pathParent.push(pathPieces[x]);
478
+ }
479
+ }
480
+
481
+ guaranteeDir(pathParent.join('/')).then(() => {
482
+ console.log("writing lock file");
483
+ console.log(lockPath);
468
484
  fs.writeFile(lockPath, 'lock', 'utf-8', () => {
469
485
  clearInterval(_interval);
470
486
  fs.readFile(lockPath, (err, data) => {
@@ -476,7 +492,9 @@ const lockFile = (path) => new Promise((resolve, reject) => {
476
492
  }
477
493
  });
478
494
  });
495
+ });
479
496
  } else {
497
+ console.log('waiting for lock');
480
498
  const { birthtime } = fs.statSync(lockPath);
481
499
  const fiveMinsAgo = Date.now() - ( 1000 * 60 * 5 );
482
500
  console.log(birthtime);
@@ -537,6 +555,7 @@ const authWorkflow = (authPath) => new Promise((resolve, reject) => {
537
555
  });
538
556
  };
539
557
 
558
+ console.log('about to lock ' + authPath);
540
559
  lockFile(authPath).then(() => {
541
560
  getLoginInfo(authPath).then((loginInfo) => {
542
561
  verifyAccessToken(loginInfo.username, loginInfo.tokens.accessToken).then(() => {
@@ -559,6 +578,55 @@ const authWorkflow = (authPath) => new Promise((resolve, reject) => {
559
578
  });
560
579
  });
561
580
 
581
+ const getConfigValue = (key, _default = undefined) => {
582
+ const config = getConfig();
583
+
584
+ let envValue = process.env[key] && `${process.env[key]}`;
585
+ if (envValue !== undefined) {
586
+ if (envValue === 'true') {
587
+ envValue = true;
588
+ } else if (envValue === 'false') {
589
+ envValue = false;
590
+ }
591
+ console.log(`Using environment value: ${envValue} for key: ${key}`);
592
+ return envValue;
593
+ }
594
+ if (config[key] === undefined && _default === undefined) {
595
+ throw new Error(`No value for ${key} found in config`);
596
+ } else if (config[key] === undefined && _default !== undefined) {
597
+ return _default;
598
+ }
599
+ console.log(`Found value ${config[key]} in config`);
600
+ return config[key];
601
+ };
602
+
603
+ let cachedConfig = {};
604
+
605
+ const getConfig = () => {
606
+
607
+ if (Object.keys(cachedConfig).length > 0) {
608
+ return cachedConfig;
609
+ }
610
+
611
+ const options = [process.cwd(), require.main.filename, process.mainModule.filename, __dirname]
612
+ let baseDir = process.cwd();
613
+ let _config = {};
614
+
615
+ for (let i = 0; i < options.length; i++) {
616
+ if (fs.existsSync(`${options[i]}/config.json`)) {
617
+ console.log(`Using config at ${options[i]}`);
618
+ _config = JSON.parse(fs.readFileSync(`${options[i]}/config.json`));
619
+ }
620
+ }
621
+
622
+ console.log('using this config data');
623
+ console.log(_config);
624
+
625
+ cachedConfig = _config;
626
+
627
+ return _config;
628
+ }
629
+
562
630
 
563
631
  module.exports = {
564
632
  guaranteeCerts,
@@ -577,5 +645,8 @@ module.exports = {
577
645
  promptLogin,
578
646
  getUserHash,
579
647
  authWorkflow,
580
- guaranteeDir
648
+ guaranteeDir,
649
+ getUrl,
650
+ getConfigValue
581
651
  };
652
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homegames-common",
3
- "version": "0.1.36",
3
+ "version": "0.2.0",
4
4
  "description": "Homegames common tools",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -17,8 +17,8 @@
17
17
  },
18
18
  "homepage": "https://github.com/homegamesio/homegames-common#readme",
19
19
  "dependencies": {
20
- "aws-sdk": "^2.809.0",
20
+ "aws-sdk": "^2.1151.0",
21
21
  "unzipper": "^0.10.11",
22
- "ws": "^7.4.1"
22
+ "ws": "^7.5.4"
23
23
  }
24
24
  }