@ejfdelgado/ejflab-back 1.1.0

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.
Files changed (64) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +2 -0
  3. package/package.json +57 -0
  4. package/srv/AudIASrv.mjs +218 -0
  5. package/srv/AuthorizationSrv.mjs +213 -0
  6. package/srv/ComputeEngineSrv.mjs +289 -0
  7. package/srv/EmailHandler.mjs +54 -0
  8. package/srv/Image2MeshSrv.mjs +101 -0
  9. package/srv/ImagiationSrv.mjs +408 -0
  10. package/srv/KeysSrv.mjs +104 -0
  11. package/srv/MainHandler.mjs +140 -0
  12. package/srv/MainReplacer.mjs +77 -0
  13. package/srv/MfaSrv.mjs +266 -0
  14. package/srv/MilvusSrv.mjs +152 -0
  15. package/srv/MinioSrv.mjs +154 -0
  16. package/srv/MongoSrv.mjs +320 -0
  17. package/srv/MyError.mjs +48 -0
  18. package/srv/MyFileService.mjs +392 -0
  19. package/srv/MyFileServiceLocal.mjs +177 -0
  20. package/srv/MyPdf.mjs +37 -0
  21. package/srv/MyShell.mjs +205 -0
  22. package/srv/MySqlSrv.mjs +43 -0
  23. package/srv/Network.mjs +111 -0
  24. package/srv/OpenCVSrv.mjs +27 -0
  25. package/srv/PageSrv.mjs +234 -0
  26. package/srv/PayUSrv.mjs +186 -0
  27. package/srv/PayUSrvConstants.mjs +46 -0
  28. package/srv/PostgresSrv.mjs +109 -0
  29. package/srv/SecretsSrv.mjs +126 -0
  30. package/srv/SocketIOCall.mjs +494 -0
  31. package/srv/TupleSrv.mjs +141 -0
  32. package/srv/UtilesSrv.mjs +8 -0
  33. package/srv/callprocessors/AskIceServersProcessor.mjs +14 -0
  34. package/srv/callprocessors/AskRoomProcessor.mjs +15 -0
  35. package/srv/callprocessors/CallUserProcessor.mjs +17 -0
  36. package/srv/callprocessors/ChatSetSawProcessor.mjs +42 -0
  37. package/srv/callprocessors/CheckSrcResponseProcessor.mjs +28 -0
  38. package/srv/callprocessors/ClientChangeProcessor.mjs +19 -0
  39. package/srv/callprocessors/CloseVideoChatProcessor.mjs +16 -0
  40. package/srv/callprocessors/DestroyModelProcessor.mjs +16 -0
  41. package/srv/callprocessors/DisconnectProcessor.mjs +53 -0
  42. package/srv/callprocessors/GenericProcessor.mjs +7 -0
  43. package/srv/callprocessors/GetModelProcessor.mjs +11 -0
  44. package/srv/callprocessors/IncludeOtherPeersProcessor.mjs +12 -0
  45. package/srv/callprocessors/LoadFlowChartProcessor.mjs +103 -0
  46. package/srv/callprocessors/MakeAnswerProcessor.mjs +17 -0
  47. package/srv/callprocessors/OnIceCandidateProcessor.mjs +13 -0
  48. package/srv/callprocessors/OpenVideoChatProcessor.mjs +17 -0
  49. package/srv/callprocessors/PauseFlowChartProcessor.mjs +16 -0
  50. package/srv/callprocessors/ProcessResponseProcessor.mjs +123 -0
  51. package/srv/callprocessors/ReadSrcResponseProcessor.mjs +30 -0
  52. package/srv/callprocessors/RegisterProcessorProcessor.mjs +23 -0
  53. package/srv/callprocessors/RegisterSourceProcessor.mjs +22 -0
  54. package/srv/callprocessors/SendChatProcessor.mjs +71 -0
  55. package/srv/callprocessors/StartFlowChartProcessor.mjs +48 -0
  56. package/srv/callprocessors/StopFlowChartProcessor.mjs +16 -0
  57. package/srv/callprocessors/SubscribemeProcessor.mjs +13 -0
  58. package/srv/callprocessors/UpdateMyInformationProcessor.mjs +30 -0
  59. package/srv/common/FirebasConfig.mjs +160 -0
  60. package/srv/common/General.mjs +69 -0
  61. package/srv/common/MimeTypeMap.mjs +142 -0
  62. package/srv/common/MyStore.mjs +169 -0
  63. package/srv/common/Usuario.mjs +101 -0
  64. package/srv/common/Utilidades.mjs +43 -0
