@telperion/reactive-storage 1.2.0 → 1.3.1

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 (55) hide show
  1. package/.build-manifest.json +86 -0
  2. package/README.md +6 -6
  3. package/fesm/reactive-storage.mjs +639 -0
  4. package/fesm/reactive-storage.mjs.map +1 -0
  5. package/index.cjs +17 -8
  6. package/index.cjs.map +1 -0
  7. package/index.d.cts +6 -0
  8. package/index.d.ts +6 -5
  9. package/index.js +5 -6
  10. package/index.js.map +1 -1
  11. package/json-parse.cjs +5 -3
  12. package/json-parse.cjs.map +1 -0
  13. package/json-parse.d.cts +8 -0
  14. package/json-parse.d.ts +3 -1
  15. package/json-parse.js +2 -1
  16. package/json-parse.js.map +1 -1
  17. package/package.json +60 -31
  18. package/reactive-cookie-storage.cjs +69 -50
  19. package/reactive-cookie-storage.cjs.map +1 -0
  20. package/reactive-cookie-storage.d.cts +77 -0
  21. package/reactive-cookie-storage.d.ts +48 -40
  22. package/reactive-cookie-storage.js +60 -41
  23. package/reactive-cookie-storage.js.map +1 -1
  24. package/reactive-storage.cjs +168 -83
  25. package/reactive-storage.cjs.map +1 -0
  26. package/reactive-storage.d.cts +165 -0
  27. package/reactive-storage.d.ts +118 -37
  28. package/reactive-storage.js +128 -43
  29. package/reactive-storage.js.map +1 -1
  30. package/reactive-web-local-storage.cjs +30 -8
  31. package/reactive-web-local-storage.cjs.map +1 -0
  32. package/reactive-web-local-storage.d.cts +37 -0
  33. package/reactive-web-local-storage.d.ts +30 -5
  34. package/reactive-web-local-storage.js +27 -5
  35. package/reactive-web-local-storage.js.map +1 -1
  36. package/reactive-web-session-storage.cjs +24 -8
  37. package/reactive-web-session-storage.cjs.map +1 -0
  38. package/reactive-web-session-storage.d.cts +31 -0
  39. package/reactive-web-session-storage.d.ts +24 -5
  40. package/reactive-web-session-storage.js +21 -5
  41. package/reactive-web-session-storage.js.map +1 -1
  42. package/reactive-web-storage.cjs +42 -27
  43. package/reactive-web-storage.cjs.map +1 -0
  44. package/reactive-web-storage.d.cts +47 -0
  45. package/reactive-web-storage.d.ts +28 -10
  46. package/reactive-web-storage.js +33 -18
  47. package/reactive-web-storage.js.map +1 -1
  48. package/async-iterator.polyfill.cjs +0 -87
  49. package/async-iterator.polyfill.d.ts +0 -2
  50. package/async-iterator.polyfill.js +0 -86
  51. package/async-iterator.polyfill.js.map +0 -1
  52. package/regex-parser.cjs +0 -29
  53. package/regex-parser.d.ts +0 -11
  54. package/regex-parser.js +0 -27
  55. package/regex-parser.js.map +0 -1
