@telperion/reactive-storage 1.1.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 (57) hide show
  1. package/.build-manifest.json +96 -0
  2. package/README.md +172 -1
  3. package/fesm/reactive-storage.mjs +620 -0
  4. package/fesm/reactive-storage.mjs.map +1 -0
  5. package/index.cjs +17 -7
  6. package/index.cjs.map +1 -0
  7. package/index.d.cts +6 -0
  8. package/index.d.ts +6 -4
  9. package/index.js +5 -5
  10. package/index.js.map +1 -1
  11. package/json-parse.cjs +18 -0
  12. package/json-parse.cjs.map +1 -0
  13. package/json-parse.d.cts +8 -0
  14. package/json-parse.d.ts +8 -0
  15. package/json-parse.js +16 -0
  16. package/json-parse.js.map +1 -0
  17. package/package.json +91 -25
  18. package/reactive-cookie-storage.cjs +217 -0
  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 +77 -0
  22. package/reactive-cookie-storage.js +215 -0
  23. package/reactive-cookie-storage.js.map +1 -0
  24. package/reactive-storage.cjs +127 -74
  25. package/reactive-storage.cjs.map +1 -0
  26. package/reactive-storage.d.cts +144 -0
  27. package/reactive-storage.d.ts +87 -27
  28. package/reactive-storage.js +84 -31
  29. package/reactive-storage.js.map +1 -1
  30. package/reactive-web-local-storage.cjs +31 -8
  31. package/reactive-web-local-storage.cjs.map +1 -0
  32. package/reactive-web-local-storage.d.cts +38 -0
  33. package/reactive-web-local-storage.d.ts +31 -5
  34. package/reactive-web-local-storage.js +28 -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 +41 -29
  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 -11
  46. package/reactive-web-storage.js +32 -20
  47. package/reactive-web-storage.js.map +1 -1
  48. package/regex-parser.cjs +4 -3
  49. package/regex-parser.cjs.map +1 -0
  50. package/regex-parser.d.cts +13 -0
  51. package/regex-parser.d.ts +3 -1
  52. package/regex-parser.js +2 -1
  53. package/regex-parser.js.map +1 -1
  54. package/async-iterator.polyfill.cjs +0 -87
  55. package/async-iterator.polyfill.d.ts +0 -2
  56. package/async-iterator.polyfill.js +0 -86
  57. package/async-iterator.polyfill.js.map +0 -1
package/index.d.ts CHANGED
@@ -1,4 +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';
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,6 +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
-
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';
6
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","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';\n"]}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"}
package/json-parse.cjs ADDED
@@ -0,0 +1,18 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Safely parse JSON string, returning undefined on failure.
5
+ * @param value - JSON string to parse
6
+ * @returns Parsed value or undefined if parsing fails
7
+ */
8
+ function jsonParse(value) {
9
+ try {
10
+ return JSON.parse(value);
11
+ }
12
+ catch {
13
+ return undefined;
14
+ }
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 };
@@ -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.js ADDED
@@ -0,0 +1,16 @@
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
+ function jsonParse(value) {
7
+ try {
8
+ return JSON.parse(value);
9
+ }
10
+ catch {
11
+ return undefined;
12
+ }
13
+ }
14
+
15
+ export { jsonParse };
16
+ //# sourceMappingURL=json-parse.js.map
@@ -0,0 +1 @@
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,17 +1,37 @@
1
1
  {
2
2
  "name": "@telperion/reactive-storage",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
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
+ "keywords": [
6
+ "rxjs",
7
+ "reactive",
8
+ "storage",
9
+ "localstorage",
10
+ "sessionstorage",
11
+ "cookies",
12
+ "observable",
13
+ "browser-storage",
14
+ "typescript",
15
+ "immutable",
16
+ "web-storage",
17
+ "reactive-programming",
18
+ "state-management",
19
+ "async-storage",
20
+ "cookie-storage",
21
+ "storage-sync"
22
+ ],
4
23
  "dependencies": {
5
24
  "tslib": "^2.3.0",
6
25
  "rxjs": "^7.8.1",
7
- "@thalesrc/js-utils": "^3.3.1",
8
- "@thalesrc/rx-utils": "^1.0.0",
9
- "@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"
10
29
  },
11
30
  "type": "module",
12
31
  "module": "./index.js",
13
32
  "main": "./index.cjs",
14
33
  "typings": "./index.d.ts",
34
+ "sideEffects": false,
15
35
  "bugs": "https://github.com/telperiontech/telperion/issues",
16
36
  "license": "MIT",
17
37
  "homepage": "https://open-source.telperion.tr",
@@ -30,39 +50,85 @@
30
50
  },
