@telperion/reactive-storage 1.2.0 → 1.3.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 (56) hide show
  1. package/.build-manifest.json +96 -0
  2. package/fesm/reactive-storage.mjs +620 -0
  3. package/fesm/reactive-storage.mjs.map +1 -0
  4. package/index.cjs +17 -8
  5. package/index.cjs.map +1 -0
  6. package/index.d.cts +6 -0
  7. package/index.d.ts +6 -5
  8. package/index.js +5 -6
  9. package/index.js.map +1 -1
  10. package/json-parse.cjs +5 -3
  11. package/json-parse.cjs.map +1 -0
  12. package/json-parse.d.cts +8 -0
  13. package/json-parse.d.ts +3 -1
  14. package/json-parse.js +2 -1
  15. package/json-parse.js.map +1 -1
  16. package/package.json +70 -31
  17. package/reactive-cookie-storage.cjs +49 -44
  18. package/reactive-cookie-storage.cjs.map +1 -0
  19. package/reactive-cookie-storage.d.cts +77 -0
  20. package/reactive-cookie-storage.d.ts +48 -40
  21. package/reactive-cookie-storage.js +40 -35
  22. package/reactive-cookie-storage.js.map +1 -1
  23. package/reactive-storage.cjs +127 -74
  24. package/reactive-storage.cjs.map +1 -0
  25. package/reactive-storage.d.cts +144 -0
  26. package/reactive-storage.d.ts +87 -27
  27. package/reactive-storage.js +84 -31
  28. package/reactive-storage.js.map +1 -1
  29. package/reactive-web-local-storage.cjs +31 -8
  30. package/reactive-web-local-storage.cjs.map +1 -0
  31. package/reactive-web-local-storage.d.cts +38 -0
  32. package/reactive-web-local-storage.d.ts +31 -5
  33. package/reactive-web-local-storage.js +28 -5
  34. package/reactive-web-local-storage.js.map +1 -1
  35. package/reactive-web-session-storage.cjs +24 -8
  36. package/reactive-web-session-storage.cjs.map +1 -0
  37. package/reactive-web-session-storage.d.cts +31 -0
  38. package/reactive-web-session-storage.d.ts +24 -5
  39. package/reactive-web-session-storage.js +21 -5
  40. package/reactive-web-session-storage.js.map +1 -1
  41. package/reactive-web-storage.cjs +39 -23
  42. package/reactive-web-storage.cjs.map +1 -0
  43. package/reactive-web-storage.d.cts +47 -0
  44. package/reactive-web-storage.d.ts +28 -10
  45. package/reactive-web-storage.js +29 -13
  46. package/reactive-web-storage.js.map +1 -1
  47. package/regex-parser.cjs +4 -3
  48. package/regex-parser.cjs.map +1 -0
  49. package/regex-parser.d.cts +13 -0
  50. package/regex-parser.d.ts +3 -1
  51. package/regex-parser.js +2 -1
  52. package/regex-parser.js.map +1 -1
  53. package/async-iterator.polyfill.cjs +0 -87
  54. package/async-iterator.polyfill.d.ts +0 -2
  55. package/async-iterator.polyfill.js +0 -86
  56. package/async-iterator.polyfill.js.map +0 -1
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.0",
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.5.0",
27
+ "@telperion/rx-utils": "^1.7.0",
28
+ "@telperion/ts-utils": "^1.1.0"
29
29
  },
30
30
  "type": "module",
31
31
  "module": "./index.js",
@@ -50,46 +50,85 @@
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"
121
+ },
122
+ "./regex-parser": {
123
+ "types": {
124
+ "import": "./regex-parser.d.ts",
125
+ "require": "./regex-parser.d.cts"
126
+ },
127
+ "import": "./regex-parser.js",
128
+ "require": "./regex-parser.cjs",
129
+ "node": "./regex-parser.cjs",
130
+ "default": "./regex-parser.js"
93
131
  }
94
- }
95
- }
132
+ },
133
+ "types": "./index.d.ts"
134
+ }
@@ -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>` (characters other than letters, digits,
13
+ * `_` and `-` become `_`), holding the store as URI-encoded JSON. Every cookie is written with
14
+ * 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 });
@@ -208,5 +211,7 @@ class ReactiveCookieStorage extends reactive_storage_1.ReactiveStorage {
208
211
  }
209
212
  }
210
213
  }
211
- exports.ReactiveCookieStorage = ReactiveCookieStorage;
212
214
  _a = ReactiveCookieStorage;
215
+
216
+ exports.ReactiveCookieStorage = ReactiveCookieStorage;
217
+ //# 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,+B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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>` (characters other than letters, digits,
26
+ * `_` and `-` become `_`), holding the store as URI-encoded JSON. Every cookie is written with
27
+ * 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 };
@@ -1,69 +1,77 @@
1
- import { ReactiveStorage } from './reactive-storage';
1
+ import { ReactiveStorage, ReactiveStorageChangeEvent } from './reactive-storage.js';
2
+ import 'rxjs';
3
+
2
4
  /**
3
- * Options for configuring cookie behavior in ReactiveCookieStorage.
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.
4
7
  */
