@dnax/core 0.15.3 → 0.15.4

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/lib/cron/index.ts CHANGED
@@ -22,18 +22,21 @@ async function initCron() {
22
22
  let cronTask = new Cron(cronOpt.pattern, {
23
23
  ...cleanDeep(cronOpt.options),
24
24
  });
25
- cronOpt.handler({
26
- io: Cfg.io,
27
- rest: new useRest({
25
+ let cronTaskExec = cronOpt?.exec || cronOpt?.handler;
26
+ if (cronTaskExec && typeof cronTaskExec == "function") {
27
+ cronTaskExec({
28
+ io: Cfg.io,
29
+ rest: new useRest({
30
+ tenant_id: t.id,
31
+ }),
32
+ task: cronTask,
33
+ });
34
+ cronTasks.push({
28
35
  tenant_id: t.id,
29
- }),
30
- task: cronTask,
31
- });
32
- cronTasks.push({
33
- tenant_id: t.id,
34
- name: cronOpt.name,
35
- cron: cronOpt,
36
- });
36
+ name: cronOpt.name,
37
+ cron: cronOpt,
38
+ });
39
+ }
37
40
  }
38
41
  })
39
42
  .catch((err) => {
@@ -15,7 +15,7 @@ async function loadPermissions() {
15
15
  let permissions: permissionSchema[] = [];
16
16
  if (Cfg.tenants) {
17
17
  for await (let t of Cfg.tenants) {
18
- let tenantPath = `${t.dir}/permissions/**/**.rabc.{ts,js}`;
18
+ let tenantPath = `${t.dir}/permissions/**/**.rbac.{ts,js}`;
19
19
  const glob = new Glob(tenantPath);
20
20
 
21
21
  for await (let file of glob.scan({
@@ -5,12 +5,21 @@ import { v4 } from "uuid";
5
5
  const wsClients = new Map<string, ServerWebSocket>();
6
6
  const wsEvents: optionsIo[] = [];
7
7
 
8
+ /**
9
+ * reason for disconnection
10
+ */
11
+ type reasonType = any;
12
+ export type callbackType = (
13
+ socket: socketIoType,
14
+ data: any | reasonType
15
+ ) => void;
16
+
8
17
  class Io {
9
18
  id: string | null;
10
19
  constructor() {
11
20
  this.id = null;
12
21
  }
13
- on(event: string, cb: Function) {
22
+ on(event: string, cb: callbackType) {
14
23
  wsEvents.push({
15
24
  type: "on",
16
25
  event: event,
@@ -54,12 +63,9 @@ export type socketIoType = {
54
63
  id: string;
55
64
  on: <T extends "connection" | "close" | string>(
56
65
  event: T,
57
- cb: (socket: {
58
- id: string;
59
- emit: (event: string, data: any) => void;
60
- }) => void
66
+ cb: callbackType
61
67
  ) => void;
62
- emit: (event: string, id: string, data: any) => void;
68
+ emit: (event: string, data: any) => void;
63
69
  broadcast: (event: string, data: any) => void;
64
70
  };
65
71
 
@@ -89,13 +95,31 @@ function webSocketServer(server: Server): WebSocketHandler {
89
95
  });
90
96
  },
91
97
  close(ws, code, reason) {
92
- console.log("close", code, reason);
93
- console.log(ws.id);
98
+ // get all closed events
99
+ let evs = wsEvents.filter((e) => e.event == "close" && e?.type == "on");
100
+
101
+ evs.map((ev) => {
102
+ if (ev && ev.cb) {
103
+ ev.cb(
104
+ {
105
+ id: ws.id,
106
+ broadcast: (event: string, data: any) => {
107
+ wsClients.forEach((client) => {
108
+ client.send(JSON.stringify({ type: "emit", event, data }));
109
+ });
110
+ },
111
+ },
112
+ reason
113
+ );
114
+ }
115
+ });
116
+ // console.log("close", code, reason);
117
+ // console.log(ws.id);
94
118
  },
95
119
  message: (ws, message: any) => {
96
120
  try {
97
121
  let options: optionsIo = JSON.parse(message);
98
-
122
+ //console.log(options);
99
123
  // find all listeners : On
100
124
  let evs = wsEvents.filter(
101
125
  (e) => e.event == options?.event && e?.type == "on"
@@ -103,10 +127,11 @@ function webSocketServer(server: Server): WebSocketHandler {
103
127
 
104
128
  // run all listeners function if matchs
105
129
  evs?.map((ev) => {
106
- if (ev && ev.cb) {
107
- ev.cb({
108
- data: options.data,
109
- ws: {
130
+ ws.token = options?.token || null;
131
+ if (ev && ev?.cb) {
132
+ ev.cb(
133
+ {
134
+ id: ws.id,
110
135
  broadcast: (event: string, data: any) => {
111
136
  // send to all clients
112
137
  wsClients.forEach((client) => {
@@ -116,12 +141,14 @@ function webSocketServer(server: Server): WebSocketHandler {
116
141
  emit: (event: string, data: any) =>
117
142
  ws.send(JSON.stringify({ type: "emit", event, data })),
118
143
  },
119
- });
144
+ options.data
145
+ );
120
146
  }
121
147
  });
122
148
 
123
149
  //wsEvents.on(options.event)
124
- } catch (err) {
150
+ } catch (err: any) {
151
+ console.log(err?.message);
125
152
  //console.log("Not a valid JSON string", err?.message);
126
153
  }
127
154
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.15.3",
3
+ "version": "0.15.4",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {
package/types/index.ts CHANGED
@@ -123,7 +123,7 @@ export type Field = {
123
123
 
124
124
  export type cronCtx = {
125
125
  rest: InstanceType<typeof useRest>;
126
- io: Io;
126
+ io: socketIoType;
127
127
  task: InstanceType<typeof Cron>;
128
128
  };
129
129
 
@@ -137,7 +137,8 @@ export type cronConfig = {
137
137
  stopAt?: Date;
138
138
  paused?: Boolean;
139
139
  };
140
- handler: (ctx: cronCtx) => void;
140
+ handler?: (ctx: cronCtx) => void;
141
+ exec?: (ctx: cronCtx) => void;
141
142
  };
142
143
 
143
144
  export type middlewareCtx = (ctx: {