joshlei-cookies 2.0.0 → 2.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +90 -9
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -207,6 +207,72 @@ const CookieManager = {
|
|
|
207
207
|
},
|
|
208
208
|
};
|
|
209
209
|
|
|
210
|
+
const _managedCookieNames = new Set();
|
|
211
|
+
|
|
212
|
+
const _ensureSyncing = (() => {
|
|
213
|
+
let initialized = false;
|
|
214
|
+
let poller = null;
|
|
215
|
+
|
|
216
|
+
return () => {
|
|
217
|
+
if (!isBrowser || initialized) return;
|
|
218
|
+
initialized = true;
|
|
219
|
+
|
|
220
|
+
try {
|
|
221
|
+
const ss = window.sessionStorage;
|
|
222
|
+
const originalRemoveItem = ss.removeItem.bind(ss);
|
|
223
|
+
const originalSetItem = ss.setItem.bind(ss);
|
|
224
|
+
|
|
225
|
+
ss.removeItem = function (key) {
|
|
226
|
+
originalRemoveItem(key);
|
|
227
|
+
try {
|
|
228
|
+
if (typeof key === 'string' && key.indexOf('jstudio_token_') === 0) {
|
|
229
|
+
const cookieName = key.slice('jstudio_token_'.length);
|
|
230
|
+
try { CookieManager.remove(cookieName); } catch (e) {}
|
|
231
|
+
_managedCookieNames.delete(cookieName);
|
|
232
|
+
}
|
|
233
|
+
} catch (e) {}
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
ss.setItem = function (key, value) {
|
|
237
|
+
originalSetItem(key, value);
|
|
238
|
+
try {
|
|
239
|
+
if (typeof key === 'string' && key.indexOf('jstudio_token_') === 0) {
|
|
240
|
+
const cookieName = key.slice('jstudio_token_'.length);
|
|
241
|
+
_managedCookieNames.add(cookieName);
|
|
242
|
+
}
|
|
243
|
+
} catch (e) {}
|
|
244
|
+
};
|
|
245
|
+
} catch (e) {}
|
|
246
|
+
|
|
247
|
+
try {
|
|
248
|
+
window.addEventListener('storage', (ev) => {
|
|
249
|
+
try {
|
|
250
|
+
if (!ev.key) return;
|
|
251
|
+
if (ev.key.indexOf('jstudio_token_') === 0 && ev.newValue === null) {
|
|
252
|
+
const cookieName = ev.key.slice('jstudio_token_'.length);
|
|
253
|
+
try { CookieManager.remove(cookieName); } catch (e) {}
|
|
254
|
+
_managedCookieNames.delete(cookieName);
|
|
255
|
+
}
|
|
256
|
+
} catch (e) {}
|
|
257
|
+
});
|
|
258
|
+
} catch (e) {}
|
|
259
|
+
|
|
260
|
+
poller = setInterval(() => {
|
|
261
|
+
try {
|
|
262
|
+
for (const name of Array.from(_managedCookieNames)) {
|
|
263
|
+
try {
|
|
264
|
+
const val = CookieManager.get(name);
|
|
265
|
+
if (val === null) {
|
|
266
|
+
try { sessionStorage.removeItem(`jstudio_token_${name}`); } catch (e) {}
|
|
267
|
+
_managedCookieNames.delete(name);
|
|
268
|
+
}
|
|
269
|
+
} catch (e) {}
|
|
270
|
+
}
|
|
271
|
+
} catch (e) {}
|
|
272
|
+
}, 2000);
|
|
273
|
+
};
|
|
274
|
+
})();
|
|
275
|
+
|
|
210
276
|
const importKey = async (rawKey) => {
|
|
211
277
|
try {
|
|
212
278
|
const crypto = await initCrypto();
|
|
@@ -309,17 +375,25 @@ export const SetJSCookie = async (name, data, options = {}) => {
|
|
|
309
375
|
const tokenParts = data.token.split('.');
|
|
310
376
|
if (tokenParts.length === 3) {
|
|
311
377
|
try {
|
|
312
|
-
|
|
378
|
+
const encryptedToken = await CryptoManager.encrypt(data.token, hashedKey);
|
|
379
|
+
sessionStorage.setItem(`jstudio_token_${name}`, encryptedToken);
|
|
313
380
|
const { token, ...rest } = data;
|
|
314
381
|
dataToStore = { ...rest, __tokenSaved: true };
|
|
315
382
|
} catch (storageErr) {
|
|
316
|
-
|
|
383
|
+
try {
|
|
384
|
+
sessionStorage.setItem(`jstudio_token_${name}`, data.token);
|
|
385
|
+
const { token, ...rest } = data;
|
|
386
|
+
dataToStore = { ...rest, __tokenSaved: true };
|
|
387
|
+
} catch (e) {
|
|
388
|
+
dataToStore = data;
|
|
389
|
+
}
|
|
317
390
|
}
|
|
318
391
|
}
|
|
319
392
|
}
|
|
320
393
|
|
|
321
394
|
const encryptedData = await CryptoManager.encrypt(JSON.stringify(dataToStore), hashedKey);
|
|
322
395
|
CookieManager.set(name, encryptedData, options);
|
|
396
|
+
try { _managedCookieNames.add(name); _ensureSyncing(); } catch (e) {}
|
|
323
397
|
} catch (error) {
|
|
324
398
|
throw new Error(`Failed to set cookie: ${error.message}`);
|
|
325
399
|
}
|
|
@@ -340,12 +414,16 @@ export const GetJSCookie = async (name) => {
|
|
|
340
414
|
const decryptedData = await CryptoManager.decrypt(encryptedData, hashedKey);
|
|
341
415
|
const parsed = JSON.parse(decryptedData);
|
|
342
416
|
|
|
343
|
-
// If token was saved separately in sessionStorage, reattach it to the returned object.
|
|
344
417
|
if (isBrowser && parsed && parsed.__tokenSaved) {
|
|
345
418
|
try {
|
|
346
|
-
const
|
|
347
|
-
if (
|
|
348
|
-
|
|
419
|
+
const stored = sessionStorage.getItem(`jstudio_token_${name}`);
|
|
420
|
+
if (stored) {
|
|
421
|
+
try {
|
|
422
|
+
const decryptedToken = await CryptoManager.decrypt(stored, hashedKey);
|
|
423
|
+
parsed.token = decryptedToken;
|
|
424
|
+
} catch (decryptErr) {
|
|
425
|
+
parsed.token = stored;
|
|
426
|
+
}
|
|
349
427
|
}
|
|
350
428
|
delete parsed.__tokenSaved;
|
|
351
429
|
} catch (storageErr) {
|
|
@@ -368,13 +446,16 @@ export const RemoveJSCookie = (name, options = {}) => {
|
|
|
368
446
|
}
|
|
369
447
|
|
|
370
448
|
try {
|
|
371
|
-
// Also remove any token saved in sessionStorage
|
|
372
449
|
try {
|
|
373
450
|
if (isBrowser) sessionStorage.removeItem(`jstudio_token_${name}`);
|
|
374
451
|
} catch (e) {}
|
|
375
|
-
|
|
376
452
|
CookieManager.remove(name, options);
|
|
453
|
+
try { _managedCookieNames.delete(name); } catch (e) {}
|
|
454
|
+
try { _ensureSyncing(); } catch (e) {}
|
|
377
455
|
} catch (error) {
|
|
378
456
|
throw new Error(`Failed to remove cookie: ${error.message}`);
|
|
379
457
|
}
|
|
380
|
-
};
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
// Start syncing when module is loaded in browser
|
|
461
|
+
try { if (isBrowser) _ensureSyncing(); } catch (e) {}
|