31
51
  "exports": {
32
52
  ".": {
33
- "default": "./index.cjs",
53
+ "types": {
54
+ "import": "./index.d.ts",
55
+ "require": "./index.d.cts"
56
+ },
34
57
  "import": "./index.js",
35
- "node": "./index.cjs",
36
58
  "require": "./index.cjs",
37
- "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"
38
81
  },
39
82
  "./reactive-storage": {
40
- "default": "./reactive-storage.cjs",
83
+ "types": {
84
+ "import": "./reactive-storage.d.ts",
85
+ "require": "./reactive-storage.d.cts"
86
+ },
41
87
  "import": "./reactive-storage.js",
42
- "node": "./reactive-storage.cjs",
43
88
  "require": "./reactive-storage.cjs",
44
- "types": "./reactive-storage.d.ts"
45
- },
46
- "./reactive-web-storage": {
47
- "default": "./reactive-web-storage.cjs",
48
- "import": "./reactive-web-storage.js",
49
- "node": "./reactive-web-storage.cjs",
50
- "require": "./reactive-web-storage.cjs",
51
- "types": "./reactive-web-storage.d.ts"
89
+ "node": "./reactive-storage.cjs",
90
+ "default": "./reactive-storage.js"
52
91
  },
53
92
  "./reactive-web-local-storage": {
54
- "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
+ },
55
97
  "import": "./reactive-web-local-storage.js",
56
- "node": "./reactive-web-local-storage.cjs",
57
98
  "require": "./reactive-web-local-storage.cjs",
58
- "types": "./reactive-web-local-storage.d.ts"
99
+ "node": "./reactive-web-local-storage.cjs",
100
+ "default": "./reactive-web-local-storage.js"
59
101
  },
60
102
  "./reactive-web-session-storage": {
61
- "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
+ },
62
107
  "import": "./reactive-web-session-storage.js",
63
- "node": "./reactive-web-session-storage.cjs",
64
108
  "require": "./reactive-web-session-storage.cjs",
65
- "types": "./reactive-web-session-storage.d.ts"
109
+ "node": "./reactive-web-session-storage.cjs",
110
+ "default": "./reactive-web-session-storage.js"
111
+ },
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"
66
131
  }
