Simplify secret generation introduced in 9e4385243c

This commit is contained in:
Raymond Hill 2019-04-30 15:19:10 -04:00
parent e23cb71b1a
commit 0714aceaaf
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 7 additions and 8 deletions

View File

@ -1044,6 +1044,7 @@ vAPI.messaging.broadcast = function(message) {
vAPI.warSecret = (function() {
let lastSecretTime = 0;
let lastSecretPtr = 0;
const generateSecret = ( ) => {
lastSecretTime = Date.now();
@ -1073,15 +1074,13 @@ vAPI.warSecret = (function() {
);
return ( ) => {
const n = Math.min(
Math.floor((Date.now() - lastSecretTime) / 1000),
secrets.length
);
for ( let i = 0; i < n; i++ ) {
secrets.pop();
secrets.unshift(generateSecret());
const now = Date.now();
if ( (now - lastSecretTime) >= 1000 ) {
lastSecretPtr = (lastSecretPtr + 1) % secrets.length;
secrets[lastSecretPtr] = generateSecret();
lastSecretTime = now;
}
return `?secret=${secrets[0]}`;
return `?secret=${secrets[lastSecretPtr]}`;
};
})();