keyro - key rotation utility
What is this
Keyro is just a simple utility to store an array of values and return the 'next one' on every 'get' access.
I have done it to mitigate the rate limit overload on some services, so a different key is used on every request, but as it is something very generic I guess it can be useful on other situations.
Requirements
This is a npm package, and you will need a node version which supports ES6. Anyway, the code is really simple, and could be reduced to drop node dependencies so it can be used also on browsers:
{ if !Array throw "pool has to be an array"; thispointer = 0; thispool = pool; } { thispool = ...thispool key; } { const value = thispoolthispointer; const hasReachTheEnd = thispointer >= thispoollength - 1; const nextPointerValue = hasReachTheEnd ? 0 : thispointer + 1; thispointer = nextPointerValue; return value; }
Installation
npm i --save keyro
Usage
const Keyro = ; const keysFromConfig = "SDF9DF897SFGD98A7SDF" "VB8N8SC7G68DF8B76S4D" "OOLI4L2L1U5I3HJU5K15"; const keyro = pool: keysFromConfig ; { return keyro;} console; // "SDF9DF897SFGD98A7SDF"console; // "VB8N8SC7G68DF8B76S4D"console; // "OOLI4L2L1U5I3HJU5K15"console; // "SDF9DF897SFGD98A7SDF"console; // "VB8N8SC7G68DF8B76S4D"console; // "OOLI4L2L1U5I3HJU5K15"