@onehat/data 1.11.4 → 1.11.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.11.4",
3
+ "version": "1.11.5",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -0,0 +1,169 @@
1
+ /** @module Repository */
2
+
3
+ import OfflineRepository from '@onehat/data/src/Repository/Offline.js';
4
+ import Cookies from 'js-cookie'; // see: https://github.com/js-cookie/js-cookie
5
+ import _ from 'lodash';
6
+
7
+ const CHUNK_SEPARATOR = 'CHUNK';
8
+
9
+ /**
10
+ * Repository representing a browser's cookie implementation
11
+ * Uses js-cookie package
12
+ * @extends OfflineRepository
13
+ */
14
+ class CookieRepository extends OfflineRepository {
15
+
16
+ constructor(config = {}) {
17
+ super(...arguments);
18
+ _.merge(this, config);
19
+ }
20
+
21
+ _storageGetValue = (name) => {
22
+ try {
23
+
24
+ if (this.debugMode) {
25
+ console.log(this.name, 'Cookie.get', name);
26
+ }
27
+
28
+
29
+ // Check if we need to assemble chunks
30
+ const record = Cookies.get(this._namespace(name)),
31
+ regex = new RegExp('^' + CHUNK_SEPARATOR + '([\\d]+)$'),
32
+ matches = record.match(regex);
33
+ let result;
34
+ if (matches && matches[1]) {
35
+ const
36
+ totalChunks = matches[1],
37
+ chunks = [];
38
+ let n,
39
+ recName;
40
+ for (n = 0; n < totalChunks; n++) {
41
+ recName = this._namespace(name) + CHUNK_SEPARATOR + n;
42
+ result = Cookies.get(recName);
43
+ chunks.push(result);
44
+ }
45
+ result = chunks.join('');
46
+
47
+ } else {
48
+ result = Cookies.get(this._namespace(name));
49
+ }
50
+
51
+ if (this.debugMode) {
52
+ console.log(this.name, 'Cookie.get results', name, result);
53
+ }
54
+
55
+ let value;
56
+ if (!_.isNil(result)) {
57
+ try {
58
+ value = JSON.parse(result);
59
+ } catch (e) {
60
+ value = result; // Invalid JSON, just return raw result
61
+ }
62
+ }
63
+ return value;
64
+ } catch (error) {
65
+ // if (this.debugMode) {
66
+ // const msg = error && error.message;
67
+ // debugger;
68
+ // }
69
+ }
70
+ }
71
+
72
+ _storageSetValue = (name, value) => {
73
+ try {
74
+ if (!_.isString(value)) {
75
+ value = JSON.stringify(value);
76
+ }
77
+ if (this.debugMode) {
78
+ console.log(this.name, 'Cookie.set', name, value);
79
+ }
80
+
81
+ const totalSize = new Blob([value]).size;
82
+ let result;
83
+ const maxSize = 4000;
84
+ if (totalSize > maxSize) {
85
+ // value is too big (values cannot be > 2048 bytes https://docs.expo.dev/versions/latest/sdk/securestore/)
86
+ // so we need to chunk the value
87
+ const totalChunks = Math.ceil(totalSize / maxSize),
88
+ chunks = new Array(totalChunks);
89
+ let i,
90
+ o,
91
+ n,
92
+ chunkValue,
93
+ recName;
94
+ for (i = 0, o = 0; i < totalChunks; ++i, o += maxSize) {
95
+ chunks[i] = value.substr(o, maxSize)
96
+ }
97
+
98
+ // Save a header into the normal value, which stores the number of chunks, and tells the repository to get the chunks next time it's read
99
+ recName = this._namespace(name);
100
+ result = Cookies.set(recName, CHUNK_SEPARATOR + totalChunks);
101
+
102
+ // now save the actual chunks
103
+ for (n = 0; n < chunks.length; n++) {
104
+ chunkValue = chunks[n];
105
+ recName = this._namespace(name) + CHUNK_SEPARATOR + n;
106
+ Cookies.set(recName, chunkValue);
107
+ }
108
+ } else {
109
+ result = Cookies.set(this._namespace(name), value);
110
+ }
111
+
112
+ // if (this.debugMode) {
113
+ // console.log(this.name, 'Cookie.set results', name, result);
114
+ // }
115
+ return result;
116
+ } catch (error) {
117
+ // if (this.debugMode) {
118
+ // const msg = error && error.message;
119
+ // debugger;
120
+ // }
121
+ }
122
+ }
123
+
124
+ _storageDeleteValue = (name) => {
125
+ try {
126
+ if (this.debugMode) {
127
+ console.log(this.name, 'Cookie.delete', name);
128
+ }
129
+
130
+
131
+ // Check if we need to delete chunks
132
+ const record = Cookies.get(this._namespace(name)),
133
+ regex = new RegExp('^' + CHUNK_SEPARATOR + '([\\d]+)$'),
134
+ matches = record.match(regex);
135
+ let result;
136
+ if (matches && matches[1]) {
137
+ const totalChunks = matches[1];
138
+ let n,
139
+ recName;
140
+ for (n = 0; n < totalChunks; n++) {
141
+ recName = this._namespace(name) + CHUNK_SEPARATOR + n;
142
+ result = Cookies.remove(recName);
143
+ }
144
+ } else {
145
+ result = Cookies.remove(this._namespace(name));
146
+ }
147
+
148
+ // if (this.debugMode) {
149
+ // console.log(this.name, 'Cookie.delete results', name, result);
150
+ // }
151
+ return result;
152
+ } catch (error) {
153
+ // if (this.debugMode) {
154
+ // const msg = error && error.message;
155
+ // debugger;
156
+ // }
157
+ }
158
+ }
159
+
160
+ clearAll = async () => {
161
+ await this.load([]);
162
+ }
163
+
164
+ };
165
+
166
+ CookieRepository.className = 'Cookie';
167
+ CookieRepository.type = 'cookie';
168
+
169
+ export default CookieRepository;