@dnax/core 0.15.1 → 0.15.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/app/hono.ts CHANGED
@@ -52,78 +52,6 @@ function HonoInstance(): typeof app {
52
52
  })
53
53
  );
54
54
 
55
- if (Cfg?.server?.logger) {
56
- if (typeof Cfg?.server?.logger == "function" && Cfg?.server?.logger) {
57
- app.use(async (c, next) => {
58
- const start = Date.now();
59
- const { action, collection, cleanDeep, useCache } = c.req.query() as Q;
60
- Cfg.server.logger(c, {
61
- mode: "incomming",
62
- start: start,
63
- action: action,
64
- collection: collection,
65
- cleanDeep: cleanDeep,
66
- useCache: stringToBoolean(useCache, false),
67
- method: c.req.method,
68
- url: c.req.url,
69
- path: c.req.path,
70
- });
71
- await next();
72
- const duration = Date.now() - start;
73
- Cfg.server.logger(c, {
74
- mode: "outgoing",
75
- duration,
76
- start,
77
- action,
78
- collection,
79
- cleanDeep,
80
- useCache: stringToBoolean(useCache, false),
81
- method: c.req.method,
82
- url: c.req.url,
83
- status: c.res.status,
84
- path: c.req.path,
85
- });
86
- });
87
- }
88
- if (typeof Cfg?.server?.logger == "boolean" && Cfg?.server?.logger) {
89
- app.use(async (c, next) => {
90
- let ip =
91
- c.req.raw.headers?.get("CF-Connecting-IP") ||
92
- c.req.raw.headers?.get("x-forwarded-for") ||
93
- c.req.raw.headers?.get("x-real-ip");
94
- const origin =
95
- c.req?.header("Origin") || c.req.raw?.headers?.get("Origin") || "";
96
- const info = getConnInfo(c);
97
- if (c.req.method == "OPTIONS") return await next();
98
- const start = Date.now();
99
- const { action, collection, cleanDeep, useCache, name } =
100
- c.req.query() as Q;
101
- // Log the incoming request
102
- console.log(
103
- `<-- ${ip || info.remote.address} ${origin} | ${c.req.method.gray} ${
104
- c.req.path?.gray
105
- } -C ${colors.blue(`${collection || ""}`)} -A ${
106
- action?.gray || ""
107
- } -Sn ${name?.gray || ""} -t ${
108
- moment(start).format("YYYY-DD-MM:HH:mm:ss").gray
109
- } \n`
110
- );
111
-
112
- await next();
113
- // Calculate the duration of the request processing
114
- const duration = Date.now() - start;
115
- console.log(
116
- `--> ${ip || info.remote.address} ${origin} | ${c.req.method.gray} ${
117
- c.req.path?.gray
118
- } -C ${colors.blue(`${collection || ""}`)} -A ${
119
- action?.gray || ""
120
- } -Sn ${name?.gray || ""} -s ${c.res.status} -d ${colors.green(
121
- `(${duration}ms)`
122
- )} -t ${moment().format("YYYY-DD-MM:HH:mm:ss").gray} \n`
123
- );
124
- });
125
- }
126
- }
127
55
  app.use(
128
56
  ipRestriction(getConnInfo, {
129
57
  allowList: Cfg?.server?.whiteListIps || [],
@@ -160,12 +88,10 @@ function HonoInstance(): typeof app {
160
88
  app.use(async (c, next) => {
161
89
  return asyncLocalStorage.run(new Map(), async () => {
162
90
  let cookie = getCookie(c);
163
- let secretKeyStudio = cookie["_STUDIO_SECRET_KEY_"] || null;
164
91
  let session = sessionStorage();
165
92
  var token = jwt.getToken("Bearer", c.req.header()["authorization"]) || "";
166
93
  var { decode, valid, error } = jwt.verify(token);
167
94
  let sessionData = {};
168
-
169
95
  if (valid && decode) {
170
96
  await localSession
171
97
  .get(decode?.sessionId)
@@ -1,11 +1,15 @@
1
1
  import type { ServerWebSocket, WebSocketHandler, Server } from "bun";
2
2
  import { EventEmitter } from "events";
3
+ import { Cfg } from "../../config";
3
4
  import { v4 } from "uuid";
4
5
  const wsClients = new Map<string, ServerWebSocket>();
5
6
  const wsEvents: optionsIo[] = [];
6
7
 
7
8
  class Io {
8
- constructor() {}
9
+ id: string | null;
10
+ constructor() {
11
+ this.id = null;
12
+ }
9
13
  on(event: string, cb: Function) {
10
14
  wsEvents.push({
11
15
  type: "on",
@@ -13,16 +17,30 @@ class Io {
13
17
  cb: cb,
14
18
  });
15
19
  }
20
+ to(id: string) {
21
+ const newInstance = new Io();
22
+ newInstance.id = id;
23
+ return newInstance;
24
+ }
16
25
  emit(event: string, data: any) {
17
- wsEvents.push({
18
- type: "emit",
19
- event: event,
20
- data: data,
26
+ if (!this?.id || this.id == null) {
27
+ console.error("No id found");
28
+ }
29
+
30
+ if (this.id) {
31
+ let client = wsClients.get(this.id);
32
+ client?.send(JSON.stringify({ type: "emit", event, data }));
33
+ }
34
+ }
35
+ broadcast(event: string, data: any) {
36
+ wsClients.forEach((client) => {
37
+ client.send(JSON.stringify({ type: "emit", event, data }));
21
38
  });
22
39
  }
23
40
  }
24
41
 
25
42
  const io = new Io();
43
+ Cfg.io = io;
26
44
  type optionsIo = {
27
45
  type: "on" | "emit";
28
46
  event: string | "connection" | "close";
@@ -41,7 +59,7 @@ export type socketIoType = {
41
59
  emit: (event: string, data: any) => void;
42
60
  }) => void
43
61
  ) => void;
44
- emit: (event: string, data: any) => void;
62
+ emit: (event: string, id: string, data: any) => void;
45
63
  broadcast: (event: string, data: any) => void;
46
64
  };
47
65
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.15.1",
3
+ "version": "0.15.3",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {
package/types/index.ts CHANGED
@@ -190,7 +190,7 @@ export type hooksCtx = (ctx: {
190
190
  c?: Context;
191
191
  rest: InstanceType<typeof useRest>;
192
192
  session?: sessionCtx;
193
- io: Io;
193
+ io: socketIoType;
194
194
  }) => any;
195
195
 
196
196
  export type ctxApi = {
@@ -364,12 +364,19 @@ export type Config = {
364
364
  server: {
365
365
  logger?: Boolean | loggerFunction;
366
366
  whiteListIps?: Array<string>;
367
- blackListIps?: Array<string>;
367
+ blackListIps?: string[];
368
368
  cors?: {
369
- origin: string[];
369
+ origin:
370
+ | string[]
371
+ | ((ctx: { origin: string; c: Context }) => string[])
372
+ | string;
373
+ /**
374
+ * default true
375
+ @default true
376
+ */
370
377
  credentials?: boolean;
371
- allowMethods?: Array<string>;
372
- allowHeaders?: Array<string>;
378
+ allowMethods?: string[];
379
+ allowHeaders?: string[];
373
380
  };
374
381
  socket?: {
375
382
  /**
@@ -438,7 +445,7 @@ export type routeOption = {
438
445
  */
439
446
  base: string; // base URL
440
447
  handler: (ctx: {
441
- io: Io;
448
+ io: socketIoType;
442
449
  router: {
443
450
  post: (path: string, clb: (c: Context) => void) => {};
444
451
  get: (path: string, clb: (c: Context) => void) => {};
@@ -450,7 +457,7 @@ export type endpointCtx = {
450
457
  enabled: boolean;
451
458
  handler: (ctx: {
452
459
  rest: InstanceType<typeof useRest>;
453
- io: Io;
460
+ io: socketIoType;
454
461
  router: {
455
462
  post: (path: string, clb: (c: Context) => void) => {};
456
463
  get: (path: string, clb: (c: Context) => void) => {};
@@ -462,7 +469,7 @@ export type routeCtx = {
462
469
  enabled: boolean;
463
470
  handler: (ctx: {
464
471
  rest: InstanceType<typeof useRest>;
465
- io: Io;
472
+ io: socketIoType;
466
473
  router: {
467
474
  post: (path: string, clb: (c: Context) => void) => {};
468
475
  get: (path: string, clb: (c: Context) => void) => {};
@@ -505,7 +512,7 @@ export type Service = {
505
512
  * authenticated user or no
506
513
  */
507
514
  isAuth: boolean;
508
- io: Io;
515
+ io: socketIoType;
509
516
  }) => any;
510
517
  exec?: (ctx: {
511
518
  /**
@@ -525,7 +532,7 @@ export type Service = {
525
532
  * authenticated user or no
526
533
  */
527
534
  isAuth: boolean;
528
- io: Io;
535
+ io: socketIoType;
529
536
  }) => any;
530
537
  };
531
538