@onehat/data 1.11.4 → 1.11.6
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
|
@@ -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;
|
|
@@ -169,17 +169,16 @@ class OfflineRepository extends MemoryRepository {
|
|
|
169
169
|
// Attempt to add
|
|
170
170
|
super._doAdd(entity);
|
|
171
171
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
.
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
});
|
|
172
|
+
let storageResult;
|
|
173
|
+
try {
|
|
174
|
+
storageResult = await this._storageSetValue(entity.id, entity.getOriginalData());
|
|
175
|
+
this._addToIndex(entity.id);
|
|
176
|
+
} catch (e) {
|
|
177
|
+
// Revert to clone
|
|
178
|
+
delete this._keyedEntities[entity.id];
|
|
179
|
+
entity.destroy();
|
|
180
|
+
this._keyedEntities[clone.id] = clone;
|
|
181
|
+
}
|
|
183
182
|
|
|
184
183
|
return storageResult;
|
|
185
184
|
}
|
|
@@ -206,15 +205,16 @@ class OfflineRepository extends MemoryRepository {
|
|
|
206
205
|
|
|
207
206
|
// Attempt to edit
|
|
208
207
|
super._doEdit(entity);
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
.
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
208
|
+
|
|
209
|
+
let storageResult;
|
|
210
|
+
try {
|
|
211
|
+
storageResult = await this._storageSetValue(entity.id, entity.getOriginalData());
|
|
212
|
+
} catch (e) {
|
|
213
|
+
// Revert to clone
|
|
214
|
+
entity.isPersisted = clone.isPersisted;
|
|
215
|
+
entity._originalData = clone._originalData;
|
|
216
|
+
entity._originalDataParsed = clone._originalDataParsed;
|
|
217
|
+
}
|
|
218
218
|
|
|
219
219
|
return storageResult;
|
|
220
220
|
}
|
|
@@ -228,16 +228,16 @@ class OfflineRepository extends MemoryRepository {
|
|
|
228
228
|
|
|
229
229
|
// Attempt to delete
|
|
230
230
|
super._doDelete(entity);
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
storageResult
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
let storageResult;
|
|
234
|
+
try {
|
|
235
|
+
storageResult = await this._storageDeleteValue(entity.id);
|
|
236
|
+
this._deleteFromIndex(entity.id);
|
|
237
|
+
} catch (e) {
|
|
238
|
+
// Revert to clone
|
|
239
|
+
this._keyedEntities[clone.id] = clone;
|
|
240
|
+
}
|
|
241
241
|
|
|
242
242
|
return storageResult;
|
|
243
243
|
}
|