@dnax/core 0.16.3 → 0.16.5
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/app/hono.ts +36 -1
- package/driver/mongo/rest.ts +179 -3
- package/lib/socket/instance.ts +4 -2
- package/package.json +1 -1
- package/tsconfig.json +2 -0
- package/types/index.ts +8 -2
- package/utils/index.ts +6 -1
package/app/hono.ts
CHANGED
|
@@ -107,7 +107,10 @@ function HonoInstance(): typeof app {
|
|
|
107
107
|
ip:
|
|
108
108
|
c.req.raw.headers?.get("CF-Connecting-IP") ||
|
|
109
109
|
c.req.raw.headers?.get("x-forwarded-for") ||
|
|
110
|
-
c.req.raw.headers?.get("x-real-ip")
|
|
110
|
+
c.req.raw.headers?.get("x-real-ip") ||
|
|
111
|
+
c.req.raw.headers?.get("remoteaddr") ||
|
|
112
|
+
"::1",
|
|
113
|
+
|
|
111
114
|
isAuth: valid ? true : false,
|
|
112
115
|
reqAt: moment().format().toString(),
|
|
113
116
|
setAt: sessionData?._v?.setAt || null,
|
|
@@ -115,6 +118,8 @@ function HonoInstance(): typeof app {
|
|
|
115
118
|
c.set("_v", _v);
|
|
116
119
|
c.set("tenant-id", c.req.header()["tenant-id"]);
|
|
117
120
|
|
|
121
|
+
//console.log("_v", _v);
|
|
122
|
+
|
|
118
123
|
if (token && valid) {
|
|
119
124
|
session.set({
|
|
120
125
|
generateToken: false,
|
|
@@ -340,12 +345,42 @@ function HonoInstance(): typeof app {
|
|
|
340
345
|
responseAuth !== null &&
|
|
341
346
|
responseAuth
|
|
342
347
|
) {
|
|
348
|
+
rest.activity.save({
|
|
349
|
+
collection: collection,
|
|
350
|
+
data: {
|
|
351
|
+
input: body,
|
|
352
|
+
},
|
|
353
|
+
action: "authCollection",
|
|
354
|
+
auth: true,
|
|
355
|
+
user: sessionStorage().get().state?.user,
|
|
356
|
+
ip:
|
|
357
|
+
c.req.raw.headers?.get("CF-Connecting-IP") ||
|
|
358
|
+
c.req.raw.headers?.get("x-forwarded-for") ||
|
|
359
|
+
c.req.raw.headers?.get("x-real-ip") ||
|
|
360
|
+
c.req.raw.headers?.get("remoteaddr") ||
|
|
361
|
+
"::1",
|
|
362
|
+
});
|
|
343
363
|
return c.json({
|
|
344
364
|
auth: true,
|
|
345
365
|
data: responseAuth,
|
|
346
366
|
token: sessionStorage().get().token,
|
|
347
367
|
});
|
|
348
368
|
} else {
|
|
369
|
+
rest.activity.save({
|
|
370
|
+
collection: collection,
|
|
371
|
+
data: {
|
|
372
|
+
input: body,
|
|
373
|
+
},
|
|
374
|
+
action: "authCollection",
|
|
375
|
+
auth: false,
|
|
376
|
+
user: sessionStorage().get().state?.user,
|
|
377
|
+
ip:
|
|
378
|
+
c.req.raw.headers?.get("CF-Connecting-IP") ||
|
|
379
|
+
c.req.raw.headers?.get("x-forwarded-for") ||
|
|
380
|
+
c.req.raw.headers?.get("x-real-ip") ||
|
|
381
|
+
c.req.raw.headers?.get("remoteaddr") ||
|
|
382
|
+
"::1",
|
|
383
|
+
});
|
|
349
384
|
c.status(401);
|
|
350
385
|
return c.json({ message: "Authentification failed" });
|
|
351
386
|
}
|
package/driver/mongo/rest.ts
CHANGED
|
@@ -75,6 +75,23 @@ class useRest {
|
|
|
75
75
|
Direct Api Mongo , use it with caution
|
|
76
76
|
*/
|
|
77
77
|
db: Tenant["database"]["db"];
|
|
78
|
+
activity: {
|
|
79
|
+
save?: (activity: {
|
|
80
|
+
collection?: string;
|
|
81
|
+
data: {
|
|
82
|
+
input: any;
|
|
83
|
+
};
|
|
84
|
+
action: string;
|
|
85
|
+
ip?: string;
|
|
86
|
+
user?: object;
|
|
87
|
+
}) => Promise<any>;
|
|
88
|
+
list?: (filter?: {
|
|
89
|
+
$match: {};
|
|
90
|
+
$limit?: number;
|
|
91
|
+
$sort?: {};
|
|
92
|
+
$skip?: number;
|
|
93
|
+
}) => Promise<Array<any>>;
|
|
94
|
+
};
|
|
78
95
|
|
|
79
96
|
constructor(options: options) {
|
|
80
97
|
this.#c = options.c;
|
|
@@ -85,6 +102,18 @@ class useRest {
|
|
|
85
102
|
this.#useHook = options?.useHook ?? true;
|
|
86
103
|
this.db = this.#tenant.database.db;
|
|
87
104
|
|
|
105
|
+
this.activity = {
|
|
106
|
+
save: async (activityInput) => {
|
|
107
|
+
return await restActivity.save(this.#tenant, activityInput);
|
|
108
|
+
},
|
|
109
|
+
list: async (activityFilter) => {
|
|
110
|
+
return await restActivity
|
|
111
|
+
.list(this.#tenant, activityFilter || {})
|
|
112
|
+
.then((e) => e)
|
|
113
|
+
.catch((err) => []);
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
|
|
88
117
|
/* if (options?.useHook == undefined || options?.useHook == null) {
|
|
89
118
|
this.#useHook = true;
|
|
90
119
|
} else {
|
|
@@ -335,6 +364,18 @@ class useRest {
|
|
|
335
364
|
}),
|
|
336
365
|
});
|
|
337
366
|
}
|
|
367
|
+
restActivity.save(this.#tenant, {
|
|
368
|
+
collection: collection,
|
|
369
|
+
data: {
|
|
370
|
+
input: data,
|
|
371
|
+
},
|
|
372
|
+
transaction: this.#session ? true : false,
|
|
373
|
+
action: "insertOne",
|
|
374
|
+
user:
|
|
375
|
+
sessionStorage().get().state?.user ||
|
|
376
|
+
sessionStorage().get().state?.by ||
|
|
377
|
+
null,
|
|
378
|
+
});
|
|
338
379
|
return resolve(toJson(data));
|
|
339
380
|
} catch (err) {
|
|
340
381
|
return reject(err);
|
|
@@ -442,6 +483,18 @@ class useRest {
|
|
|
442
483
|
}),
|
|
443
484
|
});
|
|
444
485
|
}
|
|
486
|
+
restActivity.save(this.#tenant, {
|
|
487
|
+
collection: collection,
|
|
488
|
+
data: {
|
|
489
|
+
input: toJson(data),
|
|
490
|
+
},
|
|
491
|
+
action: "insertMany",
|
|
492
|
+
transaction: this.#session ? true : false,
|
|
493
|
+
user:
|
|
494
|
+
sessionStorage().get().state?.user ||
|
|
495
|
+
sessionStorage().get().state?.by ||
|
|
496
|
+
null,
|
|
497
|
+
});
|
|
445
498
|
|
|
446
499
|
return resolve(toJson(data));
|
|
447
500
|
} catch (err) {
|
|
@@ -492,6 +545,7 @@ class useRest {
|
|
|
492
545
|
* @param params - Find params
|
|
493
546
|
* @param options
|
|
494
547
|
*/
|
|
548
|
+
|
|
495
549
|
async find(
|
|
496
550
|
collection: string,
|
|
497
551
|
params: findParam,
|
|
@@ -835,6 +889,20 @@ class useRest {
|
|
|
835
889
|
});
|
|
836
890
|
}
|
|
837
891
|
|
|
892
|
+
restActivity.save(this.#tenant, {
|
|
893
|
+
collection: collection,
|
|
894
|
+
data: {
|
|
895
|
+
input: update,
|
|
896
|
+
id: id,
|
|
897
|
+
ids: [id],
|
|
898
|
+
},
|
|
899
|
+
transaction: this.#session ? true : false,
|
|
900
|
+
action: "updateOne",
|
|
901
|
+
user:
|
|
902
|
+
sessionStorage().get().state?.user ||
|
|
903
|
+
sessionStorage().get().state?.by ||
|
|
904
|
+
null,
|
|
905
|
+
});
|
|
838
906
|
return resolve(result.doc);
|
|
839
907
|
} catch (err) {
|
|
840
908
|
return reject(err);
|
|
@@ -1098,7 +1166,19 @@ class useRest {
|
|
|
1098
1166
|
}),
|
|
1099
1167
|
});
|
|
1100
1168
|
}
|
|
1101
|
-
|
|
1169
|
+
restActivity.save(this.#tenant, {
|
|
1170
|
+
collection: collection,
|
|
1171
|
+
data: {
|
|
1172
|
+
input: update,
|
|
1173
|
+
ids: ids,
|
|
1174
|
+
},
|
|
1175
|
+
action: "updateMany",
|
|
1176
|
+
transaction: this.#session ? true : false,
|
|
1177
|
+
user:
|
|
1178
|
+
sessionStorage().get().state?.user ||
|
|
1179
|
+
sessionStorage().get().state?.by ||
|
|
1180
|
+
null,
|
|
1181
|
+
});
|
|
1102
1182
|
return resolve(result.docs);
|
|
1103
1183
|
} else {
|
|
1104
1184
|
throw new contextError("List of id required", 400);
|
|
@@ -1194,7 +1274,20 @@ class useRest {
|
|
|
1194
1274
|
}),
|
|
1195
1275
|
});
|
|
1196
1276
|
}
|
|
1197
|
-
|
|
1277
|
+
restActivity.save(this.#tenant, {
|
|
1278
|
+
collection: collection,
|
|
1279
|
+
data: {
|
|
1280
|
+
input: doc,
|
|
1281
|
+
id: id,
|
|
1282
|
+
ids: [id],
|
|
1283
|
+
},
|
|
1284
|
+
transaction: this.#session ? true : false,
|
|
1285
|
+
action: "deleteOne",
|
|
1286
|
+
user:
|
|
1287
|
+
sessionStorage().get().state?.user ||
|
|
1288
|
+
sessionStorage().get().state?.by ||
|
|
1289
|
+
null,
|
|
1290
|
+
});
|
|
1198
1291
|
return resolve(doc);
|
|
1199
1292
|
} catch (err) {
|
|
1200
1293
|
return resolve(err);
|
|
@@ -1288,6 +1381,20 @@ class useRest {
|
|
|
1288
1381
|
});
|
|
1289
1382
|
}
|
|
1290
1383
|
|
|
1384
|
+
restActivity.save(this.#tenant, {
|
|
1385
|
+
collection: collection,
|
|
1386
|
+
data: {
|
|
1387
|
+
input: null,
|
|
1388
|
+
ids: ids,
|
|
1389
|
+
},
|
|
1390
|
+
transaction: this.#session ? true : false,
|
|
1391
|
+
action: "deleteMany",
|
|
1392
|
+
user:
|
|
1393
|
+
sessionStorage().get().state?.user ||
|
|
1394
|
+
sessionStorage().get().state?.by ||
|
|
1395
|
+
null,
|
|
1396
|
+
});
|
|
1397
|
+
|
|
1291
1398
|
return resolve(deletedIds);
|
|
1292
1399
|
} catch (err) {
|
|
1293
1400
|
return reject(err);
|
|
@@ -1301,7 +1408,23 @@ class useRest {
|
|
|
1301
1408
|
.estimatedDocumentCount();
|
|
1302
1409
|
}
|
|
1303
1410
|
async dropCollection(Collection: string) {
|
|
1304
|
-
return
|
|
1411
|
+
return new Promise(async (resolve, reject) => {
|
|
1412
|
+
try {
|
|
1413
|
+
let r = await this.#tenant.database.db?.collection(Collection).drop();
|
|
1414
|
+
restActivity.save(this.#tenant, {
|
|
1415
|
+
collection: Collection,
|
|
1416
|
+
data: {},
|
|
1417
|
+
action: "dropCollection",
|
|
1418
|
+
user:
|
|
1419
|
+
sessionStorage().get().state?.user ||
|
|
1420
|
+
sessionStorage().get().state?.by ||
|
|
1421
|
+
null,
|
|
1422
|
+
});
|
|
1423
|
+
return resolve(r);
|
|
1424
|
+
} catch (err) {
|
|
1425
|
+
return reject(err);
|
|
1426
|
+
}
|
|
1427
|
+
});
|
|
1305
1428
|
}
|
|
1306
1429
|
|
|
1307
1430
|
async dropIndexes(Collection: string) {
|
|
@@ -1344,4 +1467,57 @@ class useRest {
|
|
|
1344
1467
|
}
|
|
1345
1468
|
}
|
|
1346
1469
|
|
|
1470
|
+
const restActivity = {
|
|
1471
|
+
list: async (
|
|
1472
|
+
tenant: Tenant,
|
|
1473
|
+
filter?: {
|
|
1474
|
+
$match?: {};
|
|
1475
|
+
$limit?: number;
|
|
1476
|
+
$sort?: {};
|
|
1477
|
+
$skip?: number;
|
|
1478
|
+
}
|
|
1479
|
+
) => {
|
|
1480
|
+
return await tenant.database.db
|
|
1481
|
+
?.collection("___activity__")
|
|
1482
|
+
.find({
|
|
1483
|
+
...filter?.$match,
|
|
1484
|
+
})
|
|
1485
|
+
.limit(filter?.$limit || 100)
|
|
1486
|
+
.sort({ createdAt: -1, ...filter?.$sort })
|
|
1487
|
+
.skip(filter?.$skip || 0)
|
|
1488
|
+
.toArray()
|
|
1489
|
+
.catch((err) => []);
|
|
1490
|
+
},
|
|
1491
|
+
save: async (
|
|
1492
|
+
tenant: Tenant,
|
|
1493
|
+
activity: {
|
|
1494
|
+
collection?: string;
|
|
1495
|
+
data: any;
|
|
1496
|
+
action: string;
|
|
1497
|
+
ip?: string;
|
|
1498
|
+
user?: object;
|
|
1499
|
+
transaction?: boolean;
|
|
1500
|
+
auth?: boolean;
|
|
1501
|
+
}
|
|
1502
|
+
) => {
|
|
1503
|
+
return await tenant.database.db
|
|
1504
|
+
?.collection("___activity__")
|
|
1505
|
+
.insertOne(
|
|
1506
|
+
toBson({
|
|
1507
|
+
ip: activity?.ip,
|
|
1508
|
+
tenant: tenant.id,
|
|
1509
|
+
data: activity?.data || {},
|
|
1510
|
+
action: activity.action,
|
|
1511
|
+
collection: activity?.collection,
|
|
1512
|
+
auth: activity?.auth ?? false,
|
|
1513
|
+
createdAt: moment().format(),
|
|
1514
|
+
updatedAt: moment().format(),
|
|
1515
|
+
user: activity?.user,
|
|
1516
|
+
transaction: activity?.transaction ?? false,
|
|
1517
|
+
})
|
|
1518
|
+
)
|
|
1519
|
+
.catch();
|
|
1520
|
+
},
|
|
1521
|
+
};
|
|
1522
|
+
|
|
1347
1523
|
export { useRest };
|
package/lib/socket/instance.ts
CHANGED
|
@@ -75,9 +75,9 @@ function webSocketServer(server: Server): WebSocketHandler {
|
|
|
75
75
|
let id = v4();
|
|
76
76
|
ws.id = id;
|
|
77
77
|
wsClients.set(id, ws);
|
|
78
|
+
|
|
78
79
|
// set Id to client and emit it
|
|
79
80
|
ws.send(JSON.stringify({ type: "setId", id: id }));
|
|
80
|
-
|
|
81
81
|
// run all listenners event for connection
|
|
82
82
|
let evs = wsEvents.filter(
|
|
83
83
|
(e) => e.type == "on" && e?.event == "connection"
|
|
@@ -113,13 +113,15 @@ function webSocketServer(server: Server): WebSocketHandler {
|
|
|
113
113
|
);
|
|
114
114
|
}
|
|
115
115
|
});
|
|
116
|
+
|
|
117
|
+
wsClients?.delete(ws.id);
|
|
116
118
|
// console.log("close", code, reason);
|
|
117
119
|
// console.log(ws.id);
|
|
118
120
|
},
|
|
119
121
|
message: (ws, message: any) => {
|
|
120
122
|
try {
|
|
121
123
|
let options: optionsIo = JSON.parse(message);
|
|
122
|
-
|
|
124
|
+
|
|
123
125
|
// find all listeners : On
|
|
124
126
|
let evs = wsEvents.filter(
|
|
125
127
|
(e) => e.event == options?.event && e?.type == "on"
|
package/package.json
CHANGED
package/tsconfig.json
CHANGED
package/types/index.ts
CHANGED
|
@@ -162,14 +162,20 @@ export type accessCtx = (ctx: {
|
|
|
162
162
|
|
|
163
163
|
export type sessionCtx = {
|
|
164
164
|
set: (session: {
|
|
165
|
-
state:
|
|
165
|
+
state: {
|
|
166
|
+
[key: string]: any;
|
|
167
|
+
user?: object;
|
|
168
|
+
};
|
|
166
169
|
token?: string;
|
|
167
170
|
_v?: object;
|
|
168
171
|
role?: string | null | undefined;
|
|
169
172
|
expiresIn?: string;
|
|
170
173
|
}) => void;
|
|
171
174
|
get: () => {
|
|
172
|
-
state:
|
|
175
|
+
state: {
|
|
176
|
+
[key: string]: any;
|
|
177
|
+
user?: object;
|
|
178
|
+
};
|
|
173
179
|
_v: object;
|
|
174
180
|
role: string | null | undefined;
|
|
175
181
|
token: string | null | undefined;
|
package/utils/index.ts
CHANGED
|
@@ -98,9 +98,14 @@ function isDate(date: string): boolean {
|
|
|
98
98
|
const dateRegex = /^\d{4}-\d{2}-\d{2}$/;
|
|
99
99
|
const dateRegex2 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/;
|
|
100
100
|
const dateRegex3 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/;
|
|
101
|
+
// le regex pour cette forme 2024-11-30T21:36:12+00:00
|
|
102
|
+
const dateRegex4 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+\d{2}:\d{2}$/;
|
|
101
103
|
let isDate_ =
|
|
102
104
|
!isNaN(Date.parse(date)) &&
|
|
103
|
-
(dateRegex.test(date) ||
|
|
105
|
+
(dateRegex.test(date) ||
|
|
106
|
+
dateRegex2.test(date) ||
|
|
107
|
+
dateRegex4.test(date) ||
|
|
108
|
+
dateRegex3.test(date));
|
|
104
109
|
if (isDate_) return true;
|
|
105
110
|
try {
|
|
106
111
|
isDate_ = moment(date, "YYYY-MM-DD", true).isValid();
|