@dnax/core 0.9.8 → 0.10.1
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 +17 -8
- package/lib/asyncLocalStorage.ts +39 -17
- package/lib/bento/index.ts +27 -1
- package/lib/session/index.ts +28 -0
- package/package.json +1 -1
- package/utils/index.ts +3 -1
package/app/hono.ts
CHANGED
|
@@ -10,6 +10,7 @@ import { serveStatic, getConnInfo } from "hono/bun";
|
|
|
10
10
|
import { consola } from "consola";
|
|
11
11
|
import { cors } from "hono/cors";
|
|
12
12
|
import { asyncLocalStorage, sessionStorage } from "../lib/asyncLocalStorage";
|
|
13
|
+
import { localSession } from "../lib/session";
|
|
13
14
|
import {
|
|
14
15
|
cleanPath,
|
|
15
16
|
contextError,
|
|
@@ -150,10 +151,16 @@ function HonoInstance(): typeof app {
|
|
|
150
151
|
let session = sessionStorage();
|
|
151
152
|
var token = jwt.getToken("Bearer", c.req.header()["authorization"]) || "";
|
|
152
153
|
var { decode, valid, error } = jwt.verify(token);
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
154
|
+
let sessionData = {};
|
|
155
|
+
|
|
156
|
+
if (valid && decode) {
|
|
157
|
+
await localSession
|
|
158
|
+
.get(decode?.sessionId)
|
|
159
|
+
.then((data) => {
|
|
160
|
+
//console.log("Data", data);
|
|
161
|
+
sessionData = data;
|
|
162
|
+
})
|
|
163
|
+
.catch();
|
|
157
164
|
}
|
|
158
165
|
|
|
159
166
|
let _v = {
|
|
@@ -164,21 +171,23 @@ function HonoInstance(): typeof app {
|
|
|
164
171
|
c.req.raw.headers?.get("x-real-ip"),
|
|
165
172
|
isAuth: valid ? true : false,
|
|
166
173
|
reqAt: moment().format().toString(),
|
|
167
|
-
setAt:
|
|
174
|
+
setAt: sessionData?._v?.setAt || null,
|
|
168
175
|
};
|
|
169
176
|
c.set("_v", _v);
|
|
170
177
|
c.set("tenant-id", c.req.header()["tenant-id"]);
|
|
171
178
|
|
|
172
179
|
if (token && valid) {
|
|
173
180
|
session.set({
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
181
|
+
generateToken: false,
|
|
182
|
+
token: token,
|
|
183
|
+
state: sessionData?.state || {},
|
|
184
|
+
role: sessionData?.role || null,
|
|
177
185
|
_v: { ...(_v || {}) },
|
|
178
186
|
});
|
|
179
187
|
} else {
|
|
180
188
|
// no valid token
|
|
181
189
|
session.set({
|
|
190
|
+
generateToken: false,
|
|
182
191
|
state: {},
|
|
183
192
|
role: undefined,
|
|
184
193
|
_v: { ...(_v || {}) },
|
package/lib/asyncLocalStorage.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
2
|
import type { sessionCtx } from "../types";
|
|
3
|
+
import { localSession } from "./session";
|
|
4
|
+
import { v4 } from "uuid";
|
|
3
5
|
import { jwt } from "../utils";
|
|
4
6
|
const asyncLocalStorage = new AsyncLocalStorage();
|
|
5
7
|
const key = "___sessionStorage___";
|
|
@@ -8,8 +10,8 @@ const sessionStorage = () => ({
|
|
|
8
10
|
get(): {
|
|
9
11
|
state: object;
|
|
10
12
|
_v: object;
|
|
11
|
-
|
|
12
13
|
role: string | null | undefined;
|
|
14
|
+
token?: string;
|
|
13
15
|
} {
|
|
14
16
|
let store = asyncLocalStorage.getStore() as InstanceType<typeof Map>;
|
|
15
17
|
return (
|
|
@@ -26,35 +28,55 @@ const sessionStorage = () => ({
|
|
|
26
28
|
},
|
|
27
29
|
set(
|
|
28
30
|
input: {
|
|
31
|
+
id: string;
|
|
29
32
|
state: object;
|
|
30
33
|
_v?: object;
|
|
31
34
|
role?: string;
|
|
32
|
-
token?: string
|
|
35
|
+
token?: string;
|
|
36
|
+
expiresIn?: string;
|
|
37
|
+
generateToken?: boolean;
|
|
33
38
|
} = {
|
|
39
|
+
id: "",
|
|
34
40
|
state: {},
|
|
35
41
|
_v: {},
|
|
36
|
-
|
|
42
|
+
generateToken: true,
|
|
37
43
|
}
|
|
38
|
-
) {
|
|
44
|
+
): any {
|
|
45
|
+
input.generateToken = input?.generateToken ?? true;
|
|
39
46
|
let store = asyncLocalStorage.getStore() as InstanceType<typeof Map>;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
});
|
|
47
|
+
if (!input?.id) {
|
|
48
|
+
input.id = v4();
|
|
49
|
+
}
|
|
44
50
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
+
if (input?.generateToken) {
|
|
52
|
+
input.token =
|
|
53
|
+
input.token ||
|
|
54
|
+
jwt.sign(input.id, {
|
|
55
|
+
expiresIn: input?.expiresIn || "7d",
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
let data = {
|
|
60
|
+
token: input?.token || null,
|
|
61
|
+
state: input.state,
|
|
62
|
+
role: input?.role || null,
|
|
51
63
|
_v: {
|
|
52
64
|
...(input?._v || {}),
|
|
53
65
|
setAt: new Date().toString(),
|
|
54
66
|
},
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
if (input?.generateToken) {
|
|
70
|
+
localSession.set(input?.id, data);
|
|
71
|
+
}
|
|
72
|
+
store.set(key, data);
|
|
73
|
+
return {
|
|
74
|
+
state: input.state,
|
|
75
|
+
_v: input._v,
|
|
76
|
+
role: input.role,
|
|
77
|
+
token: input.token,
|
|
78
|
+
id: input.id,
|
|
79
|
+
};
|
|
58
80
|
},
|
|
59
81
|
});
|
|
60
82
|
|
package/lib/bento/index.ts
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
import { BentoCache, bentostore } from "bentocache";
|
|
2
2
|
import { memoryDriver } from "bentocache/drivers/memory";
|
|
3
|
+
import { fileDriver } from "bentocache/drivers/file";
|
|
4
|
+
import { v4 } from "uuid";
|
|
5
|
+
import fs from "fs-extra";
|
|
6
|
+
import path from "path";
|
|
7
|
+
const cacheDirectory = path.join(process.cwd(), "/.cache/system/");
|
|
8
|
+
|
|
9
|
+
if (!fs?.existsSync(cacheDirectory + "bentocache")) {
|
|
10
|
+
fs.mkdirSync(cacheDirectory + "bentocache", {
|
|
11
|
+
recursive: true,
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
const systemCache = new BentoCache({
|
|
15
|
+
default: "systemCache",
|
|
16
|
+
stores: {
|
|
17
|
+
systemCache: bentostore().useL2Layer(
|
|
18
|
+
fileDriver({
|
|
19
|
+
directory: cacheDirectory,
|
|
20
|
+
pruneInterval: "1h",
|
|
21
|
+
//maxSize: 100 * 1024 * 1024,
|
|
22
|
+
})
|
|
23
|
+
),
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
3
27
|
const bentoCache = new BentoCache({
|
|
4
28
|
default: "dnaxCache",
|
|
5
29
|
stores: {
|
|
@@ -27,4 +51,6 @@ function bentoKey(data: object | string) {
|
|
|
27
51
|
return key;
|
|
28
52
|
}
|
|
29
53
|
|
|
30
|
-
|
|
54
|
+
const sessionCache = systemCache.namespace("tokens");
|
|
55
|
+
|
|
56
|
+
export { bentoCache, bentoKey, systemCache, sessionCache };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { v4 } from "uuid";
|
|
2
|
+
import { sessionCache } from "../bento";
|
|
3
|
+
|
|
4
|
+
const localSession = {
|
|
5
|
+
get: async (id: string) => {
|
|
6
|
+
return await sessionCache.get(id);
|
|
7
|
+
},
|
|
8
|
+
set: (
|
|
9
|
+
id: string,
|
|
10
|
+
data: object,
|
|
11
|
+
options = {
|
|
12
|
+
ttl: "7d",
|
|
13
|
+
}
|
|
14
|
+
) => {
|
|
15
|
+
sessionCache.set({
|
|
16
|
+
key: id,
|
|
17
|
+
value: data,
|
|
18
|
+
// ttl: "1m",
|
|
19
|
+
ttl: options?.ttl || "7d",
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
data: data,
|
|
24
|
+
};
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export { localSession };
|
package/package.json
CHANGED
package/utils/index.ts
CHANGED