mosquito-transport-js 0.3.1 → 0.3.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 +3 -3
- package/src/helpers/peripherals.js +21 -20
- package/src/helpers/utils.js +15 -3
- package/src/index.d.ts +1 -1
- package/src/index.js +11 -10
- package/src/polyfill.js +3 -0
- package/src/products/auth/accessor.js +2 -2
- package/src/products/auth/index.js +14 -17
- package/src/products/database/index.js +14 -17
- package/src/products/http_callable/index.js +8 -8
- package/src/products/storage/index.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mosquito-transport-js",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"description": "Javascript web sdk for mosquito-transport (https://github.com/deflexable/mosquito-transport)",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -32,9 +32,9 @@
|
|
|
32
32
|
"bson": "^6.8.0",
|
|
33
33
|
"buffer": "^6.0.3",
|
|
34
34
|
"crypto-js": "^4.2.0",
|
|
35
|
+
"entity-serializer": "^1.0.0",
|
|
35
36
|
"guard-object": "^1.1.3",
|
|
36
37
|
"limit-task": "1.0.0",
|
|
37
|
-
"json-buffer": "^3.0.1",
|
|
38
38
|
"lodash.clonedeep": "^4.5.0",
|
|
39
39
|
"lodash.get": "^4.4.2",
|
|
40
40
|
"lodash.set": "^4.3.2",
|
|
@@ -57,4 +57,4 @@
|
|
|
57
57
|
"engines": {
|
|
58
58
|
"node": ">=14"
|
|
59
59
|
}
|
|
60
|
-
}
|
|
60
|
+
}
|
|
@@ -5,6 +5,7 @@ import Utf8Encoder from 'crypto-js/enc-utf8.js';
|
|
|
5
5
|
import naclPkg from 'tweetnacl-functional';
|
|
6
6
|
import getLodash from "lodash.get";
|
|
7
7
|
import e2e_worker from "./e2e_worker";
|
|
8
|
+
import { deserialize, serialize } from "entity-serializer";
|
|
8
9
|
|
|
9
10
|
const { encrypt, decrypt } = aes_pkg;
|
|
10
11
|
const { box, randomBytes } = naclPkg;
|
|
@@ -103,52 +104,52 @@ export const decryptString = (txt, password, iv) => {
|
|
|
103
104
|
return decrypt(txt, `${password || ''}${iv || ''}`).toString(Utf8Encoder);
|
|
104
105
|
};
|
|
105
106
|
|
|
106
|
-
export const serializeE2E = (data, auth_token, serverPublicKey) => {
|
|
107
|
-
const inputData =
|
|
107
|
+
export const serializeE2E = async (data, auth_token, serverPublicKey) => {
|
|
108
|
+
const inputData = serialize([data, auth_token]);
|
|
108
109
|
|
|
109
110
|
if (inputData.byteLength > 10240) {
|
|
110
111
|
// dispatch to background thread
|
|
111
|
-
const { data, pair, nonce } = e2e_worker.encrypt(inputData, serverPublicKey);
|
|
112
|
-
const pubBase64 = Buffer.from(pair.publicKey).toString('base64'),
|
|
113
|
-
nonceBase64 = Buffer.from(nonce).toString('base64');
|
|
112
|
+
const { data, pair, nonce } = await e2e_worker.encrypt(inputData, serverPublicKey);
|
|
114
113
|
|
|
115
114
|
return [
|
|
116
|
-
|
|
115
|
+
serialize([pair.publicKey, nonce, data]),
|
|
117
116
|
[pair.secretKey, pair.publicKey]
|
|
118
117
|
];
|
|
119
118
|
}
|
|
120
119
|
|
|
121
120
|
const pair = box.keyPair(),
|
|
122
|
-
nonce = randomBytes(box.nonceLength)
|
|
123
|
-
pubBase64 = Buffer.from(pair.publicKey).toString('base64'),
|
|
124
|
-
nonceBase64 = Buffer.from(nonce).toString('base64');
|
|
121
|
+
nonce = randomBytes(box.nonceLength);
|
|
125
122
|
|
|
126
123
|
return [
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
124
|
+
serialize([
|
|
125
|
+
pair.publicKey,
|
|
126
|
+
nonce,
|
|
127
|
+
Buffer.from(
|
|
128
|
+
box(
|
|
129
|
+
inputData,
|
|
130
|
+
nonce,
|
|
131
|
+
serverPublicKey,
|
|
132
|
+
pair.secretKey
|
|
133
|
+
)
|
|
133
134
|
)
|
|
134
|
-
)
|
|
135
|
+
]),
|
|
135
136
|
[pair.secretKey, pair.publicKey]
|
|
136
137
|
];
|
|
137
138
|
};
|
|
138
139
|
|
|
139
|
-
export const deserializeE2E = (data
|
|
140
|
-
const [binaryNonce, binaryData] = data
|
|
140
|
+
export const deserializeE2E = async (data, serverPublicKey, clientPrivateKey) => {
|
|
141
|
+
const [binaryNonce, binaryData] = deserialize(data);
|
|
141
142
|
let baseArray;
|
|
142
143
|
|
|
143
144
|
if (binaryData.byteLength > 10240) {
|
|
144
145
|
// dispatch to background thread
|
|
145
|
-
baseArray = e2e_worker.decrypt(binaryData, binaryNonce, serverPublicKey, clientPrivateKey);
|
|
146
|
+
baseArray = await e2e_worker.decrypt(binaryData, binaryNonce, serverPublicKey, clientPrivateKey);
|
|
146
147
|
} else {
|
|
147
148
|
baseArray = box.open(binaryData, binaryNonce, serverPublicKey, clientPrivateKey);
|
|
148
149
|
}
|
|
149
150
|
|
|
150
151
|
if (!baseArray) throw 'Decrypting e2e message failed';
|
|
151
|
-
return
|
|
152
|
+
return deserialize(baseArray);
|
|
152
153
|
};
|
|
153
154
|
|
|
154
155
|
export const encodeBinary = (s) => Buffer.from(s, 'utf8').toString('base64');
|
package/src/helpers/utils.js
CHANGED
|
@@ -4,6 +4,7 @@ import { CacheStore, Scoped } from "./variables";
|
|
|
4
4
|
import { decryptString, encryptString, serializeE2E } from "./peripherals";
|
|
5
5
|
import { deserializeBSON, serializeToBase64 } from "../products/database/bson";
|
|
6
6
|
import { trySendPendingWrite } from "../products/database";
|
|
7
|
+
import { deserialize } from "entity-serializer";
|
|
7
8
|
|
|
8
9
|
export const updateCacheStore = (timer = 300) => {
|
|
9
10
|
try { window } catch (_) { return; }
|
|
@@ -116,18 +117,29 @@ export const getReachableServer = (projectUrl) => new Promise(resolve => {
|
|
|
116
117
|
}, true);
|
|
117
118
|
});
|
|
118
119
|
|
|
119
|
-
export const buildFetchInterface = ({ body, accessKey, authToken, method, uglify, serverE2E_PublicKey }) => {
|
|
120
|
+
export const buildFetchInterface = async ({ body, accessKey, authToken, method, uglify, serverE2E_PublicKey }) => {
|
|
120
121
|
if (!uglify) body = JSON.stringify({ ...body });
|
|
121
|
-
const [plate, keyPair] = uglify ? serializeE2E(body, authToken, serverE2E_PublicKey) : [undefined, []];
|
|
122
|
+
const [plate, keyPair] = uglify ? await serializeE2E(body, authToken, serverE2E_PublicKey) : [undefined, []];
|
|
122
123
|
|
|
123
124
|
return [{
|
|
124
125
|
body: uglify ? plate : body,
|
|
125
126
|
cache: 'no-cache',
|
|
126
127
|
headers: {
|
|
127
|
-
'Content-type': uglify ? '
|
|
128
|
+
'Content-type': uglify ? 'request/buffer' : 'application/json',
|
|
128
129
|
'Authorization': accessKey,
|
|
129
130
|
...((authToken && !uglify) ? { 'Mosquito-Token': authToken } : {})
|
|
130
131
|
},
|
|
131
132
|
method: method || 'POST'
|
|
132
133
|
}, keyPair];
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export const buildFetchResult = async (fetchRef, ugly) => {
|
|
137
|
+
if (ugly) {
|
|
138
|
+
const [data, simpleError] = deserialize(await fetchRef.arrayBuffer());
|
|
139
|
+
if (simpleError) throw simpleError;
|
|
140
|
+
return data;
|
|
141
|
+
}
|
|
142
|
+
const json = await fetchRef.json();
|
|
143
|
+
if (json.simpleError) throw json;
|
|
144
|
+
return json;
|
|
133
145
|
};
|
package/src/index.d.ts
CHANGED
|
@@ -115,7 +115,7 @@ interface MTSocket {
|
|
|
115
115
|
emitWithAck: (...args: any) => Promise<any>;
|
|
116
116
|
});
|
|
117
117
|
emit: (...args: any) => void;
|
|
118
|
-
emitWithAck: () => Promise<any>;
|
|
118
|
+
emitWithAck: (...args: any) => Promise<any>;
|
|
119
119
|
on: (route: string, callback?: () => any) => void;
|
|
120
120
|
once: (route: string, callback?: () => any) => void;
|
|
121
121
|
destroy: () => void;
|
package/src/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import './polyfill';
|
|
1
2
|
import { deserializeE2E, listenReachableServer, serializeE2E } from "./helpers/peripherals";
|
|
2
3
|
import { releaseCacheStore, awaitStore } from "./helpers/utils";
|
|
3
4
|
import { CacheStore, Scoped } from "./helpers/variables";
|
|
@@ -11,10 +12,10 @@ import { mfetch } from "./products/http_callable";
|
|
|
11
12
|
import { io } from "socket.io-client";
|
|
12
13
|
import { AUTH_PROVIDER_ID, CACHE_PROTOCOL } from "./helpers/values";
|
|
13
14
|
import EngineApi from './helpers/engine_api';
|
|
14
|
-
import { parse, stringify } from 'json-buffer';
|
|
15
15
|
import { Validator } from 'guard-object';
|
|
16
16
|
import sendMessage from "./helpers/broadcaster";
|
|
17
17
|
import cloneDeep from "lodash.clonedeep";
|
|
18
|
+
import { Buffer } from "buffer";
|
|
18
19
|
|
|
19
20
|
const {
|
|
20
21
|
_listenCollection,
|
|
@@ -177,7 +178,7 @@ export class MosquitoTransport {
|
|
|
177
178
|
tokenListener,
|
|
178
179
|
clientPrivateKey;
|
|
179
180
|
|
|
180
|
-
const listenerCallback = (route, callback) => function () {
|
|
181
|
+
const listenerCallback = (route, callback) => async function () {
|
|
181
182
|
if (reservedEventName.includes(route)) {
|
|
182
183
|
callback?.(...[...arguments]);
|
|
183
184
|
return;
|
|
@@ -187,15 +188,15 @@ export class MosquitoTransport {
|
|
|
187
188
|
let res;
|
|
188
189
|
|
|
189
190
|
if (uglify) {
|
|
190
|
-
res =
|
|
191
|
+
res = await deserializeE2E(args, serverE2E_PublicKey, clientPrivateKey);
|
|
191
192
|
} else res = args;
|
|
192
193
|
|
|
193
|
-
callback?.(...res || [], ...typeof emitable === 'function' ? [function () {
|
|
194
|
+
callback?.(...res || [], ...typeof emitable === 'function' ? [async function () {
|
|
194
195
|
const args = [...arguments];
|
|
195
196
|
let res;
|
|
196
197
|
|
|
197
198
|
if (uglify) {
|
|
198
|
-
res = serializeE2E(
|
|
199
|
+
res = (await serializeE2E(args, undefined, serverE2E_PublicKey))[0];
|
|
199
200
|
} else res = args;
|
|
200
201
|
|
|
201
202
|
emitable(res);
|
|
@@ -229,26 +230,26 @@ export class MosquitoTransport {
|
|
|
229
230
|
const hasEmitable = typeof lastEmit === 'function';
|
|
230
231
|
const mit = hasEmitable ? emittion.slice(0, -1) : emittion;
|
|
231
232
|
|
|
232
|
-
const [reqBuilder, [privateKey]] = uglify ? serializeE2E(
|
|
233
|
+
const [reqBuilder, [privateKey]] = uglify ? await serializeE2E(mit, undefined, serverE2E_PublicKey) : [undefined, []];
|
|
233
234
|
|
|
234
235
|
if (hasEmitable && promise)
|
|
235
236
|
throw 'emitWithAck cannot have function in it argument';
|
|
236
237
|
|
|
237
238
|
const result = await thisSocket[promise ? 'emitWithAck' : 'emit'](route,
|
|
238
239
|
uglify ? reqBuilder : mit,
|
|
239
|
-
...hasEmitable ? [function () {
|
|
240
|
+
...hasEmitable ? [async function () {
|
|
240
241
|
const [args] = [...arguments];
|
|
241
242
|
let res;
|
|
242
243
|
|
|
243
244
|
if (uglify) {
|
|
244
|
-
res =
|
|
245
|
+
res = await deserializeE2E(args, serverE2E_PublicKey, privateKey);
|
|
245
246
|
} else res = args;
|
|
246
247
|
|
|
247
248
|
lastEmit(...res || []);
|
|
248
249
|
}] : []
|
|
249
250
|
);
|
|
250
251
|
|
|
251
|
-
resolve((promise && result) ? uglify ?
|
|
252
|
+
resolve((promise && result) ? uglify ? (await deserializeE2E(result, serverE2E_PublicKey, privateKey))[0] : result[0] : undefined);
|
|
252
253
|
} catch (e) {
|
|
253
254
|
reject(e);
|
|
254
255
|
}
|
|
@@ -257,7 +258,7 @@ export class MosquitoTransport {
|
|
|
257
258
|
const init = async () => {
|
|
258
259
|
if (hasCancelled) return;
|
|
259
260
|
const mtoken = disableAuth ? undefined : Scoped.AuthJWTToken[projectUrl];
|
|
260
|
-
const [reqBuilder, [privateKey]] = uglify ? serializeE2E({ accessKey, a_extras: authHandshake }, mtoken, serverE2E_PublicKey) : [null, []];
|
|
261
|
+
const [reqBuilder, [privateKey]] = uglify ? await serializeE2E({ accessKey, a_extras: authHandshake }, mtoken, serverE2E_PublicKey) : [null, []];
|
|
261
262
|
|
|
262
263
|
socket = io(`${wsPrefix}://${projectUrl.split('://')[1]}`, {
|
|
263
264
|
transports: ['websocket', 'polling', 'flashsocket'],
|
package/src/polyfill.js
ADDED
|
@@ -81,7 +81,7 @@ const refreshToken = (builder, processRef, remainRetries = 7, initialRetries = 7
|
|
|
81
81
|
try {
|
|
82
82
|
const { token, refreshToken: r_token } = CacheStore.AuthStore[projectUrl];
|
|
83
83
|
|
|
84
|
-
const [reqBuilder, [privateKey]] = buildFetchInterface({
|
|
84
|
+
const [reqBuilder, [privateKey]] = await buildFetchInterface({
|
|
85
85
|
body: { token, r_token },
|
|
86
86
|
accessKey,
|
|
87
87
|
uglify,
|
|
@@ -96,7 +96,7 @@ const refreshToken = (builder, processRef, remainRetries = 7, initialRetries = 7
|
|
|
96
96
|
}
|
|
97
97
|
if (r.simpleError) throw r;
|
|
98
98
|
|
|
99
|
-
const f = uglify ? deserializeE2E(r.e2e, serverE2E_PublicKey, privateKey) : r;
|
|
99
|
+
const f = uglify ? await deserializeE2E(r.e2e, serverE2E_PublicKey, privateKey) : r;
|
|
100
100
|
|
|
101
101
|
if (CacheStore.AuthStore[projectUrl]) {
|
|
102
102
|
CacheStore.AuthStore[projectUrl].token = f.result.token;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { io } from "socket.io-client";
|
|
2
2
|
import EngineApi from "../../helpers/engine_api";
|
|
3
3
|
import { TokenRefreshListener } from "../../helpers/listeners";
|
|
4
|
-
import { awaitReachableServer, awaitStore, buildFetchInterface, updateCacheStore } from "../../helpers/utils";
|
|
4
|
+
import { awaitReachableServer, awaitStore, buildFetchInterface, buildFetchResult, updateCacheStore } from "../../helpers/utils";
|
|
5
5
|
import { CacheStore, Scoped } from "../../helpers/variables";
|
|
6
6
|
import { awaitRefreshToken, initTokenRefresher, injectFreshToken, listenToken, parseToken, triggerAuthToken } from "./accessor";
|
|
7
7
|
import { deserializeE2E, encodeBinary, serializeE2E } from "../../helpers/peripherals";
|
|
@@ -61,7 +61,7 @@ export class MTAuth {
|
|
|
61
61
|
}
|
|
62
62
|
if (processID !== lastInitRef) return;
|
|
63
63
|
const mtoken = Scoped.AuthJWTToken[projectUrl],
|
|
64
|
-
[reqBuilder, [privateKey]] = uglify ? serializeE2E({ mtoken }, undefined, serverE2E_PublicKey) : [null, []];
|
|
64
|
+
[reqBuilder, [privateKey]] = uglify ? await serializeE2E({ mtoken }, undefined, serverE2E_PublicKey) : [null, []];
|
|
65
65
|
|
|
66
66
|
socket = io(`${wsPrefix}://${baseUrl}`, {
|
|
67
67
|
transports: ['websocket', 'polling', 'flashsocket'],
|
|
@@ -74,11 +74,11 @@ export class MTAuth {
|
|
|
74
74
|
|
|
75
75
|
socket.emit(_listenUserVerification(uglify));
|
|
76
76
|
|
|
77
|
-
socket.on("onVerificationChanged", ([err, verified]) => {
|
|
77
|
+
socket.on("onVerificationChanged", async ([err, verified]) => {
|
|
78
78
|
if (err) {
|
|
79
79
|
onError?.(simplifyCaughtError(err).simpleError);
|
|
80
80
|
} else {
|
|
81
|
-
callback?.(uglify ? deserializeE2E(verified, serverE2E_PublicKey, privateKey) : verified);
|
|
81
|
+
callback?.(uglify ? await deserializeE2E(verified, serverE2E_PublicKey, privateKey) : verified);
|
|
82
82
|
}
|
|
83
83
|
});
|
|
84
84
|
|
|
@@ -171,17 +171,16 @@ const doCustomSignin = (builder, email, password) => new Promise(async (resolve,
|
|
|
171
171
|
|
|
172
172
|
try {
|
|
173
173
|
await awaitStore();
|
|
174
|
-
const [reqBuilder, [privateKey]] = buildFetchInterface({
|
|
174
|
+
const [reqBuilder, [privateKey]] = await buildFetchInterface({
|
|
175
175
|
body: { data: `${encodeBinary(email)}.${encodeBinary(password)}` },
|
|
176
176
|
accessKey,
|
|
177
177
|
serverE2E_PublicKey,
|
|
178
178
|
uglify
|
|
179
179
|
});
|
|
180
180
|
|
|
181
|
-
const
|
|
182
|
-
if (f.simpleError) throw f;
|
|
181
|
+
const data = await buildFetchResult(await fetch(_customSignin(projectUrl, uglify), reqBuilder), uglify);
|
|
183
182
|
|
|
184
|
-
const r = uglify ? deserializeE2E(
|
|
183
|
+
const r = uglify ? await deserializeE2E(data, serverE2E_PublicKey, privateKey) : data;
|
|
185
184
|
|
|
186
185
|
resolve({
|
|
187
186
|
user: parseToken(r.result.token),
|
|
@@ -199,7 +198,7 @@ const doCustomSignup = (builder, email, password, name, metadata) => new Promise
|
|
|
199
198
|
|
|
200
199
|
try {
|
|
201
200
|
await awaitStore();
|
|
202
|
-
const [reqBuilder, [privateKey]] = buildFetchInterface({
|
|
201
|
+
const [reqBuilder, [privateKey]] = await buildFetchInterface({
|
|
203
202
|
body: {
|
|
204
203
|
data: `${encodeBinary(email)}.${encodeBinary(password)}.${(encodeBinary((name || '').trim()))}`,
|
|
205
204
|
metadata,
|
|
@@ -209,10 +208,9 @@ const doCustomSignup = (builder, email, password, name, metadata) => new Promise
|
|
|
209
208
|
uglify
|
|
210
209
|
});
|
|
211
210
|
|
|
212
|
-
const
|
|
213
|
-
if (f.simpleError) throw f;
|
|
211
|
+
const data = await buildFetchResult(await fetch(_customSignup(projectUrl, uglify), reqBuilder), uglify);
|
|
214
212
|
|
|
215
|
-
const r = uglify ? deserializeE2E(
|
|
213
|
+
const r = uglify ? await deserializeE2E(data, serverE2E_PublicKey, privateKey) : data;
|
|
216
214
|
|
|
217
215
|
resolve({
|
|
218
216
|
user: parseToken(r.result.token),
|
|
@@ -249,7 +247,7 @@ export const doSignOut = async (builder) => {
|
|
|
249
247
|
try {
|
|
250
248
|
await awaitReachableServer(projectUrl);
|
|
251
249
|
|
|
252
|
-
const [reqBuilder] = buildFetchInterface({
|
|
250
|
+
const [reqBuilder] = await buildFetchInterface({
|
|
253
251
|
body: { token, r_token },
|
|
254
252
|
accessKey,
|
|
255
253
|
uglify,
|
|
@@ -269,17 +267,16 @@ const doGoogleSignin = (builder, token) => new Promise(async (resolve, reject) =
|
|
|
269
267
|
|
|
270
268
|
try {
|
|
271
269
|
await awaitStore();
|
|
272
|
-
const [reqBuilder, [privateKey]] = buildFetchInterface({
|
|
270
|
+
const [reqBuilder, [privateKey]] = await buildFetchInterface({
|
|
273
271
|
body: { token },
|
|
274
272
|
accessKey,
|
|
275
273
|
uglify,
|
|
276
274
|
serverE2E_PublicKey
|
|
277
275
|
});
|
|
278
276
|
|
|
279
|
-
const
|
|
280
|
-
if (r.simpleError) throw r;
|
|
277
|
+
const data = await buildFetchResult(await fetch(_googleSignin(projectUrl, uglify), reqBuilder), uglify);
|
|
281
278
|
|
|
282
|
-
const f = uglify ? deserializeE2E(
|
|
279
|
+
const f = uglify ? await deserializeE2E(data, serverE2E_PublicKey, privateKey) : data;
|
|
283
280
|
|
|
284
281
|
resolve({
|
|
285
282
|
user: parseToken(f.result.token),
|
|
@@ -2,7 +2,7 @@ import { io } from "socket.io-client";
|
|
|
2
2
|
import EngineApi from "../../helpers/engine_api";
|
|
3
3
|
import { DatabaseRecordsListener } from "../../helpers/listeners";
|
|
4
4
|
import { deserializeE2E, listenReachableServer, niceTry, serializeE2E } from "../../helpers/peripherals";
|
|
5
|
-
import { awaitStore, buildFetchInterface, getReachableServer } from "../../helpers/utils";
|
|
5
|
+
import { awaitStore, buildFetchInterface, buildFetchResult, getReachableServer } from "../../helpers/utils";
|
|
6
6
|
import { CacheStore, Scoped } from "../../helpers/variables";
|
|
7
7
|
import { addPendingWrites, generateRecordID, getRecord, insertRecord, listenQueryEntry, removePendingWrite, validateWriteValue } from "./accessor";
|
|
8
8
|
import { validateCollectionName, validateFilter, validateFindConfig, validateFindObject, validateListenFindConfig } from "./validator";
|
|
@@ -197,7 +197,7 @@ const listenDocument = (callback, onError, builder, config) => {
|
|
|
197
197
|
dbUrl
|
|
198
198
|
};
|
|
199
199
|
|
|
200
|
-
const [encPlate, [privateKey]] = uglify ? serializeE2E({ accessKey, _body: authObj }, mtoken, serverE2E_PublicKey) : ['', []];
|
|
200
|
+
const [encPlate, [privateKey]] = uglify ? await serializeE2E({ accessKey, _body: authObj }, mtoken, serverE2E_PublicKey) : ['', []];
|
|
201
201
|
|
|
202
202
|
socket = io(`${wsPrefix}://${baseUrl}`, {
|
|
203
203
|
transports: ['websocket', 'polling', 'flashsocket'],
|
|
@@ -218,7 +218,7 @@ const listenDocument = (callback, onError, builder, config) => {
|
|
|
218
218
|
onError(simplifyCaughtError(err).simpleError);
|
|
219
219
|
} else console.error('unhandled listen for:', { path, find }, ' error:', err);
|
|
220
220
|
} else {
|
|
221
|
-
if (uglify) snapshot = deserializeE2E(snapshot, serverE2E_PublicKey, privateKey);
|
|
221
|
+
if (uglify) snapshot = await deserializeE2E(snapshot, serverE2E_PublicKey, privateKey);
|
|
222
222
|
snapshot = deserializeBSON(snapshot)._;
|
|
223
223
|
dispatchSnapshot(snapshot);
|
|
224
224
|
|
|
@@ -290,7 +290,7 @@ const initOnDisconnectionTask = (builder, value, type) => {
|
|
|
290
290
|
socket = io(`${wsPrefix}://${baseUrl}`, {
|
|
291
291
|
transports: ['websocket', 'polling', 'flashsocket'],
|
|
292
292
|
auth: uglify ? {
|
|
293
|
-
e2e: serializeE2E({ accessKey, _body: authObj }, mtoken, serverE2E_PublicKey)[0],
|
|
293
|
+
e2e: (await serializeE2E({ accessKey, _body: authObj }, mtoken, serverE2E_PublicKey))[0],
|
|
294
294
|
_m_internal: true
|
|
295
295
|
} : {
|
|
296
296
|
...mtoken ? { mtoken } : {},
|
|
@@ -365,7 +365,7 @@ const countCollection = async (builder, config) => {
|
|
|
365
365
|
if (!disableAuth && await getReachableServer(projectUrl))
|
|
366
366
|
await awaitRefreshToken(projectUrl);
|
|
367
367
|
|
|
368
|
-
const [reqBuilder, [privateKey]] = buildFetchInterface({
|
|
368
|
+
const [reqBuilder, [privateKey]] = await buildFetchInterface({
|
|
369
369
|
body: {
|
|
370
370
|
commands: { path, find: serializeToBase64(find) },
|
|
371
371
|
dbName,
|
|
@@ -377,10 +377,9 @@ const countCollection = async (builder, config) => {
|
|
|
377
377
|
uglify
|
|
378
378
|
});
|
|
379
379
|
|
|
380
|
-
const
|
|
381
|
-
if (r.simpleError) throw r;
|
|
380
|
+
const data = await buildFetchResult(await fetch(_documentCount(projectUrl, uglify), reqBuilder), uglify);
|
|
382
381
|
|
|
383
|
-
const f = uglify ? deserializeE2E(
|
|
382
|
+
const f = uglify ? await deserializeE2E(data, serverE2E_PublicKey, privateKey) : data;
|
|
384
383
|
|
|
385
384
|
if (!disableCache)
|
|
386
385
|
setLodash(CacheStore.DatabaseCountResult, [projectUrl, dbUrl, dbName, accessId], f.result);
|
|
@@ -497,7 +496,7 @@ const findObject = async (builder, config) => {
|
|
|
497
496
|
if (!disableAuth && await getReachableServer(projectUrl))
|
|
498
497
|
await awaitRefreshToken(projectUrl);
|
|
499
498
|
|
|
500
|
-
const [reqBuilder, [privateKey]] = buildFetchInterface({
|
|
499
|
+
const [reqBuilder, [privateKey]] = await buildFetchInterface({
|
|
501
500
|
body: {
|
|
502
501
|
commands: {
|
|
503
502
|
config: pureConfig && serializeToBase64(pureConfig),
|
|
@@ -517,10 +516,9 @@ const findObject = async (builder, config) => {
|
|
|
517
516
|
uglify
|
|
518
517
|
});
|
|
519
518
|
|
|
520
|
-
const
|
|
521
|
-
if (r.simpleError) throw r;
|
|
519
|
+
const data = await buildFetchResult(await fetch((findOne ? _readDocument : _queryCollection)(projectUrl, uglify), reqBuilder), uglify);
|
|
522
520
|
|
|
523
|
-
const result = deserializeBSON((uglify ? deserializeE2E(
|
|
521
|
+
const result = deserializeBSON((uglify ? await deserializeE2E(data, serverE2E_PublicKey, privateKey) : data).result)._;
|
|
524
522
|
|
|
525
523
|
if (shouldCache) insertRecord(builder, config, accessId, result);
|
|
526
524
|
finalize({ liveResult: result || null });
|
|
@@ -634,7 +632,7 @@ const commitData = async (builder, value, type, config) => {
|
|
|
634
632
|
if (!disableAuth && await getReachableServer(projectUrl))
|
|
635
633
|
await awaitRefreshToken(projectUrl);
|
|
636
634
|
|
|
637
|
-
const [reqBuilder, [privateKey]] = buildFetchInterface({
|
|
635
|
+
const [reqBuilder, [privateKey]] = await buildFetchInterface({
|
|
638
636
|
body: {
|
|
639
637
|
commands: {
|
|
640
638
|
value: value && serializeToBase64({ _: value }),
|
|
@@ -653,12 +651,11 @@ const commitData = async (builder, value, type, config) => {
|
|
|
653
651
|
uglify
|
|
654
652
|
});
|
|
655
653
|
|
|
656
|
-
const
|
|
657
|
-
if (r.simpleError) throw r;
|
|
654
|
+
const data = await buildFetchResult(await fetch((isBatchWrite ? _writeMapDocument : _writeDocument)(projectUrl, uglify), reqBuilder), uglify);
|
|
658
655
|
|
|
659
|
-
const f = uglify ? deserializeE2E(
|
|
656
|
+
const f = uglify ? await deserializeE2E(data, serverE2E_PublicKey, privateKey) : data;
|
|
660
657
|
|
|
661
|
-
finalize({ ...f }, undefined, { removeCache: true });
|
|
658
|
+
finalize({ ...f.statusData }, undefined, { removeCache: true });
|
|
662
659
|
} catch (e) {
|
|
663
660
|
if (e?.simpleError) {
|
|
664
661
|
console.error(`${type} error (${path}), ${e.simpleError?.message}`);
|
|
@@ -7,7 +7,7 @@ import { awaitRefreshToken } from "../auth/accessor";
|
|
|
7
7
|
import { simplifyCaughtError } from "simplify-error";
|
|
8
8
|
import { guardObject, Validator } from "guard-object";
|
|
9
9
|
import cloneDeep from "lodash.clonedeep";
|
|
10
|
-
import {
|
|
10
|
+
import { serialize } from "entity-serializer";
|
|
11
11
|
|
|
12
12
|
const buildFetchData = (data) => {
|
|
13
13
|
const { ok, type, status, statusText, redirected, url, headers, size, base64 } = data;
|
|
@@ -61,7 +61,7 @@ export const mfetch = async (input = '', init, config) => {
|
|
|
61
61
|
if ('uglified' in rawHeader)
|
|
62
62
|
throw '"uglified" in header is a reserved prop';
|
|
63
63
|
|
|
64
|
-
if (
|
|
64
|
+
if (isBaseUrl && !rawApproach)
|
|
65
65
|
throw `please set { rawApproach: true } if you're trying to access different endpoint at "${input}"`;
|
|
66
66
|
|
|
67
67
|
if (body !== undefined) {
|
|
@@ -74,15 +74,15 @@ export const mfetch = async (input = '', init, config) => {
|
|
|
74
74
|
) throw `"body" must be any of string, buffer, object, File, Blob`;
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
-
const rawBody =
|
|
77
|
+
const rawBody = (body instanceof File || body instanceof Blob) ? Buffer.from(await body.arrayBuffer()) : body;
|
|
78
78
|
|
|
79
79
|
const reqId = await niceHash(
|
|
80
|
-
|
|
80
|
+
serialize([
|
|
81
81
|
rawHeader,
|
|
82
82
|
rawBody,
|
|
83
83
|
!!disableAuth,
|
|
84
84
|
input
|
|
85
|
-
])
|
|
85
|
+
]).toString('base64')
|
|
86
86
|
);
|
|
87
87
|
|
|
88
88
|
let retries = 0, hasFinalize;
|
|
@@ -145,7 +145,7 @@ export const mfetch = async (input = '', init, config) => {
|
|
|
145
145
|
const mtoken = Scoped.AuthJWTToken[projectUrl];
|
|
146
146
|
const initType = rawHeader['content-type'];
|
|
147
147
|
|
|
148
|
-
const [reqBuilder, [privateKey]] = uglified ? serializeE2E(rawBody, mtoken, serverE2E_PublicKey) : [null, []];
|
|
148
|
+
const [reqBuilder, [privateKey]] = uglified ? await serializeE2E(rawBody, mtoken, serverE2E_PublicKey) : [null, []];
|
|
149
149
|
|
|
150
150
|
const f = await fetch(isBaseUrl ? input : `${projectUrl}/${normalizeRoute(input)}`, {
|
|
151
151
|
...isBaseUrl ? {} : { method: 'POST' },
|
|
@@ -157,7 +157,7 @@ export const mfetch = async (input = '', init, config) => {
|
|
|
157
157
|
...rawHeader,
|
|
158
158
|
...uglified ? {
|
|
159
159
|
uglified,
|
|
160
|
-
'content-type': '
|
|
160
|
+
'content-type': 'request/buffer',
|
|
161
161
|
...initType ? { 'init-content-type': initType } : {}
|
|
162
162
|
} : {},
|
|
163
163
|
...(disableAuth || !mtoken || uglified || isBaseUrl) ? {} : { mtoken },
|
|
@@ -170,7 +170,7 @@ export const mfetch = async (input = '', init, config) => {
|
|
|
170
170
|
if (!isBaseUrl && simple) throw { simpleError: JSON.parse(simple) };
|
|
171
171
|
|
|
172
172
|
const base64 = uglified ?
|
|
173
|
-
Buffer.from(deserializeE2E(await f.
|
|
173
|
+
Buffer.from(await deserializeE2E(await f.arrayBuffer(), serverE2E_PublicKey, privateKey)).toString('base64') :
|
|
174
174
|
Buffer.from(await f.arrayBuffer()).toString('base64');
|
|
175
175
|
|
|
176
176
|
const resObj = {
|
|
@@ -105,7 +105,7 @@ const deleteContent = async (builder, path, isFolder) => {
|
|
|
105
105
|
try {
|
|
106
106
|
const r = await (await fetch(
|
|
107
107
|
EngineApi[isFolder ? '_deleteFolder' : '_deleteFile'](projectUrl, uglify),
|
|
108
|
-
buildFetchInterface({ path }, accessKey, Scoped.AuthJWTToken[projectUrl], 'DELETE')
|
|
108
|
+
await buildFetchInterface({ path }, accessKey, Scoped.AuthJWTToken[projectUrl], 'DELETE')
|
|
109
109
|
)).json();
|
|
110
110
|
if (r.simpleError) throw r;
|
|
111
111
|
if (r.status !== 'success') throw 'operation not successful';
|