5
- export interface ReactiveCookieStorageOptions {
6
- /** URL path where the cookie is valid. Default: '/' */
8
+ interface ReactiveCookieStorageOptions {
9
+ /** URL path the cookie is sent for, e.g. `'/'` for the whole site. Unset: the path of the current page */
7
10
  path?: string;
8
- /** Domain where the cookie is valid. Default: current domain */
11
+ /** Domain the cookie is sent to, e.g. `'example.com'` to include subdomains. Unset: the current host only */
9
12
  domain?: string;
10
- /** Cookie lifetime in seconds. Default: undefined (session cookie) */
13
+ /** Lifetime in seconds. With neither `maxAge` nor `expires`, it is a session cookie */
11
14
  maxAge?: number;
12
- /** Absolute expiration date. Default: undefined */
15
+ /** Absolute expiry date. With neither `maxAge` nor `expires`, it is a session cookie */
13
16
  expires?: Date;
14
- /** HTTPS only flag. Default: false */
17
+ /** Send the cookie over HTTPS only */
15
18
  secure?: boolean;
16
- /** CSRF protection level. Default: 'lax' */
19
+ /** Cross-site sending policy. Unset: the browser default, `lax` in current browsers */
17
20
  sameSite?: 'strict' | 'lax' | 'none';
18
21
  }
19
22
  /**
20
- * Reactive wrapper for browser cookies.
21
- * Provides RxJS-based reactive API for cookie storage with automatic change detection.
23
+ * Reactive storage on `document.cookie`: for small values the server should see too, or that must expire.
22
24
  *
23
- * **Important Limitations:**
24
- * - Cookies have a ~4KB size limit per cookie
25
- * - No native change events - use linker to sync multiple instances
26
- * - Cannot access httpOnly cookies set by the server
25
+ * Each store is one cookie named `<appName>_<storeName>` (characters other than letters, digits,
26
+ * `_` and `-` become `_`), holding the store as URI-encoded JSON. Every cookie is written with
27
+ * the attributes given in `options`.
27
28
  *
28
- * **Cross-Instance Synchronization:**
29
- * Link multiple instances using any shared value (symbol, string, object, etc.) to automatically sync changes between them.
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.
30
33
  *
31
- * @template S - Storage store name type
34
+ * Example usage:
35
+ * ```ts
36
+ * import { ReactiveCookieStorage } from '@telperion/reactive-storage/reactive-cookie-storage';
32
37
  *
33
- * @example
34
- * ```typescript
35
- * // Single instance
36
- * const storage = new ReactiveCookieStorage('my-app', {
37
- * path: '/',
38
- * maxAge: 86400,
39
- * secure: true,
40
- * sameSite: 'strict'
41
- * });
38
+ * const storage = new ReactiveCookieStorage('my-app', { path: '/', maxAge: 60 * 60 * 24, sameSite: 'strict' });
42
39
  *
43
- * // Multiple linked instances using a symbol
44
- * const linker = Symbol('cookie-sync');
45
- * const storageOne = new ReactiveCookieStorage('my-app', { secure: true }, linker);
46
- * const storageTwo = new ReactiveCookieStorage('my-app', { secure: false }, linker);
47
- * // Changes in storageOne will notify storageTwo and vice versa
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
48
  *
49
- * // Or use any other value type
50
- * const storageA = new ReactiveCookieStorage('my-app', { secure: true }, 'my-linker');
51
- * const storageB = new ReactiveCookieStorage('my-app', { secure: false }, 'my-linker');
49
+ * header.get('prefs', 'theme').subscribe(console.log);
50
+ * await settings.set('prefs', 'theme', 'dark'); // header logs "dark"
52
51
  * ```
52
+ *
53
+ * @template S - Store name type: a union of string literals restricts the store names you can use
53
54
  */
54
- export declare class ReactiveCookieStorage<S extends string = string> extends ReactiveStorage<S> {
55
+ declare class ReactiveCookieStorage<S extends string = string> extends ReactiveStorage<S> {
55
56
  #private;
56
57
  readonly appName: string;
57
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
+ */
58
64
  constructor(appName?: string, options?: ReactiveCookieStorageOptions, linker?: any);
59
65
  /**
60
- * Destroy the storage instance and cleanup resources.
61
- * Unregisters from linker group and completes the changes subject.
62
- * Call this when you're done using the storage to prevent memory leaks.
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.
63
68
  */
64
69
  destroy(): void;
65
70
  protected [ReactiveStorage.Props.GET_ALL](): Promise<any>;
66
- protected [ReactiveStorage.Props.CHANGES]: any;
71
+ protected [ReactiveStorage.Props.CHANGES]: AsyncGenerator<ReactiveStorageChangeEvent<S>>;
67
72
  protected [ReactiveStorage.Props.SET](storeName: S, value: unknown): Promise<void>;
68
73
  protected [ReactiveStorage.Props.REMOVE](storeName: S): Promise<void>;
69
74
  }
75
+
76
+ export { ReactiveCookieStorage };
77
+ export type { ReactiveCookieStorageOptions };