Skip to content

Commit

Permalink
Simplify secret generation introduced in 9e43852
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Apr 30, 2019
1 parent e23cb71 commit 0714ace
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions platform/chromium/vapi-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,7 @@ vAPI.messaging.broadcast = function(message) {

vAPI.warSecret = (function() {
let lastSecretTime = 0;
let lastSecretPtr = 0;

const generateSecret = ( ) => {
lastSecretTime = Date.now();
Expand Down Expand Up @@ -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]}`;
};
})();

Expand Down

0 comments on commit 0714ace

Please sign in to comment.