UNPKG

3.65 kBJavaScriptView Raw
1/**
2 * @type {CredentialDB}
3 * @property {Array<Object>} credentials - array of credentials
4 */
5class CredentialDB {
6
7 constructor() {
8 this.credentials = [];
9 }
10
11 /**
12 * Create credentials pool
13 * @param {Object} creds
14 * @param {string} pool - name of pool
15 */
16 createPool(creds, pool) {
17 const FREE_USER_TIMEOUT = 10 * 60 * 1000;
18
19 const credentials = creds.map(item => {
20 item.isLocked = false;
21 item.freeTimeout = function() {
22 setTimeout(() => {
23 this.isLocked = false;
24 }, FREE_USER_TIMEOUT)
25 };
26 return item
27 });
28
29 if (pool) {
30 this[pool] = credentials;
31 } else {
32 this.credentials = credentials;
33 }
34 }
35
36 /**
37 * Get free credential
38 * @param {string} pool - name of pool
39 * @return {Object}
40 * @throws {Error}
41 */
42 getCredentials(pool) {
43 let poolRef = this.credentials;
44 if (pool) {
45 if (!this[pool]) throw new Error("Pool " + pool + "doesn't exist");
46 poolRef = this[pool];
47 }
48 const freeUserIndex = poolRef.findIndex(item => item.isLocked === false);
49
50 if (freeUserIndex !== -1) {
51 poolRef[freeUserIndex].isLocked = true;
52 poolRef[freeUserIndex].freeTimeout();
53 return poolRef[freeUserIndex];
54 } else {
55 throw new Error("There are no free users");
56 }
57 }
58
59 /**
60 * Free credentials by username
61 * @param {string} pool - name of pool
62 * @param {string} username - username to free
63 */
64 freeCredentials(username, pool) {
65 let poolRef = this.credentials;
66 if (pool) {
67 if (!this[pool]) throw new Error("Pool " + pool + "doesn't exist");
68 poolRef = this[pool];
69 }
70 const userIndex = poolRef.findIndex(item => item.username === username);
71
72 if (userIndex !== -1) {
73 poolRef[userIndex].isLocked = false;
74 }
75 }
76
77 /**
78 * Update property
79 * @param {string} username - username to update
80 * @param {string} property - property to update
81 * @param {any} value - value of property
82 * @param {string} pool - name of pool
83 * @throws {Error}
84 */
85 updateProperty(username, property, value, pool) {
86 let poolRef = this.credentials;
87 if (pool) {
88 if (!this[pool]) throw new Error("Pool " + pool + "doesn't exist");
89 poolRef = this[pool];
90 }
91 const userIndex = poolRef.findIndex(item => item.username === username);
92
93 if (userIndex !== -1) {
94 poolRef[userIndex][property] = value;
95 } else {
96 throw new Error("User with " + username + " is not found");
97 }
98 }
99
100 /**
101 * Get credentials by username
102 * @param username - username to search for user
103 * @param {string} pool - name of pool
104 * @return {Object}
105 * @throws {Error}
106 */
107 getCredentialsByUsername(username, pool) {
108 let poolRef = this.credentials;
109 if (pool) {
110 if (!this[pool]) throw new Error("Pool " + pool + "doesn't exist");
111 poolRef = this[pool];
112 }
113 const freeUserIndex = poolRef.findIndex(item => item.username === username);
114
115 if (freeUserIndex !== -1) {
116 poolRef[freeUserIndex].isLocked = true;
117 poolRef[freeUserIndex].freeTimeout();
118 return poolRef[freeUserIndex];
119 } else {
120 throw new Error("There are no users with username: " + username);
121 }
122 }
123
124}
125
126module.exports = CredentialDB;
\No newline at end of file