@dnax/core 0.15.2 → 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/app/hono.ts +0 -74
- package/lib/cron/index.ts +14 -11
- package/lib/permissions.ts +1 -1
- package/lib/socket/instance.ts +42 -16
- package/package.json +1 -1
- package/types/index.ts +14 -6
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)
|
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
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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) => {
|
package/lib/permissions.ts
CHANGED
|
@@ -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/**/**.
|
|
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({
|
package/lib/socket/instance.ts
CHANGED
|
@@ -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:
|
|
22
|
+
on(event: string, cb: callbackType) {
|
|
14
23
|
wsEvents.push({
|
|
15
24
|
type: "on",
|
|
16
25
|
event: event,
|
|
@@ -20,7 +29,6 @@ class Io {
|
|
|
20
29
|
to(id: string) {
|
|
21
30
|
const newInstance = new Io();
|
|
22
31
|
newInstance.id = id;
|
|
23
|
-
|
|
24
32
|
return newInstance;
|
|
25
33
|
}
|
|
26
34
|
emit(event: string, data: any) {
|
|
@@ -55,12 +63,9 @@ export type socketIoType = {
|
|
|
55
63
|
id: string;
|
|
56
64
|
on: <T extends "connection" | "close" | string>(
|
|
57
65
|
event: T,
|
|
58
|
-
cb:
|
|
59
|
-
id: string;
|
|
60
|
-
emit: (event: string, data: any) => void;
|
|
61
|
-
}) => void
|
|
66
|
+
cb: callbackType
|
|
62
67
|
) => void;
|
|
63
|
-
emit: (event: string,
|
|
68
|
+
emit: (event: string, data: any) => void;
|
|
64
69
|
broadcast: (event: string, data: any) => void;
|
|
65
70
|
};
|
|
66
71
|
|
|
@@ -90,13 +95,31 @@ function webSocketServer(server: Server): WebSocketHandler {
|
|
|
90
95
|
});
|
|
91
96
|
},
|
|
92
97
|
close(ws, code, reason) {
|
|
93
|
-
|
|
94
|
-
|
|
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);
|
|
95
118
|
},
|
|
96
119
|
message: (ws, message: any) => {
|
|
97
120
|
try {
|
|
98
121
|
let options: optionsIo = JSON.parse(message);
|
|
99
|
-
|
|
122
|
+
//console.log(options);
|
|
100
123
|
// find all listeners : On
|
|
101
124
|
let evs = wsEvents.filter(
|
|
102
125
|
(e) => e.event == options?.event && e?.type == "on"
|
|
@@ -104,10 +127,11 @@ function webSocketServer(server: Server): WebSocketHandler {
|
|
|
104
127
|
|
|
105
128
|
// run all listeners function if matchs
|
|
106
129
|
evs?.map((ev) => {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
130
|
+
ws.token = options?.token || null;
|
|
131
|
+
if (ev && ev?.cb) {
|
|
132
|
+
ev.cb(
|
|
133
|
+
{
|
|
134
|
+
id: ws.id,
|
|
111
135
|
broadcast: (event: string, data: any) => {
|
|
112
136
|
// send to all clients
|
|
113
137
|
wsClients.forEach((client) => {
|
|
@@ -117,12 +141,14 @@ function webSocketServer(server: Server): WebSocketHandler {
|
|
|
117
141
|
emit: (event: string, data: any) =>
|
|
118
142
|
ws.send(JSON.stringify({ type: "emit", event, data })),
|
|
119
143
|
},
|
|
120
|
-
|
|
144
|
+
options.data
|
|
145
|
+
);
|
|
121
146
|
}
|
|
122
147
|
});
|
|
123
148
|
|
|
124
149
|
//wsEvents.on(options.event)
|
|
125
|
-
} catch (err) {
|
|
150
|
+
} catch (err: any) {
|
|
151
|
+
console.log(err?.message);
|
|
126
152
|
//console.log("Not a valid JSON string", err?.message);
|
|
127
153
|
}
|
|
128
154
|
},
|
package/package.json
CHANGED
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:
|
|
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
|
|
140
|
+
handler?: (ctx: cronCtx) => void;
|
|
141
|
+
exec?: (ctx: cronCtx) => void;
|
|
141
142
|
};
|
|
142
143
|
|
|
143
144
|
export type middlewareCtx = (ctx: {
|
|
@@ -364,12 +365,19 @@ export type Config = {
|
|
|
364
365
|
server: {
|
|
365
366
|
logger?: Boolean | loggerFunction;
|
|
366
367
|
whiteListIps?: Array<string>;
|
|
367
|
-
blackListIps?:
|
|
368
|
+
blackListIps?: string[];
|
|
368
369
|
cors?: {
|
|
369
|
-
origin:
|
|
370
|
+
origin:
|
|
371
|
+
| string[]
|
|
372
|
+
| ((ctx: { origin: string; c: Context }) => string[])
|
|
373
|
+
| string;
|
|
374
|
+
/**
|
|
375
|
+
* default true
|
|
376
|
+
@default true
|
|
377
|
+
*/
|
|
370
378
|
credentials?: boolean;
|
|
371
|
-
allowMethods?:
|
|
372
|
-
allowHeaders?:
|
|
379
|
+
allowMethods?: string[];
|
|
380
|
+
allowHeaders?: string[];
|
|
373
381
|
};
|
|
374
382
|
socket?: {
|
|
375
383
|
/**
|