homegames-common 0.1.3 → 0.1.6

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 +152 -59
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -9,6 +9,7 @@ const unzipper = require('unzipper');
9
9
  const crypto = require('crypto');
10
10
  const { Readable } = require('stream');
11
11
  const os = require('os');
12
+ const process = require('process');
12
13
 
13
14
  const getUserHash = (username) => {
14
15
  return crypto.createHash('md5').update(username).digest('hex');
@@ -111,21 +112,22 @@ const guaranteeDir = (dir) => new Promise((resolve, reject) => {
111
112
  });
112
113
 
113
114
  const guaranteeCertFiles = (dir) => new Promise((resolve, reject) => {
115
+
114
116
  let certPath, keyPath;
115
117
 
116
118
  fs.readdir(dir, (err, files) => {
117
119
  files.forEach(file => {
118
- if (file === 'fullchain.pem') {
120
+ if (file === 'cert.pem') {
119
121
  certPath = path.join(dir, file);
120
122
  }
121
123
 
122
- if (file === 'privkey.pem') {
124
+ if (file === 'key.pem') {
123
125
  keyPath = path.join(dir, file);
124
126
  }
125
127
  });
126
128
 
127
129
  if (!certPath) {
128
- reject('Could not find fullchain.pem');
130
+ reject('Could not find cert.pem');
129
131
  }
130
132
 
131
133
  if (!keyPath) {
@@ -141,19 +143,21 @@ const guaranteeCertFiles = (dir) => new Promise((resolve, reject) => {
141
143
  });
142
144
 
143
145
  const validateCertData = (certPaths, username, accessToken) => new Promise((resolve, reject) => {
144
- postUrl('https://certifier.homegames.link', '/verify', {
145
- checksum: ''
146
- },
147
- {
148
- 'hg-username': username,
149
- 'hg-access-token': accessToken
150
- }).then(data => {
151
- resolve(data);
152
- }).catch(err => {
153
- reject({
154
- message: err.toString()
155
- });
156
- });
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
+ // });
157
161
 
158
162
  });
159
163
 
@@ -224,7 +228,7 @@ const certInit = (certPath, loginPath) => new Promise((resolve, reject) => {
224
228
  validateExistingCerts(certPath).then((certData) => {
225
229
  }).catch(err => {
226
230
  validateLoginData(loginPath).then((loginData) => {
227
- getUrl('https://certifier.homegames.link/get-certs').then(data => {
231
+ getUrl('https://certifier.homegames.io/get-certs').then(data => {
228
232
  resolve(data);
229
233
  }).catch(err => {
230
234
  });
@@ -341,13 +345,15 @@ const getLoginInfo = (authPath) => new Promise((resolve, reject) => {
341
345
 
342
346
  const getCertData = (username, accessToken) => new Promise((resolve, reject) => {
343
347
 
344
- getUrl('https://certifier.homegames.link/get-certs', {
348
+ getUrl('https://certifier.homegames.io/get-cert', {
345
349
 
346
350
  'hg-username': username,
347
- 'hg-access-token': accessToken
351
+ 'hg-token': accessToken
348
352
  }).then(data => {
349
353
  resolve(data);
350
354
  }).catch(err => {
355
+ console.log(err.toString());
356
+ console.log('that was an error');
351
357
  });
352
358
  });
353
359
 
@@ -364,9 +370,10 @@ const bufToStream = (buf) => {
364
370
  const storeCertData = (certBundle, path) => new Promise((resolve, reject) => {
365
371
  const certStream = bufToStream(certBundle);
366
372
 
367
- certStream.pipe(unzipper.Extract({ path }));
373
+ const unzip = unzipper.Extract({ path });
374
+ certStream.pipe(unzip);
368
375
 
369
- resolve();
376
+ unzip.on('close', resolve);
370
377
  });
371
378
 
372
379
  const storeTokens = (path, username, tokens) => new Promise((resolve, reject) => {
@@ -397,11 +404,20 @@ const storeTokens = (path, username, tokens) => new Promise((resolve, reject) =>
397
404
 
398
405
  const guaranteeCerts = (authPath, certPath) => new Promise((resolve, reject) => {
399
406
 
400
- getLoginInfo(authPath).then(info => {
401
- validateExistingCerts(certPath, info.username, info.tokens.accessToken).then(() => {
402
- resolve({
403
- certPath: certPath + '/fullchain.pem',
404
- keyPath: certPath + '/privkey.pem'
407
+ authWorkflow(authPath).then(authInfo => {
408
+ getCertData(authInfo.username, authInfo.tokens.accessToken).then(certData => {
409
+ validateCertData(certPath, authInfo.username, authInfo.tokens.accessToken).then((response) => {
410
+ const data = JSON.parse(response);
411
+ if (data.success) {
412
+ storeCertData(certData, certPath).then(() => {
413
+ resolve({
414
+ certPath: `${certPath}/cert.pem`,
415
+ keyPath: `${certPath}/key.pem`,
416
+ });
417
+ });
418
+ } else {
419
+ reject(data);
420
+ }
405
421
  });
406
422
  }).catch(err => {
407
423
  reject({message: err});
@@ -445,40 +461,61 @@ const promptLogin = () => new Promise((resolve, reject) => {
445
461
  });
446
462
 
447
463
  const lockFile = (path) => new Promise((resolve, reject) => {
448
- const pid = process.getuid();
464
+
449
465
  let _interval;
450
466
 
451
467
  const acquireLock = () => {
452
468
  const lockPath = `${path}.hglock`;
453
469
  fs.exists(lockPath, (exists) => {
454
470
  if (!exists) {
455
- fs.writeFile(lockPath, '' + pid, 'utf-8', () => {
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);
484
+ fs.writeFile(lockPath, 'lock', 'utf-8', () => {
456
485
  clearInterval(_interval);
457
486
  fs.readFile(lockPath, (err, data) => {
458
487
  if (err) {
459
488
  console.log(err);
460
489
  reject(err);
461
- } else if (data.toString() === ''+ pid) {
462
- resolve();
463
490
  } else {
464
- reject('Mismatched PID');
491
+ resolve();
465
492
  }
466
493
  });
467
494
  });
495
+ });
496
+ } else {
497
+ console.log('waiting for lock');
498
+ const { birthtime } = fs.statSync(lockPath);
499
+ const fiveMinsAgo = Date.now() - ( 1000 * 60 * 5 );
500
+ console.log(birthtime);
501
+ if (new Date(birthtime).getTime() < fiveMinsAgo) {
502
+ fs.unlink(lockPath, (err) => {
503
+ console.log(err);
504
+ console.log('deleted');
505
+ });
506
+ }
468
507
  }
469
508
  });
470
509
 
471
510
  };
472
511
 
473
- _interval = setInterval(acquireLock, 50);
512
+ _interval = setInterval(acquireLock, 1000);
474
513
  });
475
514
 
476
515
  const unlockFile = (path) => new Promise((resolve, reject) => {
477
- const pid = process.getuid();
478
516
  const lockPath = `${path}.hglock`;
479
517
 
480
518
  fs.readFile(lockPath, (err, data) => {
481
- if (data.toString() === '' + pid) {
482
519
  fs.unlink(lockPath, (err) => {
483
520
  if (!err) {
484
521
  resolve();
@@ -486,9 +523,6 @@ const unlockFile = (path) => new Promise((resolve, reject) => {
486
523
  reject('Could not delete lock');
487
524
  }
488
525
  });
489
- } else {
490
- reject('Tried to remove lock that wasnt ours');
491
- }
492
526
  });
493
527
  });
494
528
 
@@ -497,36 +531,45 @@ const authWorkflow = (authPath) => new Promise((resolve, reject) => {
497
531
  reject(`No authPath provided`);
498
532
  }
499
533
 
534
+ const _doLogin = () => {
535
+ promptLogin().then((loginInfo) => {
536
+ login(loginInfo.username, loginInfo.password).then(tokens => {
537
+ storeTokens(authPath, loginInfo.username, tokens).then(() => {
538
+ verifyAccessToken(loginInfo.username, tokens.accessToken).then(() => {
539
+ unlockFile(authPath).then(() => {
540
+ resolve({
541
+ username: loginInfo.username,
542
+ tokens
543
+ });
544
+ });
545
+
546
+ });
547
+ }).catch(err => {
548
+ console.log('Failed to store auth tokens');
549
+ reject(err);
550
+ });
551
+ }).catch(err => {
552
+ console.log('Failed to login');
553
+ reject(err);
554
+ });
555
+ });
556
+ };
557
+
558
+ console.log('about to lock ' + authPath);
500
559
  lockFile(authPath).then(() => {
501
560
  getLoginInfo(authPath).then((loginInfo) => {
502
561
  verifyAccessToken(loginInfo.username, loginInfo.tokens.accessToken).then(() => {
503
562
  unlockFile(authPath).then(() => {
504
563
  resolve(loginInfo);
505
564
  });
565
+ }).catch(err => {
566
+ console.log('failed to verify access token');
567
+ _doLogin();
506
568
  });
507
569
  }).catch((err) => {
570
+ console.log(err);
508
571
  if (err.type === 'DATA_NOT_FOUND') {
509
- promptLogin().then((loginInfo) => {
510
- login(loginInfo.username, loginInfo.password).then(tokens => {
511
- storeTokens(authPath, loginInfo.username, tokens).then(() => {
512
- verifyAccessToken(loginInfo.username, tokens.accessToken).then(() => {
513
- unlockFile(authPath).then(() => {
514
- resolve({
515
- username: loginInfo.username,
516
- tokens
517
- });
518
- });
519
-
520
- });
521
- }).catch(err => {
522
- console.log('Failed to store auth tokens');
523
- reject(err);
524
- });
525
- }).catch(err => {
526
- console.log('Failed to login');
527
- reject(err);
528
- });
529
- });
572
+ _doLogin();
530
573
  }
531
574
  });
532
575
  }).catch(err => {
@@ -535,6 +578,53 @@ const authWorkflow = (authPath) => new Promise((resolve, reject) => {
535
578
  });
536
579
  });
537
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
+ try {
595
+ if (config[key] === undefined) {
596
+ throw new Error(`No value for ${key} found in config`);
597
+ }
598
+ console.log(`Found value ${config[key]} in config`);
599
+ return config[key];
600
+ } catch(err) {
601
+ console.log(err);
602
+ if (_default === undefined) {
603
+ throw new Error(`No config value found for ${key}`);
604
+ } else {
605
+ console.log(`Could not find config for key ${key}. Using default: ${_default}`);
606
+ return _default;
607
+ }
608
+ }
609
+ };
610
+
611
+
612
+ const getConfig = () => {
613
+
614
+ const options = [process.cwd(), require.main.filename, process.mainModule.filename, __dirname]
615
+ let baseDir = process.cwd();
616
+ let _config = {};
617
+
618
+ for (let i = 0; i < options.length; i++) {
619
+ if (fs.existsSync(`${options[i]}/config.js`)) {//src/util/config.js`)) {
620
+ console.log(`Using config at ${options[i]}`);
621
+ _config = require(`${options[i]}/config.js`);
622
+ }
623
+ }
624
+
625
+ return _config;
626
+ }
627
+
538
628
 
539
629
  module.exports = {
540
630
  guaranteeCerts,
@@ -553,5 +643,8 @@ module.exports = {
553
643
  promptLogin,
554
644
  getUserHash,
555
645
  authWorkflow,
556
- guaranteeDir
646
+ guaranteeDir,
647
+ getUrl,
648
+ getConfigValue
557
649
  };
650
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homegames-common",
3
- "version": "0.1.3",
3
+ "version": "0.1.6",
4
4
  "description": "Homegames common tools",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -19,6 +19,6 @@
19
19
  "dependencies": {
20
20
  "aws-sdk": "^2.809.0",
21
21
  "unzipper": "^0.10.11",
22
- "ws": "^7.4.1"
22
+ "ws": "^7.5.4"
23
23
  }
24
24
  }