@@ -0,0 +1,320 @@
1
+ import { MongoClient, ObjectId } from 'mongodb'
2
+ import * as noqlModule from '@synatic/noql';
3
+ import * as sortifyModule from "@ejfdelgado/ejflab-common/src/sortify.js";
4
+
5
+ const SQLParser = noqlModule.default;
6
+ const sortify = sortifyModule.default;
7
+
8
+ export class MongoSrv {
9
+ static getURI() {
10
+ let usr = process.env.MONGO_USR;
11
+ let pass = process.env.MONGO_PASS;
12
+ let uri = process.env.MONGO_URI;
13
+ //console.log(`MONGO_URI=${uri}`);
14
+ let completeUri = `mongodb+srv://${usr}:${pass}@${uri}/test?retryWrites=true&writeConcern=majority`;
15
+ completeUri = `mongodb://${usr}:${pass}@${uri}`;
16
+ return completeUri;
17
+ }
18
+ static releaseMongo(mongoFun) {
19
+ return async (req, res, next) => {
20
+ const { client } = req.data.mongo;
21
+ try {
22
+ await mongoFun(req, res, next);
23
+ } catch (err) {
24
+ throw err;
25
+ } finally {
26
+ console.log("Closing Mongo...");
27
+ await client.close();
28
+ console.log("Closing Mongo... OK");
29
+ }
30
+ }
31
+ }
32
+ static useMongo() {
33
+ return async (req, res, next) => {
34
+ const connectionString = MongoSrv.getURI();
35
+ const client = await MongoClient.connect(connectionString);
36
+ await client.connect();
37
+ const dbName = req.params['db'];
38
+
39
+ const body = req.body;
40
+ let collectionName = null;
41
+ let parsedSQL = null;
42
+ if (body) {
43
+ let where = body.where;
44
+ if (where) {
45
+ if (typeof where == "string") {
46
+ //console.log(`Parsing ${where}`);
47
+ // Clean weird whitespaces...
48
+ where = where.replace(/\s/g, " ");
49
+ parsedSQL = SQLParser.parseSQL(where);
50
+ collectionName = parsedSQL.collection;
51
+ } else {
52
+ const { collection } = where;
53
+ collectionName = collection;
54
+ parsedSQL = where;
55
+ }
56
+ }
57
+ }
58
+
59
+ if (!collectionName) {
60
+ collectionName = req.params['collection'];
61
+ }
62
+
63
+ const database = client.db(dbName);
64
+ const collection = database.collection(collectionName);
65
+ //console.log(`Using Mongo db: ${dbName} collection: ${collectionName}`);
66
+ if (!req.data) {
67
+ req.data = {};
68
+ }
69
+ req.data.mongo = {
70
+ client,
71
+ database,
72
+ collection,
73
+ parsedSQL,
74
+ collectionName,
75
+ dbName
76
+ };
77
+ await next();
78
+ };
79
+ }
80
+ // https://www.mongodb.com/docs/drivers/node/current/quick-reference/
81
+ static async write(req, res, next) {
82
+ const { client, database, collection, dbName, collectionName } = req.data.mongo;
83
+ const body = req.body;
84
+ let procesed = null;
85
+ procesed = await MongoSrv.create(collection, body);
86
+ if (procesed) {
87
+ console.log(`Mongo inserted ${dbName}/${collectionName} x ${procesed.insertedCount}`);
88
+ }
89
+ const response = {
90
+ status: "ok",
91
+ procesed
92
+ };
93
+ res.status(200).send(response);
94
+ }
95
+ static async writeLocal(dbName, collectionName, payload) {
96
+ return await MongoSrv.commandLocal(dbName, collectionName, payload, "write");
97
+ }
98
+ static async readLocal(dbName, payload) {
99
+ return await MongoSrv.commandLocal(dbName, null, payload, "read");
100
+ }
101
+ static async deleteLocal(dbName, payload) {
102
+ return await MongoSrv.commandLocal(dbName, null, payload, "delete");
103
+ }
104
+ static async updateLocal(dbName, payload) {
105
+ return await MongoSrv.commandLocal(dbName, null, payload, "update");
106
+ }
107
+ static async indexLocal(dbName, collectionName, payload) {
108
+ return await MongoSrv.commandLocal(dbName, collectionName, payload, "index");
109
+ }
110
+ static async createLocal(dbName, collectionName, payload) {
111
+ return await MongoSrv.commandLocal(dbName, collectionName, payload, "create");
112
+ }
113
+ static async commandLocal(dbName, collectionName, payload, type) {
114
+ const req = {
115
+ params: {
116
+ db: dbName,
117
+ collection: collectionName,
118
+ },
119
+ body: payload,
120
+ };
121
+ //console.log(`commandLocal ${JSON.stringify(req)}`);
122
+ return new Promise((resolve, reject) => {
123
+ const res = {
124
+ status: (code) => {
125
+ //console.log(`status ${code}`);
126
+ if (code == 200) {
127
+ return {
128
+ send: (response) => {
129
+ //console.log(`send ${JSON.stringify(response)}`);
130
+ resolve(response);
131
+ }
132
+ }
133
+ } else {
134
+ return {
135
+ send: (response) => {
136
+ reject(response);
137
+ }
138
+ }
139
+ }
140
+ }
141
+ };
142
+ //console.log("useMongo...");
143
+ MongoSrv.useMongo()(req, res, async () => {
144
+ //console.log("useMongo... OK");
145
+ if (type == "write") {
146
+ MongoSrv.write(req, res);
147
+ } else if (type == "read") {
148
+ MongoSrv.read(req, res);
149
+ } else if (type == "delete") {
150
+ MongoSrv.delete(req, res);
151
+ } else if (type == "update") {
152
+ MongoSrv.update(req, res);
153
+ } else if (type == "index") {
154
+ MongoSrv.index(req, res);
155
+ } else if (type == "create") {
156
+ MongoSrv.createCollection(req, res);
157
+ }
158
+ });
159
+ });
160
+ }
161
+ // Payload could be array or object
162
+ static async create(collection, payload) {
163
+ let localPayload = [];
164
+ if (payload instanceof Array) {
165
+ localPayload = payload;
166
+ } else {
167
+ localPayload.push(payload);
168
+ }
169
+ if (localPayload.length == 0) {
170
+ return null;
171
+ } else {
172
+ return await collection.insertMany(localPayload);
173
+ }
174
+ }
175
+ // Only works at the first level
176
+ static fixId(query) {
177
+ if (!query) {
178
+ return;
179
+ }
180
+ const llaves = Object.keys(query);
181
+ for (let i = 0; i < llaves.length; i++) {
182
+ const llave = llaves[i];
183
+ if (llave == "_id") {
184
+ const opers = query[llave];
185
+ const oper = Object.keys(opers)[0];
186
+ const value = opers[oper];
187
+ //console.log(`${llave} ${oper} ${value}`);
188
+ if (typeof value == "string" || typeof value == "number") {
189
+ opers[oper] = new ObjectId(value);
190
+ } else if (value instanceof Array) {
191
+ opers[oper] = value.map((single) => {
192
+ return new ObjectId(single);
193
+ });
194
+ }
195
+ }
196
+ }
197
+ }
198
+ static async read(req, res, next) {
199
+ const { client, database, collection, parsedSQL } = req.data.mongo;
200
+ let list = [];
201
+ //console.log(JSON.stringify(parsedSQL, null, 4));
202
+ const projection = {};
203
+ if (parsedSQL.projection) {
204
+ const keyNames = Object.keys(parsedSQL.projection);
205
+ keyNames.forEach((keyName) => {
206
+ projection[keyName] = 1;
207
+ });
208
+ }
209
+ MongoSrv.fixId(parsedSQL.query);
210
+ if (parsedSQL.type === 'query') {
211
+ //console.log(JSON.stringify(parsedSQL.query, null, 4));
212
+ list = await collection
213
+ .find(parsedSQL.query || {})
214
+ .project(projection)
215
+ .skip(parsedSQL.skip || 0)
216
+ .limit(parsedSQL.limit || 50)
217
+ .toArray();
218
+ } else if (parsedSQL.type === 'aggregate') {
219
+ list = await collection
220
+ .aggregate(parsedSQL.pipeline)
221
+ .toArray();
222
+ }
223
+
224
+ list.forEach((element) => {
225
+ element["_id"] = element["_id"].toString("utf8");
226
+ });
227
+
228
+ const response = {
229
+ status: "ok",
230
+ list
231
+ };
232
+ res.status(200).send(response);
233
+ }
234
+ static async delete(req, res, next) {
235
+ const { client, database, collection, parsedSQL } = req.data.mongo;
236
+
237
+ MongoSrv.fixId(parsedSQL.query);
238
+ let procesed = null;
239
+ if (parsedSQL.type === 'query') {
240
+ procesed = await collection.deleteMany(parsedSQL.query || {},);
241
+ }
242
+
243
+ const response = {
244
+ status: "ok",
245
+ procesed
246
+ };
247
+ res.status(200).send(response);
248
+ }
249
+ static async update(req, res, next) {
250
+ const { client, database, collection, parsedSQL } = req.data.mongo;
251
+ const { update } = req.body;
252
+ MongoSrv.fixId(parsedSQL.query);
253
+ let procesed = null;
254
+ if (parsedSQL.type === 'query') {
255
+ const llaves = Object.keys(update);
256
+ const updateData = { $set: {} };
257
+ llaves.forEach((llave) => {
258
+ const valor = update[llave];
259
+ updateData["$set"][llave] = valor;
260
+ });
261
+ procesed = await collection.updateMany(parsedSQL.query || {}, updateData);
262
+ }
263
+
264
+ const response = {
265
+ status: "ok",
266
+ procesed
267
+ };
268
+ res.status(200).send(response);
269
+ }
270
+ static async index(req, res, next) {
271
+ const { client, database, collection, parsedSQL } = req.data.mongo;
272
+ const indexes = req.body;
273
+
274
+ /*
275
+ let oldIndexesIterator = await collection.listIndexes();
276
+ let oldIndexes = oldIndexesIterator.toArray();
277
+ console.log(JSON.stringify(oldIndexes));
278
+ */
279
+
280
+ for (let i = 0; i < indexes.length; i++) {
281
+ const { val, opt } = indexes[i];
282
+ const indexResponse = await collection.createIndex(val, opt);
283
+ console.log(indexResponse);
284
+ }
285
+
286
+ /*
287
+ oldIndexesIterator = await collection.listIndexes();
288
+ oldIndexes = oldIndexesIterator.toArray();
289
+ console.log(JSON.stringify(oldIndexes));
290
+ */
291
+
292
+ const response = {
293
+ status: "ok",
294
+ };
295
+ res.status(200).send(response);
296
+ }
297
+ static async createCollection(req, res, next) {
298
+ const { client, database, collection, parsedSQL } = req.data.mongo;
299
+ const config = req.body;
300
+ console.log(collection);
301
+ //client.createCollection()
302
+ const response = {
303
+ status: "ok",
304
+ };
305
+ res.status(200).send(response);
306
+ }
307
+
308
+ static async ping(req, res, next) {
309
+ const connectionString = MongoSrv.getURI();
310
+ const client = await MongoClient.connect(connectionString);
311
+ await client.connect();
312
+ const admin = client.db("admin");
313
+ const result = await admin.command({ listDatabases: 1, nameOnly: true });
314
+ const response = {
315
+ result,
316
+ status: "ok",
317
+ };
318
+ res.status(200).send(response);
319
+ }
320
+ }
@@ -0,0 +1,48 @@
1
+ class MyError extends Error {
2
+ constructor(message, httpCode) {
3
+ super(message);
4
+ this.name = "MyError";
5
+ this.httpCode = httpCode;
6
+ }
7
+ }
8
+
9
+ class NoAutorizadoException extends MyError {
10
+ constructor(message) {
11
+ super(message, 401);
12
+ }
13
+ }
14
+ class NoExisteException extends MyError {
15
+ constructor(message) {
16
+ super(message, 204);
17
+ }
18
+ }
19
+ class ParametrosIncompletosException extends MyError {
20
+ constructor(message) {
21
+ super(message, 400);
22
+ }
23
+ }
24
+ class NoHayUsuarioException extends MyError {
25
+ constructor(message) {
26
+ super(message, 401);
27
+ }
28
+ }
29
+ class MalaPeticionException extends MyError {
30
+ constructor(message) {
31
+ super(message, 400);
32
+ }
33
+ }
34
+ class InesperadoException extends MyError {
35
+ constructor(message) {
36
+ super(message, 500);
37
+ }
38
+ }
39
+
40
+ export {
41
+ MyError,
42
+ NoAutorizadoException,
43
+ NoExisteException,
44
+ ParametrosIncompletosException,
45
+ NoHayUsuarioException,
46
+ MalaPeticionException,
47
+ InesperadoException
48
+ }