homegames-common 0.1.35 → 0.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 +169 -37
- 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 === '
|
|
120
|
+
if (file === 'cert.pem') {
|
|
121
121
|
certPath = path.join(dir, file);
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
-
if (file === '
|
|
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
|
|
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
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}).
|
|
155
|
-
|
|
156
|
-
|
|
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.
|
|
231
|
+
getUrl('https://certifier.homegames.io/get-certs').then(data => {
|
|
230
232
|
resolve(data);
|
|
231
233
|
}).catch(err => {
|
|
232
234
|
});
|
|
@@ -342,15 +344,16 @@ const getLoginInfo = (authPath) => new Promise((resolve, reject) => {
|
|
|
342
344
|
});
|
|
343
345
|
|
|
344
346
|
const getCertData = (username, accessToken) => new Promise((resolve, reject) => {
|
|
345
|
-
|
|
347
|
+
|
|
348
|
+
getUrl('https://certifier.homegames.io/get-cert', {
|
|
346
349
|
|
|
347
350
|
'hg-username': username,
|
|
348
351
|
'hg-token': accessToken
|
|
349
352
|
}).then(data => {
|
|
350
353
|
resolve(data);
|
|
351
354
|
}).catch(err => {
|
|
352
|
-
|
|
353
|
-
|
|
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
|
-
|
|
407
|
-
|
|
408
|
-
|
|
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}/
|
|
412
|
-
keyPath: `${certPath}/
|
|
414
|
+
certPath: `${certPath}/cert.pem`,
|
|
415
|
+
keyPath: `${certPath}/key.pem`,
|
|
413
416
|
});
|
|
414
417
|
});
|
|
415
|
-
|
|
416
|
-
|
|
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,23 +578,136 @@ 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 _config = {};
|
|
613
|
+
|
|
614
|
+
for (let i = 0; i < options.length; i++) {
|
|
615
|
+
if (fs.existsSync(`${options[i]}/config.json`)) {
|
|
616
|
+
console.log(`Using config at ${options[i]}`);
|
|
617
|
+
_config = JSON.parse(fs.readFileSync(`${options[i]}/config.json`));
|
|
618
|
+
break;
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
cachedConfig = _config;
|
|
623
|
+
|
|
624
|
+
return _config;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
const getLogLevel = (logLevel = null) => {
|
|
629
|
+
const _logLevel = logLevel || getConfigValue('LOG_LEVEL', 'INFO');
|
|
630
|
+
const levelList = ['DISABLED', 'INFO', 'DEBUG'];
|
|
631
|
+
|
|
632
|
+
return levelList.indexOf(_logLevel);
|
|
633
|
+
};
|
|
634
|
+
|
|
635
|
+
const msgToString = (msg) => {
|
|
636
|
+
return typeof msg === 'object' ? JSON.stringify(msg) : msg;
|
|
637
|
+
};
|
|
638
|
+
|
|
639
|
+
const log = {
|
|
640
|
+
info: (msg, explanation = null) => {
|
|
641
|
+
const logLevel = getLogLevel();
|
|
642
|
+
const required = getLogLevel('INFO');
|
|
643
|
+
|
|
644
|
+
if (logLevel < required) {
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
const logPath = getConfigValue('LOG_PATH');
|
|
649
|
+
|
|
650
|
+
const msgString = `[HOMEGAMES-INFO][${new Date().toTimeString()}] ${msgToString(msg)}${explanation ? ':' + os.EOL + msgToString(explanation) : ''}${os.EOL}${os.EOL}`;
|
|
651
|
+
fs.appendFile(logPath, msgString, (err) => {
|
|
652
|
+
if (err) {
|
|
653
|
+
console.error('failed log');
|
|
654
|
+
console.log(err);
|
|
655
|
+
}
|
|
656
|
+
});
|
|
657
|
+
},
|
|
658
|
+
error: (msg, explanation) => {
|
|
659
|
+
const logLevel = getLogLevel();
|
|
660
|
+
const required = getLogLevel('INFO');
|
|
661
|
+
|
|
662
|
+
if (logLevel < required) {
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
const logPath = getConfigValue('LOG_PATH');
|
|
667
|
+
|
|
668
|
+
const msgString = `[HOMEGAMES-ERROR][${new Date().toTimeString()}] ${msgToString(msg)}${explanation ? ':' + os.EOL + msgToString(explanation) : ''}${os.EOL}${os.EOL}`;
|
|
669
|
+
fs.appendFile(logPath, msgString, (err) => {
|
|
670
|
+
if (err) {
|
|
671
|
+
console.error('failed log');
|
|
672
|
+
console.log(err);
|
|
673
|
+
}
|
|
674
|
+
});
|
|
675
|
+
},
|
|
676
|
+
debug: (msg, explanation) => {
|
|
677
|
+
const logLevel = getLogLevel();
|
|
678
|
+
const required = getLogLevel('DEBUG');
|
|
679
|
+
const logPath = getConfigValue('LOG_PATH');
|
|
680
|
+
|
|
681
|
+
if (logLevel < required) {
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
const msgString = `[HOMEGAMES-DEBUG][${new Date().toTimeString()}] ${msgToString(msg)}${explanation ? ':' + os.EOL + msgToString(explanation) : ''}${os.EOL}${os.EOL}`;
|
|
686
|
+
fs.appendFile(logPath, msgString, (err) => {
|
|
687
|
+
if (err) {
|
|
688
|
+
console.error('failed log');
|
|
689
|
+
console.log(err);
|
|
690
|
+
}
|
|
691
|
+
});
|
|
692
|
+
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
}
|
|
696
|
+
|
|
562
697
|
|
|
563
698
|
module.exports = {
|
|
564
|
-
guaranteeCerts,
|
|
565
|
-
certInit,
|
|
566
|
-
getCertData,
|
|
567
699
|
signup,
|
|
568
700
|
login,
|
|
569
701
|
confirmUser,
|
|
570
|
-
validateExistingCerts,
|
|
571
702
|
getLoginInfo,
|
|
572
703
|
verifyAccessToken,
|
|
573
704
|
refreshAccessToken,
|
|
574
|
-
storeCertData,
|
|
575
705
|
linkInit,
|
|
576
|
-
storeTokens,
|
|
577
|
-
promptLogin,
|
|
578
706
|
getUserHash,
|
|
579
707
|
authWorkflow,
|
|
580
|
-
guaranteeDir
|
|
708
|
+
guaranteeDir,
|
|
709
|
+
getUrl,
|
|
710
|
+
getConfigValue,
|
|
711
|
+
log
|
|
581
712
|
};
|
|
713
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "homegames-common",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.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.
|
|
20
|
+
"aws-sdk": "^2.1151.0",
|
|
21
21
|
"unzipper": "^0.10.11",
|
|
22
|
-
"ws": "^7.4
|
|
22
|
+
"ws": "^7.5.4"
|
|
23
23
|
}
|
|
24
24
|
}
|