package/index.cjs CHANGED
@@ -1,8 +1,17 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- tslib_1.__exportStar(require("./reactive-storage"), exports);
5
- tslib_1.__exportStar(require("./reactive-web-storage"), exports);
6
- tslib_1.__exportStar(require("./reactive-web-local-storage"), exports);
7
- tslib_1.__exportStar(require("./reactive-web-session-storage"), exports);
8
- tslib_1.__exportStar(require("./reactive-cookie-storage"), exports);
1
+ 'use strict';
2
+
3
+ var reactiveStorage = require('./reactive-storage.cjs');
4
+ var reactiveWebStorage = require('./reactive-web-storage.cjs');
5
+ var reactiveWebLocalStorage = require('./reactive-web-local-storage.cjs');
6
+ var reactiveWebSessionStorage = require('./reactive-web-session-storage.cjs');
7
+ var reactiveCookieStorage = require('./reactive-cookie-storage.cjs');
8
+
9
+
10
+
11
+ exports.ReactiveStorage = reactiveStorage.ReactiveStorage;
12
+ exports.AbstractReactiveWebStorage = reactiveWebStorage.AbstractReactiveWebStorage;
13
+ exports.ReactiveWebStorage = reactiveWebStorage.ReactiveWebStorage;
14
+ exports.ReactiveWebLocalStorage = reactiveWebLocalStorage.ReactiveWebLocalStorage;
15
+ exports.ReactiveWebSessionStorage = reactiveWebSessionStorage.ReactiveWebSessionStorage;
16
+ exports.ReactiveCookieStorage = reactiveCookieStorage.ReactiveCookieStorage;
17
+ //# sourceMappingURL=index.cjs.map
package/index.cjs.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;"}
package/index.d.cts ADDED
@@ -0,0 +1,6 @@
1
+ export { ReactiveStorage, ReactiveStorageChangeEvent } from './reactive-storage.cjs';
2
+ export { AbstractReactiveWebStorage, ReactiveWebStorage } from './reactive-web-storage.cjs';
3
+ export { ReactiveWebLocalStorage } from './reactive-web-local-storage.cjs';
4
+ export { ReactiveWebSessionStorage } from './reactive-web-session-storage.cjs';
5
+ export { ReactiveCookieStorage, ReactiveCookieStorageOptions } from './reactive-cookie-storage.cjs';
6
+ import 'rxjs';
package/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- export * from './reactive-storage';
2
- export * from './reactive-web-storage';
3
- export * from './reactive-web-local-storage';
4
- export * from './reactive-web-session-storage';
5
- export * from './reactive-cookie-storage';
1
+ export { ReactiveStorage, ReactiveStorageChangeEvent } from './reactive-storage.js';
2
+ export { AbstractReactiveWebStorage, ReactiveWebStorage } from './reactive-web-storage.js';
3
+ export { ReactiveWebLocalStorage } from './reactive-web-local-storage.js';
4
+ export { ReactiveWebSessionStorage } from './reactive-web-session-storage.js';
5
+ export { ReactiveCookieStorage, ReactiveCookieStorageOptions } from './reactive-cookie-storage.js';
6
+ import 'rxjs';
package/index.js CHANGED
@@ -1,7 +1,6 @@
1
- export * from './reactive-storage';
2
- export * from './reactive-web-storage';
3
- export * from './reactive-web-local-storage';
4
- export * from './reactive-web-session-storage';
5
- export * from './reactive-cookie-storage';
6
-
1
+ export { ReactiveStorage } from './reactive-storage.js';
2
+ export { AbstractReactiveWebStorage, ReactiveWebStorage } from './reactive-web-storage.js';
3
+ export { ReactiveWebLocalStorage } from './reactive-web-local-storage.js';
4
+ export { ReactiveWebSessionStorage } from './reactive-web-session-storage.js';
5
+ export { ReactiveCookieStorage } from './reactive-cookie-storage.js';
7
6
  //# sourceMappingURL=index.js.map
package/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../libs/reactive-storage/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,wBAAwB,CAAC;AACvC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,2BAA2B,CAAC","file":"index.js","sourcesContent":["export * from './reactive-storage';\nexport * from './reactive-web-storage';\nexport * from './reactive-web-local-storage';\nexport * from './reactive-web-session-storage';\nexport * from './reactive-cookie-storage';\n"]}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"}
package/json-parse.cjs CHANGED
@@ -1,6 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.jsonParse = jsonParse;
1
+ 'use strict';
2
+
4
3
  /**
5
4
  * Safely parse JSON string, returning undefined on failure.
6
5
  * @param value - JSON string to parse
@@ -14,3 +13,6 @@ function jsonParse(value) {
14
13
  return undefined;
15
14
  }
16
15
  }
16
+
17
+ exports.jsonParse = jsonParse;
18
+ //# sourceMappingURL=json-parse.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json-parse.cjs","sources":["../../../libs/reactive-storage/src/json-parse.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAAA;;;;AAIG;AACG,SAAU,SAAS,CAAI,KAAa,EAAA;AACxC,IAAA,IAAI;AACF,QAAA,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC;IAC1B;AAAE,IAAA,MAAM;AACN,QAAA,OAAO,SAAU;IACnB;AACF;;;;"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Safely parse JSON string, returning undefined on failure.
3
+ * @param value - JSON string to parse
4
+ * @returns Parsed value or undefined if parsing fails
5
+ */
6
+ declare function jsonParse<T>(value: string): T;
7
+
8
+ export { jsonParse };
package/json-parse.d.ts CHANGED
@@ -3,4 +3,6 @@
3
3
  * @param value - JSON string to parse
