mosquito-transport 1.7.0 → 1.7.2
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/TODO +2 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +11 -2
- package/lib/products/database/base.js +9 -6
- package/lib/products/database/index.js +15 -8
- package/lib/products/storage/store.js +33 -13
- package/package.json +2 -2
package/TODO
CHANGED
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -35,6 +35,7 @@ import { statusErrorCode, useDDOS, validateDDOS_Config } from "./helpers/ddos.js
|
|
|
35
35
|
import mime from 'mime';
|
|
36
36
|
import LimitTasks from "limit-task";
|
|
37
37
|
import { cpus } from "os";
|
|
38
|
+
import { deserialize } from "entity-serializer";
|
|
38
39
|
|
|
39
40
|
const { box } = naclPkg;
|
|
40
41
|
|
|
@@ -602,8 +603,14 @@ const useMosquitoServer = (app, config) => {
|
|
|
602
603
|
},
|
|
603
604
|
disconnect: (...args) => {
|
|
604
605
|
socket.disconnect(...args);
|
|
605
|
-
}
|
|
606
|
+
},
|
|
607
|
+
disconnected: false
|
|
606
608
|
};
|
|
609
|
+
|
|
610
|
+
socket.on('disconnect', () => {
|
|
611
|
+
clonedSocket.disconnected = true;
|
|
612
|
+
});
|
|
613
|
+
// TODO: disconnected
|
|
607
614
|
onSocketSnapshot(clonedSocket);
|
|
608
615
|
} catch (e) {
|
|
609
616
|
onSocketError?.(Object.assign(simplifyCaughtError(e).simpleError, { socket }));
|
|
@@ -836,6 +843,8 @@ export default class MosquitoTransportServer {
|
|
|
836
843
|
reqBody = JSON.parse(body);
|
|
837
844
|
} catch (_) { }
|
|
838
845
|
} else reqBody = body;
|
|
846
|
+
} else if (req.headers['content-type'] === undefined && req.body) {
|
|
847
|
+
reqBody = deserialize(req.body);
|
|
839
848
|
}
|
|
840
849
|
|
|
841
850
|
req.body = reqBody;
|
|
@@ -956,7 +965,7 @@ export default class MosquitoTransportServer {
|
|
|
956
965
|
deleteFile = async (path = '') => {
|
|
957
966
|
if (Validator.LINK(path)) {
|
|
958
967
|
const url = new URL(path);
|
|
959
|
-
if (url.pathname.startsWith(`${STORAGE_ROUTE}/`))
|
|
968
|
+
if (!url.pathname.startsWith(`${STORAGE_ROUTE}/`))
|
|
960
969
|
throw `link must have a pathname that starts with ${STORAGE_ROUTE}/`;
|
|
961
970
|
path = url.pathname.substring(STORAGE_ROUTE.length);
|
|
962
971
|
}
|
|
@@ -6,16 +6,19 @@ import { Scoped } from "../../helpers/variables.js";
|
|
|
6
6
|
*/
|
|
7
7
|
export const getDB = (projectName, name, url = DEFAULT_DB) => {
|
|
8
8
|
if (!projectName) throw 'expected projectName in getDb()';
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const dbUrl = url === ADMIN_DB_URL ? 'admin' : url === DEFAULT_DB ? 'default' : url;
|
|
12
|
-
const { defaultName: dbName, instance } = getDbInstance(projectName, dbUrl) || {};
|
|
9
|
+
const { defaultName: dbName, instance } = getDbInstance(projectName, url) || {};
|
|
13
10
|
|
|
14
11
|
if (name === ADMIN_DB_NAME) name = dbName;
|
|
15
|
-
if (!instance) throw `no MongoClient was found for database with dbRef "${
|
|
12
|
+
if (!instance) throw `no MongoClient was found for database with dbRef "${url}"`;
|
|
16
13
|
// if (!name && !dbName) throw `no dbName found for database with dbRef "${dbUrl}"`;
|
|
17
14
|
|
|
18
15
|
return instance.db(name || dbName);
|
|
19
16
|
};
|
|
20
17
|
|
|
21
|
-
export const getDbInstance = (projectName, dbUrl) =>
|
|
18
|
+
export const getDbInstance = (projectName, dbUrl = DEFAULT_DB) => {
|
|
19
|
+
if (!projectName) throw 'expected projectName in getDb()';
|
|
20
|
+
if (dbUrl === 'admin' || dbUrl === 'default') throw `reserved keyword dbRef: "${dbUrl}"`;
|
|
21
|
+
|
|
22
|
+
dbUrl = dbUrl === ADMIN_DB_URL ? 'admin' : dbUrl === DEFAULT_DB ? 'default' : dbUrl;
|
|
23
|
+
return Scoped.InstancesData[projectName].mongoInstances[dbUrl];
|
|
24
|
+
}
|
|
@@ -87,7 +87,7 @@ const cleanseFind = (path, find, projectName, dbName, dbUrl) => {
|
|
|
87
87
|
|
|
88
88
|
if (instance.__intercepted) {
|
|
89
89
|
const d = instance.interceptMap?.map?.[dbName]?.[path];
|
|
90
|
-
if (d
|
|
90
|
+
if (d?.fulltext) return find;
|
|
91
91
|
}
|
|
92
92
|
return cleanseFindCore(find);
|
|
93
93
|
}
|
|
@@ -124,21 +124,24 @@ const RawValueInstructions = {
|
|
|
124
124
|
promoteValues: false
|
|
125
125
|
};
|
|
126
126
|
|
|
127
|
-
export const readDocument =
|
|
127
|
+
export const readDocument = (commands, projectName, dbName, dbUrl) => {
|
|
128
128
|
const { path, find, returnRawValue } = commands;
|
|
129
|
-
|
|
129
|
+
return getDB(projectName, dbName, dbUrl).collection(path || '')
|
|
130
130
|
.findOne(
|
|
131
131
|
cleanseFind(path, { ...find }, projectName, dbName, dbUrl),
|
|
132
132
|
returnRawValue ? RawValueInstructions : undefined
|
|
133
133
|
);
|
|
134
|
+
};
|
|
134
135
|
|
|
136
|
+
export const readDocumentExtraction = async (commands, projectName, dbName, dbUrl) => {
|
|
137
|
+
const d = await readDocument(commands, projectName, dbName, dbUrl);
|
|
135
138
|
const doc_holder = {};
|
|
136
139
|
const data = await extractDocField(d, commands, projectName, dbName, dbUrl, doc_holder);
|
|
137
140
|
|
|
138
141
|
return { data, doc_holder };
|
|
139
142
|
};
|
|
140
143
|
|
|
141
|
-
export const queryDocument =
|
|
144
|
+
export const queryDocument = (commands, projectName, dbName, dbUrl) => {
|
|
142
145
|
const { path, find, limit, sort, direction, random, returnRawValue } = commands;
|
|
143
146
|
let d = getDB(projectName, dbName, dbUrl).collection(path);
|
|
144
147
|
const findObj = cleanseFind(path, { ...find }, projectName, dbName, dbUrl);
|
|
@@ -157,7 +160,11 @@ export const queryDocument = async (commands, projectName, dbName, dbUrl) => {
|
|
|
157
160
|
if (limit) d = d.limit(limit);
|
|
158
161
|
}
|
|
159
162
|
|
|
160
|
-
|
|
163
|
+
return d.toArray();
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
export const queryDocumentExtraction = async (commands, projectName, dbName, dbUrl) => {
|
|
167
|
+
const d = await queryDocument(commands, projectName, dbName, dbUrl);
|
|
161
168
|
const doc_holder = {};
|
|
162
169
|
const data = await Promise.all(d.map(v => extractDocField(v, commands, projectName, dbName, dbUrl, doc_holder)));
|
|
163
170
|
|
|
@@ -487,7 +494,7 @@ export const databaseRoutes = ({ projectName, logger, enforceE2E_Encryption, cas
|
|
|
487
494
|
|
|
488
495
|
switch (route) {
|
|
489
496
|
case _readDocument:
|
|
490
|
-
const result = await
|
|
497
|
+
const result = await readDocumentExtraction({
|
|
491
498
|
...commands,
|
|
492
499
|
returnRawValue: true
|
|
493
500
|
}, projectName, dbName, dbUrl);
|
|
@@ -495,7 +502,7 @@ export const databaseRoutes = ({ projectName, logger, enforceE2E_Encryption, cas
|
|
|
495
502
|
res.status(200).send(await makeResult({ status: 'success', result: serializeToBase64({ _: result }) }));
|
|
496
503
|
break;
|
|
497
504
|
case _queryCollection:
|
|
498
|
-
const result1 = await
|
|
505
|
+
const result1 = await queryDocumentExtraction({
|
|
499
506
|
...commands,
|
|
500
507
|
returnRawValue: true
|
|
501
508
|
}, projectName, dbName, dbUrl);
|
|
@@ -631,7 +638,7 @@ export const databaseLiveRoutesHandler = ({
|
|
|
631
638
|
}
|
|
632
639
|
|
|
633
640
|
const callSnapshot = async () => {
|
|
634
|
-
const a = await (isDocumentWatch ?
|
|
641
|
+
const a = await (isDocumentWatch ? readDocumentExtraction : queryDocumentExtraction)({
|
|
635
642
|
...commands,
|
|
636
643
|
returnRawValue: true
|
|
637
644
|
}, projectName, dbName, dbUrl);
|
|
@@ -69,9 +69,10 @@ export const streamWritableSource = (path, makeHash, projectName, callback) => {
|
|
|
69
69
|
await handler.close();
|
|
70
70
|
};
|
|
71
71
|
/** @type {import('fs').WriteStream} */
|
|
72
|
-
let fileWriter
|
|
73
|
-
|
|
74
|
-
|
|
72
|
+
let fileWriter;
|
|
73
|
+
let bufferSize = 0;
|
|
74
|
+
let residueExecutions = [];
|
|
75
|
+
let resolvedData;
|
|
75
76
|
|
|
76
77
|
const hasher = createHash('sha256');
|
|
77
78
|
|
|
@@ -88,7 +89,7 @@ export const streamWritableSource = (path, makeHash, projectName, callback) => {
|
|
|
88
89
|
|
|
89
90
|
stream.on('error', async err => {
|
|
90
91
|
await writeReadyPromise;
|
|
91
|
-
|
|
92
|
+
fileWriter.destroy(new Error(err));
|
|
92
93
|
removeLogFlag(true);
|
|
93
94
|
});
|
|
94
95
|
|
|
@@ -144,10 +145,10 @@ export const streamWritableSource = (path, makeHash, projectName, callback) => {
|
|
|
144
145
|
await new Promise(async (resolve, reject) => {
|
|
145
146
|
const writer = createWriteStream(await ensureDir(join(FILES, path)));
|
|
146
147
|
writtenPath = writer.path;
|
|
148
|
+
writer.on('finish', resolve).on('error', reject);
|
|
149
|
+
|
|
147
150
|
createReadStream(fileWriter.path)
|
|
148
|
-
.pipe(writer)
|
|
149
|
-
.on('finish', resolve)
|
|
150
|
-
.on('error', reject);
|
|
151
|
+
.pipe(writer);
|
|
151
152
|
});
|
|
152
153
|
break;
|
|
153
154
|
}
|
|
@@ -164,7 +165,8 @@ export const streamWritableSource = (path, makeHash, projectName, callback) => {
|
|
|
164
165
|
});
|
|
165
166
|
} else uri = hashPath;
|
|
166
167
|
await removeLogFlag(true);
|
|
167
|
-
|
|
168
|
+
resolvedData = uri;
|
|
169
|
+
fileWriter.end();
|
|
168
170
|
} else {
|
|
169
171
|
try {
|
|
170
172
|
await deleteSource(path, projectName);
|
|
@@ -172,11 +174,12 @@ export const streamWritableSource = (path, makeHash, projectName, callback) => {
|
|
|
172
174
|
await rename(fileWriter.path, hashPath);
|
|
173
175
|
await saveHash();
|
|
174
176
|
await removeLogFlag();
|
|
175
|
-
|
|
177
|
+
resolvedData = hashPath;
|
|
178
|
+
fileWriter.end();
|
|
176
179
|
}
|
|
177
180
|
} catch (error) {
|
|
178
181
|
await removeLogFlag(true);
|
|
179
|
-
|
|
182
|
+
fileWriter.destroy(new Error(error));
|
|
180
183
|
}
|
|
181
184
|
});
|
|
182
185
|
await openIO(await ensureDir(PENDING_HASH_LOG)).then(async handler => {
|
|
@@ -186,25 +189,42 @@ export const streamWritableSource = (path, makeHash, projectName, callback) => {
|
|
|
186
189
|
await handler.close();
|
|
187
190
|
});
|
|
188
191
|
fileWriter = createWriteStream(await ensureDir(join(HASH_FILE, sessionID)));
|
|
192
|
+
fileWriter.on('finish', () => {
|
|
193
|
+
resolve(resolvedData);
|
|
194
|
+
});
|
|
195
|
+
fileWriter.on('error', (err) => {
|
|
196
|
+
reject(err);
|
|
197
|
+
});
|
|
189
198
|
residueExecutions.forEach(e => e());
|
|
190
199
|
residueExecutions = undefined;
|
|
191
200
|
writeReadyCallback();
|
|
192
201
|
} else {
|
|
193
|
-
|
|
202
|
+
/** @type {import('fs').WriteStream} */
|
|
203
|
+
let writable;
|
|
204
|
+
let residueBuffers = [];
|
|
194
205
|
|
|
195
206
|
stream.on('data', buf => {
|
|
196
207
|
if (residueBuffers) residueBuffers.push(buf);
|
|
197
208
|
else writable.write(buf);
|
|
198
209
|
});
|
|
199
|
-
stream.on('error',
|
|
210
|
+
stream.on('error', async (err) => {
|
|
211
|
+
await writeReadyPromise;
|
|
212
|
+
writable.destroy(err);
|
|
213
|
+
});
|
|
200
214
|
stream.on('end', async () => {
|
|
201
215
|
await writeReadyPromise;
|
|
202
216
|
try {
|
|
203
217
|
await deleteSource(path, projectName, 'hash');
|
|
204
218
|
} catch (_) { }
|
|
205
|
-
|
|
219
|
+
writable.end();
|
|
206
220
|
});
|
|
207
221
|
writable = createWriteStream(await ensureDir(join(FILES, path)));
|
|
222
|
+
writable.on('finish', () => {
|
|
223
|
+
resolve(writable.path);
|
|
224
|
+
});
|
|
225
|
+
writable.on('error', (err) => {
|
|
226
|
+
reject(err);
|
|
227
|
+
});
|
|
208
228
|
residueBuffers.forEach(buf => {
|
|
209
229
|
writable.write(buf);
|
|
210
230
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mosquito-transport",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.2",
|
|
4
4
|
"description": "Quickly spawn server infrastructure along robust authentication, database, storage, and cross-platform compatibility",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"entity-serializer": "^1.0.2",
|
|
44
44
|
"express": "^4.18.2",
|
|
45
45
|
"google-auth-library": "^8.8.0",
|
|
46
|
-
"guard-object": "^1.1.
|
|
46
|
+
"guard-object": "^1.1.4",
|
|
47
47
|
"jsonwebtoken": "^9.0.0",
|
|
48
48
|
"limit-task": "1.0.0",
|
|
49
49
|
"lodash": "^4.17.21",
|