@dnax/core 0.14.1 → 0.14.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/app/hono.ts CHANGED
@@ -10,6 +10,7 @@ import { getCookie, setCookie } from "hono/cookie";
10
10
  import colors from "@colors/colors/safe";
11
11
  import { serveStatic, getConnInfo } from "hono/bun";
12
12
  import { consola } from "consola";
13
+
13
14
  import { cors } from "hono/cors";
14
15
  import { asyncLocalStorage, sessionStorage } from "../lib/asyncLocalStorage";
15
16
  import { localSession } from "../lib/session";
@@ -40,6 +41,7 @@ import { v4 } from "uuid";
40
41
  const cache = bentoCache.namespace("DNAX_API");
41
42
  const app = new Hono();
42
43
  const API_PATH = "/api";
44
+
43
45
  function HonoInstance(): typeof app {
44
46
  // Public assets
45
47
  app.get(
package/app/index.ts CHANGED
@@ -9,6 +9,8 @@ import pidusage from "pidusage";
9
9
  import "@colors/colors";
10
10
  import pkg from "../package.json";
11
11
  import findPort from "find-open-port";
12
+ import { Server } from "socket.io";
13
+ import { webSocketServer } from "../lib/socket/instance";
12
14
  import { HonoInstance } from "./hono";
13
15
  type configRunApp = {
14
16
  register?: Array<Function>;
@@ -41,7 +43,7 @@ async function runApp(config?: configRunApp, clb?: Function) {
41
43
  // Load all ressouce
42
44
  await init();
43
45
  const HonoApp = HonoInstance();
44
- await loadSocket(HonoApp);
46
+ //await loadSocket(HonoApp);
45
47
  //BeforeStart
46
48
  if (config?.beforeStart) {
47
49
  for (const fn of config?.beforeStart) {
@@ -49,10 +51,18 @@ async function runApp(config?: configRunApp, clb?: Function) {
49
51
  }
50
52
  }
51
53
 
52
- Bun.serve({
54
+ var server = Bun.serve({
53
55
  port: PORT,
54
- fetch: HonoApp.fetch,
56
+ fetch: (req, server) => {
57
+ if (server.upgrade(req)) {
58
+ // handle authentication
59
+ return;
60
+ }
61
+
62
+ return HonoApp.fetch(req, server);
63
+ },
55
64
  maxRequestBodySize: 1024 * 1024 * 900,
65
+ websocket: webSocketServer(server),
56
66
  });
57
67
 
58
68
  console.log();
package/define/index.ts CHANGED
@@ -22,7 +22,7 @@ function Config(config: Config) {
22
22
 
23
23
  function Collection(col: Collection) {
24
24
  col.timestamps = col?.timestamps ?? true;
25
-
25
+ if (!col?.fields) col.fields = [];
26
26
  return col;
27
27
  }
28
28
 
package/lib/index.ts CHANGED
@@ -4,10 +4,11 @@ import { loadEndpoints } from "./endpoint";
4
4
  import { loadServices } from "./service";
5
5
  import { initCron } from "./cron";
6
6
  import { loadPermissions } from "./permissions";
7
-
7
+ import { loadSocket } from "../lib/socket";
8
8
  import { loadAutoRoutes } from "./routes";
9
9
  // load all ressource
10
10
  async function init(cf = { app: null }) {
11
+ await loadSocket();
11
12
  // load all tenants database
12
13
  await connectTenantsDatabase();
13
14
  // load all collections
@@ -1,17 +1,14 @@
1
- import { Cfg } from "../config";
1
+ import { Cfg } from "../../config";
2
2
  import { Server } from "socket.io";
3
- import type { Hono } from "hono";
4
- import { createServer } from "http";
5
3
  import consola from "consola";
6
4
  import { Glob } from "bun";
7
5
  import path from "path";
8
- import { useRest } from "../driver/mongo";
9
- async function loadSocket(app: InstanceType<typeof Hono>) {
10
- let httpServer = createServer(app.fetch);
11
- var io = new Server(httpServer);
12
- io.on("connection", (socket) => {
6
+ import { useRest } from "../../driver/mongo";
7
+ import { io } from "./instance";
8
+ async function loadSocket() {
9
+ /* io.on("connection", (socket) => {
13
10
  console.log("ok");
14
- });
11
+ }); */
15
12
  if (Cfg.tenants) {
16
13
  for await (let t of Cfg.tenants) {
17
14
  let tenantPath = `${t.dir}/sockets/**/**.ws.{ts,js}`;
@@ -20,18 +17,20 @@ async function loadSocket(app: InstanceType<typeof Hono>) {
20
17
  cwd: Cfg.cwd,
21
18
  })) {
22
19
  let fullPathFile = path.join(Cfg.cwd || "", file);
20
+
23
21
  await import(fullPathFile)
24
22
  .then((inject) => {
25
23
  let socketInstance = inject?.default || null;
24
+
26
25
  if (
27
26
  socketInstance &&
28
27
  socketInstance?.enabled &&
29
- socketInstance?.handler
28
+ socketInstance?.exec
30
29
  ) {
31
30
  let rest = new useRest({
32
31
  tenant_id: t.id,
33
32
  });
34
- socketInstance.handler({
33
+ socketInstance.exec({
35
34
  io,
36
35
  rest,
37
36
  });
@@ -0,0 +1,113 @@
1
+ import type { ServerWebSocket, WebSocketHandler, Server } from "bun";
2
+ import { EventEmitter } from "events";
3
+ import { v4 } from "uuid";
4
+ const wsClients = new Map<string, ServerWebSocket>();
5
+ const wsEvents: optionsIo[] = [];
6
+
7
+ class Io {
8
+ constructor() {}
9
+ on(event: string, cb: Function) {
10
+ wsEvents.push({
11
+ type: "on",
12
+ event: event,
13
+ cb: cb,
14
+ });
15
+ }
16
+ emit(event: string, data: any) {
17
+ wsEvents.push({
18
+ type: "emit",
19
+ event: event,
20
+ data: data,
21
+ });
22
+ }
23
+ }
24
+
25
+ const io = new Io();
26
+ type optionsIo = {
27
+ type: "on" | "emit";
28
+ event: string | "connection" | "close";
29
+ data?: any;
30
+ cb?: Function;
31
+ token?: string;
32
+ uuid?: string;
33
+ };
34
+
35
+ export type socketIoType = {
36
+ id: string;
37
+ on: <T extends "connection" | "close" | string>(
38
+ event: T,
39
+ cb: (socket: {
40
+ id: string;
41
+ emit: (event: string, data: any) => void;
42
+ }) => void
43
+ ) => void;
44
+ emit: (event: string, data: any) => void;
45
+ broadcast: (event: string, data: any) => void;
46
+ };
47
+
48
+ function webSocketServer(server: Server): WebSocketHandler {
49
+ return {
50
+ open: (ws) => {
51
+ let id = v4();
52
+ ws.id = id;
53
+ wsClients.set(id, ws);
54
+ // set Id to client and emit it
55
+ ws.send(JSON.stringify({ type: "setId", id: id }));
56
+
57
+ // run all listenners event for connection
58
+ let evs = wsEvents.filter(
59
+ (e) => e.type == "on" && e?.event == "connection"
60
+ );
61
+
62
+ // exec cb for conenction
63
+ evs?.map((ev) => {
64
+ if (ev && ev.cb) {
65
+ ev.cb({
66
+ id: id,
67
+ emit: (event: string, data: any) =>
68
+ ws.send(JSON.stringify({ type: "emit", event, data })),
69
+ });
70
+ }
71
+ });
72
+ },
73
+ close(ws, code, reason) {
74
+ console.log("close", code, reason);
75
+ console.log(ws.id);
76
+ },
77
+ message: (ws, message: any) => {
78
+ try {
79
+ let options: optionsIo = JSON.parse(message);
80
+
81
+ // find all listeners : On
82
+ let evs = wsEvents.filter(
83
+ (e) => e.event == options?.event && e?.type == "on"
84
+ );
85
+
86
+ // run all listeners function if matchs
87
+ evs?.map((ev) => {
88
+ if (ev && ev.cb) {
89
+ ev.cb({
90
+ data: options.data,
91
+ ws: {
92
+ broadcast: (event: string, data: any) => {
93
+ // send to all clients
94
+ wsClients.forEach((client) => {
95
+ client.send(JSON.stringify({ type: "emit", event, data }));
96
+ });
97
+ },
98
+ emit: (event: string, data: any) =>
99
+ ws.send(JSON.stringify({ type: "emit", event, data })),
100
+ },
101
+ });
102
+ }
103
+ });
104
+
105
+ //wsEvents.on(options.event)
106
+ } catch (err) {
107
+ //console.log("Not a valid JSON string", err?.message);
108
+ }
109
+ },
110
+ };
111
+ }
112
+
113
+ export { webSocketServer, wsClients, io };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.14.1",
3
+ "version": "0.14.2",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {
package/types/index.ts CHANGED
@@ -6,9 +6,9 @@ import type { Db, MongoClient } from "mongodb";
6
6
  import { useRest } from "../driver/mongo/rest";
7
7
  import { sessionStorage } from "../lib/asyncLocalStorage";
8
8
  import type { Context, Hono } from "hono";
9
- import type { Server as ServerIO, Socket as SocketType } from "socket.io";
9
+ //import type { Server as ServerIO, Socket as SocketType } from "socket.io";
10
10
 
11
- type Io = InstanceType<typeof ServerIO>;
11
+ import type { socketIoType } from "../lib/socket/instance";
12
12
 
13
13
  import { fn } from "../utils";
14
14
  import type {
@@ -20,7 +20,7 @@ import type { RouterRoute } from "hono/types";
20
20
  import type { MongoClientOptions } from "mongodb";
21
21
  export type Socket = {
22
22
  enabled: boolean;
23
- handler: (ctx: { rest: useRest; io: Io; session: sessionCtx }) => void;
23
+ exec: (ctx: { rest: useRest; io: socketIoType }) => void;
24
24
  };
25
25
 
26
26
  export type Tenant = {
package/utils/index.ts CHANGED
@@ -12,11 +12,16 @@ import collect from "collect.js";
12
12
  import * as uuid from "uuid";
13
13
  import * as urlencode from "urlencode";
14
14
  import dotJson from "dot-object";
15
+ import deepClone from "deep-clone";
15
16
 
16
17
  const JWT_SECRET = process?.env?.JWT_SECRET || "secret-libv";
17
18
  import * as _ from "radash";
18
19
  import { email } from "../lib/mail";
19
20
 
21
+ function copy(data: object): object | any {
22
+ return deepClone(data);
23
+ }
24
+
20
25
  const jwt = {
21
26
  verify: (token: string): { decode: any; valid: boolean; error: any } => {
22
27
  let decode = null;
@@ -343,4 +348,5 @@ export {
343
348
  Otp, // Otp utils
344
349
  urlencode,
345
350
  stringToBoolean,
351
+ copy,
346
352
  };