@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.
- package/.build-manifest.json +96 -0
- package/README.md +172 -1
- package/fesm/reactive-storage.mjs +620 -0
- package/fesm/reactive-storage.mjs.map +1 -0
- package/index.cjs +17 -7
- package/index.cjs.map +1 -0
- package/index.d.cts +6 -0
- package/index.d.ts +6 -4
- package/index.js +5 -5
- package/index.js.map +1 -1
- package/json-parse.cjs +18 -0
- package/json-parse.cjs.map +1 -0
- package/json-parse.d.cts +8 -0
- package/json-parse.d.ts +8 -0
- package/json-parse.js +16 -0
- package/json-parse.js.map +1 -0
- package/package.json +91 -25
- package/reactive-cookie-storage.cjs +217 -0
- package/reactive-cookie-storage.cjs.map +1 -0
- package/reactive-cookie-storage.d.cts +77 -0
- package/reactive-cookie-storage.d.ts +77 -0
- package/reactive-cookie-storage.js +215 -0
- package/reactive-cookie-storage.js.map +1 -0
- package/reactive-storage.cjs +127 -74
- package/reactive-storage.cjs.map +1 -0
- package/reactive-storage.d.cts +144 -0
- package/reactive-storage.d.ts +87 -27
- package/reactive-storage.js +84 -31
- package/reactive-storage.js.map +1 -1
- package/reactive-web-local-storage.cjs +31 -8
- package/reactive-web-local-storage.cjs.map +1 -0
- package/reactive-web-local-storage.d.cts +38 -0
- package/reactive-web-local-storage.d.ts +31 -5
- package/reactive-web-local-storage.js +28 -5
- package/reactive-web-local-storage.js.map +1 -1
- package/reactive-web-session-storage.cjs +24 -8
- package/reactive-web-session-storage.cjs.map +1 -0
- package/reactive-web-session-storage.d.cts +31 -0
- package/reactive-web-session-storage.d.ts +24 -5
- package/reactive-web-session-storage.js +21 -5
- package/reactive-web-session-storage.js.map +1 -1
- package/reactive-web-storage.cjs +41 -29
- package/reactive-web-storage.cjs.map +1 -0
- package/reactive-web-storage.d.cts +47 -0
- package/reactive-web-storage.d.ts +28 -11
- package/reactive-web-storage.js +32 -20
- package/reactive-web-storage.js.map +1 -1
- package/regex-parser.cjs +4 -3
- package/regex-parser.cjs.map +1 -0
- package/regex-parser.d.cts +13 -0
- package/regex-parser.d.ts +3 -1
- package/regex-parser.js +2 -1
- package/regex-parser.js.map +1 -1
- package/async-iterator.polyfill.cjs +0 -87
- package/async-iterator.polyfill.d.ts +0 -2
- package/async-iterator.polyfill.js +0 -86
- package/async-iterator.polyfill.js.map +0 -1
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
{
|
|
2
|
+
"schemaVersion": 1,
|
|
3
|
+
"name": "reactive-storage",
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"formats": [
|
|
6
|
+
"esm",
|
|
7
|
+
"esmFlat",
|
|
8
|
+
"cjs",
|
|
9
|
+
"dts",
|
|
10
|
+
"dcts"
|
|
11
|
+
],
|
|
12
|
+
"entries": [
|
|
13
|
+
{
|
|
14
|
+
"name": "index",
|
|
15
|
+
"sourcePath": "src/index.ts",
|
|
16
|
+
"outputs": {
|
|
17
|
+
"esm": "index.js",
|
|
18
|
+
"esmFlat": "fesm/reactive-storage.mjs",
|
|
19
|
+
"cjs": "index.cjs",
|
|
20
|
+
"dts": "index.d.ts",
|
|
21
|
+
"dcts": "index.d.cts"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
"name": "json-parse",
|
|
26
|
+
"sourcePath": "src/json-parse.ts",
|
|
27
|
+
"outputs": {
|
|
28
|
+
"esm": "json-parse.js",
|
|
29
|
+
"cjs": "json-parse.cjs",
|
|
30
|
+
"dts": "json-parse.d.ts",
|
|
31
|
+
"dcts": "json-parse.d.cts"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
"name": "reactive-cookie-storage",
|
|
36
|
+
"sourcePath": "src/reactive-cookie-storage.ts",
|
|
37
|
+
"outputs": {
|
|
38
|
+
"esm": "reactive-cookie-storage.js",
|
|
39
|
+
"cjs": "reactive-cookie-storage.cjs",
|
|
40
|
+
"dts": "reactive-cookie-storage.d.ts",
|
|
41
|
+
"dcts": "reactive-cookie-storage.d.cts"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
"name": "reactive-storage",
|
|
46
|
+
"sourcePath": "src/reactive-storage.ts",
|
|
47
|
+
"outputs": {
|
|
48
|
+
"esm": "reactive-storage.js",
|
|
49
|
+
"cjs": "reactive-storage.cjs",
|
|
50
|
+
"dts": "reactive-storage.d.ts",
|
|
51
|
+
"dcts": "reactive-storage.d.cts"
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"name": "reactive-web-local-storage",
|
|
56
|
+
"sourcePath": "src/reactive-web-local-storage.ts",
|
|
57
|
+
"outputs": {
|
|
58
|
+
"esm": "reactive-web-local-storage.js",
|
|
59
|
+
"cjs": "reactive-web-local-storage.cjs",
|
|
60
|
+
"dts": "reactive-web-local-storage.d.ts",
|
|
61
|
+
"dcts": "reactive-web-local-storage.d.cts"
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
"name": "reactive-web-session-storage",
|
|
66
|
+
"sourcePath": "src/reactive-web-session-storage.ts",
|
|
67
|
+
"outputs": {
|
|
68
|
+
"esm": "reactive-web-session-storage.js",
|
|
69
|
+
"cjs": "reactive-web-session-storage.cjs",
|
|
70
|
+
"dts": "reactive-web-session-storage.d.ts",
|
|
71
|
+
"dcts": "reactive-web-session-storage.d.cts"
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
"name": "reactive-web-storage",
|
|
76
|
+
"sourcePath": "src/reactive-web-storage.ts",
|
|
77
|
+
"outputs": {
|
|
78
|
+
"esm": "reactive-web-storage.js",
|
|
79
|
+
"cjs": "reactive-web-storage.cjs",
|
|
80
|
+
"dts": "reactive-web-storage.d.ts",
|
|
81
|
+
"dcts": "reactive-web-storage.d.cts"
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
"name": "regex-parser",
|
|
86
|
+
"sourcePath": "src/regex-parser.ts",
|
|
87
|
+
"outputs": {
|
|
88
|
+
"esm": "regex-parser.js",
|
|
89
|
+
"cjs": "regex-parser.cjs",
|
|
90
|
+
"dts": "regex-parser.d.ts",
|
|
91
|
+
"dcts": "regex-parser.d.cts"
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
],
|
|
95
|
+
"generatedAt": "2026-11-19T09:39:42.688Z"
|
|
96
|
+
}
|
package/README.md
CHANGED
|
@@ -84,6 +84,78 @@ const storage = new ReactiveWebStorage(
|
|
|
84
84
|
);
|
|
85
85
|
```
|
|
86
86
|
|
|
87
|
+
#### `ReactiveCookieStorage<S extends string>`
|
|
88
|
+
|
|
89
|
+
Reactive wrapper for browser cookies. Provides cookie-specific features including expiration control and optional polling for external changes.
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
const storage = new ReactiveCookieStorage(
|
|
93
|
+
appName?: string,
|
|
94
|
+
options?: ReactiveCookieStorageOptions
|
|
95
|
+
);
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
**Parameters:**
|
|
99
|
+
- `appName` (optional): Application namespace (default: `'app'`). Used to prefix cookie names.
|
|
100
|
+
- `options` (optional): Cookie configuration options
|
|
101
|
+
|
|
102
|
+
**Options:**
|
|
103
|
+
- `path` (string): URL path where cookie is valid. Default: `'/'`
|
|
104
|
+
- `domain` (string): Domain where cookie is valid. Default: current domain
|
|
105
|
+
- `maxAge` (number): Cookie lifetime in seconds. Default: `undefined` (session cookie)
|
|
106
|
+
- `expires` (Date): Absolute expiration date. Default: `undefined`
|
|
107
|
+
- `secure` (boolean): HTTPS only flag. Default: `false`
|
|
108
|
+
- `sameSite` ('strict' | 'lax' | 'none'): CSRF protection level. Default: `'lax'`
|
|
109
|
+
|
|
110
|
+
**Third Parameter (optional):**
|
|
111
|
+
- `linker` (any): Value to link multiple instances for cross-instance synchronization. Can be a Symbol, string, number, object, or any other value.
|
|
112
|
+
|
|
113
|
+
**Important Limitations:**
|
|
114
|
+
- **Size Limit**: Cookies are limited to ~4KB each. The library enforces a 4000 byte limit and throws an error if exceeded.
|
|
115
|
+
- **Change Detection**: Cookies have no native change events. By default, only changes made through this instance are detected. Use a linker to synchronize multiple instances.
|
|
116
|
+
- **httpOnly Cookies**: Cannot access cookies set by the server with the httpOnly flag.
|
|
117
|
+
|
|
118
|
+
**Example:**
|
|
119
|
+
```typescript
|
|
120
|
+
import { ReactiveCookieStorage } from '@telperion/reactive-storage';
|
|
121
|
+
|
|
122
|
+
// Single instance
|
|
123
|
+
const storage = new ReactiveCookieStorage('my-app', {
|
|
124
|
+
path: '/',
|
|
125
|
+
maxAge: 86400, // 1 day
|
|
126
|
+
secure: true,
|
|
127
|
+
sameSite: 'strict'
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// Subscribe to changes
|
|
131
|
+
storage.get('preferences', 'theme').subscribe(theme => {
|
|
132
|
+
console.log('Theme:', theme);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// Set with default options
|
|
136
|
+
await storage.set('preferences', '', { theme: 'dark' });
|
|
137
|
+
|
|
138
|
+
// Multiple linked instances for cross-instance sync
|
|
139
|
+
const linker = Symbol('cookie-sync'); // Can also use strings, numbers, objects, etc.
|
|
140
|
+
const storageOne = new ReactiveCookieStorage('my-app', {
|
|
141
|
+
path: '/',
|
|
142
|
+
maxAge: 86400,
|
|
143
|
+
secure: true,
|
|
144
|
+
sameSite: 'strict'
|
|
145
|
+
}, linker);
|
|
146
|
+
|
|
147
|
+
const storageTwo = new ReactiveCookieStorage('my-app', {
|
|
148
|
+
path: '/',
|
|
149
|
+
maxAge: 86400,
|
|
150
|
+
secure: false,
|
|
151
|
+
sameSite: 'lax'
|
|
152
|
+
}, linker);
|
|
153
|
+
|
|
154
|
+
// Changes in storageOne will automatically notify storageTwo
|
|
155
|
+
await storageOne.set('user', '', { name: 'John' });
|
|
156
|
+
// storageTwo subscribers will be notified
|
|
157
|
+
```
|
|
158
|
+
|
|
87
159
|
### Methods
|
|
88
160
|
|
|
89
161
|
All methods are available on storage instances:
|
|
@@ -268,6 +340,91 @@ storage.get<User>('user').subscribe(user => {
|
|
|
268
340
|
// storage.get('invalid-store'); ❌
|
|
269
341
|
```
|
|
270
342
|
|
|
343
|
+
### Working with Cookies
|
|
344
|
+
|
|
345
|
+
Cookies provide unique features like expiration control and HTTP transmission. Cookie options are configured at instance creation time.
|
|
346
|
+
|
|
347
|
+
```typescript
|
|
348
|
+
import { ReactiveCookieStorage } from '@telperion/reactive-storage';
|
|
349
|
+
|
|
350
|
+
// Session cookie (no expiration)
|
|
351
|
+
const sessionStorage = new ReactiveCookieStorage('my-app', {
|
|
352
|
+
path: '/',
|
|
353
|
+
secure: true,
|
|
354
|
+
sameSite: 'strict'
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
await sessionStorage.set('session', '', {
|
|
358
|
+
userId: '123',
|
|
359
|
+
token: 'abc'
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
// Persistent cookie with 1 week expiration
|
|
363
|
+
const persistentStorage = new ReactiveCookieStorage('my-app', {
|
|
364
|
+
path: '/',
|
|
365
|
+
maxAge: 604800, // 7 days in seconds
|
|
366
|
+
secure: true,
|
|
367
|
+
sameSite: 'strict'
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
await persistentStorage.set('remember', '', { username: 'john' });
|
|
371
|
+
|
|
372
|
+
// Cookie with absolute expiration date
|
|
373
|
+
const tempStorage = new ReactiveCookieStorage('my-app', {
|
|
374
|
+
path: '/',
|
|
375
|
+
expires: new Date('2026-12-31'),
|
|
376
|
+
secure: true,
|
|
377
|
+
sameSite: 'strict'
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
await tempStorage.set('temp', '', { data: 'value' });
|
|
381
|
+
|
|
382
|
+
// Cross-instance synchronization using linker (any value works)
|
|
383
|
+
const linker = Symbol('my-app-sync'); // Symbols, strings, objects, etc.
|
|
384
|
+
|
|
385
|
+
const storageOne = new ReactiveCookieStorage('my-app', {
|
|
386
|
+
path: '/',
|
|
387
|
+
secure: true,
|
|
388
|
+
sameSite: 'strict'
|
|
389
|
+
}, linker);
|
|
390
|
+
|
|
391
|
+
const storageTwo = new ReactiveCookieStorage('my-app', {
|
|
392
|
+
path: '/',
|
|
393
|
+
secure: false,
|
|
394
|
+
sameSite: 'lax'
|
|
395
|
+
}, linker);
|
|
396
|
+
|
|
397
|
+
// Subscribe in second instance
|
|
398
|
+
storageTwo.get('preferences', 'theme').subscribe(theme => {
|
|
399
|
+
// Will receive updates from both storageOne and storageTwo
|
|
400
|
+
console.log('Theme changed:', theme);
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
// Set in first instance - will notify second instance
|
|
404
|
+
await storageOne.set('preferences', '', { theme: 'dark' });
|
|
405
|
+
|
|
406
|
+
// Size limit handling
|
|
407
|
+
try {
|
|
408
|
+
const largeData = { items: new Array(1000).fill('data') };
|
|
409
|
+
await storage.set('large', '', largeData);
|
|
410
|
+
} catch (error) {
|
|
411
|
+
console.error('Cookie too large:', error.message);
|
|
412
|
+
// Use localStorage for large data instead
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// Cleanup when done
|
|
416
|
+
storageOne.destroy(); // Unregisters from linker group
|
|
417
|
+
storageTwo.destroy(); // Unregisters from linker group
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
**Cookie Best Practices:**
|
|
421
|
+
- Use cookies for small, security-sensitive data (auth tokens, preferences)
|
|
422
|
+
- Use `secure: true` in production for HTTPS
|
|
423
|
+
- Use `sameSite: 'strict'` for CSRF protection
|
|
424
|
+
- Use a linker (symbol, string, etc.) when you need multiple instances with different configurations
|
|
425
|
+
- Call `destroy()` when disposing instances to prevent memory leaks
|
|
426
|
+
- Use localStorage for data > 4KB
|
|
427
|
+
|
|
271
428
|
### Integration with Framework-Specific Wrappers
|
|
272
429
|
|
|
273
430
|
This library serves as the foundation for framework-specific integrations:
|
|
@@ -277,16 +434,30 @@ This library serves as the foundation for framework-specific integrations:
|
|
|
277
434
|
|
|
278
435
|
## How It Works
|
|
279
436
|
|
|
437
|
+
### Web Storage (localStorage/sessionStorage)
|
|
438
|
+
|
|
280
439
|
1. **Storage Namespacing**: Data is stored with app-prefixed keys (e.g., `my-app/user`)
|
|
281
440
|
2. **JSON Serialization**: All data is serialized to JSON for storage compatibility
|
|
282
441
|
3. **Change Tracking**: Internal Subject emits changes that update all subscribers
|
|
283
442
|
4. **FIFO Queue**: Write operations are queued using `@FifoPromise` decorator to prevent race conditions
|
|
284
443
|
5. **Immutability**: All emitted values are deep-frozen to prevent accidental mutations
|
|
444
|
+
6. **Native Events**: localStorage uses the storage event for automatic cross-tab synchronization
|
|
445
|
+
|
|
446
|
+
### Cookie Storage
|
|
447
|
+
|
|
448
|
+
1. **Cookie Namespacing**: Cookie names are prefixed with app name (e.g., `my-app_user`)
|
|
449
|
+
2. **JSON Serialization + Encoding**: Data is JSON-stringified then URL-encoded for cookie compatibility
|
|
450
|
+
3. **Change Tracking**: RxJS Subject emits changes to this instance and linked instances
|
|
451
|
+
4. **FIFO Queue**: Write operations are queued using `@FifoPromise` decorator to prevent race conditions
|
|
452
|
+
5. **Immutability**: All emitted values are deep-frozen to prevent accidental mutations
|
|
453
|
+
6. **Size Validation**: Enforces 4KB limit before setting cookies
|
|
454
|
+
7. **Cross-Instance Sync**: Optional linker (any value) enables synchronous change notification between instances
|
|
285
455
|
|
|
286
456
|
## Browser Compatibility
|
|
287
457
|
|
|
288
458
|
Works in all modern browsers that support:
|
|
289
|
-
- Web Storage API (localStorage/sessionStorage)
|
|
459
|
+
- Web Storage API (localStorage/sessionStorage) - for web storage classes
|
|
460
|
+
- Cookie API (document.cookie) - for cookie storage class
|
|
290
461
|
- ES2015+ features
|
|
291
462
|
- RxJS 7+
|
|
292
463
|
|