mosquito-transport-js 0.3.2 → 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
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;
|
|
@@ -104,51 +105,51 @@ export const decryptString = (txt, password, iv) => {
|
|
|
104
105
|
};
|
|
105
106
|
|
|
106
107
|
export const serializeE2E = async (data, auth_token, serverPublicKey) => {
|
|
107
|
-
const inputData =
|
|
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 = async (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; }
|
|
@@ -124,10 +125,21 @@ export const buildFetchInterface = async ({ body, accessKey, authToken, method,
|
|
|
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.js
CHANGED
|
@@ -12,7 +12,6 @@ import { mfetch } from "./products/http_callable";
|
|
|
12
12
|
import { io } from "socket.io-client";
|
|
13
13
|
import { AUTH_PROVIDER_ID, CACHE_PROTOCOL } from "./helpers/values";
|
|
14
14
|
import EngineApi from './helpers/engine_api';
|
|
15
|
-
import { parse, stringify } from 'json-buffer';
|
|
16
15
|
import { Validator } from 'guard-object';
|
|
17
16
|
import sendMessage from "./helpers/broadcaster";
|
|
18
17
|
import cloneDeep from "lodash.clonedeep";
|
|
@@ -189,7 +188,7 @@ export class MosquitoTransport {
|
|
|
189
188
|
let res;
|
|
190
189
|
|
|
191
190
|
if (uglify) {
|
|
192
|
-
res =
|
|
191
|
+
res = await deserializeE2E(args, serverE2E_PublicKey, clientPrivateKey);
|
|
193
192
|
} else res = args;
|
|
194
193
|
|
|
195
194
|
callback?.(...res || [], ...typeof emitable === 'function' ? [async function () {
|
|
@@ -197,7 +196,7 @@ export class MosquitoTransport {
|
|
|
197
196
|
let res;
|
|
198
197
|
|
|
199
198
|
if (uglify) {
|
|
200
|
-
res = (await serializeE2E(
|
|
199
|
+
res = (await serializeE2E(args, undefined, serverE2E_PublicKey))[0];
|
|
201
200
|
} else res = args;
|
|
202
201
|
|
|
203
202
|
emitable(res);
|
|
@@ -231,7 +230,7 @@ export class MosquitoTransport {
|
|
|
231
230
|
const hasEmitable = typeof lastEmit === 'function';
|
|
232
231
|
const mit = hasEmitable ? emittion.slice(0, -1) : emittion;
|
|
233
232
|
|
|
234
|
-
const [reqBuilder, [privateKey]] = uglify ? await serializeE2E(
|
|
233
|
+
const [reqBuilder, [privateKey]] = uglify ? await serializeE2E(mit, undefined, serverE2E_PublicKey) : [undefined, []];
|
|
235
234
|
|
|
236
235
|
if (hasEmitable && promise)
|
|
237
236
|
throw 'emitWithAck cannot have function in it argument';
|
|
@@ -243,14 +242,14 @@ export class MosquitoTransport {
|
|
|
243
242
|
let res;
|
|
244
243
|
|
|
245
244
|
if (uglify) {
|
|
246
|
-
res =
|
|
245
|
+
res = await deserializeE2E(args, serverE2E_PublicKey, privateKey);
|
|
247
246
|
} else res = args;
|
|
248
247
|
|
|
249
248
|
lastEmit(...res || []);
|
|
250
249
|
}] : []
|
|
251
250
|
);
|
|
252
251
|
|
|
253
|
-
resolve((promise && result) ? uglify ?
|
|
252
|
+
resolve((promise && result) ? uglify ? (await deserializeE2E(result, serverE2E_PublicKey, privateKey))[0] : result[0] : undefined);
|
|
254
253
|
} catch (e) {
|
|
255
254
|
reject(e);
|
|
256
255
|
}
|
|
@@ -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";
|
|
@@ -178,10 +178,9 @@ const doCustomSignin = (builder, email, password) => new Promise(async (resolve,
|
|
|
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 ? await deserializeE2E(
|
|
183
|
+
const r = uglify ? await deserializeE2E(data, serverE2E_PublicKey, privateKey) : data;
|
|
185
184
|
|
|
186
185
|
resolve({
|
|
187
186
|
user: parseToken(r.result.token),
|
|
@@ -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 ? await deserializeE2E(
|
|
213
|
+
const r = uglify ? await deserializeE2E(data, serverE2E_PublicKey, privateKey) : data;
|
|
216
214
|
|
|
217
215
|
resolve({
|
|
218
216
|
user: parseToken(r.result.token),
|
|
@@ -276,10 +274,9 @@ const doGoogleSignin = (builder, token) => new Promise(async (resolve, reject) =
|
|
|
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 ? await 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";
|
|
@@ -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 ? await 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);
|
|
@@ -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 ? await 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 });
|
|
@@ -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 ? await 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;
|
|
@@ -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;
|
|
@@ -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(await 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 = {
|