@merkur/plugin-session-storage 0.32.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/LICENSE +7 -0
- package/README.md +47 -0
- package/lib/index.cjs +131 -0
- package/lib/index.es9.cjs +109 -0
- package/lib/index.es9.mjs +106 -0
- package/lib/index.js +131 -0
- package/lib/index.mjs +128 -0
- package/lib/index.umd.js +1 -0
- package/package.json +57 -0
- package/rollup.config.mjs +11 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2023 Miroslav Jancarik jancarikmiroslav@gmail.com
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://merkur.js.org/docs/getting-started" title="Getting started">
|
|
3
|
+
<img src="https://raw.githubusercontent.com/mjancarik/merkur/master/images/merkur-illustration.png" width="100px" height="100px" alt="Merkur illustration"/>
|
|
4
|
+
</a>
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
# Merkur
|
|
8
|
+
|
|
9
|
+
[](https://github.com/mjancarik/merkur/actions/workflows/ci.yml)
|
|
10
|
+
[](https://www.npmjs.com/package/@merkur/core)
|
|
11
|
+

|
|
12
|
+
[](https://github.com/prettier/prettier)
|
|
13
|
+
|
|
14
|
+
The [Merkur](https://merkur.js.org/) is tiny extensible javascript library for front-end microservices(micro frontends). It allows by default server side rendering for loading performance boost. You can connect it with other frameworks or languages because merkur defines easy API. You can use one of six predefined template's library [Preact](https://preactjs.com/), [µhtml](https://github.com/WebReflection/uhtml#readme), [Svelte](https://svelte.dev/) and [vanilla](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) but you can easily extend for others.
|
|
15
|
+
|
|
16
|
+
## Features
|
|
17
|
+
- Flexible templating engine
|
|
18
|
+
- Usable with all tech stacks
|
|
19
|
+
- SSR-ready by default
|
|
20
|
+
- Easy extensible with plugins
|
|
21
|
+
- Tiny - 1 KB minified + gzipped
|
|
22
|
+
|
|
23
|
+
## Getting started
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npx @merkur/create-widget <name>
|
|
27
|
+
|
|
28
|
+
cd name
|
|
29
|
+
|
|
30
|
+
npm run dev // Point your browser at http://localhost:4444/
|
|
31
|
+
```
|
|
32
|
+

|
|
33
|
+
## Documentation
|
|
34
|
+
|
|
35
|
+
To check out [live demo](https://merkur.js.org/demo) and [docs](https://merkur.js.org/docs), visit [https://merkur.js.org](https://merkur.js.org).
|
|
36
|
+
|
|
37
|
+
## Contribution
|
|
38
|
+
|
|
39
|
+
Contribute to this project via [Pull-Requests](https://github.com/mjancarik/merkur/pulls).
|
|
40
|
+
|
|
41
|
+
We are following [Conventional Commits Specification](https://www.conventionalcommits.org/en/v1.0.0/#summary). To simplify the commit process, you can use `npm run commit` command. It opens an interactive interface, which should help you with commit message composition.
|
|
42
|
+
|
|
43
|
+
Thank you to all the people who already contributed to Merkur!
|
|
44
|
+
|
|
45
|
+
<a href="https://github.com/mjancarik/merkur/graphs/contributors">
|
|
46
|
+
<img src="https://contrib.rocks/image?repo=mjancarik/merkur" />
|
|
47
|
+
</a>
|
package/lib/index.cjs
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@merkur/core');
|
|
4
|
+
|
|
5
|
+
const KEY_PREFIX_SEPARATOR = '__';
|
|
6
|
+
|
|
7
|
+
function setKeyPrefix(
|
|
8
|
+
widget,
|
|
9
|
+
additionalWords = [],
|
|
10
|
+
defaultWords = ['widget', widget.name, widget.version]
|
|
11
|
+
) {
|
|
12
|
+
const words = [...defaultWords, ...additionalWords].filter(Boolean);
|
|
13
|
+
const prefix = `${KEY_PREFIX_SEPARATOR}${words.join(
|
|
14
|
+
KEY_PREFIX_SEPARATOR
|
|
15
|
+
)}${KEY_PREFIX_SEPARATOR}`;
|
|
16
|
+
|
|
17
|
+
widget.$in.sessionStorage.keyPrefix = prefix;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function sessionStoragePlugin() {
|
|
21
|
+
return {
|
|
22
|
+
async setup(widget) {
|
|
23
|
+
widget = {
|
|
24
|
+
...sessionStorageAPI(),
|
|
25
|
+
...widget,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
widget.$in.sessionStorage = {};
|
|
29
|
+
setKeyPrefix(widget);
|
|
30
|
+
|
|
31
|
+
widget.$dependencies.sessionStorage = getNativeSessionStorage();
|
|
32
|
+
|
|
33
|
+
return widget;
|
|
34
|
+
},
|
|
35
|
+
async create(widget) {
|
|
36
|
+
core.bindWidgetToFunctions(widget, widget.sessionStorage);
|
|
37
|
+
|
|
38
|
+
return widget;
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function sessionStorageAPI() {
|
|
44
|
+
return {
|
|
45
|
+
sessionStorage: {
|
|
46
|
+
get(widget, key) {
|
|
47
|
+
const {
|
|
48
|
+
$dependencies: { sessionStorage },
|
|
49
|
+
$in: {
|
|
50
|
+
sessionStorage: { keyPrefix },
|
|
51
|
+
},
|
|
52
|
+
} = widget;
|
|
53
|
+
|
|
54
|
+
if (!sessionStorage) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
return JSON.parse(sessionStorage.getItem(keyPrefix + key))?.value;
|
|
60
|
+
} catch (error) {
|
|
61
|
+
throw new Error(
|
|
62
|
+
`merkur.plugin-session-storage.get: Failed to parse a session storage item value identified by the key ${
|
|
63
|
+
keyPrefix + key
|
|
64
|
+
}: ${error.message}`
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Saves a value under the key.
|
|
71
|
+
* @param {object} widget A widget object.
|
|
72
|
+
* @param {string} key A key
|
|
73
|
+
* @param {*} value A value
|
|
74
|
+
* @return {boolean} It's `true` when the operation was successful,
|
|
75
|
+
* otherwise `false`.
|
|
76
|
+
*/
|
|
77
|
+
set(widget, key, value) {
|
|
78
|
+
const {
|
|
79
|
+
$dependencies: { sessionStorage },
|
|
80
|
+
$in: {
|
|
81
|
+
sessionStorage: { keyPrefix },
|
|
82
|
+
},
|
|
83
|
+
} = widget;
|
|
84
|
+
|
|
85
|
+
if (!sessionStorage) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
sessionStorage.setItem(
|
|
91
|
+
keyPrefix + key,
|
|
92
|
+
JSON.stringify({
|
|
93
|
+
created: Date.now(),
|
|
94
|
+
value,
|
|
95
|
+
})
|
|
96
|
+
);
|
|
97
|
+
} catch (error) {
|
|
98
|
+
console.error(error);
|
|
99
|
+
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return true;
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
delete(widget, key) {
|
|
107
|
+
const {
|
|
108
|
+
$dependencies: { sessionStorage },
|
|
109
|
+
$in: {
|
|
110
|
+
sessionStorage: { keyPrefix },
|
|
111
|
+
},
|
|
112
|
+
} = widget;
|
|
113
|
+
|
|
114
|
+
if (!sessionStorage) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
sessionStorage.removeItem(keyPrefix + key);
|
|
119
|
+
|
|
120
|
+
return true;
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function getNativeSessionStorage() {
|
|
127
|
+
return typeof window === 'undefined' ? undefined : window.sessionStorage;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
exports.sessionStoragePlugin = sessionStoragePlugin;
|
|
131
|
+
exports.setKeyPrefix = setKeyPrefix;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@merkur/core');
|
|
4
|
+
const KEY_PREFIX_SEPARATOR = '__';
|
|
5
|
+
function setKeyPrefix(widget, additionalWords = [], defaultWords = ['widget', widget.name, widget.version]) {
|
|
6
|
+
const words = [...defaultWords, ...additionalWords].filter(Boolean);
|
|
7
|
+
const prefix = `${KEY_PREFIX_SEPARATOR}${words.join(KEY_PREFIX_SEPARATOR)}${KEY_PREFIX_SEPARATOR}`;
|
|
8
|
+
widget.$in.sessionStorage.keyPrefix = prefix;
|
|
9
|
+
}
|
|
10
|
+
function sessionStoragePlugin() {
|
|
11
|
+
return {
|
|
12
|
+
async setup(widget) {
|
|
13
|
+
widget = {
|
|
14
|
+
...sessionStorageAPI(),
|
|
15
|
+
...widget
|
|
16
|
+
};
|
|
17
|
+
widget.$in.sessionStorage = {};
|
|
18
|
+
setKeyPrefix(widget);
|
|
19
|
+
widget.$dependencies.sessionStorage = getNativeSessionStorage();
|
|
20
|
+
return widget;
|
|
21
|
+
},
|
|
22
|
+
async create(widget) {
|
|
23
|
+
core.bindWidgetToFunctions(widget, widget.sessionStorage);
|
|
24
|
+
return widget;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function sessionStorageAPI() {
|
|
29
|
+
return {
|
|
30
|
+
sessionStorage: {
|
|
31
|
+
get(widget, key) {
|
|
32
|
+
const {
|
|
33
|
+
$dependencies: {
|
|
34
|
+
sessionStorage
|
|
35
|
+
},
|
|
36
|
+
$in: {
|
|
37
|
+
sessionStorage: {
|
|
38
|
+
keyPrefix
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
} = widget;
|
|
42
|
+
if (!sessionStorage) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
var _JSON$parse;
|
|
47
|
+
return (_JSON$parse = JSON.parse(sessionStorage.getItem(keyPrefix + key))) === null || _JSON$parse === void 0 ? void 0 : _JSON$parse.value;
|
|
48
|
+
} catch (error) {
|
|
49
|
+
throw new Error(`merkur.plugin-session-storage.get: Failed to parse a session storage item value identified by the key ${keyPrefix + key}: ${error.message}`);
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
/**
|
|
53
|
+
* Saves a value under the key.
|
|
54
|
+
* @param {object} widget A widget object.
|
|
55
|
+
* @param {string} key A key
|
|
56
|
+
* @param {*} value A value
|
|
57
|
+
* @return {boolean} It's `true` when the operation was successful,
|
|
58
|
+
* otherwise `false`.
|
|
59
|
+
*/
|
|
60
|
+
set(widget, key, value) {
|
|
61
|
+
const {
|
|
62
|
+
$dependencies: {
|
|
63
|
+
sessionStorage
|
|
64
|
+
},
|
|
65
|
+
$in: {
|
|
66
|
+
sessionStorage: {
|
|
67
|
+
keyPrefix
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
} = widget;
|
|
71
|
+
if (!sessionStorage) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
sessionStorage.setItem(keyPrefix + key, JSON.stringify({
|
|
76
|
+
created: Date.now(),
|
|
77
|
+
value
|
|
78
|
+
}));
|
|
79
|
+
} catch (error) {
|
|
80
|
+
console.error(error);
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
return true;
|
|
84
|
+
},
|
|
85
|
+
delete(widget, key) {
|
|
86
|
+
const {
|
|
87
|
+
$dependencies: {
|
|
88
|
+
sessionStorage
|
|
89
|
+
},
|
|
90
|
+
$in: {
|
|
91
|
+
sessionStorage: {
|
|
92
|
+
keyPrefix
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
} = widget;
|
|
96
|
+
if (!sessionStorage) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
sessionStorage.removeItem(keyPrefix + key);
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function getNativeSessionStorage() {
|
|
106
|
+
return typeof window === 'undefined' ? undefined : window.sessionStorage;
|
|
107
|
+
}
|
|
108
|
+
exports.sessionStoragePlugin = sessionStoragePlugin;
|
|
109
|
+
exports.setKeyPrefix = setKeyPrefix;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { bindWidgetToFunctions } from '@merkur/core';
|
|
2
|
+
const KEY_PREFIX_SEPARATOR = '__';
|
|
3
|
+
function setKeyPrefix(widget, additionalWords = [], defaultWords = ['widget', widget.name, widget.version]) {
|
|
4
|
+
const words = [...defaultWords, ...additionalWords].filter(Boolean);
|
|
5
|
+
const prefix = `${KEY_PREFIX_SEPARATOR}${words.join(KEY_PREFIX_SEPARATOR)}${KEY_PREFIX_SEPARATOR}`;
|
|
6
|
+
widget.$in.sessionStorage.keyPrefix = prefix;
|
|
7
|
+
}
|
|
8
|
+
function sessionStoragePlugin() {
|
|
9
|
+
return {
|
|
10
|
+
async setup(widget) {
|
|
11
|
+
widget = {
|
|
12
|
+
...sessionStorageAPI(),
|
|
13
|
+
...widget
|
|
14
|
+
};
|
|
15
|
+
widget.$in.sessionStorage = {};
|
|
16
|
+
setKeyPrefix(widget);
|
|
17
|
+
widget.$dependencies.sessionStorage = getNativeSessionStorage();
|
|
18
|
+
return widget;
|
|
19
|
+
},
|
|
20
|
+
async create(widget) {
|
|
21
|
+
bindWidgetToFunctions(widget, widget.sessionStorage);
|
|
22
|
+
return widget;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function sessionStorageAPI() {
|
|
27
|
+
return {
|
|
28
|
+
sessionStorage: {
|
|
29
|
+
get(widget, key) {
|
|
30
|
+
const {
|
|
31
|
+
$dependencies: {
|
|
32
|
+
sessionStorage
|
|
33
|
+
},
|
|
34
|
+
$in: {
|
|
35
|
+
sessionStorage: {
|
|
36
|
+
keyPrefix
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
} = widget;
|
|
40
|
+
if (!sessionStorage) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
var _JSON$parse;
|
|
45
|
+
return (_JSON$parse = JSON.parse(sessionStorage.getItem(keyPrefix + key))) === null || _JSON$parse === void 0 ? void 0 : _JSON$parse.value;
|
|
46
|
+
} catch (error) {
|
|
47
|
+
throw new Error(`merkur.plugin-session-storage.get: Failed to parse a session storage item value identified by the key ${keyPrefix + key}: ${error.message}`);
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
/**
|
|
51
|
+
* Saves a value under the key.
|
|
52
|
+
* @param {object} widget A widget object.
|
|
53
|
+
* @param {string} key A key
|
|
54
|
+
* @param {*} value A value
|
|
55
|
+
* @return {boolean} It's `true` when the operation was successful,
|
|
56
|
+
* otherwise `false`.
|
|
57
|
+
*/
|
|
58
|
+
set(widget, key, value) {
|
|
59
|
+
const {
|
|
60
|
+
$dependencies: {
|
|
61
|
+
sessionStorage
|
|
62
|
+
},
|
|
63
|
+
$in: {
|
|
64
|
+
sessionStorage: {
|
|
65
|
+
keyPrefix
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
} = widget;
|
|
69
|
+
if (!sessionStorage) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
sessionStorage.setItem(keyPrefix + key, JSON.stringify({
|
|
74
|
+
created: Date.now(),
|
|
75
|
+
value
|
|
76
|
+
}));
|
|
77
|
+
} catch (error) {
|
|
78
|
+
console.error(error);
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
return true;
|
|
82
|
+
},
|
|
83
|
+
delete(widget, key) {
|
|
84
|
+
const {
|
|
85
|
+
$dependencies: {
|
|
86
|
+
sessionStorage
|
|
87
|
+
},
|
|
88
|
+
$in: {
|
|
89
|
+
sessionStorage: {
|
|
90
|
+
keyPrefix
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
} = widget;
|
|
94
|
+
if (!sessionStorage) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
sessionStorage.removeItem(keyPrefix + key);
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function getNativeSessionStorage() {
|
|
104
|
+
return typeof window === 'undefined' ? undefined : window.sessionStorage;
|
|
105
|
+
}
|
|
106
|
+
export { sessionStoragePlugin, setKeyPrefix };
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@merkur/core');
|
|
4
|
+
|
|
5
|
+
const KEY_PREFIX_SEPARATOR = '__';
|
|
6
|
+
|
|
7
|
+
function setKeyPrefix(
|
|
8
|
+
widget,
|
|
9
|
+
additionalWords = [],
|
|
10
|
+
defaultWords = ['widget', widget.name, widget.version]
|
|
11
|
+
) {
|
|
12
|
+
const words = [...defaultWords, ...additionalWords].filter(Boolean);
|
|
13
|
+
const prefix = `${KEY_PREFIX_SEPARATOR}${words.join(
|
|
14
|
+
KEY_PREFIX_SEPARATOR
|
|
15
|
+
)}${KEY_PREFIX_SEPARATOR}`;
|
|
16
|
+
|
|
17
|
+
widget.$in.sessionStorage.keyPrefix = prefix;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function sessionStoragePlugin() {
|
|
21
|
+
return {
|
|
22
|
+
async setup(widget) {
|
|
23
|
+
widget = {
|
|
24
|
+
...sessionStorageAPI(),
|
|
25
|
+
...widget,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
widget.$in.sessionStorage = {};
|
|
29
|
+
setKeyPrefix(widget);
|
|
30
|
+
|
|
31
|
+
widget.$dependencies.sessionStorage = getNativeSessionStorage();
|
|
32
|
+
|
|
33
|
+
return widget;
|
|
34
|
+
},
|
|
35
|
+
async create(widget) {
|
|
36
|
+
core.bindWidgetToFunctions(widget, widget.sessionStorage);
|
|
37
|
+
|
|
38
|
+
return widget;
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function sessionStorageAPI() {
|
|
44
|
+
return {
|
|
45
|
+
sessionStorage: {
|
|
46
|
+
get(widget, key) {
|
|
47
|
+
const {
|
|
48
|
+
$dependencies: { sessionStorage },
|
|
49
|
+
$in: {
|
|
50
|
+
sessionStorage: { keyPrefix },
|
|
51
|
+
},
|
|
52
|
+
} = widget;
|
|
53
|
+
|
|
54
|
+
if (!sessionStorage) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
return JSON.parse(sessionStorage.getItem(keyPrefix + key))?.value;
|
|
60
|
+
} catch (error) {
|
|
61
|
+
throw new Error(
|
|
62
|
+
`merkur.plugin-session-storage.get: Failed to parse a session storage item value identified by the key ${
|
|
63
|
+
keyPrefix + key
|
|
64
|
+
}: ${error.message}`
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Saves a value under the key.
|
|
71
|
+
* @param {object} widget A widget object.
|
|
72
|
+
* @param {string} key A key
|
|
73
|
+
* @param {*} value A value
|
|
74
|
+
* @return {boolean} It's `true` when the operation was successful,
|
|
75
|
+
* otherwise `false`.
|
|
76
|
+
*/
|
|
77
|
+
set(widget, key, value) {
|
|
78
|
+
const {
|
|
79
|
+
$dependencies: { sessionStorage },
|
|
80
|
+
$in: {
|
|
81
|
+
sessionStorage: { keyPrefix },
|
|
82
|
+
},
|
|
83
|
+
} = widget;
|
|
84
|
+
|
|
85
|
+
if (!sessionStorage) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
sessionStorage.setItem(
|
|
91
|
+
keyPrefix + key,
|
|
92
|
+
JSON.stringify({
|
|
93
|
+
created: Date.now(),
|
|
94
|
+
value,
|
|
95
|
+
})
|
|
96
|
+
);
|
|
97
|
+
} catch (error) {
|
|
98
|
+
console.error(error);
|
|
99
|
+
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return true;
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
delete(widget, key) {
|
|
107
|
+
const {
|
|
108
|
+
$dependencies: { sessionStorage },
|
|
109
|
+
$in: {
|
|
110
|
+
sessionStorage: { keyPrefix },
|
|
111
|
+
},
|
|
112
|
+
} = widget;
|
|
113
|
+
|
|
114
|
+
if (!sessionStorage) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
sessionStorage.removeItem(keyPrefix + key);
|
|
119
|
+
|
|
120
|
+
return true;
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function getNativeSessionStorage() {
|
|
127
|
+
return typeof window === 'undefined' ? undefined : window.sessionStorage;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
exports.sessionStoragePlugin = sessionStoragePlugin;
|
|
131
|
+
exports.setKeyPrefix = setKeyPrefix;
|
package/lib/index.mjs
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { bindWidgetToFunctions } from '@merkur/core';
|
|
2
|
+
|
|
3
|
+
const KEY_PREFIX_SEPARATOR = '__';
|
|
4
|
+
|
|
5
|
+
function setKeyPrefix(
|
|
6
|
+
widget,
|
|
7
|
+
additionalWords = [],
|
|
8
|
+
defaultWords = ['widget', widget.name, widget.version]
|
|
9
|
+
) {
|
|
10
|
+
const words = [...defaultWords, ...additionalWords].filter(Boolean);
|
|
11
|
+
const prefix = `${KEY_PREFIX_SEPARATOR}${words.join(
|
|
12
|
+
KEY_PREFIX_SEPARATOR
|
|
13
|
+
)}${KEY_PREFIX_SEPARATOR}`;
|
|
14
|
+
|
|
15
|
+
widget.$in.sessionStorage.keyPrefix = prefix;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function sessionStoragePlugin() {
|
|
19
|
+
return {
|
|
20
|
+
async setup(widget) {
|
|
21
|
+
widget = {
|
|
22
|
+
...sessionStorageAPI(),
|
|
23
|
+
...widget,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
widget.$in.sessionStorage = {};
|
|
27
|
+
setKeyPrefix(widget);
|
|
28
|
+
|
|
29
|
+
widget.$dependencies.sessionStorage = getNativeSessionStorage();
|
|
30
|
+
|
|
31
|
+
return widget;
|
|
32
|
+
},
|
|
33
|
+
async create(widget) {
|
|
34
|
+
bindWidgetToFunctions(widget, widget.sessionStorage);
|
|
35
|
+
|
|
36
|
+
return widget;
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function sessionStorageAPI() {
|
|
42
|
+
return {
|
|
43
|
+
sessionStorage: {
|
|
44
|
+
get(widget, key) {
|
|
45
|
+
const {
|
|
46
|
+
$dependencies: { sessionStorage },
|
|
47
|
+
$in: {
|
|
48
|
+
sessionStorage: { keyPrefix },
|
|
49
|
+
},
|
|
50
|
+
} = widget;
|
|
51
|
+
|
|
52
|
+
if (!sessionStorage) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
try {
|
|
57
|
+
return JSON.parse(sessionStorage.getItem(keyPrefix + key))?.value;
|
|
58
|
+
} catch (error) {
|
|
59
|
+
throw new Error(
|
|
60
|
+
`merkur.plugin-session-storage.get: Failed to parse a session storage item value identified by the key ${
|
|
61
|
+
keyPrefix + key
|
|
62
|
+
}: ${error.message}`
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Saves a value under the key.
|
|
69
|
+
* @param {object} widget A widget object.
|
|
70
|
+
* @param {string} key A key
|
|
71
|
+
* @param {*} value A value
|
|
72
|
+
* @return {boolean} It's `true` when the operation was successful,
|
|
73
|
+
* otherwise `false`.
|
|
74
|
+
*/
|
|
75
|
+
set(widget, key, value) {
|
|
76
|
+
const {
|
|
77
|
+
$dependencies: { sessionStorage },
|
|
78
|
+
$in: {
|
|
79
|
+
sessionStorage: { keyPrefix },
|
|
80
|
+
},
|
|
81
|
+
} = widget;
|
|
82
|
+
|
|
83
|
+
if (!sessionStorage) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
try {
|
|
88
|
+
sessionStorage.setItem(
|
|
89
|
+
keyPrefix + key,
|
|
90
|
+
JSON.stringify({
|
|
91
|
+
created: Date.now(),
|
|
92
|
+
value,
|
|
93
|
+
})
|
|
94
|
+
);
|
|
95
|
+
} catch (error) {
|
|
96
|
+
console.error(error);
|
|
97
|
+
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return true;
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
delete(widget, key) {
|
|
105
|
+
const {
|
|
106
|
+
$dependencies: { sessionStorage },
|
|
107
|
+
$in: {
|
|
108
|
+
sessionStorage: { keyPrefix },
|
|
109
|
+
},
|
|
110
|
+
} = widget;
|
|
111
|
+
|
|
112
|
+
if (!sessionStorage) {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
sessionStorage.removeItem(keyPrefix + key);
|
|
117
|
+
|
|
118
|
+
return true;
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function getNativeSessionStorage() {
|
|
125
|
+
return typeof window === 'undefined' ? undefined : window.sessionStorage;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export { sessionStoragePlugin, setKeyPrefix };
|
package/lib/index.umd.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function e(r){return e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},e(r)}!function(e,r){if("function"==typeof define&&define.amd)define("@merkur/plugin-session-storage",["exports","@merkur/core"],r);else if("undefined"!=typeof exports)r(exports,require("@merkur/core"));else{var t={exports:{}};r(t.exports,e.Merkur.Core),e.merkurPluginSessionStorage=t.exports}}("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:this,(function(r,t){function n(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function o(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?n(Object(t),!0).forEach((function(r){i(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):n(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}function i(r,t,n){return(t=function(r){var t=function(r,t){if("object"!==e(r)||null===r)return r;var n=r[Symbol.toPrimitive];if(void 0!==n){var o=n.call(r,t||"default");if("object"!==e(o))return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(r)}(r,"string");return"symbol"===e(t)?t:String(t)}(t))in r?Object.defineProperty(r,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):r[t]=n,r}function s(e,r,t,n,o,i,s){try{var u=e[i](s),a=u.value}catch(e){return void t(e)}u.done?r(a):Promise.resolve(a).then(n,o)}function u(e){return function(){var r=this,t=arguments;return new Promise((function(n,o){var i=e.apply(r,t);function u(e){s(i,n,o,u,a,"next",e)}function a(e){s(i,n,o,u,a,"throw",e)}u(void 0)}))}}function a(e){return function(e){if(Array.isArray(e))return c(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||function(e,r){if(!e)return;if("string"==typeof e)return c(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);"Object"===t&&e.constructor&&(t=e.constructor.name);if("Map"===t||"Set"===t)return Array.from(e);if("Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t))return c(e,r)}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function c(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=new Array(r);t<r;t++)n[t]=e[t];return n}Object.defineProperty(r,"__esModule",{value:!0}),r.sessionStoragePlugin=function(){return{setup:function(e){return u((function*(){return(e=o(o({},{sessionStorage:{get:function(e,r){var t=e.$dependencies.sessionStorage,n=e.$in.sessionStorage.keyPrefix;if(!t)return null;try{var o;return null===(o=JSON.parse(t.getItem(n+r)))||void 0===o?void 0:o.value}catch(e){throw new Error("merkur.plugin-session-storage.get: Failed to parse a session storage item value identified by the key ".concat(n+r,": ").concat(e.message))}},set:function(e,r,t){var n=e.$dependencies.sessionStorage,o=e.$in.sessionStorage.keyPrefix;if(!n)return!1;try{n.setItem(o+r,JSON.stringify({created:Date.now(),value:t}))}catch(e){return console.error(e),!1}return!0},delete:function(e,r){var t=e.$dependencies.sessionStorage,n=e.$in.sessionStorage.keyPrefix;return!!t&&(t.removeItem(n+r),!0)}}}),e)).$in.sessionStorage={},l(e),e.$dependencies.sessionStorage="undefined"==typeof window?void 0:window.sessionStorage,e}))()},create:function(e){return u((function*(){return(0,t.bindWidgetToFunctions)(e,e.sessionStorage),e}))()}}},r.setKeyPrefix=l;var f="__";function l(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:["widget",e.name,e.version],n=[].concat(a(t),a(r)).filter(Boolean),o="".concat(f).concat(n.join(f)).concat(f);e.$in.sessionStorage.keyPrefix=o}}));
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@merkur/plugin-session-storage",
|
|
3
|
+
"version": "0.32.0",
|
|
4
|
+
"description": "Merkur session storage.",
|
|
5
|
+
"main": "lib/index",
|
|
6
|
+
"module": "lib/index",
|
|
7
|
+
"unpkg": "lib/index.umd.js",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./lib/index.mjs",
|
|
12
|
+
"require": "./lib/index.cjs"
|
|
13
|
+
},
|
|
14
|
+
"./lib/index.es9.mjs": "./lib/index.es9.mjs",
|
|
15
|
+
"./lib/index.es9.cjs": "./lib/index.es9.cjs"
|
|
16
|
+
},
|
|
17
|
+
"browser": {
|
|
18
|
+
"./lib/index.js": "./lib/index.js",
|
|
19
|
+
"./lib/index.cjs": "./lib/index.cjs",
|
|
20
|
+
"./lib/index.mjs": "./lib/index.mjs",
|
|
21
|
+
"./lib/index.es9.mjs": "./lib/index.es9.mjs"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"preversion": "npm test",
|
|
25
|
+
"test": "jest --no-watchman -c ./jest.config.js",
|
|
26
|
+
"test:es:version": "es-check es11 ./lib/index.mjs --module && es-check es9 ./lib/index.es9.mjs --module && es-check es9 ./lib/index.es9.cjs --module",
|
|
27
|
+
"build": "rollup -c rollup.config.mjs",
|
|
28
|
+
"prepare": "npm run build"
|
|
29
|
+
},
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "git+https://github.com/mjancarik/merkur.git"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"merkur",
|
|
36
|
+
"plugin",
|
|
37
|
+
"microservices",
|
|
38
|
+
"microfrontends"
|
|
39
|
+
],
|
|
40
|
+
"author": "Ondřej Baše",
|
|
41
|
+
"license": "ISC",
|
|
42
|
+
"bugs": {
|
|
43
|
+
"url": "https://github.com/mjancarik/merkur/issues"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"registry": "https://registry.npmjs.org/",
|
|
47
|
+
"access": "public"
|
|
48
|
+
},
|
|
49
|
+
"homepage": "https://merkur.js.org/",
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@merkur/core": "^0.32.0"
|
|
52
|
+
},
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"@merkur/core": "*"
|
|
55
|
+
},
|
|
56
|
+
"gitHead": "c45ffe12725e3c4afcf7f13869a25a96f6807055"
|
|
57
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createRollupESConfig,
|
|
3
|
+
createRollupES9Config,
|
|
4
|
+
createRollupUMDConfig,
|
|
5
|
+
} from '../../createRollupConfig.mjs';
|
|
6
|
+
|
|
7
|
+
let esConfig = createRollupESConfig();
|
|
8
|
+
let es9Config = createRollupES9Config();
|
|
9
|
+
let umdConfig = createRollupUMDConfig();
|
|
10
|
+
|
|
11
|
+
export default [esConfig, es9Config, umdConfig];
|