@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.
- package/.build-manifest.json +96 -0
- package/fesm/reactive-storage.mjs +620 -0
- package/fesm/reactive-storage.mjs.map +1 -0
- package/index.cjs +17 -8
- package/index.cjs.map +1 -0
- package/index.d.cts +6 -0
- package/index.d.ts +6 -5
- package/index.js +5 -6
- package/index.js.map +1 -1
- package/json-parse.cjs +5 -3
- package/json-parse.cjs.map +1 -0
- package/json-parse.d.cts +8 -0
- package/json-parse.d.ts +3 -1
- package/json-parse.js +2 -1
- package/json-parse.js.map +1 -1
- package/package.json +70 -31
- package/reactive-cookie-storage.cjs +49 -44
- package/reactive-cookie-storage.cjs.map +1 -0
- package/reactive-cookie-storage.d.cts +77 -0
- package/reactive-cookie-storage.d.ts +48 -40
- package/reactive-cookie-storage.js +40 -35
- package/reactive-cookie-storage.js.map +1 -1
- 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 +39 -23
- package/reactive-web-storage.cjs.map +1 -0
- package/reactive-web-storage.d.cts +47 -0
- package/reactive-web-storage.d.ts +28 -10
- package/reactive-web-storage.js +29 -13
- 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
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
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
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
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
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,"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"}
|
package/json-parse.cjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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;;;;"}
|
package/json-parse.d.cts
ADDED
package/json-parse.d.ts
CHANGED
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
|
-
|
|
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":["
|
|
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.
|
|
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
|
-
"@
|
|
27
|
-
"@
|
|
28
|
-
"@
|
|
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
|
-
"
|
|
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
|
-
"
|
|
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
|
-
"
|
|
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
|
-
"
|
|
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
|
-
"
|
|
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
|
-
"
|
|
99
|
+
"node": "./reactive-web-local-storage.cjs",
|
|
100
|
+
"default": "./reactive-web-local-storage.js"
|
|
79
101
|
},
|
|
80
102
|
"./reactive-web-session-storage": {
|
|
81
|
-
"
|
|
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
|
-
"
|
|
109
|
+
"node": "./reactive-web-session-storage.cjs",
|
|
110
|
+
"default": "./reactive-web-session-storage.js"
|
|
86
111
|
},
|
|
87
|
-
"./reactive-
|
|
88
|
-
"
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
"
|
|
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
|
-
|
|
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
|
|
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
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
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
|
-
*
|
|
19
|
-
*
|
|
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
|
-
*
|
|
21
|
+
* Example usage:
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { ReactiveCookieStorage } from '@telperion/reactive-storage/reactive-cookie-storage';
|
|
22
24
|
*
|
|
23
|
-
*
|
|
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
|
-
* //
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
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
|
-
*
|
|
40
|
-
*
|
|
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
|
|
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
|
|
52
|
+
return jsonParse.jsonParse(value);
|
|
55
53
|
}
|
|
56
|
-
#changes$ = new
|
|
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
|
-
*
|
|
73
|
-
*
|
|
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 [
|
|
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
|
-
[
|
|
95
|
-
async [
|
|
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 [
|
|
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],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ACnE,IAAI,IAAI,CAAC,OAAO;YAAE,MAAM,IAAI,aAAa,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE,CAAA,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>` (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
|
-
*
|
|
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
|
-
|
|
6
|
-
/** URL path
|
|
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
|
|
11
|
+
/** Domain the cookie is sent to, e.g. `'example.com'` to include subdomains. Unset: the current host only */
|
|
9
12
|
domain?: string;
|
|
10
|
-
/**
|
|
13
|
+
/** Lifetime in seconds. With neither `maxAge` nor `expires`, it is a session cookie */
|
|
11
14
|
maxAge?: number;
|
|
12
|
-
/** Absolute
|
|
15
|
+
/** Absolute expiry date. With neither `maxAge` nor `expires`, it is a session cookie */
|
|
13
16
|
expires?: Date;
|
|
14
|
-
/**
|
|
17
|
+
/** Send the cookie over HTTPS only */
|
|
15
18
|
secure?: boolean;
|
|
16
|
-
/**
|
|
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
|
|
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
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
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
|
-
*
|
|
29
|
-
*
|
|
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
|
-
*
|
|
34
|
+
* Example usage:
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { ReactiveCookieStorage } from '@telperion/reactive-storage/reactive-cookie-storage';
|
|
32
37
|
*
|
|
33
|
-
*
|
|
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
|
-
* //
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
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
|
-
*
|
|
50
|
-
*
|
|
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
|
-
|
|
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
|
-
*
|
|
61
|
-
*
|
|
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]:
|
|
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 };
|