4
4
  * @returns Parsed value or undefined if parsing fails
5
5
  */
6
- export declare function jsonParse<T>(value: string): T;
6
+ declare function jsonParse<T>(value: string): T;
7
+
8
+ export { jsonParse };
package/json-parse.js CHANGED
@@ -3,7 +3,7 @@
3
3
  * @param value - JSON string to parse
4
4
  * @returns Parsed value or undefined if parsing fails
5
5
  */
6
- export function jsonParse(value) {
6
+ function jsonParse(value) {
7
7
  try {
8
8
  return JSON.parse(value);
9
9
  }
@@ -12,4 +12,5 @@ export function jsonParse(value) {
12
12
  }
13
13
  }
14
14
 
15
+ export { jsonParse };
15
16
  //# sourceMappingURL=json-parse.js.map
package/json-parse.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../libs/reactive-storage/src/json-parse.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,SAAS,CAAI,KAAa;IACxC,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAC3B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAU,CAAC;IACpB,CAAC;AACH,CAAC","file":"json-parse.js","sourcesContent":["/**\r\n * Safely parse JSON string, returning undefined on failure.\r\n * @param value - JSON string to parse\r\n * @returns Parsed value or undefined if parsing fails\r\n */\r\nexport function jsonParse<T>(value: string): T {\r\n try {\r\n return JSON.parse(value);\r\n } catch {\r\n return undefined!;\r\n }\r\n}\r\n"]}
1
+ {"version":3,"file":"json-parse.js","sources":["../../../libs/reactive-storage/src/json-parse.ts"],"sourcesContent":[null],"names":[],"mappings":"AAAA;;;;AAIG;AACG,SAAU,SAAS,CAAI,KAAa,EAAA;AACxC,IAAA,IAAI;AACF,QAAA,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC;IAC1B;AAAE,IAAA,MAAM;AACN,QAAA,OAAO,SAAU;IACnB;AACF;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@telperion/reactive-storage",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "description": "RxJS-based reactive storage library for browser environments. Provides Observable-based wrappers for localStorage, sessionStorage, and cookies with immutability, type safety, and cross-instance synchronization support. Built with TypeScript and RxJS for reactive data management.",
5
5
  "keywords": [
6
6
  "rxjs",
@@ -23,9 +23,9 @@
23
23
  "dependencies": {
24
24
  "tslib": "^2.3.0",
25
25
  "rxjs": "^7.8.1",
26
- "@thalesrc/js-utils": "^3.3.1",
27
- "@thalesrc/rx-utils": "^1.0.0",
28
- "@thalesrc/ts-utils": "^1.0.1"
26
+ "@telperion/js-utils": "^3.9.0",
27
+ "@telperion/rx-utils": "^1.8.0",
28
+ "@telperion/ts-utils": "^1.1.0"
29
29
  },
30
30
  "type": "module",
31
31
  "module": "./index.js",
@@ -50,46 +50,75 @@
50
50
  },
51
51
  "exports": {
52
52
  ".": {
53
- "default": "./index.cjs",
53
+ "types": {
54
+ "import": "./index.d.ts",
55
+ "require": "./index.d.cts"
56
+ },
54
57
  "import": "./index.js",
55
- "node": "./index.cjs",
56
58
  "require": "./index.cjs",
57
- "types": "./index.d.ts"
59
+ "node": "./index.cjs",
60
+ "default": "./index.js"
61
+ },
62
+ "./json-parse": {
63
+ "types": {
64
+ "import": "./json-parse.d.ts",
65
+ "require": "./json-parse.d.cts"
66
+ },
67
+ "import": "./json-parse.js",
68
+ "require": "./json-parse.cjs",
69
+ "node": "./json-parse.cjs",
70
+ "default": "./json-parse.js"
71
+ },
72
+ "./reactive-cookie-storage": {
73
+ "types": {
74
+ "import": "./reactive-cookie-storage.d.ts",
75
+ "require": "./reactive-cookie-storage.d.cts"
76
+ },
77
+ "import": "./reactive-cookie-storage.js",
78
+ "require": "./reactive-cookie-storage.cjs",
79
+ "node": "./reactive-cookie-storage.cjs",
80
+ "default": "./reactive-cookie-storage.js"
58
81
  },
59
82
  "./reactive-storage": {
60
- "default": "./reactive-storage.cjs",
83
+ "types": {
84
+ "import": "./reactive-storage.d.ts",
85
+ "require": "./reactive-storage.d.cts"
86
+ },
61
87
  "import": "./reactive-storage.js",
62
- "node": "./reactive-storage.cjs",
63
88
  "require": "./reactive-storage.cjs",
64
- "types": "./reactive-storage.d.ts"
65
- },
66
- "./reactive-web-storage": {
67
- "default": "./reactive-web-storage.cjs",
68
- "import": "./reactive-web-storage.js",
69
- "node": "./reactive-web-storage.cjs",
70
- "require": "./reactive-web-storage.cjs",
71
- "types": "./reactive-web-storage.d.ts"
89
+ "node": "./reactive-storage.cjs",
90
+ "default": "./reactive-storage.js"
72
91
  },
73
92
  "./reactive-web-local-storage": {
74
- "default": "./reactive-web-local-storage.cjs",
93
+ "types": {
94
+ "import": "./reactive-web-local-storage.d.ts",
95
+ "require": "./reactive-web-local-storage.d.cts"
96
+ },
75
97
  "import": "./reactive-web-local-storage.js",
76
- "node": "./reactive-web-local-storage.cjs",
77
98
  "require": "./reactive-web-local-storage.cjs",
78
- "types": "./reactive-web-local-storage.d.ts"
99
+ "node": "./reactive-web-local-storage.cjs",
100
+ "default": "./reactive-web-local-storage.js"
79
101
  },
80
102
  "./reactive-web-session-storage": {
81
- "default": "./reactive-web-session-storage.cjs",
103
+ "types": {
104
+ "import": "./reactive-web-session-storage.d.ts",
105
+ "require": "./reactive-web-session-storage.d.cts"
106
+ },
82
107
  "import": "./reactive-web-session-storage.js",
83
- "node": "./reactive-web-session-storage.cjs",
84
108
  "require": "./reactive-web-session-storage.cjs",
85
- "types": "./reactive-web-session-storage.d.ts"
109
+ "node": "./reactive-web-session-storage.cjs",
110
+ "default": "./reactive-web-session-storage.js"
86
111
  },
87
- "./reactive-cookie-storage": {
88
- "default": "./reactive-cookie-storage.cjs",
89
- "import": "./reactive-cookie-storage.js",
90
- "node": "./reactive-cookie-storage.cjs",
91
- "require": "./reactive-cookie-storage.cjs",
92
- "types": "./reactive-cookie-storage.d.ts"
112
+ "./reactive-web-storage": {
113
+ "types": {
114
+ "import": "./reactive-web-storage.d.ts",
115
+ "require": "./reactive-web-storage.d.cts"
116
+ },
117
+ "import": "./reactive-web-storage.js",
118
+ "require": "./reactive-web-storage.cjs",
119
+ "node": "./reactive-web-storage.cjs",
120
+ "default": "./reactive-web-storage.js"
93
121
  }
94
- }
95
- }
122
+ },
123
+ "types": "./index.d.ts"
124
+ }
@@ -1,47 +1,45 @@
1
- "use strict";
1
+ 'use strict';
2
+
3
+ var rxjs = require('rxjs');
4
+ var toAsyncIteratable = require('@telperion/rx-utils/to-async-iteratable');
5
+ var reactiveStorage = require('./reactive-storage.cjs');
6
+ var jsonParse = require('./json-parse.cjs');
7
+
2
8
  var _a;
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.ReactiveCookieStorage = void 0;
5
- const rxjs_1 = require("rxjs");
6
- const reactive_storage_1 = require("./reactive-storage");
7
- const async_iterator_polyfill_1 = require("./async-iterator.polyfill");
8
- const json_parse_1 = require("./json-parse");
9
9
  /**
10
- * Reactive wrapper for browser cookies.
11
- * Provides RxJS-based reactive API for cookie storage with automatic change detection.
10
+ * Reactive storage on `document.cookie`: for small values the server should see too, or that must expire.
12
11
  *
13
- * **Important Limitations:**
14
- * - Cookies have a ~4KB size limit per cookie
15
- * - No native change events - use linker to sync multiple instances
16
- * - Cannot access httpOnly cookies set by the server
12
+ * Each store is one cookie named `<appName>_<storeName>`, holding the store as URI-encoded JSON.
13
+ * Characters a cookie name cannot hold are percent-encoded in both parts (`user.settings` stays,
14
+ * `user settings` becomes `user%20settings`). Every cookie is written with the attributes given in `options`.
17
15
  *
18
- * **Cross-Instance Synchronization:**
19
- * Link multiple instances using any shared value (symbol, string, object, etc.) to automatically sync changes between them.
16
+ * - A cookie is limited to 4000 bytes; a larger write is rejected with an error.
17
+ * - Browsers have no change event for cookies, so only writes made through this instance are observed.
18
+ * Pass the same `linker` to several instances to have each one notified of the others' writes.
19
+ * - `httpOnly` cookies set by the server cannot be read or written from JavaScript.
20
20
  *
21
- * @template S - Storage store name type
21
+ * Example usage:
22
+ * ```ts
23
+ * import { ReactiveCookieStorage } from '@telperion/reactive-storage/reactive-cookie-storage';
22
24
  *
23
- * @example
24
- * ```typescript
25
- * // Single instance
26
- * const storage = new ReactiveCookieStorage('my-app', {
27
- * path: '/',
28
- * maxAge: 86400,
29
- * secure: true,
30
- * sameSite: 'strict'
31
- * });
25
+ * const storage = new ReactiveCookieStorage('my-app', { path: '/', maxAge: 60 * 60 * 24, sameSite: 'strict' });
32
26
  *
33
- * // Multiple linked instances using a symbol
34
- * const linker = Symbol('cookie-sync');
35
- * const storageOne = new ReactiveCookieStorage('my-app', { secure: true }, linker);
36
- * const storageTwo = new ReactiveCookieStorage('my-app', { secure: false }, linker);
37
- * // Changes in storageOne will notify storageTwo and vice versa
27
+ * await storage.set('consent', 'analytics', true); // cookie my-app_consent, valid for a day
28
+ * ```
29
+ *
30
+ * Linked instances:
31
+ * ```ts
32
+ * const linker = Symbol('cookies');
33
+ * const header = new ReactiveCookieStorage('my-app', { path: '/' }, linker);
34
+ * const settings = new ReactiveCookieStorage('my-app', { path: '/' }, linker);
38
35
  *
39
- * // Or use any other value type
40
- * const storageA = new ReactiveCookieStorage('my-app', { secure: true }, 'my-linker');
41
- * const storageB = new ReactiveCookieStorage('my-app', { secure: false }, 'my-linker');
36
+ * header.get('prefs', 'theme').subscribe(console.log);
37
+ * await settings.set('prefs', 'theme', 'dark'); // header logs "dark"
42
38
  * ```
39
+ *
40
+ * @template S - Store name type: a union of string literals restricts the store names you can use
43
41
  */
