react-native-mosquito-transport 0.0.14
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/CODE_OF_CONDUCT.md +133 -0
- package/CONTRIBUTING.md +114 -0
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/android/build.gradle +77 -0
- package/android/gradle.properties +5 -0
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/java/com/mosquitodb/MosquitodbModule.java +32 -0
- package/android/src/main/java/com/mosquitodb/MosquitodbPackage.java +28 -0
- package/example/.bundle/config +2 -0
- package/example/.node-version +1 -0
- package/example/.watchmanconfig +1 -0
- package/example/Gemfile +6 -0
- package/example/android/app/build.gradle +170 -0
- package/example/android/app/debug.keystore +0 -0
- package/example/android/app/proguard-rules.pro +10 -0
- package/example/android/app/src/debug/AndroidManifest.xml +13 -0
- package/example/android/app/src/debug/java/com/mosquitodbexample/ReactNativeFlipper.java +75 -0
- package/example/android/app/src/main/AndroidManifest.xml +25 -0
- package/example/android/app/src/main/java/com/mosquitodbexample/MainActivity.java +35 -0
- package/example/android/app/src/main/java/com/mosquitodbexample/MainApplication.java +62 -0
- package/example/android/app/src/main/res/drawable/rn_edit_text_material.xml +36 -0
- package/example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png +0 -0
- package/example/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png +0 -0
- package/example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png +0 -0
- package/example/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png +0 -0
- package/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png +0 -0
- package/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png +0 -0
- package/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png +0 -0
- package/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png +0 -0
- package/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png +0 -0
- package/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png +0 -0
- package/example/android/app/src/main/res/values/strings.xml +3 -0
- package/example/android/app/src/main/res/values/styles.xml +9 -0
- package/example/android/app/src/release/java/com/mosquitodbexample/ReactNativeFlipper.java +20 -0
- package/example/android/build.gradle +21 -0
- package/example/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/example/android/gradle/wrapper/gradle-wrapper.properties +5 -0
- package/example/android/gradle.properties +44 -0
- package/example/android/gradlew +234 -0
- package/example/android/gradlew.bat +89 -0
- package/example/android/settings.gradle +4 -0
- package/example/app.json +4 -0
- package/example/babel.config.js +17 -0
- package/example/index.js +5 -0
- package/example/ios/.xcode.env +11 -0
- package/example/ios/File.swift +6 -0
- package/example/ios/MosquitodbExample/AppDelegate.h +6 -0
- package/example/ios/MosquitodbExample/AppDelegate.mm +36 -0
- package/example/ios/MosquitodbExample/Images.xcassets/AppIcon.appiconset/Contents.json +53 -0
- package/example/ios/MosquitodbExample/Images.xcassets/Contents.json +6 -0
- package/example/ios/MosquitodbExample/Info.plist +55 -0
- package/example/ios/MosquitodbExample/LaunchScreen.storyboard +47 -0
- package/example/ios/MosquitodbExample/main.m +10 -0
- package/example/ios/MosquitodbExample-Bridging-Header.h +3 -0
- package/example/ios/MosquitodbExample.xcodeproj/project.pbxproj +702 -0
- package/example/ios/MosquitodbExample.xcodeproj/xcshareddata/xcschemes/MosquitodbExample.xcscheme +88 -0
- package/example/ios/MosquitodbExampleTests/Info.plist +24 -0
- package/example/ios/MosquitodbExampleTests/MosquitodbExampleTests.m +66 -0
- package/example/ios/Podfile +60 -0
- package/example/metro.config.js +40 -0
- package/example/package.json +22 -0
- package/example/react-native.config.js +10 -0
- package/example/src/App.tsx +31 -0
- package/ios/Mosquitodb-Bridging-Header.h +2 -0
- package/ios/Mosquitodb.m +22 -0
- package/ios/Mosquitodb.swift +305 -0
- package/ios/Mosquitodb.xcodeproj/project.pbxproj +283 -0
- package/package.json +45 -0
- package/react-native-mosquitodb.podspec +35 -0
- package/src/helpers/EngineApi.js +34 -0
- package/src/helpers/listeners.js +7 -0
- package/src/helpers/peripherals.js +195 -0
- package/src/helpers/utils.js +113 -0
- package/src/helpers/values.js +72 -0
- package/src/helpers/variables.js +34 -0
- package/src/index.d.ts +373 -0
- package/src/index.js +369 -0
- package/src/products/auth/accessor.js +151 -0
- package/src/products/auth/index.js +279 -0
- package/src/products/database/accessor.js +316 -0
- package/src/products/database/index.js +603 -0
- package/src/products/database/types.js +22 -0
- package/src/products/database/validator.js +282 -0
- package/src/products/http_callable/index.js +230 -0
- package/src/products/storage/index.js +217 -0
|
@@ -0,0 +1,603 @@
|
|
|
1
|
+
import { io } from "socket.io-client";
|
|
2
|
+
import EngineApi from "../../helpers/EngineApi";
|
|
3
|
+
import { DatabaseRecordsListener } from "../../helpers/listeners";
|
|
4
|
+
import { IS_WHOLE_NUMBER, cloneInstance, deserializeE2E, listenReachableServer, niceTry, serializeE2E, simplifyCaughtError } from "../../helpers/peripherals";
|
|
5
|
+
import { awaitStore, buildFetchInterface, getReachableServer } from "../../helpers/utils";
|
|
6
|
+
import { CacheStore, Scoped } from "../../helpers/variables";
|
|
7
|
+
import { addPendingWrites, generateRecordID, getRecord, insertRecord, listenQueryEntry, removePendingWrite } from "./accessor";
|
|
8
|
+
import { validateCollectionPath, validateFilter, validateReadConfig, validateWriteValue } from "./validator";
|
|
9
|
+
import { awaitRefreshToken, listenToken } from "../auth/accessor";
|
|
10
|
+
import { DEFAULT_DB_NAME, DEFAULT_DB_URL, DELIVERY, RETRIEVAL } from "../../helpers/values";
|
|
11
|
+
import setLodash from 'lodash/set';
|
|
12
|
+
|
|
13
|
+
export class MTCollection {
|
|
14
|
+
constructor(config) {
|
|
15
|
+
this.builder = { ...config };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
find = (find) => ({
|
|
19
|
+
get: (config) => findObject({ ...this.builder, command: { find } }, config),
|
|
20
|
+
listen: (callback, error, config) => listenDocument(callback, error, { ...this.builder, command: { find } }, config),
|
|
21
|
+
count: (config) => countCollection({ ...this.builder, command: { find } }, config),
|
|
22
|
+
limit: (limit) => ({
|
|
23
|
+
get: (config) => findObject({ ...this.builder, command: { find, limit } }, config),
|
|
24
|
+
random: (config) => findObject({ ...this.builder, command: { find, limit, random: true } }, config),
|
|
25
|
+
listen: (callback, error, config) => listenDocument(callback, error, { ...this.builder, command: { find, limit } }, config),
|
|
26
|
+
sort: (sort, direction) => ({
|
|
27
|
+
get: (config) => findObject({ ...this.builder, command: { find, limit, sort, direction } }, config),
|
|
28
|
+
listen: (callback, error, config) => listenDocument(callback, error, {
|
|
29
|
+
...this.builder,
|
|
30
|
+
command: { find, limit, sort, direction }
|
|
31
|
+
}, config)
|
|
32
|
+
})
|
|
33
|
+
}),
|
|
34
|
+
sort: (sort, direction) => ({
|
|
35
|
+
get: (config) => findObject({ ...this.builder, command: { find, sort, direction } }, config),
|
|
36
|
+
listen: (callback, error, config) => listenDocument(callback, error, {
|
|
37
|
+
...this.builder,
|
|
38
|
+
command: { find, sort, direction }
|
|
39
|
+
}, config),
|
|
40
|
+
limit: (limit) => ({
|
|
41
|
+
get: (config) => findObject({ ...this.builder, command: { find, sort, direction, limit } }, config),
|
|
42
|
+
listen: (callback, error, config) => listenDocument(callback, error, {
|
|
43
|
+
...this.builder,
|
|
44
|
+
command: { find, sort, direction, limit }
|
|
45
|
+
}, config)
|
|
46
|
+
})
|
|
47
|
+
})
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
sort = (sort, direction) => this.find().sort(sort, direction);
|
|
51
|
+
|
|
52
|
+
limit = (limit) => this.find().limit(limit);
|
|
53
|
+
|
|
54
|
+
count = (config) => countCollection({ ...this.builder }, config);
|
|
55
|
+
|
|
56
|
+
get = (config) => findObject({ ...this.builder }, config);
|
|
57
|
+
|
|
58
|
+
listen = (callback, error, config) => listenDocument(callback, error, { ...this.builder }, config);
|
|
59
|
+
|
|
60
|
+
findOne = (findOne = {}) => ({
|
|
61
|
+
listen: (callback, error, config) => listenDocument(callback, error, { ...this.builder, command: { findOne } }, config),
|
|
62
|
+
get: (config) => findObject({ ...this.builder, command: { findOne } }, config)
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
onDisconnect = () => ({
|
|
66
|
+
setOne: (value) => initOnDisconnectionTask({ ...this.builder }, value, 'setOne'),
|
|
67
|
+
setMany: (value) => initOnDisconnectionTask({ ...this.builder }, value, 'setMany'),
|
|
68
|
+
updateOne: (find, value) => initOnDisconnectionTask({ ...this.builder, command: { find } }, value, 'updateOne'),
|
|
69
|
+
updateMany: (find, value) => initOnDisconnectionTask({ ...this.builder, command: { find } }, value, 'updateMany'),
|
|
70
|
+
mergeOne: (find, value) => initOnDisconnectionTask({ ...this.builder, command: { find } }, value, 'mergeOne'),
|
|
71
|
+
mergeMany: (find, value) => initOnDisconnectionTask({ ...this.builder, command: { find } }, value, 'mergeMany'),
|
|
72
|
+
deleteOne: (find) => initOnDisconnectionTask({ ...this.builder, command: { find } }, null, 'deleteOne'),
|
|
73
|
+
deleteMany: (find) => initOnDisconnectionTask({ ...this.builder, command: { find } }, null, 'deleteMany'),
|
|
74
|
+
replaceOne: (find, value) => initOnDisconnectionTask({ ...this.builder, command: { find } }, value, 'replaceOne'),
|
|
75
|
+
putOne: (find, value) => initOnDisconnectionTask({ ...this.builder, command: { find } }, value, 'putOne')
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
setOne = (value, config) => commitData(this.builder, value, 'setOne', config);
|
|
79
|
+
|
|
80
|
+
setMany = (value, config) => commitData(this.builder, value, 'setMany', config);
|
|
81
|
+
|
|
82
|
+
updateOne = (find, value, config) => commitData({ ...this.builder, find }, value, 'updateOne', config);
|
|
83
|
+
|
|
84
|
+
updateMany = (find, value, config) => commitData({ ...this.builder, find }, value, 'updateMany', config);
|
|
85
|
+
|
|
86
|
+
mergeOne = (find, value, config) => commitData({ ...this.builder, find }, value, 'mergeOne', config);
|
|
87
|
+
|
|
88
|
+
mergeMany = (find, value, config) => commitData({ ...this.builder, find }, value, 'mergeMany', config);
|
|
89
|
+
|
|
90
|
+
replaceOne = (find, value, config) => commitData({ ...this.builder, find }, value, 'replaceOne', config);
|
|
91
|
+
|
|
92
|
+
putOne = (find, value, config) => commitData({ ...this.builder, find }, value, 'putOne', config);
|
|
93
|
+
|
|
94
|
+
deleteOne = (find, config) => commitData({ ...this.builder, find }, null, 'deleteOne', config);
|
|
95
|
+
|
|
96
|
+
deleteMany = (find, config) => commitData({ ...this.builder, find }, null, 'deleteMany', config);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export const batchWrite = (builder, map, config) => commitData({ ...builder }, map, 'batchWrite', config);
|
|
100
|
+
|
|
101
|
+
const {
|
|
102
|
+
_listenCollection,
|
|
103
|
+
_listenDocument,
|
|
104
|
+
_startDisconnectWriteTask,
|
|
105
|
+
_cancelDisconnectWriteTask,
|
|
106
|
+
_documentCount,
|
|
107
|
+
_readDocument,
|
|
108
|
+
_queryCollection,
|
|
109
|
+
_writeDocument,
|
|
110
|
+
_writeMapDocument
|
|
111
|
+
} = EngineApi;
|
|
112
|
+
|
|
113
|
+
const listenDocument = (callback, onError, builder, config) => {
|
|
114
|
+
const { projectUrl, serverE2E_PublicKey, baseUrl, dbUrl, dbName, accessKey, path, disableCache, command, uglify } = builder,
|
|
115
|
+
{ find, findOne, sort, direction, limit } = command,
|
|
116
|
+
{ disableAuth } = config || {},
|
|
117
|
+
accessId = generateRecordID(builder, config),
|
|
118
|
+
shouldCache = !disableCache,
|
|
119
|
+
processId = `${++Scoped.AnyProcessIte}`;
|
|
120
|
+
|
|
121
|
+
validateReadConfig(config, ['retrieval', 'disableAuth']);
|
|
122
|
+
validateFilter(findOne || find);
|
|
123
|
+
validateCollectionPath(path);
|
|
124
|
+
|
|
125
|
+
let hasCancelled,
|
|
126
|
+
hasRespond,
|
|
127
|
+
cacheListener,
|
|
128
|
+
socket,
|
|
129
|
+
wasDisconnected,
|
|
130
|
+
lastToken = Scoped.AuthJWTToken[projectUrl] || null,
|
|
131
|
+
lastInitRef = 0,
|
|
132
|
+
connectedListener;
|
|
133
|
+
|
|
134
|
+
if (shouldCache) {
|
|
135
|
+
cacheListener = listenQueryEntry(callback, { accessId, builder, config, processId });
|
|
136
|
+
|
|
137
|
+
connectedListener = listenReachableServer(async connected => {
|
|
138
|
+
connectedListener();
|
|
139
|
+
await awaitStore();
|
|
140
|
+
if (!connected && !hasRespond && !hasCancelled && shouldCache)
|
|
141
|
+
DatabaseRecordsListener.dispatch(accessId, processId);
|
|
142
|
+
}, projectUrl);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const init = async () => {
|
|
146
|
+
const processID = ++lastInitRef;
|
|
147
|
+
if (!disableAuth) await awaitRefreshToken(projectUrl);
|
|
148
|
+
if (hasCancelled || processID !== lastInitRef) return;
|
|
149
|
+
|
|
150
|
+
const mtoken = disableAuth ? undefined : Scoped.AuthJWTToken[projectUrl],
|
|
151
|
+
authObj = {
|
|
152
|
+
commands: {
|
|
153
|
+
config,
|
|
154
|
+
path,
|
|
155
|
+
find: findOne || find,
|
|
156
|
+
sort,
|
|
157
|
+
direction,
|
|
158
|
+
limit
|
|
159
|
+
},
|
|
160
|
+
dbName,
|
|
161
|
+
dbUrl
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
const [encPlate, [privateKey]] = uglify ? serializeE2E({ accessKey, _body: authObj }, mtoken, serverE2E_PublicKey) : ['', []];
|
|
165
|
+
|
|
166
|
+
socket = io(`ws://${baseUrl}`, {
|
|
167
|
+
auth: uglify ? { e2e: encPlate, _m_internal: true } : {
|
|
168
|
+
accessKey,
|
|
169
|
+
_body: authObj,
|
|
170
|
+
...mtoken ? { mtoken } : {},
|
|
171
|
+
_m_internal: true
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
socket.emit((findOne ? _listenDocument : _listenCollection)(uglify));
|
|
176
|
+
socket.on('mSnapshot', async ([err, snapshot]) => {
|
|
177
|
+
hasRespond = true;
|
|
178
|
+
if (err) {
|
|
179
|
+
onError?.(simplifyCaughtError(err).simpleError);
|
|
180
|
+
} else {
|
|
181
|
+
if (uglify) snapshot = deserializeE2E(snapshot, serverE2E_PublicKey, privateKey);
|
|
182
|
+
callback?.(snapshot);
|
|
183
|
+
|
|
184
|
+
if (shouldCache)
|
|
185
|
+
insertRecord(builder, accessId, { sort, direction, limit, find, findOne, config }, snapshot);
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
socket.on('connect', () => {
|
|
190
|
+
if (wasDisconnected) socket.emit((findOne ? _listenDocument : _listenCollection)(uglify));
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
socket.on('disconnect', () => {
|
|
194
|
+
wasDisconnected = true;
|
|
195
|
+
});
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
init();
|
|
199
|
+
|
|
200
|
+
const tokenListener = listenToken(t => {
|
|
201
|
+
if ((t || null) !== lastToken) {
|
|
202
|
+
socket?.close?.();
|
|
203
|
+
wasDisconnected = undefined;
|
|
204
|
+
init();
|
|
205
|
+
}
|
|
206
|
+
lastToken = t;
|
|
207
|
+
}, projectUrl);
|
|
208
|
+
|
|
209
|
+
return () => {
|
|
210
|
+
if (hasCancelled) return;
|
|
211
|
+
hasCancelled = true;
|
|
212
|
+
connectedListener?.();
|
|
213
|
+
cacheListener?.();
|
|
214
|
+
tokenListener?.();
|
|
215
|
+
if (socket) socket.close();
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const initOnDisconnectionTask = (builder, value, type) => {
|
|
220
|
+
const { projectUrl, baseUrl, serverE2E_PublicKey, dbUrl, dbName, accessKey, path, command, uglify } = builder,
|
|
221
|
+
{ find } = command || {},
|
|
222
|
+
disableAuth = false;
|
|
223
|
+
|
|
224
|
+
validateCollectionPath(path);
|
|
225
|
+
let hasCancelled,
|
|
226
|
+
socket,
|
|
227
|
+
wasDisconnected,
|
|
228
|
+
lastToken = Scoped.AuthJWTToken[projectUrl] || null,
|
|
229
|
+
lastInitRef = 0;
|
|
230
|
+
|
|
231
|
+
const init = async () => {
|
|
232
|
+
const processID = ++lastInitRef;
|
|
233
|
+
if (!disableAuth) await awaitRefreshToken(projectUrl);
|
|
234
|
+
if (hasCancelled || processID !== lastInitRef) return;
|
|
235
|
+
|
|
236
|
+
const mtoken = disableAuth ? undefined : Scoped.AuthJWTToken[projectUrl],
|
|
237
|
+
authObj = {
|
|
238
|
+
commands: { path, find, value, scope: type },
|
|
239
|
+
dbName,
|
|
240
|
+
dbUrl
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
socket = io(`ws://${baseUrl}`, {
|
|
244
|
+
auth: uglify ? {
|
|
245
|
+
e2e: serializeE2E(authObj, mtoken, serverE2E_PublicKey)[0],
|
|
246
|
+
_m_internal: true
|
|
247
|
+
} : {
|
|
248
|
+
...mtoken ? { mtoken } : {},
|
|
249
|
+
accessKey,
|
|
250
|
+
_body: authObj,
|
|
251
|
+
_m_internal: true
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
socket.emit(_startDisconnectWriteTask(uglify));
|
|
255
|
+
|
|
256
|
+
socket.on('connect', () => {
|
|
257
|
+
if (wasDisconnected) socket.emit(_startDisconnectWriteTask(uglify));
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
socket.on('disconnect', () => {
|
|
261
|
+
wasDisconnected = true;
|
|
262
|
+
});
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
init();
|
|
266
|
+
|
|
267
|
+
const tokenListener = listenToken(async t => {
|
|
268
|
+
if ((t || null) !== lastToken) {
|
|
269
|
+
if (socket) {
|
|
270
|
+
await niceTry(() => socket.timeout(10000).emitWithAck(_cancelDisconnectWriteTask(uglify)));
|
|
271
|
+
socket.close();
|
|
272
|
+
}
|
|
273
|
+
wasDisconnected = undefined;
|
|
274
|
+
init();
|
|
275
|
+
}
|
|
276
|
+
lastToken = t;
|
|
277
|
+
}, projectUrl);
|
|
278
|
+
|
|
279
|
+
return () => {
|
|
280
|
+
if (hasCancelled) return;
|
|
281
|
+
tokenListener();
|
|
282
|
+
if (socket)
|
|
283
|
+
(async () => {
|
|
284
|
+
await niceTry(() => socket.timeout(10000).emitWithAck(_cancelDisconnectWriteTask(uglify)));
|
|
285
|
+
socket.close();
|
|
286
|
+
})();
|
|
287
|
+
hasCancelled = true;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const countCollection = async (builder, config) => {
|
|
292
|
+
const { projectUrl, serverE2E_PublicKey, dbUrl, dbName, accessKey, maxRetries = 7, uglify, path, disableCache, command = {} } = builder,
|
|
293
|
+
{ find } = command,
|
|
294
|
+
{ disableAuth } = config || {},
|
|
295
|
+
accessId = generateRecordID({ ...builder, countDoc: true }, config);
|
|
296
|
+
|
|
297
|
+
await awaitStore();
|
|
298
|
+
validateReadConfig(config, [
|
|
299
|
+
'excludeFields',
|
|
300
|
+
'returnOnly',
|
|
301
|
+
'extraction',
|
|
302
|
+
'episode',
|
|
303
|
+
'retrieval',
|
|
304
|
+
'disableMinimizer'
|
|
305
|
+
]);
|
|
306
|
+
validateFilter(find || {});
|
|
307
|
+
validateCollectionPath(path);
|
|
308
|
+
|
|
309
|
+
let retries = 0;
|
|
310
|
+
|
|
311
|
+
const readValue = () => new Promise(async (resolve, reject) => {
|
|
312
|
+
++retries;
|
|
313
|
+
|
|
314
|
+
const finalize = (a, b) => {
|
|
315
|
+
if (isNaN(a)) {
|
|
316
|
+
reject(b);
|
|
317
|
+
} else resolve(a);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
try {
|
|
321
|
+
if (!disableAuth && await getReachableServer(projectUrl)) await awaitRefreshToken(projectUrl);
|
|
322
|
+
|
|
323
|
+
const [reqBuilder, [privateKey]] = buildFetchInterface({
|
|
324
|
+
body: {
|
|
325
|
+
commands: { path, find },
|
|
326
|
+
dbName,
|
|
327
|
+
dbUrl
|
|
328
|
+
},
|
|
329
|
+
accessKey,
|
|
330
|
+
...disableAuth ? {} : { authToken: Scoped.AuthJWTToken[projectUrl] },
|
|
331
|
+
serverE2E_PublicKey,
|
|
332
|
+
uglify
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
const r = await (await fetch(_documentCount(projectUrl, uglify), reqBuilder)).json();
|
|
336
|
+
if (r.simpleError) throw r;
|
|
337
|
+
|
|
338
|
+
const f = uglify ? deserializeE2E(r.e2e, serverE2E_PublicKey, privateKey) : r;
|
|
339
|
+
|
|
340
|
+
if (!disableCache)
|
|
341
|
+
setLodash(CacheStore.DatabaseCountResult, [projectUrl, dbUrl || DEFAULT_DB_URL, dbName || DEFAULT_DB_NAME, accessId], f.result);
|
|
342
|
+
|
|
343
|
+
finalize(f.result);
|
|
344
|
+
} catch (e) {
|
|
345
|
+
const b4Data = setLodash(CacheStore.DatabaseCountResult, [projectUrl, dbUrl || DEFAULT_DB_URL, dbName || DEFAULT_DB_NAME, accessId]);
|
|
346
|
+
|
|
347
|
+
if (e?.simpleError) {
|
|
348
|
+
finalize(undefined, e.simpleError);
|
|
349
|
+
} else if (!disableCache && !isNaN(b4Data)) {
|
|
350
|
+
finalize(b4Data);
|
|
351
|
+
} else if (retries > maxRetries) {
|
|
352
|
+
finalize(undefined, { error: 'retry_limit_exceeded', message: `retry exceed limit(${maxRetries})` });
|
|
353
|
+
} else {
|
|
354
|
+
const onlineListener = listenReachableServer(connected => {
|
|
355
|
+
if (connected) {
|
|
356
|
+
onlineListener();
|
|
357
|
+
readValue().then(
|
|
358
|
+
e => { finalize(e); },
|
|
359
|
+
e => { finalize(undefined, e); }
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
}, projectUrl);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
const g = await readValue();
|
|
368
|
+
return g;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const findObject = async (builder, config) => {
|
|
372
|
+
const { projectUrl, serverE2E_PublicKey, dbUrl, dbName, accessKey, maxRetries = 7, path, disableCache, uglify, command } = builder,
|
|
373
|
+
{ find, findOne, sort, direction, limit, random } = command,
|
|
374
|
+
{ retrieval = RETRIEVAL.DEFAULT, episode = 0, disableAuth, disableMinimizer } = config || {},
|
|
375
|
+
enableMinimizer = !disableMinimizer,
|
|
376
|
+
accessId = generateRecordID(builder, config),
|
|
377
|
+
processAccessId = `${accessId}${projectUrl}${dbUrl}${dbName}${retrieval}`,
|
|
378
|
+
getRecordData = () => getRecord(builder, accessId),
|
|
379
|
+
shouldCache = (retrieval === RETRIEVAL.DEFAULT ? !disableCache : true) &&
|
|
380
|
+
retrieval !== RETRIEVAL.NO_CACHE_NO_AWAIT;
|
|
381
|
+
|
|
382
|
+
await awaitStore();
|
|
383
|
+
if (shouldCache) {
|
|
384
|
+
validateReadConfig(config);
|
|
385
|
+
validateCollectionPath(path);
|
|
386
|
+
validateFilter(findOne || find);
|
|
387
|
+
|
|
388
|
+
if (typeof limit === 'number' && (!IS_WHOLE_NUMBER(limit) || limit <= 0))
|
|
389
|
+
throw `limit() has an invalid argument for "${path}", expected a positive whole number but got ${limit}`;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
let retries = 0, hasFinalize;
|
|
393
|
+
|
|
394
|
+
const readValue = () => new Promise(async (resolve, reject) => {
|
|
395
|
+
const retryProcess = ++retries,
|
|
396
|
+
instantProcess = retryProcess === 1;
|
|
397
|
+
|
|
398
|
+
const finalize = (a, b) => {
|
|
399
|
+
const res = (instantProcess && a) ?
|
|
400
|
+
(a.liveResult || a.liveResult === null) ?
|
|
401
|
+
(a.liveResult || undefined) :
|
|
402
|
+
a.episode[episode] : a;
|
|
403
|
+
|
|
404
|
+
if (a) {
|
|
405
|
+
resolve(instantProcess ? res : a);
|
|
406
|
+
} else reject(b);
|
|
407
|
+
if (hasFinalize || !instantProcess) return;
|
|
408
|
+
hasFinalize = true;
|
|
409
|
+
|
|
410
|
+
if (enableMinimizer) {
|
|
411
|
+
(Scoped.PendingDbReadCollective.pendingResolution[processAccessId] || []).forEach(e => {
|
|
412
|
+
e(a ? { result: cloneInstance(res) } : undefined, b);
|
|
413
|
+
});
|
|
414
|
+
if (Scoped.PendingDbReadCollective.pendingResolution[processAccessId])
|
|
415
|
+
delete Scoped.PendingDbReadCollective.pendingResolution[processAccessId];
|
|
416
|
+
|
|
417
|
+
if (Scoped.PendingDbReadCollective.pendingProcess[processAccessId])
|
|
418
|
+
delete Scoped.PendingDbReadCollective.pendingProcess[processAccessId];
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
try {
|
|
423
|
+
if (instantProcess) {
|
|
424
|
+
if (enableMinimizer) {
|
|
425
|
+
if (Scoped.PendingDbReadCollective.pendingProcess[processAccessId]) {
|
|
426
|
+
if (!Scoped.PendingDbReadCollective.pendingResolution[processAccessId])
|
|
427
|
+
Scoped.PendingDbReadCollective.pendingResolution[processAccessId] = [];
|
|
428
|
+
|
|
429
|
+
Scoped.PendingDbReadCollective.pendingResolution[processAccessId].push((a, b) => {
|
|
430
|
+
if (a) resolve(a.result);
|
|
431
|
+
else reject(b);
|
|
432
|
+
});
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
Scoped.PendingDbReadCollective.pendingProcess[processAccessId] = true;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
if (retrieval.startsWith('sticky') && await getRecordData()) {
|
|
439
|
+
finalize({ episode: await getRecordData() });
|
|
440
|
+
if (retrieval !== RETRIEVAL.STICKY_RELOAD) return;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
if (!disableAuth && await getReachableServer(projectUrl))
|
|
444
|
+
await awaitRefreshToken(projectUrl);
|
|
445
|
+
|
|
446
|
+
const [reqBuilder, [privateKey]] = buildFetchInterface({
|
|
447
|
+
body: {
|
|
448
|
+
commands: { config, path, find: findOne || find, sort, direction, limit, random },
|
|
449
|
+
dbName,
|
|
450
|
+
dbUrl
|
|
451
|
+
},
|
|
452
|
+
accessKey,
|
|
453
|
+
authToken: disableAuth ? undefined : Scoped.AuthJWTToken[projectUrl],
|
|
454
|
+
serverE2E_PublicKey,
|
|
455
|
+
uglify
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
const r = await (await fetch((findOne ? _readDocument : _queryCollection)(projectUrl, uglify), reqBuilder)).json();
|
|
459
|
+
if (r.simpleError) throw r;
|
|
460
|
+
|
|
461
|
+
const f = uglify ? deserializeE2E(r.e2e, serverE2E_PublicKey, privateKey) : r;
|
|
462
|
+
|
|
463
|
+
if (shouldCache) insertRecord(builder, accessId, { ...command, config }, f.result);
|
|
464
|
+
finalize({ liveResult: f.result || null });
|
|
465
|
+
} catch (e) {
|
|
466
|
+
if (e?.simpleError) {
|
|
467
|
+
finalize(undefined, e?.simpleError);
|
|
468
|
+
} else if (
|
|
469
|
+
(retrieval === RETRIEVAL.CACHE_NO_AWAIT && !(await getRecordData())) ||
|
|
470
|
+
retrieval === RETRIEVAL.STICKY_NO_AWAIT ||
|
|
471
|
+
retrieval === RETRIEVAL.NO_CACHE_NO_AWAIT
|
|
472
|
+
) {
|
|
473
|
+
finalize(undefined, simplifyCaughtError(e).simpleError);
|
|
474
|
+
} else if (
|
|
475
|
+
shouldCache &&
|
|
476
|
+
(retrieval === RETRIEVAL.DEFAULT || retrieval === RETRIEVAL.CACHE_NO_AWAIT) &&
|
|
477
|
+
await getRecordData()
|
|
478
|
+
) {
|
|
479
|
+
finalize({ episode: await getRecordData() });
|
|
480
|
+
} else if (retries > maxRetries) {
|
|
481
|
+
finalize(undefined, { error: 'retry_limit_exceeded', message: `retry exceed limit(${maxRetries})` });
|
|
482
|
+
} else {
|
|
483
|
+
const onlineListener = listenReachableServer(connected => {
|
|
484
|
+
if (connected) {
|
|
485
|
+
onlineListener();
|
|
486
|
+
readValue().then(
|
|
487
|
+
e => { finalize(e); },
|
|
488
|
+
e => { finalize(undefined, e); }
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
}, projectUrl);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
const g = await readValue();
|
|
497
|
+
return g;
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
const commitData = async (builder, value, type, config) => {
|
|
501
|
+
const { projectUrl, serverE2E_PublicKey, dbUrl, dbName, accessKey, maxRetries = 7, path, find, disableCache, uglify } = builder,
|
|
502
|
+
{ disableAuth, delivery = DELIVERY.DEFAULT, stepping } = config || {},
|
|
503
|
+
writeId = `${Date.now() + ++Scoped.PendingIte}`,
|
|
504
|
+
isBatchWrite = type === 'batchWrite',
|
|
505
|
+
shouldCache = (delivery === DELIVERY.DEFAULT ? !disableCache : true) &&
|
|
506
|
+
delivery !== DELIVERY.NO_CACHE &&
|
|
507
|
+
delivery !== DELIVERY.NO_AWAIT_NO_CACHE &&
|
|
508
|
+
delivery !== DELIVERY.AWAIT_NO_CACHE;
|
|
509
|
+
|
|
510
|
+
await awaitStore();
|
|
511
|
+
if (shouldCache) {
|
|
512
|
+
validateCollectionPath(path);
|
|
513
|
+
// TODO: batchWrite
|
|
514
|
+
validateWriteValue(value, builder.find, type);
|
|
515
|
+
await addPendingWrites(builder, writeId, { value, type, find });
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
let retries = 0, hasFinalize;
|
|
519
|
+
|
|
520
|
+
const sendValue = () => new Promise(async (resolve, reject) => {
|
|
521
|
+
const retryProcess = ++retries,
|
|
522
|
+
instantProcess = retryProcess === 1;
|
|
523
|
+
|
|
524
|
+
const finalize = (a, b, c) => {
|
|
525
|
+
const { removeCache, revertCache } = c || {};
|
|
526
|
+
|
|
527
|
+
if (!instantProcess) {
|
|
528
|
+
if (a) a = { a, c };
|
|
529
|
+
if (b) b = { b, c };
|
|
530
|
+
}
|
|
531
|
+
if (a) {
|
|
532
|
+
resolve(a);
|
|
533
|
+
} else reject(b);
|
|
534
|
+
if (hasFinalize || !instantProcess) return;
|
|
535
|
+
hasFinalize = true;
|
|
536
|
+
if (removeCache && shouldCache) removePendingWrite(builder, writeId, revertCache);
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
try {
|
|
540
|
+
if (!disableAuth && await getReachableServer(projectUrl))
|
|
541
|
+
await awaitRefreshToken(projectUrl);
|
|
542
|
+
|
|
543
|
+
const [reqBuilder, [privateKey]] = buildFetchInterface({
|
|
544
|
+
body: {
|
|
545
|
+
commands: {
|
|
546
|
+
value,
|
|
547
|
+
...isBatchWrite ? { stepping } : {
|
|
548
|
+
path,
|
|
549
|
+
scope: type,
|
|
550
|
+
find
|
|
551
|
+
}
|
|
552
|
+
},
|
|
553
|
+
dbName,
|
|
554
|
+
dbUrl
|
|
555
|
+
},
|
|
556
|
+
accessKey,
|
|
557
|
+
serverE2E_PublicKey,
|
|
558
|
+
authToken: disableAuth ? undefined : Scoped.AuthJWTToken[projectUrl],
|
|
559
|
+
uglify
|
|
560
|
+
});
|
|
561
|
+
|
|
562
|
+
const r = await (await fetch((isBatchWrite ? _writeMapDocument : _writeDocument)(projectUrl, uglify), reqBuilder)).json();
|
|
563
|
+
if (r.simpleError) throw r;
|
|
564
|
+
|
|
565
|
+
const f = uglify ? deserializeE2E(r.e2e, serverE2E_PublicKey, privateKey) : r;
|
|
566
|
+
|
|
567
|
+
finalize({ status: 'sent', committed: f.committed }, undefined, { removeCache: true });
|
|
568
|
+
} catch (e) {
|
|
569
|
+
if (e?.simpleError) {
|
|
570
|
+
console.error(`${type} error (${path}), ${e.simpleError?.message}`);
|
|
571
|
+
finalize(undefined, e?.simpleError, { removeCache: true, revertCache: true });
|
|
572
|
+
} else if (
|
|
573
|
+
delivery === DELIVERY.NO_AWAIT ||
|
|
574
|
+
delivery === DELIVERY.CACHE_NO_AWAIT ||
|
|
575
|
+
delivery === DELIVERY.NO_AWAIT_NO_CACHE ||
|
|
576
|
+
delivery === DELIVERY.NO_CACHE
|
|
577
|
+
) {
|
|
578
|
+
finalize(
|
|
579
|
+
undefined,
|
|
580
|
+
simplifyCaughtError(e).simpleError,
|
|
581
|
+
await getReachableServer(projectUrl) ? { removeCache: true } : null
|
|
582
|
+
);
|
|
583
|
+
} else if (retries > maxRetries) {
|
|
584
|
+
finalize(undefined, { error: 'retry_limit_exceeded', message: `retry exceed limit(${maxRetries})` });
|
|
585
|
+
} else {
|
|
586
|
+
if (delivery === DELIVERY.AWAIT_NO_CACHE) {
|
|
587
|
+
const onlineListener = listenReachableServer(connected => {
|
|
588
|
+
if (connected) {
|
|
589
|
+
onlineListener();
|
|
590
|
+
sendValue().then(
|
|
591
|
+
e => { finalize(e.a, undefined, e.c); },
|
|
592
|
+
e => { finalize(undefined, e.b, e.c); }
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
}, projectUrl);
|
|
596
|
+
} else if (shouldCache) finalize({ status: 'pending' });
|
|
597
|
+
else finalize(undefined, simplifyCaughtError(e).simpleError);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
});
|
|
601
|
+
|
|
602
|
+
return await sendValue();
|
|
603
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
|
|
2
|
+
export const TIMESTAMP = { $timestamp: "now" };
|
|
3
|
+
|
|
4
|
+
export const IS_TIMESTAMP = (t) => t && (typeof t.$timestamp === 'number' || t.$timestamp === 'now') && Object.keys(t).length === 1;
|
|
5
|
+
|
|
6
|
+
export const DOCUMENT_EXTRACTION = (path) => ({ $dynamicValue: path });
|
|
7
|
+
|
|
8
|
+
export const GEO_JSON = (lat, lng) => ({
|
|
9
|
+
type: "Point",
|
|
10
|
+
coordinates: [lng, lat],
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
export const FIND_GEO_JSON = (location, offSetMeters, centerMeters) => ({
|
|
14
|
+
$near: {
|
|
15
|
+
$geometry: {
|
|
16
|
+
type: "Point",
|
|
17
|
+
coordinates: location.reverse()
|
|
18
|
+
},
|
|
19
|
+
$minDistance: centerMeters || 0,
|
|
20
|
+
$maxDistance: offSetMeters
|
|
21
|
+
}
|
|
22
|
+
});
|