67
- }
68
- }
132
+ },
133
+ "types": "./index.d.ts"
134
+ }
@@ -0,0 +1,217 @@
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
+
8
+ var _a;
9
+ /**
10
+ * Reactive storage on `document.cookie`: for small values the server should see too, or that must expire.
11
+ *
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`.
15
+ *
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
+ *
21
+ * Example usage:
22
+ * ```ts
23
+ * import { ReactiveCookieStorage } from '@telperion/reactive-storage/reactive-cookie-storage';
24
+ *
25
+ * const storage = new ReactiveCookieStorage('my-app', { path: '/', maxAge: 60 * 60 * 24, sameSite: 'strict' });
26
+ *
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);
35
+ *
36
+ * header.get('prefs', 'theme').subscribe(console.log);
37
+ * await settings.set('prefs', 'theme', 'dark'); // header logs "dark"
38
+ * ```
39
+ *
40
+ * @template S - Store name type: a union of string literals restricts the store names you can use
41
+ */
42
+ class ReactiveCookieStorage extends reactiveStorage.ReactiveStorage {
43
+ appName;
44
+ options;
45
+ static #MAX_COOKIE_SIZE = 4000; // Conservative 4KB limit
46
+ static #LINKERS = new Map();
47
+ /**
48
+ * Safely parse JSON with error handling.
49
+ * Returns undefined if parsing fails.
50
+ */
51
+ static #parse(value) {
52
+ return jsonParse.jsonParse(value);
53
+ }
54
+ #changes$ = new rxjs.Subject();
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
+ */
61
+ constructor(appName = 'app', options = {}, linker) {
62
+ super();
63
+ this.appName = appName;
64
+ this.options = options;
65
+ // Register to linker group if provided
66
+ if (linker) {
67
+ this.#linker = linker;
68
+ if (!_a.#LINKERS.has(linker)) {
69
+ _a.#LINKERS.set(linker, new Set());
70
+ }
71
+ _a.#LINKERS.get(linker).add(this);
72
+ }
73
+ }
74
+ /**
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.
77
+ */
78
+ destroy() {
79
+ // Unregister from linker group
80
+ if (this.#linker) {
81
+ const linkedInstances = _a.#LINKERS.get(this.#linker);
82
+ if (linkedInstances) {
83
+ linkedInstances.delete(this);
84
+ if (linkedInstances.size === 0) {
85
+ _a.#LINKERS.delete(this.#linker);
86
+ }
87
+ }
88
+ }
89
+ this.#changes$.complete();
90
+ }
91
+ async [reactiveStorage.ReactiveStorage.Props.GET_ALL]() {
92
+ const cookies = this.#getAppCookies();
93
+ return Object.fromEntries(Object.entries(cookies).map(([storeName, value]) => [storeName, _a.#parse(value)]));
94
+ }
95
+ // Prefer an Observable's own async iterator (rxjs 8, or `@telperion/rx-utils/static/async-iterator`)
96
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
97
+ [reactiveStorage.ReactiveStorage.Props.CHANGES] = this.#changes$[Symbol.asyncIterator]?.() ?? toAsyncIteratable.makeAsyncIterator(this.#changes$);
98
+ async [reactiveStorage.ReactiveStorage.Props.SET](storeName, value) {
99
+ const serialized = JSON.stringify(value);
100
+ const cookieName = this.#getCookieName(storeName);
101
+ // Validate size before setting
102
+ const cookieString = `${cookieName}=${encodeURIComponent(serialized)}`;
103
+ this.#validateSize(cookieString);
104
+ this.#setCookie(cookieName, serialized); // Uses instance options
105
+ const event = { storeName, value };
106
+ this.#changes$.next(event);
107
+ this.#notifyLinkedInstances(event);
108
+ }
109
+ async [reactiveStorage.ReactiveStorage.Props.REMOVE](storeName) {
110
+ const cookieName = this.#getCookieName(storeName);
111
+ // Delete cookie by setting max-age to 0
112
+ this.#setCookie(cookieName, '', { maxAge: 0 });
113
+ const event = { storeName, value: undefined };
114
+ this.#changes$.next(event);
115
+ this.#notifyLinkedInstances(event);
116
+ }
117
+ /**
118
+ * Convert store name to cookie name by prefixing with app name.
119
+ * Sanitizes to only allow alphanumeric, underscore, and hyphen.
120
+ */
121
+ #getCookieName(storeName) {
122
+ return `${this.appName}_${storeName}`.replace(/[^a-zA-Z0-9_-]/g, '_');
123
+ }
124
+ /**
125
+ * Parse document.cookie string into key-value object.
126
+ * Handles decoding of cookie values.
127
+ */
128
+ #parseCookies() {
129
+ if (!document.cookie) {
130
+ return {};
131
+ }
132
+ return Object.fromEntries(document.cookie
133
+ .split('; ')
134
+ .filter(Boolean)
135
+ .map((cookie) => {
136
+ const separatorIndex = cookie.indexOf('=');
137
+ if (separatorIndex === -1) {
138
+ return [cookie, ''];
139
+ }
140
+ const name = cookie.slice(0, separatorIndex);
141
+ const value = cookie.slice(separatorIndex + 1);
142
+ try {
143
+ return [name, decodeURIComponent(value)];
144
+ }
145
+ catch {
146
+ return [name, value]; // Return raw value if decode fails
147
+ }
148
+ }));
149
+ }
150
+ /**
151
+ * Get all cookies that belong to this app (matching appName prefix).
152
+ * Returns object with storeName -> encoded value.
153
+ */
154
+ #getAppCookies() {
155
+ const allCookies = this.#parseCookies();
156
+ const prefix = `${this.appName}_`;
157
+ const appCookies = {};
158
+ for (const [cookieName, value] of Object.entries(allCookies)) {
159
+ if (cookieName.startsWith(prefix)) {
160
+ const storeName = cookieName.slice(prefix.length);
161
+ appCookies[storeName] = value;
162
+ }
163
+ }
164
+ return appCookies;
165
+ }
166
+ /**
167
+ * Build and set a cookie with the instance's configured options.
168
+ * Pass options parameter to override specific settings (e.g., maxAge: 0 to delete).
169
+ */
170
+ #setCookie(name, value, overrides = {}) {
171
+ const opts = { ...this.options, ...overrides };
172
+ let cookie = `${name}=${encodeURIComponent(value)}`;
173
+ if (opts.path !== undefined)
174
+ cookie += `; path=${opts.path}`;
175
+ if (opts.domain)
176
+ cookie += `; domain=${opts.domain}`;
177
+ if (opts.maxAge !== undefined)
178
+ cookie += `; max-age=${opts.maxAge}`;
179
+ if (opts.expires)
180
+ cookie += `; expires=${opts.expires.toUTCString()}`;
181
+ if (opts.secure)
182
+ cookie += '; secure';
183
+ if (opts.sameSite)
184
+ cookie += `; samesite=${opts.sameSite}`;
185
+ document.cookie = cookie;
186
+ }
187
+ /**
188
+ * Validate that a cookie string doesn't exceed size limits.
189
+ * Throws an error if the cookie is too large.
190
+ */
191
+ #validateSize(cookieString) {
192
+ if (cookieString.length > _a.#MAX_COOKIE_SIZE) {
193
+ throw new Error(`Cookie size (${cookieString.length} bytes) exceeds ${_a.#MAX_COOKIE_SIZE} byte limit. ` +
194
+ `Consider using localStorage for larger data or splitting across multiple stores.`);
195
+ }
196
+ }
197
+ /**
198
+ * Notify all other linked storage instances about a change.
199
+ * This instance will not be notified (to prevent infinite loops).
200
+ */
201
+ #notifyLinkedInstances(event) {
202
+ if (!this.#linker)
203
+ return;
204
+ const linkedInstances = _a.#LINKERS.get(this.#linker);
205
+ if (!linkedInstances)
206
+ return;
207
+ for (const instance of linkedInstances) {
208
+ if (instance !== this) {
209
+ instance.#changes$.next(event);
210
+ }
211
+ }
212
+ }
213
+ }
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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgC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,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>` (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 };
@@ -0,0 +1,77 @@
1
+ import { ReactiveStorage, ReactiveStorageChangeEvent } from './reactive-storage.js';
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 };