44
- class ReactiveCookieStorage extends reactive_storage_1.ReactiveStorage {
42
+ class ReactiveCookieStorage extends reactiveStorage.ReactiveStorage {
45
43
  appName;
46
44
  options;
47
45
  static #MAX_COOKIE_SIZE = 4000; // Conservative 4KB limit
@@ -51,10 +49,15 @@ class ReactiveCookieStorage extends reactive_storage_1.ReactiveStorage {
51
49
  * Returns undefined if parsing fails.
52
50
  */
53
51
  static #parse(value) {
54
- return (0, json_parse_1.jsonParse)(value);
52
+ return jsonParse.jsonParse(value);
55
53
  }
56
- #changes$ = new rxjs_1.Subject();
54
+ #changes$ = new rxjs.Subject();
57
55
  #linker;
56
+ /**
57
+ * @param appName - Prefix of this app's cookie names: each store is saved as `<appName>_<storeName>`. Defaults to `'app'`
58
+ * @param options - Attributes written with every cookie
59
+ * @param linker - Any truthy value, e.g. a symbol: instances created with the same linker are notified of each other's writes
60
+ */
58
61
  constructor(appName = 'app', options = {}, linker) {
59
62
  super();
60
63
  this.appName = appName;
@@ -69,9 +72,8 @@ class ReactiveCookieStorage extends reactive_storage_1.ReactiveStorage {
69
72
  }
70
73
  }
71
74
  /**
72
- * Destroy the storage instance and cleanup resources.
73
- * Unregisters from linker group and completes the changes subject.
74
- * Call this when you're done using the storage to prevent memory leaks.
75
+ * Releases the instance: unlinks it from its linker group and stops its change stream.
76
+ * Call it when the storage is no longer used, so linked instances do not keep it alive.
75
77
  */
76
78
  destroy() {
77
79
  // Unregister from linker group
@@ -86,13 +88,14 @@ class ReactiveCookieStorage extends reactive_storage_1.ReactiveStorage {
86
88
  }
87
89
  this.#changes$.complete();
88
90
  }
89
- async [reactive_storage_1.ReactiveStorage.Props.GET_ALL]() {
91
+ async [reactiveStorage.ReactiveStorage.Props.GET_ALL]() {
90
92
  const cookies = this.#getAppCookies();
91
93
  return Object.fromEntries(Object.entries(cookies).map(([storeName, value]) => [storeName, _a.#parse(value)]));
92
94
  }
95
+ // Prefer an Observable's own async iterator (rxjs 8, or `@telperion/rx-utils/static/async-iterator`)
93
96
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
94
- [reactive_storage_1.ReactiveStorage.Props.CHANGES] = this.#changes$[Symbol.asyncIterator] ?? (0, async_iterator_polyfill_1.toAsyncIterator)(this.#changes$);
95
- async [reactive_storage_1.ReactiveStorage.Props.SET](storeName, value) {
97
+ [reactiveStorage.ReactiveStorage.Props.CHANGES] = this.#changes$[Symbol.asyncIterator]?.() ?? toAsyncIteratable.makeAsyncIterator(this.#changes$);
98
+ async [reactiveStorage.ReactiveStorage.Props.SET](storeName, value) {
96
99
  const serialized = JSON.stringify(value);
97
100
  const cookieName = this.#getCookieName(storeName);
98
101
  // Validate size before setting
@@ -103,7 +106,7 @@ class ReactiveCookieStorage extends reactive_storage_1.ReactiveStorage {
103
106
  this.#changes$.next(event);
104
107
  this.#notifyLinkedInstances(event);
105
108
  }
106
- async [reactive_storage_1.ReactiveStorage.Props.REMOVE](storeName) {
109
+ async [reactiveStorage.ReactiveStorage.Props.REMOVE](storeName) {
107
110
  const cookieName = this.#getCookieName(storeName);
108
111
  // Delete cookie by setting max-age to 0
109
112
  this.#setCookie(cookieName, '', { maxAge: 0 });
@@ -112,11 +115,18 @@ class ReactiveCookieStorage extends reactive_storage_1.ReactiveStorage {
112
115
  this.#notifyLinkedInstances(event);
113
116
  }
114
117
  /**
115
- * Convert store name to cookie name by prefixing with app name.
116
- * Sanitizes to only allow alphanumeric, underscore, and hyphen.
118
+ * Cookie-name-safe form of an app or store name that decodes back to it: URI encoding, plus
119
+ * `(` and `)`, which it leaves but cookie names forbid. Letters, digits, `_` and `-` stay as they are.
117
120
  */
121
+ static #encodeName(name) {
122
+ return encodeURIComponent(name).replace(/[()]/g, (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`);
123
+ }
124
+ /** `<appName>_<storeName>`, each part encoded with {@link ReactiveCookieStorage.#encodeName} */
118
125
  #getCookieName(storeName) {
119
- return `${this.appName}_${storeName}`.replace(/[^a-zA-Z0-9_-]/g, '_');
126
+ return `${this.#cookiePrefix}${_a.#encodeName(storeName)}`;
127
+ }
128
+ get #cookiePrefix() {
129
+ return `${_a.#encodeName(this.appName)}_`;
120
130
  }
121
131
  /**
122
132
  * Parse document.cookie string into key-value object.
@@ -150,11 +160,18 @@ class ReactiveCookieStorage extends reactive_storage_1.ReactiveStorage {
150
160
  */
151
161
  #getAppCookies() {
152
162
  const allCookies = this.#parseCookies();
153
- const prefix = `${this.appName}_`;
163
+ const prefix = this.#cookiePrefix;
154
164
  const appCookies = {};
155
165
  for (const [cookieName, value] of Object.entries(allCookies)) {
156
- if (cookieName.startsWith(prefix)) {
157
- const storeName = cookieName.slice(prefix.length);
166
+ if (cookieName.startsWith(prefix) && cookieName.length > prefix.length) {
167
+ const encoded = cookieName.slice(prefix.length);
168
+ let storeName;
169
+ try {
170
+ storeName = decodeURIComponent(encoded);
171
+ }
172
+ catch {
173
+ storeName = encoded; // Not written by this library
174
+ }
158
175
  appCookies[storeName] = value;
159
176
  }
160
177
  }
@@ -208,5 +225,7 @@ class ReactiveCookieStorage extends reactive_storage_1.ReactiveStorage {
208
225
  }
209
226
  }
210
227
  }
211
- exports.ReactiveCookieStorage = ReactiveCookieStorage;
212
228
  _a = ReactiveCookieStorage;
229
+
230
+ exports.ReactiveCookieStorage = ReactiveCookieStorage;
231
+ //# sourceMappingURL=reactive-cookie-storage.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reactive-cookie-storage.cjs","sources":["../../../libs/reactive-storage/src/reactive-cookie-storage.ts"],"sourcesContent":[null],"names":["ReactiveStorage","jsonParse","Subject","makeAsyncIterator"],"mappings":";;;;;;;;AAyBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCG;AACG,MAAO,qBAAiD,SAAQA,+BAAkB,CAAA;AAqBpE,IAAA,OAAA;AACC,IAAA,OAAA;AArBnB,IAAA,OAAgB,gBAAgB,GAAG,IAAI,CAAC;AACxC,IAAA,OAAgB,QAAQ,GAAG,IAAI,GAAG,EAAwC;AAE1E;;;AAGG;IACH,OAAO,MAAM,CAAC,KAAa,EAAA;AACzB,QAAA,OAAOC,mBAAS,CAAC,KAAK,CAAC;IACzB;AAES,IAAA,SAAS,GAAG,IAAIC,YAAO,EAAiC;AACxD,IAAA,OAAO;AAEhB;;;;AAIG;AACH,IAAA,WAAA,CACkB,UAAkB,KAAK,EACtB,OAAA,GAAwC,EAAE,EAC3D,MAAY,EAAA;AAEZ,QAAA,KAAK,EAAE;QAJS,IAAA,CAAA,OAAO,GAAP,OAAO;QACN,IAAA,CAAA,OAAO,GAAP,OAAO;;QAMxB,IAAI,MAAM,EAAE;AAC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A,CAAE;QACrE,IAAI,IAAI,CAAC,MAAM;YAAE,MAAM,IAAI,UAAU;QACrC,IAAI,IAAI,CAAC,QAAQ;AAAE,YAAA,MAAM,IAAI,CAAA,WAAA,EAAc,IAAI,CAAC,QAAQ,EAAE;AAE1D,QAAA,QAAQ,CAAC,MAAM,GAAG,MAAM;IAC1B;AAEA;;;AAGG;AACH,IAAA,aAAa,CAAC,YAAoB,EAAA;QAChC,IAAI,YAAY,CAAC,MAAM,GAAG,EAAqB,CAAC,gBAAgB,EAAE;YAChE,MAAM,IAAI,KAAK,CACb,CAAA,aAAA,EAAgB,YAAY,CAAC,MAAM,CAAA,gBAAA,EAAmB,EAAqB,CAAC,gBAAgB,CAAA,aAAA,CAAe;AACzG,gBAAA,CAAA,gFAAA,CAAkF,CACrF;QACH;IACF;AAEA;;;AAGG;AACH,IAAA,sBAAsB,CAAC,KAAoC,EAAA;QACzD,IAAI,CAAC,IAAI,CAAC,OAAO;YAAE;AAEnB,QAAA,MAAM,eAAe,GAAG,EAAqB,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC;AACxE,QAAA,IAAI,CAAC,eAAe;YAAE;AAEtB,QAAA,KAAK,MAAM,QAAQ,IAAI,eAAe,EAAE;AACtC,YAAA,IAAI,QAAQ,KAAK,IAAI,EAAE;AACrB,gBAAA,QAAQ,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC;YAChC;QACF;IACF;;;;;;"}
@@ -0,0 +1,77 @@
1
+ import { ReactiveStorage, ReactiveStorageChangeEvent } from './reactive-storage.cjs';
2
+ import 'rxjs';
3
+
4
+ /**
5
+ * Attributes written with every cookie of a {@link ReactiveCookieStorage}. Unset attributes are
6
+ * left out of the cookie, so the browser applies its own defaults.
7
+ */
8
+ interface ReactiveCookieStorageOptions {
9
+ /** URL path the cookie is sent for, e.g. `'/'` for the whole site. Unset: the path of the current page */
10
+ path?: string;
11
+ /** Domain the cookie is sent to, e.g. `'example.com'` to include subdomains. Unset: the current host only */
12
+ domain?: string;
13
+ /** Lifetime in seconds. With neither `maxAge` nor `expires`, it is a session cookie */
14
+ maxAge?: number;
15
+ /** Absolute expiry date. With neither `maxAge` nor `expires`, it is a session cookie */
16
+ expires?: Date;
17
+ /** Send the cookie over HTTPS only */
18
+ secure?: boolean;
19
+ /** Cross-site sending policy. Unset: the browser default, `lax` in current browsers */
20
+ sameSite?: 'strict' | 'lax' | 'none';
21
+ }
22
+ /**
23
+ * Reactive storage on `document.cookie`: for small values the server should see too, or that must expire.
24
+ *
25
+ * Each store is one cookie named `<appName>_<storeName>`, holding the store as URI-encoded JSON.
26
+ * Characters a cookie name cannot hold are percent-encoded in both parts (`user.settings` stays,
27
+ * `user settings` becomes `user%20settings`). Every cookie is written with the attributes given in `options`.
28
+ *
29
+ * - A cookie is limited to 4000 bytes; a larger write is rejected with an error.
30
+ * - Browsers have no change event for cookies, so only writes made through this instance are observed.
31
+ * Pass the same `linker` to several instances to have each one notified of the others' writes.
32
+ * - `httpOnly` cookies set by the server cannot be read or written from JavaScript.
33
+ *
34
+ * Example usage:
35
+ * ```ts
36
+ * import { ReactiveCookieStorage } from '@telperion/reactive-storage/reactive-cookie-storage';
37
+ *
38
+ * const storage = new ReactiveCookieStorage('my-app', { path: '/', maxAge: 60 * 60 * 24, sameSite: 'strict' });
39
+ *
40
+ * await storage.set('consent', 'analytics', true); // cookie my-app_consent, valid for a day
41
+ * ```
42
+ *
43
+ * Linked instances:
44
+ * ```ts
45
+ * const linker = Symbol('cookies');
46
+ * const header = new ReactiveCookieStorage('my-app', { path: '/' }, linker);
47
+ * const settings = new ReactiveCookieStorage('my-app', { path: '/' }, linker);
48
+ *
49
+ * header.get('prefs', 'theme').subscribe(console.log);
50
+ * await settings.set('prefs', 'theme', 'dark'); // header logs "dark"
51
+ * ```
52
+ *
53
+ * @template S - Store name type: a union of string literals restricts the store names you can use
54
+ */
55
+ declare class ReactiveCookieStorage<S extends string = string> extends ReactiveStorage<S> {
56
+ #private;
57
+ readonly appName: string;
58
+ private readonly options;
59
+ /**
60
+ * @param appName - Prefix of this app's cookie names: each store is saved as `<appName>_<storeName>`. Defaults to `'app'`
61
+ * @param options - Attributes written with every cookie
62
+ * @param linker - Any truthy value, e.g. a symbol: instances created with the same linker are notified of each other's writes
63
+ */
64
+ constructor(appName?: string, options?: ReactiveCookieStorageOptions, linker?: any);
65
+ /**
66
+ * Releases the instance: unlinks it from its linker group and stops its change stream.
67
+ * Call it when the storage is no longer used, so linked instances do not keep it alive.
68
+ */
69
+ destroy(): void;
70
+ protected [ReactiveStorage.Props.GET_ALL](): Promise<any>;
71
+ protected [ReactiveStorage.Props.CHANGES]: AsyncGenerator<ReactiveStorageChangeEvent<S>>;
72
+ protected [ReactiveStorage.Props.SET](storeName: S, value: unknown): Promise<void>;
73
+ protected [ReactiveStorage.Props.REMOVE](storeName: S): Promise<void>;
74
+ }
75
+
76
+ export { ReactiveCookieStorage };
77
+ export type { ReactiveCookieStorageOptions };