@onehat/data 1.13.1 → 1.13.3

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.13.1",
3
+ "version": "1.13.3",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -65,7 +65,7 @@ class AsyncStorageRepository extends OfflineRepository {
65
65
  const results = await AsyncStorage.multiGet(this._namespace(keys));
66
66
 
67
67
  if (this.debugMode) {
68
- console.log(this.name, 'AsyncStorage.multiGet results', name, results);
68
+ console.log(this.name, 'AsyncStorage.multiGet results', keys, results);
69
69
  }
70
70
 
71
71
  let values = [];
@@ -7,6 +7,8 @@ import OfflineRepository from '@onehat/data/src/Repository/Offline.js';
7
7
  import * as SecureStore from 'expo-secure-store'; // see: https://docs.expo.io/versions/latest/sdk/securestore/
8
8
  import _ from 'lodash';
9
9
 
10
+ const CHUNK_SEPARATOR = 'CHUNK';
11
+
10
12
  /**
11
13
  * Repository representing Expo's SecureStore
12
14
  * Uses expo-secure-store package
@@ -25,7 +27,29 @@ class SecureStoreRepository extends OfflineRepository {
25
27
  console.log(this.name, 'SecureStore.get', name);
26
28
  }
27
29
 
28
- const result = await SecureStore.getItemAsync(this._namespace(name));
30
+
31
+ // Check if we need to assemble chunks
32
+ const
33
+ record = await SecureStore.getItemAsync(this._namespace(name)),
34
+ regex = new RegExp('^' + CHUNK_SEPARATOR + '([\\d]+)$'),
35
+ matches = record.match(regex);
36
+ let result;
37
+ if (matches && matches[1]) {
38
+ const
39
+ totalChunks = matches[1],
40
+ chunks = [];
41
+ let n,
42
+ recName;
43
+ for (n = 0; n < totalChunks; n++) {
44
+ recName = this._namespace(name) + CHUNK_SEPARATOR + n;
45
+ result = await SecureStore.getItemAsync(recName);
46
+ chunks.push(result);
47
+ }
48
+ result = chunks.join('');
49
+
50
+ } else {
51
+ result = await SecureStore.getItemAsync(this._namespace(name));
52
+ }
29
53
 
30
54
  if (this.debugMode) {
31
55
  console.log(this.name, 'SecureStore.get results', name, result);
@@ -57,7 +81,35 @@ class SecureStoreRepository extends OfflineRepository {
57
81
  console.log(this.name, 'SecureStore.set', name, value);
58
82
  }
59
83
 
60
- const result = await SecureStore.setItemAsync(this._namespace(name), value);
84
+ const
85
+ totalSize = new Blob([value]).size,
86
+ maxSize = 2000;
87
+ let result;
88
+ if (totalSize > maxSize) {
89
+ // value is too big (values cannot be > 2048 bytes https://docs.expo.dev/versions/latest/sdk/securestore/)
90
+ // so we need to chunk the value
91
+ const totalChunks = Math.ceil(totalSize / maxSize),
92
+ chunks = new Array(totalChunks);
93
+ let i, o, n,
94
+ chunkValue,
95
+ recName;
96
+ for (i = 0, o = 0; i < totalChunks; ++i, o += maxSize) {
97
+ chunks[i] = value.substr(o, maxSize)
98
+ }
99
+
100
+ // 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
101
+ recName = this._namespace(name);
102
+ result = await SecureStore.setItemAsync(recName, CHUNK_SEPARATOR + totalChunks);
103
+
104
+ // now save the actual chunks
105
+ for (n = 0; n < chunks.length; n++) {
106
+ chunkValue = chunks[n];
107
+ recName = this._namespace(name) + CHUNK_SEPARATOR + n;
108
+ await SecureStore.setItemAsync(recName, chunkValue);
109
+ }
110
+ } else {
111
+ result = await SecureStore.setItemAsync(this._namespace(name), value);
112
+ }
61
113
 
62
114
  // if (this.debugMode) {
63
115
  // console.log(this.name, 'SecureStore.set results', name, result);
@@ -77,7 +129,24 @@ class SecureStoreRepository extends OfflineRepository {
77
129
  console.log(this.name, 'SecureStore.delete', name);
78
130
  }
79
131
 
80
- const result = await SecureStore.deleteItemAsync(this._namespace(name));
132
+
133
+ // Check if we need to delete chunks
134
+ const
135
+ record = await SecureStore.getItemAsync(this._namespace(name)),
136
+ regex = new RegExp('^' + CHUNK_SEPARATOR + '([\\d]+)$'),
137
+ matches = record.match(regex);
138
+ let result;
139
+ if (matches && matches[1]) {
140
+ const totalChunks = matches[1];
141
+ let n,
142
+ recName;
143
+ for (n = 0; n < totalChunks; n++) {
144
+ recName = this._namespace(name) + CHUNK_SEPARATOR + n;
145
+ result = await SecureStore.deleteItemAsync(recName);
146
+ }
147
+ } else {
148
+ result = await SecureStore.deleteItemAsync(this._namespace(name));
149
+ }
81
150
 
82
151
  // if (this.debugMode) {
83
152
  // console.log(this.name, 'SecureStore.delete results', name, result);