joshlei-cookies 2.5.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.
Files changed (2) hide show
  1. package/index.js +73 -2
  2. 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();
@@ -327,6 +393,7 @@ export const SetJSCookie = async (name, data, options = {}) => {
327
393
 
328
394
  const encryptedData = await CryptoManager.encrypt(JSON.stringify(dataToStore), hashedKey);
329
395
  CookieManager.set(name, encryptedData, options);
396
+ try { _managedCookieNames.add(name); _ensureSyncing(); } catch (e) {}
330
397
  } catch (error) {
331
398
  throw new Error(`Failed to set cookie: ${error.message}`);
332
399
  }
@@ -382,9 +449,13 @@ export const RemoveJSCookie = (name, options = {}) => {
382
449
  try {
383
450
  if (isBrowser) sessionStorage.removeItem(`jstudio_token_${name}`);
384
451
  } catch (e) {}
385
-
386
452
  CookieManager.remove(name, options);
453
+ try { _managedCookieNames.delete(name); } catch (e) {}
454
+ try { _ensureSyncing(); } catch (e) {}
387
455
  } catch (error) {
388
456
  throw new Error(`Failed to remove cookie: ${error.message}`);
389
457
  }
390
- };
458
+ };
459
+
460
+ // Start syncing when module is loaded in browser
461
+ try { if (isBrowser) _ensureSyncing(); } catch (e) {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "joshlei-cookies",
3
- "version": "2.5.0",
3
+ "version": "2.8.0",
4
4
  "description": "A secure cookie management library with built-in AES encryption for browser environments",
5
5
  "main": "index.js",
6
6
  "type": "module",