-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
75 lines (62 loc) · 2.22 KB
/
Copy pathutils.js
File metadata and controls
75 lines (62 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const bcrypt = require('bcrypt');
const saltRounds = 10;
let hashedPassword = process.env.PASS_HASH;
const cliPWArray = process.argv.filter(a=>a.includes('-PW'));
const cliPassword = cliPWArray.length === 0 ? null: cliPWArray[0].replace('-PW=','');
//const bToMb = 1000000;
//const MAX_MEGABYTE_SIZE = (process.env.MAX_TILE_CACHE_SIZE || 500) * bToMb;//mb times 1,000,000 = bytes
const memCache = {}
if(!hashedPassword && !cliPassword ){
console.error('You need to set up a password for this deployment by running the server at least once with the -PW=[password] flag');
process.exit(1);
}
async function createHash(_pass){
const salt = await bcrypt.genSalt(saltRounds);
const hash = await bcrypt.hash(_pass, salt)
return hash
}
if(cliPassword){
if(hashedPassword){
console.error('A password has already been set. Delete the hashed password from the .env and run again');
process.exit(1);
}
createHash(cliPassword).then((hashPass)=>{
hashedPassword = hashPass;
fs.appendFileSync('./.env', `\nPASS_HASH=${hashedPassword}`);
console.log('Password set! Run the script again without the -PW flag');
process.exit(0);
})
}
async function authenticate(_password){
return await bcrypt.compare(_password,hashedPassword);
}
function addTile(_key, _mvt){
memCache[_key] = _mvt;
}
function getCachedTile(_key){
return memCache[_key] || false;
}
function clearPctOfCache(_pct){
const _fract = _pct/100;
let keys = Object.keys(memCache);
let _len = keys.length;
keys
.splice(_len- Math.round(_len*_fract))
.forEach((_key)=>{
if(memCache[_key]){
delete memCache[_key];
}
});
}
function manageLimit(){
const limit = 2000;
const pctPerRemoval = 25;
const numCachedTiles = Object.keys(memCache).length;
if(numCachedTiles > limit){ //TODO: Put limit, and delete amnt in .env!
clearPctOfCache(pctPerRemoval);//TODO: put clear amount in .env
console.log('cleared cache. tiles left: ',Object.keys(memCache).length)
}else{
console.log('cache is ',(numCachedTiles/limit)*100 + ' pct full' )
}
}
module.exports = {addTile, getCachedTile,authenticate,clearPctOfCache,manageLimit }