-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
329 lines (304 loc) · 12.1 KB
/
index.ts
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import getProcessLock from './processLock';
/**
* @author: SuperTokens (https://github.com/supertokens)
* This library was created as a part of a larger project, SuperTokens(https://supertokens.io) - the best session management solution.
* You can also check out our other projects on https://github.com/supertokens
*
* To contribute to this package visit https://github.com/supertokens/browser-tabs-lock
* If you face any problems you can file an issue on https://github.com/supertokens/browser-tabs-lock/issues
*
* If you have any questions or if you just want to say hi visit https://supertokens.io/discord
*/
/**
* @constant
* @type {string}
* @default
* @description All the locks taken by this package will have this as prefix
*/
const LOCK_STORAGE_KEY = 'browser-tabs-lock-key';
declare let setTimeout: any;
declare let window: any;
declare let clearTimeout: any;
export type StorageHandler = {
key: (index: number) => Promise<string | null>;
getItem: (key: string) => Promise<string | null>;
clear: () => Promise<void>;
removeItem: (key: string) => Promise<void>;
setItem: (key: string, value: string) => Promise<void>;
/**
* Sync versions of the storage functions
*/
keySync: (index: number) => string | null;
getItemSync: (key: string) => string | null;
clearSync: () => void;
removeItemSync: (key: string) => void;
setItemSync: (key: string, value: string) => void;
};
const DEFAULT_STORAGE_HANDLER: StorageHandler = {
key: async (index: number) => {
throw new Error("Unsupported");
},
getItem: async (key: string) => {
throw new Error("Unsupported");
},
clear: async () => {
return window.localStorage.clear();
},
removeItem: async (key: string) => {
throw new Error("Unsupported");
},
setItem: async (key: string, value: string) => {
throw new Error("Unsupported");
},
keySync: (index: number) => {
return window.localStorage.key(index);
},
getItemSync: (key: string) => {
return window.localStorage.getItem(key);
},
clearSync: () => {
return window.localStorage.clear();
},
removeItemSync: (key: string) => {
return window.localStorage.removeItem(key);
},
setItemSync: (key: string, value: string) => {
return window.localStorage.setItem(key, value);
},
}
/**
* @function delay
* @param {number} milliseconds - How long the delay should be in terms of milliseconds
* @returns {Promise<void>}
*/
function delay(milliseconds: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, milliseconds));
}
/**
* @function generateRandomString
* @params {number} length - How long the random string should be
* @returns {string}
* @description returns random string whose length is equal to the length passed as parameter
*/
function generateRandomString(length: number): string {
const CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz';
let randomstring = '';
for (let i = 0; i < length; i++) {
const INDEX = Math.floor(Math.random() * CHARS.length);
randomstring += CHARS[INDEX];
}
return randomstring;
}
/**
* @function getLockId
* @returns {string}
* @description Generates an id which will be unique for the browser tab
*/
function getLockId(): string {
return Date.now().toString() + generateRandomString(15)
}
export default class SuperTokensLock {
private static waiters: Array<any> | undefined = undefined;
private id: string;
private acquiredIatSet: Set<String> = new Set<String>();
private storageHandler: StorageHandler | undefined = undefined;
constructor(storageHandler?: StorageHandler) {
this.id = getLockId();
this.acquireLock = this.acquireLock.bind(this);
this.releaseLock = this.releaseLock.bind(this);
this.releaseLock__private__ = this.releaseLock__private__.bind(this);
this.waitForSomethingToChange = this.waitForSomethingToChange.bind(this);
this.refreshLockWhileAcquired = this.refreshLockWhileAcquired.bind(this);
this.storageHandler = storageHandler;
if (SuperTokensLock.waiters === undefined) {
SuperTokensLock.waiters = [];
}
}
/**
* @async
* @memberOf Lock
* @function acquireLock
* @param {string} lockKey - Key for which the lock is being acquired
* @param {number} [timeout=5000] - Maximum time for which the function will wait to acquire the lock
* @returns {Promise<boolean>}
* @description Will return true if lock is being acquired, else false.
* Also the lock can be acquired for maximum 10 secs
*/
async acquireLock(lockKey: string, timeout: number = 5000) {
let iat = Date.now() + generateRandomString(4);
const MAX_TIME = Date.now() + timeout;
const STORAGE_KEY = `${LOCK_STORAGE_KEY}-${lockKey}`;
const STORAGE: StorageHandler = this.storageHandler === undefined ? DEFAULT_STORAGE_HANDLER : this.storageHandler;
while (Date.now() < MAX_TIME) {
await delay(30);
let lockObj = STORAGE.getItemSync(STORAGE_KEY);
if (lockObj === null) {
const TIMEOUT_KEY = `${this.id}-${lockKey}-${iat}`;
// there is a problem if setItem happens at the exact same time for 2 different processes.. so we add some random delay here.
await delay(Math.floor(Math.random() * 25));
STORAGE.setItemSync(STORAGE_KEY, JSON.stringify({
id: this.id,
iat,
timeoutKey: TIMEOUT_KEY,
timeAcquired: Date.now(),
timeRefreshed: Date.now()
}));
await delay(30); // this is to prevent race conditions. This time must be more than the time it takes for storage.setItem
let lockObjPostDelay = STORAGE.getItemSync(STORAGE_KEY);
if (lockObjPostDelay !== null) {
let parsedLockObjPostDelay = JSON.parse(lockObjPostDelay);
if (parsedLockObjPostDelay.id === this.id && parsedLockObjPostDelay.iat === iat) {
this.acquiredIatSet.add(iat);
this.refreshLockWhileAcquired(STORAGE_KEY, iat);
return true;
}
}
} else {
SuperTokensLock.lockCorrector(this.storageHandler === undefined ? DEFAULT_STORAGE_HANDLER : this.storageHandler);
await this.waitForSomethingToChange(MAX_TIME);
}
iat = Date.now() + generateRandomString(4);
}
return false;
}
private async refreshLockWhileAcquired(storageKey: string, iat: string) {
setTimeout(async () => {
await getProcessLock().lock(iat);
if (!this.acquiredIatSet.has(iat)) {
getProcessLock().unlock(iat);
return;
}
const STORAGE: StorageHandler = this.storageHandler === undefined ? DEFAULT_STORAGE_HANDLER : this.storageHandler;
let lockObj = STORAGE.getItemSync(storageKey);
if (lockObj !== null) {
let parsedLockObj = JSON.parse(lockObj);
parsedLockObj.timeRefreshed = Date.now();
STORAGE.setItemSync(storageKey, JSON.stringify(parsedLockObj));
getProcessLock().unlock(iat);
} else {
getProcessLock().unlock(iat);
return;
}
this.refreshLockWhileAcquired(storageKey, iat);
}, 1000);
}
private async waitForSomethingToChange(MAX_TIME: number) {
await new Promise(resolve => {
let resolvedCalled = false;
let startedAt = Date.now();
const MIN_TIME_TO_WAIT = 50; // ms
let removedListeners = false;
function stopWaiting() {
if (!removedListeners) {
window.removeEventListener('storage', stopWaiting);
SuperTokensLock.removeFromWaiting(stopWaiting);
clearTimeout(timeOutId);
removedListeners = true;
}
if (!resolvedCalled) {
resolvedCalled = true;
let timeToWait = MIN_TIME_TO_WAIT - (Date.now() - startedAt);
if (timeToWait > 0) {
setTimeout(resolve, timeToWait);
} else {
resolve(null);
}
}
}
window.addEventListener('storage', stopWaiting);
SuperTokensLock.addToWaiting(stopWaiting);
let timeOutId = setTimeout(stopWaiting, Math.max(0, MAX_TIME - Date.now()));
});
}
private static addToWaiting(func: any) {
this.removeFromWaiting(func);
if (SuperTokensLock.waiters === undefined) {
return;
}
SuperTokensLock.waiters.push(func);
}
private static removeFromWaiting(func: any) {
if (SuperTokensLock.waiters === undefined) {
return;
}
SuperTokensLock.waiters = SuperTokensLock.waiters.filter(i => i !== func);
}
private static notifyWaiters() {
if (SuperTokensLock.waiters === undefined) {
return;
}
let waiters = [...SuperTokensLock.waiters]; // so that if Lock.waiters is changed it's ok.
waiters.forEach(i => i());
}
/**
* @function releaseLock
* @memberOf Lock
* @param {string} lockKey - Key for which lock is being released
* @returns {void}
* @description Release a lock.
*/
async releaseLock(lockKey: string) {
return await this.releaseLock__private__(lockKey);
}
/**
* @function releaseLock
* @memberOf Lock
* @param {string} lockKey - Key for which lock is being released
* @returns {void}
* @description Release a lock.
*/
private async releaseLock__private__(lockKey: string) {
const STORAGE: StorageHandler = this.storageHandler === undefined ? DEFAULT_STORAGE_HANDLER : this.storageHandler;
const STORAGE_KEY = `${LOCK_STORAGE_KEY}-${lockKey}`;
let lockObj = STORAGE.getItemSync(STORAGE_KEY);
if (lockObj === null) {
return;
}
let parsedlockObj = JSON.parse(lockObj);
if (parsedlockObj.id === this.id) {
await getProcessLock().lock(parsedlockObj.iat);
this.acquiredIatSet.delete(parsedlockObj.iat);
STORAGE.removeItemSync(STORAGE_KEY);
getProcessLock().unlock(parsedlockObj.iat);
SuperTokensLock.notifyWaiters();
}
}
/**
* @function lockCorrector
* @returns {void}
* @description If a lock is acquired by a tab and the tab is closed before the lock is
* released, this function will release those locks
*/
private static lockCorrector(storageHandler: StorageHandler) {
const MIN_ALLOWED_TIME = Date.now() - 5000;
const STORAGE = storageHandler;
const KEYS: string[] = [];
let currIndex = 0;
while (true) {
let key = STORAGE.keySync(currIndex);
if (key === null) {
break;
}
KEYS.push(key);
currIndex++;
}
let notifyWaiters = false;
for (let i = 0; i < KEYS.length; i++) {
const LOCK_KEY = KEYS[i];
if (LOCK_KEY.includes(LOCK_STORAGE_KEY)) {
let lockObj = STORAGE.getItemSync(LOCK_KEY);
if (lockObj !== null) {
let parsedlockObj = JSON.parse(lockObj);
if ((parsedlockObj.timeRefreshed === undefined && parsedlockObj.timeAcquired < MIN_ALLOWED_TIME) ||
(parsedlockObj.timeRefreshed !== undefined && parsedlockObj.timeRefreshed < MIN_ALLOWED_TIME)) {
STORAGE.removeItemSync(LOCK_KEY);
notifyWaiters = true;
}
}
}
}
if (notifyWaiters) {
SuperTokensLock.notifyWaiters();
}
}
}